blob: edc110d90df4041e7d337976951bd0d17525f1f7 [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>
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>
Eric Biggers2d985f82022-08-26 23:58:45 -070029#include <linux/stat.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);
Christoph Hellwig5bf83e92022-07-13 07:53:09 +020058 if (ret)
59 pr_warn_ratelimited(
60 "VFS: Dirty inode writeback failed for block device %pg (err=%d).\n",
61 bdev, ret);
Christoph Hellwig564f00f2015-01-14 10:42:33 +010062 spin_lock(&inode->i_lock);
63 }
64 spin_unlock(&inode->i_lock);
65}
66
Peter Zijlstraf9a14392007-05-06 14:49:55 -070067/* Kill _all_ buffers and pagecache , dirty or not.. */
Zheng Bin3373a342020-06-18 12:21:38 +080068static void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Al Viroff01bb42011-09-16 02:31:11 -040070 struct address_space *mapping = bdev->bd_inode->i_mapping;
71
Matthew Wilcox (Oracle)77165062021-05-04 18:32:45 -070072 if (mapping_empty(mapping))
Peter Zijlstraf9a14392007-05-06 14:49:55 -070073 return;
Al Viroff01bb42011-09-16 02:31:11 -040074
Peter Zijlstraf9a14392007-05-06 14:49:55 -070075 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040076 truncate_inode_pages(mapping, 0);
Zheng Bin3373a342020-06-18 12:21:38 +080077}
Al Viroff01bb42011-09-16 02:31:11 -040078
79/* Invalidate clean unused buffers and pagecache. */
80void invalidate_bdev(struct block_device *bdev)
81{
82 struct address_space *mapping = bdev->bd_inode->i_mapping;
83
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070084 if (mapping->nrpages) {
85 invalidate_bh_lrus();
86 lru_add_drain_all(); /* make sure all lru add caches are flushed */
87 invalidate_mapping_pages(mapping, 0, -1);
88 }
Al Viroff01bb42011-09-16 02:31:11 -040089}
90EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Jan Kara384d87e2020-09-04 10:58:52 +020092/*
93 * Drop all buffers & page cache for given bdev range. This function bails
94 * with error if bdev has other exclusive owner (such as filesystem).
95 */
96int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
97 loff_t lstart, loff_t lend)
98{
Jan Kara384d87e2020-09-04 10:58:52 +020099 /*
100 * If we don't hold exclusive handle for the device, upgrade to it
101 * while we discard the buffer cache to avoid discarding buffers
102 * under live filesystem.
103 */
104 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100105 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200106 if (err)
Jan Kara56887cf2021-02-22 10:48:09 +0100107 goto invalidate;
Jan Kara384d87e2020-09-04 10:58:52 +0200108 }
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100109
Jan Kara384d87e2020-09-04 10:58:52 +0200110 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100111 if (!(mode & FMODE_EXCL))
112 bd_abort_claiming(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200113 return 0;
Jan Kara56887cf2021-02-22 10:48:09 +0100114
115invalidate:
116 /*
117 * Someone else has handle exclusively open. Try invalidating instead.
118 * The 'end' argument is inclusive so the rounding is safe.
119 */
120 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
121 lstart >> PAGE_SHIFT,
122 lend >> PAGE_SHIFT);
Jan Kara384d87e2020-09-04 10:58:52 +0200123}
Jan Kara384d87e2020-09-04 10:58:52 +0200124
Jan Kara04906b22019-01-14 09:48:10 +0100125static void set_init_blocksize(struct block_device *bdev)
126{
Maxim Mikityanskiy8dc932d2021-01-26 21:59:07 +0200127 unsigned int bsize = bdev_logical_block_size(bdev);
128 loff_t size = i_size_read(bdev->bd_inode);
129
130 while (bsize < PAGE_SIZE) {
131 if (size & bsize)
132 break;
133 bsize <<= 1;
134 }
135 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
Jan Kara04906b22019-01-14 09:48:10 +0100136}
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138int set_blocksize(struct block_device *bdev, int size)
139{
140 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700141 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 return -EINVAL;
143
144 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400145 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 return -EINVAL;
147
148 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200149 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 bdev->bd_inode->i_blkbits = blksize_bits(size);
152 kill_bdev(bdev);
153 }
154 return 0;
155}
156
157EXPORT_SYMBOL(set_blocksize);
158
159int sb_set_blocksize(struct super_block *sb, int size)
160{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 if (set_blocksize(sb->s_bdev, size))
162 return 0;
163 /* If we get here, we know size is power of two
164 * and it's value is between 512 and PAGE_SIZE */
165 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800166 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return sb->s_blocksize;
168}
169
170EXPORT_SYMBOL(sb_set_blocksize);
171
172int sb_min_blocksize(struct super_block *sb, int size)
173{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400174 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (size < minsize)
176 size = minsize;
177 return sb_set_blocksize(sb, size);
178}
179
180EXPORT_SYMBOL(sb_min_blocksize);
181
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200182int sync_blockdev_nowait(struct block_device *bdev)
Jan Kara5cee5812009-04-27 16:43:51 +0200183{
184 if (!bdev)
185 return 0;
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200186 return filemap_flush(bdev->bd_inode->i_mapping);
Jan Kara5cee5812009-04-27 16:43:51 +0200187}
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200188EXPORT_SYMBOL_GPL(sync_blockdev_nowait);
Jan Kara5cee5812009-04-27 16:43:51 +0200189
Nick Piggin585d3bc2009-02-25 10:44:19 +0100190/*
191 * Write out and wait upon all the dirty data associated with a block
192 * device via its mapping. Does not take the superblock lock.
193 */
194int sync_blockdev(struct block_device *bdev)
195{
Christoph Hellwig70164eb2021-10-19 08:25:25 +0200196 if (!bdev)
197 return 0;
198 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100199}
200EXPORT_SYMBOL(sync_blockdev);
201
Yuezhang Mo97d6fb1b2022-04-12 12:23:10 +0900202int sync_blockdev_range(struct block_device *bdev, loff_t lstart, loff_t lend)
203{
204 return filemap_write_and_wait_range(bdev->bd_inode->i_mapping,
205 lstart, lend);
206}
207EXPORT_SYMBOL(sync_blockdev_range);
208
Nick Piggin585d3bc2009-02-25 10:44:19 +0100209/*
210 * Write out and wait upon all dirty data associated with this
211 * device. Filesystem data as well as the underlying block
212 * device. Takes the superblock lock.
213 */
214int fsync_bdev(struct block_device *bdev)
215{
216 struct super_block *sb = get_super(bdev);
217 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200218 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100219 drop_super(sb);
220 return res;
221 }
222 return sync_blockdev(bdev);
223}
Al Viro47e44912009-04-01 07:07:16 -0400224EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100225
226/**
Randy Dunlap2e833c82022-11-30 23:03:31 -0800227 * freeze_bdev - lock a filesystem and force it into a consistent state
Nick Piggin585d3bc2009-02-25 10:44:19 +0100228 * @bdev: blockdevice to lock
229 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100230 * If a superblock is found on this device, we take the s_umount semaphore
231 * on it to make sure nobody unmounts until the snapshot creation is done.
232 * The reference counter (bd_fsfreeze_count) guarantees that only the last
233 * unfreeze process can unfreeze the frozen filesystem actually when multiple
234 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
235 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
236 * actually.
237 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100238int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100239{
240 struct super_block *sb;
241 int error = 0;
242
243 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100244 if (++bdev->bd_fsfreeze_count > 1)
245 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100246
Christoph Hellwig45042302009-08-03 23:28:35 +0200247 sb = get_active_super(bdev);
248 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100249 goto sync;
Benjamin Marzinski48b6bca62014-11-13 20:42:03 -0600250 if (sb->s_op->freeze_super)
251 error = sb->s_op->freeze_super(sb);
252 else
253 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400254 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100255
256 if (error) {
257 bdev->bd_fsfreeze_count--;
258 goto done;
259 }
260 bdev->bd_fsfreeze_sb = sb;
261
262sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100263 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100264done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100265 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100266 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100267}
268EXPORT_SYMBOL(freeze_bdev);
269
270/**
Randy Dunlap2e833c82022-11-30 23:03:31 -0800271 * thaw_bdev - unlock filesystem
Nick Piggin585d3bc2009-02-25 10:44:19 +0100272 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100273 *
274 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
275 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100276int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100277{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100278 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200279 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100280
281 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200282 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400283 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100284
Christoph Hellwig45042302009-08-03 23:28:35 +0200285 error = 0;
286 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400287 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100288
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100289 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200290 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400291 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200292
Benjamin Marzinski48b6bca62014-11-13 20:42:03 -0600293 if (sb->s_op->thaw_super)
294 error = sb->s_op->thaw_super(sb);
295 else
296 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200297 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400298 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000299 else
300 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400301out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100302 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200303 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100304}
305EXPORT_SYMBOL(thaw_bdev);
306
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700307/**
308 * bdev_read_page() - Start reading a page from a block device
309 * @bdev: The device to read the page from
310 * @sector: The offset on the device to read the page to (need not be aligned)
311 * @page: The page to read
312 *
313 * On entry, the page should be locked. It will be unlocked when the page
314 * has been read. If the block driver implements rw_page synchronously,
315 * that will be true on exit from this function, but it need not be.
316 *
317 * Errors returned by this function are usually "soft", eg out of memory, or
318 * queue full; callers should try a different route to read this page rather
319 * than propagate an error back up the stack.
320 *
321 * Return: negative errno if an error occurs, 0 if submission was successful.
322 */
323int bdev_read_page(struct block_device *bdev, sector_t sector,
324 struct page *page)
325{
326 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800327 int result = -EOPNOTSUPP;
328
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400329 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800330 return result;
331
Pavel Begunkov025a3862021-10-14 15:03:27 +0100332 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800333 if (result)
334 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700335 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
336 REQ_OP_READ);
Pavel Begunkov025a3862021-10-14 15:03:27 +0100337 blk_queue_exit(bdev_get_queue(bdev));
Dan Williams2e6edc952015-11-19 13:29:28 -0800338 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700339}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700340
341/**
342 * bdev_write_page() - Start writing a page to a block device
343 * @bdev: The device to write the page to
344 * @sector: The offset on the device to write the page to (need not be aligned)
345 * @page: The page to write
346 * @wbc: The writeback_control for the write
347 *
348 * On entry, the page should be locked and not currently under writeback.
349 * On exit, if the write started successfully, the page will be unlocked and
350 * under writeback. If the write failed already (eg the driver failed to
351 * queue the page to the device), the page will still be locked. If the
352 * caller is a ->writepage implementation, it will need to unlock the page.
353 *
354 * Errors returned by this function are usually "soft", eg out of memory, or
355 * queue full; callers should try a different route to write this page rather
356 * than propagate an error back up the stack.
357 *
358 * Return: negative errno if an error occurs, 0 if submission was successful.
359 */
360int bdev_write_page(struct block_device *bdev, sector_t sector,
361 struct page *page, struct writeback_control *wbc)
362{
363 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700364 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800365
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400366 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700367 return -EOPNOTSUPP;
Pavel Begunkov025a3862021-10-14 15:03:27 +0100368 result = blk_queue_enter(bdev_get_queue(bdev), 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800369 if (result)
370 return result;
371
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700372 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700373 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
374 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700375 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700376 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700377 } else {
378 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700379 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700380 }
Pavel Begunkov025a3862021-10-14 15:03:27 +0100381 blk_queue_exit(bdev_get_queue(bdev));
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700382 return result;
383}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * pseudo-fs
387 */
388
389static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800390static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392static struct inode *bdev_alloc_inode(struct super_block *sb)
393{
Muchun Songfd60b282022-03-22 14:41:03 -0700394 struct bdev_inode *ei = alloc_inode_sb(sb, bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 if (!ei)
397 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100398 memset(&ei->bdev, 0, sizeof(ei->bdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 return &ei->vfs_inode;
400}
401
Al Viro41149cb2019-04-10 15:12:38 -0400402static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100404 struct block_device *bdev = I_BDEV(inode);
405
406 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100407 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100408
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200409 if (!bdev_is_partition(bdev)) {
410 if (bdev->bd_disk && bdev->bd_disk->bdi)
411 bdi_put(bdev->bd_disk->bdi);
Christoph Hellwig340e8452021-07-22 09:53:54 +0200412 kfree(bdev->bd_disk);
Christoph Hellwig889c05c2021-08-16 14:26:14 +0200413 }
Christoph Hellwig9451aa02021-08-16 14:26:13 +0200414
415 if (MAJOR(bdev->bd_dev) == BLOCK_EXT_MAJOR)
416 blk_free_ext_minor(MINOR(bdev->bd_dev));
417
Al Viro41149cb2019-04-10 15:12:38 -0400418 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100419}
420
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100421static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100423 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Christoph Lametera35afb82007-05-16 22:10:57 -0700425 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426}
427
Al Virob57922d2010-06-07 14:34:48 -0400428static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700430 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400431 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200432 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800435static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 .statfs = simple_statfs,
437 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400438 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400440 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441};
442
David Howells9030d162019-03-25 16:38:23 +0000443static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
David Howells9030d162019-03-25 16:38:23 +0000445 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
446 if (!ctx)
447 return -ENOMEM;
448 fc->s_iflags |= SB_I_CGROUPWB;
449 ctx->ops = &bdev_sops;
450 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
453static struct file_system_type bd_type = {
454 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000455 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 .kill_sb = kill_anon_super,
457};
458
Tejun Heoa212b102015-05-22 17:13:33 -0400459struct super_block *blockdev_superblock __read_mostly;
460EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
462void __init bdev_cache_init(void)
463{
464 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300465 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800468 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800469 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900470 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 err = register_filesystem(&bd_type);
472 if (err)
473 panic("Cannot register bdev pseudo-fs");
474 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (IS_ERR(bd_mnt))
476 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300477 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478}
479
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100480struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481{
482 struct block_device *bdev;
483 struct inode *inode;
484
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100485 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (!inode)
487 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100488 inode->i_mode = S_IFBLK;
489 inode->i_rdev = 0;
490 inode->i_data.a_ops = &def_blk_aops;
491 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100493 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100494 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100495 spin_lock_init(&bdev->bd_size_lock);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100496 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100497 bdev->bd_inode = inode;
Pavel Begunkov17220ca2021-10-14 15:03:26 +0100498 bdev->bd_queue = disk->queue;
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100499 bdev->bd_stats = alloc_percpu(struct disk_stats);
500 if (!bdev->bd_stats) {
501 iput(inode);
502 return NULL;
503 }
Tetsuo Handa06cc9782021-10-02 18:23:02 +0900504 bdev->bd_disk = disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 return bdev;
506}
507
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100508void bdev_add(struct block_device *bdev, dev_t dev)
509{
510 bdev->bd_dev = dev;
511 bdev->bd_inode->i_rdev = dev;
512 bdev->bd_inode->i_ino = dev;
513 insert_inode_hash(bdev->bd_inode);
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516long nr_blockdev_pages(void)
517{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200518 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200520
521 spin_lock(&blockdev_superblock->s_inode_list_lock);
522 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
523 ret += inode->i_mapping->nrpages;
524 spin_unlock(&blockdev_superblock->s_inode_list_lock);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return ret;
527}
528
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900529/**
530 * bd_may_claim - test whether a block device can be claimed
531 * @bdev: block device of interest
532 * @whole: whole block device containing @bdev, may equal @bdev
533 * @holder: holder trying to claim @bdev
534 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300535 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900536 *
537 * CONTEXT:
538 * spin_lock(&bdev_lock).
539 *
540 * RETURNS:
541 * %true if @bdev can be claimed, %false otherwise.
542 */
543static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
544 void *holder)
545{
546 if (bdev->bd_holder == holder)
547 return true; /* already a holder */
548 else if (bdev->bd_holder != NULL)
549 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700550 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900551 return true; /* is a whole device which isn't held */
552
Tejun Heoe525fd82010-11-13 11:55:17 +0100553 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900554 return true; /* is a partition of a device that is being partitioned */
555 else if (whole->bd_holder != NULL)
556 return false; /* is a partition of a held device */
557 else
558 return true; /* is a partition of an un-held device */
559}
560
561/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200562 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +0900563 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +0900564 * @holder: holder trying to claim @bdev
565 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200566 * Claim @bdev. This function fails if @bdev is already claimed by another
567 * holder and waits if another claiming is in progress. return, the caller
568 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +0900569 *
570 * RETURNS:
571 * 0 if @bdev can be claimed, -EBUSY otherwise.
572 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100573int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +0900574{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100575 struct block_device *whole = bdev_whole(bdev);
576
577 if (WARN_ON_ONCE(!holder))
578 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +0900579retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200580 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900581 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200582 if (!bd_may_claim(bdev, whole, holder)) {
583 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900584 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200585 }
Tejun Heo6b4517a2010-04-07 18:53:59 +0900586
Tejun Heoe75aa852010-08-04 17:59:39 +0200587 /* if claiming is already in progress, wait for it to finish */
588 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +0900589 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
590 DEFINE_WAIT(wait);
591
592 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
593 spin_unlock(&bdev_lock);
594 schedule();
595 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900596 goto retry;
597 }
598
599 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200600 whole->bd_claiming = holder;
601 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900602 return 0;
603}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +0200604EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +0900605
Jan Kara89e524c02019-07-30 13:10:14 +0200606static void bd_clear_claiming(struct block_device *whole, void *holder)
607{
608 lockdep_assert_held(&bdev_lock);
609 /* tell others that we're done */
610 BUG_ON(whole->bd_claiming != holder);
611 whole->bd_claiming = NULL;
612 wake_up_bit(&whole->bd_claiming, 0);
613}
614
615/**
616 * bd_finish_claiming - finish claiming of a block device
617 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200618 * @holder: holder that has claimed @bdev
619 *
620 * Finish exclusive open of a block device. Mark the device as exlusively
621 * open by the holder and wake up all waiters for exclusive open to finish.
622 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100623static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200624{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100625 struct block_device *whole = bdev_whole(bdev);
626
Jan Kara89e524c02019-07-30 13:10:14 +0200627 spin_lock(&bdev_lock);
628 BUG_ON(!bd_may_claim(bdev, whole, holder));
629 /*
630 * Note that for a whole device bd_holders will be incremented twice,
631 * and bd_holder will be set to bd_may_claim before being set to holder
632 */
633 whole->bd_holders++;
634 whole->bd_holder = bd_may_claim;
635 bdev->bd_holders++;
636 bdev->bd_holder = holder;
637 bd_clear_claiming(whole, holder);
638 spin_unlock(&bdev_lock);
639}
Jan Kara89e524c02019-07-30 13:10:14 +0200640
641/**
642 * bd_abort_claiming - abort claiming of a block device
643 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +0200644 * @holder: holder that has claimed @bdev
645 *
646 * Abort claiming of a block device when the exclusive open failed. This can be
647 * also used when exclusive open is not actually desired and we just needed
648 * to block other exclusive openers for a while.
649 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100650void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +0200651{
652 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100653 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +0200654 spin_unlock(&bdev_lock);
655}
656EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +0900657
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200658static void blkdev_flush_mapping(struct block_device *bdev)
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100659{
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200660 WARN_ON_ONCE(bdev->bd_holders);
Christoph Hellwigd3c4a432021-04-06 08:22:55 +0200661 sync_blockdev(bdev);
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200662 kill_bdev(bdev);
663 bdev_write_inode(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +0100664}
665
Christoph Hellwig362529d2021-05-25 08:12:54 +0200666static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100668 struct gendisk *disk = bdev->bd_disk;
Colin Ian Kingaf22fef2021-11-26 23:06:52 +0000669 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Christoph Hellwig362529d2021-05-25 08:12:54 +0200671 if (disk->fops->open) {
672 ret = disk->fops->open(bdev, mode);
673 if (ret) {
674 /* avoid ghost partitions on a removed medium */
675 if (ret == -ENOMEDIUM &&
676 test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200677 bdev_disk_changed(disk, true);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 }
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200682 if (!atomic_read(&bdev->bd_openers))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200683 set_init_blocksize(bdev);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200684 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
Christoph Hellwig03842642021-06-24 14:32:40 +0200685 bdev_disk_changed(disk, false);
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200686 atomic_inc(&bdev->bd_openers);
Nian Yanchuan483546c2022-02-28 01:01:24 +0800687 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200688}
689
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200690static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
691{
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200692 if (atomic_dec_and_test(&bdev->bd_openers))
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200693 blkdev_flush_mapping(bdev);
694 if (bdev->bd_disk->fops->release)
695 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
696}
697
Christoph Hellwig362529d2021-05-25 08:12:54 +0200698static int blkdev_get_part(struct block_device *part, fmode_t mode)
699{
700 struct gendisk *disk = part->bd_disk;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200701 int ret;
702
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200703 if (atomic_read(&part->bd_openers))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200704 goto done;
705
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200706 ret = blkdev_get_whole(bdev_whole(part), mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +0200707 if (ret)
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200708 return ret;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200709
710 ret = -ENXIO;
711 if (!bdev_nr_sectors(part))
712 goto out_blkdev_put;
713
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200714 disk->open_partitions++;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200715 set_init_blocksize(part);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200716done:
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200717 atomic_inc(&part->bd_openers);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200719
720out_blkdev_put:
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200721 blkdev_put_whole(bdev_whole(part), mode);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200722 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723}
724
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200725static void blkdev_put_part(struct block_device *part, fmode_t mode)
726{
727 struct block_device *whole = bdev_whole(part);
728
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200729 if (!atomic_dec_and_test(&part->bd_openers))
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200730 return;
731 blkdev_flush_mapping(part);
Christoph Hellwigab4b5702021-05-25 08:12:59 +0200732 whole->bd_disk->open_partitions--;
Christoph Hellwigc8276b92021-05-25 08:12:58 +0200733 blkdev_put_whole(whole, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734}
735
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100736struct block_device *blkdev_get_no_open(dev_t dev)
737{
738 struct block_device *bdev;
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200739 struct inode *inode;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100740
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200741 inode = ilookup(blockdev_superblock, dev);
Christoph Hellwigfbdee712022-01-04 08:16:47 +0100742 if (!inode && IS_ENABLED(CONFIG_BLOCK_LEGACY_AUTOLOAD)) {
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100743 blk_request_module(dev);
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200744 inode = ilookup(blockdev_superblock, dev);
Christoph Hellwigfbdee712022-01-04 08:16:47 +0100745 if (inode)
746 pr_warn_ratelimited(
Christoph Hellwig451f0b62022-02-25 19:14:40 +0100747"block device autoloading is deprecated and will be removed.\n");
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100748 }
Christoph Hellwigfbdee712022-01-04 08:16:47 +0100749 if (!inode)
750 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100751
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200752 /* switch from the inode reference to a device mode one: */
753 bdev = &BDEV_I(inode)->bdev;
754 if (!kobject_get_unless_zero(&bdev->bd_device.kobj))
755 bdev = NULL;
756 iput(inode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100757 return bdev;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100758}
759
760void blkdev_put_no_open(struct block_device *bdev)
761{
Christoph Hellwig9d3b8812021-07-22 09:53:58 +0200762 put_device(&bdev->bd_device);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100763}
764
Tejun Heod4d77622010-11-13 11:55:18 +0100765/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100766 * blkdev_get_by_dev - open a block device by device number
767 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +0100768 * @mode: FMODE_* mask
769 * @holder: exclusive holder identifier
770 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100771 * Open the block device described by device number @dev. If @mode includes
772 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
773 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
774 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +0100775 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100776 * Use this interface ONLY if you really do not have anything better - i.e. when
777 * you are behind a truly sucky interface and all you are given is a device
778 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +0100779 *
780 * CONTEXT:
781 * Might sleep.
782 *
783 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100784 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +0100785 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100786struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100788 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100789 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100790 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100791 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +0100792
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100793 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100794 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +0100795 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
796 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200797 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100798 return ERR_PTR(ret);
799
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100800 bdev = blkdev_get_no_open(dev);
801 if (!bdev)
802 return ERR_PTR(-ENXIO);
803 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +0200804
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100805 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100806 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100807 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100808 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100809 }
810
811 disk_block_events(disk);
812
Christoph Hellwiga8698702021-05-25 08:12:56 +0200813 mutex_lock(&disk->open_mutex);
Christoph Hellwig362529d2021-05-25 08:12:54 +0200814 ret = -ENXIO;
Christoph Hellwig50b4aec2021-08-09 08:40:28 +0200815 if (!disk_live(disk))
Christoph Hellwig362529d2021-05-25 08:12:54 +0200816 goto abort_claiming;
Ming Leiefcf5932021-11-11 10:03:43 +0800817 if (!try_module_get(disk->fops->owner))
818 goto abort_claiming;
Christoph Hellwig362529d2021-05-25 08:12:54 +0200819 if (bdev_is_partition(bdev))
820 ret = blkdev_get_part(bdev, mode);
821 else
822 ret = blkdev_get_whole(bdev, mode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100823 if (ret)
Ming Leiefcf5932021-11-11 10:03:43 +0800824 goto put_module;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100825 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100826 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100827
828 /*
829 * Block event polling for write claims if requested. Any write
830 * holder makes the write_holder state stick until all are
831 * released. This is good enough and tracking individual
832 * writeable reference is too fragile given the way @mode is
833 * used in blkdev_get/put().
834 */
835 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
Christoph Hellwig1545e0b2021-11-22 14:06:13 +0100836 (disk->event_flags & DISK_EVENT_FLAG_BLOCK_ON_EXCL_WRITE)) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100837 bdev->bd_write_holder = true;
838 unblock_events = false;
839 }
840 }
Christoph Hellwiga8698702021-05-25 08:12:56 +0200841 mutex_unlock(&disk->open_mutex);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +0100842
843 if (unblock_events)
844 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100845 return bdev;
Ming Leiefcf5932021-11-11 10:03:43 +0800846put_module:
847 module_put(disk->fops->owner);
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 */
Christoph Hellwig9acf3812022-03-30 07:29:07 +0200908 if (atomic_read(&bdev->bd_openers) == 1)
Tejun Heoe525fd82010-11-13 11:55:17 +0100909 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
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
Ming Leiefcf5932021-11-11 10:03:43 +0800956 module_put(disk->fops->owner);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100957 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -0800958}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -0800959EXPORT_SYMBOL(blkdev_put);
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961/**
Matthew Wilcox (Oracle)0ba45662021-12-13 17:11:13 +0000962 * lookup_bdev() - Look up a struct block_device by name.
963 * @pathname: Name of the block device in the filesystem.
964 * @dev: Pointer to the block device's dev_t, if found.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 *
Jackie Liu057178c2021-10-21 15:13:43 +0800966 * Lookup the block device's dev_t at @pathname in the current
Matthew Wilcox (Oracle)0ba45662021-12-13 17:11:13 +0000967 * namespace if possible and return it in @dev.
Jackie Liu057178c2021-10-21 15:13:43 +0800968 *
Matthew Wilcox (Oracle)0ba45662021-12-13 17:11:13 +0000969 * Context: May sleep.
970 * Return: 0 if succeeded, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100972int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -0400975 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 int error;
977
Al Viro421748e2008-08-02 01:04:36 -0400978 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100979 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Al Viro421748e2008-08-02 01:04:36 -0400981 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100983 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
David Howellsbb6687342015-03-17 22:26:21 +0000985 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 error = -ENOTBLK;
987 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100988 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -0500990 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100991 goto out_path_put;
992
993 *dev = inode->i_rdev;
994 error = 0;
995out_path_put:
Al Viro421748e2008-08-02 01:04:36 -0400996 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +0100997 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
Al Virod5686b42008-08-01 05:00:11 -0400999EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
NeilBrown93b270f2011-02-24 17:25:47 +11001001int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001002{
1003 struct super_block *sb = get_super(bdev);
1004 int res = 0;
1005
1006 if (sb) {
1007 /*
1008 * no need to lock the super, get_super holds the
1009 * read mutex so the filesystem cannot go away
1010 * under us (->put_super runs with the write lock
1011 * hold).
1012 */
1013 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001014 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001015 drop_super(sb);
1016 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001017 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001018 return res;
1019}
1020EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001021
Christoph Hellwig1e03a362021-10-19 08:25:30 +02001022void sync_bdevs(bool wait)
Jan Kara5c0d6b62012-07-03 16:45:31 +02001023{
1024 struct inode *inode, *old_inode = NULL;
1025
Dave Chinner74278da92015-03-04 12:37:22 -05001026 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001027 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1028 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001029 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001030
1031 spin_lock(&inode->i_lock);
1032 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1033 mapping->nrpages == 0) {
1034 spin_unlock(&inode->i_lock);
1035 continue;
1036 }
1037 __iget(inode);
1038 spin_unlock(&inode->i_lock);
Dave Chinner74278da92015-03-04 12:37:22 -05001039 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001040 /*
1041 * We hold a reference to 'inode' so it couldn't have been
1042 * removed from s_inodes list while we dropped the
Dave Chinner74278da92015-03-04 12:37:22 -05001043 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001044 * be holding the last reference and we cannot iput it under
Dave Chinner74278da92015-03-04 12:37:22 -05001045 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001046 * later.
1047 */
1048 iput(old_inode);
1049 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001050 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001051
Christoph Hellwiga8698702021-05-25 08:12:56 +02001052 mutex_lock(&bdev->bd_disk->open_mutex);
Christoph Hellwig9acf3812022-03-30 07:29:07 +02001053 if (!atomic_read(&bdev->bd_openers)) {
Christoph Hellwig1e03a362021-10-19 08:25:30 +02001054 ; /* skip */
1055 } else if (wait) {
1056 /*
1057 * We keep the error status of individual mapping so
1058 * that applications can catch the writeback error using
1059 * fsync(2). See filemap_fdatawait_keep_errors() for
1060 * details.
1061 */
1062 filemap_fdatawait_keep_errors(inode->i_mapping);
1063 } else {
1064 filemap_fdatawrite(inode->i_mapping);
1065 }
Christoph Hellwiga8698702021-05-25 08:12:56 +02001066 mutex_unlock(&bdev->bd_disk->open_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001067
Dave Chinner74278da92015-03-04 12:37:22 -05001068 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001069 }
Dave Chinner74278da92015-03-04 12:37:22 -05001070 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001071 iput(old_inode);
1072}
Eric Biggers2d985f82022-08-26 23:58:45 -07001073
1074/*
1075 * Handle STATX_DIOALIGN for block devices.
1076 *
1077 * Note that the inode passed to this is the inode of a block device node file,
1078 * not the block device's internal inode. Therefore it is *not* valid to use
1079 * I_BDEV() here; the block device has to be looked up by i_rdev instead.
1080 */
1081void bdev_statx_dioalign(struct inode *inode, struct kstat *stat)
1082{
1083 struct block_device *bdev;
1084
1085 bdev = blkdev_get_no_open(inode->i_rdev);
1086 if (!bdev)
1087 return;
1088
1089 stat->dio_mem_align = bdev_dma_alignment(bdev) + 1;
1090 stat->dio_offset_align = bdev_logical_block_size(bdev);
1091 stat->result_mask |= STATX_DIOALIGN;
1092
1093 blkdev_put_no_open(bdev);
1094}