blob: ac9b3c158a770ad12b1d3120a8e1e1704835caef [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
Matthew Wilcox (Oracle)77165062021-05-04 18:32:45 -070082 if (mapping_empty(mapping))
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)
Jan Kara56887cf2021-02-22 10:48:09 +0100121 goto invalidate;
Jan Kara384d87e2020-09-04 10:58:52 +0200122 }
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;
Jan Kara56887cf2021-02-22 10:48:09 +0100128
129invalidate:
130 /*
131 * Someone else has handle exclusively open. Try invalidating instead.
132 * The 'end' argument is inclusive so the rounding is safe.
133 */
134 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
135 lstart >> PAGE_SHIFT,
136 lend >> PAGE_SHIFT);
Jan Kara384d87e2020-09-04 10:58:52 +0200137}
Jan Kara384d87e2020-09-04 10:58:52 +0200138
Jan Kara04906b22019-01-14 09:48:10 +0100139static void set_init_blocksize(struct block_device *bdev)
140{
Maxim Mikityanskiy8dc932d2021-01-26 21:59:07 +0200141 unsigned int bsize = bdev_logical_block_size(bdev);
142 loff_t size = i_size_read(bdev->bd_inode);
143
144 while (bsize < PAGE_SIZE) {
145 if (size & bsize)
146 break;
147 bsize <<= 1;
148 }
149 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
Jan Kara04906b22019-01-14 09:48:10 +0100150}
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152int set_blocksize(struct block_device *bdev, int size)
153{
154 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700155 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return -EINVAL;
157
158 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400159 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 return -EINVAL;
161
162 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200163 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 bdev->bd_inode->i_blkbits = blksize_bits(size);
166 kill_bdev(bdev);
167 }
168 return 0;
169}
170
171EXPORT_SYMBOL(set_blocksize);
172
173int sb_set_blocksize(struct super_block *sb, int size)
174{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (set_blocksize(sb->s_bdev, size))
176 return 0;
177 /* If we get here, we know size is power of two
178 * and it's value is between 512 and PAGE_SIZE */
179 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800180 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return sb->s_blocksize;
182}
183
184EXPORT_SYMBOL(sb_set_blocksize);
185
186int sb_min_blocksize(struct super_block *sb, int size)
187{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400188 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (size < minsize)
190 size = minsize;
191 return sb_set_blocksize(sb, size);
192}
193
194EXPORT_SYMBOL(sb_min_blocksize);
195
196static int
197blkdev_get_block(struct inode *inode, sector_t iblock,
198 struct buffer_head *bh, int create)
199{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 bh->b_bdev = I_BDEV(inode);
201 bh->b_blocknr = iblock;
202 set_buffer_mapped(bh);
203 return 0;
204}
205
Dan Williams4ebb16c2015-10-28 07:48:19 +0900206static struct inode *bdev_file_inode(struct file *file)
207{
208 return file->f_mapping->host;
209}
210
Jens Axboe78250c02016-11-17 17:50:47 +0100211static unsigned int dio_bio_write_op(struct kiocb *iocb)
212{
213 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
214
215 /* avoid the need for a I/O completion work item */
216 if (iocb->ki_flags & IOCB_DSYNC)
217 op |= REQ_FUA;
218 return op;
219}
220
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600221#define DIO_INLINE_BIO_VECS 4
222
223static void blkdev_bio_end_io_simple(struct bio *bio)
224{
225 struct task_struct *waiter = bio->bi_private;
226
227 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700228 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600229}
230
231static ssize_t
232__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000233 unsigned int nr_pages)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600234{
235 struct file *file = iocb->ki_filp;
236 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200237 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600238 loff_t pos = iocb->ki_pos;
239 bool should_dirty = false;
240 struct bio bio;
241 ssize_t ret;
242 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600243
Jens Axboe9a794fb2016-11-22 08:12:39 -0700244 if ((pos | iov_iter_alignment(iter)) &
245 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600246 return -EINVAL;
247
Jens Axboe72ecad22016-11-16 23:11:42 -0700248 if (nr_pages <= DIO_INLINE_BIO_VECS)
249 vecs = inline_vecs;
250 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700251 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
252 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700253 if (!vecs)
254 return -ENOMEM;
255 }
256
Ming Lei3a83f462016-11-22 08:57:21 -0700257 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200258 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900259 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600260 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600261 bio.bi_private = current;
262 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700263 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600264
265 ret = bio_iov_iter_get_pages(&bio, iter);
266 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200267 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600268 ret = bio.bi_iter.bi_size;
269
270 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100271 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600272 if (iter_is_iovec(iter))
273 should_dirty = true;
274 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100275 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600276 task_io_account_write(ret);
277 }
Pavel Begunkovf8b78ca2020-11-20 17:10:28 +0000278 if (iocb->ki_flags & IOCB_NOWAIT)
279 bio.bi_opf |= REQ_NOWAIT;
Jens Axboed1e36282018-08-29 10:36:56 -0600280 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700281 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600282
283 qc = submit_bio(&bio);
284 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800285 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600286 if (!READ_ONCE(bio.bi_private))
287 break;
288 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700289 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800290 blk_io_schedule();
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600291 }
292 __set_current_state(TASK_RUNNING);
293
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200294 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200295 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700296 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600297
Martin Wilck9362dd12018-07-25 23:15:08 +0200298out:
299 if (vecs != inline_vecs)
300 kfree(vecs);
301
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600302 bio_uninit(&bio);
303
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600304 return ret;
305}
306
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700307struct blkdev_dio {
308 union {
309 struct kiocb *iocb;
310 struct task_struct *waiter;
311 };
312 size_t size;
313 atomic_t ref;
314 bool multi_bio : 1;
315 bool should_dirty : 1;
316 bool is_sync : 1;
317 struct bio bio;
318};
319
Kent Overstreet52190f82018-05-20 18:25:55 -0400320static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700321
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700322static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
323{
324 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
325 struct request_queue *q = bdev_get_queue(bdev);
326
327 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
328}
329
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700330static void blkdev_bio_end_io(struct bio *bio)
331{
332 struct blkdev_dio *dio = bio->bi_private;
333 bool should_dirty = dio->should_dirty;
334
Jason Yana89afe52019-04-12 10:09:16 +0800335 if (bio->bi_status && !dio->bio.bi_status)
336 dio->bio.bi_status = bio->bi_status;
337
338 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700339 if (!dio->is_sync) {
340 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200341 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700342
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200343 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700344 ret = dio->size;
345 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200346 } else {
347 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700348 }
349
350 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100351 if (dio->multi_bio)
352 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700353 } else {
354 struct task_struct *waiter = dio->waiter;
355
356 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700357 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700358 }
359 }
360
361 if (should_dirty) {
362 bio_check_pages_dirty(bio);
363 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200364 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700365 bio_put(bio);
366 }
367}
368
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000369static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
370 unsigned int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800371{
372 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900373 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700374 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100375 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700376 struct blkdev_dio *dio;
377 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700378 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100379 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700380 loff_t pos = iocb->ki_pos;
381 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe7b6620d2019-08-15 11:09:16 -0600382 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700383
Jens Axboe9a794fb2016-11-22 08:12:39 -0700384 if ((pos | iov_iter_alignment(iter)) &
385 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700386 return -EINVAL;
387
Jens Axboe7b6620d2019-08-15 11:09:16 -0600388 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700389
390 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100391 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100392 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700393 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100394 bio_get(bio);
395 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700396 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100397 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700398
399 dio->size = 0;
400 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100401 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700402
Jens Axboecb700eb2018-11-15 19:56:53 -0700403 /*
404 * Don't plug for HIPRI/polled IO, as those should go straight
405 * to issue
406 */
407 if (!is_poll)
408 blk_start_plug(&plug);
409
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700410 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200411 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900412 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600413 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700414 bio->bi_private = dio;
415 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700416 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700417
Jens Axboee15c2ff2019-08-06 13:34:31 -0600418 ret = bio_iov_iter_get_pages(bio, iter);
419 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200420 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700421 bio_endio(bio);
422 break;
423 }
424
425 if (is_read) {
426 bio->bi_opf = REQ_OP_READ;
427 if (dio->should_dirty)
428 bio_set_pages_dirty(bio);
429 } else {
430 bio->bi_opf = dio_bio_write_op(iocb);
431 task_io_account_write(bio->bi_iter.bi_size);
432 }
Pavel Begunkovf8b78ca2020-11-20 17:10:28 +0000433 if (iocb->ki_flags & IOCB_NOWAIT)
434 bio->bi_opf |= REQ_NOWAIT;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700435
Jens Axboe7b6620d2019-08-15 11:09:16 -0600436 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700437 pos += bio->bi_iter.bi_size;
438
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100439 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700440 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700441 bool polled = false;
442
443 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700444 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700445 polled = true;
446 }
Jens Axboed34513d2018-11-06 14:29:11 -0700447
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700448 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700449
450 if (polled)
451 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700452 break;
453 }
454
455 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100456 /*
457 * AIO needs an extra reference to ensure the dio
458 * structure which is embedded into the first bio
459 * stays around.
460 */
461 if (!is_sync)
462 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700463 dio->multi_bio = true;
464 atomic_set(&dio->ref, 2);
465 } else {
466 atomic_inc(&dio->ref);
467 }
468
Jens Axboe7b6620d2019-08-15 11:09:16 -0600469 submit_bio(bio);
470 bio = bio_alloc(GFP_KERNEL, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700471 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700472
473 if (!is_poll)
474 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700475
Christoph Hellwig690e5322017-01-24 14:50:19 +0100476 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700477 return -EIOCBQUEUED;
478
479 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800480 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700481 if (!READ_ONCE(dio->waiter))
482 break;
483
484 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700485 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800486 blk_io_schedule();
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700487 }
488 __set_current_state(TASK_RUNNING);
489
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200490 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200491 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600492 if (likely(!ret))
493 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700494
495 bio_put(&dio->bio);
496 return ret;
497}
498
499static ssize_t
500blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
501{
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000502 unsigned int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800503
Pavel Begunkov3e1a88e2021-01-09 16:03:02 +0000504 if (!iov_iter_count(iter))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600505 return 0;
Pavel Begunkov3e1a88e2021-01-09 16:03:02 +0000506
Christoph Hellwiga8affc02021-03-11 12:01:37 +0100507 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
508 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_VECS)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600509 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700510
Matthew Wilcox (Oracle)5f7136d2021-01-29 04:38:57 +0000511 return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800512}
513
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700514static __init int blkdev_init(void)
515{
Kent Overstreet52190f82018-05-20 18:25:55 -0400516 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700517}
518module_init(blkdev_init);
519
Jan Kara5cee5812009-04-27 16:43:51 +0200520int __sync_blockdev(struct block_device *bdev, int wait)
521{
522 if (!bdev)
523 return 0;
524 if (!wait)
525 return filemap_flush(bdev->bd_inode->i_mapping);
526 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
527}
528
Nick Piggin585d3bc2009-02-25 10:44:19 +0100529/*
530 * Write out and wait upon all the dirty data associated with a block
531 * device via its mapping. Does not take the superblock lock.
532 */
533int sync_blockdev(struct block_device *bdev)
534{
Jan Kara5cee5812009-04-27 16:43:51 +0200535 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100536}
537EXPORT_SYMBOL(sync_blockdev);
538
539/*
540 * Write out and wait upon all dirty data associated with this
541 * device. Filesystem data as well as the underlying block
542 * device. Takes the superblock lock.
543 */
544int fsync_bdev(struct block_device *bdev)
545{
546 struct super_block *sb = get_super(bdev);
547 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200548 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100549 drop_super(sb);
550 return res;
551 }
552 return sync_blockdev(bdev);
553}
Al Viro47e44912009-04-01 07:07:16 -0400554EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100555
556/**
557 * freeze_bdev -- lock a filesystem and force it into a consistent state
558 * @bdev: blockdevice to lock
559 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100560 * If a superblock is found on this device, we take the s_umount semaphore
561 * on it to make sure nobody unmounts until the snapshot creation is done.
562 * The reference counter (bd_fsfreeze_count) guarantees that only the last
563 * unfreeze process can unfreeze the frozen filesystem actually when multiple
564 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
565 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
566 * actually.
567 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100568int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100569{
570 struct super_block *sb;
571 int error = 0;
572
573 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100574 if (++bdev->bd_fsfreeze_count > 1)
575 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100576
Christoph Hellwig45042302009-08-03 23:28:35 +0200577 sb = get_active_super(bdev);
578 if (!sb)
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100579 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600580 if (sb->s_op->freeze_super)
581 error = sb->s_op->freeze_super(sb);
582 else
583 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400584 deactivate_super(sb);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100585
586 if (error) {
587 bdev->bd_fsfreeze_count--;
588 goto done;
589 }
590 bdev->bd_fsfreeze_sb = sb;
591
592sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100593 sync_blockdev(bdev);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100594done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100595 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100596 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100597}
598EXPORT_SYMBOL(freeze_bdev);
599
600/**
601 * thaw_bdev -- unlock filesystem
602 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100603 *
604 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
605 */
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100606int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100607{
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100608 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200609 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100610
611 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200612 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400613 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100614
Christoph Hellwig45042302009-08-03 23:28:35 +0200615 error = 0;
616 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400617 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100618
Christoph Hellwig040f04b2020-11-24 11:54:06 +0100619 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200620 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400621 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200622
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600623 if (sb->s_op->thaw_super)
624 error = sb->s_op->thaw_super(sb);
625 else
626 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200627 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400628 bdev->bd_fsfreeze_count++;
Satya Tangirala04a6a532020-12-24 04:49:54 +0000629 else
630 bdev->bd_fsfreeze_sb = NULL;
Josef Bacik18e9e512010-03-23 10:34:56 -0400631out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100632 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200633 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100634}
635EXPORT_SYMBOL(thaw_bdev);
636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
638{
639 return block_write_full_page(page, blkdev_get_block, wbc);
640}
641
642static int blkdev_readpage(struct file * file, struct page * page)
643{
644 return block_read_full_page(page, blkdev_get_block);
645}
646
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700647static void blkdev_readahead(struct readahead_control *rac)
Akinobu Mita447f05b2014-10-09 15:26:58 -0700648{
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700649 mpage_readahead(rac, blkdev_get_block);
Akinobu Mita447f05b2014-10-09 15:26:58 -0700650}
651
Nick Piggin6272b5a2007-10-16 01:25:04 -0700652static int blkdev_write_begin(struct file *file, struct address_space *mapping,
653 loff_t pos, unsigned len, unsigned flags,
654 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200656 return block_write_begin(mapping, pos, len, flags, pagep,
657 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658}
659
Nick Piggin6272b5a2007-10-16 01:25:04 -0700660static int blkdev_write_end(struct file *file, struct address_space *mapping,
661 loff_t pos, unsigned len, unsigned copied,
662 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700664 int ret;
665 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
666
667 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300668 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700669
670 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
673/*
674 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500675 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 * so we compute the size by hand (just as in block_read/write above)
677 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800678static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900680 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 loff_t retval;
682
Al Viro59551022016-01-22 15:40:57 -0500683 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400684 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500685 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 return retval;
687}
688
Josef Bacik02c24a82011-07-16 20:44:56 -0400689int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900691 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400692 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100693 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200694
Jeff Layton372cf242017-07-06 07:02:28 -0400695 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200696 if (error)
697 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100698
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400699 /*
700 * There is no need to serialise calls to blkdev_issue_flush with
701 * i_mutex and doing so causes performance issues with concurrent
702 * O_SYNC writers to a block device.
703 */
Christoph Hellwigc6bf3f02021-01-26 15:52:35 +0100704 error = blkdev_issue_flush(bdev);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100705 if (error == -EOPNOTSUPP)
706 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400707
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100708 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700710EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700712/**
713 * bdev_read_page() - Start reading a page from a block device
714 * @bdev: The device to read the page from
715 * @sector: The offset on the device to read the page to (need not be aligned)
716 * @page: The page to read
717 *
718 * On entry, the page should be locked. It will be unlocked when the page
719 * has been read. If the block driver implements rw_page synchronously,
720 * that will be true on exit from this function, but it need not be.
721 *
722 * Errors returned by this function are usually "soft", eg out of memory, or
723 * queue full; callers should try a different route to read this page rather
724 * than propagate an error back up the stack.
725 *
726 * Return: negative errno if an error occurs, 0 if submission was successful.
727 */
728int bdev_read_page(struct block_device *bdev, sector_t sector,
729 struct page *page)
730{
731 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800732 int result = -EOPNOTSUPP;
733
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400734 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800735 return result;
736
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200737 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800738 if (result)
739 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700740 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
741 REQ_OP_READ);
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200742 blk_queue_exit(bdev->bd_disk->queue);
Dan Williams2e6edc952015-11-19 13:29:28 -0800743 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700744}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700745
746/**
747 * bdev_write_page() - Start writing a page to a block device
748 * @bdev: The device to write the page to
749 * @sector: The offset on the device to write the page to (need not be aligned)
750 * @page: The page to write
751 * @wbc: The writeback_control for the write
752 *
753 * On entry, the page should be locked and not currently under writeback.
754 * On exit, if the write started successfully, the page will be unlocked and
755 * under writeback. If the write failed already (eg the driver failed to
756 * queue the page to the device), the page will still be locked. If the
757 * caller is a ->writepage implementation, it will need to unlock the page.
758 *
759 * Errors returned by this function are usually "soft", eg out of memory, or
760 * queue full; callers should try a different route to write this page rather
761 * than propagate an error back up the stack.
762 *
763 * Return: negative errno if an error occurs, 0 if submission was successful.
764 */
765int bdev_write_page(struct block_device *bdev, sector_t sector,
766 struct page *page, struct writeback_control *wbc)
767{
768 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700769 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800770
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400771 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700772 return -EOPNOTSUPP;
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200773 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800774 if (result)
775 return result;
776
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700777 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700778 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
779 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700780 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700781 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700782 } else {
783 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700784 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700785 }
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200786 blk_queue_exit(bdev->bd_disk->queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700787 return result;
788}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790/*
791 * pseudo-fs
792 */
793
794static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800795static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797static struct inode *bdev_alloc_inode(struct super_block *sb)
798{
Christoph Lametere94b1762006-12-06 20:33:17 -0800799 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 if (!ei)
802 return NULL;
Christoph Hellwig2d2f6f12021-01-07 19:36:40 +0100803 memset(&ei->bdev, 0, sizeof(ei->bdev));
804 ei->bdev.bd_bdi = &noop_backing_dev_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 return &ei->vfs_inode;
806}
807
Al Viro41149cb2019-04-10 15:12:38 -0400808static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100810 struct block_device *bdev = I_BDEV(inode);
811
812 free_percpu(bdev->bd_stats);
Christoph Hellwig231926d2020-11-24 12:01:45 +0100813 kfree(bdev->bd_meta_info);
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100814
Al Viro41149cb2019-04-10 15:12:38 -0400815 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100816}
817
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100818static void init_once(void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819{
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100820 struct bdev_inode *ei = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Christoph Lametera35afb82007-05-16 22:10:57 -0700822 inode_init_once(&ei->vfs_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823}
824
Al Virob57922d2010-06-07 14:34:48 -0400825static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700828 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400829 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200830 clear_inode(inode);
Jan Karaf7597412017-03-23 01:37:00 +0100831 /* Detach inode from wb early as bdi_put() may free bdi->wb */
832 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100833 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100834 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100835 bdev->bd_bdi = &noop_backing_dev_info;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800839static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 .statfs = simple_statfs,
841 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400842 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400844 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845};
846
David Howells9030d162019-03-25 16:38:23 +0000847static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
David Howells9030d162019-03-25 16:38:23 +0000849 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
850 if (!ctx)
851 return -ENOMEM;
852 fc->s_iflags |= SB_I_CGROUPWB;
853 ctx->ops = &bdev_sops;
854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855}
856
857static struct file_system_type bd_type = {
858 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000859 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .kill_sb = kill_anon_super,
861};
862
Tejun Heoa212b102015-05-22 17:13:33 -0400863struct super_block *blockdev_superblock __read_mostly;
864EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865
866void __init bdev_cache_init(void)
867{
868 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300869 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800872 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800873 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900874 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 err = register_filesystem(&bd_type);
876 if (err)
877 panic("Cannot register bdev pseudo-fs");
878 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (IS_ERR(bd_mnt))
880 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300881 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882}
883
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100884struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 struct block_device *bdev;
887 struct inode *inode;
888
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100889 inode = new_inode(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!inode)
891 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100892 inode->i_mode = S_IFBLK;
893 inode->i_rdev = 0;
894 inode->i_data.a_ops = &def_blk_aops;
895 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100897 bdev = I_BDEV(inode);
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100898 mutex_init(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100899 spin_lock_init(&bdev->bd_size_lock);
900 bdev->bd_disk = disk;
901 bdev->bd_partno = partno;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100902 bdev->bd_inode = inode;
Christoph Hellwige6cb5382020-11-23 15:41:40 +0100903#ifdef CONFIG_SYSFS
904 INIT_LIST_HEAD(&bdev->bd_holder_disks);
905#endif
Christoph Hellwig15e3d2c2020-11-24 09:34:00 +0100906 bdev->bd_stats = alloc_percpu(struct disk_stats);
907 if (!bdev->bd_stats) {
908 iput(inode);
909 return NULL;
910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 return bdev;
912}
913
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +0100914void bdev_add(struct block_device *bdev, dev_t dev)
915{
916 bdev->bd_dev = dev;
917 bdev->bd_inode->i_rdev = dev;
918 bdev->bd_inode->i_ino = dev;
919 insert_inode_hash(bdev->bd_inode);
920}
921
922static struct block_device *bdget(dev_t dev)
923{
924 struct inode *inode;
925
926 inode = ilookup(blockdev_superblock, dev);
927 if (!inode)
928 return NULL;
929 return &BDEV_I(inode)->bdev;
930}
931
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200932/**
933 * bdgrab -- Grab a reference to an already referenced block device
934 * @bdev: Block device to grab a reference to.
Christoph Hellwig3a4174e2020-11-27 16:45:27 +0100935 *
936 * Returns the block_device with an additional reference when successful,
937 * or NULL if the inode is already beeing freed.
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200938 */
939struct block_device *bdgrab(struct block_device *bdev)
940{
Christoph Hellwig3a4174e2020-11-27 16:45:27 +0100941 if (!igrab(bdev->bd_inode))
942 return NULL;
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200943 return bdev;
944}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700945EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947long nr_blockdev_pages(void)
948{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200949 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200951
952 spin_lock(&blockdev_superblock->s_inode_list_lock);
953 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
954 ret += inode->i_mapping->nrpages;
955 spin_unlock(&blockdev_superblock->s_inode_list_lock);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return ret;
958}
959
960void bdput(struct block_device *bdev)
961{
962 iput(bdev->bd_inode);
963}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964EXPORT_SYMBOL(bdput);
965
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900966/**
967 * bd_may_claim - test whether a block device can be claimed
968 * @bdev: block device of interest
969 * @whole: whole block device containing @bdev, may equal @bdev
970 * @holder: holder trying to claim @bdev
971 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300972 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900973 *
974 * CONTEXT:
975 * spin_lock(&bdev_lock).
976 *
977 * RETURNS:
978 * %true if @bdev can be claimed, %false otherwise.
979 */
980static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
981 void *holder)
982{
983 if (bdev->bd_holder == holder)
984 return true; /* already a holder */
985 else if (bdev->bd_holder != NULL)
986 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -0700987 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900988 return true; /* is a whole device which isn't held */
989
Tejun Heoe525fd82010-11-13 11:55:17 +0100990 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +0900991 return true; /* is a partition of a device that is being partitioned */
992 else if (whole->bd_holder != NULL)
993 return false; /* is a partition of a held device */
994 else
995 return true; /* is a partition of an un-held device */
996}
997
998/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +0200999 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +09001000 * @bdev: block device of interest
Tejun Heo6b4517a2010-04-07 18:53:59 +09001001 * @holder: holder trying to claim @bdev
1002 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001003 * Claim @bdev. This function fails if @bdev is already claimed by another
1004 * holder and waits if another claiming is in progress. return, the caller
1005 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +09001006 *
1007 * RETURNS:
1008 * 0 if @bdev can be claimed, -EBUSY otherwise.
1009 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001010int bd_prepare_to_claim(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001011{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001012 struct block_device *whole = bdev_whole(bdev);
1013
1014 if (WARN_ON_ONCE(!holder))
1015 return -EINVAL;
Tejun Heo6b4517a2010-04-07 18:53:59 +09001016retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001017 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001018 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001019 if (!bd_may_claim(bdev, whole, holder)) {
1020 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001021 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001022 }
Tejun Heo6b4517a2010-04-07 18:53:59 +09001023
Tejun Heoe75aa852010-08-04 17:59:39 +02001024 /* if claiming is already in progress, wait for it to finish */
1025 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001026 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1027 DEFINE_WAIT(wait);
1028
1029 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1030 spin_unlock(&bdev_lock);
1031 schedule();
1032 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001033 goto retry;
1034 }
1035
1036 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001037 whole->bd_claiming = holder;
1038 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001039 return 0;
1040}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +02001041EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +09001042
Jan Kara89e524c02019-07-30 13:10:14 +02001043static void bd_clear_claiming(struct block_device *whole, void *holder)
1044{
1045 lockdep_assert_held(&bdev_lock);
1046 /* tell others that we're done */
1047 BUG_ON(whole->bd_claiming != holder);
1048 whole->bd_claiming = NULL;
1049 wake_up_bit(&whole->bd_claiming, 0);
1050}
1051
1052/**
1053 * bd_finish_claiming - finish claiming of a block device
1054 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +02001055 * @holder: holder that has claimed @bdev
1056 *
1057 * Finish exclusive open of a block device. Mark the device as exlusively
1058 * open by the holder and wake up all waiters for exclusive open to finish.
1059 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001060static void bd_finish_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +02001061{
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001062 struct block_device *whole = bdev_whole(bdev);
1063
Jan Kara89e524c02019-07-30 13:10:14 +02001064 spin_lock(&bdev_lock);
1065 BUG_ON(!bd_may_claim(bdev, whole, holder));
1066 /*
1067 * Note that for a whole device bd_holders will be incremented twice,
1068 * and bd_holder will be set to bd_may_claim before being set to holder
1069 */
1070 whole->bd_holders++;
1071 whole->bd_holder = bd_may_claim;
1072 bdev->bd_holders++;
1073 bdev->bd_holder = holder;
1074 bd_clear_claiming(whole, holder);
1075 spin_unlock(&bdev_lock);
1076}
Jan Kara89e524c02019-07-30 13:10:14 +02001077
1078/**
1079 * bd_abort_claiming - abort claiming of a block device
1080 * @bdev: block device of interest
Jan Kara89e524c02019-07-30 13:10:14 +02001081 * @holder: holder that has claimed @bdev
1082 *
1083 * Abort claiming of a block device when the exclusive open failed. This can be
1084 * also used when exclusive open is not actually desired and we just needed
1085 * to block other exclusive openers for a while.
1086 */
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001087void bd_abort_claiming(struct block_device *bdev, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +02001088{
1089 spin_lock(&bdev_lock);
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001090 bd_clear_claiming(bdev_whole(bdev), holder);
Jan Kara89e524c02019-07-30 13:10:14 +02001091 spin_unlock(&bdev_lock);
1092}
1093EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001094
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001095#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001096struct bd_holder_disk {
1097 struct list_head list;
1098 struct gendisk *disk;
1099 int refcnt;
1100};
1101
1102static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1103 struct gendisk *disk)
1104{
1105 struct bd_holder_disk *holder;
1106
1107 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1108 if (holder->disk == disk)
1109 return holder;
1110 return NULL;
1111}
1112
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001113static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001114{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001115 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001116}
1117
1118static void del_symlink(struct kobject *from, struct kobject *to)
1119{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001120 sysfs_remove_link(from, kobject_name(to));
1121}
1122
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001123/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001124 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1125 * @bdev: the claimed slave bdev
1126 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001127 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001128 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1129 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001130 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001131 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001132 * - from "slaves" directory of the holder @disk to the claimed @bdev
1133 * - from "holders" directory of the @bdev to the holder @disk
1134 *
1135 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1136 * passed to bd_link_disk_holder(), then:
1137 *
1138 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1139 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1140 *
1141 * The caller must have claimed @bdev before calling this function and
1142 * ensure that both @bdev and @disk are valid during the creation and
1143 * lifetime of these symlinks.
1144 *
1145 * CONTEXT:
1146 * Might sleep.
1147 *
1148 * RETURNS:
1149 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001150 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001151int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001152{
Tejun Heo49731ba2011-01-14 18:43:57 +01001153 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001154 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001155
Christoph Hellwiga8698702021-05-25 08:12:56 +02001156 mutex_lock(&bdev->bd_disk->open_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001157
Tejun Heo49731ba2011-01-14 18:43:57 +01001158 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001159
Tejun Heoe09b4572010-11-13 11:55:17 +01001160 /* FIXME: remove the following once add_disk() handles errors */
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001161 if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir))
Tejun Heoe09b4572010-11-13 11:55:17 +01001162 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001163
Tejun Heo49731ba2011-01-14 18:43:57 +01001164 holder = bd_find_holder_disk(bdev, disk);
1165 if (holder) {
1166 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001167 goto out_unlock;
1168 }
1169
Tejun Heo49731ba2011-01-14 18:43:57 +01001170 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1171 if (!holder) {
1172 ret = -ENOMEM;
1173 goto out_unlock;
1174 }
1175
1176 INIT_LIST_HEAD(&holder->list);
1177 holder->disk = disk;
1178 holder->refcnt = 1;
1179
Christoph Hellwig8d652692020-11-17 08:18:55 +01001180 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
Tejun Heo49731ba2011-01-14 18:43:57 +01001181 if (ret)
1182 goto out_free;
1183
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001184 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
Tejun Heo49731ba2011-01-14 18:43:57 +01001185 if (ret)
1186 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001187 /*
1188 * bdev could be deleted beneath us which would implicitly destroy
1189 * the holder directory. Hold on to it.
1190 */
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001191 kobject_get(bdev->bd_holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001192
1193 list_add(&holder->list, &bdev->bd_holder_disks);
1194 goto out_unlock;
1195
1196out_del:
Christoph Hellwig8d652692020-11-17 08:18:55 +01001197 del_symlink(disk->slave_dir, bdev_kobj(bdev));
Tejun Heo49731ba2011-01-14 18:43:57 +01001198out_free:
1199 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001200out_unlock:
Christoph Hellwiga8698702021-05-25 08:12:56 +02001201 mutex_unlock(&bdev->bd_disk->open_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001202 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001203}
Tejun Heoe09b4572010-11-13 11:55:17 +01001204EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001205
Tejun Heo49731ba2011-01-14 18:43:57 +01001206/**
1207 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1208 * @bdev: the calimed slave bdev
1209 * @disk: the holding disk
1210 *
1211 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1212 *
1213 * CONTEXT:
1214 * Might sleep.
1215 */
1216void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001217{
Tejun Heo49731ba2011-01-14 18:43:57 +01001218 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001219
Christoph Hellwiga8698702021-05-25 08:12:56 +02001220 mutex_lock(&bdev->bd_disk->open_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001221
Tejun Heo49731ba2011-01-14 18:43:57 +01001222 holder = bd_find_holder_disk(bdev, disk);
1223
1224 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
Christoph Hellwig8d652692020-11-17 08:18:55 +01001225 del_symlink(disk->slave_dir, bdev_kobj(bdev));
Christoph Hellwig1bdd5ae2020-11-23 19:00:13 +01001226 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
1227 kobject_put(bdev->bd_holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001228 list_del_init(&holder->list);
1229 kfree(holder);
1230 }
1231
Christoph Hellwiga8698702021-05-25 08:12:56 +02001232 mutex_unlock(&bdev->bd_disk->open_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001233}
Tejun Heo49731ba2011-01-14 18:43:57 +01001234EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001235#endif
1236
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001237static void blkdev_flush_mapping(struct block_device *bdev)
1238{
1239 WARN_ON_ONCE(bdev->bd_holders);
1240 sync_blockdev(bdev);
1241 kill_bdev(bdev);
1242 bdev_write_inode(bdev);
1243}
NeilBrown37be4122006-12-08 02:36:16 -08001244
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001245int bdev_disk_changed(struct block_device *bdev, bool invalidate)
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001246{
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001247 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigd173b652021-04-08 21:41:40 +02001248 int ret = 0;
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001249
Christoph Hellwiga8698702021-05-25 08:12:56 +02001250 lockdep_assert_held(&disk->open_mutex);
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001251
Gulam Mohamedbc6a3852021-05-14 15:18:42 +02001252 if (!(disk->flags & GENHD_FL_UP))
1253 return -ENXIO;
1254
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001255rescan:
Christoph Hellwigab4b5702021-05-25 08:12:59 +02001256 if (disk->open_partitions)
Christoph Hellwigd3c4a432021-04-06 08:22:55 +02001257 return -EBUSY;
1258 sync_blockdev(bdev);
1259 invalidate_bdev(bdev);
1260 blk_drop_partitions(disk);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001261
Chris Chiu51167842021-03-23 16:52:19 +08001262 clear_bit(GD_NEED_PART_SCAN, &disk->state);
1263
Christoph Hellwigd981cb52020-03-18 09:12:06 +01001264 /*
1265 * Historically we only set the capacity to zero for devices that
1266 * support partitions (independ of actually having partitions created).
1267 * Doing that is rather inconsistent, but changing it broke legacy
1268 * udisks polling for legacy ide-cdrom devices. Use the crude check
1269 * below to get the sane behavior for most device while not breaking
1270 * userspace for this particular setup.
1271 */
1272 if (invalidate) {
1273 if (disk_part_scan_enabled(disk) ||
1274 !(disk->flags & GENHD_FL_REMOVABLE))
1275 set_capacity(disk, 0);
Christoph Hellwigd981cb52020-03-18 09:12:06 +01001276 }
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001277
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001278 if (get_capacity(disk)) {
1279 ret = blk_add_partitions(disk, bdev);
1280 if (ret == -EAGAIN)
1281 goto rescan;
Eric Biggers490547c2019-12-02 10:21:34 -08001282 } else if (invalidate) {
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001283 /*
1284 * Tell userspace that the media / partition table may have
1285 * changed.
1286 */
1287 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001288 }
1289
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001290 return ret;
1291}
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001292/*
Randy Dunlap3d742d42021-02-24 12:00:48 -08001293 * Only exported for loop and dasd for historic reasons. Don't use in new
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001294 * code!
1295 */
1296EXPORT_SYMBOL_GPL(bdev_disk_changed);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001297
Christoph Hellwig362529d2021-05-25 08:12:54 +02001298static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001300 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwigcb8432d2020-11-26 18:47:17 +01001301 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Christoph Hellwig362529d2021-05-25 08:12:54 +02001303 if (disk->fops->open) {
1304 ret = disk->fops->open(bdev, mode);
1305 if (ret) {
1306 /* avoid ghost partitions on a removed medium */
1307 if (ret == -ENOMEDIUM &&
1308 test_bit(GD_NEED_PART_SCAN, &disk->state))
1309 bdev_disk_changed(bdev, true);
1310 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 }
1312 }
Christoph Hellwig362529d2021-05-25 08:12:54 +02001313
1314 if (!bdev->bd_openers) {
1315 set_init_blocksize(bdev);
1316 if (bdev->bd_bdi == &noop_backing_dev_info)
1317 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1318 }
1319 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
1320 bdev_disk_changed(bdev, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 bdev->bd_openers++;
Christoph Hellwig362529d2021-05-25 08:12:54 +02001322 return 0;;
1323}
1324
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001325static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
1326{
1327 if (!--bdev->bd_openers)
1328 blkdev_flush_mapping(bdev);
1329 if (bdev->bd_disk->fops->release)
1330 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
1331}
1332
Christoph Hellwig362529d2021-05-25 08:12:54 +02001333static int blkdev_get_part(struct block_device *part, fmode_t mode)
1334{
1335 struct gendisk *disk = part->bd_disk;
1336 struct block_device *whole;
1337 int ret;
1338
1339 if (part->bd_openers)
1340 goto done;
1341
1342 whole = bdgrab(disk->part0);
Christoph Hellwig362529d2021-05-25 08:12:54 +02001343 ret = blkdev_get_whole(whole, mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001344 if (ret)
Christoph Hellwig362529d2021-05-25 08:12:54 +02001345 goto out_put_whole;
Christoph Hellwig362529d2021-05-25 08:12:54 +02001346
1347 ret = -ENXIO;
1348 if (!bdev_nr_sectors(part))
1349 goto out_blkdev_put;
1350
Christoph Hellwigab4b5702021-05-25 08:12:59 +02001351 disk->open_partitions++;
Christoph Hellwig362529d2021-05-25 08:12:54 +02001352 set_init_blocksize(part);
1353 if (part->bd_bdi == &noop_backing_dev_info)
1354 part->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1355done:
1356 part->bd_openers++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 return 0;
Christoph Hellwig362529d2021-05-25 08:12:54 +02001358
1359out_blkdev_put:
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001360 blkdev_put_whole(whole, mode);
Christoph Hellwig362529d2021-05-25 08:12:54 +02001361out_put_whole:
1362 bdput(whole);
1363 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001366static void blkdev_put_part(struct block_device *part, fmode_t mode)
1367{
1368 struct block_device *whole = bdev_whole(part);
1369
1370 if (--part->bd_openers)
1371 return;
1372 blkdev_flush_mapping(part);
Christoph Hellwigab4b5702021-05-25 08:12:59 +02001373 whole->bd_disk->open_partitions--;
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001374 blkdev_put_whole(whole, mode);
1375 bdput(whole);
1376}
1377
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001378struct block_device *blkdev_get_no_open(dev_t dev)
1379{
1380 struct block_device *bdev;
1381 struct gendisk *disk;
1382
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001383 bdev = bdget(dev);
1384 if (!bdev) {
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001385 blk_request_module(dev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001386 bdev = bdget(dev);
1387 if (!bdev)
Christoph Hellwig6c60ff02021-05-14 15:18:41 +02001388 return NULL;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001389 }
1390
1391 disk = bdev->bd_disk;
1392 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj))
1393 goto bdput;
1394 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
1395 goto put_disk;
1396 if (!try_module_get(bdev->bd_disk->fops->owner))
1397 goto put_disk;
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001398 return bdev;
1399put_disk:
1400 put_disk(disk);
1401bdput:
1402 bdput(bdev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001403 return NULL;
1404}
1405
1406void blkdev_put_no_open(struct block_device *bdev)
1407{
1408 module_put(bdev->bd_disk->fops->owner);
1409 put_disk(bdev->bd_disk);
1410 bdput(bdev);
1411}
1412
Tejun Heod4d77622010-11-13 11:55:18 +01001413/**
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001414 * blkdev_get_by_dev - open a block device by device number
1415 * @dev: device number of block device to open
Tejun Heod4d77622010-11-13 11:55:18 +01001416 * @mode: FMODE_* mask
1417 * @holder: exclusive holder identifier
1418 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001419 * Open the block device described by device number @dev. If @mode includes
1420 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1421 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1422 * the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +01001423 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001424 * Use this interface ONLY if you really do not have anything better - i.e. when
1425 * you are behind a truly sucky interface and all you are given is a device
1426 * number. Everything else should use blkdev_get_by_path().
Tejun Heod4d77622010-11-13 11:55:18 +01001427 *
1428 * CONTEXT:
1429 * Might sleep.
1430 *
1431 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001432 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +01001433 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001434struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001436 bool unblock_events = true;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001437 struct block_device *bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001438 struct gendisk *disk;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001439 int ret;
Tejun Heoe525fd82010-11-13 11:55:17 +01001440
Christoph Hellwig7918f0f2020-11-23 13:44:44 +01001441 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001442 MAJOR(dev), MINOR(dev),
Christoph Hellwig7918f0f2020-11-23 13:44:44 +01001443 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
1444 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001445 if (ret)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001446 return ERR_PTR(ret);
1447
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001448 bdev = blkdev_get_no_open(dev);
1449 if (!bdev)
1450 return ERR_PTR(-ENXIO);
1451 disk = bdev->bd_disk;
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001452
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001453 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001454 ret = bd_prepare_to_claim(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001455 if (ret)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001456 goto put_blkdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001457 }
1458
1459 disk_block_events(disk);
1460
Christoph Hellwiga8698702021-05-25 08:12:56 +02001461 mutex_lock(&disk->open_mutex);
Christoph Hellwig362529d2021-05-25 08:12:54 +02001462 ret = -ENXIO;
1463 if (!(disk->flags & GENHD_FL_UP))
1464 goto abort_claiming;
1465 if (bdev_is_partition(bdev))
1466 ret = blkdev_get_part(bdev, mode);
1467 else
1468 ret = blkdev_get_whole(bdev, mode);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001469 if (ret)
1470 goto abort_claiming;
1471 if (mode & FMODE_EXCL) {
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001472 bd_finish_claiming(bdev, holder);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001473
1474 /*
1475 * Block event polling for write claims if requested. Any write
1476 * holder makes the write_holder state stick until all are
1477 * released. This is good enough and tracking individual
1478 * writeable reference is too fragile given the way @mode is
1479 * used in blkdev_get/put().
1480 */
1481 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1482 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1483 bdev->bd_write_holder = true;
1484 unblock_events = false;
1485 }
1486 }
Christoph Hellwiga8698702021-05-25 08:12:56 +02001487 mutex_unlock(&disk->open_mutex);
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001488
1489 if (unblock_events)
1490 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001491 return bdev;
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001492
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001493abort_claiming:
1494 if (mode & FMODE_EXCL)
Christoph Hellwig37c3fc92020-11-25 21:20:08 +01001495 bd_abort_claiming(bdev, holder);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001496 mutex_unlock(&disk->open_mutex);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001497 disk_unblock_events(disk);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001498put_blkdev:
1499 blkdev_put_no_open(bdev);
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001500 return ERR_PTR(ret);
NeilBrown37be4122006-12-08 02:36:16 -08001501}
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001502EXPORT_SYMBOL(blkdev_get_by_dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503
Tejun Heod4d77622010-11-13 11:55:18 +01001504/**
1505 * blkdev_get_by_path - open a block device by name
1506 * @path: path to the block device to open
1507 * @mode: FMODE_* mask
1508 * @holder: exclusive holder identifier
1509 *
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001510 * Open the block device described by the device file at @path. If @mode
1511 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1512 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1513 * nest for the same @holder.
Tejun Heod4d77622010-11-13 11:55:18 +01001514 *
1515 * CONTEXT:
1516 * Might sleep.
1517 *
1518 * RETURNS:
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001519 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
Tejun Heod4d77622010-11-13 11:55:18 +01001520 */
1521struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1522 void *holder)
1523{
1524 struct block_device *bdev;
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001525 dev_t dev;
1526 int error;
Tejun Heod4d77622010-11-13 11:55:18 +01001527
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001528 error = lookup_bdev(path, &dev);
1529 if (error)
1530 return ERR_PTR(error);
Tejun Heod4d77622010-11-13 11:55:18 +01001531
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001532 bdev = blkdev_get_by_dev(dev, mode, holder);
1533 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
Chuck Ebberte51900f2011-02-16 18:11:53 -05001534 blkdev_put(bdev, mode);
1535 return ERR_PTR(-EACCES);
1536 }
1537
Tejun Heod4d77622010-11-13 11:55:18 +01001538 return bdev;
1539}
1540EXPORT_SYMBOL(blkdev_get_by_path);
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542static int blkdev_open(struct inode * inode, struct file * filp)
1543{
1544 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
1546 /*
1547 * Preserve backwards compatibility and allow large file access
1548 * even if userspace doesn't ask for it explicitly. Some mkfs
1549 * binary needs it. We might want to drop this workaround
1550 * during an unstable branch.
1551 */
1552 filp->f_flags |= O_LARGEFILE;
1553
Jens Axboea304f072020-05-22 09:14:08 -06001554 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001555
Al Viro572c4892007-10-08 13:24:05 -04001556 if (filp->f_flags & O_NDELAY)
1557 filp->f_mode |= FMODE_NDELAY;
1558 if (filp->f_flags & O_EXCL)
1559 filp->f_mode |= FMODE_EXCL;
1560 if ((filp->f_flags & O_ACCMODE) == 3)
1561 filp->f_mode |= FMODE_WRITE_IOCTL;
1562
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001563 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
1564 if (IS_ERR(bdev))
1565 return PTR_ERR(bdev);
Al Viro572c4892007-10-08 13:24:05 -04001566 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001567 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001568 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Al Viro4385bab2013-05-05 22:11:03 -04001571void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001572{
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001573 struct gendisk *disk = bdev->bd_disk;
1574
Christoph Hellwig210a6d72021-05-25 08:12:55 +02001575 /*
1576 * Sync early if it looks like we're the last one. If someone else
1577 * opens the block device between now and the decrement of bd_openers
1578 * then we did a sync that we didn't need to, but that's not the end
1579 * of the world and we want to avoid long (could be several minute)
1580 * syncs while holding the mutex.
1581 */
1582 if (bdev->bd_openers == 1)
1583 sync_blockdev(bdev);
1584
Christoph Hellwiga8698702021-05-25 08:12:56 +02001585 mutex_lock(&disk->open_mutex);
Tejun Heoe525fd82010-11-13 11:55:17 +01001586 if (mode & FMODE_EXCL) {
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001587 struct block_device *whole = bdev_whole(bdev);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001588 bool bdev_free;
1589
1590 /*
1591 * Release a claim on the device. The holder fields
Christoph Hellwiga8698702021-05-25 08:12:56 +02001592 * are protected with bdev_lock. open_mutex is to
Tejun Heo6a027ef2010-11-13 11:55:17 +01001593 * synchronize disk_holder unlinking.
1594 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001595 spin_lock(&bdev_lock);
1596
1597 WARN_ON_ONCE(--bdev->bd_holders < 0);
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001598 WARN_ON_ONCE(--whole->bd_holders < 0);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001599
Tejun Heo6a027ef2010-11-13 11:55:17 +01001600 if ((bdev_free = !bdev->bd_holders))
1601 bdev->bd_holder = NULL;
Christoph Hellwiga954ea82020-11-23 13:29:55 +01001602 if (!whole->bd_holders)
1603 whole->bd_holder = NULL;
Tejun Heo6a027ef2010-11-13 11:55:17 +01001604
1605 spin_unlock(&bdev_lock);
1606
Tejun Heo77ea8872010-12-08 20:57:37 +01001607 /*
1608 * If this was the last claim, remove holder link and
1609 * unblock evpoll if it was a write holder.
1610 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001611 if (bdev_free && bdev->bd_write_holder) {
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001612 disk_unblock_events(disk);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001613 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001614 }
Tejun Heo69362172011-03-09 19:54:27 +01001615 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001616
Tejun Heo85ef06d2011-07-01 16:17:47 +02001617 /*
1618 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1619 * event. This is to ensure detection of media removal commanded
1620 * from userland - e.g. eject(1).
1621 */
Christoph Hellwig5b56b6ed2020-11-23 10:19:22 +01001622 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
Tejun Heo85ef06d2011-07-01 16:17:47 +02001623
Christoph Hellwigc8276b92021-05-25 08:12:58 +02001624 if (bdev_is_partition(bdev))
1625 blkdev_put_part(bdev, mode);
1626 else
1627 blkdev_put_whole(bdev, mode);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001628 mutex_unlock(&disk->open_mutex);
1629
Christoph Hellwig22ae8ce2020-11-26 09:23:26 +01001630 blkdev_put_no_open(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001631}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001632EXPORT_SYMBOL(blkdev_put);
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634static int blkdev_close(struct inode * inode, struct file * filp)
1635{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001636 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001637 blkdev_put(bdev, filp->f_mode);
1638 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639}
1640
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001641static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001643 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001644 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001645
1646 /*
1647 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1648 * to updated it before every ioctl.
1649 */
Al Viro56b26ad2008-09-19 03:17:36 -04001650 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001651 mode |= FMODE_NDELAY;
1652 else
1653 mode &= ~FMODE_NDELAY;
1654
Al Viro56b26ad2008-09-19 03:17:36 -04001655 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001658/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001659 * Write data to the block device. Only intended for the block device itself
1660 * and the raw driver which basically is a fake block device.
1661 *
1662 * Does not take i_mutex for the write and thus is not for general purpose
1663 * use.
1664 */
Al Viro1456c0a2014-04-03 03:21:50 -04001665ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02001666{
1667 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001668 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04001669 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001670 struct blk_plug plug;
yangerkuncf7b39a2021-04-01 15:18:07 +08001671 size_t shorted = 0;
Christoph Hellwigeef99382009-08-20 17:43:41 +02001672 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04001673
Al Viro7ec7b942015-04-07 11:35:14 -04001674 if (bdev_read_only(I_BDEV(bd_inode)))
1675 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04001676
Christoph Hellwigbb3247a392020-09-21 09:19:55 +02001677 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
Darrick J. Wongdc617f22019-08-20 07:55:16 -07001678 return -ETXTBSY;
1679
Al Viro7ec7b942015-04-07 11:35:14 -04001680 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04001681 return 0;
1682
Al Viro7ec7b942015-04-07 11:35:14 -04001683 if (iocb->ki_pos >= size)
1684 return -ENOSPC;
1685
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001686 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1687 return -EOPNOTSUPP;
1688
yangerkuncf7b39a2021-04-01 15:18:07 +08001689 size -= iocb->ki_pos;
1690 if (iov_iter_count(from) > size) {
1691 shorted = iov_iter_count(from) - size;
1692 iov_iter_truncate(from, size);
1693 }
Christoph Hellwigeef99382009-08-20 17:43:41 +02001694
Jianpeng Ma53362a02012-08-02 09:50:39 +02001695 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04001696 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07001697 if (ret > 0)
1698 ret = generic_write_sync(iocb, ret);
yangerkuncf7b39a2021-04-01 15:18:07 +08001699 iov_iter_reexpand(from, iov_iter_count(from) + shorted);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001700 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001701 return ret;
1702}
Al Viro1456c0a2014-04-03 03:21:50 -04001703EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001704
David Jefferyb2de5252014-09-29 10:21:10 -04001705ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001706{
1707 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001708 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001709 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04001710 loff_t pos = iocb->ki_pos;
yangerkuncf7b39a2021-04-01 15:18:07 +08001711 size_t shorted = 0;
1712 ssize_t ret;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001713
1714 if (pos >= size)
1715 return 0;
1716
1717 size -= pos;
yangerkuncf7b39a2021-04-01 15:18:07 +08001718 if (iov_iter_count(to) > size) {
1719 shorted = iov_iter_count(to) - size;
1720 iov_iter_truncate(to, size);
1721 }
1722
1723 ret = generic_file_read_iter(iocb, to);
1724 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
1725 return ret;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001726}
David Jefferyb2de5252014-09-29 10:21:10 -04001727EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001728
Christoph Hellwigeef99382009-08-20 17:43:41 +02001729/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001730 * Try to release a page associated with block device when the system
1731 * is under memory pressure.
1732 */
1733static int blkdev_releasepage(struct page *page, gfp_t wait)
1734{
1735 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1736
1737 if (super && super->s_op->bdev_try_to_free_page)
1738 return super->s_op->bdev_try_to_free_page(super, page, wait);
1739
1740 return try_to_free_buffers(page);
1741}
1742
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001743static int blkdev_writepages(struct address_space *mapping,
1744 struct writeback_control *wbc)
1745{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001746 return generic_writepages(mapping, wbc);
1747}
1748
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001749static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .readpage = blkdev_readpage,
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001751 .readahead = blkdev_readahead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001753 .write_begin = blkdev_write_begin,
1754 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001755 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001756 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08001758 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07001759 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760};
1761
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001762#define BLKDEV_FALLOC_FL_SUPPORTED \
1763 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1764 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1765
1766static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1767 loff_t len)
1768{
1769 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001770 loff_t end = start + len - 1;
1771 loff_t isize;
1772 int error;
1773
1774 /* Fail if we don't recognize the flags. */
1775 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1776 return -EOPNOTSUPP;
1777
1778 /* Don't go off the end of the device. */
1779 isize = i_size_read(bdev->bd_inode);
1780 if (start >= isize)
1781 return -EINVAL;
1782 if (end >= isize) {
1783 if (mode & FALLOC_FL_KEEP_SIZE) {
1784 len = isize - start;
1785 end = start + len - 1;
1786 } else
1787 return -EINVAL;
1788 }
1789
1790 /*
1791 * Don't allow IO that isn't aligned to logical block size.
1792 */
1793 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1794 return -EINVAL;
1795
1796 /* Invalidate the page cache, including dirty pages. */
Jan Kara384d87e2020-09-04 10:58:52 +02001797 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1798 if (error)
1799 return error;
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001800
1801 switch (mode) {
1802 case FALLOC_FL_ZERO_RANGE:
1803 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1804 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02001805 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001806 break;
1807 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02001808 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1809 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001810 break;
1811 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001812 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1813 GFP_KERNEL, 0);
1814 break;
1815 default:
1816 return -EOPNOTSUPP;
1817 }
1818 if (error)
1819 return error;
1820
1821 /*
Jan Kara767630c2021-01-07 16:40:34 +01001822 * Invalidate the page cache again; if someone wandered in and dirtied
1823 * a page, we just discard it - userspace has no way of knowing whether
1824 * the write happened before or after discard completing...
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001825 */
Jan Kara767630c2021-01-07 16:40:34 +01001826 return truncate_bdev_range(bdev, file->f_mode, start, end);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001827}
1828
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001829const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 .open = blkdev_open,
1831 .release = blkdev_close,
1832 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04001833 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04001834 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07001835 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07001836 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07001837 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001838 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839#ifdef CONFIG_COMPAT
1840 .compat_ioctl = compat_blkdev_ioctl,
1841#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08001842 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04001843 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001844 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845};
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847/**
1848 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08001849 * @pathname: special file representing the block device
Randy Dunlap875b2372020-12-28 19:47:06 -08001850 * @dev: return value of the block device's dev_t
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02001852 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 * namespace if possible and return it. Return ERR_PTR(error)
1854 * otherwise.
1855 */
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001856int lookup_bdev(const char *pathname, dev_t *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04001859 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 int error;
1861
Al Viro421748e2008-08-02 01:04:36 -04001862 if (!pathname || !*pathname)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001863 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Al Viro421748e2008-08-02 01:04:36 -04001865 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 if (error)
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001867 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
David Howellsbb6687342015-03-17 22:26:21 +00001869 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 error = -ENOTBLK;
1871 if (!S_ISBLK(inode->i_mode))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001872 goto out_path_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05001874 if (!may_open_dev(&path))
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001875 goto out_path_put;
1876
1877 *dev = inode->i_rdev;
1878 error = 0;
1879out_path_put:
Al Viro421748e2008-08-02 01:04:36 -04001880 path_put(&path);
Christoph Hellwig4e7b5672020-11-23 13:38:40 +01001881 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882}
Al Virod5686b42008-08-01 05:00:11 -04001883EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
NeilBrown93b270f2011-02-24 17:25:47 +11001885int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01001886{
1887 struct super_block *sb = get_super(bdev);
1888 int res = 0;
1889
1890 if (sb) {
1891 /*
1892 * no need to lock the super, get_super holds the
1893 * read mutex so the filesystem cannot go away
1894 * under us (->put_super runs with the write lock
1895 * hold).
1896 */
1897 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11001898 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01001899 drop_super(sb);
1900 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07001901 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01001902 return res;
1903}
1904EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001905
1906void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1907{
1908 struct inode *inode, *old_inode = NULL;
1909
Dave Chinner74278da2015-03-04 12:37:22 -05001910 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001911 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1912 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01001913 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02001914
1915 spin_lock(&inode->i_lock);
1916 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1917 mapping->nrpages == 0) {
1918 spin_unlock(&inode->i_lock);
1919 continue;
1920 }
1921 __iget(inode);
1922 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05001923 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001924 /*
1925 * We hold a reference to 'inode' so it couldn't have been
1926 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05001927 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02001928 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05001929 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02001930 * later.
1931 */
1932 iput(old_inode);
1933 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01001934 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001935
Christoph Hellwiga8698702021-05-25 08:12:56 +02001936 mutex_lock(&bdev->bd_disk->open_mutex);
Rabin Vincentaf309222016-12-01 09:18:28 +01001937 if (bdev->bd_openers)
1938 func(bdev, arg);
Christoph Hellwiga8698702021-05-25 08:12:56 +02001939 mutex_unlock(&bdev->bd_disk->open_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001940
Dave Chinner74278da2015-03-04 12:37:22 -05001941 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001942 }
Dave Chinner74278da2015-03-04 12:37:22 -05001943 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02001944 iput(old_inode);
1945}