blob: 7e6156203a71c5ba2d62d69250b4e9607205b01c [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
Jan Kara5cee5812009-04-27 16:43:51 +0200188int __sync_blockdev(struct block_device *bdev, int wait)
189{
190 if (!bdev)
191 return 0;
192 if (!wait)
193 return filemap_flush(bdev->bd_inode->i_mapping);
194 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
195}
196
Nick Piggin585d3bc2009-02-25 10:44:19 +0100197/*
198 * Write out and wait upon all the dirty data associated with a block
199 * device via its mapping. Does not take the superblock lock.
200 */
201int sync_blockdev(struct block_device *bdev)
202{
Jan Kara5cee5812009-04-27 16:43:51 +0200203 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100204}
205EXPORT_SYMBOL(sync_blockdev);
206
207/*
208 * Write out and wait upon all dirty data associated with this
209 * device. Filesystem data as well as the underlying block
210 * device. Takes the superblock lock.
211 */
212int fsync_bdev(struct block_device *bdev)
213{
214 struct super_block *sb = get_super(bdev);
215 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200216 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100217 drop_super(sb);
218 return res;
219 }
220 return sync_blockdev(bdev);
221}
Al Viro47e44912009-04-01 07:07:16 -0400222EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100223
224/**
225 * freeze_bdev -- lock a filesystem and force it into a consistent state
226 * @bdev: blockdevice to lock
227 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100228 * If a superblock is found on this device, we take the s_umount semaphore
229 * on it to make sure nobody unmounts until the snapshot creation is done.
230 * The reference counter (bd_fsfreeze_count) guarantees that only the last
231 * unfreeze process can unfreeze the frozen filesystem actually when multiple
232 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
233 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
234 * actually.
235 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100236int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100237{
238 struct super_block *sb;
239 int error = 0;
240
241 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100242 if (++bdev->bd_fsfreeze_count > 1)
243 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100244
Christoph Hellwig45042302009-08-03 23:28:35 +0200245 sb = get_active_super(bdev);
246 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100247 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600248 if (sb->s_op->freeze_super)
249 error = sb->s_op->freeze_super(sb);
250 else
251 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400252 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100253
254 if (error) {
255 bdev->bd_fsfreeze_count--;
256 goto done;
257 }
258 bdev->bd_fsfreeze_sb = sb;
259
260sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100261 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100262done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100263 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100264 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100265}
266EXPORT_SYMBOL(freeze_bdev);
267
268/**
269 * thaw_bdev -- unlock filesystem
270 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100271 *
272 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
273 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100274int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100275{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100276 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200277 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100278
279 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200280 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400281 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100282
Christoph Hellwig45042302009-08-03 23:28:35 +0200283 error = 0;
284 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400285 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100286
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100287 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200288 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400289 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200290
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600291 if (sb->s_op->thaw_super)
292 error = sb->s_op->thaw_super(sb);
293 else
294 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200295 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400296 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000297 else
298 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400299out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100300 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200301 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100302}
303EXPORT_SYMBOL(thaw_bdev);
304
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700305/**
306 * bdev_read_page() - Start reading a page from a block device
307 * @bdev: The device to read the page from
308 * @sector: The offset on the device to read the page to (need not be aligned)
309 * @page: The page to read
310 *
311 * On entry, the page should be locked. It will be unlocked when the page
312 * has been read. If the block driver implements rw_page synchronously,
313 * that will be true on exit from this function, but it need not be.
314 *
315 * Errors returned by this function are usually "soft", eg out of memory, or
316 * queue full; callers should try a different route to read this page rather
317 * than propagate an error back up the stack.
318 *
319 * Return: negative errno if an error occurs, 0 if submission was successful.
320 */
321int bdev_read_page(struct block_device *bdev, sector_t sector,
322 struct page *page)
323{
324 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800325 int result = -EOPNOTSUPP;
326
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400327 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800328 return result;
329
Pavel Begunkov025a3862021-10-14 15:03:27 +0100330 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800331 if (result)
332 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700333 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
334 REQ_OP_READ);
Pavel Begunkov025a3862021-10-14 15:03:27 +0100335 blk_queue_exit(bdev_get_queue(bdev));
Dan Williams2e6edc952015-11-19 13:29:28 -0800336 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700337}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700338
339/**
340 * bdev_write_page() - Start writing a page to a block device
341 * @bdev: The device to write the page to
342 * @sector: The offset on the device to write the page to (need not be aligned)
343 * @page: The page to write
344 * @wbc: The writeback_control for the write
345 *
346 * On entry, the page should be locked and not currently under writeback.
347 * On exit, if the write started successfully, the page will be unlocked and
348 * under writeback. If the write failed already (eg the driver failed to
349 * queue the page to the device), the page will still be locked. If the
350 * caller is a ->writepage implementation, it will need to unlock the page.
351 *
352 * Errors returned by this function are usually "soft", eg out of memory, or
353 * queue full; callers should try a different route to write this page rather
354 * than propagate an error back up the stack.
355 *
356 * Return: negative errno if an error occurs, 0 if submission was successful.
357 */
358int bdev_write_page(struct block_device *bdev, sector_t sector,
359 struct page *page, struct writeback_control *wbc)
360{
361 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700362 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800363
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400364 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700365 return -EOPNOTSUPP;
Pavel Begunkov025a3862021-10-14 15:03:27 +0100366 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800367 if (result)
368 return result;
369
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700370 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700371 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
372 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700373 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700374 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700375 } else {
376 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700377 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700378 }
Pavel Begunkov025a3862021-10-14 15:03:27 +0100379 blk_queue_exit(bdev_get_queue(bdev));
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700380 return result;
381}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
384 * pseudo-fs
385 */
386
387static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800388static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
390static struct inode *bdev_alloc_inode(struct super_block *sb)
391{
Christoph Lametere94b1762006-12-06 20:33:17 -0800392 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (!ei)
395 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100396 memset(&ei->bdev, 0, sizeof(ei->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return &ei->vfs_inode;
398}
399
Al Viro41149cb2019-04-10 15:12:38 -0400400static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100402 struct block_device *bdev = I_BDEV(inode);
403
404 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100405 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100406
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200407 if (!bdev_is_partition(bdev)) {
408 if (bdev->bd_disk && bdev->bd_disk->bdi)
409 bdi_put(bdev->bd_disk->bdi);
Christoph Hellwig340e8452021-07-22 09:53:54 +0200410 kfree(bdev->bd_disk);
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200411 }
Christoph Hellwig9451aa02021-08-16 14:26:13 +0200412
413 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
414 blk_free_ext_minor(MINOR(bdev->bd_dev));
415
Al Viro41149cb2019-04-10 15:12:38 -0400416 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100417}
418
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100419static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100421 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Christoph Lametera35afb82007-05-16 22:10:57 -0700423 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
Al Virob57922d2010-06-07 14:34:48 -0400426static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700428 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400429 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200430 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431}
432
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800433static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 .statfs = simple_statfs,
435 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400436 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400438 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439};
440
David Howells9030d162019-03-25 16:38:23 +0000441static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
David Howells9030d162019-03-25 16:38:23 +0000443 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
444 if (!ctx)
445 return -ENOMEM;
446 fc->s_iflags |= SB_I_CGROUPWB;
447 ctx->ops = &bdev_sops;
448 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
451static struct file_system_type bd_type = {
452 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000453 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 .kill_sb = kill_anon_super,
455};
456
Tejun Heoa212b102015-05-22 17:13:33 -0400457struct super_block *blockdev_superblock __read_mostly;
458EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460void __init bdev_cache_init(void)
461{
462 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300463 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800466 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800467 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900468 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 err = register_filesystem(&bd_type);
470 if (err)
471 panic("Cannot register bdev pseudo-fs");
472 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (IS_ERR(bd_mnt))
474 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300475 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100478struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 struct block_device *bdev;
481 struct inode *inode;
482
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100483 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (!inode)
485 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100486 inode->i_mode = S_IFBLK;
487 inode->i_rdev = 0;
488 inode->i_data.a_ops = &def_blk_aops;
489 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100491 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100492 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100493 spin_lock_init(&bdev->bd_size_lock);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100494 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100495 bdev->bd_inode = inode;
Pavel Begunkov17220ca2021-10-14 15:03:26 +0100496 bdev->bd_queue = disk->queue;
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100497 bdev->bd_stats = alloc_percpu(struct disk_stats);
498 if (!bdev->bd_stats) {
499 iput(inode);
500 return NULL;
501 }
Tetsuo Handa06cc9782021-10-02 18:23:02 +0900502 bdev->bd_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return bdev;
504}
505
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100506void bdev_add(struct block_device *bdev, dev_t dev)
507{
508 bdev->bd_dev = dev;
509 bdev->bd_inode->i_rdev = dev;
510 bdev->bd_inode->i_ino = dev;
511 insert_inode_hash(bdev->bd_inode);
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514long nr_blockdev_pages(void)
515{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200516 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200518
519 spin_lock(&blockdev_superblock->s_inode_list_lock);
520 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
521 ret += inode->i_mapping->nrpages;
522 spin_unlock(&blockdev_superblock->s_inode_list_lock);
523
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return ret;
525}
526
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900527/**
528 * bd_may_claim - test whether a block device can be claimed
529 * @bdev: block device of interest
530 * @whole: whole block device containing @bdev, may equal @bdev
531 * @holder: holder trying to claim @bdev
532 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300533 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900534 *
535 * CONTEXT:
536 * spin_lock(&bdev_lock).
537 *
538 * RETURNS:
539 * %true if @bdev can be claimed, %false otherwise.
540 */
541static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
542 void *holder)
543{
544 if (bdev->bd_holder == holder)
545 return true; /* already a holder */
546 else if (bdev->bd_holder != NULL)
547 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700548 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900549 return true; /* is a whole device which isn't held */
550
Tejun Heoe525fd82010-11-13 11:55:17 +0100551 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900552 return true; /* is a partition of a device that is being partitioned */
553 else if (whole->bd_holder != NULL)
554 return false; /* is a partition of a held device */
555 else
556 return true; /* is a partition of an un-held device */
557}
558
559/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200560 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +0900561 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +0900562 * @holder: holder trying to claim @bdev
563 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200564 * Claim @bdev. This function fails if @bdev is already claimed by another
565 * holder and waits if another claiming is in progress. return, the caller
566 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +0900567 *
568 * RETURNS:
569 * 0 if @bdev can be claimed, -EBUSY otherwise.
570 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100571int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +0900572{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100573 struct block_device *whole = bdev_whole(bdev);
574
575 if (WARN_ON_ONCE(!holder))
576 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +0900577retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200578 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900579 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200580 if (!bd_may_claim(bdev, whole, holder)) {
581 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900582 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200583 }
Tejun Heo6b4517a2010-04-07 18:53:59 +0900584
Tejun Heoe75aa852010-08-04 17:59:39 +0200585 /* if claiming is already in progress, wait for it to finish */
586 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900587 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
588 DEFINE_WAIT(wait);
589
590 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
591 spin_unlock(&bdev_lock);
592 schedule();
593 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900594 goto retry;
595 }
596
597 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200598 whole->bd_claiming = holder;
599 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900600 return 0;
601}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +0200602EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +0900603
Jan Kara89e524c02019-07-30 13:10:14 +0200604static void bd_clear_claiming(struct block_device *whole, void *holder)
605{
606 lockdep_assert_held(&bdev_lock);
607 /* tell others that we're done */
608 BUG_ON(whole->bd_claiming != holder);
609 whole->bd_claiming = NULL;
610 wake_up_bit(&whole->bd_claiming, 0);
611}
612
613/**
614 * bd_finish_claiming - finish claiming of a block device
615 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200616 * @holder: holder that has claimed @bdev
617 *
618 * Finish exclusive open of a block device. Mark the device as exlusively
619 * open by the holder and wake up all waiters for exclusive open to finish.
620 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100621static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200622{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100623 struct block_device *whole = bdev_whole(bdev);
624
Jan Kara89e524c02019-07-30 13:10:14 +0200625 spin_lock(&bdev_lock);
626 BUG_ON(!bd_may_claim(bdev, whole, holder));
627 /*
628 * Note that for a whole device bd_holders will be incremented twice,
629 * and bd_holder will be set to bd_may_claim before being set to holder
630 */
631 whole->bd_holders++;
632 whole->bd_holder = bd_may_claim;
633 bdev->bd_holders++;
634 bdev->bd_holder = holder;
635 bd_clear_claiming(whole, holder);
636 spin_unlock(&bdev_lock);
637}
Jan Kara89e524c02019-07-30 13:10:14 +0200638
639/**
640 * bd_abort_claiming - abort claiming of a block device
641 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200642 * @holder: holder that has claimed @bdev
643 *
644 * Abort claiming of a block device when the exclusive open failed. This can be
645 * also used when exclusive open is not actually desired and we just needed
646 * to block other exclusive openers for a while.
647 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100648void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200649{
650 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100651 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +0200652 spin_unlock(&bdev_lock);
653}
654EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900655
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200656static void blkdev_flush_mapping(struct block_device *bdev)
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100657{
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200658 WARN_ON_ONCE(bdev->bd_holders);
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200659 sync_blockdev(bdev);
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200660 kill_bdev(bdev);
661 bdev_write_inode(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100662}
663
Christoph Hellwig362529d2021-05-25 08:12:54 +0200664static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100666 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100667 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Christoph Hellwig362529d2021-05-25 08:12:54 +0200669 if (disk->fops->open) {
670 ret = disk->fops->open(bdev, mode);
671 if (ret) {
672 /* avoid ghost partitions on a removed medium */
673 if (ret == -ENOMEDIUM &&
674 test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200675 bdev_disk_changed(disk, true);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200676 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200680 if (!bdev->bd_openers)
Christoph Hellwig362529d2021-05-25 08:12:54 +0200681 set_init_blocksize(bdev);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200682 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200683 bdev_disk_changed(disk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 bdev->bd_openers++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200685 return 0;;
686}
687
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200688static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
689{
690 if (!--bdev->bd_openers)
691 blkdev_flush_mapping(bdev);
692 if (bdev->bd_disk->fops->release)
693 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
694}
695
Christoph Hellwig362529d2021-05-25 08:12:54 +0200696static int blkdev_get_part(struct block_device *part, fmode_t mode)
697{
698 struct gendisk *disk = part->bd_disk;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200699 int ret;
700
701 if (part->bd_openers)
702 goto done;
703
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200704 ret = blkdev_get_whole(bdev_whole(part), mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200705 if (ret)
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200706 return ret;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200707
708 ret = -ENXIO;
709 if (!bdev_nr_sectors(part))
710 goto out_blkdev_put;
711
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200712 disk->open_partitions++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200713 set_init_blocksize(part);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200714done:
715 part->bd_openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200717
718out_blkdev_put:
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200719 blkdev_put_whole(bdev_whole(part), mode);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200720 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200723static void blkdev_put_part(struct block_device *part, fmode_t mode)
724{
725 struct block_device *whole = bdev_whole(part);
726
727 if (--part->bd_openers)
728 return;
729 blkdev_flush_mapping(part);
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200730 whole->bd_disk->open_partitions--;
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200731 blkdev_put_whole(whole, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100734struct block_device *blkdev_get_no_open(dev_t dev)
735{
736 struct block_device *bdev;
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200737 struct inode *inode;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100738
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200739 inode = ilookup(blockdev_superblock, dev);
740 if (!inode) {
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100741 blk_request_module(dev);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200742 inode = ilookup(blockdev_superblock, dev);
743 if (!inode)
Christoph Hellwig6c60ff02021-05-14 15:18:41 +0200744 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100745 }
746
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200747 /* switch from the inode reference to a device mode one: */
748 bdev = &BDEV_I(inode)->bdev;
749 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
750 bdev = NULL;
751 iput(inode);
752
753 if (!bdev)
754 return NULL;
755 if ((bdev->bd_disk->flags & GENHD_FL_HIDDEN) ||
756 !try_module_get(bdev->bd_disk->fops->owner)) {
757 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{
766 module_put(bdev->bd_disk->fops->owner);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200767 put_device(&bdev->bd_device);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100768}
769
Tejun Heod4d77622010-11-13 11:55:18 +0100770/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100771 * blkdev_get_by_dev - open a block device by device number
772 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +0100773 * @mode: FMODE_* mask
774 * @holder: exclusive holder identifier
775 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100776 * Open the block device described by device number @dev. If @mode includes
777 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
778 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
779 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100780 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100781 * Use this interface ONLY if you really do not have anything better - i.e. when
782 * you are behind a truly sucky interface and all you are given is a device
783 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +0100784 *
785 * CONTEXT:
786 * Might sleep.
787 *
788 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100789 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100790 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100791struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100793 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100794 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100795 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100796 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +0100797
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100798 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100799 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100800 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
801 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200802 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100803 return ERR_PTR(ret);
804
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100805 bdev = blkdev_get_no_open(dev);
806 if (!bdev)
807 return ERR_PTR(-ENXIO);
808 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200809
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100810 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100811 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100812 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100813 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100814 }
815
816 disk_block_events(disk);
817
Christoph Hellwiga8698702021-05-25 08:12:56 +0200818 mutex_lock(&disk->open_mutex);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200819 ret = -ENXIO;
Christoph Hellwig50b4aec2021-08-09 08:40:28 +0200820 if (!disk_live(disk))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200821 goto abort_claiming;
822 if (bdev_is_partition(bdev))
823 ret = blkdev_get_part(bdev, mode);
824 else
825 ret = blkdev_get_whole(bdev, mode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100826 if (ret)
827 goto abort_claiming;
828 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100829 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100830
831 /*
832 * Block event polling for write claims if requested. Any write
833 * holder makes the write_holder state stick until all are
834 * released. This is good enough and tracking individual
835 * writeable reference is too fragile given the way @mode is
836 * used in blkdev_get/put().
837 */
838 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
839 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
840 bdev->bd_write_holder = true;
841 unblock_events = false;
842 }
843 }
Christoph Hellwiga8698702021-05-25 08:12:56 +0200844 mutex_unlock(&disk->open_mutex);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100845
846 if (unblock_events)
847 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100848 return bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100849
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100850abort_claiming:
851 if (mode & FMODE_EXCL)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100852 bd_abort_claiming(bdev, holder);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200853 mutex_unlock(&disk->open_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100854 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100855put_blkdev:
856 blkdev_put_no_open(bdev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100857 return ERR_PTR(ret);
NeilBrown37be4122006-12-08 02:36:16 -0800858}
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100859EXPORT_SYMBOL(blkdev_get_by_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Tejun Heod4d77622010-11-13 11:55:18 +0100861/**
862 * blkdev_get_by_path - open a block device by name
863 * @path: path to the block device to open
864 * @mode: FMODE_* mask
865 * @holder: exclusive holder identifier
866 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100867 * Open the block device described by the device file at @path. If @mode
868 * includes %FMODE_EXCL, the block device is opened with exclusive access.
869 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
870 * nest for the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100871 *
872 * CONTEXT:
873 * Might sleep.
874 *
875 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100876 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100877 */
878struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
879 void *holder)
880{
881 struct block_device *bdev;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100882 dev_t dev;
883 int error;
Tejun Heod4d77622010-11-13 11:55:18 +0100884
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100885 error = lookup_bdev(path, &dev);
886 if (error)
887 return ERR_PTR(error);
Tejun Heod4d77622010-11-13 11:55:18 +0100888
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100889 bdev = blkdev_get_by_dev(dev, mode, holder);
890 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
Chuck Ebberte51900f2011-02-16 18:11:53 -0500891 blkdev_put(bdev, mode);
892 return ERR_PTR(-EACCES);
893 }
894
Tejun Heod4d77622010-11-13 11:55:18 +0100895 return bdev;
896}
897EXPORT_SYMBOL(blkdev_get_by_path);
898
Al Viro4385bab2013-05-05 22:11:03 -0400899void blkdev_put(struct block_device *bdev, fmode_t mode)
Al Viro572c4892007-10-08 13:24:05 -0400900{
901 struct gendisk *disk = bdev->bd_disk;
Al Viro572c4892007-10-08 13:24:05 -0400902
903 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 * Sync early if it looks like we're the last one. If someone else
Pavel Emelianov6a2aae02006-10-28 10:38:33 -0700905 * opens the block device between now and the decrement of bd_openers
906 * then we did a sync that we didn't need to, but that's not the end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 * of the world and we want to avoid long (could be several minute)
Al Viro572c4892007-10-08 13:24:05 -0400908 * syncs while holding the mutex.
909 */
Tejun Heoe525fd82010-11-13 11:55:17 +0100910 if (bdev->bd_openers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 sync_blockdev(bdev);
912
Christoph Hellwiga8698702021-05-25 08:12:56 +0200913 mutex_lock(&disk->open_mutex);
Tejun Heoe525fd82010-11-13 11:55:17 +0100914 if (mode & FMODE_EXCL) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100915 struct block_device *whole = bdev_whole(bdev);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100916 bool bdev_free;
917
918 /*
919 * Release a claim on the device. The holder fields
Christoph Hellwiga8698702021-05-25 08:12:56 +0200920 * are protected with bdev_lock. open_mutex is to
Tejun Heo6a027ef2010-11-13 11:55:17 +0100921 * synchronize disk_holder unlinking.
922 */
Tejun Heo6a027ef2010-11-13 11:55:17 +0100923 spin_lock(&bdev_lock);
924
925 WARN_ON_ONCE(--bdev->bd_holders < 0);
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100926 WARN_ON_ONCE(--whole->bd_holders < 0);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100927
Tejun Heo6a027ef2010-11-13 11:55:17 +0100928 if ((bdev_free = !bdev->bd_holders))
929 bdev->bd_holder = NULL;
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100930 if (!whole->bd_holders)
931 whole->bd_holder = NULL;
Tejun Heo6a027ef2010-11-13 11:55:17 +0100932
933 spin_unlock(&bdev_lock);
934
Tejun Heo77ea8872010-12-08 20:57:37 +0100935 /*
936 * If this was the last claim, remove holder link and
937 * unblock evpoll if it was a write holder.
938 */
Tejun Heo85ef06d2011-07-01 16:17:47 +0200939 if (bdev_free && bdev->bd_write_holder) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100940 disk_unblock_events(disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200941 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +0100942 }
Tejun Heo69362172011-03-09 19:54:27 +0100943 }
Tejun Heo77ea8872010-12-08 20:57:37 +0100944
Tejun Heo85ef06d2011-07-01 16:17:47 +0200945 /*
946 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
947 * event. This is to ensure detection of media removal commanded
948 * from userland - e.g. eject(1).
949 */
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100950 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200951
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200952 if (bdev_is_partition(bdev))
953 blkdev_put_part(bdev, mode);
954 else
955 blkdev_put_whole(bdev, mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200956 mutex_unlock(&disk->open_mutex);
957
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100958 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -0800959}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800960EXPORT_SYMBOL(blkdev_put);
961
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962/**
963 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -0800964 * @pathname: special file representing the block device
Randy Dunlap875b2372020-12-28 19:47:06 -0800965 * @dev: return value of the block device's dev_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 *
Jackie Liu057178c2021-10-21 15:13:43 +0800967 * Lookup the block device's dev_t at @pathname in the current
968 * namespace if possible and return it by @dev.
969 *
970 * RETURNS:
971 * 0 if succeeded, errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100973int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -0400976 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 int error;
978
Al Viro421748e2008-08-02 01:04:36 -0400979 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100980 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Al Viro421748e2008-08-02 01:04:36 -0400982 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100984 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
David Howellsbb6687342015-03-17 22:26:21 +0000986 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 error = -ENOTBLK;
988 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100989 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -0500991 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100992 goto out_path_put;
993
994 *dev = inode->i_rdev;
995 error = 0;
996out_path_put:
Al Viro421748e2008-08-02 01:04:36 -0400997 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100998 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Al Virod5686b42008-08-01 05:00:11 -04001000EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
NeilBrown93b270f2011-02-24 17:25:47 +11001002int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001003{
1004 struct super_block *sb = get_super(bdev);
1005 int res = 0;
1006
1007 if (sb) {
1008 /*
1009 * no need to lock the super, get_super holds the
1010 * read mutex so the filesystem cannot go away
1011 * under us (->put_super runs with the write lock
1012 * hold).
1013 */
1014 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001015 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001016 drop_super(sb);
1017 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001018 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001019 return res;
1020}
1021EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001022
1023void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1024{
1025 struct inode *inode, *old_inode = NULL;
1026
Dave Chinner74278da2015-03-04 12:37:22 -05001027 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001028 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1029 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001030 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001031
1032 spin_lock(&inode->i_lock);
1033 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1034 mapping->nrpages == 0) {
1035 spin_unlock(&inode->i_lock);
1036 continue;
1037 }
1038 __iget(inode);
1039 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05001040 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001041 /*
1042 * We hold a reference to 'inode' so it couldn't have been
1043 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05001044 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001045 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05001046 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001047 * later.
1048 */
1049 iput(old_inode);
1050 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001051 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001052
Christoph Hellwiga8698702021-05-25 08:12:56 +02001053 mutex_lock(&bdev->bd_disk->open_mutex);
Rabin Vincentaf309222016-12-01 09:18:28 +01001054 if (bdev->bd_openers)
1055 func(bdev, arg);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001056 mutex_unlock(&bdev->bd_disk->open_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001057
Dave Chinner74278da2015-03-04 12:37:22 -05001058 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001059 }
Dave Chinner74278da2015-03-04 12:37:22 -05001060 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001061 iput(old_inode);
1062}