blob: 52b6f646cdbd0af27d827d9e4d983ec1455c87f0 [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>
Domenico Andreoli56939e02020-03-23 08:22:15 -070037#include <linux/suspend.h>
David Howells07f3f052006-09-30 20:52:18 +020038#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40struct bdev_inode {
41 struct block_device bdev;
42 struct inode vfs_inode;
43};
44
Adrian Bunk4c54ac62008-02-18 13:48:31 +010045static const struct address_space_operations def_blk_aops;
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static inline struct bdev_inode *BDEV_I(struct inode *inode)
48{
49 return container_of(inode, struct bdev_inode, vfs_inode);
50}
51
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020052struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053{
54 return &BDEV_I(inode)->bdev;
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056EXPORT_SYMBOL(I_BDEV);
57
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070058static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010059{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070060 struct inode *inode = bdev->bd_inode;
61 int ret;
62
Christoph Hellwig564f00f2015-01-14 10:42:33 +010063 spin_lock(&inode->i_lock);
64 while (inode->i_state & I_DIRTY) {
65 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070066 ret = write_inode_now(inode, true);
67 if (ret) {
68 char name[BDEVNAME_SIZE];
69 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
70 "for block device %s (err=%d).\n",
71 bdevname(bdev, name), ret);
72 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010073 spin_lock(&inode->i_lock);
74 }
75 spin_unlock(&inode->i_lock);
76}
77
Peter Zijlstraf9a14392007-05-06 14:49:55 -070078/* Kill _all_ buffers and pagecache , dirty or not.. */
Al Viroff01bb42011-09-16 02:31:11 -040079void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
Al Viroff01bb42011-09-16 02:31:11 -040081 struct address_space *mapping = bdev->bd_inode->i_mapping;
82
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080083 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
Peter Zijlstraf9a14392007-05-06 14:49:55 -070084 return;
Al Viroff01bb42011-09-16 02:31:11 -040085
Peter Zijlstraf9a14392007-05-06 14:49:55 -070086 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040087 truncate_inode_pages(mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
Al Viroff01bb42011-09-16 02:31:11 -040089EXPORT_SYMBOL(kill_bdev);
90
91/* Invalidate clean unused buffers and pagecache. */
92void invalidate_bdev(struct block_device *bdev)
93{
94 struct address_space *mapping = bdev->bd_inode->i_mapping;
95
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070096 if (mapping->nrpages) {
97 invalidate_bh_lrus();
98 lru_add_drain_all(); /* make sure all lru add caches are flushed */
99 invalidate_mapping_pages(mapping, 0, -1);
100 }
Al Viroff01bb42011-09-16 02:31:11 -0400101 /* 99% of the time, we don't need to flush the cleancache on the bdev.
102 * But, for the strange corners, lets be cautious
103 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400104 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400105}
106EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Jan Kara04906b22019-01-14 09:48:10 +0100108static void set_init_blocksize(struct block_device *bdev)
109{
110 unsigned bsize = bdev_logical_block_size(bdev);
111 loff_t size = i_size_read(bdev->bd_inode);
112
113 while (bsize < PAGE_SIZE) {
114 if (size & bsize)
115 break;
116 bsize <<= 1;
117 }
118 bdev->bd_block_size = bsize;
119 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122int set_blocksize(struct block_device *bdev, int size)
123{
124 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700125 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return -EINVAL;
127
128 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400129 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 return -EINVAL;
131
132 /* Don't change the size if it is same as current */
133 if (bdev->bd_block_size != size) {
134 sync_blockdev(bdev);
135 bdev->bd_block_size = size;
136 bdev->bd_inode->i_blkbits = blksize_bits(size);
137 kill_bdev(bdev);
138 }
139 return 0;
140}
141
142EXPORT_SYMBOL(set_blocksize);
143
144int sb_set_blocksize(struct super_block *sb, int size)
145{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (set_blocksize(sb->s_bdev, size))
147 return 0;
148 /* If we get here, we know size is power of two
149 * and it's value is between 512 and PAGE_SIZE */
150 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800151 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return sb->s_blocksize;
153}
154
155EXPORT_SYMBOL(sb_set_blocksize);
156
157int sb_min_blocksize(struct super_block *sb, int size)
158{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400159 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (size < minsize)
161 size = minsize;
162 return sb_set_blocksize(sb, size);
163}
164
165EXPORT_SYMBOL(sb_min_blocksize);
166
167static int
168blkdev_get_block(struct inode *inode, sector_t iblock,
169 struct buffer_head *bh, int create)
170{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 bh->b_bdev = I_BDEV(inode);
172 bh->b_blocknr = iblock;
173 set_buffer_mapped(bh);
174 return 0;
175}
176
Dan Williams4ebb16c2015-10-28 07:48:19 +0900177static struct inode *bdev_file_inode(struct file *file)
178{
179 return file->f_mapping->host;
180}
181
Jens Axboe78250c02016-11-17 17:50:47 +0100182static unsigned int dio_bio_write_op(struct kiocb *iocb)
183{
184 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
185
186 /* avoid the need for a I/O completion work item */
187 if (iocb->ki_flags & IOCB_DSYNC)
188 op |= REQ_FUA;
189 return op;
190}
191
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600192#define DIO_INLINE_BIO_VECS 4
193
194static void blkdev_bio_end_io_simple(struct bio *bio)
195{
196 struct task_struct *waiter = bio->bi_private;
197
198 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700199 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600200}
201
202static ssize_t
203__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
204 int nr_pages)
205{
206 struct file *file = iocb->ki_filp;
207 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200208 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600209 loff_t pos = iocb->ki_pos;
210 bool should_dirty = false;
211 struct bio bio;
212 ssize_t ret;
213 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600214
Jens Axboe9a794fb2016-11-22 08:12:39 -0700215 if ((pos | iov_iter_alignment(iter)) &
216 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600217 return -EINVAL;
218
Jens Axboe72ecad22016-11-16 23:11:42 -0700219 if (nr_pages <= DIO_INLINE_BIO_VECS)
220 vecs = inline_vecs;
221 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700222 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
223 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700224 if (!vecs)
225 return -ENOMEM;
226 }
227
Ming Lei3a83f462016-11-22 08:57:21 -0700228 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200229 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900230 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600231 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600232 bio.bi_private = current;
233 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700234 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600235
236 ret = bio_iov_iter_get_pages(&bio, iter);
237 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200238 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600239 ret = bio.bi_iter.bi_size;
240
241 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100242 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600243 if (iter_is_iovec(iter))
244 should_dirty = true;
245 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100246 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600247 task_io_account_write(ret);
248 }
Jens Axboed1e36282018-08-29 10:36:56 -0600249 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700250 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600251
252 qc = submit_bio(&bio);
253 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800254 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600255 if (!READ_ONCE(bio.bi_private))
256 break;
257 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700258 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600259 io_schedule();
260 }
261 __set_current_state(TASK_RUNNING);
262
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200263 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200264 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700265 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600266
Martin Wilck9362dd12018-07-25 23:15:08 +0200267out:
268 if (vecs != inline_vecs)
269 kfree(vecs);
270
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600271 bio_uninit(&bio);
272
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600273 return ret;
274}
275
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700276struct blkdev_dio {
277 union {
278 struct kiocb *iocb;
279 struct task_struct *waiter;
280 };
281 size_t size;
282 atomic_t ref;
283 bool multi_bio : 1;
284 bool should_dirty : 1;
285 bool is_sync : 1;
286 struct bio bio;
287};
288
Kent Overstreet52190f82018-05-20 18:25:55 -0400289static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700290
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700291static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
292{
293 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
294 struct request_queue *q = bdev_get_queue(bdev);
295
296 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
297}
298
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700299static void blkdev_bio_end_io(struct bio *bio)
300{
301 struct blkdev_dio *dio = bio->bi_private;
302 bool should_dirty = dio->should_dirty;
303
Jason Yana89afe52019-04-12 10:09:16 +0800304 if (bio->bi_status && !dio->bio.bi_status)
305 dio->bio.bi_status = bio->bi_status;
306
307 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700308 if (!dio->is_sync) {
309 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200310 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700311
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200312 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700313 ret = dio->size;
314 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200315 } else {
316 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700317 }
318
319 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100320 if (dio->multi_bio)
321 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700322 } else {
323 struct task_struct *waiter = dio->waiter;
324
325 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700326 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700327 }
328 }
329
330 if (should_dirty) {
331 bio_check_pages_dirty(bio);
332 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200333 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700334 bio_put(bio);
335 }
336}
337
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800338static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700339__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800340{
341 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900342 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700343 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100344 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700345 struct blkdev_dio *dio;
346 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700347 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100348 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700349 loff_t pos = iocb->ki_pos;
350 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe7b6620d2019-08-15 11:09:16 -0600351 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700352
Jens Axboe9a794fb2016-11-22 08:12:39 -0700353 if ((pos | iov_iter_alignment(iter)) &
354 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700355 return -EINVAL;
356
Jens Axboe7b6620d2019-08-15 11:09:16 -0600357 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700358
359 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100360 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100361 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700362 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100363 bio_get(bio);
364 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700365 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100366 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700367
368 dio->size = 0;
369 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100370 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700371
Jens Axboecb700eb2018-11-15 19:56:53 -0700372 /*
373 * Don't plug for HIPRI/polled IO, as those should go straight
374 * to issue
375 */
376 if (!is_poll)
377 blk_start_plug(&plug);
378
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700379 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200380 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900381 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600382 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700383 bio->bi_private = dio;
384 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700385 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700386
Jens Axboee15c2ff2019-08-06 13:34:31 -0600387 ret = bio_iov_iter_get_pages(bio, iter);
388 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200389 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700390 bio_endio(bio);
391 break;
392 }
393
394 if (is_read) {
395 bio->bi_opf = REQ_OP_READ;
396 if (dio->should_dirty)
397 bio_set_pages_dirty(bio);
398 } else {
399 bio->bi_opf = dio_bio_write_op(iocb);
400 task_io_account_write(bio->bi_iter.bi_size);
401 }
402
Jens Axboe7b6620d2019-08-15 11:09:16 -0600403 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700404 pos += bio->bi_iter.bi_size;
405
406 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
407 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700408 bool polled = false;
409
410 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700411 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700412 polled = true;
413 }
Jens Axboed34513d2018-11-06 14:29:11 -0700414
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700415 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700416
417 if (polled)
418 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700419 break;
420 }
421
422 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100423 /*
424 * AIO needs an extra reference to ensure the dio
425 * structure which is embedded into the first bio
426 * stays around.
427 */
428 if (!is_sync)
429 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700430 dio->multi_bio = true;
431 atomic_set(&dio->ref, 2);
432 } else {
433 atomic_inc(&dio->ref);
434 }
435
Jens Axboe7b6620d2019-08-15 11:09:16 -0600436 submit_bio(bio);
437 bio = bio_alloc(GFP_KERNEL, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700438 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700439
440 if (!is_poll)
441 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700442
Christoph Hellwig690e5322017-01-24 14:50:19 +0100443 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700444 return -EIOCBQUEUED;
445
446 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800447 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700448 if (!READ_ONCE(dio->waiter))
449 break;
450
451 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700452 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700453 io_schedule();
454 }
455 __set_current_state(TASK_RUNNING);
456
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200457 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200458 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600459 if (likely(!ret))
460 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700461
462 bio_put(&dio->bio);
463 return ret;
464}
465
466static ssize_t
467blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
468{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600469 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800470
Jens Axboe72ecad22016-11-16 23:11:42 -0700471 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600472 if (!nr_pages)
473 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700474 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600475 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700476
477 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800478}
479
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700480static __init int blkdev_init(void)
481{
Kent Overstreet52190f82018-05-20 18:25:55 -0400482 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700483}
484module_init(blkdev_init);
485
Jan Kara5cee5812009-04-27 16:43:51 +0200486int __sync_blockdev(struct block_device *bdev, int wait)
487{
488 if (!bdev)
489 return 0;
490 if (!wait)
491 return filemap_flush(bdev->bd_inode->i_mapping);
492 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
493}
494
Nick Piggin585d3bc2009-02-25 10:44:19 +0100495/*
496 * Write out and wait upon all the dirty data associated with a block
497 * device via its mapping. Does not take the superblock lock.
498 */
499int sync_blockdev(struct block_device *bdev)
500{
Jan Kara5cee5812009-04-27 16:43:51 +0200501 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100502}
503EXPORT_SYMBOL(sync_blockdev);
504
505/*
506 * Write out and wait upon all dirty data associated with this
507 * device. Filesystem data as well as the underlying block
508 * device. Takes the superblock lock.
509 */
510int fsync_bdev(struct block_device *bdev)
511{
512 struct super_block *sb = get_super(bdev);
513 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200514 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100515 drop_super(sb);
516 return res;
517 }
518 return sync_blockdev(bdev);
519}
Al Viro47e44912009-04-01 07:07:16 -0400520EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100521
522/**
523 * freeze_bdev -- lock a filesystem and force it into a consistent state
524 * @bdev: blockdevice to lock
525 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100526 * If a superblock is found on this device, we take the s_umount semaphore
527 * on it to make sure nobody unmounts until the snapshot creation is done.
528 * The reference counter (bd_fsfreeze_count) guarantees that only the last
529 * unfreeze process can unfreeze the frozen filesystem actually when multiple
530 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
531 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
532 * actually.
533 */
534struct super_block *freeze_bdev(struct block_device *bdev)
535{
536 struct super_block *sb;
537 int error = 0;
538
539 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200540 if (++bdev->bd_fsfreeze_count > 1) {
541 /*
542 * We don't even need to grab a reference - the first call
543 * to freeze_bdev grab an active reference and only the last
544 * thaw_bdev drops it.
545 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100546 sb = get_super(bdev);
Andrey Ryabinin5bb53c02016-08-23 18:55:31 +0300547 if (sb)
548 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100549 mutex_unlock(&bdev->bd_fsfreeze_mutex);
550 return sb;
551 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100552
Christoph Hellwig45042302009-08-03 23:28:35 +0200553 sb = get_active_super(bdev);
554 if (!sb)
555 goto out;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600556 if (sb->s_op->freeze_super)
557 error = sb->s_op->freeze_super(sb);
558 else
559 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400560 if (error) {
561 deactivate_super(sb);
562 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200563 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400564 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100565 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400566 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200567 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100568 sync_blockdev(bdev);
569 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200570 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100571}
572EXPORT_SYMBOL(freeze_bdev);
573
574/**
575 * thaw_bdev -- unlock filesystem
576 * @bdev: blockdevice to unlock
577 * @sb: associated superblock
578 *
579 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
580 */
581int thaw_bdev(struct block_device *bdev, struct super_block *sb)
582{
Christoph Hellwig45042302009-08-03 23:28:35 +0200583 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100584
585 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200586 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400587 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100588
Christoph Hellwig45042302009-08-03 23:28:35 +0200589 error = 0;
590 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400591 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100592
Christoph Hellwig45042302009-08-03 23:28:35 +0200593 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400594 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200595
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600596 if (sb->s_op->thaw_super)
597 error = sb->s_op->thaw_super(sb);
598 else
599 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200600 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400601 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400602out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100603 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200604 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100605}
606EXPORT_SYMBOL(thaw_bdev);
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
609{
610 return block_write_full_page(page, blkdev_get_block, wbc);
611}
612
613static int blkdev_readpage(struct file * file, struct page * page)
614{
615 return block_read_full_page(page, blkdev_get_block);
616}
617
Akinobu Mita447f05b2014-10-09 15:26:58 -0700618static int blkdev_readpages(struct file *file, struct address_space *mapping,
619 struct list_head *pages, unsigned nr_pages)
620{
621 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
622}
623
Nick Piggin6272b5a2007-10-16 01:25:04 -0700624static int blkdev_write_begin(struct file *file, struct address_space *mapping,
625 loff_t pos, unsigned len, unsigned flags,
626 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200628 return block_write_begin(mapping, pos, len, flags, pagep,
629 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
Nick Piggin6272b5a2007-10-16 01:25:04 -0700632static int blkdev_write_end(struct file *file, struct address_space *mapping,
633 loff_t pos, unsigned len, unsigned copied,
634 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700636 int ret;
637 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
638
639 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300640 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700641
642 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643}
644
645/*
646 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500647 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * so we compute the size by hand (just as in block_read/write above)
649 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800650static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900652 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 loff_t retval;
654
Al Viro59551022016-01-22 15:40:57 -0500655 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400656 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500657 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 return retval;
659}
660
Josef Bacik02c24a82011-07-16 20:44:56 -0400661int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900663 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400664 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100665 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200666
Jeff Layton372cf242017-07-06 07:02:28 -0400667 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200668 if (error)
669 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100670
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400671 /*
672 * There is no need to serialise calls to blkdev_issue_flush with
673 * i_mutex and doing so causes performance issues with concurrent
674 * O_SYNC writers to a block device.
675 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200676 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100677 if (error == -EOPNOTSUPP)
678 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400679
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100680 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700682EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700684/**
685 * bdev_read_page() - Start reading a page from a block device
686 * @bdev: The device to read the page from
687 * @sector: The offset on the device to read the page to (need not be aligned)
688 * @page: The page to read
689 *
690 * On entry, the page should be locked. It will be unlocked when the page
691 * has been read. If the block driver implements rw_page synchronously,
692 * that will be true on exit from this function, but it need not be.
693 *
694 * Errors returned by this function are usually "soft", eg out of memory, or
695 * queue full; callers should try a different route to read this page rather
696 * than propagate an error back up the stack.
697 *
698 * Return: negative errno if an error occurs, 0 if submission was successful.
699 */
700int bdev_read_page(struct block_device *bdev, sector_t sector,
701 struct page *page)
702{
703 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800704 int result = -EOPNOTSUPP;
705
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400706 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800707 return result;
708
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800709 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800710 if (result)
711 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700712 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
713 REQ_OP_READ);
Dan Williams2e6edc952015-11-19 13:29:28 -0800714 blk_queue_exit(bdev->bd_queue);
715 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700716}
717EXPORT_SYMBOL_GPL(bdev_read_page);
718
719/**
720 * bdev_write_page() - Start writing a page to a block device
721 * @bdev: The device to write the page to
722 * @sector: The offset on the device to write the page to (need not be aligned)
723 * @page: The page to write
724 * @wbc: The writeback_control for the write
725 *
726 * On entry, the page should be locked and not currently under writeback.
727 * On exit, if the write started successfully, the page will be unlocked and
728 * under writeback. If the write failed already (eg the driver failed to
729 * queue the page to the device), the page will still be locked. If the
730 * caller is a ->writepage implementation, it will need to unlock the page.
731 *
732 * Errors returned by this function are usually "soft", eg out of memory, or
733 * queue full; callers should try a different route to write this page rather
734 * than propagate an error back up the stack.
735 *
736 * Return: negative errno if an error occurs, 0 if submission was successful.
737 */
738int bdev_write_page(struct block_device *bdev, sector_t sector,
739 struct page *page, struct writeback_control *wbc)
740{
741 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700742 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800743
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400744 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700745 return -EOPNOTSUPP;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800746 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800747 if (result)
748 return result;
749
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700750 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700751 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
752 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700753 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700754 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700755 } else {
756 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700757 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700758 }
Dan Williams2e6edc952015-11-19 13:29:28 -0800759 blk_queue_exit(bdev->bd_queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700760 return result;
761}
762EXPORT_SYMBOL_GPL(bdev_write_page);
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764/*
765 * pseudo-fs
766 */
767
768static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800769static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771static struct inode *bdev_alloc_inode(struct super_block *sb)
772{
Christoph Lametere94b1762006-12-06 20:33:17 -0800773 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 if (!ei)
775 return NULL;
776 return &ei->vfs_inode;
777}
778
Al Viro41149cb2019-04-10 15:12:38 -0400779static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Al Viro41149cb2019-04-10 15:12:38 -0400781 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100782}
783
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700784static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct bdev_inode *ei = (struct bdev_inode *) foo;
787 struct block_device *bdev = &ei->bdev;
788
Christoph Lametera35afb82007-05-16 22:10:57 -0700789 memset(bdev, 0, sizeof(*bdev));
790 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700791 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100792#ifdef CONFIG_SYSFS
793 INIT_LIST_HEAD(&bdev->bd_holder_disks);
794#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100795 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700796 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800797 /* Initialize mutex for freeze. */
798 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799}
800
Al Virob57922d2010-06-07 14:34:48 -0400801static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700804 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400805 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200806 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_lock(&bdev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 list_del_init(&bdev->bd_list);
809 spin_unlock(&bdev_lock);
Jan Karaf7597412017-03-23 01:37:00 +0100810 /* Detach inode from wb early as bdi_put() may free bdi->wb */
811 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100812 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100813 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100814 bdev->bd_bdi = &noop_backing_dev_info;
815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800818static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 .statfs = simple_statfs,
820 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400821 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400823 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824};
825
David Howells9030d162019-03-25 16:38:23 +0000826static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
David Howells9030d162019-03-25 16:38:23 +0000828 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
829 if (!ctx)
830 return -ENOMEM;
831 fc->s_iflags |= SB_I_CGROUPWB;
832 ctx->ops = &bdev_sops;
833 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834}
835
836static struct file_system_type bd_type = {
837 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000838 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 .kill_sb = kill_anon_super,
840};
841
Tejun Heoa212b102015-05-22 17:13:33 -0400842struct super_block *blockdev_superblock __read_mostly;
843EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845void __init bdev_cache_init(void)
846{
847 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300848 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800851 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800852 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900853 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 err = register_filesystem(&bd_type);
855 if (err)
856 panic("Cannot register bdev pseudo-fs");
857 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 if (IS_ERR(bd_mnt))
859 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300860 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863/*
864 * Most likely _very_ bad one - but then it's hardly critical for small
865 * /dev and can be fixed when somebody will need really large one.
866 * Keep in mind that it will be fed through icache hash function too.
867 */
868static inline unsigned long hash(dev_t dev)
869{
870 return MAJOR(dev)+MINOR(dev);
871}
872
873static int bdev_test(struct inode *inode, void *data)
874{
875 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
876}
877
878static int bdev_set(struct inode *inode, void *data)
879{
880 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
881 return 0;
882}
883
884static LIST_HEAD(all_bdevs);
885
Jan Karaf44f1ab2017-02-02 15:56:49 +0100886/*
887 * If there is a bdev inode for this device, unhash it so that it gets evicted
888 * as soon as last inode reference is dropped.
889 */
890void bdev_unhash_inode(dev_t dev)
891{
892 struct inode *inode;
893
894 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
895 if (inode) {
896 remove_inode_hash(inode);
897 iput(inode);
898 }
899}
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901struct block_device *bdget(dev_t dev)
902{
903 struct block_device *bdev;
904 struct inode *inode;
905
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800906 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 bdev_test, bdev_set, &dev);
908
909 if (!inode)
910 return NULL;
911
912 bdev = &BDEV_I(inode)->bdev;
913
914 if (inode->i_state & I_NEW) {
915 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000916 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 bdev->bd_inode = inode;
Fabian Frederick93407472017-02-27 14:28:32 -0800918 bdev->bd_block_size = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 bdev->bd_part_count = 0;
920 bdev->bd_invalidated = 0;
921 inode->i_mode = S_IFBLK;
922 inode->i_rdev = dev;
923 inode->i_bdev = bdev;
924 inode->i_data.a_ops = &def_blk_aops;
925 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 spin_lock(&bdev_lock);
927 list_add(&bdev->bd_list, &all_bdevs);
928 spin_unlock(&bdev_lock);
929 unlock_new_inode(inode);
930 }
931 return bdev;
932}
933
934EXPORT_SYMBOL(bdget);
935
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200936/**
937 * bdgrab -- Grab a reference to an already referenced block device
938 * @bdev: Block device to grab a reference to.
939 */
940struct block_device *bdgrab(struct block_device *bdev)
941{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400942 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200943 return bdev;
944}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700945EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200946
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947long nr_blockdev_pages(void)
948{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700949 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 long ret = 0;
951 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700952 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 ret += bdev->bd_inode->i_mapping->nrpages;
954 }
955 spin_unlock(&bdev_lock);
956 return ret;
957}
958
959void bdput(struct block_device *bdev)
960{
961 iput(bdev->bd_inode);
962}
963
964EXPORT_SYMBOL(bdput);
965
966static struct block_device *bd_acquire(struct inode *inode)
967{
968 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700969
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 spin_lock(&bdev_lock);
971 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +0100972 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100973 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 spin_unlock(&bdev_lock);
975 return bdev;
976 }
977 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700978
Jan Karacccd9fb2017-02-21 18:09:48 +0100979 /*
980 * i_bdev references block device inode that was already shut down
981 * (corresponding device got removed). Remove the reference and look
982 * up block device inode again just in case new device got
983 * reestablished under the same device number.
984 */
985 if (bdev)
986 bd_forget(inode);
987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 bdev = bdget(inode->i_rdev);
989 if (bdev) {
990 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700991 if (!inode->i_bdev) {
992 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -0400993 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700994 * and it's released in clear_inode() of inode.
995 * So, we can access it via ->i_mapping always
996 * without igrab().
997 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100998 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700999 inode->i_bdev = bdev;
1000 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002 spin_unlock(&bdev_lock);
1003 }
1004 return bdev;
1005}
1006
1007/* Call when you free inode */
1008
1009void bd_forget(struct inode *inode)
1010{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001011 struct block_device *bdev = NULL;
1012
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001014 if (!sb_is_blkdev_sb(inode->i_sb))
1015 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001016 inode->i_bdev = NULL;
1017 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001019
1020 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001021 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001024/**
1025 * bd_may_claim - test whether a block device can be claimed
1026 * @bdev: block device of interest
1027 * @whole: whole block device containing @bdev, may equal @bdev
1028 * @holder: holder trying to claim @bdev
1029 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001030 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001031 *
1032 * CONTEXT:
1033 * spin_lock(&bdev_lock).
1034 *
1035 * RETURNS:
1036 * %true if @bdev can be claimed, %false otherwise.
1037 */
1038static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1039 void *holder)
1040{
1041 if (bdev->bd_holder == holder)
1042 return true; /* already a holder */
1043 else if (bdev->bd_holder != NULL)
1044 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001045 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001046 return true; /* is a whole device which isn't held */
1047
Tejun Heoe525fd82010-11-13 11:55:17 +01001048 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001049 return true; /* is a partition of a device that is being partitioned */
1050 else if (whole->bd_holder != NULL)
1051 return false; /* is a partition of a held device */
1052 else
1053 return true; /* is a partition of an un-held device */
1054}
1055
1056/**
Tejun Heo6b4517a2010-04-07 18:53:59 +09001057 * bd_prepare_to_claim - prepare to claim a block device
1058 * @bdev: block device of interest
1059 * @whole: the whole device containing @bdev, may equal @bdev
1060 * @holder: holder trying to claim @bdev
1061 *
1062 * Prepare to claim @bdev. This function fails if @bdev is already
1063 * claimed by another holder and waits if another claiming is in
1064 * progress. This function doesn't actually claim. On successful
1065 * return, the caller has ownership of bd_claiming and bd_holder[s].
1066 *
1067 * CONTEXT:
1068 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1069 * it multiple times.
1070 *
1071 * RETURNS:
1072 * 0 if @bdev can be claimed, -EBUSY otherwise.
1073 */
1074static int bd_prepare_to_claim(struct block_device *bdev,
1075 struct block_device *whole, void *holder)
1076{
1077retry:
1078 /* if someone else claimed, fail */
1079 if (!bd_may_claim(bdev, whole, holder))
1080 return -EBUSY;
1081
Tejun Heoe75aa852010-08-04 17:59:39 +02001082 /* if claiming is already in progress, wait for it to finish */
1083 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001084 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1085 DEFINE_WAIT(wait);
1086
1087 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1088 spin_unlock(&bdev_lock);
1089 schedule();
1090 finish_wait(wq, &wait);
1091 spin_lock(&bdev_lock);
1092 goto retry;
1093 }
1094
1095 /* yay, all mine */
1096 return 0;
1097}
1098
Jan Kara560e7cb2018-02-26 13:01:42 +01001099static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1100{
1101 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1102
1103 if (!disk)
1104 return NULL;
1105 /*
1106 * Now that we hold gendisk reference we make sure bdev we looked up is
1107 * not stale. If it is, it means device got removed and created before
1108 * we looked up gendisk and we fail open in such case. Associating
1109 * unhashed bdev with newly created gendisk could lead to two bdevs
1110 * (and thus two independent caches) being associated with one device
1111 * which is bad.
1112 */
1113 if (inode_unhashed(bdev->bd_inode)) {
1114 put_disk_and_module(disk);
1115 return NULL;
1116 }
1117 return disk;
1118}
1119
Tejun Heo6b4517a2010-04-07 18:53:59 +09001120/**
1121 * bd_start_claiming - start claiming a block device
1122 * @bdev: block device of interest
1123 * @holder: holder trying to claim @bdev
1124 *
1125 * @bdev is about to be opened exclusively. Check @bdev can be opened
1126 * exclusively and mark that an exclusive open is in progress. Each
1127 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +10001128 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1129 * fail).
1130 *
1131 * This function is used to gain exclusive access to the block device
1132 * without actually causing other exclusive open attempts to fail. It
1133 * should be used when the open sequence itself requires exclusive
1134 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +09001135 *
1136 * CONTEXT:
1137 * Might sleep.
1138 *
1139 * RETURNS:
1140 * Pointer to the block device containing @bdev on success, ERR_PTR()
1141 * value on failure.
1142 */
Jan Kara89e524c02019-07-30 13:10:14 +02001143struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001144{
1145 struct gendisk *disk;
1146 struct block_device *whole;
1147 int partno, err;
1148
1149 might_sleep();
1150
1151 /*
1152 * @bdev might not have been initialized properly yet, look up
1153 * and grab the outer block device the hard way.
1154 */
Jan Kara560e7cb2018-02-26 13:01:42 +01001155 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001156 if (!disk)
1157 return ERR_PTR(-ENXIO);
1158
Tejun Heod4c208b2011-06-13 12:45:48 +02001159 /*
1160 * Normally, @bdev should equal what's returned from bdget_disk()
1161 * if partno is 0; however, some drivers (floppy) use multiple
1162 * bdev's for the same physical device and @bdev may be one of the
1163 * aliases. Keep @bdev if partno is 0. This means claimer
1164 * tracking is broken for those devices but it has always been that
1165 * way.
1166 */
1167 if (partno)
1168 whole = bdget_disk(disk, 0);
1169 else
1170 whole = bdgrab(bdev);
1171
Jan Kara9df6c292018-02-26 13:01:39 +01001172 put_disk_and_module(disk);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001173 if (!whole)
1174 return ERR_PTR(-ENOMEM);
1175
1176 /* prepare to claim, if successful, mark claiming in progress */
1177 spin_lock(&bdev_lock);
1178
1179 err = bd_prepare_to_claim(bdev, whole, holder);
1180 if (err == 0) {
1181 whole->bd_claiming = holder;
1182 spin_unlock(&bdev_lock);
1183 return whole;
1184 } else {
1185 spin_unlock(&bdev_lock);
1186 bdput(whole);
1187 return ERR_PTR(err);
1188 }
1189}
Jan Kara89e524c02019-07-30 13:10:14 +02001190EXPORT_SYMBOL(bd_start_claiming);
1191
1192static void bd_clear_claiming(struct block_device *whole, void *holder)
1193{
1194 lockdep_assert_held(&bdev_lock);
1195 /* tell others that we're done */
1196 BUG_ON(whole->bd_claiming != holder);
1197 whole->bd_claiming = NULL;
1198 wake_up_bit(&whole->bd_claiming, 0);
1199}
1200
1201/**
1202 * bd_finish_claiming - finish claiming of a block device
1203 * @bdev: block device of interest
1204 * @whole: whole block device (returned from bd_start_claiming())
1205 * @holder: holder that has claimed @bdev
1206 *
1207 * Finish exclusive open of a block device. Mark the device as exlusively
1208 * open by the holder and wake up all waiters for exclusive open to finish.
1209 */
1210void bd_finish_claiming(struct block_device *bdev, struct block_device *whole,
1211 void *holder)
1212{
1213 spin_lock(&bdev_lock);
1214 BUG_ON(!bd_may_claim(bdev, whole, holder));
1215 /*
1216 * Note that for a whole device bd_holders will be incremented twice,
1217 * and bd_holder will be set to bd_may_claim before being set to holder
1218 */
1219 whole->bd_holders++;
1220 whole->bd_holder = bd_may_claim;
1221 bdev->bd_holders++;
1222 bdev->bd_holder = holder;
1223 bd_clear_claiming(whole, holder);
1224 spin_unlock(&bdev_lock);
1225}
1226EXPORT_SYMBOL(bd_finish_claiming);
1227
1228/**
1229 * bd_abort_claiming - abort claiming of a block device
1230 * @bdev: block device of interest
1231 * @whole: whole block device (returned from bd_start_claiming())
1232 * @holder: holder that has claimed @bdev
1233 *
1234 * Abort claiming of a block device when the exclusive open failed. This can be
1235 * also used when exclusive open is not actually desired and we just needed
1236 * to block other exclusive openers for a while.
1237 */
1238void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1239 void *holder)
1240{
1241 spin_lock(&bdev_lock);
1242 bd_clear_claiming(whole, holder);
1243 spin_unlock(&bdev_lock);
1244}
1245EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001246
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001247#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001248struct bd_holder_disk {
1249 struct list_head list;
1250 struct gendisk *disk;
1251 int refcnt;
1252};
1253
1254static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1255 struct gendisk *disk)
1256{
1257 struct bd_holder_disk *holder;
1258
1259 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1260 if (holder->disk == disk)
1261 return holder;
1262 return NULL;
1263}
1264
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001265static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001266{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001267 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001268}
1269
1270static void del_symlink(struct kobject *from, struct kobject *to)
1271{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001272 sysfs_remove_link(from, kobject_name(to));
1273}
1274
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001275/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001276 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1277 * @bdev: the claimed slave bdev
1278 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001279 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001280 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1281 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001282 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001283 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001284 * - from "slaves" directory of the holder @disk to the claimed @bdev
1285 * - from "holders" directory of the @bdev to the holder @disk
1286 *
1287 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1288 * passed to bd_link_disk_holder(), then:
1289 *
1290 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1291 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1292 *
1293 * The caller must have claimed @bdev before calling this function and
1294 * ensure that both @bdev and @disk are valid during the creation and
1295 * lifetime of these symlinks.
1296 *
1297 * CONTEXT:
1298 * Might sleep.
1299 *
1300 * RETURNS:
1301 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001302 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001303int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001304{
Tejun Heo49731ba2011-01-14 18:43:57 +01001305 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001306 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001307
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001308 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001309
Tejun Heo49731ba2011-01-14 18:43:57 +01001310 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001311
Tejun Heoe09b4572010-11-13 11:55:17 +01001312 /* FIXME: remove the following once add_disk() handles errors */
1313 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1314 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001315
Tejun Heo49731ba2011-01-14 18:43:57 +01001316 holder = bd_find_holder_disk(bdev, disk);
1317 if (holder) {
1318 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001319 goto out_unlock;
1320 }
1321
Tejun Heo49731ba2011-01-14 18:43:57 +01001322 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1323 if (!holder) {
1324 ret = -ENOMEM;
1325 goto out_unlock;
1326 }
1327
1328 INIT_LIST_HEAD(&holder->list);
1329 holder->disk = disk;
1330 holder->refcnt = 1;
1331
1332 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1333 if (ret)
1334 goto out_free;
1335
1336 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1337 if (ret)
1338 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001339 /*
1340 * bdev could be deleted beneath us which would implicitly destroy
1341 * the holder directory. Hold on to it.
1342 */
1343 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001344
1345 list_add(&holder->list, &bdev->bd_holder_disks);
1346 goto out_unlock;
1347
1348out_del:
1349 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1350out_free:
1351 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001352out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001353 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001354 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001355}
Tejun Heoe09b4572010-11-13 11:55:17 +01001356EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001357
Tejun Heo49731ba2011-01-14 18:43:57 +01001358/**
1359 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1360 * @bdev: the calimed slave bdev
1361 * @disk: the holding disk
1362 *
1363 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1364 *
1365 * CONTEXT:
1366 * Might sleep.
1367 */
1368void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001369{
Tejun Heo49731ba2011-01-14 18:43:57 +01001370 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001371
Tejun Heo49731ba2011-01-14 18:43:57 +01001372 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001373
Tejun Heo49731ba2011-01-14 18:43:57 +01001374 holder = bd_find_holder_disk(bdev, disk);
1375
1376 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1377 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1378 del_symlink(bdev->bd_part->holder_dir,
1379 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001380 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001381 list_del_init(&holder->list);
1382 kfree(holder);
1383 }
1384
1385 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001386}
Tejun Heo49731ba2011-01-14 18:43:57 +01001387EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001388#endif
1389
Andrew Patterson0c002c22008-09-04 14:27:20 -06001390/**
Andrew Patterson56ade442008-09-04 14:27:40 -06001391 * flush_disk - invalidates all buffer-cache entries on a disk
1392 *
1393 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -08001394 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -06001395 *
1396 * Invalidates all buffer-cache entries on a disk. It should be called
1397 * when a disk has been changed -- either by a media change or online
1398 * resize.
1399 */
NeilBrown93b270f2011-02-24 17:25:47 +11001400static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -06001401{
NeilBrown93b270f2011-02-24 17:25:47 +11001402 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -06001403 printk(KERN_WARNING "VFS: busy inodes on changed media or "
Dmitry Monakhov424081f2015-04-13 16:31:34 +04001404 "resized disk %s\n",
1405 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
Andrew Patterson56ade442008-09-04 14:27:40 -06001406 }
Jan Karacba22d82019-10-21 10:38:00 +02001407 bdev->bd_invalidated = 1;
Andrew Patterson56ade442008-09-04 14:27:40 -06001408}
1409
1410/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001411 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001412 * @disk: struct gendisk to check
1413 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001414 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001415 *
1416 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001417 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1418 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001419 */
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001420static void check_disk_size_change(struct gendisk *disk,
1421 struct block_device *bdev, bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001422{
1423 loff_t disk_size, bdev_size;
1424
1425 disk_size = (loff_t)get_capacity(disk) << 9;
1426 bdev_size = i_size_read(bdev->bd_inode);
1427 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001428 if (verbose) {
1429 printk(KERN_INFO
1430 "%s: detected capacity change from %lld to %lld\n",
1431 disk->disk_name, bdev_size, disk_size);
1432 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001433 i_size_write(bdev->bd_inode, disk_size);
shunki-fujita849cf552018-04-05 16:20:07 -07001434 if (bdev_size > disk_size)
1435 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001436 }
Christoph Hellwig979c690d2019-11-14 15:34:37 +01001437 bdev->bd_invalidated = 0;
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001438}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001439
1440/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001441 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001442 * @disk: struct gendisk to be revalidated
1443 *
1444 * This routine is a wrapper for lower-level driver's revalidate_disk
1445 * call-backs. It is used to do common pre and post operations needed
1446 * for all revalidate_disk operations.
1447 */
1448int revalidate_disk(struct gendisk *disk)
1449{
1450 int ret = 0;
1451
1452 if (disk->fops->revalidate_disk)
1453 ret = disk->fops->revalidate_disk(disk);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001454
Jan Kara31cb1d62019-05-15 08:57:40 +02001455 /*
1456 * Hidden disks don't have associated bdev so there's no point in
1457 * revalidating it.
1458 */
1459 if (!(disk->flags & GENHD_FL_HIDDEN)) {
1460 struct block_device *bdev = bdget_disk(disk, 0);
1461
1462 if (!bdev)
1463 return ret;
1464
1465 mutex_lock(&bdev->bd_mutex);
1466 check_disk_size_change(disk, bdev, ret == 0);
Jan Kara31cb1d62019-05-15 08:57:40 +02001467 mutex_unlock(&bdev->bd_mutex);
1468 bdput(bdev);
1469 }
Andrew Patterson0c002c22008-09-04 14:27:20 -06001470 return ret;
1471}
1472EXPORT_SYMBOL(revalidate_disk);
1473
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474/*
1475 * This routine checks whether a removable media has been changed,
1476 * and invalidates all buffer-cache-entries in that case. This
1477 * is a relatively slow routine, so we have to try to minimize using
1478 * it. Thus it is called only upon a 'mount' or 'open'. This
1479 * is the best way of combining speed and utility, I think.
1480 * People changing diskettes in the middle of an operation deserve
1481 * to lose :-)
1482 */
1483int check_disk_change(struct block_device *bdev)
1484{
1485 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001486 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001487 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Tejun Heo77ea8872010-12-08 20:57:37 +01001489 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1490 DISK_EVENT_EJECT_REQUEST);
1491 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493
NeilBrown93b270f2011-02-24 17:25:47 +11001494 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 if (bdops->revalidate_disk)
1496 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 return 1;
1498}
1499
1500EXPORT_SYMBOL(check_disk_change);
1501
1502void bd_set_size(struct block_device *bdev, loff_t size)
1503{
Al Viro59551022016-01-22 15:40:57 -05001504 inode_lock(bdev->bd_inode);
Guo Chaod646a022013-02-21 15:16:42 -08001505 i_size_write(bdev->bd_inode, size);
Al Viro59551022016-01-22 15:40:57 -05001506 inode_unlock(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507}
1508EXPORT_SYMBOL(bd_set_size);
1509
Al Viro4385bab2013-05-05 22:11:03 -04001510static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001511
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001512int bdev_disk_changed(struct block_device *bdev, bool invalidate)
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001513{
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001514 struct gendisk *disk = bdev->bd_disk;
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001515 int ret;
1516
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001517 lockdep_assert_held(&bdev->bd_mutex);
1518
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001519rescan:
1520 ret = blk_drop_partitions(disk, bdev);
1521 if (ret)
1522 return ret;
1523
Christoph Hellwigd981cb52020-03-18 09:12:06 +01001524 /*
1525 * Historically we only set the capacity to zero for devices that
1526 * support partitions (independ of actually having partitions created).
1527 * Doing that is rather inconsistent, but changing it broke legacy
1528 * udisks polling for legacy ide-cdrom devices. Use the crude check
1529 * below to get the sane behavior for most device while not breaking
1530 * userspace for this particular setup.
1531 */
1532 if (invalidate) {
1533 if (disk_part_scan_enabled(disk) ||
1534 !(disk->flags & GENHD_FL_REMOVABLE))
1535 set_capacity(disk, 0);
1536 } else {
1537 if (disk->fops->revalidate_disk)
1538 disk->fops->revalidate_disk(disk);
1539 }
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001540
1541 check_disk_size_change(disk, bdev, !invalidate);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001542
Christoph Hellwig142fe8f2019-11-14 15:34:35 +01001543 if (get_capacity(disk)) {
1544 ret = blk_add_partitions(disk, bdev);
1545 if (ret == -EAGAIN)
1546 goto rescan;
Eric Biggers490547c2019-12-02 10:21:34 -08001547 } else if (invalidate) {
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001548 /*
1549 * Tell userspace that the media / partition table may have
1550 * changed.
1551 */
1552 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001553 }
1554
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001555 return ret;
1556}
Christoph Hellwigf0b870d2019-11-14 15:34:36 +01001557/*
1558 * Only exported for for loop and dasd for historic reasons. Don't use in new
1559 * code!
1560 */
1561EXPORT_SYMBOL_GPL(bdev_disk_changed);
Christoph Hellwiga1548b62019-11-14 15:34:34 +01001562
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001563/*
1564 * bd_mutex locking:
1565 *
1566 * mutex_lock(part->bd_mutex)
1567 * mutex_lock_nested(whole->bd_mutex, 1)
1568 */
1569
Al Viro572c4892007-10-08 13:24:05 -04001570static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001573 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001574 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001575 int perm = 0;
Jan Kara89736652018-02-26 13:01:40 +01001576 bool first_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Al Viro572c4892007-10-08 13:24:05 -04001578 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001579 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001580 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001581 perm |= MAY_WRITE;
1582 /*
1583 * hooks: /n/, see "layering violations".
1584 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001585 if (!for_part) {
1586 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1587 if (ret != 0) {
1588 bdput(bdev);
1589 return ret;
1590 }
Al Viro82666022008-08-01 05:32:04 -04001591 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001592
NeilBrownd3374822009-01-09 08:31:10 +11001593 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001594
Tejun Heo89f97492008-11-05 10:21:06 +01001595 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001596 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001597 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001598 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Tejun Heo69e02c52011-03-09 19:54:27 +01001600 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001601 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001603 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 bdev->bd_disk = disk;
Andi Kleen87192a22012-01-12 17:20:34 -08001605 bdev->bd_queue = disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001607 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001608
Tejun Heocf771cb2008-09-03 09:01:09 +02001609 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001610 ret = -ENXIO;
1611 bdev->bd_part = disk_get_part(disk, partno);
1612 if (!bdev->bd_part)
1613 goto out_clear;
1614
Tejun Heo1196f8b2011-04-21 20:54:45 +02001615 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001617 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001618 if (ret == -ERESTARTSYS) {
1619 /* Lost a race with 'disk' being
1620 * deleted, try again.
1621 * See md.c
1622 */
1623 disk_put_part(bdev->bd_part);
1624 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001625 bdev->bd_disk = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001626 bdev->bd_queue = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001627 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001628 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001629 put_disk_and_module(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001630 goto restart;
1631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
Tejun Heo7e697232011-05-23 13:26:07 +02001633
Jan Kara04906b22019-01-14 09:48:10 +01001634 if (!ret) {
Tejun Heo7e697232011-05-23 13:26:07 +02001635 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
Jan Kara04906b22019-01-14 09:48:10 +01001636 set_init_blocksize(bdev);
1637 }
Tejun Heo7e697232011-05-23 13:26:07 +02001638
Tejun Heo1196f8b2011-04-21 20:54:45 +02001639 /*
1640 * If the device is invalidated, rescan partition
1641 * if open succeeded or failed with -ENOMEDIUM.
1642 * The latter is necessary to prevent ghost
1643 * partitions on a removed medium.
1644 */
Jan Kara731dc482019-10-21 10:37:59 +02001645 if (bdev->bd_invalidated &&
1646 (!ret || ret == -ENOMEDIUM))
1647 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Dan Williams5a023cd2015-11-30 10:20:29 -08001648
Tejun Heo1196f8b2011-04-21 20:54:45 +02001649 if (ret)
1650 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 struct block_device *whole;
1653 whole = bdget_disk(disk, 0);
1654 ret = -ENOMEM;
1655 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001656 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001657 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001658 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001660 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 bdev->bd_contains = whole;
Tejun Heo89f97492008-11-05 10:21:06 +01001662 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001663 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001664 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001666 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Tejun Heo89f97492008-11-05 10:21:06 +01001668 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Jan Kara04906b22019-01-14 09:48:10 +01001669 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
Jan Kara03e26272017-03-23 01:36:53 +01001671
1672 if (bdev->bd_bdi == &noop_backing_dev_info)
1673 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001676 ret = 0;
1677 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001678 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001679 /* the same as first opener case, read comment there */
Jan Kara731dc482019-10-21 10:37:59 +02001680 if (bdev->bd_invalidated &&
1681 (!ret || ret == -ENOMEDIUM))
1682 bdev_disk_changed(bdev, ret == -ENOMEDIUM);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001683 if (ret)
1684 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 }
1687 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001688 if (for_part)
1689 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001690 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001691 disk_unblock_events(disk);
Jan Kara89736652018-02-26 13:01:40 +01001692 /* only one opener holds refs to the module and disk */
1693 if (!first_open)
1694 put_disk_and_module(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696
Tejun Heo0762b8b2008-08-25 19:56:12 +09001697 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001698 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001700 bdev->bd_part = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001701 bdev->bd_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001703 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001705 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001706 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001707 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001708 put_disk_and_module(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001709 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001710 bdput(bdev);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return ret;
1713}
1714
Tejun Heod4d77622010-11-13 11:55:18 +01001715/**
1716 * blkdev_get - open a block device
1717 * @bdev: block_device to open
1718 * @mode: FMODE_* mask
1719 * @holder: exclusive holder identifier
1720 *
1721 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1722 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1723 * @holder is invalid. Exclusive opens may nest for the same @holder.
1724 *
1725 * On success, the reference count of @bdev is unchanged. On failure,
1726 * @bdev is put.
1727 *
1728 * CONTEXT:
1729 * Might sleep.
1730 *
1731 * RETURNS:
1732 * 0 on success, -errno on failure.
1733 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001734int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Tejun Heoe525fd82010-11-13 11:55:17 +01001736 struct block_device *whole = NULL;
1737 int res;
1738
1739 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1740
1741 if ((mode & FMODE_EXCL) && holder) {
1742 whole = bd_start_claiming(bdev, holder);
1743 if (IS_ERR(whole)) {
1744 bdput(bdev);
1745 return PTR_ERR(whole);
1746 }
1747 }
1748
1749 res = __blkdev_get(bdev, mode, 0);
1750
1751 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001752 struct gendisk *disk = whole->bd_disk;
1753
Tejun Heo6a027ef2010-11-13 11:55:17 +01001754 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001755 mutex_lock(&bdev->bd_mutex);
Jan Karae91455b2019-08-07 11:36:47 +02001756 if (!res)
1757 bd_finish_claiming(bdev, whole, holder);
1758 else
1759 bd_abort_claiming(bdev, whole, holder);
Tejun Heo77ea8872010-12-08 20:57:37 +01001760 /*
Tejun Heod4dc2102011-04-21 20:54:46 +02001761 * Block event polling for write claims if requested. Any
1762 * write holder makes the write_holder state stick until
1763 * all are released. This is good enough and tracking
1764 * individual writeable reference is too fragile given the
1765 * way @mode is used in blkdev_get/put().
Tejun Heo77ea8872010-12-08 20:57:37 +01001766 */
Tejun Heo4c49ff32011-06-01 08:27:41 +02001767 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1768 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001769 bdev->bd_write_holder = true;
Tejun Heod4dc2102011-04-21 20:54:46 +02001770 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001771 }
1772
1773 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001774 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001775 }
1776
1777 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001778}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779EXPORT_SYMBOL(blkdev_get);
1780
Tejun Heod4d77622010-11-13 11:55:18 +01001781/**
1782 * blkdev_get_by_path - open a block device by name
1783 * @path: path to the block device to open
1784 * @mode: FMODE_* mask
1785 * @holder: exclusive holder identifier
1786 *
1787 * Open the blockdevice described by the device file at @path. @mode
1788 * and @holder are identical to blkdev_get().
1789 *
1790 * On success, the returned block_device has reference count of one.
1791 *
1792 * CONTEXT:
1793 * Might sleep.
1794 *
1795 * RETURNS:
1796 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1797 */
1798struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1799 void *holder)
1800{
1801 struct block_device *bdev;
1802 int err;
1803
1804 bdev = lookup_bdev(path);
1805 if (IS_ERR(bdev))
1806 return bdev;
1807
1808 err = blkdev_get(bdev, mode, holder);
1809 if (err)
1810 return ERR_PTR(err);
1811
Chuck Ebberte51900f2011-02-16 18:11:53 -05001812 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1813 blkdev_put(bdev, mode);
1814 return ERR_PTR(-EACCES);
1815 }
1816
Tejun Heod4d77622010-11-13 11:55:18 +01001817 return bdev;
1818}
1819EXPORT_SYMBOL(blkdev_get_by_path);
1820
1821/**
1822 * blkdev_get_by_dev - open a block device by device number
1823 * @dev: device number of block device to open
1824 * @mode: FMODE_* mask
1825 * @holder: exclusive holder identifier
1826 *
1827 * Open the blockdevice described by device number @dev. @mode and
1828 * @holder are identical to blkdev_get().
1829 *
1830 * Use it ONLY if you really do not have anything better - i.e. when
1831 * you are behind a truly sucky interface and all you are given is a
1832 * device number. _Never_ to be used for internal purposes. If you
1833 * ever need it - reconsider your API.
1834 *
1835 * On success, the returned block_device has reference count of one.
1836 *
1837 * CONTEXT:
1838 * Might sleep.
1839 *
1840 * RETURNS:
1841 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1842 */
1843struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1844{
1845 struct block_device *bdev;
1846 int err;
1847
1848 bdev = bdget(dev);
1849 if (!bdev)
1850 return ERR_PTR(-ENOMEM);
1851
1852 err = blkdev_get(bdev, mode, holder);
1853 if (err)
1854 return ERR_PTR(err);
1855
1856 return bdev;
1857}
1858EXPORT_SYMBOL(blkdev_get_by_dev);
1859
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860static int blkdev_open(struct inode * inode, struct file * filp)
1861{
1862 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864 /*
1865 * Preserve backwards compatibility and allow large file access
1866 * even if userspace doesn't ask for it explicitly. Some mkfs
1867 * binary needs it. We might want to drop this workaround
1868 * during an unstable branch.
1869 */
1870 filp->f_flags |= O_LARGEFILE;
1871
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001872 filp->f_mode |= FMODE_NOWAIT;
1873
Al Viro572c4892007-10-08 13:24:05 -04001874 if (filp->f_flags & O_NDELAY)
1875 filp->f_mode |= FMODE_NDELAY;
1876 if (filp->f_flags & O_EXCL)
1877 filp->f_mode |= FMODE_EXCL;
1878 if ((filp->f_flags & O_ACCMODE) == 3)
1879 filp->f_mode |= FMODE_WRITE_IOCTL;
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001882 if (bdev == NULL)
1883 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Al Viro572c4892007-10-08 13:24:05 -04001885 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001886 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Al Viro572c4892007-10-08 13:24:05 -04001887
Tejun Heoe525fd82010-11-13 11:55:17 +01001888 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889}
1890
Al Viro4385bab2013-05-05 22:11:03 -04001891static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001892{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001893 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001894 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001895
NeilBrown6796bf52006-12-08 02:36:16 -08001896 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001897 if (for_part)
1898 bdev->bd_part_count--;
1899
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001900 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001901 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001902 sync_blockdev(bdev);
1903 kill_bdev(bdev);
Ilya Dryomov43d1c0e2015-11-20 22:22:34 +01001904
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001905 bdev_write_inode(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001906 }
1907 if (bdev->bd_contains == bdev) {
1908 if (disk->fops->release)
Al Virodb2a1442013-05-05 21:52:57 -04001909 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001910 }
1911 if (!bdev->bd_openers) {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001912 disk_put_part(bdev->bd_part);
1913 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001914 bdev->bd_disk = NULL;
NeilBrown37be4122006-12-08 02:36:16 -08001915 if (bdev != bdev->bd_contains)
1916 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001917 bdev->bd_contains = NULL;
Tejun Heo523e1d32011-10-19 14:31:07 +02001918
Jan Kara9df6c292018-02-26 13:01:39 +01001919 put_disk_and_module(disk);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001920 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001921 mutex_unlock(&bdev->bd_mutex);
1922 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001923 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001924 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001925}
1926
Al Viro4385bab2013-05-05 22:11:03 -04001927void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001928{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001929 mutex_lock(&bdev->bd_mutex);
1930
Tejun Heoe525fd82010-11-13 11:55:17 +01001931 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001932 bool bdev_free;
1933
1934 /*
1935 * Release a claim on the device. The holder fields
1936 * are protected with bdev_lock. bd_mutex is to
1937 * synchronize disk_holder unlinking.
1938 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001939 spin_lock(&bdev_lock);
1940
1941 WARN_ON_ONCE(--bdev->bd_holders < 0);
1942 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1943
1944 /* bd_contains might point to self, check in a separate step */
1945 if ((bdev_free = !bdev->bd_holders))
1946 bdev->bd_holder = NULL;
1947 if (!bdev->bd_contains->bd_holders)
1948 bdev->bd_contains->bd_holder = NULL;
1949
1950 spin_unlock(&bdev_lock);
1951
Tejun Heo77ea8872010-12-08 20:57:37 +01001952 /*
1953 * If this was the last claim, remove holder link and
1954 * unblock evpoll if it was a write holder.
1955 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001956 if (bdev_free && bdev->bd_write_holder) {
1957 disk_unblock_events(bdev->bd_disk);
1958 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001959 }
Tejun Heo69362172011-03-09 19:54:27 +01001960 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001961
Tejun Heo85ef06d2011-07-01 16:17:47 +02001962 /*
1963 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1964 * event. This is to ensure detection of media removal commanded
1965 * from userland - e.g. eject(1).
1966 */
1967 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1968
1969 mutex_unlock(&bdev->bd_mutex);
1970
Al Viro4385bab2013-05-05 22:11:03 -04001971 __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001972}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001973EXPORT_SYMBOL(blkdev_put);
1974
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975static int blkdev_close(struct inode * inode, struct file * filp)
1976{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001977 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001978 blkdev_put(bdev, filp->f_mode);
1979 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001982static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001984 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001985 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001986
1987 /*
1988 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1989 * to updated it before every ioctl.
1990 */
Al Viro56b26ad2008-09-19 03:17:36 -04001991 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001992 mode |= FMODE_NDELAY;
1993 else
1994 mode &= ~FMODE_NDELAY;
1995
Al Viro56b26ad2008-09-19 03:17:36 -04001996 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997}
1998
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001999/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02002000 * Write data to the block device. Only intended for the block device itself
2001 * and the raw driver which basically is a fake block device.
2002 *
2003 * Does not take i_mutex for the write and thus is not for general purpose
2004 * use.
2005 */
Al Viro1456c0a2014-04-03 03:21:50 -04002006ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02002007{
2008 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002009 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04002010 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002011 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02002012 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04002013
Al Viro7ec7b942015-04-07 11:35:14 -04002014 if (bdev_read_only(I_BDEV(bd_inode)))
2015 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04002016
Domenico Andreoli56939e02020-03-23 08:22:15 -07002017 /* uswsusp needs write permission to the swap */
2018 if (IS_SWAPFILE(bd_inode) && !hibernation_available())
Darrick J. Wongdc617f22019-08-20 07:55:16 -07002019 return -ETXTBSY;
2020
Al Viro7ec7b942015-04-07 11:35:14 -04002021 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04002022 return 0;
2023
Al Viro7ec7b942015-04-07 11:35:14 -04002024 if (iocb->ki_pos >= size)
2025 return -ENOSPC;
2026
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02002027 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
2028 return -EOPNOTSUPP;
2029
Al Viro7ec7b942015-04-07 11:35:14 -04002030 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002031
Jianpeng Ma53362a02012-08-02 09:50:39 +02002032 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04002033 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07002034 if (ret > 0)
2035 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002036 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002037 return ret;
2038}
Al Viro1456c0a2014-04-03 03:21:50 -04002039EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002040
David Jefferyb2de5252014-09-29 10:21:10 -04002041ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002042{
2043 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002044 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002045 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04002046 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002047
2048 if (pos >= size)
2049 return 0;
2050
2051 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04002052 iov_iter_truncate(to, size);
2053 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002054}
David Jefferyb2de5252014-09-29 10:21:10 -04002055EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002056
Christoph Hellwigeef99382009-08-20 17:43:41 +02002057/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002058 * Try to release a page associated with block device when the system
2059 * is under memory pressure.
2060 */
2061static int blkdev_releasepage(struct page *page, gfp_t wait)
2062{
2063 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
2064
2065 if (super && super->s_op->bdev_try_to_free_page)
2066 return super->s_op->bdev_try_to_free_page(super, page, wait);
2067
2068 return try_to_free_buffers(page);
2069}
2070
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002071static int blkdev_writepages(struct address_space *mapping,
2072 struct writeback_control *wbc)
2073{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002074 return generic_writepages(mapping, wbc);
2075}
2076
Adrian Bunk4c54ac62008-02-18 13:48:31 +01002077static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 .readpage = blkdev_readpage,
Akinobu Mita447f05b2014-10-09 15:26:58 -07002079 .readpages = blkdev_readpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07002081 .write_begin = blkdev_write_begin,
2082 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002083 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002084 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08002086 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07002087 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088};
2089
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002090#define BLKDEV_FALLOC_FL_SUPPORTED \
2091 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
2092 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
2093
2094static long blkdev_fallocate(struct file *file, int mode, loff_t start,
2095 loff_t len)
2096{
2097 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002098 struct address_space *mapping;
2099 loff_t end = start + len - 1;
2100 loff_t isize;
2101 int error;
2102
2103 /* Fail if we don't recognize the flags. */
2104 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
2105 return -EOPNOTSUPP;
2106
2107 /* Don't go off the end of the device. */
2108 isize = i_size_read(bdev->bd_inode);
2109 if (start >= isize)
2110 return -EINVAL;
2111 if (end >= isize) {
2112 if (mode & FALLOC_FL_KEEP_SIZE) {
2113 len = isize - start;
2114 end = start + len - 1;
2115 } else
2116 return -EINVAL;
2117 }
2118
2119 /*
2120 * Don't allow IO that isn't aligned to logical block size.
2121 */
2122 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
2123 return -EINVAL;
2124
2125 /* Invalidate the page cache, including dirty pages. */
2126 mapping = bdev->bd_inode->i_mapping;
2127 truncate_inode_pages_range(mapping, start, end);
2128
2129 switch (mode) {
2130 case FALLOC_FL_ZERO_RANGE:
2131 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
2132 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02002133 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002134 break;
2135 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02002136 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2137 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002138 break;
2139 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002140 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2141 GFP_KERNEL, 0);
2142 break;
2143 default:
2144 return -EOPNOTSUPP;
2145 }
2146 if (error)
2147 return error;
2148
2149 /*
2150 * Invalidate again; if someone wandered in and dirtied a page,
2151 * the caller will be given -EBUSY. The third argument is
2152 * inclusive, so the rounding here is safe.
2153 */
2154 return invalidate_inode_pages2_range(mapping,
2155 start >> PAGE_SHIFT,
2156 end >> PAGE_SHIFT);
2157}
2158
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002159const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 .open = blkdev_open,
2161 .release = blkdev_close,
2162 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04002163 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04002164 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07002165 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07002166 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07002167 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07002168 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169#ifdef CONFIG_COMPAT
2170 .compat_ioctl = compat_blkdev_ioctl,
2171#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08002172 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002173 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002174 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175};
2176
2177int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
2178{
2179 int res;
2180 mm_segment_t old_fs = get_fs();
2181 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04002182 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 set_fs(old_fs);
2184 return res;
2185}
2186
2187EXPORT_SYMBOL(ioctl_by_bdev);
2188
2189/**
2190 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08002191 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02002193 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 * namespace if possible and return it. Return ERR_PTR(error)
2195 * otherwise.
2196 */
Al Viro421748e2008-08-02 01:04:36 -04002197struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198{
2199 struct block_device *bdev;
2200 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04002201 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 int error;
2203
Al Viro421748e2008-08-02 01:04:36 -04002204 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 return ERR_PTR(-EINVAL);
2206
Al Viro421748e2008-08-02 01:04:36 -04002207 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 if (error)
2209 return ERR_PTR(error);
2210
David Howellsbb6687342015-03-17 22:26:21 +00002211 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 error = -ENOTBLK;
2213 if (!S_ISBLK(inode->i_mode))
2214 goto fail;
2215 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002216 if (!may_open_dev(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto fail;
2218 error = -ENOMEM;
2219 bdev = bd_acquire(inode);
2220 if (!bdev)
2221 goto fail;
2222out:
Al Viro421748e2008-08-02 01:04:36 -04002223 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 return bdev;
2225fail:
2226 bdev = ERR_PTR(error);
2227 goto out;
2228}
Al Virod5686b42008-08-01 05:00:11 -04002229EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
NeilBrown93b270f2011-02-24 17:25:47 +11002231int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01002232{
2233 struct super_block *sb = get_super(bdev);
2234 int res = 0;
2235
2236 if (sb) {
2237 /*
2238 * no need to lock the super, get_super holds the
2239 * read mutex so the filesystem cannot go away
2240 * under us (->put_super runs with the write lock
2241 * hold).
2242 */
2243 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11002244 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01002245 drop_super(sb);
2246 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002247 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01002248 return res;
2249}
2250EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002251
2252void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2253{
2254 struct inode *inode, *old_inode = NULL;
2255
Dave Chinner74278da2015-03-04 12:37:22 -05002256 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002257 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2258 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01002259 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02002260
2261 spin_lock(&inode->i_lock);
2262 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2263 mapping->nrpages == 0) {
2264 spin_unlock(&inode->i_lock);
2265 continue;
2266 }
2267 __iget(inode);
2268 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05002269 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002270 /*
2271 * We hold a reference to 'inode' so it couldn't have been
2272 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05002273 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02002274 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05002275 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02002276 * later.
2277 */
2278 iput(old_inode);
2279 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01002280 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002281
Rabin Vincentaf309222016-12-01 09:18:28 +01002282 mutex_lock(&bdev->bd_mutex);
2283 if (bdev->bd_openers)
2284 func(bdev, arg);
2285 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002286
Dave Chinner74278da2015-03-04 12:37:22 -05002287 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002288 }
Dave Chinner74278da2015-03-04 12:37:22 -05002289 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002290 iput(old_inode);
2291}