blob: b1d087e5e205f6afb0f48393c1bbc452f95c3e6c [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
Christoph Hellwig7b51e702020-12-10 08:55:44 +01005 * Copyright (C) 2016 - 2020 Christoph Hellwig
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/slab.h>
11#include <linux/kmod.h>
12#include <linux/major.h>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070013#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/blkdev.h>
Christoph Hellwigfe45e632021-09-20 14:33:27 +020015#include <linux/blk-integrity.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040016#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/module.h>
18#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070019#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040021#include <linux/swap.h>
David Howells811d7362006-08-29 19:06:09 +010022#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000024#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/uio.h>
26#include <linux/namei.h>
Al Viroff01bb42011-09-16 02:31:11 -040027#include <linux/cleancache.h>
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +010028#include <linux/part_stat.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Christoph Hellwig0dca4462021-09-07 16:13:03 +020030#include "../fs/internal.h"
31#include "blk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33struct bdev_inode {
34 struct block_device bdev;
35 struct inode vfs_inode;
36};
37
38static inline struct bdev_inode *BDEV_I(struct inode *inode)
39{
40 return container_of(inode, struct bdev_inode, vfs_inode);
41}
42
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020043struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044{
45 return &BDEV_I(inode)->bdev;
46}
Linus Torvalds1da177e2005-04-16 15:20:36 -070047EXPORT_SYMBOL(I_BDEV);
48
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070049static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010050{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070051 struct inode *inode = bdev->bd_inode;
52 int ret;
53
Christoph Hellwig564f00f2015-01-14 10:42:33 +010054 spin_lock(&inode->i_lock);
55 while (inode->i_state & I_DIRTY) {
56 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070057 ret = write_inode_now(inode, true);
58 if (ret) {
59 char name[BDEVNAME_SIZE];
60 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
61 "for block device %s (err=%d).\n",
62 bdevname(bdev, name), ret);
63 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010064 spin_lock(&inode->i_lock);
65 }
66 spin_unlock(&inode->i_lock);
67}
68
Peter Zijlstraf9a14392007-05-06 14:49:55 -070069/* Kill _all_ buffers and pagecache , dirty or not.. */
Zheng Bin3373a342020-06-18 12:21:38 +080070static void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Al Viroff01bb42011-09-16 02:31:11 -040072 struct address_space *mapping = bdev->bd_inode->i_mapping;
73
Matthew Wilcox (Oracle)77165062021-05-04 18:32:45 -070074 if (mapping_empty(mapping))
Peter Zijlstraf9a14392007-05-06 14:49:55 -070075 return;
Al Viroff01bb42011-09-16 02:31:11 -040076
Peter Zijlstraf9a14392007-05-06 14:49:55 -070077 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040078 truncate_inode_pages(mapping, 0);
Zheng Bin3373a342020-06-18 12:21:38 +080079}
Al Viroff01bb42011-09-16 02:31:11 -040080
81/* Invalidate clean unused buffers and pagecache. */
82void invalidate_bdev(struct block_device *bdev)
83{
84 struct address_space *mapping = bdev->bd_inode->i_mapping;
85
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070086 if (mapping->nrpages) {
87 invalidate_bh_lrus();
88 lru_add_drain_all(); /* make sure all lru add caches are flushed */
89 invalidate_mapping_pages(mapping, 0, -1);
90 }
Al Viroff01bb42011-09-16 02:31:11 -040091 /* 99% of the time, we don't need to flush the cleancache on the bdev.
92 * But, for the strange corners, lets be cautious
93 */
Dan Magenheimer31677602011-09-21 11:56:28 -040094 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -040095}
96EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Jan Kara384d87e2020-09-04 10:58:52 +020098/*
99 * Drop all buffers & page cache for given bdev range. This function bails
100 * with error if bdev has other exclusive owner (such as filesystem).
101 */
102int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
103 loff_t lstart, loff_t lend)
104{
Jan Kara384d87e2020-09-04 10:58:52 +0200105 /*
106 * If we don't hold exclusive handle for the device, upgrade to it
107 * while we discard the buffer cache to avoid discarding buffers
108 * under live filesystem.
109 */
110 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100111 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200112 if (err)
Jan Kara56887cf2021-02-22 10:48:09 +0100113 goto invalidate;
Jan Kara384d87e2020-09-04 10:58:52 +0200114 }
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100115
Jan Kara384d87e2020-09-04 10:58:52 +0200116 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100117 if (!(mode & FMODE_EXCL))
118 bd_abort_claiming(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200119 return 0;
Jan Kara56887cf2021-02-22 10:48:09 +0100120
121invalidate:
122 /*
123 * Someone else has handle exclusively open. Try invalidating instead.
124 * The 'end' argument is inclusive so the rounding is safe.
125 */
126 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
127 lstart >> PAGE_SHIFT,
128 lend >> PAGE_SHIFT);
Jan Kara384d87e2020-09-04 10:58:52 +0200129}
Jan Kara384d87e2020-09-04 10:58:52 +0200130
Jan Kara04906b22019-01-14 09:48:10 +0100131static void set_init_blocksize(struct block_device *bdev)
132{
Maxim Mikityanskiy8dc932d2021-01-26 21:59:07 +0200133 unsigned int bsize = bdev_logical_block_size(bdev);
134 loff_t size = i_size_read(bdev->bd_inode);
135
136 while (bsize < PAGE_SIZE) {
137 if (size & bsize)
138 break;
139 bsize <<= 1;
140 }
141 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
Jan Kara04906b22019-01-14 09:48:10 +0100142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int set_blocksize(struct block_device *bdev, int size)
145{
146 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700147 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149
150 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400151 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return -EINVAL;
153
154 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200155 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 bdev->bd_inode->i_blkbits = blksize_bits(size);
158 kill_bdev(bdev);
159 }
160 return 0;
161}
162
163EXPORT_SYMBOL(set_blocksize);
164
165int sb_set_blocksize(struct super_block *sb, int size)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (set_blocksize(sb->s_bdev, size))
168 return 0;
169 /* If we get here, we know size is power of two
170 * and it's value is between 512 and PAGE_SIZE */
171 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800172 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return sb->s_blocksize;
174}
175
176EXPORT_SYMBOL(sb_set_blocksize);
177
178int sb_min_blocksize(struct super_block *sb, int size)
179{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400180 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (size < minsize)
182 size = minsize;
183 return sb_set_blocksize(sb, size);
184}
185
186EXPORT_SYMBOL(sb_min_blocksize);
187
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200188int sync_blockdev_nowait(struct block_device *bdev)
Jan Kara5cee5812009-04-27 16:43:51 +0200189{
190 if (!bdev)
191 return 0;
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200192 return filemap_flush(bdev->bd_inode->i_mapping);
Jan Kara5cee5812009-04-27 16:43:51 +0200193}
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200194EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
Jan Kara5cee5812009-04-27 16:43:51 +0200195
Nick Piggin585d3bc2009-02-25 10:44:19 +0100196/*
197 * Write out and wait upon all the dirty data associated with a block
198 * device via its mapping. Does not take the superblock lock.
199 */
200int sync_blockdev(struct block_device *bdev)
201{
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200202 if (!bdev)
203 return 0;
204 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100205}
206EXPORT_SYMBOL(sync_blockdev);
207
208/*
209 * Write out and wait upon all dirty data associated with this
210 * device. Filesystem data as well as the underlying block
211 * device. Takes the superblock lock.
212 */
213int fsync_bdev(struct block_device *bdev)
214{
215 struct super_block *sb = get_super(bdev);
216 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200217 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100218 drop_super(sb);
219 return res;
220 }
221 return sync_blockdev(bdev);
222}
Al Viro47e44912009-04-01 07:07:16 -0400223EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100224
225/**
226 * freeze_bdev -- lock a filesystem and force it into a consistent state
227 * @bdev: blockdevice to lock
228 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100229 * If a superblock is found on this device, we take the s_umount semaphore
230 * on it to make sure nobody unmounts until the snapshot creation is done.
231 * The reference counter (bd_fsfreeze_count) guarantees that only the last
232 * unfreeze process can unfreeze the frozen filesystem actually when multiple
233 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
234 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
235 * actually.
236 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100237int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100238{
239 struct super_block *sb;
240 int error = 0;
241
242 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100243 if (++bdev->bd_fsfreeze_count > 1)
244 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100245
Christoph Hellwig45042302009-08-03 23:28:35 +0200246 sb = get_active_super(bdev);
247 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100248 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600249 if (sb->s_op->freeze_super)
250 error = sb->s_op->freeze_super(sb);
251 else
252 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400253 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100254
255 if (error) {
256 bdev->bd_fsfreeze_count--;
257 goto done;
258 }
259 bdev->bd_fsfreeze_sb = sb;
260
261sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100262 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100263done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100264 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100265 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100266}
267EXPORT_SYMBOL(freeze_bdev);
268
269/**
270 * thaw_bdev -- unlock filesystem
271 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100272 *
273 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
274 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100275int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100276{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100277 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200278 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100279
280 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200281 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400282 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100283
Christoph Hellwig45042302009-08-03 23:28:35 +0200284 error = 0;
285 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400286 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100287
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100288 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200289 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400290 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200291
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600292 if (sb->s_op->thaw_super)
293 error = sb->s_op->thaw_super(sb);
294 else
295 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200296 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400297 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000298 else
299 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400300out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100301 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200302 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100303}
304EXPORT_SYMBOL(thaw_bdev);
305
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700306/**
307 * bdev_read_page() - Start reading a page from a block device
308 * @bdev: The device to read the page from
309 * @sector: The offset on the device to read the page to (need not be aligned)
310 * @page: The page to read
311 *
312 * On entry, the page should be locked. It will be unlocked when the page
313 * has been read. If the block driver implements rw_page synchronously,
314 * that will be true on exit from this function, but it need not be.
315 *
316 * Errors returned by this function are usually "soft", eg out of memory, or
317 * queue full; callers should try a different route to read this page rather
318 * than propagate an error back up the stack.
319 *
320 * Return: negative errno if an error occurs, 0 if submission was successful.
321 */
322int bdev_read_page(struct block_device *bdev, sector_t sector,
323 struct page *page)
324{
325 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800326 int result = -EOPNOTSUPP;
327
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400328 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800329 return result;
330
Pavel Begunkov025a3862021-10-14 15:03:27 +0100331 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800332 if (result)
333 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700334 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
335 REQ_OP_READ);
Pavel Begunkov025a3862021-10-14 15:03:27 +0100336 blk_queue_exit(bdev_get_queue(bdev));
Dan Williams2e6edc952015-11-19 13:29:28 -0800337 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700338}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700339
340/**
341 * bdev_write_page() - Start writing a page to a block device
342 * @bdev: The device to write the page to
343 * @sector: The offset on the device to write the page to (need not be aligned)
344 * @page: The page to write
345 * @wbc: The writeback_control for the write
346 *
347 * On entry, the page should be locked and not currently under writeback.
348 * On exit, if the write started successfully, the page will be unlocked and
349 * under writeback. If the write failed already (eg the driver failed to
350 * queue the page to the device), the page will still be locked. If the
351 * caller is a ->writepage implementation, it will need to unlock the page.
352 *
353 * Errors returned by this function are usually "soft", eg out of memory, or
354 * queue full; callers should try a different route to write this page rather
355 * than propagate an error back up the stack.
356 *
357 * Return: negative errno if an error occurs, 0 if submission was successful.
358 */
359int bdev_write_page(struct block_device *bdev, sector_t sector,
360 struct page *page, struct writeback_control *wbc)
361{
362 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700363 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800364
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400365 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700366 return -EOPNOTSUPP;
Pavel Begunkov025a3862021-10-14 15:03:27 +0100367 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800368 if (result)
369 return result;
370
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700371 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700372 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
373 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700374 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700375 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700376 } else {
377 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700378 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700379 }
Pavel Begunkov025a3862021-10-14 15:03:27 +0100380 blk_queue_exit(bdev_get_queue(bdev));
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700381 return result;
382}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
385 * pseudo-fs
386 */
387
388static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800389static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391static struct inode *bdev_alloc_inode(struct super_block *sb)
392{
Christoph Lametere94b1762006-12-06 20:33:17 -0800393 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (!ei)
396 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100397 memset(&ei->bdev, 0, sizeof(ei->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return &ei->vfs_inode;
399}
400
Al Viro41149cb2019-04-10 15:12:38 -0400401static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100403 struct block_device *bdev = I_BDEV(inode);
404
405 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100406 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100407
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200408 if (!bdev_is_partition(bdev)) {
409 if (bdev->bd_disk && bdev->bd_disk->bdi)
410 bdi_put(bdev->bd_disk->bdi);
Christoph Hellwig340e8452021-07-22 09:53:54 +0200411 kfree(bdev->bd_disk);
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200412 }
Christoph Hellwig9451aa02021-08-16 14:26:13 +0200413
414 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
415 blk_free_ext_minor(MINOR(bdev->bd_dev));
416
Al Viro41149cb2019-04-10 15:12:38 -0400417 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100418}
419
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100420static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100422 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Christoph Lametera35afb82007-05-16 22:10:57 -0700424 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425}
426
Al Virob57922d2010-06-07 14:34:48 -0400427static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700429 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400430 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200431 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800434static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 .statfs = simple_statfs,
436 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400437 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400439 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440};
441
David Howells9030d162019-03-25 16:38:23 +0000442static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
David Howells9030d162019-03-25 16:38:23 +0000444 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
445 if (!ctx)
446 return -ENOMEM;
447 fc->s_iflags |= SB_I_CGROUPWB;
448 ctx->ops = &bdev_sops;
449 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
452static struct file_system_type bd_type = {
453 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000454 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 .kill_sb = kill_anon_super,
456};
457
Tejun Heoa212b102015-05-22 17:13:33 -0400458struct super_block *blockdev_superblock __read_mostly;
459EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461void __init bdev_cache_init(void)
462{
463 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300464 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800467 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800468 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900469 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 err = register_filesystem(&bd_type);
471 if (err)
472 panic("Cannot register bdev pseudo-fs");
473 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (IS_ERR(bd_mnt))
475 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300476 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100479struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct block_device *bdev;
482 struct inode *inode;
483
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100484 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 if (!inode)
486 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100487 inode->i_mode = S_IFBLK;
488 inode->i_rdev = 0;
489 inode->i_data.a_ops = &def_blk_aops;
490 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100492 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100493 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100494 spin_lock_init(&bdev->bd_size_lock);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100495 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100496 bdev->bd_inode = inode;
Pavel Begunkov17220ca2021-10-14 15:03:26 +0100497 bdev->bd_queue = disk->queue;
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100498 bdev->bd_stats = alloc_percpu(struct disk_stats);
499 if (!bdev->bd_stats) {
500 iput(inode);
501 return NULL;
502 }
Tetsuo Handa06cc9782021-10-02 18:23:02 +0900503 bdev->bd_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return bdev;
505}
506
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100507void bdev_add(struct block_device *bdev, dev_t dev)
508{
509 bdev->bd_dev = dev;
510 bdev->bd_inode->i_rdev = dev;
511 bdev->bd_inode->i_ino = dev;
512 insert_inode_hash(bdev->bd_inode);
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515long nr_blockdev_pages(void)
516{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200517 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200519
520 spin_lock(&blockdev_superblock->s_inode_list_lock);
521 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
522 ret += inode->i_mapping->nrpages;
523 spin_unlock(&blockdev_superblock->s_inode_list_lock);
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 return ret;
526}
527
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900528/**
529 * bd_may_claim - test whether a block device can be claimed
530 * @bdev: block device of interest
531 * @whole: whole block device containing @bdev, may equal @bdev
532 * @holder: holder trying to claim @bdev
533 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300534 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900535 *
536 * CONTEXT:
537 * spin_lock(&bdev_lock).
538 *
539 * RETURNS:
540 * %true if @bdev can be claimed, %false otherwise.
541 */
542static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
543 void *holder)
544{
545 if (bdev->bd_holder == holder)
546 return true; /* already a holder */
547 else if (bdev->bd_holder != NULL)
548 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700549 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900550 return true; /* is a whole device which isn't held */
551
Tejun Heoe525fd82010-11-13 11:55:17 +0100552 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900553 return true; /* is a partition of a device that is being partitioned */
554 else if (whole->bd_holder != NULL)
555 return false; /* is a partition of a held device */
556 else
557 return true; /* is a partition of an un-held device */
558}
559
560/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200561 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +0900562 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +0900563 * @holder: holder trying to claim @bdev
564 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200565 * Claim @bdev. This function fails if @bdev is already claimed by another
566 * holder and waits if another claiming is in progress. return, the caller
567 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +0900568 *
569 * RETURNS:
570 * 0 if @bdev can be claimed, -EBUSY otherwise.
571 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100572int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +0900573{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100574 struct block_device *whole = bdev_whole(bdev);
575
576 if (WARN_ON_ONCE(!holder))
577 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +0900578retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200579 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900580 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200581 if (!bd_may_claim(bdev, whole, holder)) {
582 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900583 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200584 }
Tejun Heo6b4517a2010-04-07 18:53:59 +0900585
Tejun Heoe75aa852010-08-04 17:59:39 +0200586 /* if claiming is already in progress, wait for it to finish */
587 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900588 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
589 DEFINE_WAIT(wait);
590
591 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
592 spin_unlock(&bdev_lock);
593 schedule();
594 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900595 goto retry;
596 }
597
598 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200599 whole->bd_claiming = holder;
600 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900601 return 0;
602}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +0200603EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +0900604
Jan Kara89e524c02019-07-30 13:10:14 +0200605static void bd_clear_claiming(struct block_device *whole, void *holder)
606{
607 lockdep_assert_held(&bdev_lock);
608 /* tell others that we're done */
609 BUG_ON(whole->bd_claiming != holder);
610 whole->bd_claiming = NULL;
611 wake_up_bit(&whole->bd_claiming, 0);
612}
613
614/**
615 * bd_finish_claiming - finish claiming of a block device
616 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200617 * @holder: holder that has claimed @bdev
618 *
619 * Finish exclusive open of a block device. Mark the device as exlusively
620 * open by the holder and wake up all waiters for exclusive open to finish.
621 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100622static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200623{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100624 struct block_device *whole = bdev_whole(bdev);
625
Jan Kara89e524c02019-07-30 13:10:14 +0200626 spin_lock(&bdev_lock);
627 BUG_ON(!bd_may_claim(bdev, whole, holder));
628 /*
629 * Note that for a whole device bd_holders will be incremented twice,
630 * and bd_holder will be set to bd_may_claim before being set to holder
631 */
632 whole->bd_holders++;
633 whole->bd_holder = bd_may_claim;
634 bdev->bd_holders++;
635 bdev->bd_holder = holder;
636 bd_clear_claiming(whole, holder);
637 spin_unlock(&bdev_lock);
638}
Jan Kara89e524c02019-07-30 13:10:14 +0200639
640/**
641 * bd_abort_claiming - abort claiming of a block device
642 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200643 * @holder: holder that has claimed @bdev
644 *
645 * Abort claiming of a block device when the exclusive open failed. This can be
646 * also used when exclusive open is not actually desired and we just needed
647 * to block other exclusive openers for a while.
648 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100649void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200650{
651 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100652 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +0200653 spin_unlock(&bdev_lock);
654}
655EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900656
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200657static void blkdev_flush_mapping(struct block_device *bdev)
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100658{
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200659 WARN_ON_ONCE(bdev->bd_holders);
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200660 sync_blockdev(bdev);
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200661 kill_bdev(bdev);
662 bdev_write_inode(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100663}
664
Christoph Hellwig362529d2021-05-25 08:12:54 +0200665static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100667 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100668 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Christoph Hellwig362529d2021-05-25 08:12:54 +0200670 if (disk->fops->open) {
671 ret = disk->fops->open(bdev, mode);
672 if (ret) {
673 /* avoid ghost partitions on a removed medium */
674 if (ret == -ENOMEDIUM &&
675 test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200676 bdev_disk_changed(disk, true);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200677 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200681 if (!bdev->bd_openers)
Christoph Hellwig362529d2021-05-25 08:12:54 +0200682 set_init_blocksize(bdev);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200683 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200684 bdev_disk_changed(disk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 bdev->bd_openers++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200686 return 0;;
687}
688
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200689static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
690{
691 if (!--bdev->bd_openers)
692 blkdev_flush_mapping(bdev);
693 if (bdev->bd_disk->fops->release)
694 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
695}
696
Christoph Hellwig362529d2021-05-25 08:12:54 +0200697static int blkdev_get_part(struct block_device *part, fmode_t mode)
698{
699 struct gendisk *disk = part->bd_disk;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200700 int ret;
701
702 if (part->bd_openers)
703 goto done;
704
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200705 ret = blkdev_get_whole(bdev_whole(part), mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200706 if (ret)
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200707 return ret;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200708
709 ret = -ENXIO;
710 if (!bdev_nr_sectors(part))
711 goto out_blkdev_put;
712
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200713 disk->open_partitions++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200714 set_init_blocksize(part);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200715done:
716 part->bd_openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200718
719out_blkdev_put:
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200720 blkdev_put_whole(bdev_whole(part), mode);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200721 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
723
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200724static void blkdev_put_part(struct block_device *part, fmode_t mode)
725{
726 struct block_device *whole = bdev_whole(part);
727
728 if (--part->bd_openers)
729 return;
730 blkdev_flush_mapping(part);
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200731 whole->bd_disk->open_partitions--;
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200732 blkdev_put_whole(whole, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100735struct block_device *blkdev_get_no_open(dev_t dev)
736{
737 struct block_device *bdev;
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200738 struct inode *inode;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100739
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200740 inode = ilookup(blockdev_superblock, dev);
741 if (!inode) {
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100742 blk_request_module(dev);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200743 inode = ilookup(blockdev_superblock, dev);
744 if (!inode)
Christoph Hellwig6c60ff02021-05-14 15:18:41 +0200745 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100746 }
747
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200748 /* switch from the inode reference to a device mode one: */
749 bdev = &BDEV_I(inode)->bdev;
750 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
751 bdev = NULL;
752 iput(inode);
753
754 if (!bdev)
755 return NULL;
Ming Leiefcf5932021-11-11 10:03:43 +0800756 if ((bdev->bd_disk->flags & GENHD_FL_HIDDEN)) {
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200757 put_device(&bdev->bd_device);
758 return NULL;
759 }
760
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100761 return bdev;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100762}
763
764void blkdev_put_no_open(struct block_device *bdev)
765{
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200766 put_device(&bdev->bd_device);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100767}
768
Tejun Heod4d77622010-11-13 11:55:18 +0100769/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100770 * blkdev_get_by_dev - open a block device by device number
771 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +0100772 * @mode: FMODE_* mask
773 * @holder: exclusive holder identifier
774 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100775 * Open the block device described by device number @dev. If @mode includes
776 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
777 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
778 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100779 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100780 * Use this interface ONLY if you really do not have anything better - i.e. when
781 * you are behind a truly sucky interface and all you are given is a device
782 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +0100783 *
784 * CONTEXT:
785 * Might sleep.
786 *
787 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100788 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100789 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100790struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100792 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100793 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100794 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100795 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +0100796
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100797 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100798 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100799 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
800 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200801 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100802 return ERR_PTR(ret);
803
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100804 bdev = blkdev_get_no_open(dev);
805 if (!bdev)
806 return ERR_PTR(-ENXIO);
807 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200808
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100809 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100810 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100811 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100812 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100813 }
814
815 disk_block_events(disk);
816
Christoph Hellwiga8698702021-05-25 08:12:56 +0200817 mutex_lock(&disk->open_mutex);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200818 ret = -ENXIO;
Christoph Hellwig50b4aec2021-08-09 08:40:28 +0200819 if (!disk_live(disk))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200820 goto abort_claiming;
Ming Leiefcf5932021-11-11 10:03:43 +0800821 if (!try_module_get(disk->fops->owner))
822 goto abort_claiming;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200823 if (bdev_is_partition(bdev))
824 ret = blkdev_get_part(bdev, mode);
825 else
826 ret = blkdev_get_whole(bdev, mode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100827 if (ret)
Ming Leiefcf5932021-11-11 10:03:43 +0800828 goto put_module;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100829 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100830 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100831
832 /*
833 * Block event polling for write claims if requested. Any write
834 * holder makes the write_holder state stick until all are
835 * released. This is good enough and tracking individual
836 * writeable reference is too fragile given the way @mode is
837 * used in blkdev_get/put().
838 */
839 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
840 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
841 bdev->bd_write_holder = true;
842 unblock_events = false;
843 }
844 }
Christoph Hellwiga8698702021-05-25 08:12:56 +0200845 mutex_unlock(&disk->open_mutex);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100846
847 if (unblock_events)
848 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100849 return bdev;
Ming Leiefcf5932021-11-11 10:03:43 +0800850put_module:
851 module_put(disk->fops->owner);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100852abort_claiming:
853 if (mode & FMODE_EXCL)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100854 bd_abort_claiming(bdev, holder);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200855 mutex_unlock(&disk->open_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100856 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100857put_blkdev:
858 blkdev_put_no_open(bdev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100859 return ERR_PTR(ret);
NeilBrown37be4122006-12-08 02:36:16 -0800860}
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100861EXPORT_SYMBOL(blkdev_get_by_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Tejun Heod4d77622010-11-13 11:55:18 +0100863/**
864 * blkdev_get_by_path - open a block device by name
865 * @path: path to the block device to open
866 * @mode: FMODE_* mask
867 * @holder: exclusive holder identifier
868 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100869 * Open the block device described by the device file at @path. If @mode
870 * includes %FMODE_EXCL, the block device is opened with exclusive access.
871 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
872 * nest for the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100873 *
874 * CONTEXT:
875 * Might sleep.
876 *
877 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100878 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100879 */
880struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
881 void *holder)
882{
883 struct block_device *bdev;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100884 dev_t dev;
885 int error;
Tejun Heod4d77622010-11-13 11:55:18 +0100886
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100887 error = lookup_bdev(path, &dev);
888 if (error)
889 return ERR_PTR(error);
Tejun Heod4d77622010-11-13 11:55:18 +0100890
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100891 bdev = blkdev_get_by_dev(dev, mode, holder);
892 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
Chuck Ebberte51900f2011-02-16 18:11:53 -0500893 blkdev_put(bdev, mode);
894 return ERR_PTR(-EACCES);
895 }
896
Tejun Heod4d77622010-11-13 11:55:18 +0100897 return bdev;
898}
899EXPORT_SYMBOL(blkdev_get_by_path);
900
Al Viro4385bab2013-05-05 22:11:03 -0400901void blkdev_put(struct block_device *bdev, fmode_t mode)
Al Viro572c4892007-10-08 13:24:05 -0400902{
903 struct gendisk *disk = bdev->bd_disk;
Al Viro572c4892007-10-08 13:24:05 -0400904
905 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * Sync early if it looks like we're the last one. If someone else
Pavel Emelianov6a2aae02006-10-28 10:38:33 -0700907 * opens the block device between now and the decrement of bd_openers
908 * then we did a sync that we didn't need to, but that's not the end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * of the world and we want to avoid long (could be several minute)
Al Viro572c4892007-10-08 13:24:05 -0400910 * syncs while holding the mutex.
911 */
Tejun Heoe525fd82010-11-13 11:55:17 +0100912 if (bdev->bd_openers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 sync_blockdev(bdev);
914
Christoph Hellwiga8698702021-05-25 08:12:56 +0200915 mutex_lock(&disk->open_mutex);
Tejun Heoe525fd82010-11-13 11:55:17 +0100916 if (mode & FMODE_EXCL) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100917 struct block_device *whole = bdev_whole(bdev);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100918 bool bdev_free;
919
920 /*
921 * Release a claim on the device. The holder fields
Christoph Hellwiga8698702021-05-25 08:12:56 +0200922 * are protected with bdev_lock. open_mutex is to
Tejun Heo6a027ef2010-11-13 11:55:17 +0100923 * synchronize disk_holder unlinking.
924 */
Tejun Heo6a027ef2010-11-13 11:55:17 +0100925 spin_lock(&bdev_lock);
926
927 WARN_ON_ONCE(--bdev->bd_holders < 0);
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100928 WARN_ON_ONCE(--whole->bd_holders < 0);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100929
Tejun Heo6a027ef2010-11-13 11:55:17 +0100930 if ((bdev_free = !bdev->bd_holders))
931 bdev->bd_holder = NULL;
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100932 if (!whole->bd_holders)
933 whole->bd_holder = NULL;
Tejun Heo6a027ef2010-11-13 11:55:17 +0100934
935 spin_unlock(&bdev_lock);
936
Tejun Heo77ea8872010-12-08 20:57:37 +0100937 /*
938 * If this was the last claim, remove holder link and
939 * unblock evpoll if it was a write holder.
940 */
Tejun Heo85ef06d2011-07-01 16:17:47 +0200941 if (bdev_free && bdev->bd_write_holder) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100942 disk_unblock_events(disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200943 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +0100944 }
Tejun Heo69362172011-03-09 19:54:27 +0100945 }
Tejun Heo77ea8872010-12-08 20:57:37 +0100946
Tejun Heo85ef06d2011-07-01 16:17:47 +0200947 /*
948 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
949 * event. This is to ensure detection of media removal commanded
950 * from userland - e.g. eject(1).
951 */
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100952 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200953
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200954 if (bdev_is_partition(bdev))
955 blkdev_put_part(bdev, mode);
956 else
957 blkdev_put_whole(bdev, mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200958 mutex_unlock(&disk->open_mutex);
959
Ming Leiefcf5932021-11-11 10:03:43 +0800960 module_put(disk->fops->owner);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100961 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -0800962}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800963EXPORT_SYMBOL(blkdev_put);
964
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965/**
966 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -0800967 * @pathname: special file representing the block device
Randy Dunlap875b2372020-12-28 19:47:06 -0800968 * @dev: return value of the block device's dev_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 *
Jackie Liu057178c2021-10-21 15:13:43 +0800970 * Lookup the block device's dev_t at @pathname in the current
971 * namespace if possible and return it by @dev.
972 *
973 * RETURNS:
974 * 0 if succeeded, errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100976int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -0400979 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 int error;
981
Al Viro421748e2008-08-02 01:04:36 -0400982 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100983 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Al Viro421748e2008-08-02 01:04:36 -0400985 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100987 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988
David Howellsbb6687342015-03-17 22:26:21 +0000989 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 error = -ENOTBLK;
991 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100992 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -0500994 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100995 goto out_path_put;
996
997 *dev = inode->i_rdev;
998 error = 0;
999out_path_put:
Al Viro421748e2008-08-02 01:04:36 -04001000 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001001 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002}
Al Virod5686b42008-08-01 05:00:11 -04001003EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
NeilBrown93b270f2011-02-24 17:25:47 +11001005int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001006{
1007 struct super_block *sb = get_super(bdev);
1008 int res = 0;
1009
1010 if (sb) {
1011 /*
1012 * no need to lock the super, get_super holds the
1013 * read mutex so the filesystem cannot go away
1014 * under us (->put_super runs with the write lock
1015 * hold).
1016 */
1017 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001018 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001019 drop_super(sb);
1020 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001021 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001022 return res;
1023}
1024EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001025
Christoph Hellwig1e03a362021-10-19 08:25:30 +02001026void sync_bdevs(bool wait)
Jan Kara5c0d6b62012-07-03 16:45:31 +02001027{
1028 struct inode *inode, *old_inode = NULL;
1029
Dave Chinner74278da2015-03-04 12:37:22 -05001030 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001031 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1032 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001033 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001034
1035 spin_lock(&inode->i_lock);
1036 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1037 mapping->nrpages == 0) {
1038 spin_unlock(&inode->i_lock);
1039 continue;
1040 }
1041 __iget(inode);
1042 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05001043 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001044 /*
1045 * We hold a reference to 'inode' so it couldn't have been
1046 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05001047 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001048 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05001049 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001050 * later.
1051 */
1052 iput(old_inode);
1053 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001054 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001055
Christoph Hellwiga8698702021-05-25 08:12:56 +02001056 mutex_lock(&bdev->bd_disk->open_mutex);
Christoph Hellwig1e03a362021-10-19 08:25:30 +02001057 if (!bdev->bd_openers) {
1058 ; /* skip */
1059 } else if (wait) {
1060 /*
1061 * We keep the error status of individual mapping so
1062 * that applications can catch the writeback error using
1063 * fsync(2). See filemap_fdatawait_keep_errors() for
1064 * details.
1065 */
1066 filemap_fdatawait_keep_errors(inode->i_mapping);
1067 } else {
1068 filemap_fdatawrite(inode->i_mapping);
1069 }
Christoph Hellwiga8698702021-05-25 08:12:56 +02001070 mutex_unlock(&bdev->bd_disk->open_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001071
Dave Chinner74278da2015-03-04 12:37:22 -05001072 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001073 }
Dave Chinner74278da2015-03-04 12:37:22 -05001074 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001075 iput(old_inode);
1076}