blob: 37662a52d538ff3cba3044d4efeaff36dd9c8612 [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/*
3 * linux/fs/block_dev.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/mm.h>
11#include <linux/fcntl.h>
12#include <linux/slab.h>
13#include <linux/kmod.h>
14#include <linux/major.h>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070015#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/highmem.h>
17#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040018#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070021#include <linux/magic.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040023#include <linux/swap.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010024#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010025#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mpage.h>
27#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000028#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/uio.h>
30#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070031#include <linux/log2.h>
Al Viroff01bb42011-09-16 02:31:11 -040032#include <linux/cleancache.h>
Christoph Hellwig189ce2b2016-10-31 11:59:25 -060033#include <linux/task_io_accounting_ops.h>
Darrick J. Wong25f4c412016-10-11 13:51:11 -070034#include <linux/falloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080035#include <linux/uaccess.h>
Domenico Andreoli56939e02020-03-23 08:22:15 -070036#include <linux/suspend.h>
David Howells07f3f052006-09-30 20:52:18 +020037#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct bdev_inode {
40 struct block_device bdev;
41 struct inode vfs_inode;
42};
43
Adrian Bunk4c54ac62008-02-18 13:48:31 +010044static const struct address_space_operations def_blk_aops;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static inline struct bdev_inode *BDEV_I(struct inode *inode)
47{
48 return container_of(inode, struct bdev_inode, vfs_inode);
49}
50
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020051struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 return &BDEV_I(inode)->bdev;
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055EXPORT_SYMBOL(I_BDEV);
56
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070057static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010058{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070059 struct inode *inode = bdev->bd_inode;
60 int ret;
61
Christoph Hellwig564f00f2015-01-14 10:42:33 +010062 spin_lock(&inode->i_lock);
63 while (inode->i_state & I_DIRTY) {
64 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070065 ret = write_inode_now(inode, true);
66 if (ret) {
67 char name[BDEVNAME_SIZE];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev, name), ret);
71 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010072 spin_lock(&inode->i_lock);
73 }
74 spin_unlock(&inode->i_lock);
75}
76
Peter Zijlstraf9a14392007-05-06 14:49:55 -070077/* Kill _all_ buffers and pagecache , dirty or not.. */
Zheng Bin3373a342020-06-18 12:21:38 +080078static void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viroff01bb42011-09-16 02:31:11 -040080 struct address_space *mapping = bdev->bd_inode->i_mapping;
81
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080082 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
Peter Zijlstraf9a14392007-05-06 14:49:55 -070083 return;
Al Viroff01bb42011-09-16 02:31:11 -040084
Peter Zijlstraf9a14392007-05-06 14:49:55 -070085 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040086 truncate_inode_pages(mapping, 0);
Zheng Bin3373a342020-06-18 12:21:38 +080087}
Al Viroff01bb42011-09-16 02:31:11 -040088
89/* Invalidate clean unused buffers and pagecache. */
90void invalidate_bdev(struct block_device *bdev)
91{
92 struct address_space *mapping = bdev->bd_inode->i_mapping;
93
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070094 if (mapping->nrpages) {
95 invalidate_bh_lrus();
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping, 0, -1);
98 }
Al Viroff01bb42011-09-16 02:31:11 -040099 /* 99% of the time, we don't need to flush the cleancache on the bdev.
100 * But, for the strange corners, lets be cautious
101 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400102 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400103}
104EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jan Kara384d87e2020-09-04 10:58:52 +0200106/*
107 * Drop all buffers & page cache for given bdev range. This function bails
108 * with error if bdev has other exclusive owner (such as filesystem).
109 */
110int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111 loff_t lstart, loff_t lend)
112{
113 struct block_device *claimed_bdev = NULL;
114 int err;
115
116 /*
117 * If we don't hold exclusive handle for the device, upgrade to it
118 * while we discard the buffer cache to avoid discarding buffers
119 * under live filesystem.
120 */
121 if (!(mode & FMODE_EXCL)) {
122 claimed_bdev = bdev->bd_contains;
123 err = bd_prepare_to_claim(bdev, claimed_bdev,
124 truncate_bdev_range);
125 if (err)
126 return err;
127 }
128 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
129 if (claimed_bdev)
130 bd_abort_claiming(bdev, claimed_bdev, truncate_bdev_range);
131 return 0;
132}
133EXPORT_SYMBOL(truncate_bdev_range);
134
Jan Kara04906b22019-01-14 09:48:10 +0100135static void set_init_blocksize(struct block_device *bdev)
136{
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200137 bdev->bd_inode->i_blkbits = blksize_bits(bdev_logical_block_size(bdev));
Jan Kara04906b22019-01-14 09:48:10 +0100138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140int set_blocksize(struct block_device *bdev, int size)
141{
142 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700143 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 return -EINVAL;
145
146 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400147 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return -EINVAL;
149
150 /* Don't change the size if it is same as current */
Christoph Hellwig6b7b1812020-06-26 10:01:55 +0200151 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 sync_blockdev(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 bdev->bd_inode->i_blkbits = blksize_bits(size);
154 kill_bdev(bdev);
155 }
156 return 0;
157}
158
159EXPORT_SYMBOL(set_blocksize);
160
161int sb_set_blocksize(struct super_block *sb, int size)
162{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (set_blocksize(sb->s_bdev, size))
164 return 0;
165 /* If we get here, we know size is power of two
166 * and it's value is between 512 and PAGE_SIZE */
167 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800168 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return sb->s_blocksize;
170}
171
172EXPORT_SYMBOL(sb_set_blocksize);
173
174int sb_min_blocksize(struct super_block *sb, int size)
175{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400176 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 if (size < minsize)
178 size = minsize;
179 return sb_set_blocksize(sb, size);
180}
181
182EXPORT_SYMBOL(sb_min_blocksize);
183
184static int
185blkdev_get_block(struct inode *inode, sector_t iblock,
186 struct buffer_head *bh, int create)
187{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 bh->b_bdev = I_BDEV(inode);
189 bh->b_blocknr = iblock;
190 set_buffer_mapped(bh);
191 return 0;
192}
193
Dan Williams4ebb16c2015-10-28 07:48:19 +0900194static struct inode *bdev_file_inode(struct file *file)
195{
196 return file->f_mapping->host;
197}
198
Jens Axboe78250c02016-11-17 17:50:47 +0100199static unsigned int dio_bio_write_op(struct kiocb *iocb)
200{
201 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
202
203 /* avoid the need for a I/O completion work item */
204 if (iocb->ki_flags & IOCB_DSYNC)
205 op |= REQ_FUA;
206 return op;
207}
208
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600209#define DIO_INLINE_BIO_VECS 4
210
211static void blkdev_bio_end_io_simple(struct bio *bio)
212{
213 struct task_struct *waiter = bio->bi_private;
214
215 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700216 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600217}
218
219static ssize_t
220__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
221 int nr_pages)
222{
223 struct file *file = iocb->ki_filp;
224 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200225 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600226 loff_t pos = iocb->ki_pos;
227 bool should_dirty = false;
228 struct bio bio;
229 ssize_t ret;
230 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600231
Jens Axboe9a794fb2016-11-22 08:12:39 -0700232 if ((pos | iov_iter_alignment(iter)) &
233 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600234 return -EINVAL;
235
Jens Axboe72ecad22016-11-16 23:11:42 -0700236 if (nr_pages <= DIO_INLINE_BIO_VECS)
237 vecs = inline_vecs;
238 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700239 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
240 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700241 if (!vecs)
242 return -ENOMEM;
243 }
244
Ming Lei3a83f462016-11-22 08:57:21 -0700245 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200246 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900247 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600248 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600249 bio.bi_private = current;
250 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700251 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600252
253 ret = bio_iov_iter_get_pages(&bio, iter);
254 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200255 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600256 ret = bio.bi_iter.bi_size;
257
258 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100259 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600260 if (iter_is_iovec(iter))
261 should_dirty = true;
262 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100263 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600264 task_io_account_write(ret);
265 }
Jens Axboed1e36282018-08-29 10:36:56 -0600266 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700267 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600268
269 qc = submit_bio(&bio);
270 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800271 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600272 if (!READ_ONCE(bio.bi_private))
273 break;
274 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700275 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800276 blk_io_schedule();
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600277 }
278 __set_current_state(TASK_RUNNING);
279
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200280 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200281 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700282 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600283
Martin Wilck9362dd12018-07-25 23:15:08 +0200284out:
285 if (vecs != inline_vecs)
286 kfree(vecs);
287
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600288 bio_uninit(&bio);
289
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600290 return ret;
291}
292
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700293struct blkdev_dio {
294 union {
295 struct kiocb *iocb;
296 struct task_struct *waiter;
297 };
298 size_t size;
299 atomic_t ref;
300 bool multi_bio : 1;
301 bool should_dirty : 1;
302 bool is_sync : 1;
303 struct bio bio;
304};
305
Kent Overstreet52190f82018-05-20 18:25:55 -0400306static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700307
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700308static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
309{
310 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
311 struct request_queue *q = bdev_get_queue(bdev);
312
313 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
314}
315
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700316static void blkdev_bio_end_io(struct bio *bio)
317{
318 struct blkdev_dio *dio = bio->bi_private;
319 bool should_dirty = dio->should_dirty;
320
Jason Yana89afe52019-04-12 10:09:16 +0800321 if (bio->bi_status && !dio->bio.bi_status)
322 dio->bio.bi_status = bio->bi_status;
323
324 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700325 if (!dio->is_sync) {
326 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200327 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700328
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200329 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700330 ret = dio->size;
331 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200332 } else {
333 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700334 }
335
336 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100337 if (dio->multi_bio)
338 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700339 } else {
340 struct task_struct *waiter = dio->waiter;
341
342 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700343 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700344 }
345 }
346
347 if (should_dirty) {
348 bio_check_pages_dirty(bio);
349 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200350 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700351 bio_put(bio);
352 }
353}
354
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800355static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700356__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800357{
358 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900359 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700360 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100361 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700362 struct blkdev_dio *dio;
363 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700364 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100365 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700366 loff_t pos = iocb->ki_pos;
367 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe7b6620d2019-08-15 11:09:16 -0600368 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700369
Jens Axboe9a794fb2016-11-22 08:12:39 -0700370 if ((pos | iov_iter_alignment(iter)) &
371 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700372 return -EINVAL;
373
Jens Axboe7b6620d2019-08-15 11:09:16 -0600374 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700375
376 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100377 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100378 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700379 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100380 bio_get(bio);
381 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700382 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100383 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700384
385 dio->size = 0;
386 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100387 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700388
Jens Axboecb700eb2018-11-15 19:56:53 -0700389 /*
390 * Don't plug for HIPRI/polled IO, as those should go straight
391 * to issue
392 */
393 if (!is_poll)
394 blk_start_plug(&plug);
395
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700396 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200397 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900398 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600399 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700400 bio->bi_private = dio;
401 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700402 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700403
Jens Axboee15c2ff2019-08-06 13:34:31 -0600404 ret = bio_iov_iter_get_pages(bio, iter);
405 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200406 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700407 bio_endio(bio);
408 break;
409 }
410
411 if (is_read) {
412 bio->bi_opf = REQ_OP_READ;
413 if (dio->should_dirty)
414 bio_set_pages_dirty(bio);
415 } else {
416 bio->bi_opf = dio_bio_write_op(iocb);
417 task_io_account_write(bio->bi_iter.bi_size);
418 }
419
Jens Axboe7b6620d2019-08-15 11:09:16 -0600420 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700421 pos += bio->bi_iter.bi_size;
422
423 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
424 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700425 bool polled = false;
426
427 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700428 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700429 polled = true;
430 }
Jens Axboed34513d2018-11-06 14:29:11 -0700431
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700432 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700433
434 if (polled)
435 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700436 break;
437 }
438
439 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100440 /*
441 * AIO needs an extra reference to ensure the dio
442 * structure which is embedded into the first bio
443 * stays around.
444 */
445 if (!is_sync)
446 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700447 dio->multi_bio = true;
448 atomic_set(&dio->ref, 2);
449 } else {
450 atomic_inc(&dio->ref);
451 }
452
Jens Axboe7b6620d2019-08-15 11:09:16 -0600453 submit_bio(bio);
454 bio = bio_alloc(GFP_KERNEL, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700455 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700456
457 if (!is_poll)
458 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700459
Christoph Hellwig690e5322017-01-24 14:50:19 +0100460 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700461 return -EIOCBQUEUED;
462
463 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800464 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700465 if (!READ_ONCE(dio->waiter))
466 break;
467
468 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700469 !blk_poll(bdev_get_queue(bdev), qc, true))
Ming Leie6249cd2020-05-03 09:54:22 +0800470 blk_io_schedule();
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700471 }
472 __set_current_state(TASK_RUNNING);
473
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200474 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200475 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600476 if (likely(!ret))
477 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700478
479 bio_put(&dio->bio);
480 return ret;
481}
482
483static ssize_t
484blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
485{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600486 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800487
Jens Axboe72ecad22016-11-16 23:11:42 -0700488 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600489 if (!nr_pages)
490 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700491 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600492 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700493
494 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800495}
496
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700497static __init int blkdev_init(void)
498{
Kent Overstreet52190f82018-05-20 18:25:55 -0400499 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700500}
501module_init(blkdev_init);
502
Jan Kara5cee5812009-04-27 16:43:51 +0200503int __sync_blockdev(struct block_device *bdev, int wait)
504{
505 if (!bdev)
506 return 0;
507 if (!wait)
508 return filemap_flush(bdev->bd_inode->i_mapping);
509 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
510}
511
Nick Piggin585d3bc2009-02-25 10:44:19 +0100512/*
513 * Write out and wait upon all the dirty data associated with a block
514 * device via its mapping. Does not take the superblock lock.
515 */
516int sync_blockdev(struct block_device *bdev)
517{
Jan Kara5cee5812009-04-27 16:43:51 +0200518 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100519}
520EXPORT_SYMBOL(sync_blockdev);
521
522/*
523 * Write out and wait upon all dirty data associated with this
524 * device. Filesystem data as well as the underlying block
525 * device. Takes the superblock lock.
526 */
527int fsync_bdev(struct block_device *bdev)
528{
529 struct super_block *sb = get_super(bdev);
530 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200531 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100532 drop_super(sb);
533 return res;
534 }
535 return sync_blockdev(bdev);
536}
Al Viro47e44912009-04-01 07:07:16 -0400537EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100538
539/**
540 * freeze_bdev -- lock a filesystem and force it into a consistent state
541 * @bdev: blockdevice to lock
542 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100543 * If a superblock is found on this device, we take the s_umount semaphore
544 * on it to make sure nobody unmounts until the snapshot creation is done.
545 * The reference counter (bd_fsfreeze_count) guarantees that only the last
546 * unfreeze process can unfreeze the frozen filesystem actually when multiple
547 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
548 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
549 * actually.
550 */
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100551int freeze_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100552{
553 struct super_block *sb;
554 int error = 0;
555
556 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100557 if (++bdev->bd_fsfreeze_count > 1)
558 goto done;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100559
Christoph Hellwig45042302009-08-03 23:28:35 +0200560 sb = get_active_super(bdev);
561 if (!sb)
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100562 goto sync;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600563 if (sb->s_op->freeze_super)
564 error = sb->s_op->freeze_super(sb);
565 else
566 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400567 deactivate_super(sb);
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100568
569 if (error) {
570 bdev->bd_fsfreeze_count--;
571 goto done;
572 }
573 bdev->bd_fsfreeze_sb = sb;
574
575sync:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100576 sync_blockdev(bdev);
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100577done:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100578 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100579 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100580}
581EXPORT_SYMBOL(freeze_bdev);
582
583/**
584 * thaw_bdev -- unlock filesystem
585 * @bdev: blockdevice to unlock
Nick Piggin585d3bc2009-02-25 10:44:19 +0100586 *
587 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
588 */
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100589int thaw_bdev(struct block_device *bdev)
Nick Piggin585d3bc2009-02-25 10:44:19 +0100590{
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100591 struct super_block *sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200592 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100593
594 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200595 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400596 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100597
Christoph Hellwig45042302009-08-03 23:28:35 +0200598 error = 0;
599 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400600 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100601
Christoph Hellwigd96cd09a2020-11-24 11:54:06 +0100602 sb = bdev->bd_fsfreeze_sb;
Christoph Hellwig45042302009-08-03 23:28:35 +0200603 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400604 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200605
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600606 if (sb->s_op->thaw_super)
607 error = sb->s_op->thaw_super(sb);
608 else
609 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200610 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400611 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400612out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100613 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200614 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100615}
616EXPORT_SYMBOL(thaw_bdev);
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
619{
620 return block_write_full_page(page, blkdev_get_block, wbc);
621}
622
623static int blkdev_readpage(struct file * file, struct page * page)
624{
625 return block_read_full_page(page, blkdev_get_block);
626}
627
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700628static void blkdev_readahead(struct readahead_control *rac)
Akinobu Mita447f05b2014-10-09 15:26:58 -0700629{
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -0700630 mpage_readahead(rac, blkdev_get_block);
Akinobu Mita447f05b2014-10-09 15:26:58 -0700631}
632
Nick Piggin6272b5a2007-10-16 01:25:04 -0700633static int blkdev_write_begin(struct file *file, struct address_space *mapping,
634 loff_t pos, unsigned len, unsigned flags,
635 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200637 return block_write_begin(mapping, pos, len, flags, pagep,
638 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Nick Piggin6272b5a2007-10-16 01:25:04 -0700641static int blkdev_write_end(struct file *file, struct address_space *mapping,
642 loff_t pos, unsigned len, unsigned copied,
643 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700645 int ret;
646 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
647
648 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300649 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700650
651 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/*
655 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500656 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * so we compute the size by hand (just as in block_read/write above)
658 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800659static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900661 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 loff_t retval;
663
Al Viro59551022016-01-22 15:40:57 -0500664 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400665 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500666 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return retval;
668}
669
Josef Bacik02c24a82011-07-16 20:44:56 -0400670int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900672 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400673 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100674 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200675
Jeff Layton372cf242017-07-06 07:02:28 -0400676 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200677 if (error)
678 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100679
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400680 /*
681 * There is no need to serialise calls to blkdev_issue_flush with
682 * i_mutex and doing so causes performance issues with concurrent
683 * O_SYNC writers to a block device.
684 */
Christoph Hellwig93985542020-05-13 14:36:00 +0200685 error = blkdev_issue_flush(bdev, GFP_KERNEL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100686 if (error == -EOPNOTSUPP)
687 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400688
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100689 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700691EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700693/**
694 * bdev_read_page() - Start reading a page from a block device
695 * @bdev: The device to read the page from
696 * @sector: The offset on the device to read the page to (need not be aligned)
697 * @page: The page to read
698 *
699 * On entry, the page should be locked. It will be unlocked when the page
700 * has been read. If the block driver implements rw_page synchronously,
701 * that will be true on exit from this function, but it need not be.
702 *
703 * Errors returned by this function are usually "soft", eg out of memory, or
704 * queue full; callers should try a different route to read this page rather
705 * than propagate an error back up the stack.
706 *
707 * Return: negative errno if an error occurs, 0 if submission was successful.
708 */
709int bdev_read_page(struct block_device *bdev, sector_t sector,
710 struct page *page)
711{
712 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800713 int result = -EOPNOTSUPP;
714
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400715 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800716 return result;
717
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200718 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800719 if (result)
720 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700721 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
722 REQ_OP_READ);
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200723 blk_queue_exit(bdev->bd_disk->queue);
Dan Williams2e6edc952015-11-19 13:29:28 -0800724 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700725}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700726
727/**
728 * bdev_write_page() - Start writing a page to a block device
729 * @bdev: The device to write the page to
730 * @sector: The offset on the device to write the page to (need not be aligned)
731 * @page: The page to write
732 * @wbc: The writeback_control for the write
733 *
734 * On entry, the page should be locked and not currently under writeback.
735 * On exit, if the write started successfully, the page will be unlocked and
736 * under writeback. If the write failed already (eg the driver failed to
737 * queue the page to the device), the page will still be locked. If the
738 * caller is a ->writepage implementation, it will need to unlock the page.
739 *
740 * Errors returned by this function are usually "soft", eg out of memory, or
741 * queue full; callers should try a different route to write this page rather
742 * than propagate an error back up the stack.
743 *
744 * Return: negative errno if an error occurs, 0 if submission was successful.
745 */
746int bdev_write_page(struct block_device *bdev, sector_t sector,
747 struct page *page, struct writeback_control *wbc)
748{
749 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700750 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800751
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400752 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700753 return -EOPNOTSUPP;
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200754 result = blk_queue_enter(bdev->bd_disk->queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800755 if (result)
756 return result;
757
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700758 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700759 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
760 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700761 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700762 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700763 } else {
764 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700765 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700766 }
Christoph Hellwige556f6b2020-06-26 10:01:56 +0200767 blk_queue_exit(bdev->bd_disk->queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700768 return result;
769}
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771/*
772 * pseudo-fs
773 */
774
775static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800776static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778static struct inode *bdev_alloc_inode(struct super_block *sb)
779{
Christoph Lametere94b1762006-12-06 20:33:17 -0800780 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (!ei)
782 return NULL;
783 return &ei->vfs_inode;
784}
785
Al Viro41149cb2019-04-10 15:12:38 -0400786static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Al Viro41149cb2019-04-10 15:12:38 -0400788 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100789}
790
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700791static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792{
793 struct bdev_inode *ei = (struct bdev_inode *) foo;
794 struct block_device *bdev = &ei->bdev;
795
Christoph Lametera35afb82007-05-16 22:10:57 -0700796 memset(bdev, 0, sizeof(*bdev));
797 mutex_init(&bdev->bd_mutex);
Tejun Heo49731ba2011-01-14 18:43:57 +0100798#ifdef CONFIG_SYSFS
799 INIT_LIST_HEAD(&bdev->bd_holder_disks);
800#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100801 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700802 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800803 /* Initialize mutex for freeze. */
804 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Al Virob57922d2010-06-07 14:34:48 -0400807static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
809 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700810 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400811 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200812 clear_inode(inode);
Jan Karaf7597412017-03-23 01:37:00 +0100813 /* Detach inode from wb early as bdi_put() may free bdi->wb */
814 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100815 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100816 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100817 bdev->bd_bdi = &noop_backing_dev_info;
818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819}
820
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800821static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .statfs = simple_statfs,
823 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400824 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400826 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827};
828
David Howells9030d162019-03-25 16:38:23 +0000829static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830{
David Howells9030d162019-03-25 16:38:23 +0000831 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
832 if (!ctx)
833 return -ENOMEM;
834 fc->s_iflags |= SB_I_CGROUPWB;
835 ctx->ops = &bdev_sops;
836 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
839static struct file_system_type bd_type = {
840 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000841 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 .kill_sb = kill_anon_super,
843};
844
Tejun Heoa212b102015-05-22 17:13:33 -0400845struct super_block *blockdev_superblock __read_mostly;
846EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848void __init bdev_cache_init(void)
849{
850 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300851 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800854 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800855 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900856 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 err = register_filesystem(&bd_type);
858 if (err)
859 panic("Cannot register bdev pseudo-fs");
860 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (IS_ERR(bd_mnt))
862 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300863 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
866/*
867 * Most likely _very_ bad one - but then it's hardly critical for small
868 * /dev and can be fixed when somebody will need really large one.
869 * Keep in mind that it will be fed through icache hash function too.
870 */
871static inline unsigned long hash(dev_t dev)
872{
873 return MAJOR(dev)+MINOR(dev);
874}
875
876static int bdev_test(struct inode *inode, void *data)
877{
878 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
879}
880
881static int bdev_set(struct inode *inode, void *data)
882{
883 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
884 return 0;
885}
886
Christoph Hellwig10ed1662020-09-25 18:06:18 +0200887static struct block_device *bdget(dev_t dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
889 struct block_device *bdev;
890 struct inode *inode;
891
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800892 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 bdev_test, bdev_set, &dev);
894
895 if (!inode)
896 return NULL;
897
898 bdev = &BDEV_I(inode)->bdev;
899
900 if (inode->i_state & I_NEW) {
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +0200901 spin_lock_init(&bdev->bd_size_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000903 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 bdev->bd_inode = inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 bdev->bd_part_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 inode->i_mode = S_IFBLK;
907 inode->i_rdev = dev;
908 inode->i_bdev = bdev;
909 inode->i_data.a_ops = &def_blk_aops;
910 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 unlock_new_inode(inode);
912 }
913 return bdev;
914}
915
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200916/**
917 * bdgrab -- Grab a reference to an already referenced block device
918 * @bdev: Block device to grab a reference to.
919 */
920struct block_device *bdgrab(struct block_device *bdev)
921{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400922 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200923 return bdev;
924}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700925EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200926
Christoph Hellwig10ed1662020-09-25 18:06:18 +0200927struct block_device *bdget_part(struct hd_struct *part)
928{
929 return bdget(part_devt(part));
930}
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932long nr_blockdev_pages(void)
933{
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200934 struct inode *inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 long ret = 0;
Christoph Hellwig1008fe62020-06-26 10:01:58 +0200936
937 spin_lock(&blockdev_superblock->s_inode_list_lock);
938 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
939 ret += inode->i_mapping->nrpages;
940 spin_unlock(&blockdev_superblock->s_inode_list_lock);
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return ret;
943}
944
945void bdput(struct block_device *bdev)
946{
947 iput(bdev->bd_inode);
948}
949
950EXPORT_SYMBOL(bdput);
951
952static struct block_device *bd_acquire(struct inode *inode)
953{
954 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 spin_lock(&bdev_lock);
957 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +0100958 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100959 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 spin_unlock(&bdev_lock);
961 return bdev;
962 }
963 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700964
Jan Karacccd9fb2017-02-21 18:09:48 +0100965 /*
966 * i_bdev references block device inode that was already shut down
967 * (corresponding device got removed). Remove the reference and look
968 * up block device inode again just in case new device got
969 * reestablished under the same device number.
970 */
971 if (bdev)
972 bd_forget(inode);
973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 bdev = bdget(inode->i_rdev);
975 if (bdev) {
976 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700977 if (!inode->i_bdev) {
978 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -0400979 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700980 * and it's released in clear_inode() of inode.
981 * So, we can access it via ->i_mapping always
982 * without igrab().
983 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100984 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700985 inode->i_bdev = bdev;
986 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 spin_unlock(&bdev_lock);
989 }
990 return bdev;
991}
992
993/* Call when you free inode */
994
995void bd_forget(struct inode *inode)
996{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700997 struct block_device *bdev = NULL;
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001000 if (!sb_is_blkdev_sb(inode->i_sb))
1001 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001002 inode->i_bdev = NULL;
1003 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001005
1006 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001007 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008}
1009
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001010/**
1011 * bd_may_claim - test whether a block device can be claimed
1012 * @bdev: block device of interest
1013 * @whole: whole block device containing @bdev, may equal @bdev
1014 * @holder: holder trying to claim @bdev
1015 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001016 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001017 *
1018 * CONTEXT:
1019 * spin_lock(&bdev_lock).
1020 *
1021 * RETURNS:
1022 * %true if @bdev can be claimed, %false otherwise.
1023 */
1024static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1025 void *holder)
1026{
1027 if (bdev->bd_holder == holder)
1028 return true; /* already a holder */
1029 else if (bdev->bd_holder != NULL)
1030 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001031 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001032 return true; /* is a whole device which isn't held */
1033
Tejun Heoe525fd82010-11-13 11:55:17 +01001034 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001035 return true; /* is a partition of a device that is being partitioned */
1036 else if (whole->bd_holder != NULL)
1037 return false; /* is a partition of a held device */
1038 else
1039 return true; /* is a partition of an un-held device */
1040}
1041
1042/**
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001043 * bd_prepare_to_claim - claim a block device
Tejun Heo6b4517a2010-04-07 18:53:59 +09001044 * @bdev: block device of interest
1045 * @whole: the whole device containing @bdev, may equal @bdev
1046 * @holder: holder trying to claim @bdev
1047 *
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001048 * Claim @bdev. This function fails if @bdev is already claimed by another
1049 * holder and waits if another claiming is in progress. return, the caller
1050 * has ownership of bd_claiming and bd_holder[s].
Tejun Heo6b4517a2010-04-07 18:53:59 +09001051 *
1052 * RETURNS:
1053 * 0 if @bdev can be claimed, -EBUSY otherwise.
1054 */
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +02001055int bd_prepare_to_claim(struct block_device *bdev, struct block_device *whole,
1056 void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001057{
1058retry:
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001059 spin_lock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001060 /* if someone else claimed, fail */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001061 if (!bd_may_claim(bdev, whole, holder)) {
1062 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001063 return -EBUSY;
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001064 }
Tejun Heo6b4517a2010-04-07 18:53:59 +09001065
Tejun Heoe75aa852010-08-04 17:59:39 +02001066 /* if claiming is already in progress, wait for it to finish */
1067 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001068 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1069 DEFINE_WAIT(wait);
1070
1071 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1072 spin_unlock(&bdev_lock);
1073 schedule();
1074 finish_wait(wq, &wait);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001075 goto retry;
1076 }
1077
1078 /* yay, all mine */
Christoph Hellwig58e46ed2020-07-16 16:33:08 +02001079 whole->bd_claiming = holder;
1080 spin_unlock(&bdev_lock);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001081 return 0;
1082}
Christoph Hellwigecbe6bc2020-07-16 16:33:09 +02001083EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
Tejun Heo6b4517a2010-04-07 18:53:59 +09001084
Jan Kara560e7cb2018-02-26 13:01:42 +01001085static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1086{
1087 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1088
1089 if (!disk)
1090 return NULL;
1091 /*
1092 * Now that we hold gendisk reference we make sure bdev we looked up is
1093 * not stale. If it is, it means device got removed and created before
1094 * we looked up gendisk and we fail open in such case. Associating
1095 * unhashed bdev with newly created gendisk could lead to two bdevs
1096 * (and thus two independent caches) being associated with one device
1097 * which is bad.
1098 */
1099 if (inode_unhashed(bdev->bd_inode)) {
1100 put_disk_and_module(disk);
1101 return NULL;
1102 }
1103 return disk;
1104}
1105
Jan Kara89e524c02019-07-30 13:10:14 +02001106static void bd_clear_claiming(struct block_device *whole, void *holder)
1107{
1108 lockdep_assert_held(&bdev_lock);
1109 /* tell others that we're done */
1110 BUG_ON(whole->bd_claiming != holder);
1111 whole->bd_claiming = NULL;
1112 wake_up_bit(&whole->bd_claiming, 0);
1113}
1114
1115/**
1116 * bd_finish_claiming - finish claiming of a block device
1117 * @bdev: block device of interest
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001118 * @whole: whole block device
Jan Kara89e524c02019-07-30 13:10:14 +02001119 * @holder: holder that has claimed @bdev
1120 *
1121 * Finish exclusive open of a block device. Mark the device as exlusively
1122 * open by the holder and wake up all waiters for exclusive open to finish.
1123 */
Christoph Hellwig764b23b2020-06-20 09:16:36 +02001124static void bd_finish_claiming(struct block_device *bdev,
1125 struct block_device *whole, void *holder)
Jan Kara89e524c02019-07-30 13:10:14 +02001126{
1127 spin_lock(&bdev_lock);
1128 BUG_ON(!bd_may_claim(bdev, whole, holder));
1129 /*
1130 * Note that for a whole device bd_holders will be incremented twice,
1131 * and bd_holder will be set to bd_may_claim before being set to holder
1132 */
1133 whole->bd_holders++;
1134 whole->bd_holder = bd_may_claim;
1135 bdev->bd_holders++;
1136 bdev->bd_holder = holder;
1137 bd_clear_claiming(whole, holder);
1138 spin_unlock(&bdev_lock);
1139}
Jan Kara89e524c02019-07-30 13:10:14 +02001140
1141/**
1142 * bd_abort_claiming - abort claiming of a block device
1143 * @bdev: block device of interest
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001144 * @whole: whole block device
Jan Kara89e524c02019-07-30 13:10:14 +02001145 * @holder: holder that has claimed @bdev
1146 *
1147 * Abort claiming of a block device when the exclusive open failed. This can be
1148 * also used when exclusive open is not actually desired and we just needed
1149 * to block other exclusive openers for a while.
1150 */
1151void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1152 void *holder)
1153{
1154 spin_lock(&bdev_lock);
1155 bd_clear_claiming(whole, holder);
1156 spin_unlock(&bdev_lock);
1157}
1158EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001159
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001160#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001161struct bd_holder_disk {
1162 struct list_head list;
1163 struct gendisk *disk;
1164 int refcnt;
1165};
1166
1167static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1168 struct gendisk *disk)
1169{
1170 struct bd_holder_disk *holder;
1171
1172 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1173 if (holder->disk == disk)
1174 return holder;
1175 return NULL;
1176}
1177
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001178static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001179{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001180 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001181}
1182
1183static void del_symlink(struct kobject *from, struct kobject *to)
1184{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001185 sysfs_remove_link(from, kobject_name(to));
1186}
1187
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001188/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001189 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1190 * @bdev: the claimed slave bdev
1191 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001192 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001193 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1194 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001195 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001196 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001197 * - from "slaves" directory of the holder @disk to the claimed @bdev
1198 * - from "holders" directory of the @bdev to the holder @disk
1199 *
1200 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1201 * passed to bd_link_disk_holder(), then:
1202 *
1203 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1204 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1205 *
1206 * The caller must have claimed @bdev before calling this function and
1207 * ensure that both @bdev and @disk are valid during the creation and
1208 * lifetime of these symlinks.
1209 *
1210 * CONTEXT:
1211 * Might sleep.
1212 *
1213 * RETURNS:
1214 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001215 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001216int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001217{
Tejun Heo49731ba2011-01-14 18:43:57 +01001218 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001219 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001220
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001221 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001222
Tejun Heo49731ba2011-01-14 18:43:57 +01001223 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001224
Tejun Heoe09b4572010-11-13 11:55:17 +01001225 /* FIXME: remove the following once add_disk() handles errors */
1226 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1227 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001228
Tejun Heo49731ba2011-01-14 18:43:57 +01001229 holder = bd_find_holder_disk(bdev, disk);
1230 if (holder) {
1231 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001232 goto out_unlock;
1233 }
1234
Tejun Heo49731ba2011-01-14 18:43:57 +01001235 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1236 if (!holder) {
1237 ret = -ENOMEM;
1238 goto out_unlock;
1239 }
1240
1241 INIT_LIST_HEAD(&holder->list);
1242 holder->disk = disk;
1243 holder->refcnt = 1;
1244
1245 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1246 if (ret)
1247 goto out_free;
1248
1249 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1250 if (ret)
1251 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001252 /*
1253 * bdev could be deleted beneath us which would implicitly destroy
1254 * the holder directory. Hold on to it.
1255 */
1256 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001257
1258 list_add(&holder->list, &bdev->bd_holder_disks);
1259 goto out_unlock;
1260
1261out_del:
1262 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1263out_free:
1264 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001265out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001266 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001267 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001268}
Tejun Heoe09b4572010-11-13 11:55:17 +01001269EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001270
Tejun Heo49731ba2011-01-14 18:43:57 +01001271/**
1272 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1273 * @bdev: the calimed slave bdev
1274 * @disk: the holding disk
1275 *
1276 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1277 *
1278 * CONTEXT:
1279 * Might sleep.
1280 */
1281void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001282{
Tejun Heo49731ba2011-01-14 18:43:57 +01001283 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001284
Tejun Heo49731ba2011-01-14 18:43:57 +01001285 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001286
Tejun Heo49731ba2011-01-14 18:43:57 +01001287 holder = bd_find_holder_disk(bdev, disk);
1288
1289 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1290 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1291 del_symlink(bdev->bd_part->holder_dir,
1292 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001293 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001294 list_del_init(&holder->list);
1295 kfree(holder);
1296 }
1297
1298 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001299}
Tejun Heo49731ba2011-01-14 18:43:57 +01001300EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001301#endif
1302
Andrew Patterson0c002c22008-09-04 14:27:20 -06001303/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001304 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001305 * @disk: struct gendisk to check
1306 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001307 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001308 *
1309 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001310 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1311 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001312 */
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001313static void check_disk_size_change(struct gendisk *disk,
1314 struct block_device *bdev, bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001315{
1316 loff_t disk_size, bdev_size;
1317
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +02001318 spin_lock(&bdev->bd_size_lock);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001319 disk_size = (loff_t)get_capacity(disk) << 9;
1320 bdev_size = i_size_read(bdev->bd_inode);
1321 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001322 if (verbose) {
1323 printk(KERN_INFO
1324 "%s: detected capacity change from %lld to %lld\n",
1325 disk->disk_name, bdev_size, disk_size);
1326 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001327 i_size_write(bdev->bd_inode, disk_size);
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +02001328 }
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +02001329 spin_unlock(&bdev->bd_size_lock);
1330
1331 if (bdev_size > disk_size) {
1332 if (__invalidate_device(bdev, false))
Christoph Hellwig9a3ffbb2020-07-08 14:25:43 +02001333 pr_warn("VFS: busy inodes on resized disk %s\n",
1334 disk->disk_name);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001335 }
1336}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001337
1338/**
Christoph Hellwig659e56b2020-09-01 17:57:43 +02001339 * revalidate_disk_size - checks for disk size change and adjusts bdev size.
1340 * @disk: struct gendisk to check
1341 * @verbose: if %true log a message about a size change if there is any
1342 *
1343 * This routine checks to see if the bdev size does not match the disk size
1344 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1345 * are freed.
1346 */
1347void revalidate_disk_size(struct gendisk *disk, bool verbose)
1348{
1349 struct block_device *bdev;
1350
1351 /*
1352 * Hidden disks don't have associated bdev so there's no point in
1353 * revalidating them.
1354 */
1355 if (disk->flags & GENHD_FL_HIDDEN)
1356 return;
1357
1358 bdev = bdget_disk(disk, 0);
1359 if (bdev) {
1360 check_disk_size_change(disk, bdev, verbose);
1361 bdput(bdev);
1362 }
1363}
1364EXPORT_SYMBOL(revalidate_disk_size);
1365
Christoph Hellwig611bee52020-08-23 11:10:41 +02001366void bd_set_nr_sectors(struct block_device *bdev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367{
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +02001368 spin_lock(&bdev->bd_size_lock);
Christoph Hellwig611bee52020-08-23 11:10:41 +02001369 i_size_write(bdev->bd_inode, (loff_t)sectors << SECTOR_SHIFT);
Christoph Hellwigc2b4bb82020-08-23 11:10:42 +02001370 spin_unlock(&bdev->bd_size_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371}
Christoph Hellwig611bee52020-08-23 11:10:41 +02001372EXPORT_SYMBOL(bd_set_nr_sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Al Viro4385bab2013-05-05 22:11:03 -04001374static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001375
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001376int bdev_disk_changed(struct block_device *bdev, bool invalidate)
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001377{
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001378 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001379 int ret;
1380
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001381 lockdep_assert_held(&bdev->bd_mutex);
1382
Christoph Hellwig38430f02020-09-21 09:19:45 +02001383 clear_bit(GD_NEED_PART_SCAN, &bdev->bd_disk->state);
Christoph Hellwig6540fbf2020-09-01 17:57:41 +02001384
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001385rescan:
Christoph Hellwigd46430b2020-04-14 09:28:57 +02001386 ret = blk_drop_partitions(bdev);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001387 if (ret)
1388 return ret;
1389
Christoph Hellwigd981cb52020-03-18 09:12:06 +01001390 /*
1391 * Historically we only set the capacity to zero for devices that
1392 * support partitions (independ of actually having partitions created).
1393 * Doing that is rather inconsistent, but changing it broke legacy
1394 * udisks polling for legacy ide-cdrom devices. Use the crude check
1395 * below to get the sane behavior for most device while not breaking
1396 * userspace for this particular setup.
1397 */
1398 if (invalidate) {
1399 if (disk_part_scan_enabled(disk) ||
1400 !(disk->flags & GENHD_FL_REMOVABLE))
1401 set_capacity(disk, 0);
1402 } else {
1403 if (disk->fops->revalidate_disk)
1404 disk->fops->revalidate_disk(disk);
1405 }
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001406
1407 check_disk_size_change(disk, bdev, !invalidate);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001408
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001409 if (get_capacity(disk)) {
1410 ret = blk_add_partitions(disk, bdev);
1411 if (ret == -EAGAIN)
1412 goto rescan;
Eric Biggers490547c2019-12-02 10:21:34 -08001413 } else if (invalidate) {
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001414 /*
1415 * Tell userspace that the media / partition table may have
1416 * changed.
1417 */
1418 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001419 }
1420
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001421 return ret;
1422}
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001423/*
1424 * Only exported for for loop and dasd for historic reasons. Don't use in new
1425 * code!
1426 */
1427EXPORT_SYMBOL_GPL(bdev_disk_changed);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001428
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001429/*
1430 * bd_mutex locking:
1431 *
1432 * mutex_lock(part->bd_mutex)
1433 * mutex_lock_nested(whole->bd_mutex, 1)
1434 */
1435
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001436static int __blkdev_get(struct block_device *bdev, fmode_t mode, void *holder,
1437 int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438{
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001439 struct block_device *whole = NULL, *claiming = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001441 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001442 int partno;
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001443 bool first_open = false, unblock_events = true, need_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
NeilBrownd3374822009-01-09 08:31:10 +11001445 restart:
Christoph Hellwigc5638ab2020-07-16 16:33:07 +02001446 need_restart = false;
Tejun Heo89f97492008-11-05 10:21:06 +01001447 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001448 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001449 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001452 if (partno) {
1453 whole = bdget_disk(disk, 0);
1454 if (!whole) {
1455 ret = -ENOMEM;
1456 goto out_put_disk;
1457 }
1458 }
1459
1460 if (!for_part && (mode & FMODE_EXCL)) {
1461 WARN_ON_ONCE(!holder);
1462 if (whole)
1463 claiming = whole;
1464 else
1465 claiming = bdev;
1466 ret = bd_prepare_to_claim(bdev, claiming, holder);
1467 if (ret)
1468 goto out_put_whole;
1469 }
1470
Tejun Heo69e02c52011-03-09 19:54:27 +01001471 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001472 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001474 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 bdev->bd_disk = disk;
1476 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001477 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001478
Tejun Heocf771cb2008-09-03 09:01:09 +02001479 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001480 ret = -ENXIO;
1481 bdev->bd_part = disk_get_part(disk, partno);
1482 if (!bdev->bd_part)
1483 goto out_clear;
1484
Tejun Heo1196f8b2011-04-21 20:54:45 +02001485 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001487 ret = disk->fops->open(bdev, mode);
Christoph Hellwigc5638ab2020-07-16 16:33:07 +02001488 /*
1489 * If we lost a race with 'disk' being deleted,
1490 * try again. See md.c
1491 */
1492 if (ret == -ERESTARTSYS)
1493 need_restart = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 }
Tejun Heo7e697232011-05-23 13:26:07 +02001495
Jan Kara04906b22019-01-14 09:48:10 +01001496 if (!ret) {
Christoph Hellwig611bee52020-08-23 11:10:41 +02001497 bd_set_nr_sectors(bdev, get_capacity(disk));
Jan Kara04906b22019-01-14 09:48:10 +01001498 set_init_blocksize(bdev);
1499 }
Tejun Heo7e697232011-05-23 13:26:07 +02001500
Tejun Heo1196f8b2011-04-21 20:54:45 +02001501 /*
1502 * If the device is invalidated, rescan partition
1503 * if open succeeded or failed with -ENOMEDIUM.
1504 * The latter is necessary to prevent ghost
1505 * partitions on a removed medium.
1506 */
Christoph Hellwig38430f02020-09-21 09:19:45 +02001507 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
Jan Kara731dc482019-10-21 10:37:59 +02001508 (!ret || ret == -ENOMEDIUM))
1509 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Dan Williams5a023cd2015-11-30 10:20:29 -08001510
Tejun Heo1196f8b2011-04-21 20:54:45 +02001511 if (ret)
1512 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 } else {
NeilBrown37be4122006-12-08 02:36:16 -08001514 BUG_ON(for_part);
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001515 ret = __blkdev_get(whole, mode, NULL, 1);
1516 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001517 goto out_clear;
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001518 bdev->bd_contains = bdgrab(whole);
Tejun Heo89f97492008-11-05 10:21:06 +01001519 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001520 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001521 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001523 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 }
Christoph Hellwig611bee52020-08-23 11:10:41 +02001525 bd_set_nr_sectors(bdev, bdev->bd_part->nr_sects);
Jan Kara04906b22019-01-14 09:48:10 +01001526 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 }
Jan Kara03e26272017-03-23 01:36:53 +01001528
1529 if (bdev->bd_bdi == &noop_backing_dev_info)
1530 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001533 ret = 0;
1534 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001535 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001536 /* the same as first opener case, read comment there */
Christoph Hellwig38430f02020-09-21 09:19:45 +02001537 if (test_bit(GD_NEED_PART_SCAN, &disk->state) &&
Jan Kara731dc482019-10-21 10:37:59 +02001538 (!ret || ret == -ENOMEDIUM))
1539 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001540 if (ret)
1541 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543 }
1544 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001545 if (for_part)
1546 bdev->bd_part_count++;
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001547 if (claiming)
1548 bd_finish_claiming(bdev, claiming, holder);
1549
1550 /*
1551 * Block event polling for write claims if requested. Any write holder
1552 * makes the write_holder state stick until all are released. This is
1553 * good enough and tracking individual writeable reference is too
1554 * fragile given the way @mode is used in blkdev_get/put().
1555 */
1556 if (claiming && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1557 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1558 bdev->bd_write_holder = true;
1559 unblock_events = false;
1560 }
Arjan van de Venc039e312006-03-23 03:00:28 -08001561 mutex_unlock(&bdev->bd_mutex);
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001562
1563 if (unblock_events)
1564 disk_unblock_events(disk);
1565
Jan Kara89736652018-02-26 13:01:40 +01001566 /* only one opener holds refs to the module and disk */
1567 if (!first_open)
1568 put_disk_and_module(disk);
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001569 if (whole)
1570 bdput(whole);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
1572
Tejun Heo0762b8b2008-08-25 19:56:12 +09001573 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001574 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001576 bdev->bd_part = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001578 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001580 out_unlock_bdev:
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001581 if (claiming)
1582 bd_abort_claiming(bdev, claiming, holder);
Arjan van de Venc039e312006-03-23 03:00:28 -08001583 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001584 disk_unblock_events(disk);
Christoph Hellwig5b642d8b2020-07-16 16:33:10 +02001585 out_put_whole:
1586 if (whole)
1587 bdput(whole);
1588 out_put_disk:
Jan Kara9df6c292018-02-26 13:01:39 +01001589 put_disk_and_module(disk);
Christoph Hellwigc5638ab2020-07-16 16:33:07 +02001590 if (need_restart)
1591 goto restart;
Dan Carpenter4345cab2011-03-19 13:53:31 +01001592 out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return ret;
1594}
1595
Tejun Heod4d77622010-11-13 11:55:18 +01001596/**
1597 * blkdev_get - open a block device
1598 * @bdev: block_device to open
1599 * @mode: FMODE_* mask
1600 * @holder: exclusive holder identifier
1601 *
1602 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1603 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1604 * @holder is invalid. Exclusive opens may nest for the same @holder.
1605 *
1606 * On success, the reference count of @bdev is unchanged. On failure,
1607 * @bdev is put.
1608 *
1609 * CONTEXT:
1610 * Might sleep.
1611 *
1612 * RETURNS:
1613 * 0 on success, -errno on failure.
1614 */
Christoph Hellwig1fb1a2a2020-09-21 09:19:58 +02001615static int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616{
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001617 int ret, perm = 0;
Tejun Heoe525fd82010-11-13 11:55:17 +01001618
Christoph Hellwige5c7fb42020-08-31 20:02:36 +02001619 if (mode & FMODE_READ)
1620 perm |= MAY_READ;
1621 if (mode & FMODE_WRITE)
1622 perm |= MAY_WRITE;
1623 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1624 if (ret)
1625 goto bdput;
1626
1627 ret =__blkdev_get(bdev, mode, holder, 0);
1628 if (ret)
1629 goto bdput;
1630 return 0;
1631
1632bdput:
1633 bdput(bdev);
1634 return ret;
NeilBrown37be4122006-12-08 02:36:16 -08001635}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Tejun Heod4d77622010-11-13 11:55:18 +01001637/**
1638 * blkdev_get_by_path - open a block device by name
1639 * @path: path to the block device to open
1640 * @mode: FMODE_* mask
1641 * @holder: exclusive holder identifier
1642 *
1643 * Open the blockdevice described by the device file at @path. @mode
1644 * and @holder are identical to blkdev_get().
1645 *
1646 * On success, the returned block_device has reference count of one.
1647 *
1648 * CONTEXT:
1649 * Might sleep.
1650 *
1651 * RETURNS:
1652 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1653 */
1654struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1655 void *holder)
1656{
1657 struct block_device *bdev;
1658 int err;
1659
1660 bdev = lookup_bdev(path);
1661 if (IS_ERR(bdev))
1662 return bdev;
1663
1664 err = blkdev_get(bdev, mode, holder);
1665 if (err)
1666 return ERR_PTR(err);
1667
Chuck Ebberte51900f2011-02-16 18:11:53 -05001668 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1669 blkdev_put(bdev, mode);
1670 return ERR_PTR(-EACCES);
1671 }
1672
Tejun Heod4d77622010-11-13 11:55:18 +01001673 return bdev;
1674}
1675EXPORT_SYMBOL(blkdev_get_by_path);
1676
1677/**
1678 * blkdev_get_by_dev - open a block device by device number
1679 * @dev: device number of block device to open
1680 * @mode: FMODE_* mask
1681 * @holder: exclusive holder identifier
1682 *
1683 * Open the blockdevice described by device number @dev. @mode and
1684 * @holder are identical to blkdev_get().
1685 *
1686 * Use it ONLY if you really do not have anything better - i.e. when
1687 * you are behind a truly sucky interface and all you are given is a
1688 * device number. _Never_ to be used for internal purposes. If you
1689 * ever need it - reconsider your API.
1690 *
1691 * On success, the returned block_device has reference count of one.
1692 *
1693 * CONTEXT:
1694 * Might sleep.
1695 *
1696 * RETURNS:
1697 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1698 */
1699struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1700{
1701 struct block_device *bdev;
1702 int err;
1703
1704 bdev = bdget(dev);
1705 if (!bdev)
1706 return ERR_PTR(-ENOMEM);
1707
1708 err = blkdev_get(bdev, mode, holder);
1709 if (err)
1710 return ERR_PTR(err);
1711
1712 return bdev;
1713}
1714EXPORT_SYMBOL(blkdev_get_by_dev);
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716static int blkdev_open(struct inode * inode, struct file * filp)
1717{
1718 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 /*
1721 * Preserve backwards compatibility and allow large file access
1722 * even if userspace doesn't ask for it explicitly. Some mkfs
1723 * binary needs it. We might want to drop this workaround
1724 * during an unstable branch.
1725 */
1726 filp->f_flags |= O_LARGEFILE;
1727
Jens Axboea304f072020-05-22 09:14:08 -06001728 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001729
Al Viro572c4892007-10-08 13:24:05 -04001730 if (filp->f_flags & O_NDELAY)
1731 filp->f_mode |= FMODE_NDELAY;
1732 if (filp->f_flags & O_EXCL)
1733 filp->f_mode |= FMODE_EXCL;
1734 if ((filp->f_flags & O_ACCMODE) == 3)
1735 filp->f_mode |= FMODE_WRITE_IOCTL;
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001738 if (bdev == NULL)
1739 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Al Viro572c4892007-10-08 13:24:05 -04001741 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001742 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Al Viro572c4892007-10-08 13:24:05 -04001743
Tejun Heoe525fd82010-11-13 11:55:17 +01001744 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
Al Viro4385bab2013-05-05 22:11:03 -04001747static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001748{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001749 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001750 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001751
Douglas Andersonb849dd82020-03-24 14:48:27 -07001752 /*
1753 * Sync early if it looks like we're the last one. If someone else
1754 * opens the block device between now and the decrement of bd_openers
1755 * then we did a sync that we didn't need to, but that's not the end
1756 * of the world and we want to avoid long (could be several minute)
1757 * syncs while holding the mutex.
1758 */
1759 if (bdev->bd_openers == 1)
1760 sync_blockdev(bdev);
1761
NeilBrown6796bf52006-12-08 02:36:16 -08001762 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001763 if (for_part)
1764 bdev->bd_part_count--;
1765
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001766 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001767 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001768 sync_blockdev(bdev);
1769 kill_bdev(bdev);
Ilya Dryomov43d1c0e2015-11-20 22:22:34 +01001770
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001771 bdev_write_inode(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001772 }
1773 if (bdev->bd_contains == bdev) {
1774 if (disk->fops->release)
Al Virodb2a1442013-05-05 21:52:57 -04001775 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001776 }
1777 if (!bdev->bd_openers) {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001778 disk_put_part(bdev->bd_part);
1779 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001780 bdev->bd_disk = NULL;
NeilBrown37be4122006-12-08 02:36:16 -08001781 if (bdev != bdev->bd_contains)
1782 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001783 bdev->bd_contains = NULL;
Tejun Heo523e1d32011-10-19 14:31:07 +02001784
Jan Kara9df6c292018-02-26 13:01:39 +01001785 put_disk_and_module(disk);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001786 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001787 mutex_unlock(&bdev->bd_mutex);
1788 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001789 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001790 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001791}
1792
Al Viro4385bab2013-05-05 22:11:03 -04001793void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001794{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001795 mutex_lock(&bdev->bd_mutex);
1796
Tejun Heoe525fd82010-11-13 11:55:17 +01001797 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001798 bool bdev_free;
1799
1800 /*
1801 * Release a claim on the device. The holder fields
1802 * are protected with bdev_lock. bd_mutex is to
1803 * synchronize disk_holder unlinking.
1804 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001805 spin_lock(&bdev_lock);
1806
1807 WARN_ON_ONCE(--bdev->bd_holders < 0);
1808 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1809
1810 /* bd_contains might point to self, check in a separate step */
1811 if ((bdev_free = !bdev->bd_holders))
1812 bdev->bd_holder = NULL;
1813 if (!bdev->bd_contains->bd_holders)
1814 bdev->bd_contains->bd_holder = NULL;
1815
1816 spin_unlock(&bdev_lock);
1817
Tejun Heo77ea8872010-12-08 20:57:37 +01001818 /*
1819 * If this was the last claim, remove holder link and
1820 * unblock evpoll if it was a write holder.
1821 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001822 if (bdev_free && bdev->bd_write_holder) {
1823 disk_unblock_events(bdev->bd_disk);
1824 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001825 }
Tejun Heo69362172011-03-09 19:54:27 +01001826 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001827
Tejun Heo85ef06d2011-07-01 16:17:47 +02001828 /*
1829 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1830 * event. This is to ensure detection of media removal commanded
1831 * from userland - e.g. eject(1).
1832 */
1833 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1834
1835 mutex_unlock(&bdev->bd_mutex);
1836
Al Viro4385bab2013-05-05 22:11:03 -04001837 __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001838}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001839EXPORT_SYMBOL(blkdev_put);
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841static int blkdev_close(struct inode * inode, struct file * filp)
1842{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001843 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001844 blkdev_put(bdev, filp->f_mode);
1845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001848static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001850 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001851 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001852
1853 /*
1854 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1855 * to updated it before every ioctl.
1856 */
Al Viro56b26ad2008-09-19 03:17:36 -04001857 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001858 mode |= FMODE_NDELAY;
1859 else
1860 mode &= ~FMODE_NDELAY;
1861
Al Viro56b26ad2008-09-19 03:17:36 -04001862 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863}
1864
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001865/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001866 * Write data to the block device. Only intended for the block device itself
1867 * and the raw driver which basically is a fake block device.
1868 *
1869 * Does not take i_mutex for the write and thus is not for general purpose
1870 * use.
1871 */
Al Viro1456c0a2014-04-03 03:21:50 -04001872ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02001873{
1874 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001875 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04001876 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001877 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02001878 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04001879
Al Viro7ec7b942015-04-07 11:35:14 -04001880 if (bdev_read_only(I_BDEV(bd_inode)))
1881 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04001882
Christoph Hellwigbb3247a392020-09-21 09:19:55 +02001883 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
Darrick J. Wongdc617f22019-08-20 07:55:16 -07001884 return -ETXTBSY;
1885
Al Viro7ec7b942015-04-07 11:35:14 -04001886 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04001887 return 0;
1888
Al Viro7ec7b942015-04-07 11:35:14 -04001889 if (iocb->ki_pos >= size)
1890 return -ENOSPC;
1891
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001892 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1893 return -EOPNOTSUPP;
1894
Al Viro7ec7b942015-04-07 11:35:14 -04001895 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001896
Jianpeng Ma53362a02012-08-02 09:50:39 +02001897 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04001898 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07001899 if (ret > 0)
1900 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001901 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001902 return ret;
1903}
Al Viro1456c0a2014-04-03 03:21:50 -04001904EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001905
David Jefferyb2de5252014-09-29 10:21:10 -04001906ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001907{
1908 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001909 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001910 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04001911 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001912
1913 if (pos >= size)
1914 return 0;
1915
1916 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04001917 iov_iter_truncate(to, size);
1918 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001919}
David Jefferyb2de5252014-09-29 10:21:10 -04001920EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001921
Christoph Hellwigeef99382009-08-20 17:43:41 +02001922/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001923 * Try to release a page associated with block device when the system
1924 * is under memory pressure.
1925 */
1926static int blkdev_releasepage(struct page *page, gfp_t wait)
1927{
1928 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1929
1930 if (super && super->s_op->bdev_try_to_free_page)
1931 return super->s_op->bdev_try_to_free_page(super, page, wait);
1932
1933 return try_to_free_buffers(page);
1934}
1935
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001936static int blkdev_writepages(struct address_space *mapping,
1937 struct writeback_control *wbc)
1938{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001939 return generic_writepages(mapping, wbc);
1940}
1941
Adrian Bunk4c54ac62008-02-18 13:48:31 +01001942static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 .readpage = blkdev_readpage,
Matthew Wilcox (Oracle)d4388342020-06-01 21:47:02 -07001944 .readahead = blkdev_readahead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07001946 .write_begin = blkdev_write_begin,
1947 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001948 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001949 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08001951 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07001952 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953};
1954
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001955#define BLKDEV_FALLOC_FL_SUPPORTED \
1956 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1957 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1958
1959static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1960 loff_t len)
1961{
1962 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001963 loff_t end = start + len - 1;
1964 loff_t isize;
1965 int error;
1966
1967 /* Fail if we don't recognize the flags. */
1968 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1969 return -EOPNOTSUPP;
1970
1971 /* Don't go off the end of the device. */
1972 isize = i_size_read(bdev->bd_inode);
1973 if (start >= isize)
1974 return -EINVAL;
1975 if (end >= isize) {
1976 if (mode & FALLOC_FL_KEEP_SIZE) {
1977 len = isize - start;
1978 end = start + len - 1;
1979 } else
1980 return -EINVAL;
1981 }
1982
1983 /*
1984 * Don't allow IO that isn't aligned to logical block size.
1985 */
1986 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1987 return -EINVAL;
1988
1989 /* Invalidate the page cache, including dirty pages. */
Jan Kara384d87e2020-09-04 10:58:52 +02001990 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1991 if (error)
1992 return error;
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001993
1994 switch (mode) {
1995 case FALLOC_FL_ZERO_RANGE:
1996 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1997 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02001998 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07001999 break;
2000 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02002001 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2002 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002003 break;
2004 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002005 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2006 GFP_KERNEL, 0);
2007 break;
2008 default:
2009 return -EOPNOTSUPP;
2010 }
2011 if (error)
2012 return error;
2013
2014 /*
2015 * Invalidate again; if someone wandered in and dirtied a page,
2016 * the caller will be given -EBUSY. The third argument is
2017 * inclusive, so the rounding here is safe.
2018 */
Jan Kara384d87e2020-09-04 10:58:52 +02002019 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002020 start >> PAGE_SHIFT,
2021 end >> PAGE_SHIFT);
2022}
2023
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002024const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 .open = blkdev_open,
2026 .release = blkdev_close,
2027 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04002028 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04002029 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07002030 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07002031 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07002032 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07002033 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034#ifdef CONFIG_COMPAT
2035 .compat_ioctl = compat_blkdev_ioctl,
2036#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08002037 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002038 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002039 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040};
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042/**
2043 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08002044 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02002046 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 * namespace if possible and return it. Return ERR_PTR(error)
2048 * otherwise.
2049 */
Al Viro421748e2008-08-02 01:04:36 -04002050struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051{
2052 struct block_device *bdev;
2053 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04002054 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 int error;
2056
Al Viro421748e2008-08-02 01:04:36 -04002057 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return ERR_PTR(-EINVAL);
2059
Al Viro421748e2008-08-02 01:04:36 -04002060 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (error)
2062 return ERR_PTR(error);
2063
David Howellsbb6687342015-03-17 22:26:21 +00002064 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 error = -ENOTBLK;
2066 if (!S_ISBLK(inode->i_mode))
2067 goto fail;
2068 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002069 if (!may_open_dev(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 goto fail;
2071 error = -ENOMEM;
2072 bdev = bd_acquire(inode);
2073 if (!bdev)
2074 goto fail;
2075out:
Al Viro421748e2008-08-02 01:04:36 -04002076 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return bdev;
2078fail:
2079 bdev = ERR_PTR(error);
2080 goto out;
2081}
Al Virod5686b42008-08-01 05:00:11 -04002082EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083
NeilBrown93b270f2011-02-24 17:25:47 +11002084int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01002085{
2086 struct super_block *sb = get_super(bdev);
2087 int res = 0;
2088
2089 if (sb) {
2090 /*
2091 * no need to lock the super, get_super holds the
2092 * read mutex so the filesystem cannot go away
2093 * under us (->put_super runs with the write lock
2094 * hold).
2095 */
2096 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11002097 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01002098 drop_super(sb);
2099 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002100 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01002101 return res;
2102}
2103EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002104
2105void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2106{
2107 struct inode *inode, *old_inode = NULL;
2108
Dave Chinner74278da2015-03-04 12:37:22 -05002109 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002110 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2111 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01002112 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02002113
2114 spin_lock(&inode->i_lock);
2115 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2116 mapping->nrpages == 0) {
2117 spin_unlock(&inode->i_lock);
2118 continue;
2119 }
2120 __iget(inode);
2121 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05002122 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002123 /*
2124 * We hold a reference to 'inode' so it couldn't have been
2125 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05002126 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02002127 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05002128 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02002129 * later.
2130 */
2131 iput(old_inode);
2132 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01002133 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002134
Rabin Vincentaf309222016-12-01 09:18:28 +01002135 mutex_lock(&bdev->bd_mutex);
2136 if (bdev->bd_openers)
2137 func(bdev, arg);
2138 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002139
Dave Chinner74278da2015-03-04 12:37:22 -05002140 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002141 }
Dave Chinner74278da2015-03-04 12:37:22 -05002142 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002143 iput(old_inode);
2144}