blob: 235b5042672e9f15cd137c22965eaa3aae9f3f61 [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>
10#include <linux/fcntl.h>
11#include <linux/slab.h>
12#include <linux/kmod.h>
13#include <linux/major.h>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070014#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highmem.h>
16#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040017#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070020#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040022#include <linux/swap.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010023#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010024#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/mpage.h>
26#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000027#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/uio.h>
29#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070030#include <linux/log2.h>
Al Viroff01bb42011-09-16 02:31:11 -040031#include <linux/cleancache.h>
Christoph Hellwig189ce2b2016-10-31 11:59:25 -060032#include <linux/task_io_accounting_ops.h>
Darrick J. Wong25f4c412016-10-11 13:51:11 -070033#include <linux/falloc.h>
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +010034#include <linux/part_stat.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080035#include <linux/uaccess.h>
Domenico Andreoli56939e02020-03-23 08:22:15 -070036#include <linux/suspend.h>
David Howells07f3f052006-09-30 20:52:18 +020037#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct bdev_inode {
40 struct block_device bdev;
41 struct inode vfs_inode;
42};
43
Adrian Bunk4c54ac62008-02-18 13:48:31 +010044static const struct address_space_operations def_blk_aops;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static inline struct bdev_inode *BDEV_I(struct inode *inode)
47{
48 return container_of(inode, struct bdev_inode, vfs_inode);
49}
50
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020051struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 return &BDEV_I(inode)->bdev;
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055EXPORT_SYMBOL(I_BDEV);
56
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070057static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010058{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070059 struct inode *inode = bdev->bd_inode;
60 int ret;
61
Christoph Hellwig564f00f2015-01-14 10:42:33 +010062 spin_lock(&inode->i_lock);
63 while (inode->i_state & I_DIRTY) {
64 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070065 ret = write_inode_now(inode, true);
66 if (ret) {
67 char name[BDEVNAME_SIZE];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev, name), ret);
71 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010072 spin_lock(&inode->i_lock);
73 }
74 spin_unlock(&inode->i_lock);
75}
76
Peter Zijlstraf9a14392007-05-06 14:49:55 -070077/* Kill _all_ buffers and pagecache , dirty or not.. */
Zheng Bin3373a342020-06-18 12:21:38 +080078static void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viroff01bb42011-09-16 02:31:11 -040080 struct address_space *mapping = bdev->bd_inode->i_mapping;
81
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080082 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
Peter Zijlstraf9a14392007-05-06 14:49:55 -070083 return;
Al Viroff01bb42011-09-16 02:31:11 -040084
Peter Zijlstraf9a14392007-05-06 14:49:55 -070085 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040086 truncate_inode_pages(mapping, 0);
Zheng Bin3373a342020-06-18 12:21:38 +080087}
Al Viroff01bb42011-09-16 02:31:11 -040088
89/* Invalidate clean unused buffers and pagecache. */
90void invalidate_bdev(struct block_device *bdev)
91{
92 struct address_space *mapping = bdev->bd_inode->i_mapping;
93
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070094 if (mapping->nrpages) {
95 invalidate_bh_lrus();
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping, 0, -1);
98 }
Al Viroff01bb42011-09-16 02:31:11 -040099 /* 99% of the time, we don't need to flush the cleancache on the bdev.
100 * But, for the strange corners, lets be cautious
101 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400102 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400103}
104EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jan Kara384d87e2020-09-04 10:58:52 +0200106/*
107 * Drop all buffers & page cache for given bdev range. This function bails
108 * with error if bdev has other exclusive owner (such as filesystem).
109 */
110int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111 loff_t lstart, loff_t lend)
112{
Jan Kara384d87e2020-09-04 10:58:52 +0200113 /*
114 * If we don't hold exclusive handle for the device, upgrade to it
115 * while we discard the buffer cache to avoid discarding buffers
116 * under live filesystem.
117 */
118 if (!(mode & FMODE_EXCL)) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100119 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200120 if (err)
121 return err;
122 }
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100123
Jan Kara384d87e2020-09-04 10:58:52 +0200124 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100125 if (!(mode & FMODE_EXCL))
126 bd_abort_claiming(bdev, truncate_bdev_range);
Jan Kara384d87e2020-09-04 10:58:52 +0200127 return 0;
128}
129EXPORT_SYMBOL(truncate_bdev_range);
130
Jan Kara04906b22019-01-14 09:48:10 +0100131static void set_init_blocksize(struct block_device *bdev)
132{
Maxim Mikityanskiy8dc932d2021-01-26 21:59:07 +0200133 unsigned int bsize = bdev_logical_block_size(bdev);
134 loff_t size = i_size_read(bdev->bd_inode);
135
136 while (bsize < PAGE_SIZE) {
137 if (size & bsize)
138 break;
139 bsize <<= 1;
140 }
141 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
Jan Kara04906b22019-01-14 09:48:10 +0100142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144int set_blocksize(struct block_device *bdev, int size)
145{
146 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700147 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149
150 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400151 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return -EINVAL;
153
154 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200155 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 bdev->bd_inode->i_blkbits = blksize_bits(size);
158 kill_bdev(bdev);
159 }
160 return 0;
161}
162
163EXPORT_SYMBOL(set_blocksize);
164
165int sb_set_blocksize(struct super_block *sb, int size)
166{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (set_blocksize(sb->s_bdev, size))
168 return 0;
169 /* If we get here, we know size is power of two
170 * and it's value is between 512 and PAGE_SIZE */
171 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800172 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return sb->s_blocksize;
174}
175
176EXPORT_SYMBOL(sb_set_blocksize);
177
178int sb_min_blocksize(struct super_block *sb, int size)
179{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400180 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 if (size < minsize)
182 size = minsize;
183 return sb_set_blocksize(sb, size);
184}
185
186EXPORT_SYMBOL(sb_min_blocksize);
187
188static int
189blkdev_get_block(struct inode *inode, sector_t iblock,
190 struct buffer_head *bh, int create)
191{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 bh->b_bdev = I_BDEV(inode);
193 bh->b_blocknr = iblock;
194 set_buffer_mapped(bh);
195 return 0;
196}
197
Dan Williams4ebb16c2015-10-28 07:48:19 +0900198static struct inode *bdev_file_inode(struct file *file)
199{
200 return file->f_mapping->host;
201}
202
Jens Axboe78250c02016-11-17 17:50:47 +0100203static unsigned int dio_bio_write_op(struct kiocb *iocb)
204{
205 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
206
207 /* avoid the need for a I/O completion work item */
208 if (iocb->ki_flags & IOCB_DSYNC)
209 op |= REQ_FUA;
210 return op;
211}
212
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600213#define DIO_INLINE_BIO_VECS 4
214
215static void blkdev_bio_end_io_simple(struct bio *bio)
216{
217 struct task_struct *waiter = bio->bi_private;
218
219 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700220 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600221}
222
223static ssize_t
224__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
225 int nr_pages)
226{
227 struct file *file = iocb->ki_filp;
228 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200229 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600230 loff_t pos = iocb->ki_pos;
231 bool should_dirty = false;
232 struct bio bio;
233 ssize_t ret;
234 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600235
Jens Axboe9a794fb2016-11-22 08:12:39 -0700236 if ((pos | iov_iter_alignment(iter)) &
237 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600238 return -EINVAL;
239
Jens Axboe72ecad22016-11-16 23:11:42 -0700240 if (nr_pages <= DIO_INLINE_BIO_VECS)
241 vecs = inline_vecs;
242 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700243 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
244 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700245 if (!vecs)
246 return -ENOMEM;
247 }
248
Ming Lei3a83f462016-11-22 08:57:21 -0700249 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200250 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900251 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600252 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600253 bio.bi_private = current;
254 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700255 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600256
257 ret = bio_iov_iter_get_pages(&bio, iter);
258 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200259 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600260 ret = bio.bi_iter.bi_size;
261
262 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100263 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600264 if (iter_is_iovec(iter))
265 should_dirty = true;
266 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100267 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600268 task_io_account_write(ret);
269 }
Jens Axboed1e36282018-08-29 10:36:56 -0600270 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700271 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600272
273 qc = submit_bio(&bio);
274 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800275 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600276 if (!READ_ONCE(bio.bi_private))
277 break;
278 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700279 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800280 blk_io_schedule();
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600281 }
282 __set_current_state(TASK_RUNNING);
283
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200284 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200285 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700286 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600287
Martin Wilck9362dd12018-07-25 23:15:08 +0200288out:
289 if (vecs != inline_vecs)
290 kfree(vecs);
291
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600292 bio_uninit(&bio);
293
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600294 return ret;
295}
296
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700297struct blkdev_dio {
298 union {
299 struct kiocb *iocb;
300 struct task_struct *waiter;
301 };
302 size_t size;
303 atomic_t ref;
304 bool multi_bio : 1;
305 bool should_dirty : 1;
306 bool is_sync : 1;
307 struct bio bio;
308};
309
Kent Overstreet52190f82018-05-20 18:25:55 -0400310static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700311
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700312static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
313{
314 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
315 struct request_queue *q = bdev_get_queue(bdev);
316
317 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
318}
319
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700320static void blkdev_bio_end_io(struct bio *bio)
321{
322 struct blkdev_dio *dio = bio->bi_private;
323 bool should_dirty = dio->should_dirty;
324
Jason Yana89afe52019-04-12 10:09:16 +0800325 if (bio->bi_status && !dio->bio.bi_status)
326 dio->bio.bi_status = bio->bi_status;
327
328 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700329 if (!dio->is_sync) {
330 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200331 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700332
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200333 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700334 ret = dio->size;
335 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200336 } else {
337 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700338 }
339
340 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100341 if (dio->multi_bio)
342 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700343 } else {
344 struct task_struct *waiter = dio->waiter;
345
346 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700347 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700348 }
349 }
350
351 if (should_dirty) {
352 bio_check_pages_dirty(bio);
353 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200354 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700355 bio_put(bio);
356 }
357}
358
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800359static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700360__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800361{
362 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900363 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700364 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100365 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700366 struct blkdev_dio *dio;
367 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700368 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100369 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700370 loff_t pos = iocb->ki_pos;
371 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe7b6620d2019-08-15 11:09:16 -0600372 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700373
Jens Axboe9a794fb2016-11-22 08:12:39 -0700374 if ((pos | iov_iter_alignment(iter)) &
375 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700376 return -EINVAL;
377
Jens Axboe7b6620d2019-08-15 11:09:16 -0600378 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700379
380 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100381 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100382 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700383 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100384 bio_get(bio);
385 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700386 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100387 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700388
389 dio->size = 0;
390 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100391 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700392
Jens Axboecb700eb2018-11-15 19:56:53 -0700393 /*
394 * Don't plug for HIPRI/polled IO, as those should go straight
395 * to issue
396 */
397 if (!is_poll)
398 blk_start_plug(&plug);
399
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700400 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200401 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900402 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600403 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700404 bio->bi_private = dio;
405 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700406 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700407
Jens Axboee15c2ff2019-08-06 13:34:31 -0600408 ret = bio_iov_iter_get_pages(bio, iter);
409 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200410 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700411 bio_endio(bio);
412 break;
413 }
414
415 if (is_read) {
416 bio->bi_opf = REQ_OP_READ;
417 if (dio->should_dirty)
418 bio_set_pages_dirty(bio);
419 } else {
420 bio->bi_opf = dio_bio_write_op(iocb);
421 task_io_account_write(bio->bi_iter.bi_size);
422 }
423
Jens Axboe7b6620d2019-08-15 11:09:16 -0600424 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700425 pos += bio->bi_iter.bi_size;
426
427 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
428 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700429 bool polled = false;
430
431 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700432 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700433 polled = true;
434 }
Jens Axboed34513d2018-11-06 14:29:11 -0700435
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700436 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700437
438 if (polled)
439 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700440 break;
441 }
442
443 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100444 /*
445 * AIO needs an extra reference to ensure the dio
446 * structure which is embedded into the first bio
447 * stays around.
448 */
449 if (!is_sync)
450 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700451 dio->multi_bio = true;
452 atomic_set(&dio->ref, 2);
453 } else {
454 atomic_inc(&dio->ref);
455 }
456
Jens Axboe7b6620d2019-08-15 11:09:16 -0600457 submit_bio(bio);
458 bio = bio_alloc(GFP_KERNEL, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700459 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700460
461 if (!is_poll)
462 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700463
Christoph Hellwig690e5322017-01-24 14:50:19 +0100464 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700465 return -EIOCBQUEUED;
466
467 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800468 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700469 if (!READ_ONCE(dio->waiter))
470 break;
471
472 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700473 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800474 blk_io_schedule();
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700475 }
476 __set_current_state(TASK_RUNNING);
477
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200478 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200479 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600480 if (likely(!ret))
481 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700482
483 bio_put(&dio->bio);
484 return ret;
485}
486
487static ssize_t
488blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
489{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600490 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800491
Jens Axboe72ecad22016-11-16 23:11:42 -0700492 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600493 if (!nr_pages)
494 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700495 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600496 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700497
498 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800499}
500
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700501static __init int blkdev_init(void)
502{
Kent Overstreet52190f82018-05-20 18:25:55 -0400503 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700504}
505module_init(blkdev_init);
506
Jan Kara5cee5812009-04-27 16:43:51 +0200507int __sync_blockdev(struct block_device *bdev, int wait)
508{
509 if (!bdev)
510 return 0;
511 if (!wait)
512 return filemap_flush(bdev->bd_inode->i_mapping);
513 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
514}
515
Nick Piggin585d3bc2009-02-25 10:44:19 +0100516/*
517 * Write out and wait upon all the dirty data associated with a block
518 * device via its mapping. Does not take the superblock lock.
519 */
520int sync_blockdev(struct block_device *bdev)
521{
Jan Kara5cee5812009-04-27 16:43:51 +0200522 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100523}
524EXPORT_SYMBOL(sync_blockdev);
525
526/*
527 * Write out and wait upon all dirty data associated with this
528 * device. Filesystem data as well as the underlying block
529 * device. Takes the superblock lock.
530 */
531int fsync_bdev(struct block_device *bdev)
532{
533 struct super_block *sb = get_super(bdev);
534 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200535 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100536 drop_super(sb);
537 return res;
538 }
539 return sync_blockdev(bdev);
540}
Al Viro47e44912009-04-01 07:07:16 -0400541EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100542
543/**
544 * freeze_bdev -- lock a filesystem and force it into a consistent state
545 * @bdev: blockdevice to lock
546 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100547 * If a superblock is found on this device, we take the s_umount semaphore
548 * on it to make sure nobody unmounts until the snapshot creation is done.
549 * The reference counter (bd_fsfreeze_count) guarantees that only the last
550 * unfreeze process can unfreeze the frozen filesystem actually when multiple
551 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
552 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
553 * actually.
554 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100555int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100556{
557 struct super_block *sb;
558 int error = 0;
559
560 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100561 if (++bdev->bd_fsfreeze_count > 1)
562 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100563
Christoph Hellwig45042302009-08-03 23:28:35 +0200564 sb = get_active_super(bdev);
565 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100566 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600567 if (sb->s_op->freeze_super)
568 error = sb->s_op->freeze_super(sb);
569 else
570 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400571 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100572
573 if (error) {
574 bdev->bd_fsfreeze_count--;
575 goto done;
576 }
577 bdev->bd_fsfreeze_sb = sb;
578
579sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100580 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100581done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100582 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100583 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100584}
585EXPORT_SYMBOL(freeze_bdev);
586
587/**
588 * thaw_bdev -- unlock filesystem
589 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100590 *
591 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
592 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100593int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100594{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100595 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200596 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100597
598 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200599 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400600 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100601
Christoph Hellwig45042302009-08-03 23:28:35 +0200602 error = 0;
603 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400604 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100605
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100606 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200607 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400608 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200609
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600610 if (sb->s_op->thaw_super)
611 error = sb->s_op->thaw_super(sb);
612 else
613 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200614 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400615 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000616 else
617 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400618out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100619 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200620 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100621}
622EXPORT_SYMBOL(thaw_bdev);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
625{
626 return block_write_full_page(page, blkdev_get_block, wbc);
627}
628
629static int blkdev_readpage(struct file * file, struct page * page)
630{
631 return block_read_full_page(page, blkdev_get_block);
632}
633
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700634static void blkdev_readahead(struct readahead_control *rac)
Akinobu Mita447f05b2014-10-09 15:26:58 -0700635{
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700636 mpage_readahead(rac, blkdev_get_block);
Akinobu Mita447f05b2014-10-09 15:26:58 -0700637}
638
Nick Piggin6272b5a2007-10-16 01:25:04 -0700639static int blkdev_write_begin(struct file *file, struct address_space *mapping,
640 loff_t pos, unsigned len, unsigned flags,
641 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200643 return block_write_begin(mapping, pos, len, flags, pagep,
644 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Nick Piggin6272b5a2007-10-16 01:25:04 -0700647static int blkdev_write_end(struct file *file, struct address_space *mapping,
648 loff_t pos, unsigned len, unsigned copied,
649 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700651 int ret;
652 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
653
654 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300655 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700656
657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
660/*
661 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500662 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 * so we compute the size by hand (just as in block_read/write above)
664 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800665static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900667 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 loff_t retval;
669
Al Viro59551022016-01-22 15:40:57 -0500670 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400671 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500672 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 return retval;
674}
675
Josef Bacik02c24a82011-07-16 20:44:56 -0400676int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900678 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400679 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100680 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200681
Jeff Layton372cf242017-07-06 07:02:28 -0400682 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200683 if (error)
684 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100685
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400686 /*
687 * There is no need to serialise calls to blkdev_issue_flush with
688 * i_mutex and doing so causes performance issues with concurrent
689 * O_SYNC writers to a block device.
690 */
Christoph Hellwig93985542020-05-13 14:36:00 +0200691 error = blkdev_issue_flush(bdev, GFP_KERNEL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100692 if (error == -EOPNOTSUPP)
693 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400694
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100695 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700697EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700699/**
700 * bdev_read_page() - Start reading a page from a block device
701 * @bdev: The device to read the page from
702 * @sector: The offset on the device to read the page to (need not be aligned)
703 * @page: The page to read
704 *
705 * On entry, the page should be locked. It will be unlocked when the page
706 * has been read. If the block driver implements rw_page synchronously,
707 * that will be true on exit from this function, but it need not be.
708 *
709 * Errors returned by this function are usually "soft", eg out of memory, or
710 * queue full; callers should try a different route to read this page rather
711 * than propagate an error back up the stack.
712 *
713 * Return: negative errno if an error occurs, 0 if submission was successful.
714 */
715int bdev_read_page(struct block_device *bdev, sector_t sector,
716 struct page *page)
717{
718 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800719 int result = -EOPNOTSUPP;
720
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400721 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800722 return result;
723
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200724 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800725 if (result)
726 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700727 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
728 REQ_OP_READ);
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200729 blk_queue_exit(bdev->bd_disk->queue);
Dan Williams2e6edc952015-11-19 13:29:28 -0800730 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700731}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700732
733/**
734 * bdev_write_page() - Start writing a page to a block device
735 * @bdev: The device to write the page to
736 * @sector: The offset on the device to write the page to (need not be aligned)
737 * @page: The page to write
738 * @wbc: The writeback_control for the write
739 *
740 * On entry, the page should be locked and not currently under writeback.
741 * On exit, if the write started successfully, the page will be unlocked and
742 * under writeback. If the write failed already (eg the driver failed to
743 * queue the page to the device), the page will still be locked. If the
744 * caller is a ->writepage implementation, it will need to unlock the page.
745 *
746 * Errors returned by this function are usually "soft", eg out of memory, or
747 * queue full; callers should try a different route to write this page rather
748 * than propagate an error back up the stack.
749 *
750 * Return: negative errno if an error occurs, 0 if submission was successful.
751 */
752int bdev_write_page(struct block_device *bdev, sector_t sector,
753 struct page *page, struct writeback_control *wbc)
754{
755 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700756 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800757
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400758 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700759 return -EOPNOTSUPP;
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200760 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800761 if (result)
762 return result;
763
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700764 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700765 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
766 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700767 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700768 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700769 } else {
770 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700771 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700772 }
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200773 blk_queue_exit(bdev->bd_disk->queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700774 return result;
775}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777/*
778 * pseudo-fs
779 */
780
781static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800782static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784static struct inode *bdev_alloc_inode(struct super_block *sb)
785{
Christoph Lametere94b1762006-12-06 20:33:17 -0800786 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if (!ei)
789 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100790 memset(&ei->bdev, 0, sizeof(ei->bdev));
791 ei->bdev.bd_bdi = &noop_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 return &ei->vfs_inode;
793}
794
Al Viro41149cb2019-04-10 15:12:38 -0400795static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100797 struct block_device *bdev = I_BDEV(inode);
798
799 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100800 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100801
Al Viro41149cb2019-04-10 15:12:38 -0400802 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100803}
804
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100805static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100807 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Christoph Lametera35afb82007-05-16 22:10:57 -0700809 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Al Virob57922d2010-06-07 14:34:48 -0400812static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700815 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400816 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200817 clear_inode(inode);
Jan Karaf7597412017-03-23 01:37:00 +0100818 /* Detach inode from wb early as bdi_put() may free bdi->wb */
819 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100820 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100821 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100822 bdev->bd_bdi = &noop_backing_dev_info;
823 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824}
825
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800826static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 .statfs = simple_statfs,
828 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400829 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400831 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832};
833
David Howells9030d162019-03-25 16:38:23 +0000834static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
David Howells9030d162019-03-25 16:38:23 +0000836 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
837 if (!ctx)
838 return -ENOMEM;
839 fc->s_iflags |= SB_I_CGROUPWB;
840 ctx->ops = &bdev_sops;
841 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
843
844static struct file_system_type bd_type = {
845 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000846 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 .kill_sb = kill_anon_super,
848};
849
Tejun Heoa212b102015-05-22 17:13:33 -0400850struct super_block *blockdev_superblock __read_mostly;
851EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
853void __init bdev_cache_init(void)
854{
855 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300856 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800859 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800860 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900861 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 err = register_filesystem(&bd_type);
863 if (err)
864 panic("Cannot register bdev pseudo-fs");
865 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (IS_ERR(bd_mnt))
867 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300868 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}
870
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100871struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 struct block_device *bdev;
874 struct inode *inode;
875
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100876 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 if (!inode)
878 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100879 inode->i_mode = S_IFBLK;
880 inode->i_rdev = 0;
881 inode->i_data.a_ops = &def_blk_aops;
882 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100884 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100885 mutex_init(&bdev->bd_mutex);
886 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100887 spin_lock_init(&bdev->bd_size_lock);
888 bdev->bd_disk = disk;
889 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100890 bdev->bd_inode = inode;
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100891#ifdef CONFIG_SYSFS
892 INIT_LIST_HEAD(&bdev->bd_holder_disks);
893#endif
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100894 bdev->bd_stats = alloc_percpu(struct disk_stats);
895 if (!bdev->bd_stats) {
896 iput(inode);
897 return NULL;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 return bdev;
900}
901
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100902void bdev_add(struct block_device *bdev, dev_t dev)
903{
904 bdev->bd_dev = dev;
905 bdev->bd_inode->i_rdev = dev;
906 bdev->bd_inode->i_ino = dev;
907 insert_inode_hash(bdev->bd_inode);
908}
909
910static struct block_device *bdget(dev_t dev)
911{
912 struct inode *inode;
913
914 inode = ilookup(blockdev_superblock, dev);
915 if (!inode)
916 return NULL;
917 return &BDEV_I(inode)->bdev;
918}
919
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200920/**
921 * bdgrab -- Grab a reference to an already referenced block device
922 * @bdev: Block device to grab a reference to.
Christoph Hellwig3a4174e2020-11-27 16:45:27 +0100923 *
924 * Returns the block_device with an additional reference when successful,
925 * or NULL if the inode is already beeing freed.
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200926 */
927struct block_device *bdgrab(struct block_device *bdev)
928{
Christoph Hellwig3a4174e2020-11-27 16:45:27 +0100929 if (!igrab(bdev->bd_inode))
930 return NULL;
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200931 return bdev;
932}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700933EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935long nr_blockdev_pages(void)
936{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200937 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200939
940 spin_lock(&blockdev_superblock->s_inode_list_lock);
941 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
942 ret += inode->i_mapping->nrpages;
943 spin_unlock(&blockdev_superblock->s_inode_list_lock);
944
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 return ret;
946}
947
948void bdput(struct block_device *bdev)
949{
950 iput(bdev->bd_inode);
951}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952EXPORT_SYMBOL(bdput);
953
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900954/**
955 * bd_may_claim - test whether a block device can be claimed
956 * @bdev: block device of interest
957 * @whole: whole block device containing @bdev, may equal @bdev
958 * @holder: holder trying to claim @bdev
959 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300960 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900961 *
962 * CONTEXT:
963 * spin_lock(&bdev_lock).
964 *
965 * RETURNS:
966 * %true if @bdev can be claimed, %false otherwise.
967 */
968static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
969 void *holder)
970{
971 if (bdev->bd_holder == holder)
972 return true; /* already a holder */
973 else if (bdev->bd_holder != NULL)
974 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700975 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900976 return true; /* is a whole device which isn't held */
977
Tejun Heoe525fd82010-11-13 11:55:17 +0100978 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900979 return true; /* is a partition of a device that is being partitioned */
980 else if (whole->bd_holder != NULL)
981 return false; /* is a partition of a held device */
982 else
983 return true; /* is a partition of an un-held device */
984}
985
986/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200987 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +0900988 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +0900989 * @holder: holder trying to claim @bdev
990 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200991 * Claim @bdev. This function fails if @bdev is already claimed by another
992 * holder and waits if another claiming is in progress. return, the caller
993 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +0900994 *
995 * RETURNS:
996 * 0 if @bdev can be claimed, -EBUSY otherwise.
997 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +0100998int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +0900999{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001000 struct block_device *whole = bdev_whole(bdev);
1001
1002 if (WARN_ON_ONCE(!holder))
1003 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +09001004retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001005 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001006 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001007 if (!bd_may_claim(bdev, whole, holder)) {
1008 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001009 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001010 }
Tejun Heo6b4517a2010-04-07 18:53:59 +09001011
Tejun Heoe75aa852010-08-04 17:59:39 +02001012 /* if claiming is already in progress, wait for it to finish */
1013 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001014 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1015 DEFINE_WAIT(wait);
1016
1017 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1018 spin_unlock(&bdev_lock);
1019 schedule();
1020 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001021 goto retry;
1022 }
1023
1024 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001025 whole->bd_claiming = holder;
1026 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001027 return 0;
1028}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +02001029EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +09001030
Jan Kara89e524c02019-07-30 13:10:14 +02001031static void bd_clear_claiming(struct block_device *whole, void *holder)
1032{
1033 lockdep_assert_held(&bdev_lock);
1034 /* tell others that we're done */
1035 BUG_ON(whole->bd_claiming != holder);
1036 whole->bd_claiming = NULL;
1037 wake_up_bit(&whole->bd_claiming, 0);
1038}
1039
1040/**
1041 * bd_finish_claiming - finish claiming of a block device
1042 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +02001043 * @holder: holder that has claimed @bdev
1044 *
1045 * Finish exclusive open of a block device. Mark the device as exlusively
1046 * open by the holder and wake up all waiters for exclusive open to finish.
1047 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001048static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +02001049{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001050 struct block_device *whole = bdev_whole(bdev);
1051
Jan Kara89e524c02019-07-30 13:10:14 +02001052 spin_lock(&bdev_lock);
1053 BUG_ON(!bd_may_claim(bdev, whole, holder));
1054 /*
1055 * Note that for a whole device bd_holders will be incremented twice,
1056 * and bd_holder will be set to bd_may_claim before being set to holder
1057 */
1058 whole->bd_holders++;
1059 whole->bd_holder = bd_may_claim;
1060 bdev->bd_holders++;
1061 bdev->bd_holder = holder;
1062 bd_clear_claiming(whole, holder);
1063 spin_unlock(&bdev_lock);
1064}
Jan Kara89e524c02019-07-30 13:10:14 +02001065
1066/**
1067 * bd_abort_claiming - abort claiming of a block device
1068 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +02001069 * @holder: holder that has claimed @bdev
1070 *
1071 * Abort claiming of a block device when the exclusive open failed. This can be
1072 * also used when exclusive open is not actually desired and we just needed
1073 * to block other exclusive openers for a while.
1074 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001075void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +02001076{
1077 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001078 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +02001079 spin_unlock(&bdev_lock);
1080}
1081EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001082
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001083#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001084struct bd_holder_disk {
1085 struct list_head list;
1086 struct gendisk *disk;
1087 int refcnt;
1088};
1089
1090static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1091 struct gendisk *disk)
1092{
1093 struct bd_holder_disk *holder;
1094
1095 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1096 if (holder->disk == disk)
1097 return holder;
1098 return NULL;
1099}
1100
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001101static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001102{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001103 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001104}
1105
1106static void del_symlink(struct kobject *from, struct kobject *to)
1107{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001108 sysfs_remove_link(from, kobject_name(to));
1109}
1110
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001111/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001112 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1113 * @bdev: the claimed slave bdev
1114 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001115 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001116 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1117 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001118 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001119 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001120 * - from "slaves" directory of the holder @disk to the claimed @bdev
1121 * - from "holders" directory of the @bdev to the holder @disk
1122 *
1123 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1124 * passed to bd_link_disk_holder(), then:
1125 *
1126 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1127 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1128 *
1129 * The caller must have claimed @bdev before calling this function and
1130 * ensure that both @bdev and @disk are valid during the creation and
1131 * lifetime of these symlinks.
1132 *
1133 * CONTEXT:
1134 * Might sleep.
1135 *
1136 * RETURNS:
1137 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001138 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001139int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001140{
Tejun Heo49731ba2011-01-14 18:43:57 +01001141 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001142 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001143
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001144 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001145
Tejun Heo49731ba2011-01-14 18:43:57 +01001146 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001147
Tejun Heoe09b4572010-11-13 11:55:17 +01001148 /* FIXME: remove the following once add_disk() handles errors */
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001149 if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir))
Tejun Heoe09b4572010-11-13 11:55:17 +01001150 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001151
Tejun Heo49731ba2011-01-14 18:43:57 +01001152 holder = bd_find_holder_disk(bdev, disk);
1153 if (holder) {
1154 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001155 goto out_unlock;
1156 }
1157
Tejun Heo49731ba2011-01-14 18:43:57 +01001158 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1159 if (!holder) {
1160 ret = -ENOMEM;
1161 goto out_unlock;
1162 }
1163
1164 INIT_LIST_HEAD(&holder->list);
1165 holder->disk = disk;
1166 holder->refcnt = 1;
1167
Christoph Hellwig8d652692020-11-17 08:18:55 +01001168 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
Tejun Heo49731ba2011-01-14 18:43:57 +01001169 if (ret)
1170 goto out_free;
1171
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001172 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
Tejun Heo49731ba2011-01-14 18:43:57 +01001173 if (ret)
1174 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001175 /*
1176 * bdev could be deleted beneath us which would implicitly destroy
1177 * the holder directory. Hold on to it.
1178 */
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001179 kobject_get(bdev->bd_holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001180
1181 list_add(&holder->list, &bdev->bd_holder_disks);
1182 goto out_unlock;
1183
1184out_del:
Christoph Hellwig8d652692020-11-17 08:18:55 +01001185 del_symlink(disk->slave_dir, bdev_kobj(bdev));
Tejun Heo49731ba2011-01-14 18:43:57 +01001186out_free:
1187 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001188out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001189 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001190 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001191}
Tejun Heoe09b4572010-11-13 11:55:17 +01001192EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001193
Tejun Heo49731ba2011-01-14 18:43:57 +01001194/**
1195 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1196 * @bdev: the calimed slave bdev
1197 * @disk: the holding disk
1198 *
1199 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1200 *
1201 * CONTEXT:
1202 * Might sleep.
1203 */
1204void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001205{
Tejun Heo49731ba2011-01-14 18:43:57 +01001206 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001207
Tejun Heo49731ba2011-01-14 18:43:57 +01001208 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001209
Tejun Heo49731ba2011-01-14 18:43:57 +01001210 holder = bd_find_holder_disk(bdev, disk);
1211
1212 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
Christoph Hellwig8d652692020-11-17 08:18:55 +01001213 del_symlink(disk->slave_dir, bdev_kobj(bdev));
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001214 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
1215 kobject_put(bdev->bd_holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001216 list_del_init(&holder->list);
1217 kfree(holder);
1218 }
1219
1220 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001221}
Tejun Heo49731ba2011-01-14 18:43:57 +01001222EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001223#endif
1224
Al Viro4385bab2013-05-05 22:11:03 -04001225static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001226
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001227int bdev_disk_changed(struct block_device *bdev, bool invalidate)
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001228{
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001229 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001230 int ret;
1231
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001232 lockdep_assert_held(&bdev->bd_mutex);
1233
Christoph Hellwig38430f02020-09-21 09:19:45 +02001234 clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig6540fbf2020-09-01 17:57:41 +02001235
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001236rescan:
Christoph Hellwigd46430b2020-04-14 09:28:57 +02001237 ret = blk_drop_partitions(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001238 if (ret)
1239 return ret;
1240
Christoph Hellwigd981cb52020-03-18 09:12:06 +01001241 /*
1242 * Historically we only set the capacity to zero for devices that
1243 * support partitions (independ of actually having partitions created).
1244 * Doing that is rather inconsistent, but changing it broke legacy
1245 * udisks polling for legacy ide-cdrom devices. Use the crude check
1246 * below to get the sane behavior for most device while not breaking
1247 * userspace for this particular setup.
1248 */
1249 if (invalidate) {
1250 if (disk_part_scan_enabled(disk) ||
1251 !(disk->flags & GENHD_FL_REMOVABLE))
1252 set_capacity(disk, 0);
1253 } else {
1254 if (disk->fops->revalidate_disk)
1255 disk->fops->revalidate_disk(disk);
1256 }
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001257
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001258 if (get_capacity(disk)) {
1259 ret = blk_add_partitions(disk, bdev);
1260 if (ret == -EAGAIN)
1261 goto rescan;
Eric Biggers490547c2019-12-02 10:21:34 -08001262 } else if (invalidate) {
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001263 /*
1264 * Tell userspace that the media / partition table may have
1265 * changed.
1266 */
1267 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001268 }
1269
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001270 return ret;
1271}
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001272/*
1273 * Only exported for for loop and dasd for historic reasons. Don't use in new
1274 * code!
1275 */
1276EXPORT_SYMBOL_GPL(bdev_disk_changed);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001277
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001278/*
1279 * bd_mutex locking:
1280 *
1281 * mutex_lock(part->bd_mutex)
1282 * mutex_lock_nested(whole->bd_mutex, 1)
1283 */
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001284static int __blkdev_get(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001286 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001287 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 if (!bdev->bd_openers) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001290 if (!bdev_is_partition(bdev)) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001291 ret = 0;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001292 if (disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001293 ret = disk->fops->open(bdev, mode);
Tejun Heo7e697232011-05-23 13:26:07 +02001294
Christoph Hellwiga7824832020-11-26 18:43:37 +01001295 if (!ret)
Jan Kara04906b22019-01-14 09:48:10 +01001296 set_init_blocksize(bdev);
Tejun Heo7e697232011-05-23 13:26:07 +02001297
Tejun Heo1196f8b2011-04-21 20:54:45 +02001298 /*
1299 * If the device is invalidated, rescan partition
1300 * if open succeeded or failed with -ENOMEDIUM.
1301 * The latter is necessary to prevent ghost
1302 * partitions on a removed medium.
1303 */
Christoph Hellwig38430f02020-09-21 09:19:45 +02001304 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
Jan Kara731dc482019-10-21 10:37:59 +02001305 (!ret || ret == -ENOMEDIUM))
1306 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Dan Williams5a023cd2015-11-30 10:20:29 -08001307
Tejun Heo1196f8b2011-04-21 20:54:45 +02001308 if (ret)
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001309 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 } else {
Christoph Hellwig977115c2020-11-26 10:41:07 +01001311 struct block_device *whole = bdgrab(disk->part0);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001312
1313 mutex_lock_nested(&whole->bd_mutex, 1);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001314 ret = __blkdev_get(whole, mode);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001315 if (ret) {
1316 mutex_unlock(&whole->bd_mutex);
1317 bdput(whole);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001318 return ret;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001319 }
1320 whole->bd_part_count++;
1321 mutex_unlock(&whole->bd_mutex);
1322
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001323 if (!(disk->flags & GENHD_FL_UP) ||
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001324 !bdev_nr_sectors(bdev)) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001325 __blkdev_put(whole, mode, 1);
Christoph Hellwig63d99322020-11-26 09:22:18 +01001326 bdput(whole);
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001327 return -ENXIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
Jan Kara04906b22019-01-14 09:48:10 +01001329 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 }
Jan Kara03e26272017-03-23 01:36:53 +01001331
1332 if (bdev->bd_bdi == &noop_backing_dev_info)
1333 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 } else {
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001335 if (!bdev_is_partition(bdev)) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001336 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001337 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001338 /* the same as first opener case, read comment there */
Christoph Hellwig38430f02020-09-21 09:19:45 +02001339 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
Jan Kara731dc482019-10-21 10:37:59 +02001340 (!ret || ret == -ENOMEDIUM))
1341 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001342 if (ret)
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001343 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
1345 }
1346 bdev->bd_openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001350struct block_device *blkdev_get_no_open(dev_t dev)
1351{
1352 struct block_device *bdev;
1353 struct gendisk *disk;
1354
1355 down_read(&bdev_lookup_sem);
1356 bdev = bdget(dev);
1357 if (!bdev) {
1358 up_read(&bdev_lookup_sem);
1359 blk_request_module(dev);
1360 down_read(&bdev_lookup_sem);
1361
1362 bdev = bdget(dev);
1363 if (!bdev)
1364 goto unlock;
1365 }
1366
1367 disk = bdev->bd_disk;
1368 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj))
1369 goto bdput;
1370 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
1371 goto put_disk;
1372 if (!try_module_get(bdev->bd_disk->fops->owner))
1373 goto put_disk;
1374 up_read(&bdev_lookup_sem);
1375 return bdev;
1376put_disk:
1377 put_disk(disk);
1378bdput:
1379 bdput(bdev);
1380unlock:
1381 up_read(&bdev_lookup_sem);
1382 return NULL;
1383}
1384
1385void blkdev_put_no_open(struct block_device *bdev)
1386{
1387 module_put(bdev->bd_disk->fops->owner);
1388 put_disk(bdev->bd_disk);
1389 bdput(bdev);
1390}
1391
Tejun Heod4d77622010-11-13 11:55:18 +01001392/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001393 * blkdev_get_by_dev - open a block device by device number
1394 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +01001395 * @mode: FMODE_* mask
1396 * @holder: exclusive holder identifier
1397 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001398 * Open the block device described by device number @dev. If @mode includes
1399 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1400 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1401 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +01001402 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001403 * Use this interface ONLY if you really do not have anything better - i.e. when
1404 * you are behind a truly sucky interface and all you are given is a device
1405 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +01001406 *
1407 * CONTEXT:
1408 * Might sleep.
1409 *
1410 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001411 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +01001412 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001413struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001415 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001416 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001417 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001418 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +01001419
Christoph Hellwig7918f0f2020-11-23 13:44:44 +01001420 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001421 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +01001422 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
1423 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001424 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001425 return ERR_PTR(ret);
1426
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001427 /*
1428 * If we lost a race with 'disk' being deleted, try again. See md.c.
1429 */
1430retry:
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001431 bdev = blkdev_get_no_open(dev);
1432 if (!bdev)
1433 return ERR_PTR(-ENXIO);
1434 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001435
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001436 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001437 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001438 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001439 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001440 }
1441
1442 disk_block_events(disk);
1443
1444 mutex_lock(&bdev->bd_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001445 ret =__blkdev_get(bdev, mode);
1446 if (ret)
1447 goto abort_claiming;
1448 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001449 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001450
1451 /*
1452 * Block event polling for write claims if requested. Any write
1453 * holder makes the write_holder state stick until all are
1454 * released. This is good enough and tracking individual
1455 * writeable reference is too fragile given the way @mode is
1456 * used in blkdev_get/put().
1457 */
1458 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1459 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1460 bdev->bd_write_holder = true;
1461 unblock_events = false;
1462 }
1463 }
1464 mutex_unlock(&bdev->bd_mutex);
1465
1466 if (unblock_events)
1467 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001468 return bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001469
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001470abort_claiming:
1471 if (mode & FMODE_EXCL)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001472 bd_abort_claiming(bdev, holder);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001473 mutex_unlock(&bdev->bd_mutex);
1474 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001475put_blkdev:
1476 blkdev_put_no_open(bdev);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001477 if (ret == -ERESTARTSYS)
1478 goto retry;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001479 return ERR_PTR(ret);
NeilBrown37be4122006-12-08 02:36:16 -08001480}
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001481EXPORT_SYMBOL(blkdev_get_by_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482
Tejun Heod4d77622010-11-13 11:55:18 +01001483/**
1484 * blkdev_get_by_path - open a block device by name
1485 * @path: path to the block device to open
1486 * @mode: FMODE_* mask
1487 * @holder: exclusive holder identifier
1488 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001489 * Open the block device described by the device file at @path. If @mode
1490 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1491 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1492 * nest for the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +01001493 *
1494 * CONTEXT:
1495 * Might sleep.
1496 *
1497 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001498 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +01001499 */
1500struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1501 void *holder)
1502{
1503 struct block_device *bdev;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001504 dev_t dev;
1505 int error;
Tejun Heod4d77622010-11-13 11:55:18 +01001506
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001507 error = lookup_bdev(path, &dev);
1508 if (error)
1509 return ERR_PTR(error);
Tejun Heod4d77622010-11-13 11:55:18 +01001510
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001511 bdev = blkdev_get_by_dev(dev, mode, holder);
1512 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
Chuck Ebberte51900f2011-02-16 18:11:53 -05001513 blkdev_put(bdev, mode);
1514 return ERR_PTR(-EACCES);
1515 }
1516
Tejun Heod4d77622010-11-13 11:55:18 +01001517 return bdev;
1518}
1519EXPORT_SYMBOL(blkdev_get_by_path);
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521static int blkdev_open(struct inode * inode, struct file * filp)
1522{
1523 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
1525 /*
1526 * Preserve backwards compatibility and allow large file access
1527 * even if userspace doesn't ask for it explicitly. Some mkfs
1528 * binary needs it. We might want to drop this workaround
1529 * during an unstable branch.
1530 */
1531 filp->f_flags |= O_LARGEFILE;
1532
Jens Axboea304f072020-05-22 09:14:08 -06001533 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001534
Al Viro572c4892007-10-08 13:24:05 -04001535 if (filp->f_flags & O_NDELAY)
1536 filp->f_mode |= FMODE_NDELAY;
1537 if (filp->f_flags & O_EXCL)
1538 filp->f_mode |= FMODE_EXCL;
1539 if ((filp->f_flags & O_ACCMODE) == 3)
1540 filp->f_mode |= FMODE_WRITE_IOCTL;
1541
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001542 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
1543 if (IS_ERR(bdev))
1544 return PTR_ERR(bdev);
Al Viro572c4892007-10-08 13:24:05 -04001545 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001546 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001547 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548}
1549
Al Viro4385bab2013-05-05 22:11:03 -04001550static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001551{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001552 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001553 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001554
Douglas Andersonb849dd82020-03-24 14:48:27 -07001555 /*
1556 * Sync early if it looks like we're the last one. If someone else
1557 * opens the block device between now and the decrement of bd_openers
1558 * then we did a sync that we didn't need to, but that's not the end
1559 * of the world and we want to avoid long (could be several minute)
1560 * syncs while holding the mutex.
1561 */
1562 if (bdev->bd_openers == 1)
1563 sync_blockdev(bdev);
1564
NeilBrown6796bf52006-12-08 02:36:16 -08001565 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001566 if (for_part)
1567 bdev->bd_part_count--;
1568
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001569 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001570 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001571 sync_blockdev(bdev);
1572 kill_bdev(bdev);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001573 bdev_write_inode(bdev);
Christoph Hellwigec5d4512020-11-17 12:01:26 +01001574 if (bdev_is_partition(bdev))
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001575 victim = bdev_whole(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001576 }
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001577
1578 if (!bdev_is_partition(bdev) && disk->fops->release)
1579 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001580 mutex_unlock(&bdev->bd_mutex);
Christoph Hellwig63d99322020-11-26 09:22:18 +01001581 if (victim) {
Al Viro9a1c3542008-02-22 20:40:24 -05001582 __blkdev_put(victim, mode, 1);
Christoph Hellwig63d99322020-11-26 09:22:18 +01001583 bdput(victim);
1584 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001585}
1586
Al Viro4385bab2013-05-05 22:11:03 -04001587void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001588{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001589 struct gendisk *disk = bdev->bd_disk;
1590
Tejun Heo85ef06d2011-07-01 16:17:47 +02001591 mutex_lock(&bdev->bd_mutex);
1592
Tejun Heoe525fd82010-11-13 11:55:17 +01001593 if (mode & FMODE_EXCL) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001594 struct block_device *whole = bdev_whole(bdev);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001595 bool bdev_free;
1596
1597 /*
1598 * Release a claim on the device. The holder fields
1599 * are protected with bdev_lock. bd_mutex is to
1600 * synchronize disk_holder unlinking.
1601 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001602 spin_lock(&bdev_lock);
1603
1604 WARN_ON_ONCE(--bdev->bd_holders < 0);
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001605 WARN_ON_ONCE(--whole->bd_holders < 0);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001606
Tejun Heo6a027ef2010-11-13 11:55:17 +01001607 if ((bdev_free = !bdev->bd_holders))
1608 bdev->bd_holder = NULL;
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001609 if (!whole->bd_holders)
1610 whole->bd_holder = NULL;
Tejun Heo6a027ef2010-11-13 11:55:17 +01001611
1612 spin_unlock(&bdev_lock);
1613
Tejun Heo77ea8872010-12-08 20:57:37 +01001614 /*
1615 * If this was the last claim, remove holder link and
1616 * unblock evpoll if it was a write holder.
1617 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001618 if (bdev_free && bdev->bd_write_holder) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001619 disk_unblock_events(disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001620 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001621 }
Tejun Heo69362172011-03-09 19:54:27 +01001622 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001623
Tejun Heo85ef06d2011-07-01 16:17:47 +02001624 /*
1625 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1626 * event. This is to ensure detection of media removal commanded
1627 * from userland - e.g. eject(1).
1628 */
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001629 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001630 mutex_unlock(&bdev->bd_mutex);
1631
Al Viro4385bab2013-05-05 22:11:03 -04001632 __blkdev_put(bdev, mode, 0);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001633 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001634}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001635EXPORT_SYMBOL(blkdev_put);
1636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637static int blkdev_close(struct inode * inode, struct file * filp)
1638{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001639 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001640 blkdev_put(bdev, filp->f_mode);
1641 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642}
1643
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001644static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001646 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001647 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001648
1649 /*
1650 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1651 * to updated it before every ioctl.
1652 */
Al Viro56b26ad2008-09-19 03:17:36 -04001653 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001654 mode |= FMODE_NDELAY;
1655 else
1656 mode &= ~FMODE_NDELAY;
1657
Al Viro56b26ad2008-09-19 03:17:36 -04001658 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659}
1660
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001661/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001662 * Write data to the block device. Only intended for the block device itself
1663 * and the raw driver which basically is a fake block device.
1664 *
1665 * Does not take i_mutex for the write and thus is not for general purpose
1666 * use.
1667 */
Al Viro1456c0a2014-04-03 03:21:50 -04001668ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02001669{
1670 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001671 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04001672 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001673 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02001674 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04001675
Al Viro7ec7b942015-04-07 11:35:14 -04001676 if (bdev_read_only(I_BDEV(bd_inode)))
1677 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04001678
Christoph Hellwigbb3247a392020-09-21 09:19:55 +02001679 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
Darrick J. Wongdc617f22019-08-20 07:55:16 -07001680 return -ETXTBSY;
1681
Al Viro7ec7b942015-04-07 11:35:14 -04001682 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04001683 return 0;
1684
Al Viro7ec7b942015-04-07 11:35:14 -04001685 if (iocb->ki_pos >= size)
1686 return -ENOSPC;
1687
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001688 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1689 return -EOPNOTSUPP;
1690
Al Viro7ec7b942015-04-07 11:35:14 -04001691 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001692
Jianpeng Ma53362a02012-08-02 09:50:39 +02001693 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04001694 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07001695 if (ret > 0)
1696 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001697 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001698 return ret;
1699}
Al Viro1456c0a2014-04-03 03:21:50 -04001700EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001701
David Jefferyb2de5252014-09-29 10:21:10 -04001702ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001703{
1704 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001705 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001706 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04001707 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001708
1709 if (pos >= size)
1710 return 0;
1711
1712 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04001713 iov_iter_truncate(to, size);
1714 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001715}
David Jefferyb2de5252014-09-29 10:21:10 -04001716EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001717
Christoph Hellwigeef99382009-08-20 17:43:41 +02001718/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001719 * Try to release a page associated with block device when the system
1720 * is under memory pressure.
1721 */
1722static int blkdev_releasepage(struct page *page, gfp_t wait)
1723{
1724 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1725
1726 if (super && super->s_op->bdev_try_to_free_page)
1727 return super->s_op->bdev_try_to_free_page(super, page, wait);
1728
1729 return try_to_free_buffers(page);
1730}
1731
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001732static int blkdev_writepages(struct address_space *mapping,
1733 struct writeback_control *wbc)
1734{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001735 return generic_writepages(mapping, wbc);
1736}
1737
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001738static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .readpage = blkdev_readpage,
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001740 .readahead = blkdev_readahead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001742 .write_begin = blkdev_write_begin,
1743 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001744 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001745 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08001747 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07001748 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749};
1750
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001751#define BLKDEV_FALLOC_FL_SUPPORTED \
1752 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1753 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1754
1755static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1756 loff_t len)
1757{
1758 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001759 loff_t end = start + len - 1;
1760 loff_t isize;
1761 int error;
1762
1763 /* Fail if we don't recognize the flags. */
1764 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1765 return -EOPNOTSUPP;
1766
1767 /* Don't go off the end of the device. */
1768 isize = i_size_read(bdev->bd_inode);
1769 if (start >= isize)
1770 return -EINVAL;
1771 if (end >= isize) {
1772 if (mode & FALLOC_FL_KEEP_SIZE) {
1773 len = isize - start;
1774 end = start + len - 1;
1775 } else
1776 return -EINVAL;
1777 }
1778
1779 /*
1780 * Don't allow IO that isn't aligned to logical block size.
1781 */
1782 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1783 return -EINVAL;
1784
1785 /* Invalidate the page cache, including dirty pages. */
Jan Kara384d87e2020-09-04 10:58:52 +02001786 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1787 if (error)
1788 return error;
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001789
1790 switch (mode) {
1791 case FALLOC_FL_ZERO_RANGE:
1792 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1793 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02001794 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001795 break;
1796 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02001797 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1798 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001799 break;
1800 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001801 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1802 GFP_KERNEL, 0);
1803 break;
1804 default:
1805 return -EOPNOTSUPP;
1806 }
1807 if (error)
1808 return error;
1809
1810 /*
1811 * Invalidate again; if someone wandered in and dirtied a page,
1812 * the caller will be given -EBUSY. The third argument is
1813 * inclusive, so the rounding here is safe.
1814 */
Jan Kara384d87e2020-09-04 10:58:52 +02001815 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001816 start >> PAGE_SHIFT,
1817 end >> PAGE_SHIFT);
1818}
1819
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001820const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 .open = blkdev_open,
1822 .release = blkdev_close,
1823 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04001824 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04001825 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07001826 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07001827 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001828 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001829 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830#ifdef CONFIG_COMPAT
1831 .compat_ioctl = compat_blkdev_ioctl,
1832#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08001833 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001834 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001835 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836};
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838/**
1839 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001840 * @pathname: special file representing the block device
Randy Dunlap875b2372020-12-28 19:47:06 -08001841 * @dev: return value of the block device's dev_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001843 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 * namespace if possible and return it. Return ERR_PTR(error)
1845 * otherwise.
1846 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001847int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001850 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int error;
1852
Al Viro421748e2008-08-02 01:04:36 -04001853 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001854 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Al Viro421748e2008-08-02 01:04:36 -04001856 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001858 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
David Howellsbb6687342015-03-17 22:26:21 +00001860 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 error = -ENOTBLK;
1862 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001863 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05001865 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001866 goto out_path_put;
1867
1868 *dev = inode->i_rdev;
1869 error = 0;
1870out_path_put:
Al Viro421748e2008-08-02 01:04:36 -04001871 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001872 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873}
Al Virod5686b42008-08-01 05:00:11 -04001874EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
NeilBrown93b270f2011-02-24 17:25:47 +11001876int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001877{
1878 struct super_block *sb = get_super(bdev);
1879 int res = 0;
1880
1881 if (sb) {
1882 /*
1883 * no need to lock the super, get_super holds the
1884 * read mutex so the filesystem cannot go away
1885 * under us (->put_super runs with the write lock
1886 * hold).
1887 */
1888 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001889 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001890 drop_super(sb);
1891 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001892 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001893 return res;
1894}
1895EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001896
1897void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1898{
1899 struct inode *inode, *old_inode = NULL;
1900
Dave Chinner74278da2015-03-04 12:37:22 -05001901 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001902 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1903 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001904 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001905
1906 spin_lock(&inode->i_lock);
1907 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1908 mapping->nrpages == 0) {
1909 spin_unlock(&inode->i_lock);
1910 continue;
1911 }
1912 __iget(inode);
1913 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05001914 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001915 /*
1916 * We hold a reference to 'inode' so it couldn't have been
1917 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05001918 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001919 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05001920 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001921 * later.
1922 */
1923 iput(old_inode);
1924 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001925 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001926
Rabin Vincentaf309222016-12-01 09:18:28 +01001927 mutex_lock(&bdev->bd_mutex);
1928 if (bdev->bd_openers)
1929 func(bdev, arg);
1930 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001931
Dave Chinner74278da2015-03-04 12:37:22 -05001932 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001933 }
Dave Chinner74278da2015-03-04 12:37:22 -05001934 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001935 iput(old_inode);
1936}