blob: ec10dacd18d08b3f6ec864d6cbd6bf9054535ae1 [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>
Dan Williamsb0686262017-01-26 20:37:35 -080022#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040024#include <linux/swap.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010025#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010026#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mpage.h>
28#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000029#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/uio.h>
31#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070032#include <linux/log2.h>
Al Viroff01bb42011-09-16 02:31:11 -040033#include <linux/cleancache.h>
Christoph Hellwig189ce2b2016-10-31 11:59:25 -060034#include <linux/task_io_accounting_ops.h>
Darrick J. Wong25f4c412016-10-11 13:51:11 -070035#include <linux/falloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.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.. */
Al Viroff01bb42011-09-16 02:31:11 -040078void 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Al Viroff01bb42011-09-16 02:31:11 -040088EXPORT_SYMBOL(kill_bdev);
89
90/* Invalidate clean unused buffers and pagecache. */
91void invalidate_bdev(struct block_device *bdev)
92{
93 struct address_space *mapping = bdev->bd_inode->i_mapping;
94
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070095 if (mapping->nrpages) {
96 invalidate_bh_lrus();
97 lru_add_drain_all(); /* make sure all lru add caches are flushed */
98 invalidate_mapping_pages(mapping, 0, -1);
99 }
Al Viroff01bb42011-09-16 02:31:11 -0400100 /* 99% of the time, we don't need to flush the cleancache on the bdev.
101 * But, for the strange corners, lets be cautious
102 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400103 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400104}
105EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jan Kara04906b22019-01-14 09:48:10 +0100107static void set_init_blocksize(struct block_device *bdev)
108{
109 unsigned bsize = bdev_logical_block_size(bdev);
110 loff_t size = i_size_read(bdev->bd_inode);
111
112 while (bsize < PAGE_SIZE) {
113 if (size & bsize)
114 break;
115 bsize <<= 1;
116 }
117 bdev->bd_block_size = bsize;
118 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int set_blocksize(struct block_device *bdev, int size)
122{
123 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700124 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
126
127 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400128 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -EINVAL;
130
131 /* Don't change the size if it is same as current */
132 if (bdev->bd_block_size != size) {
133 sync_blockdev(bdev);
134 bdev->bd_block_size = size;
135 bdev->bd_inode->i_blkbits = blksize_bits(size);
136 kill_bdev(bdev);
137 }
138 return 0;
139}
140
141EXPORT_SYMBOL(set_blocksize);
142
143int sb_set_blocksize(struct super_block *sb, int size)
144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (set_blocksize(sb->s_bdev, size))
146 return 0;
147 /* If we get here, we know size is power of two
148 * and it's value is between 512 and PAGE_SIZE */
149 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800150 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return sb->s_blocksize;
152}
153
154EXPORT_SYMBOL(sb_set_blocksize);
155
156int sb_min_blocksize(struct super_block *sb, int size)
157{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400158 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (size < minsize)
160 size = minsize;
161 return sb_set_blocksize(sb, size);
162}
163
164EXPORT_SYMBOL(sb_min_blocksize);
165
166static int
167blkdev_get_block(struct inode *inode, sector_t iblock,
168 struct buffer_head *bh, int create)
169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bh->b_bdev = I_BDEV(inode);
171 bh->b_blocknr = iblock;
172 set_buffer_mapped(bh);
173 return 0;
174}
175
Dan Williams4ebb16c2015-10-28 07:48:19 +0900176static struct inode *bdev_file_inode(struct file *file)
177{
178 return file->f_mapping->host;
179}
180
Jens Axboe78250c02016-11-17 17:50:47 +0100181static unsigned int dio_bio_write_op(struct kiocb *iocb)
182{
183 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
184
185 /* avoid the need for a I/O completion work item */
186 if (iocb->ki_flags & IOCB_DSYNC)
187 op |= REQ_FUA;
188 return op;
189}
190
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600191#define DIO_INLINE_BIO_VECS 4
192
193static void blkdev_bio_end_io_simple(struct bio *bio)
194{
195 struct task_struct *waiter = bio->bi_private;
196
197 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700198 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600199}
200
201static ssize_t
202__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
203 int nr_pages)
204{
205 struct file *file = iocb->ki_filp;
206 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200207 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600208 loff_t pos = iocb->ki_pos;
209 bool should_dirty = false;
210 struct bio bio;
211 ssize_t ret;
212 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600213
Jens Axboe9a794fb2016-11-22 08:12:39 -0700214 if ((pos | iov_iter_alignment(iter)) &
215 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600216 return -EINVAL;
217
Jens Axboe72ecad22016-11-16 23:11:42 -0700218 if (nr_pages <= DIO_INLINE_BIO_VECS)
219 vecs = inline_vecs;
220 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700221 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
222 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700223 if (!vecs)
224 return -ENOMEM;
225 }
226
Ming Lei3a83f462016-11-22 08:57:21 -0700227 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200228 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900229 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600230 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600231 bio.bi_private = current;
232 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700233 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600234
235 ret = bio_iov_iter_get_pages(&bio, iter);
236 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200237 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600238 ret = bio.bi_iter.bi_size;
239
240 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100241 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600242 if (iter_is_iovec(iter))
243 should_dirty = true;
244 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100245 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600246 task_io_account_write(ret);
247 }
Jens Axboed1e36282018-08-29 10:36:56 -0600248 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700249 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600250
251 qc = submit_bio(&bio);
252 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800253 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600254 if (!READ_ONCE(bio.bi_private))
255 break;
256 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700257 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600258 io_schedule();
259 }
260 __set_current_state(TASK_RUNNING);
261
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200262 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200263 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700264 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600265
Martin Wilck9362dd12018-07-25 23:15:08 +0200266out:
267 if (vecs != inline_vecs)
268 kfree(vecs);
269
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600270 bio_uninit(&bio);
271
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600272 return ret;
273}
274
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700275struct blkdev_dio {
276 union {
277 struct kiocb *iocb;
278 struct task_struct *waiter;
279 };
280 size_t size;
281 atomic_t ref;
282 bool multi_bio : 1;
283 bool should_dirty : 1;
284 bool is_sync : 1;
285 struct bio bio;
286};
287
Kent Overstreet52190f82018-05-20 18:25:55 -0400288static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700289
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700290static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
291{
292 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
293 struct request_queue *q = bdev_get_queue(bdev);
294
295 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
296}
297
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700298static void blkdev_bio_end_io(struct bio *bio)
299{
300 struct blkdev_dio *dio = bio->bi_private;
301 bool should_dirty = dio->should_dirty;
302
Jason Yana89afe52019-04-12 10:09:16 +0800303 if (bio->bi_status && !dio->bio.bi_status)
304 dio->bio.bi_status = bio->bi_status;
305
306 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700307 if (!dio->is_sync) {
308 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200309 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700310
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200311 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700312 ret = dio->size;
313 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200314 } else {
315 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700316 }
317
318 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100319 if (dio->multi_bio)
320 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700321 } else {
322 struct task_struct *waiter = dio->waiter;
323
324 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700325 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700326 }
327 }
328
329 if (should_dirty) {
330 bio_check_pages_dirty(bio);
331 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200332 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700333 bio_put(bio);
334 }
335}
336
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800337static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700338__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800339{
340 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900341 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700342 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100343 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700344 struct blkdev_dio *dio;
345 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700346 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100347 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700348 loff_t pos = iocb->ki_pos;
349 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe7b6620d2019-08-15 11:09:16 -0600350 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700351
Jens Axboe9a794fb2016-11-22 08:12:39 -0700352 if ((pos | iov_iter_alignment(iter)) &
353 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700354 return -EINVAL;
355
Jens Axboe7b6620d2019-08-15 11:09:16 -0600356 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700357
358 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100359 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100360 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700361 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100362 bio_get(bio);
363 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700364 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100365 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700366
367 dio->size = 0;
368 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100369 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700370
Jens Axboecb700eb2018-11-15 19:56:53 -0700371 /*
372 * Don't plug for HIPRI/polled IO, as those should go straight
373 * to issue
374 */
375 if (!is_poll)
376 blk_start_plug(&plug);
377
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700378 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200379 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900380 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600381 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700382 bio->bi_private = dio;
383 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700384 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700385
Jens Axboee15c2ff2019-08-06 13:34:31 -0600386 ret = bio_iov_iter_get_pages(bio, iter);
387 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200388 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700389 bio_endio(bio);
390 break;
391 }
392
393 if (is_read) {
394 bio->bi_opf = REQ_OP_READ;
395 if (dio->should_dirty)
396 bio_set_pages_dirty(bio);
397 } else {
398 bio->bi_opf = dio_bio_write_op(iocb);
399 task_io_account_write(bio->bi_iter.bi_size);
400 }
401
Jens Axboe7b6620d2019-08-15 11:09:16 -0600402 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700403 pos += bio->bi_iter.bi_size;
404
405 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
406 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700407 bool polled = false;
408
409 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700410 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700411 polled = true;
412 }
Jens Axboed34513d2018-11-06 14:29:11 -0700413
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700414 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700415
416 if (polled)
417 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700418 break;
419 }
420
421 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100422 /*
423 * AIO needs an extra reference to ensure the dio
424 * structure which is embedded into the first bio
425 * stays around.
426 */
427 if (!is_sync)
428 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700429 dio->multi_bio = true;
430 atomic_set(&dio->ref, 2);
431 } else {
432 atomic_inc(&dio->ref);
433 }
434
Jens Axboe7b6620d2019-08-15 11:09:16 -0600435 submit_bio(bio);
436 bio = bio_alloc(GFP_KERNEL, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700437 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700438
439 if (!is_poll)
440 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700441
Christoph Hellwig690e5322017-01-24 14:50:19 +0100442 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700443 return -EIOCBQUEUED;
444
445 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800446 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700447 if (!READ_ONCE(dio->waiter))
448 break;
449
450 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700451 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700452 io_schedule();
453 }
454 __set_current_state(TASK_RUNNING);
455
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200456 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200457 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600458 if (likely(!ret))
459 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700460
461 bio_put(&dio->bio);
462 return ret;
463}
464
465static ssize_t
466blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
467{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600468 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800469
Jens Axboe72ecad22016-11-16 23:11:42 -0700470 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600471 if (!nr_pages)
472 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700473 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600474 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700475
476 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800477}
478
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700479static __init int blkdev_init(void)
480{
Kent Overstreet52190f82018-05-20 18:25:55 -0400481 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700482}
483module_init(blkdev_init);
484
Jan Kara5cee5812009-04-27 16:43:51 +0200485int __sync_blockdev(struct block_device *bdev, int wait)
486{
487 if (!bdev)
488 return 0;
489 if (!wait)
490 return filemap_flush(bdev->bd_inode->i_mapping);
491 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
492}
493
Nick Piggin585d3bc2009-02-25 10:44:19 +0100494/*
495 * Write out and wait upon all the dirty data associated with a block
496 * device via its mapping. Does not take the superblock lock.
497 */
498int sync_blockdev(struct block_device *bdev)
499{
Jan Kara5cee5812009-04-27 16:43:51 +0200500 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100501}
502EXPORT_SYMBOL(sync_blockdev);
503
504/*
505 * Write out and wait upon all dirty data associated with this
506 * device. Filesystem data as well as the underlying block
507 * device. Takes the superblock lock.
508 */
509int fsync_bdev(struct block_device *bdev)
510{
511 struct super_block *sb = get_super(bdev);
512 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200513 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100514 drop_super(sb);
515 return res;
516 }
517 return sync_blockdev(bdev);
518}
Al Viro47e44912009-04-01 07:07:16 -0400519EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100520
521/**
522 * freeze_bdev -- lock a filesystem and force it into a consistent state
523 * @bdev: blockdevice to lock
524 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100525 * If a superblock is found on this device, we take the s_umount semaphore
526 * on it to make sure nobody unmounts until the snapshot creation is done.
527 * The reference counter (bd_fsfreeze_count) guarantees that only the last
528 * unfreeze process can unfreeze the frozen filesystem actually when multiple
529 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
530 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
531 * actually.
532 */
533struct super_block *freeze_bdev(struct block_device *bdev)
534{
535 struct super_block *sb;
536 int error = 0;
537
538 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200539 if (++bdev->bd_fsfreeze_count > 1) {
540 /*
541 * We don't even need to grab a reference - the first call
542 * to freeze_bdev grab an active reference and only the last
543 * thaw_bdev drops it.
544 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100545 sb = get_super(bdev);
Andrey Ryabinin5bb53c02016-08-23 18:55:31 +0300546 if (sb)
547 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100548 mutex_unlock(&bdev->bd_fsfreeze_mutex);
549 return sb;
550 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100551
Christoph Hellwig45042302009-08-03 23:28:35 +0200552 sb = get_active_super(bdev);
553 if (!sb)
554 goto out;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600555 if (sb->s_op->freeze_super)
556 error = sb->s_op->freeze_super(sb);
557 else
558 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400559 if (error) {
560 deactivate_super(sb);
561 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200562 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400563 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100564 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400565 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200566 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100567 sync_blockdev(bdev);
568 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200569 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100570}
571EXPORT_SYMBOL(freeze_bdev);
572
573/**
574 * thaw_bdev -- unlock filesystem
575 * @bdev: blockdevice to unlock
576 * @sb: associated superblock
577 *
578 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
579 */
580int thaw_bdev(struct block_device *bdev, struct super_block *sb)
581{
Christoph Hellwig45042302009-08-03 23:28:35 +0200582 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100583
584 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200585 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400586 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100587
Christoph Hellwig45042302009-08-03 23:28:35 +0200588 error = 0;
589 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400590 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100591
Christoph Hellwig45042302009-08-03 23:28:35 +0200592 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400593 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200594
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600595 if (sb->s_op->thaw_super)
596 error = sb->s_op->thaw_super(sb);
597 else
598 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200599 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400600 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400601out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100602 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200603 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100604}
605EXPORT_SYMBOL(thaw_bdev);
606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
608{
609 return block_write_full_page(page, blkdev_get_block, wbc);
610}
611
612static int blkdev_readpage(struct file * file, struct page * page)
613{
614 return block_read_full_page(page, blkdev_get_block);
615}
616
Akinobu Mita447f05b2014-10-09 15:26:58 -0700617static int blkdev_readpages(struct file *file, struct address_space *mapping,
618 struct list_head *pages, unsigned nr_pages)
619{
620 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
621}
622
Nick Piggin6272b5a2007-10-16 01:25:04 -0700623static int blkdev_write_begin(struct file *file, struct address_space *mapping,
624 loff_t pos, unsigned len, unsigned flags,
625 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200627 return block_write_begin(mapping, pos, len, flags, pagep,
628 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629}
630
Nick Piggin6272b5a2007-10-16 01:25:04 -0700631static int blkdev_write_end(struct file *file, struct address_space *mapping,
632 loff_t pos, unsigned len, unsigned copied,
633 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700635 int ret;
636 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
637
638 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300639 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700640
641 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
644/*
645 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500646 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 * so we compute the size by hand (just as in block_read/write above)
648 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800649static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900651 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 loff_t retval;
653
Al Viro59551022016-01-22 15:40:57 -0500654 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400655 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500656 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return retval;
658}
659
Josef Bacik02c24a82011-07-16 20:44:56 -0400660int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900662 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400663 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100664 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200665
Jeff Layton372cf242017-07-06 07:02:28 -0400666 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200667 if (error)
668 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100669
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400670 /*
671 * There is no need to serialise calls to blkdev_issue_flush with
672 * i_mutex and doing so causes performance issues with concurrent
673 * O_SYNC writers to a block device.
674 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200675 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100676 if (error == -EOPNOTSUPP)
677 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400678
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100679 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700681EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700683/**
684 * bdev_read_page() - Start reading a page from a block device
685 * @bdev: The device to read the page from
686 * @sector: The offset on the device to read the page to (need not be aligned)
687 * @page: The page to read
688 *
689 * On entry, the page should be locked. It will be unlocked when the page
690 * has been read. If the block driver implements rw_page synchronously,
691 * that will be true on exit from this function, but it need not be.
692 *
693 * Errors returned by this function are usually "soft", eg out of memory, or
694 * queue full; callers should try a different route to read this page rather
695 * than propagate an error back up the stack.
696 *
697 * Return: negative errno if an error occurs, 0 if submission was successful.
698 */
699int bdev_read_page(struct block_device *bdev, sector_t sector,
700 struct page *page)
701{
702 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800703 int result = -EOPNOTSUPP;
704
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400705 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800706 return result;
707
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800708 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800709 if (result)
710 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700711 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
712 REQ_OP_READ);
Dan Williams2e6edc952015-11-19 13:29:28 -0800713 blk_queue_exit(bdev->bd_queue);
714 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700715}
716EXPORT_SYMBOL_GPL(bdev_read_page);
717
718/**
719 * bdev_write_page() - Start writing a page to a block device
720 * @bdev: The device to write the page to
721 * @sector: The offset on the device to write the page to (need not be aligned)
722 * @page: The page to write
723 * @wbc: The writeback_control for the write
724 *
725 * On entry, the page should be locked and not currently under writeback.
726 * On exit, if the write started successfully, the page will be unlocked and
727 * under writeback. If the write failed already (eg the driver failed to
728 * queue the page to the device), the page will still be locked. If the
729 * caller is a ->writepage implementation, it will need to unlock the page.
730 *
731 * Errors returned by this function are usually "soft", eg out of memory, or
732 * queue full; callers should try a different route to write this page rather
733 * than propagate an error back up the stack.
734 *
735 * Return: negative errno if an error occurs, 0 if submission was successful.
736 */
737int bdev_write_page(struct block_device *bdev, sector_t sector,
738 struct page *page, struct writeback_control *wbc)
739{
740 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700741 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800742
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400743 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700744 return -EOPNOTSUPP;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800745 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800746 if (result)
747 return result;
748
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700749 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700750 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
751 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700752 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700753 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700754 } else {
755 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700756 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700757 }
Dan Williams2e6edc952015-11-19 13:29:28 -0800758 blk_queue_exit(bdev->bd_queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700759 return result;
760}
761EXPORT_SYMBOL_GPL(bdev_write_page);
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763/*
764 * pseudo-fs
765 */
766
767static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800768static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static struct inode *bdev_alloc_inode(struct super_block *sb)
771{
Christoph Lametere94b1762006-12-06 20:33:17 -0800772 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 if (!ei)
774 return NULL;
775 return &ei->vfs_inode;
776}
777
Al Viro41149cb2019-04-10 15:12:38 -0400778static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Al Viro41149cb2019-04-10 15:12:38 -0400780 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100781}
782
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700783static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
785 struct bdev_inode *ei = (struct bdev_inode *) foo;
786 struct block_device *bdev = &ei->bdev;
787
Christoph Lametera35afb82007-05-16 22:10:57 -0700788 memset(bdev, 0, sizeof(*bdev));
789 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700790 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100791#ifdef CONFIG_SYSFS
792 INIT_LIST_HEAD(&bdev->bd_holder_disks);
793#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100794 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700795 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800796 /* Initialize mutex for freeze. */
797 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
799
Al Virob57922d2010-06-07 14:34:48 -0400800static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
802 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700803 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400804 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200805 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 spin_lock(&bdev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 list_del_init(&bdev->bd_list);
808 spin_unlock(&bdev_lock);
Jan Karaf7597412017-03-23 01:37:00 +0100809 /* Detach inode from wb early as bdi_put() may free bdi->wb */
810 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100811 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100812 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100813 bdev->bd_bdi = &noop_backing_dev_info;
814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
816
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800817static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 .statfs = simple_statfs,
819 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400820 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400822 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823};
824
David Howells9030d162019-03-25 16:38:23 +0000825static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
David Howells9030d162019-03-25 16:38:23 +0000827 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
828 if (!ctx)
829 return -ENOMEM;
830 fc->s_iflags |= SB_I_CGROUPWB;
831 ctx->ops = &bdev_sops;
832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
835static struct file_system_type bd_type = {
836 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000837 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 .kill_sb = kill_anon_super,
839};
840
Tejun Heoa212b102015-05-22 17:13:33 -0400841struct super_block *blockdev_superblock __read_mostly;
842EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844void __init bdev_cache_init(void)
845{
846 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300847 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800850 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800851 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900852 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 err = register_filesystem(&bd_type);
854 if (err)
855 panic("Cannot register bdev pseudo-fs");
856 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (IS_ERR(bd_mnt))
858 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300859 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860}
861
862/*
863 * Most likely _very_ bad one - but then it's hardly critical for small
864 * /dev and can be fixed when somebody will need really large one.
865 * Keep in mind that it will be fed through icache hash function too.
866 */
867static inline unsigned long hash(dev_t dev)
868{
869 return MAJOR(dev)+MINOR(dev);
870}
871
872static int bdev_test(struct inode *inode, void *data)
873{
874 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
875}
876
877static int bdev_set(struct inode *inode, void *data)
878{
879 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
880 return 0;
881}
882
883static LIST_HEAD(all_bdevs);
884
Jan Karaf44f1ab2017-02-02 15:56:49 +0100885/*
886 * If there is a bdev inode for this device, unhash it so that it gets evicted
887 * as soon as last inode reference is dropped.
888 */
889void bdev_unhash_inode(dev_t dev)
890{
891 struct inode *inode;
892
893 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
894 if (inode) {
895 remove_inode_hash(inode);
896 iput(inode);
897 }
898}
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900struct block_device *bdget(dev_t dev)
901{
902 struct block_device *bdev;
903 struct inode *inode;
904
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800905 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 bdev_test, bdev_set, &dev);
907
908 if (!inode)
909 return NULL;
910
911 bdev = &BDEV_I(inode)->bdev;
912
913 if (inode->i_state & I_NEW) {
914 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000915 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 bdev->bd_inode = inode;
Fabian Frederick93407472017-02-27 14:28:32 -0800917 bdev->bd_block_size = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 bdev->bd_part_count = 0;
919 bdev->bd_invalidated = 0;
920 inode->i_mode = S_IFBLK;
921 inode->i_rdev = dev;
922 inode->i_bdev = bdev;
923 inode->i_data.a_ops = &def_blk_aops;
924 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 spin_lock(&bdev_lock);
926 list_add(&bdev->bd_list, &all_bdevs);
927 spin_unlock(&bdev_lock);
928 unlock_new_inode(inode);
929 }
930 return bdev;
931}
932
933EXPORT_SYMBOL(bdget);
934
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200935/**
936 * bdgrab -- Grab a reference to an already referenced block device
937 * @bdev: Block device to grab a reference to.
938 */
939struct block_device *bdgrab(struct block_device *bdev)
940{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400941 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200942 return bdev;
943}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700944EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200945
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946long nr_blockdev_pages(void)
947{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700948 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 long ret = 0;
950 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700951 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 ret += bdev->bd_inode->i_mapping->nrpages;
953 }
954 spin_unlock(&bdev_lock);
955 return ret;
956}
957
958void bdput(struct block_device *bdev)
959{
960 iput(bdev->bd_inode);
961}
962
963EXPORT_SYMBOL(bdput);
964
965static struct block_device *bd_acquire(struct inode *inode)
966{
967 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 spin_lock(&bdev_lock);
970 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +0100971 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100972 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 spin_unlock(&bdev_lock);
974 return bdev;
975 }
976 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700977
Jan Karacccd9fb2017-02-21 18:09:48 +0100978 /*
979 * i_bdev references block device inode that was already shut down
980 * (corresponding device got removed). Remove the reference and look
981 * up block device inode again just in case new device got
982 * reestablished under the same device number.
983 */
984 if (bdev)
985 bd_forget(inode);
986
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 bdev = bdget(inode->i_rdev);
988 if (bdev) {
989 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700990 if (!inode->i_bdev) {
991 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -0400992 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700993 * and it's released in clear_inode() of inode.
994 * So, we can access it via ->i_mapping always
995 * without igrab().
996 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100997 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700998 inode->i_bdev = bdev;
999 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 spin_unlock(&bdev_lock);
1002 }
1003 return bdev;
1004}
1005
1006/* Call when you free inode */
1007
1008void bd_forget(struct inode *inode)
1009{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001010 struct block_device *bdev = NULL;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001013 if (!sb_is_blkdev_sb(inode->i_sb))
1014 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001015 inode->i_bdev = NULL;
1016 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001018
1019 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001020 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001023/**
1024 * bd_may_claim - test whether a block device can be claimed
1025 * @bdev: block device of interest
1026 * @whole: whole block device containing @bdev, may equal @bdev
1027 * @holder: holder trying to claim @bdev
1028 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001029 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001030 *
1031 * CONTEXT:
1032 * spin_lock(&bdev_lock).
1033 *
1034 * RETURNS:
1035 * %true if @bdev can be claimed, %false otherwise.
1036 */
1037static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1038 void *holder)
1039{
1040 if (bdev->bd_holder == holder)
1041 return true; /* already a holder */
1042 else if (bdev->bd_holder != NULL)
1043 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001044 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001045 return true; /* is a whole device which isn't held */
1046
Tejun Heoe525fd82010-11-13 11:55:17 +01001047 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001048 return true; /* is a partition of a device that is being partitioned */
1049 else if (whole->bd_holder != NULL)
1050 return false; /* is a partition of a held device */
1051 else
1052 return true; /* is a partition of an un-held device */
1053}
1054
1055/**
Tejun Heo6b4517a2010-04-07 18:53:59 +09001056 * bd_prepare_to_claim - prepare to claim a block device
1057 * @bdev: block device of interest
1058 * @whole: the whole device containing @bdev, may equal @bdev
1059 * @holder: holder trying to claim @bdev
1060 *
1061 * Prepare to claim @bdev. This function fails if @bdev is already
1062 * claimed by another holder and waits if another claiming is in
1063 * progress. This function doesn't actually claim. On successful
1064 * return, the caller has ownership of bd_claiming and bd_holder[s].
1065 *
1066 * CONTEXT:
1067 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1068 * it multiple times.
1069 *
1070 * RETURNS:
1071 * 0 if @bdev can be claimed, -EBUSY otherwise.
1072 */
1073static int bd_prepare_to_claim(struct block_device *bdev,
1074 struct block_device *whole, void *holder)
1075{
1076retry:
1077 /* if someone else claimed, fail */
1078 if (!bd_may_claim(bdev, whole, holder))
1079 return -EBUSY;
1080
Tejun Heoe75aa852010-08-04 17:59:39 +02001081 /* if claiming is already in progress, wait for it to finish */
1082 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001083 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1084 DEFINE_WAIT(wait);
1085
1086 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1087 spin_unlock(&bdev_lock);
1088 schedule();
1089 finish_wait(wq, &wait);
1090 spin_lock(&bdev_lock);
1091 goto retry;
1092 }
1093
1094 /* yay, all mine */
1095 return 0;
1096}
1097
Jan Kara560e7cb2018-02-26 13:01:42 +01001098static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1099{
1100 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1101
1102 if (!disk)
1103 return NULL;
1104 /*
1105 * Now that we hold gendisk reference we make sure bdev we looked up is
1106 * not stale. If it is, it means device got removed and created before
1107 * we looked up gendisk and we fail open in such case. Associating
1108 * unhashed bdev with newly created gendisk could lead to two bdevs
1109 * (and thus two independent caches) being associated with one device
1110 * which is bad.
1111 */
1112 if (inode_unhashed(bdev->bd_inode)) {
1113 put_disk_and_module(disk);
1114 return NULL;
1115 }
1116 return disk;
1117}
1118
Tejun Heo6b4517a2010-04-07 18:53:59 +09001119/**
1120 * bd_start_claiming - start claiming a block device
1121 * @bdev: block device of interest
1122 * @holder: holder trying to claim @bdev
1123 *
1124 * @bdev is about to be opened exclusively. Check @bdev can be opened
1125 * exclusively and mark that an exclusive open is in progress. Each
1126 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +10001127 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1128 * fail).
1129 *
1130 * This function is used to gain exclusive access to the block device
1131 * without actually causing other exclusive open attempts to fail. It
1132 * should be used when the open sequence itself requires exclusive
1133 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +09001134 *
1135 * CONTEXT:
1136 * Might sleep.
1137 *
1138 * RETURNS:
1139 * Pointer to the block device containing @bdev on success, ERR_PTR()
1140 * value on failure.
1141 */
Jan Kara89e524c02019-07-30 13:10:14 +02001142struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001143{
1144 struct gendisk *disk;
1145 struct block_device *whole;
1146 int partno, err;
1147
1148 might_sleep();
1149
1150 /*
1151 * @bdev might not have been initialized properly yet, look up
1152 * and grab the outer block device the hard way.
1153 */
Jan Kara560e7cb2018-02-26 13:01:42 +01001154 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001155 if (!disk)
1156 return ERR_PTR(-ENXIO);
1157
Tejun Heod4c208b2011-06-13 12:45:48 +02001158 /*
1159 * Normally, @bdev should equal what's returned from bdget_disk()
1160 * if partno is 0; however, some drivers (floppy) use multiple
1161 * bdev's for the same physical device and @bdev may be one of the
1162 * aliases. Keep @bdev if partno is 0. This means claimer
1163 * tracking is broken for those devices but it has always been that
1164 * way.
1165 */
1166 if (partno)
1167 whole = bdget_disk(disk, 0);
1168 else
1169 whole = bdgrab(bdev);
1170
Jan Kara9df6c292018-02-26 13:01:39 +01001171 put_disk_and_module(disk);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001172 if (!whole)
1173 return ERR_PTR(-ENOMEM);
1174
1175 /* prepare to claim, if successful, mark claiming in progress */
1176 spin_lock(&bdev_lock);
1177
1178 err = bd_prepare_to_claim(bdev, whole, holder);
1179 if (err == 0) {
1180 whole->bd_claiming = holder;
1181 spin_unlock(&bdev_lock);
1182 return whole;
1183 } else {
1184 spin_unlock(&bdev_lock);
1185 bdput(whole);
1186 return ERR_PTR(err);
1187 }
1188}
Jan Kara89e524c02019-07-30 13:10:14 +02001189EXPORT_SYMBOL(bd_start_claiming);
1190
1191static void bd_clear_claiming(struct block_device *whole, void *holder)
1192{
1193 lockdep_assert_held(&bdev_lock);
1194 /* tell others that we're done */
1195 BUG_ON(whole->bd_claiming != holder);
1196 whole->bd_claiming = NULL;
1197 wake_up_bit(&whole->bd_claiming, 0);
1198}
1199
1200/**
1201 * bd_finish_claiming - finish claiming of a block device
1202 * @bdev: block device of interest
1203 * @whole: whole block device (returned from bd_start_claiming())
1204 * @holder: holder that has claimed @bdev
1205 *
1206 * Finish exclusive open of a block device. Mark the device as exlusively
1207 * open by the holder and wake up all waiters for exclusive open to finish.
1208 */
1209void bd_finish_claiming(struct block_device *bdev, struct block_device *whole,
1210 void *holder)
1211{
1212 spin_lock(&bdev_lock);
1213 BUG_ON(!bd_may_claim(bdev, whole, holder));
1214 /*
1215 * Note that for a whole device bd_holders will be incremented twice,
1216 * and bd_holder will be set to bd_may_claim before being set to holder
1217 */
1218 whole->bd_holders++;
1219 whole->bd_holder = bd_may_claim;
1220 bdev->bd_holders++;
1221 bdev->bd_holder = holder;
1222 bd_clear_claiming(whole, holder);
1223 spin_unlock(&bdev_lock);
1224}
1225EXPORT_SYMBOL(bd_finish_claiming);
1226
1227/**
1228 * bd_abort_claiming - abort claiming of a block device
1229 * @bdev: block device of interest
1230 * @whole: whole block device (returned from bd_start_claiming())
1231 * @holder: holder that has claimed @bdev
1232 *
1233 * Abort claiming of a block device when the exclusive open failed. This can be
1234 * also used when exclusive open is not actually desired and we just needed
1235 * to block other exclusive openers for a while.
1236 */
1237void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1238 void *holder)
1239{
1240 spin_lock(&bdev_lock);
1241 bd_clear_claiming(whole, holder);
1242 spin_unlock(&bdev_lock);
1243}
1244EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001245
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001246#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001247struct bd_holder_disk {
1248 struct list_head list;
1249 struct gendisk *disk;
1250 int refcnt;
1251};
1252
1253static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1254 struct gendisk *disk)
1255{
1256 struct bd_holder_disk *holder;
1257
1258 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1259 if (holder->disk == disk)
1260 return holder;
1261 return NULL;
1262}
1263
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001264static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001265{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001266 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001267}
1268
1269static void del_symlink(struct kobject *from, struct kobject *to)
1270{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001271 sysfs_remove_link(from, kobject_name(to));
1272}
1273
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001274/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001275 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1276 * @bdev: the claimed slave bdev
1277 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001278 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001279 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1280 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001281 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001282 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001283 * - from "slaves" directory of the holder @disk to the claimed @bdev
1284 * - from "holders" directory of the @bdev to the holder @disk
1285 *
1286 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1287 * passed to bd_link_disk_holder(), then:
1288 *
1289 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1290 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1291 *
1292 * The caller must have claimed @bdev before calling this function and
1293 * ensure that both @bdev and @disk are valid during the creation and
1294 * lifetime of these symlinks.
1295 *
1296 * CONTEXT:
1297 * Might sleep.
1298 *
1299 * RETURNS:
1300 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001301 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001302int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001303{
Tejun Heo49731ba2011-01-14 18:43:57 +01001304 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001305 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001306
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001307 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001308
Tejun Heo49731ba2011-01-14 18:43:57 +01001309 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001310
Tejun Heoe09b4572010-11-13 11:55:17 +01001311 /* FIXME: remove the following once add_disk() handles errors */
1312 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1313 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001314
Tejun Heo49731ba2011-01-14 18:43:57 +01001315 holder = bd_find_holder_disk(bdev, disk);
1316 if (holder) {
1317 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001318 goto out_unlock;
1319 }
1320
Tejun Heo49731ba2011-01-14 18:43:57 +01001321 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1322 if (!holder) {
1323 ret = -ENOMEM;
1324 goto out_unlock;
1325 }
1326
1327 INIT_LIST_HEAD(&holder->list);
1328 holder->disk = disk;
1329 holder->refcnt = 1;
1330
1331 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1332 if (ret)
1333 goto out_free;
1334
1335 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1336 if (ret)
1337 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001338 /*
1339 * bdev could be deleted beneath us which would implicitly destroy
1340 * the holder directory. Hold on to it.
1341 */
1342 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001343
1344 list_add(&holder->list, &bdev->bd_holder_disks);
1345 goto out_unlock;
1346
1347out_del:
1348 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1349out_free:
1350 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001351out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001352 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001353 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001354}
Tejun Heoe09b4572010-11-13 11:55:17 +01001355EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001356
Tejun Heo49731ba2011-01-14 18:43:57 +01001357/**
1358 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1359 * @bdev: the calimed slave bdev
1360 * @disk: the holding disk
1361 *
1362 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1363 *
1364 * CONTEXT:
1365 * Might sleep.
1366 */
1367void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001368{
Tejun Heo49731ba2011-01-14 18:43:57 +01001369 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001370
Tejun Heo49731ba2011-01-14 18:43:57 +01001371 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001372
Tejun Heo49731ba2011-01-14 18:43:57 +01001373 holder = bd_find_holder_disk(bdev, disk);
1374
1375 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1376 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1377 del_symlink(bdev->bd_part->holder_dir,
1378 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001379 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001380 list_del_init(&holder->list);
1381 kfree(holder);
1382 }
1383
1384 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001385}
Tejun Heo49731ba2011-01-14 18:43:57 +01001386EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001387#endif
1388
Andrew Patterson0c002c22008-09-04 14:27:20 -06001389/**
Andrew Patterson56ade442008-09-04 14:27:40 -06001390 * flush_disk - invalidates all buffer-cache entries on a disk
1391 *
1392 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -08001393 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -06001394 *
1395 * Invalidates all buffer-cache entries on a disk. It should be called
1396 * when a disk has been changed -- either by a media change or online
1397 * resize.
1398 */
NeilBrown93b270f2011-02-24 17:25:47 +11001399static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -06001400{
NeilBrown93b270f2011-02-24 17:25:47 +11001401 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -06001402 printk(KERN_WARNING "VFS: busy inodes on changed media or "
Dmitry Monakhov424081f2015-04-13 16:31:34 +04001403 "resized disk %s\n",
1404 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
Andrew Patterson56ade442008-09-04 14:27:40 -06001405 }
Jan Karacba22d82019-10-21 10:38:00 +02001406 bdev->bd_invalidated = 1;
Andrew Patterson56ade442008-09-04 14:27:40 -06001407}
1408
1409/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001410 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001411 * @disk: struct gendisk to check
1412 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001413 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001414 *
1415 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001416 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1417 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001418 */
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001419static void check_disk_size_change(struct gendisk *disk,
1420 struct block_device *bdev, bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001421{
1422 loff_t disk_size, bdev_size;
1423
1424 disk_size = (loff_t)get_capacity(disk) << 9;
1425 bdev_size = i_size_read(bdev->bd_inode);
1426 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001427 if (verbose) {
1428 printk(KERN_INFO
1429 "%s: detected capacity change from %lld to %lld\n",
1430 disk->disk_name, bdev_size, disk_size);
1431 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001432 i_size_write(bdev->bd_inode, disk_size);
shunki-fujita849cf552018-04-05 16:20:07 -07001433 if (bdev_size > disk_size)
1434 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001435 }
1436}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001437
1438/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001439 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001440 * @disk: struct gendisk to be revalidated
1441 *
1442 * This routine is a wrapper for lower-level driver's revalidate_disk
1443 * call-backs. It is used to do common pre and post operations needed
1444 * for all revalidate_disk operations.
1445 */
1446int revalidate_disk(struct gendisk *disk)
1447{
1448 int ret = 0;
1449
1450 if (disk->fops->revalidate_disk)
1451 ret = disk->fops->revalidate_disk(disk);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001452
Jan Kara31cb1d62019-05-15 08:57:40 +02001453 /*
1454 * Hidden disks don't have associated bdev so there's no point in
1455 * revalidating it.
1456 */
1457 if (!(disk->flags & GENHD_FL_HIDDEN)) {
1458 struct block_device *bdev = bdget_disk(disk, 0);
1459
1460 if (!bdev)
1461 return ret;
1462
1463 mutex_lock(&bdev->bd_mutex);
1464 check_disk_size_change(disk, bdev, ret == 0);
1465 bdev->bd_invalidated = 0;
1466 mutex_unlock(&bdev->bd_mutex);
1467 bdput(bdev);
1468 }
Andrew Patterson0c002c22008-09-04 14:27:20 -06001469 return ret;
1470}
1471EXPORT_SYMBOL(revalidate_disk);
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473/*
1474 * This routine checks whether a removable media has been changed,
1475 * and invalidates all buffer-cache-entries in that case. This
1476 * is a relatively slow routine, so we have to try to minimize using
1477 * it. Thus it is called only upon a 'mount' or 'open'. This
1478 * is the best way of combining speed and utility, I think.
1479 * People changing diskettes in the middle of an operation deserve
1480 * to lose :-)
1481 */
1482int check_disk_change(struct block_device *bdev)
1483{
1484 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001485 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001486 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
Tejun Heo77ea8872010-12-08 20:57:37 +01001488 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1489 DISK_EVENT_EJECT_REQUEST);
1490 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491 return 0;
1492
NeilBrown93b270f2011-02-24 17:25:47 +11001493 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 if (bdops->revalidate_disk)
1495 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 return 1;
1497}
1498
1499EXPORT_SYMBOL(check_disk_change);
1500
1501void bd_set_size(struct block_device *bdev, loff_t size)
1502{
Al Viro59551022016-01-22 15:40:57 -05001503 inode_lock(bdev->bd_inode);
Guo Chaod646a022013-02-21 15:16:42 -08001504 i_size_write(bdev->bd_inode, size);
Al Viro59551022016-01-22 15:40:57 -05001505 inode_unlock(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506}
1507EXPORT_SYMBOL(bd_set_size);
1508
Al Viro4385bab2013-05-05 22:11:03 -04001509static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001510
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001511static int rescan_partitions(struct gendisk *disk, struct block_device *bdev,
1512 bool invalidate)
1513{
1514 int ret;
1515
1516rescan:
1517 ret = blk_drop_partitions(disk, bdev);
1518 if (ret)
1519 return ret;
1520
1521 if (invalidate)
1522 set_capacity(disk, 0);
1523 else if (disk->fops->revalidate_disk)
1524 disk->fops->revalidate_disk(disk);
1525
1526 check_disk_size_change(disk, bdev, !invalidate);
1527 bdev->bd_invalidated = 0;
1528
1529 if (!get_capacity(disk)) {
1530 /*
1531 * Tell userspace that the media / partition table may have
1532 * changed.
1533 */
1534 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
1535 return 0;
1536 }
1537
1538 ret = blk_add_partitions(disk, bdev);
1539 if (ret == -EAGAIN)
1540 goto rescan;
1541 return ret;
1542}
1543
1544
Jan Kara731dc482019-10-21 10:37:59 +02001545static void bdev_disk_changed(struct block_device *bdev, bool invalidate)
1546{
Jan Karacba22d82019-10-21 10:38:00 +02001547 if (disk_part_scan_enabled(bdev->bd_disk)) {
Christoph Hellwig6917d062019-11-14 15:34:33 +01001548 rescan_partitions(bdev->bd_disk, bdev, invalidate);
Jan Karacba22d82019-10-21 10:38:00 +02001549 } else {
1550 check_disk_size_change(bdev->bd_disk, bdev, !invalidate);
1551 bdev->bd_invalidated = 0;
1552 }
Jan Kara731dc482019-10-21 10:37:59 +02001553}
1554
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001555/*
1556 * bd_mutex locking:
1557 *
1558 * mutex_lock(part->bd_mutex)
1559 * mutex_lock_nested(whole->bd_mutex, 1)
1560 */
1561
Al Viro572c4892007-10-08 13:24:05 -04001562static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001565 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001566 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001567 int perm = 0;
Jan Kara89736652018-02-26 13:01:40 +01001568 bool first_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Al Viro572c4892007-10-08 13:24:05 -04001570 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001571 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001572 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001573 perm |= MAY_WRITE;
1574 /*
1575 * hooks: /n/, see "layering violations".
1576 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001577 if (!for_part) {
1578 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1579 if (ret != 0) {
1580 bdput(bdev);
1581 return ret;
1582 }
Al Viro82666022008-08-01 05:32:04 -04001583 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001584
NeilBrownd3374822009-01-09 08:31:10 +11001585 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001586
Tejun Heo89f97492008-11-05 10:21:06 +01001587 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001588 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001589 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001590 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Tejun Heo69e02c52011-03-09 19:54:27 +01001592 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001593 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001595 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 bdev->bd_disk = disk;
Andi Kleen87192a22012-01-12 17:20:34 -08001597 bdev->bd_queue = disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001599 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001600
Tejun Heocf771cb2008-09-03 09:01:09 +02001601 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001602 ret = -ENXIO;
1603 bdev->bd_part = disk_get_part(disk, partno);
1604 if (!bdev->bd_part)
1605 goto out_clear;
1606
Tejun Heo1196f8b2011-04-21 20:54:45 +02001607 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001609 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001610 if (ret == -ERESTARTSYS) {
1611 /* Lost a race with 'disk' being
1612 * deleted, try again.
1613 * See md.c
1614 */
1615 disk_put_part(bdev->bd_part);
1616 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001617 bdev->bd_disk = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001618 bdev->bd_queue = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001619 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001620 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001621 put_disk_and_module(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001622 goto restart;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Tejun Heo7e697232011-05-23 13:26:07 +02001625
Jan Kara04906b22019-01-14 09:48:10 +01001626 if (!ret) {
Tejun Heo7e697232011-05-23 13:26:07 +02001627 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
Jan Kara04906b22019-01-14 09:48:10 +01001628 set_init_blocksize(bdev);
1629 }
Tejun Heo7e697232011-05-23 13:26:07 +02001630
Tejun Heo1196f8b2011-04-21 20:54:45 +02001631 /*
1632 * If the device is invalidated, rescan partition
1633 * if open succeeded or failed with -ENOMEDIUM.
1634 * The latter is necessary to prevent ghost
1635 * partitions on a removed medium.
1636 */
Jan Kara731dc482019-10-21 10:37:59 +02001637 if (bdev->bd_invalidated &&
1638 (!ret || ret == -ENOMEDIUM))
1639 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Dan Williams5a023cd2015-11-30 10:20:29 -08001640
Tejun Heo1196f8b2011-04-21 20:54:45 +02001641 if (ret)
1642 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 struct block_device *whole;
1645 whole = bdget_disk(disk, 0);
1646 ret = -ENOMEM;
1647 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001648 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001649 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001650 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001652 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 bdev->bd_contains = whole;
Tejun Heo89f97492008-11-05 10:21:06 +01001654 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001655 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001656 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001658 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 }
Tejun Heo89f97492008-11-05 10:21:06 +01001660 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Jan Kara04906b22019-01-14 09:48:10 +01001661 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 }
Jan Kara03e26272017-03-23 01:36:53 +01001663
1664 if (bdev->bd_bdi == &noop_backing_dev_info)
1665 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001668 ret = 0;
1669 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001670 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001671 /* the same as first opener case, read comment there */
Jan Kara731dc482019-10-21 10:37:59 +02001672 if (bdev->bd_invalidated &&
1673 (!ret || ret == -ENOMEDIUM))
1674 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001675 if (ret)
1676 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 }
1678 }
1679 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001680 if (for_part)
1681 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001682 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001683 disk_unblock_events(disk);
Jan Kara89736652018-02-26 13:01:40 +01001684 /* only one opener holds refs to the module and disk */
1685 if (!first_open)
1686 put_disk_and_module(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 return 0;
1688
Tejun Heo0762b8b2008-08-25 19:56:12 +09001689 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001690 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001692 bdev->bd_part = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001693 bdev->bd_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001695 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001697 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001698 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001699 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001700 put_disk_and_module(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001701 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001702 bdput(bdev);
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 return ret;
1705}
1706
Tejun Heod4d77622010-11-13 11:55:18 +01001707/**
1708 * blkdev_get - open a block device
1709 * @bdev: block_device to open
1710 * @mode: FMODE_* mask
1711 * @holder: exclusive holder identifier
1712 *
1713 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1714 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1715 * @holder is invalid. Exclusive opens may nest for the same @holder.
1716 *
1717 * On success, the reference count of @bdev is unchanged. On failure,
1718 * @bdev is put.
1719 *
1720 * CONTEXT:
1721 * Might sleep.
1722 *
1723 * RETURNS:
1724 * 0 on success, -errno on failure.
1725 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001726int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727{
Tejun Heoe525fd82010-11-13 11:55:17 +01001728 struct block_device *whole = NULL;
1729 int res;
1730
1731 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1732
1733 if ((mode & FMODE_EXCL) && holder) {
1734 whole = bd_start_claiming(bdev, holder);
1735 if (IS_ERR(whole)) {
1736 bdput(bdev);
1737 return PTR_ERR(whole);
1738 }
1739 }
1740
1741 res = __blkdev_get(bdev, mode, 0);
1742
1743 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001744 struct gendisk *disk = whole->bd_disk;
1745
Tejun Heo6a027ef2010-11-13 11:55:17 +01001746 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001747 mutex_lock(&bdev->bd_mutex);
Jan Karae91455b2019-08-07 11:36:47 +02001748 if (!res)
1749 bd_finish_claiming(bdev, whole, holder);
1750 else
1751 bd_abort_claiming(bdev, whole, holder);
Tejun Heo77ea8872010-12-08 20:57:37 +01001752 /*
Tejun Heod4dc2102011-04-21 20:54:46 +02001753 * Block event polling for write claims if requested. Any
1754 * write holder makes the write_holder state stick until
1755 * all are released. This is good enough and tracking
1756 * individual writeable reference is too fragile given the
1757 * way @mode is used in blkdev_get/put().
Tejun Heo77ea8872010-12-08 20:57:37 +01001758 */
Tejun Heo4c49ff32011-06-01 08:27:41 +02001759 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1760 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001761 bdev->bd_write_holder = true;
Tejun Heod4dc2102011-04-21 20:54:46 +02001762 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001763 }
1764
1765 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001766 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001767 }
1768
1769 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001770}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771EXPORT_SYMBOL(blkdev_get);
1772
Tejun Heod4d77622010-11-13 11:55:18 +01001773/**
1774 * blkdev_get_by_path - open a block device by name
1775 * @path: path to the block device to open
1776 * @mode: FMODE_* mask
1777 * @holder: exclusive holder identifier
1778 *
1779 * Open the blockdevice described by the device file at @path. @mode
1780 * and @holder are identical to blkdev_get().
1781 *
1782 * On success, the returned block_device has reference count of one.
1783 *
1784 * CONTEXT:
1785 * Might sleep.
1786 *
1787 * RETURNS:
1788 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1789 */
1790struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1791 void *holder)
1792{
1793 struct block_device *bdev;
1794 int err;
1795
1796 bdev = lookup_bdev(path);
1797 if (IS_ERR(bdev))
1798 return bdev;
1799
1800 err = blkdev_get(bdev, mode, holder);
1801 if (err)
1802 return ERR_PTR(err);
1803
Chuck Ebberte51900f2011-02-16 18:11:53 -05001804 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1805 blkdev_put(bdev, mode);
1806 return ERR_PTR(-EACCES);
1807 }
1808
Tejun Heod4d77622010-11-13 11:55:18 +01001809 return bdev;
1810}
1811EXPORT_SYMBOL(blkdev_get_by_path);
1812
1813/**
1814 * blkdev_get_by_dev - open a block device by device number
1815 * @dev: device number of block device to open
1816 * @mode: FMODE_* mask
1817 * @holder: exclusive holder identifier
1818 *
1819 * Open the blockdevice described by device number @dev. @mode and
1820 * @holder are identical to blkdev_get().
1821 *
1822 * Use it ONLY if you really do not have anything better - i.e. when
1823 * you are behind a truly sucky interface and all you are given is a
1824 * device number. _Never_ to be used for internal purposes. If you
1825 * ever need it - reconsider your API.
1826 *
1827 * On success, the returned block_device has reference count of one.
1828 *
1829 * CONTEXT:
1830 * Might sleep.
1831 *
1832 * RETURNS:
1833 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1834 */
1835struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1836{
1837 struct block_device *bdev;
1838 int err;
1839
1840 bdev = bdget(dev);
1841 if (!bdev)
1842 return ERR_PTR(-ENOMEM);
1843
1844 err = blkdev_get(bdev, mode, holder);
1845 if (err)
1846 return ERR_PTR(err);
1847
1848 return bdev;
1849}
1850EXPORT_SYMBOL(blkdev_get_by_dev);
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852static int blkdev_open(struct inode * inode, struct file * filp)
1853{
1854 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /*
1857 * Preserve backwards compatibility and allow large file access
1858 * even if userspace doesn't ask for it explicitly. Some mkfs
1859 * binary needs it. We might want to drop this workaround
1860 * during an unstable branch.
1861 */
1862 filp->f_flags |= O_LARGEFILE;
1863
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001864 filp->f_mode |= FMODE_NOWAIT;
1865
Al Viro572c4892007-10-08 13:24:05 -04001866 if (filp->f_flags & O_NDELAY)
1867 filp->f_mode |= FMODE_NDELAY;
1868 if (filp->f_flags & O_EXCL)
1869 filp->f_mode |= FMODE_EXCL;
1870 if ((filp->f_flags & O_ACCMODE) == 3)
1871 filp->f_mode |= FMODE_WRITE_IOCTL;
1872
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001874 if (bdev == NULL)
1875 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Al Viro572c4892007-10-08 13:24:05 -04001877 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001878 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Al Viro572c4892007-10-08 13:24:05 -04001879
Tejun Heoe525fd82010-11-13 11:55:17 +01001880 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
Al Viro4385bab2013-05-05 22:11:03 -04001883static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001884{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001885 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001886 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001887
NeilBrown6796bf52006-12-08 02:36:16 -08001888 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001889 if (for_part)
1890 bdev->bd_part_count--;
1891
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001892 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001893 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001894 sync_blockdev(bdev);
1895 kill_bdev(bdev);
Ilya Dryomov43d1c0e2015-11-20 22:22:34 +01001896
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001897 bdev_write_inode(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001898 }
1899 if (bdev->bd_contains == bdev) {
1900 if (disk->fops->release)
Al Virodb2a1442013-05-05 21:52:57 -04001901 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001902 }
1903 if (!bdev->bd_openers) {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001904 disk_put_part(bdev->bd_part);
1905 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001906 bdev->bd_disk = NULL;
NeilBrown37be4122006-12-08 02:36:16 -08001907 if (bdev != bdev->bd_contains)
1908 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001909 bdev->bd_contains = NULL;
Tejun Heo523e1d32011-10-19 14:31:07 +02001910
Jan Kara9df6c292018-02-26 13:01:39 +01001911 put_disk_and_module(disk);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001912 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001913 mutex_unlock(&bdev->bd_mutex);
1914 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001915 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001916 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001917}
1918
Al Viro4385bab2013-05-05 22:11:03 -04001919void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001920{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001921 mutex_lock(&bdev->bd_mutex);
1922
Tejun Heoe525fd82010-11-13 11:55:17 +01001923 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001924 bool bdev_free;
1925
1926 /*
1927 * Release a claim on the device. The holder fields
1928 * are protected with bdev_lock. bd_mutex is to
1929 * synchronize disk_holder unlinking.
1930 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001931 spin_lock(&bdev_lock);
1932
1933 WARN_ON_ONCE(--bdev->bd_holders < 0);
1934 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1935
1936 /* bd_contains might point to self, check in a separate step */
1937 if ((bdev_free = !bdev->bd_holders))
1938 bdev->bd_holder = NULL;
1939 if (!bdev->bd_contains->bd_holders)
1940 bdev->bd_contains->bd_holder = NULL;
1941
1942 spin_unlock(&bdev_lock);
1943
Tejun Heo77ea8872010-12-08 20:57:37 +01001944 /*
1945 * If this was the last claim, remove holder link and
1946 * unblock evpoll if it was a write holder.
1947 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001948 if (bdev_free && bdev->bd_write_holder) {
1949 disk_unblock_events(bdev->bd_disk);
1950 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001951 }
Tejun Heo69362172011-03-09 19:54:27 +01001952 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001953
Tejun Heo85ef06d2011-07-01 16:17:47 +02001954 /*
1955 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1956 * event. This is to ensure detection of media removal commanded
1957 * from userland - e.g. eject(1).
1958 */
1959 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1960
1961 mutex_unlock(&bdev->bd_mutex);
1962
Al Viro4385bab2013-05-05 22:11:03 -04001963 __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001964}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001965EXPORT_SYMBOL(blkdev_put);
1966
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967static int blkdev_close(struct inode * inode, struct file * filp)
1968{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001969 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001970 blkdev_put(bdev, filp->f_mode);
1971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972}
1973
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001974static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001976 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001977 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001978
1979 /*
1980 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1981 * to updated it before every ioctl.
1982 */
Al Viro56b26ad2008-09-19 03:17:36 -04001983 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001984 mode |= FMODE_NDELAY;
1985 else
1986 mode &= ~FMODE_NDELAY;
1987
Al Viro56b26ad2008-09-19 03:17:36 -04001988 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001991/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001992 * Write data to the block device. Only intended for the block device itself
1993 * and the raw driver which basically is a fake block device.
1994 *
1995 * Does not take i_mutex for the write and thus is not for general purpose
1996 * use.
1997 */
Al Viro1456c0a2014-04-03 03:21:50 -04001998ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02001999{
2000 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002001 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04002002 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002003 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02002004 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04002005
Al Viro7ec7b942015-04-07 11:35:14 -04002006 if (bdev_read_only(I_BDEV(bd_inode)))
2007 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04002008
Darrick J. Wongdc617f22019-08-20 07:55:16 -07002009 if (IS_SWAPFILE(bd_inode))
2010 return -ETXTBSY;
2011
Al Viro7ec7b942015-04-07 11:35:14 -04002012 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04002013 return 0;
2014
Al Viro7ec7b942015-04-07 11:35:14 -04002015 if (iocb->ki_pos >= size)
2016 return -ENOSPC;
2017
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02002018 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
2019 return -EOPNOTSUPP;
2020
Al Viro7ec7b942015-04-07 11:35:14 -04002021 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002022
Jianpeng Ma53362a02012-08-02 09:50:39 +02002023 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04002024 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07002025 if (ret > 0)
2026 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002027 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002028 return ret;
2029}
Al Viro1456c0a2014-04-03 03:21:50 -04002030EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002031
David Jefferyb2de5252014-09-29 10:21:10 -04002032ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002033{
2034 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002035 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002036 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04002037 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002038
2039 if (pos >= size)
2040 return 0;
2041
2042 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04002043 iov_iter_truncate(to, size);
2044 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002045}
David Jefferyb2de5252014-09-29 10:21:10 -04002046EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002047
Christoph Hellwigeef99382009-08-20 17:43:41 +02002048/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002049 * Try to release a page associated with block device when the system
2050 * is under memory pressure.
2051 */
2052static int blkdev_releasepage(struct page *page, gfp_t wait)
2053{
2054 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
2055
2056 if (super && super->s_op->bdev_try_to_free_page)
2057 return super->s_op->bdev_try_to_free_page(super, page, wait);
2058
2059 return try_to_free_buffers(page);
2060}
2061
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002062static int blkdev_writepages(struct address_space *mapping,
2063 struct writeback_control *wbc)
2064{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002065 return generic_writepages(mapping, wbc);
2066}
2067
Adrian Bunk4c54ac62008-02-18 13:48:31 +01002068static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 .readpage = blkdev_readpage,
Akinobu Mita447f05b2014-10-09 15:26:58 -07002070 .readpages = blkdev_readpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07002072 .write_begin = blkdev_write_begin,
2073 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002074 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002075 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08002077 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07002078 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079};
2080
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002081#define BLKDEV_FALLOC_FL_SUPPORTED \
2082 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
2083 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
2084
2085static long blkdev_fallocate(struct file *file, int mode, loff_t start,
2086 loff_t len)
2087{
2088 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002089 struct address_space *mapping;
2090 loff_t end = start + len - 1;
2091 loff_t isize;
2092 int error;
2093
2094 /* Fail if we don't recognize the flags. */
2095 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
2096 return -EOPNOTSUPP;
2097
2098 /* Don't go off the end of the device. */
2099 isize = i_size_read(bdev->bd_inode);
2100 if (start >= isize)
2101 return -EINVAL;
2102 if (end >= isize) {
2103 if (mode & FALLOC_FL_KEEP_SIZE) {
2104 len = isize - start;
2105 end = start + len - 1;
2106 } else
2107 return -EINVAL;
2108 }
2109
2110 /*
2111 * Don't allow IO that isn't aligned to logical block size.
2112 */
2113 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
2114 return -EINVAL;
2115
2116 /* Invalidate the page cache, including dirty pages. */
2117 mapping = bdev->bd_inode->i_mapping;
2118 truncate_inode_pages_range(mapping, start, end);
2119
2120 switch (mode) {
2121 case FALLOC_FL_ZERO_RANGE:
2122 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
2123 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02002124 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002125 break;
2126 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02002127 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2128 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002129 break;
2130 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002131 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2132 GFP_KERNEL, 0);
2133 break;
2134 default:
2135 return -EOPNOTSUPP;
2136 }
2137 if (error)
2138 return error;
2139
2140 /*
2141 * Invalidate again; if someone wandered in and dirtied a page,
2142 * the caller will be given -EBUSY. The third argument is
2143 * inclusive, so the rounding here is safe.
2144 */
2145 return invalidate_inode_pages2_range(mapping,
2146 start >> PAGE_SHIFT,
2147 end >> PAGE_SHIFT);
2148}
2149
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002150const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 .open = blkdev_open,
2152 .release = blkdev_close,
2153 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04002154 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04002155 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07002156 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07002157 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07002158 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07002159 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160#ifdef CONFIG_COMPAT
2161 .compat_ioctl = compat_blkdev_ioctl,
2162#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08002163 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002164 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002165 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166};
2167
2168int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
2169{
2170 int res;
2171 mm_segment_t old_fs = get_fs();
2172 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04002173 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 set_fs(old_fs);
2175 return res;
2176}
2177
2178EXPORT_SYMBOL(ioctl_by_bdev);
2179
2180/**
2181 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08002182 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02002184 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 * namespace if possible and return it. Return ERR_PTR(error)
2186 * otherwise.
2187 */
Al Viro421748e2008-08-02 01:04:36 -04002188struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190 struct block_device *bdev;
2191 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04002192 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 int error;
2194
Al Viro421748e2008-08-02 01:04:36 -04002195 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 return ERR_PTR(-EINVAL);
2197
Al Viro421748e2008-08-02 01:04:36 -04002198 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 if (error)
2200 return ERR_PTR(error);
2201
David Howellsbb6687342015-03-17 22:26:21 +00002202 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 error = -ENOTBLK;
2204 if (!S_ISBLK(inode->i_mode))
2205 goto fail;
2206 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002207 if (!may_open_dev(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 goto fail;
2209 error = -ENOMEM;
2210 bdev = bd_acquire(inode);
2211 if (!bdev)
2212 goto fail;
2213out:
Al Viro421748e2008-08-02 01:04:36 -04002214 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 return bdev;
2216fail:
2217 bdev = ERR_PTR(error);
2218 goto out;
2219}
Al Virod5686b42008-08-01 05:00:11 -04002220EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
NeilBrown93b270f2011-02-24 17:25:47 +11002222int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01002223{
2224 struct super_block *sb = get_super(bdev);
2225 int res = 0;
2226
2227 if (sb) {
2228 /*
2229 * no need to lock the super, get_super holds the
2230 * read mutex so the filesystem cannot go away
2231 * under us (->put_super runs with the write lock
2232 * hold).
2233 */
2234 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11002235 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01002236 drop_super(sb);
2237 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002238 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01002239 return res;
2240}
2241EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002242
2243void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2244{
2245 struct inode *inode, *old_inode = NULL;
2246
Dave Chinner74278da2015-03-04 12:37:22 -05002247 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002248 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2249 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01002250 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02002251
2252 spin_lock(&inode->i_lock);
2253 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2254 mapping->nrpages == 0) {
2255 spin_unlock(&inode->i_lock);
2256 continue;
2257 }
2258 __iget(inode);
2259 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05002260 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002261 /*
2262 * We hold a reference to 'inode' so it couldn't have been
2263 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05002264 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02002265 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05002266 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02002267 * later.
2268 */
2269 iput(old_inode);
2270 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01002271 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002272
Rabin Vincentaf309222016-12-01 09:18:28 +01002273 mutex_lock(&bdev->bd_mutex);
2274 if (bdev->bd_openers)
2275 func(bdev, arg);
2276 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002277
Dave Chinner74278da2015-03-04 12:37:22 -05002278 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002279 }
Dave Chinner74278da2015-03-04 12:37:22 -05002280 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002281 iput(old_inode);
2282}