blob: cf2780cb44a74fddb4624aecb22161203b62d6b7 [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>
Tejun Heo66114ca2015-05-22 17:13:32 -040015#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/module.h>
17#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070018#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040020#include <linux/swap.h>
David Howells811d7362006-08-29 19:06:09 +010021#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000023#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/uio.h>
25#include <linux/namei.h>
Al Viroff01bb42011-09-16 02:31:11 -040026#include <linux/cleancache.h>
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +010027#include <linux/part_stat.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080028#include <linux/uaccess.h>
Christoph Hellwig0dca4462021-09-07 16:13:03 +020029#include "../fs/internal.h"
30#include "blk.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32struct bdev_inode {
33 struct block_device bdev;
34 struct inode vfs_inode;
35};
36
37static inline struct bdev_inode *BDEV_I(struct inode *inode)
38{
39 return container_of(inode, struct bdev_inode, vfs_inode);
40}
41
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020042struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070043{
44 return &BDEV_I(inode)->bdev;
45}
Linus Torvalds1da177e2005-04-16 15:20:36 -070046EXPORT_SYMBOL(I_BDEV);
47
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070048static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010049{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070050 struct inode *inode = bdev->bd_inode;
51 int ret;
52
Christoph Hellwig564f00f2015-01-14 10:42:33 +010053 spin_lock(&inode->i_lock);
54 while (inode->i_state & I_DIRTY) {
55 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070056 ret = write_inode_now(inode, true);
57 if (ret) {
58 char name[BDEVNAME_SIZE];
59 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
60 "for block device %s (err=%d).\n",
61 bdevname(bdev, name), ret);
62 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010063 spin_lock(&inode->i_lock);
64 }
65 spin_unlock(&inode->i_lock);
66}
67
Peter Zijlstraf9a14392007-05-06 14:49:55 -070068/* Kill _all_ buffers and pagecache , dirty or not.. */
Zheng Bin3373a342020-06-18 12:21:38 +080069static void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Al Viroff01bb42011-09-16 02:31:11 -040071 struct address_space *mapping = bdev->bd_inode->i_mapping;
72
Matthew Wilcox (Oracle)77165062021-05-04 18:32:45 -070073 if (mapping_empty(mapping))
Peter Zijlstraf9a14392007-05-06 14:49:55 -070074 return;
Al Viroff01bb42011-09-16 02:31:11 -040075
Peter Zijlstraf9a14392007-05-06 14:49:55 -070076 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040077 truncate_inode_pages(mapping, 0);
Zheng Bin3373a342020-06-18 12:21:38 +080078}
Al Viroff01bb42011-09-16 02:31:11 -040079
80/* Invalidate clean unused buffers and pagecache. */
81void invalidate_bdev(struct block_device *bdev)
82{
83 struct address_space *mapping = bdev->bd_inode->i_mapping;
84
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070085 if (mapping->nrpages) {
86 invalidate_bh_lrus();
87 lru_add_drain_all(); /* make sure all lru add caches are flushed */
88 invalidate_mapping_pages(mapping, 0, -1);
89 }
Al Viroff01bb42011-09-16 02:31:11 -040090 /* 99% of the time, we don't need to flush the cleancache on the bdev.
91 * But, for the strange corners, lets be cautious
92 */
Dan Magenheimer31677602011-09-21 11:56:28 -040093 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -040094}
95EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Jan Kara384d87e2020-09-04 10:58:52 +020097/*
98 * Drop all buffers & page cache for given bdev range. This function bails
99 * with error if bdev has other exclusive owner (such as filesystem).
100 */
101int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
102 loff_t lstart, loff_t lend)
103{
Jan Kara384d87e2020-09-04 10:58:52 +0200104 /*
105 * If we don't hold exclusive handle for the device, upgrade to it
106 * while we discard the buffer cache to avoid discarding buffers
107 * under live filesystem.
108 */
109 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100110 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200111 if (err)
Jan Kara56887cf2021-02-22 10:48:09 +0100112 goto invalidate;
Jan Kara384d87e2020-09-04 10:58:52 +0200113 }
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100114
Jan Kara384d87e2020-09-04 10:58:52 +0200115 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100116 if (!(mode & FMODE_EXCL))
117 bd_abort_claiming(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200118 return 0;
Jan Kara56887cf2021-02-22 10:48:09 +0100119
120invalidate:
121 /*
122 * Someone else has handle exclusively open. Try invalidating instead.
123 * The 'end' argument is inclusive so the rounding is safe.
124 */
125 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
126 lstart >> PAGE_SHIFT,
127 lend >> PAGE_SHIFT);
Jan Kara384d87e2020-09-04 10:58:52 +0200128}
Jan Kara384d87e2020-09-04 10:58:52 +0200129
Jan Kara04906b22019-01-14 09:48:10 +0100130static void set_init_blocksize(struct block_device *bdev)
131{
Maxim Mikityanskiy8dc932d2021-01-26 21:59:07 +0200132 unsigned int bsize = bdev_logical_block_size(bdev);
133 loff_t size = i_size_read(bdev->bd_inode);
134
135 while (bsize < PAGE_SIZE) {
136 if (size & bsize)
137 break;
138 bsize <<= 1;
139 }
140 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
Jan Kara04906b22019-01-14 09:48:10 +0100141}
142
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143int set_blocksize(struct block_device *bdev, int size)
144{
145 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700146 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 return -EINVAL;
148
149 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400150 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return -EINVAL;
152
153 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200154 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 bdev->bd_inode->i_blkbits = blksize_bits(size);
157 kill_bdev(bdev);
158 }
159 return 0;
160}
161
162EXPORT_SYMBOL(set_blocksize);
163
164int sb_set_blocksize(struct super_block *sb, int size)
165{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (set_blocksize(sb->s_bdev, size))
167 return 0;
168 /* If we get here, we know size is power of two
169 * and it's value is between 512 and PAGE_SIZE */
170 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800171 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return sb->s_blocksize;
173}
174
175EXPORT_SYMBOL(sb_set_blocksize);
176
177int sb_min_blocksize(struct super_block *sb, int size)
178{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400179 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 if (size < minsize)
181 size = minsize;
182 return sb_set_blocksize(sb, size);
183}
184
185EXPORT_SYMBOL(sb_min_blocksize);
186
Jan Kara5cee5812009-04-27 16:43:51 +0200187int __sync_blockdev(struct block_device *bdev, int wait)
188{
189 if (!bdev)
190 return 0;
191 if (!wait)
192 return filemap_flush(bdev->bd_inode->i_mapping);
193 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
194}
195
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{
Jan Kara5cee5812009-04-27 16:43:51 +0200202 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100203}
204EXPORT_SYMBOL(sync_blockdev);
205
206/*
207 * Write out and wait upon all dirty data associated with this
208 * device. Filesystem data as well as the underlying block
209 * device. Takes the superblock lock.
210 */
211int fsync_bdev(struct block_device *bdev)
212{
213 struct super_block *sb = get_super(bdev);
214 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200215 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100216 drop_super(sb);
217 return res;
218 }
219 return sync_blockdev(bdev);
220}
Al Viro47e44912009-04-01 07:07:16 -0400221EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100222
223/**
224 * freeze_bdev -- lock a filesystem and force it into a consistent state
225 * @bdev: blockdevice to lock
226 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100227 * If a superblock is found on this device, we take the s_umount semaphore
228 * on it to make sure nobody unmounts until the snapshot creation is done.
229 * The reference counter (bd_fsfreeze_count) guarantees that only the last
230 * unfreeze process can unfreeze the frozen filesystem actually when multiple
231 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
232 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
233 * actually.
234 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100235int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100236{
237 struct super_block *sb;
238 int error = 0;
239
240 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100241 if (++bdev->bd_fsfreeze_count > 1)
242 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100243
Christoph Hellwig45042302009-08-03 23:28:35 +0200244 sb = get_active_super(bdev);
245 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100246 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600247 if (sb->s_op->freeze_super)
248 error = sb->s_op->freeze_super(sb);
249 else
250 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400251 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100252
253 if (error) {
254 bdev->bd_fsfreeze_count--;
255 goto done;
256 }
257 bdev->bd_fsfreeze_sb = sb;
258
259sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100260 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100261done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100262 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100263 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100264}
265EXPORT_SYMBOL(freeze_bdev);
266
267/**
268 * thaw_bdev -- unlock filesystem
269 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100270 *
271 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
272 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100273int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100274{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100275 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200276 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100277
278 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200279 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400280 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100281
Christoph Hellwig45042302009-08-03 23:28:35 +0200282 error = 0;
283 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400284 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100285
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100286 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200287 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400288 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200289
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600290 if (sb->s_op->thaw_super)
291 error = sb->s_op->thaw_super(sb);
292 else
293 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200294 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400295 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000296 else
297 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400298out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100299 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200300 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100301}
302EXPORT_SYMBOL(thaw_bdev);
303
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700304/**
305 * bdev_read_page() - Start reading a page from a block device
306 * @bdev: The device to read the page from
307 * @sector: The offset on the device to read the page to (need not be aligned)
308 * @page: The page to read
309 *
310 * On entry, the page should be locked. It will be unlocked when the page
311 * has been read. If the block driver implements rw_page synchronously,
312 * that will be true on exit from this function, but it need not be.
313 *
314 * Errors returned by this function are usually "soft", eg out of memory, or
315 * queue full; callers should try a different route to read this page rather
316 * than propagate an error back up the stack.
317 *
318 * Return: negative errno if an error occurs, 0 if submission was successful.
319 */
320int bdev_read_page(struct block_device *bdev, sector_t sector,
321 struct page *page)
322{
323 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800324 int result = -EOPNOTSUPP;
325
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400326 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800327 return result;
328
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200329 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800330 if (result)
331 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700332 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
333 REQ_OP_READ);
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200334 blk_queue_exit(bdev->bd_disk->queue);
Dan Williams2e6edc952015-11-19 13:29:28 -0800335 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700336}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700337
338/**
339 * bdev_write_page() - Start writing a page to a block device
340 * @bdev: The device to write the page to
341 * @sector: The offset on the device to write the page to (need not be aligned)
342 * @page: The page to write
343 * @wbc: The writeback_control for the write
344 *
345 * On entry, the page should be locked and not currently under writeback.
346 * On exit, if the write started successfully, the page will be unlocked and
347 * under writeback. If the write failed already (eg the driver failed to
348 * queue the page to the device), the page will still be locked. If the
349 * caller is a ->writepage implementation, it will need to unlock the page.
350 *
351 * Errors returned by this function are usually "soft", eg out of memory, or
352 * queue full; callers should try a different route to write this page rather
353 * than propagate an error back up the stack.
354 *
355 * Return: negative errno if an error occurs, 0 if submission was successful.
356 */
357int bdev_write_page(struct block_device *bdev, sector_t sector,
358 struct page *page, struct writeback_control *wbc)
359{
360 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700361 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800362
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400363 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700364 return -EOPNOTSUPP;
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200365 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800366 if (result)
367 return result;
368
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700369 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700370 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
371 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700372 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700373 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700374 } else {
375 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700376 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700377 }
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200378 blk_queue_exit(bdev->bd_disk->queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700379 return result;
380}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * pseudo-fs
384 */
385
386static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800387static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389static struct inode *bdev_alloc_inode(struct super_block *sb)
390{
Christoph Lametere94b1762006-12-06 20:33:17 -0800391 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (!ei)
394 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100395 memset(&ei->bdev, 0, sizeof(ei->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 return &ei->vfs_inode;
397}
398
Al Viro41149cb2019-04-10 15:12:38 -0400399static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100401 struct block_device *bdev = I_BDEV(inode);
402
403 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100404 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100405
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200406 if (!bdev_is_partition(bdev)) {
407 if (bdev->bd_disk && bdev->bd_disk->bdi)
408 bdi_put(bdev->bd_disk->bdi);
Christoph Hellwig340e8452021-07-22 09:53:54 +0200409 kfree(bdev->bd_disk);
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200410 }
Christoph Hellwig9451aa02021-08-16 14:26:13 +0200411
412 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
413 blk_free_ext_minor(MINOR(bdev->bd_dev));
414
Al Viro41149cb2019-04-10 15:12:38 -0400415 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100416}
417
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100418static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100420 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Christoph Lametera35afb82007-05-16 22:10:57 -0700422 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423}
424
Al Virob57922d2010-06-07 14:34:48 -0400425static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700427 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400428 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200429 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430}
431
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800432static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 .statfs = simple_statfs,
434 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400435 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400437 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438};
439
David Howells9030d162019-03-25 16:38:23 +0000440static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
David Howells9030d162019-03-25 16:38:23 +0000442 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
443 if (!ctx)
444 return -ENOMEM;
445 fc->s_iflags |= SB_I_CGROUPWB;
446 ctx->ops = &bdev_sops;
447 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450static struct file_system_type bd_type = {
451 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000452 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 .kill_sb = kill_anon_super,
454};
455
Tejun Heoa212b102015-05-22 17:13:33 -0400456struct super_block *blockdev_superblock __read_mostly;
457EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459void __init bdev_cache_init(void)
460{
461 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300462 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800465 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800466 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900467 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 err = register_filesystem(&bd_type);
469 if (err)
470 panic("Cannot register bdev pseudo-fs");
471 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (IS_ERR(bd_mnt))
473 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300474 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100477struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 struct block_device *bdev;
480 struct inode *inode;
481
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100482 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (!inode)
484 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100485 inode->i_mode = S_IFBLK;
486 inode->i_rdev = 0;
487 inode->i_data.a_ops = &def_blk_aops;
488 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100490 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100491 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100492 spin_lock_init(&bdev->bd_size_lock);
493 bdev->bd_disk = disk;
494 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100495 bdev->bd_inode = inode;
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100496 bdev->bd_stats = alloc_percpu(struct disk_stats);
497 if (!bdev->bd_stats) {
498 iput(inode);
499 return NULL;
500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 return bdev;
502}
503
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100504void bdev_add(struct block_device *bdev, dev_t dev)
505{
506 bdev->bd_dev = dev;
507 bdev->bd_inode->i_rdev = dev;
508 bdev->bd_inode->i_ino = dev;
509 insert_inode_hash(bdev->bd_inode);
510}
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512long nr_blockdev_pages(void)
513{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200514 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200516
517 spin_lock(&blockdev_superblock->s_inode_list_lock);
518 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
519 ret += inode->i_mapping->nrpages;
520 spin_unlock(&blockdev_superblock->s_inode_list_lock);
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 return ret;
523}
524
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900525/**
526 * bd_may_claim - test whether a block device can be claimed
527 * @bdev: block device of interest
528 * @whole: whole block device containing @bdev, may equal @bdev
529 * @holder: holder trying to claim @bdev
530 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300531 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900532 *
533 * CONTEXT:
534 * spin_lock(&bdev_lock).
535 *
536 * RETURNS:
537 * %true if @bdev can be claimed, %false otherwise.
538 */
539static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
540 void *holder)
541{
542 if (bdev->bd_holder == holder)
543 return true; /* already a holder */
544 else if (bdev->bd_holder != NULL)
545 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700546 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900547 return true; /* is a whole device which isn't held */
548
Tejun Heoe525fd82010-11-13 11:55:17 +0100549 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900550 return true; /* is a partition of a device that is being partitioned */
551 else if (whole->bd_holder != NULL)
552 return false; /* is a partition of a held device */
553 else
554 return true; /* is a partition of an un-held device */
555}
556
557/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200558 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +0900559 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +0900560 * @holder: holder trying to claim @bdev
561 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200562 * Claim @bdev. This function fails if @bdev is already claimed by another
563 * holder and waits if another claiming is in progress. return, the caller
564 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +0900565 *
566 * RETURNS:
567 * 0 if @bdev can be claimed, -EBUSY otherwise.
568 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100569int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +0900570{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100571 struct block_device *whole = bdev_whole(bdev);
572
573 if (WARN_ON_ONCE(!holder))
574 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +0900575retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200576 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900577 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200578 if (!bd_may_claim(bdev, whole, holder)) {
579 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900580 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200581 }
Tejun Heo6b4517a2010-04-07 18:53:59 +0900582
Tejun Heoe75aa852010-08-04 17:59:39 +0200583 /* if claiming is already in progress, wait for it to finish */
584 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900585 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
586 DEFINE_WAIT(wait);
587
588 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
589 spin_unlock(&bdev_lock);
590 schedule();
591 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900592 goto retry;
593 }
594
595 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200596 whole->bd_claiming = holder;
597 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900598 return 0;
599}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +0200600EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +0900601
Jan Kara89e524c02019-07-30 13:10:14 +0200602static void bd_clear_claiming(struct block_device *whole, void *holder)
603{
604 lockdep_assert_held(&bdev_lock);
605 /* tell others that we're done */
606 BUG_ON(whole->bd_claiming != holder);
607 whole->bd_claiming = NULL;
608 wake_up_bit(&whole->bd_claiming, 0);
609}
610
611/**
612 * bd_finish_claiming - finish claiming of a block device
613 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200614 * @holder: holder that has claimed @bdev
615 *
616 * Finish exclusive open of a block device. Mark the device as exlusively
617 * open by the holder and wake up all waiters for exclusive open to finish.
618 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100619static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200620{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100621 struct block_device *whole = bdev_whole(bdev);
622
Jan Kara89e524c02019-07-30 13:10:14 +0200623 spin_lock(&bdev_lock);
624 BUG_ON(!bd_may_claim(bdev, whole, holder));
625 /*
626 * Note that for a whole device bd_holders will be incremented twice,
627 * and bd_holder will be set to bd_may_claim before being set to holder
628 */
629 whole->bd_holders++;
630 whole->bd_holder = bd_may_claim;
631 bdev->bd_holders++;
632 bdev->bd_holder = holder;
633 bd_clear_claiming(whole, holder);
634 spin_unlock(&bdev_lock);
635}
Jan Kara89e524c02019-07-30 13:10:14 +0200636
637/**
638 * bd_abort_claiming - abort claiming of a block device
639 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200640 * @holder: holder that has claimed @bdev
641 *
642 * Abort claiming of a block device when the exclusive open failed. This can be
643 * also used when exclusive open is not actually desired and we just needed
644 * to block other exclusive openers for a while.
645 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100646void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200647{
648 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100649 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +0200650 spin_unlock(&bdev_lock);
651}
652EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900653
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200654static void blkdev_flush_mapping(struct block_device *bdev)
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100655{
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200656 WARN_ON_ONCE(bdev->bd_holders);
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200657 sync_blockdev(bdev);
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200658 kill_bdev(bdev);
659 bdev_write_inode(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100660}
661
Christoph Hellwig362529d2021-05-25 08:12:54 +0200662static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100664 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigcb8432d2020-11-26 18:47:17 +0100665 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
Christoph Hellwig362529d2021-05-25 08:12:54 +0200667 if (disk->fops->open) {
668 ret = disk->fops->open(bdev, mode);
669 if (ret) {
670 /* avoid ghost partitions on a removed medium */
671 if (ret == -ENOMEDIUM &&
672 test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200673 bdev_disk_changed(disk, true);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200674 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
Christoph Hellwiga11d7fc2021-08-09 16:17:44 +0200678 if (!bdev->bd_openers)
Christoph Hellwig362529d2021-05-25 08:12:54 +0200679 set_init_blocksize(bdev);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200680 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200681 bdev_disk_changed(disk, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 bdev->bd_openers++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200683 return 0;;
684}
685
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200686static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
687{
688 if (!--bdev->bd_openers)
689 blkdev_flush_mapping(bdev);
690 if (bdev->bd_disk->fops->release)
691 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
692}
693
Christoph Hellwig362529d2021-05-25 08:12:54 +0200694static int blkdev_get_part(struct block_device *part, fmode_t mode)
695{
696 struct gendisk *disk = part->bd_disk;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200697 int ret;
698
699 if (part->bd_openers)
700 goto done;
701
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200702 ret = blkdev_get_whole(bdev_whole(part), mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200703 if (ret)
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200704 return ret;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200705
706 ret = -ENXIO;
707 if (!bdev_nr_sectors(part))
708 goto out_blkdev_put;
709
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200710 disk->open_partitions++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200711 set_init_blocksize(part);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200712done:
713 part->bd_openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200715
716out_blkdev_put:
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200717 blkdev_put_whole(bdev_whole(part), mode);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200718 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
720
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200721static void blkdev_put_part(struct block_device *part, fmode_t mode)
722{
723 struct block_device *whole = bdev_whole(part);
724
725 if (--part->bd_openers)
726 return;
727 blkdev_flush_mapping(part);
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200728 whole->bd_disk->open_partitions--;
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200729 blkdev_put_whole(whole, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100732struct block_device *blkdev_get_no_open(dev_t dev)
733{
734 struct block_device *bdev;
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200735 struct inode *inode;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100736
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200737 inode = ilookup(blockdev_superblock, dev);
738 if (!inode) {
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100739 blk_request_module(dev);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200740 inode = ilookup(blockdev_superblock, dev);
741 if (!inode)
Christoph Hellwig6c60ff02021-05-14 15:18:41 +0200742 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100743 }
744
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200745 /* switch from the inode reference to a device mode one: */
746 bdev = &BDEV_I(inode)->bdev;
747 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
748 bdev = NULL;
749 iput(inode);
750
751 if (!bdev)
752 return NULL;
753 if ((bdev->bd_disk->flags & GENHD_FL_HIDDEN) ||
754 !try_module_get(bdev->bd_disk->fops->owner)) {
755 put_device(&bdev->bd_device);
756 return NULL;
757 }
758
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100759 return bdev;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100760}
761
762void blkdev_put_no_open(struct block_device *bdev)
763{
764 module_put(bdev->bd_disk->fops->owner);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200765 put_device(&bdev->bd_device);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100766}
767
Tejun Heod4d77622010-11-13 11:55:18 +0100768/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100769 * blkdev_get_by_dev - open a block device by device number
770 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +0100771 * @mode: FMODE_* mask
772 * @holder: exclusive holder identifier
773 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100774 * Open the block device described by device number @dev. If @mode includes
775 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
776 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
777 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100778 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100779 * Use this interface ONLY if you really do not have anything better - i.e. when
780 * you are behind a truly sucky interface and all you are given is a device
781 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +0100782 *
783 * CONTEXT:
784 * Might sleep.
785 *
786 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100787 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100788 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100789struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100791 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100792 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100793 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100794 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +0100795
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100796 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100797 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100798 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
799 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200800 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100801 return ERR_PTR(ret);
802
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100803 bdev = blkdev_get_no_open(dev);
804 if (!bdev)
805 return ERR_PTR(-ENXIO);
806 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200807
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100808 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100809 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100810 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100811 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100812 }
813
814 disk_block_events(disk);
815
Christoph Hellwiga8698702021-05-25 08:12:56 +0200816 mutex_lock(&disk->open_mutex);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200817 ret = -ENXIO;
Christoph Hellwig50b4aec2021-08-09 08:40:28 +0200818 if (!disk_live(disk))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200819 goto abort_claiming;
820 if (bdev_is_partition(bdev))
821 ret = blkdev_get_part(bdev, mode);
822 else
823 ret = blkdev_get_whole(bdev, mode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100824 if (ret)
825 goto abort_claiming;
826 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100827 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100828
829 /*
830 * Block event polling for write claims if requested. Any write
831 * holder makes the write_holder state stick until all are
832 * released. This is good enough and tracking individual
833 * writeable reference is too fragile given the way @mode is
834 * used in blkdev_get/put().
835 */
836 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
837 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
838 bdev->bd_write_holder = true;
839 unblock_events = false;
840 }
841 }
Christoph Hellwiga8698702021-05-25 08:12:56 +0200842 mutex_unlock(&disk->open_mutex);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100843
844 if (unblock_events)
845 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100846 return bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100847
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100848abort_claiming:
849 if (mode & FMODE_EXCL)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100850 bd_abort_claiming(bdev, holder);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200851 mutex_unlock(&disk->open_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100852 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100853put_blkdev:
854 blkdev_put_no_open(bdev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100855 return ERR_PTR(ret);
NeilBrown37be4122006-12-08 02:36:16 -0800856}
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100857EXPORT_SYMBOL(blkdev_get_by_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Tejun Heod4d77622010-11-13 11:55:18 +0100859/**
860 * blkdev_get_by_path - open a block device by name
861 * @path: path to the block device to open
862 * @mode: FMODE_* mask
863 * @holder: exclusive holder identifier
864 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100865 * Open the block device described by the device file at @path. If @mode
866 * includes %FMODE_EXCL, the block device is opened with exclusive access.
867 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
868 * nest for the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100869 *
870 * CONTEXT:
871 * Might sleep.
872 *
873 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100874 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100875 */
876struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
877 void *holder)
878{
879 struct block_device *bdev;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100880 dev_t dev;
881 int error;
Tejun Heod4d77622010-11-13 11:55:18 +0100882
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100883 error = lookup_bdev(path, &dev);
884 if (error)
885 return ERR_PTR(error);
Tejun Heod4d77622010-11-13 11:55:18 +0100886
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100887 bdev = blkdev_get_by_dev(dev, mode, holder);
888 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
Chuck Ebberte51900f2011-02-16 18:11:53 -0500889 blkdev_put(bdev, mode);
890 return ERR_PTR(-EACCES);
891 }
892
Tejun Heod4d77622010-11-13 11:55:18 +0100893 return bdev;
894}
895EXPORT_SYMBOL(blkdev_get_by_path);
896
Al Viro4385bab2013-05-05 22:11:03 -0400897void blkdev_put(struct block_device *bdev, fmode_t mode)
Al Viro572c4892007-10-08 13:24:05 -0400898{
899 struct gendisk *disk = bdev->bd_disk;
Al Viro572c4892007-10-08 13:24:05 -0400900
901 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 * Sync early if it looks like we're the last one. If someone else
Pavel Emelianov6a2aae02006-10-28 10:38:33 -0700903 * opens the block device between now and the decrement of bd_openers
904 * then we did a sync that we didn't need to, but that's not the end
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * of the world and we want to avoid long (could be several minute)
Al Viro572c4892007-10-08 13:24:05 -0400906 * syncs while holding the mutex.
907 */
Tejun Heoe525fd82010-11-13 11:55:17 +0100908 if (bdev->bd_openers == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 sync_blockdev(bdev);
910
Christoph Hellwiga8698702021-05-25 08:12:56 +0200911 mutex_lock(&disk->open_mutex);
Tejun Heoe525fd82010-11-13 11:55:17 +0100912 if (mode & FMODE_EXCL) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100913 struct block_device *whole = bdev_whole(bdev);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100914 bool bdev_free;
915
916 /*
917 * Release a claim on the device. The holder fields
Christoph Hellwiga8698702021-05-25 08:12:56 +0200918 * are protected with bdev_lock. open_mutex is to
Tejun Heo6a027ef2010-11-13 11:55:17 +0100919 * synchronize disk_holder unlinking.
920 */
Tejun Heo6a027ef2010-11-13 11:55:17 +0100921 spin_lock(&bdev_lock);
922
923 WARN_ON_ONCE(--bdev->bd_holders < 0);
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100924 WARN_ON_ONCE(--whole->bd_holders < 0);
Tejun Heo6a027ef2010-11-13 11:55:17 +0100925
Tejun Heo6a027ef2010-11-13 11:55:17 +0100926 if ((bdev_free = !bdev->bd_holders))
927 bdev->bd_holder = NULL;
Christoph Hellwiga954ea82020-11-23 13:29:55 +0100928 if (!whole->bd_holders)
929 whole->bd_holder = NULL;
Tejun Heo6a027ef2010-11-13 11:55:17 +0100930
931 spin_unlock(&bdev_lock);
932
Tejun Heo77ea8872010-12-08 20:57:37 +0100933 /*
934 * If this was the last claim, remove holder link and
935 * unblock evpoll if it was a write holder.
936 */
Tejun Heo85ef06d2011-07-01 16:17:47 +0200937 if (bdev_free && bdev->bd_write_holder) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100938 disk_unblock_events(disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200939 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +0100940 }
Tejun Heo69362172011-03-09 19:54:27 +0100941 }
Tejun Heo77ea8872010-12-08 20:57:37 +0100942
Tejun Heo85ef06d2011-07-01 16:17:47 +0200943 /*
944 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
945 * event. This is to ensure detection of media removal commanded
946 * from userland - e.g. eject(1).
947 */
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100948 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
Tejun Heo85ef06d2011-07-01 16:17:47 +0200949
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200950 if (bdev_is_partition(bdev))
951 blkdev_put_part(bdev, mode);
952 else
953 blkdev_put_whole(bdev, mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200954 mutex_unlock(&disk->open_mutex);
955
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100956 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -0800957}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800958EXPORT_SYMBOL(blkdev_put);
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960/**
961 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -0800962 * @pathname: special file representing the block device
Randy Dunlap875b2372020-12-28 19:47:06 -0800963 * @dev: return value of the block device's dev_t
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 *
Randy Dunlap57d1b532008-10-09 10:42:38 +0200965 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 * namespace if possible and return it. Return ERR_PTR(error)
967 * otherwise.
968 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100969int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -0400972 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 int error;
974
Al Viro421748e2008-08-02 01:04:36 -0400975 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100976 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Al Viro421748e2008-08-02 01:04:36 -0400978 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100980 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
David Howellsbb6687342015-03-17 22:26:21 +0000982 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 error = -ENOTBLK;
984 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100985 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -0500987 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100988 goto out_path_put;
989
990 *dev = inode->i_rdev;
991 error = 0;
992out_path_put:
Al Viro421748e2008-08-02 01:04:36 -0400993 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100994 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
Al Virod5686b42008-08-01 05:00:11 -0400996EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
NeilBrown93b270f2011-02-24 17:25:47 +1100998int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +0100999{
1000 struct super_block *sb = get_super(bdev);
1001 int res = 0;
1002
1003 if (sb) {
1004 /*
1005 * no need to lock the super, get_super holds the
1006 * read mutex so the filesystem cannot go away
1007 * under us (->put_super runs with the write lock
1008 * hold).
1009 */
1010 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001011 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001012 drop_super(sb);
1013 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001014 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001015 return res;
1016}
1017EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001018
1019void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1020{
1021 struct inode *inode, *old_inode = NULL;
1022
Dave Chinner74278da2015-03-04 12:37:22 -05001023 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001024 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1025 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001026 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001027
1028 spin_lock(&inode->i_lock);
1029 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1030 mapping->nrpages == 0) {
1031 spin_unlock(&inode->i_lock);
1032 continue;
1033 }
1034 __iget(inode);
1035 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05001036 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001037 /*
1038 * We hold a reference to 'inode' so it couldn't have been
1039 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05001040 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001041 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05001042 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001043 * later.
1044 */
1045 iput(old_inode);
1046 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001047 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001048
Christoph Hellwiga8698702021-05-25 08:12:56 +02001049 mutex_lock(&bdev->bd_disk->open_mutex);
Rabin Vincentaf309222016-12-01 09:18:28 +01001050 if (bdev->bd_openers)
1051 func(bdev, arg);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001052 mutex_unlock(&bdev->bd_disk->open_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001053
Dave Chinner74278da2015-03-04 12:37:22 -05001054 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001055 }
Dave Chinner74278da2015-03-04 12:37:22 -05001056 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001057 iput(old_inode);
1058}