blob: eb657ab9406062cdd23136e66fc265e472523a80 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/block_dev.c
4 *
5 * Copyright (C) 1991, 1992 Linus Torvalds
6 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7 */
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/init.h>
10#include <linux/mm.h>
11#include <linux/fcntl.h>
12#include <linux/slab.h>
13#include <linux/kmod.h>
14#include <linux/major.h>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070015#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/highmem.h>
17#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040018#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/module.h>
20#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070021#include <linux/magic.h>
Dan Williamsb0686262017-01-26 20:37:35 -080022#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040024#include <linux/swap.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010025#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010026#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/mpage.h>
28#include <linux/mount.h>
David Howells9030d162019-03-25 16:38:23 +000029#include <linux/pseudo_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/uio.h>
31#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070032#include <linux/log2.h>
Al Viroff01bb42011-09-16 02:31:11 -040033#include <linux/cleancache.h>
Christoph Hellwig189ce2b2016-10-31 11:59:25 -060034#include <linux/task_io_accounting_ops.h>
Darrick J. Wong25f4c412016-10-11 13:51:11 -070035#include <linux/falloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080036#include <linux/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020037#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39struct bdev_inode {
40 struct block_device bdev;
41 struct inode vfs_inode;
42};
43
Adrian Bunk4c54ac62008-02-18 13:48:31 +010044static const struct address_space_operations def_blk_aops;
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046static inline struct bdev_inode *BDEV_I(struct inode *inode)
47{
48 return container_of(inode, struct bdev_inode, vfs_inode);
49}
50
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020051struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070052{
53 return &BDEV_I(inode)->bdev;
54}
Linus Torvalds1da177e2005-04-16 15:20:36 -070055EXPORT_SYMBOL(I_BDEV);
56
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070057static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010058{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070059 struct inode *inode = bdev->bd_inode;
60 int ret;
61
Christoph Hellwig564f00f2015-01-14 10:42:33 +010062 spin_lock(&inode->i_lock);
63 while (inode->i_state & I_DIRTY) {
64 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070065 ret = write_inode_now(inode, true);
66 if (ret) {
67 char name[BDEVNAME_SIZE];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev, name), ret);
71 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010072 spin_lock(&inode->i_lock);
73 }
74 spin_unlock(&inode->i_lock);
75}
76
Peter Zijlstraf9a14392007-05-06 14:49:55 -070077/* Kill _all_ buffers and pagecache , dirty or not.. */
Al Viroff01bb42011-09-16 02:31:11 -040078void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
Al Viroff01bb42011-09-16 02:31:11 -040080 struct address_space *mapping = bdev->bd_inode->i_mapping;
81
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080082 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
Peter Zijlstraf9a14392007-05-06 14:49:55 -070083 return;
Al Viroff01bb42011-09-16 02:31:11 -040084
Peter Zijlstraf9a14392007-05-06 14:49:55 -070085 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040086 truncate_inode_pages(mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
Al Viroff01bb42011-09-16 02:31:11 -040088EXPORT_SYMBOL(kill_bdev);
89
90/* Invalidate clean unused buffers and pagecache. */
91void invalidate_bdev(struct block_device *bdev)
92{
93 struct address_space *mapping = bdev->bd_inode->i_mapping;
94
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070095 if (mapping->nrpages) {
96 invalidate_bh_lrus();
97 lru_add_drain_all(); /* make sure all lru add caches are flushed */
98 invalidate_mapping_pages(mapping, 0, -1);
99 }
Al Viroff01bb42011-09-16 02:31:11 -0400100 /* 99% of the time, we don't need to flush the cleancache on the bdev.
101 * But, for the strange corners, lets be cautious
102 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400103 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400104}
105EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Jan Kara04906b22019-01-14 09:48:10 +0100107static void set_init_blocksize(struct block_device *bdev)
108{
109 unsigned bsize = bdev_logical_block_size(bdev);
110 loff_t size = i_size_read(bdev->bd_inode);
111
112 while (bsize < PAGE_SIZE) {
113 if (size & bsize)
114 break;
115 bsize <<= 1;
116 }
117 bdev->bd_block_size = bsize;
118 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
119}
120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121int set_blocksize(struct block_device *bdev, int size)
122{
123 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700124 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return -EINVAL;
126
127 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400128 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 return -EINVAL;
130
131 /* Don't change the size if it is same as current */
132 if (bdev->bd_block_size != size) {
133 sync_blockdev(bdev);
134 bdev->bd_block_size = size;
135 bdev->bd_inode->i_blkbits = blksize_bits(size);
136 kill_bdev(bdev);
137 }
138 return 0;
139}
140
141EXPORT_SYMBOL(set_blocksize);
142
143int sb_set_blocksize(struct super_block *sb, int size)
144{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (set_blocksize(sb->s_bdev, size))
146 return 0;
147 /* If we get here, we know size is power of two
148 * and it's value is between 512 and PAGE_SIZE */
149 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800150 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 return sb->s_blocksize;
152}
153
154EXPORT_SYMBOL(sb_set_blocksize);
155
156int sb_min_blocksize(struct super_block *sb, int size)
157{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400158 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 if (size < minsize)
160 size = minsize;
161 return sb_set_blocksize(sb, size);
162}
163
164EXPORT_SYMBOL(sb_min_blocksize);
165
166static int
167blkdev_get_block(struct inode *inode, sector_t iblock,
168 struct buffer_head *bh, int create)
169{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 bh->b_bdev = I_BDEV(inode);
171 bh->b_blocknr = iblock;
172 set_buffer_mapped(bh);
173 return 0;
174}
175
Dan Williams4ebb16c2015-10-28 07:48:19 +0900176static struct inode *bdev_file_inode(struct file *file)
177{
178 return file->f_mapping->host;
179}
180
Jens Axboe78250c02016-11-17 17:50:47 +0100181static unsigned int dio_bio_write_op(struct kiocb *iocb)
182{
183 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
184
185 /* avoid the need for a I/O completion work item */
186 if (iocb->ki_flags & IOCB_DSYNC)
187 op |= REQ_FUA;
188 return op;
189}
190
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600191#define DIO_INLINE_BIO_VECS 4
192
193static void blkdev_bio_end_io_simple(struct bio *bio)
194{
195 struct task_struct *waiter = bio->bi_private;
196
197 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700198 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600199}
200
201static ssize_t
202__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
203 int nr_pages)
204{
205 struct file *file = iocb->ki_filp;
206 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200207 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600208 loff_t pos = iocb->ki_pos;
209 bool should_dirty = false;
210 struct bio bio;
211 ssize_t ret;
212 blk_qc_t qc;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600213
Jens Axboe9a794fb2016-11-22 08:12:39 -0700214 if ((pos | iov_iter_alignment(iter)) &
215 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600216 return -EINVAL;
217
Jens Axboe72ecad22016-11-16 23:11:42 -0700218 if (nr_pages <= DIO_INLINE_BIO_VECS)
219 vecs = inline_vecs;
220 else {
Kees Cook6da2ec52018-06-12 13:55:00 -0700221 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
222 GFP_KERNEL);
Jens Axboe72ecad22016-11-16 23:11:42 -0700223 if (!vecs)
224 return -ENOMEM;
225 }
226
Ming Lei3a83f462016-11-22 08:57:21 -0700227 bio_init(&bio, vecs, nr_pages);
Christoph Hellwig74d46992017-08-23 19:10:32 +0200228 bio_set_dev(&bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900229 bio.bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600230 bio.bi_write_hint = iocb->ki_hint;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600231 bio.bi_private = current;
232 bio.bi_end_io = blkdev_bio_end_io_simple;
Adam Manzanares074111c2018-05-22 10:52:20 -0700233 bio.bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600234
235 ret = bio_iov_iter_get_pages(&bio, iter);
236 if (unlikely(ret))
Martin Wilck9362dd12018-07-25 23:15:08 +0200237 goto out;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600238 ret = bio.bi_iter.bi_size;
239
240 if (iov_iter_rw(iter) == READ) {
Jens Axboe78250c02016-11-17 17:50:47 +0100241 bio.bi_opf = REQ_OP_READ;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600242 if (iter_is_iovec(iter))
243 should_dirty = true;
244 } else {
Jens Axboe78250c02016-11-17 17:50:47 +0100245 bio.bi_opf = dio_bio_write_op(iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600246 task_io_account_write(ret);
247 }
Jens Axboed1e36282018-08-29 10:36:56 -0600248 if (iocb->ki_flags & IOCB_HIPRI)
Jens Axboe0bbb2802018-12-21 09:10:46 -0700249 bio_set_polled(&bio, iocb);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600250
251 qc = submit_bio(&bio);
252 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800253 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600254 if (!READ_ONCE(bio.bi_private))
255 break;
256 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700257 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600258 io_schedule();
259 }
260 __set_current_state(TASK_RUNNING);
261
Christoph Hellwig9fec4a22019-06-26 15:49:26 +0200262 bio_release_pages(&bio, should_dirty);
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200263 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700264 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600265
Martin Wilck9362dd12018-07-25 23:15:08 +0200266out:
267 if (vecs != inline_vecs)
268 kfree(vecs);
269
Jens Axboe9ae3b3f52017-06-28 15:30:13 -0600270 bio_uninit(&bio);
271
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600272 return ret;
273}
274
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700275struct blkdev_dio {
276 union {
277 struct kiocb *iocb;
278 struct task_struct *waiter;
279 };
280 size_t size;
281 atomic_t ref;
282 bool multi_bio : 1;
283 bool should_dirty : 1;
284 bool is_sync : 1;
285 struct bio bio;
286};
287
Kent Overstreet52190f82018-05-20 18:25:55 -0400288static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700289
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700290static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
291{
292 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
293 struct request_queue *q = bdev_get_queue(bdev);
294
295 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
296}
297
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700298static void blkdev_bio_end_io(struct bio *bio)
299{
300 struct blkdev_dio *dio = bio->bi_private;
301 bool should_dirty = dio->should_dirty;
302
Jason Yana89afe52019-04-12 10:09:16 +0800303 if (bio->bi_status && !dio->bio.bi_status)
304 dio->bio.bi_status = bio->bi_status;
305
306 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700307 if (!dio->is_sync) {
308 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200309 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700310
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200311 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700312 ret = dio->size;
313 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200314 } else {
315 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700316 }
317
318 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100319 if (dio->multi_bio)
320 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700321 } else {
322 struct task_struct *waiter = dio->waiter;
323
324 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700325 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700326 }
327 }
328
329 if (should_dirty) {
330 bio_check_pages_dirty(bio);
331 } else {
Christoph Hellwig57dfe3c2019-06-26 15:49:25 +0200332 bio_release_pages(bio, false);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700333 bio_put(bio);
334 }
335}
336
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800337static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700338__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800339{
340 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900341 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700342 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100343 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700344 struct blkdev_dio *dio;
345 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700346 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100347 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Jens Axboe6a430742019-07-16 13:56:42 -0600348 bool nowait = (iocb->ki_flags & IOCB_NOWAIT) != 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700349 loff_t pos = iocb->ki_pos;
350 blk_qc_t qc = BLK_QC_T_NONE;
Jens Axboe6a430742019-07-16 13:56:42 -0600351 gfp_t gfp;
Jens Axboee15c2ff2019-08-06 13:34:31 -0600352 int ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700353
Jens Axboe9a794fb2016-11-22 08:12:39 -0700354 if ((pos | iov_iter_alignment(iter)) &
355 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700356 return -EINVAL;
357
Jens Axboe6a430742019-07-16 13:56:42 -0600358 if (nowait)
359 gfp = GFP_NOWAIT;
360 else
361 gfp = GFP_KERNEL;
362
363 bio = bio_alloc_bioset(gfp, nr_pages, &blkdev_dio_pool);
364 if (!bio)
365 return -EAGAIN;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700366
367 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100368 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100369 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700370 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100371 bio_get(bio);
372 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700373 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100374 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700375
376 dio->size = 0;
377 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100378 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700379
Jens Axboecb700eb2018-11-15 19:56:53 -0700380 /*
381 * Don't plug for HIPRI/polled IO, as those should go straight
382 * to issue
383 */
384 if (!is_poll)
385 blk_start_plug(&plug);
386
Jens Axboe6a430742019-07-16 13:56:42 -0600387 ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700388 for (;;) {
Christoph Hellwig74d46992017-08-23 19:10:32 +0200389 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900390 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600391 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700392 bio->bi_private = dio;
393 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700394 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700395
Jens Axboee15c2ff2019-08-06 13:34:31 -0600396 ret = bio_iov_iter_get_pages(bio, iter);
397 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200398 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700399 bio_endio(bio);
400 break;
401 }
402
403 if (is_read) {
404 bio->bi_opf = REQ_OP_READ;
405 if (dio->should_dirty)
406 bio_set_pages_dirty(bio);
407 } else {
408 bio->bi_opf = dio_bio_write_op(iocb);
409 task_io_account_write(bio->bi_iter.bi_size);
410 }
411
Jens Axboe6a430742019-07-16 13:56:42 -0600412 /*
413 * Tell underlying layer to not block for resource shortage.
414 * And if we would have blocked, return error inline instead
415 * of through the bio->bi_end_io() callback.
416 */
417 if (nowait)
418 bio->bi_opf |= (REQ_NOWAIT | REQ_NOWAIT_INLINE);
419
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700420 pos += bio->bi_iter.bi_size;
421
422 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
423 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700424 bool polled = false;
425
426 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700427 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700428 polled = true;
429 }
Jens Axboed34513d2018-11-06 14:29:11 -0700430
Jens Axboee15c2ff2019-08-06 13:34:31 -0600431 dio->size += bio->bi_iter.bi_size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700432 qc = submit_bio(bio);
Jens Axboe6a430742019-07-16 13:56:42 -0600433 if (qc == BLK_QC_T_EAGAIN) {
Jens Axboee15c2ff2019-08-06 13:34:31 -0600434 dio->size -= bio->bi_iter.bi_size;
435 ret = -EAGAIN;
Jens Axboe6a430742019-07-16 13:56:42 -0600436 goto error;
437 }
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700438
439 if (polled)
440 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700441 break;
442 }
443
444 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100445 /*
446 * AIO needs an extra reference to ensure the dio
447 * structure which is embedded into the first bio
448 * stays around.
449 */
450 if (!is_sync)
451 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700452 dio->multi_bio = true;
453 atomic_set(&dio->ref, 2);
454 } else {
455 atomic_inc(&dio->ref);
456 }
457
Jens Axboee15c2ff2019-08-06 13:34:31 -0600458 dio->size += bio->bi_iter.bi_size;
Jens Axboe6a430742019-07-16 13:56:42 -0600459 qc = submit_bio(bio);
460 if (qc == BLK_QC_T_EAGAIN) {
Jens Axboee15c2ff2019-08-06 13:34:31 -0600461 dio->size -= bio->bi_iter.bi_size;
462 ret = -EAGAIN;
Jens Axboe6a430742019-07-16 13:56:42 -0600463 goto error;
464 }
Jens Axboe6a430742019-07-16 13:56:42 -0600465
466 bio = bio_alloc(gfp, nr_pages);
467 if (!bio) {
Jens Axboee15c2ff2019-08-06 13:34:31 -0600468 ret = -EAGAIN;
Jens Axboe6a430742019-07-16 13:56:42 -0600469 goto error;
470 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700471 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700472
473 if (!is_poll)
474 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700475
Christoph Hellwig690e5322017-01-24 14:50:19 +0100476 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700477 return -EIOCBQUEUED;
478
479 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800480 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700481 if (!READ_ONCE(dio->waiter))
482 break;
483
484 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700485 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700486 io_schedule();
487 }
488 __set_current_state(TASK_RUNNING);
489
Jens Axboe6a430742019-07-16 13:56:42 -0600490out:
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200491 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200492 ret = blk_status_to_errno(dio->bio.bi_status);
Jens Axboee15c2ff2019-08-06 13:34:31 -0600493 if (likely(!ret))
494 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700495
496 bio_put(&dio->bio);
497 return ret;
Jens Axboe6a430742019-07-16 13:56:42 -0600498error:
499 if (!is_poll)
500 blk_finish_plug(&plug);
501 goto out;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700502}
503
504static ssize_t
505blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
506{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600507 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800508
Jens Axboe72ecad22016-11-16 23:11:42 -0700509 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600510 if (!nr_pages)
511 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700512 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600513 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700514
515 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800516}
517
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700518static __init int blkdev_init(void)
519{
Kent Overstreet52190f82018-05-20 18:25:55 -0400520 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700521}
522module_init(blkdev_init);
523
Jan Kara5cee5812009-04-27 16:43:51 +0200524int __sync_blockdev(struct block_device *bdev, int wait)
525{
526 if (!bdev)
527 return 0;
528 if (!wait)
529 return filemap_flush(bdev->bd_inode->i_mapping);
530 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
531}
532
Nick Piggin585d3bc2009-02-25 10:44:19 +0100533/*
534 * Write out and wait upon all the dirty data associated with a block
535 * device via its mapping. Does not take the superblock lock.
536 */
537int sync_blockdev(struct block_device *bdev)
538{
Jan Kara5cee5812009-04-27 16:43:51 +0200539 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100540}
541EXPORT_SYMBOL(sync_blockdev);
542
543/*
544 * Write out and wait upon all dirty data associated with this
545 * device. Filesystem data as well as the underlying block
546 * device. Takes the superblock lock.
547 */
548int fsync_bdev(struct block_device *bdev)
549{
550 struct super_block *sb = get_super(bdev);
551 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200552 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100553 drop_super(sb);
554 return res;
555 }
556 return sync_blockdev(bdev);
557}
Al Viro47e44912009-04-01 07:07:16 -0400558EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100559
560/**
561 * freeze_bdev -- lock a filesystem and force it into a consistent state
562 * @bdev: blockdevice to lock
563 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100564 * If a superblock is found on this device, we take the s_umount semaphore
565 * on it to make sure nobody unmounts until the snapshot creation is done.
566 * The reference counter (bd_fsfreeze_count) guarantees that only the last
567 * unfreeze process can unfreeze the frozen filesystem actually when multiple
568 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
569 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
570 * actually.
571 */
572struct super_block *freeze_bdev(struct block_device *bdev)
573{
574 struct super_block *sb;
575 int error = 0;
576
577 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200578 if (++bdev->bd_fsfreeze_count > 1) {
579 /*
580 * We don't even need to grab a reference - the first call
581 * to freeze_bdev grab an active reference and only the last
582 * thaw_bdev drops it.
583 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100584 sb = get_super(bdev);
Andrey Ryabinin5bb53c02016-08-23 18:55:31 +0300585 if (sb)
586 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100587 mutex_unlock(&bdev->bd_fsfreeze_mutex);
588 return sb;
589 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100590
Christoph Hellwig45042302009-08-03 23:28:35 +0200591 sb = get_active_super(bdev);
592 if (!sb)
593 goto out;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600594 if (sb->s_op->freeze_super)
595 error = sb->s_op->freeze_super(sb);
596 else
597 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400598 if (error) {
599 deactivate_super(sb);
600 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200601 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400602 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100603 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400604 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200605 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100606 sync_blockdev(bdev);
607 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200608 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100609}
610EXPORT_SYMBOL(freeze_bdev);
611
612/**
613 * thaw_bdev -- unlock filesystem
614 * @bdev: blockdevice to unlock
615 * @sb: associated superblock
616 *
617 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
618 */
619int thaw_bdev(struct block_device *bdev, struct super_block *sb)
620{
Christoph Hellwig45042302009-08-03 23:28:35 +0200621 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100622
623 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200624 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400625 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100626
Christoph Hellwig45042302009-08-03 23:28:35 +0200627 error = 0;
628 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400629 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100630
Christoph Hellwig45042302009-08-03 23:28:35 +0200631 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400632 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200633
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600634 if (sb->s_op->thaw_super)
635 error = sb->s_op->thaw_super(sb);
636 else
637 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200638 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400639 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400640out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100641 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200642 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100643}
644EXPORT_SYMBOL(thaw_bdev);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
647{
648 return block_write_full_page(page, blkdev_get_block, wbc);
649}
650
651static int blkdev_readpage(struct file * file, struct page * page)
652{
653 return block_read_full_page(page, blkdev_get_block);
654}
655
Akinobu Mita447f05b2014-10-09 15:26:58 -0700656static int blkdev_readpages(struct file *file, struct address_space *mapping,
657 struct list_head *pages, unsigned nr_pages)
658{
659 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
660}
661
Nick Piggin6272b5a2007-10-16 01:25:04 -0700662static int blkdev_write_begin(struct file *file, struct address_space *mapping,
663 loff_t pos, unsigned len, unsigned flags,
664 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200666 return block_write_begin(mapping, pos, len, flags, pagep,
667 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668}
669
Nick Piggin6272b5a2007-10-16 01:25:04 -0700670static int blkdev_write_end(struct file *file, struct address_space *mapping,
671 loff_t pos, unsigned len, unsigned copied,
672 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700674 int ret;
675 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
676
677 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300678 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700679
680 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/*
684 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500685 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 * so we compute the size by hand (just as in block_read/write above)
687 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800688static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900690 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 loff_t retval;
692
Al Viro59551022016-01-22 15:40:57 -0500693 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400694 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500695 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 return retval;
697}
698
Josef Bacik02c24a82011-07-16 20:44:56 -0400699int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900701 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400702 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100703 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200704
Jeff Layton372cf242017-07-06 07:02:28 -0400705 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200706 if (error)
707 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100708
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400709 /*
710 * There is no need to serialise calls to blkdev_issue_flush with
711 * i_mutex and doing so causes performance issues with concurrent
712 * O_SYNC writers to a block device.
713 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200714 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100715 if (error == -EOPNOTSUPP)
716 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400717
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100718 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700720EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700722/**
723 * bdev_read_page() - Start reading a page from a block device
724 * @bdev: The device to read the page from
725 * @sector: The offset on the device to read the page to (need not be aligned)
726 * @page: The page to read
727 *
728 * On entry, the page should be locked. It will be unlocked when the page
729 * has been read. If the block driver implements rw_page synchronously,
730 * that will be true on exit from this function, but it need not be.
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 read 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_read_page(struct block_device *bdev, sector_t sector,
739 struct page *page)
740{
741 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800742 int result = -EOPNOTSUPP;
743
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400744 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800745 return result;
746
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800747 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800748 if (result)
749 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700750 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
751 REQ_OP_READ);
Dan Williams2e6edc952015-11-19 13:29:28 -0800752 blk_queue_exit(bdev->bd_queue);
753 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700754}
755EXPORT_SYMBOL_GPL(bdev_read_page);
756
757/**
758 * bdev_write_page() - Start writing a page to a block device
759 * @bdev: The device to write the page to
760 * @sector: The offset on the device to write the page to (need not be aligned)
761 * @page: The page to write
762 * @wbc: The writeback_control for the write
763 *
764 * On entry, the page should be locked and not currently under writeback.
765 * On exit, if the write started successfully, the page will be unlocked and
766 * under writeback. If the write failed already (eg the driver failed to
767 * queue the page to the device), the page will still be locked. If the
768 * caller is a ->writepage implementation, it will need to unlock the page.
769 *
770 * Errors returned by this function are usually "soft", eg out of memory, or
771 * queue full; callers should try a different route to write this page rather
772 * than propagate an error back up the stack.
773 *
774 * Return: negative errno if an error occurs, 0 if submission was successful.
775 */
776int bdev_write_page(struct block_device *bdev, sector_t sector,
777 struct page *page, struct writeback_control *wbc)
778{
779 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700780 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800781
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400782 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700783 return -EOPNOTSUPP;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800784 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800785 if (result)
786 return result;
787
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700788 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700789 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
790 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700791 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700792 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700793 } else {
794 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700795 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700796 }
Dan Williams2e6edc952015-11-19 13:29:28 -0800797 blk_queue_exit(bdev->bd_queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700798 return result;
799}
800EXPORT_SYMBOL_GPL(bdev_write_page);
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802/*
803 * pseudo-fs
804 */
805
806static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800807static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809static struct inode *bdev_alloc_inode(struct super_block *sb)
810{
Christoph Lametere94b1762006-12-06 20:33:17 -0800811 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!ei)
813 return NULL;
814 return &ei->vfs_inode;
815}
816
Al Viro41149cb2019-04-10 15:12:38 -0400817static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818{
Al Viro41149cb2019-04-10 15:12:38 -0400819 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100820}
821
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700822static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823{
824 struct bdev_inode *ei = (struct bdev_inode *) foo;
825 struct block_device *bdev = &ei->bdev;
826
Christoph Lametera35afb82007-05-16 22:10:57 -0700827 memset(bdev, 0, sizeof(*bdev));
828 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700829 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100830#ifdef CONFIG_SYSFS
831 INIT_LIST_HEAD(&bdev->bd_holder_disks);
832#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100833 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700834 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800835 /* Initialize mutex for freeze. */
836 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Al Virob57922d2010-06-07 14:34:48 -0400839static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700842 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400843 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200844 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_lock(&bdev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 list_del_init(&bdev->bd_list);
847 spin_unlock(&bdev_lock);
Jan Karaf7597412017-03-23 01:37:00 +0100848 /* Detach inode from wb early as bdi_put() may free bdi->wb */
849 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100850 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100851 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100852 bdev->bd_bdi = &noop_backing_dev_info;
853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
855
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800856static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 .statfs = simple_statfs,
858 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400859 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400861 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862};
863
David Howells9030d162019-03-25 16:38:23 +0000864static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
David Howells9030d162019-03-25 16:38:23 +0000866 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
867 if (!ctx)
868 return -ENOMEM;
869 fc->s_iflags |= SB_I_CGROUPWB;
870 ctx->ops = &bdev_sops;
871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874static struct file_system_type bd_type = {
875 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000876 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 .kill_sb = kill_anon_super,
878};
879
Tejun Heoa212b102015-05-22 17:13:33 -0400880struct super_block *blockdev_superblock __read_mostly;
881EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
883void __init bdev_cache_init(void)
884{
885 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300886 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800889 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800890 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900891 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 err = register_filesystem(&bd_type);
893 if (err)
894 panic("Cannot register bdev pseudo-fs");
895 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (IS_ERR(bd_mnt))
897 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300898 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
901/*
902 * Most likely _very_ bad one - but then it's hardly critical for small
903 * /dev and can be fixed when somebody will need really large one.
904 * Keep in mind that it will be fed through icache hash function too.
905 */
906static inline unsigned long hash(dev_t dev)
907{
908 return MAJOR(dev)+MINOR(dev);
909}
910
911static int bdev_test(struct inode *inode, void *data)
912{
913 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
914}
915
916static int bdev_set(struct inode *inode, void *data)
917{
918 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
919 return 0;
920}
921
922static LIST_HEAD(all_bdevs);
923
Jan Karaf44f1ab2017-02-02 15:56:49 +0100924/*
925 * If there is a bdev inode for this device, unhash it so that it gets evicted
926 * as soon as last inode reference is dropped.
927 */
928void bdev_unhash_inode(dev_t dev)
929{
930 struct inode *inode;
931
932 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
933 if (inode) {
934 remove_inode_hash(inode);
935 iput(inode);
936 }
937}
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939struct block_device *bdget(dev_t dev)
940{
941 struct block_device *bdev;
942 struct inode *inode;
943
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800944 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 bdev_test, bdev_set, &dev);
946
947 if (!inode)
948 return NULL;
949
950 bdev = &BDEV_I(inode)->bdev;
951
952 if (inode->i_state & I_NEW) {
953 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000954 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 bdev->bd_inode = inode;
Fabian Frederick93407472017-02-27 14:28:32 -0800956 bdev->bd_block_size = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 bdev->bd_part_count = 0;
958 bdev->bd_invalidated = 0;
959 inode->i_mode = S_IFBLK;
960 inode->i_rdev = dev;
961 inode->i_bdev = bdev;
962 inode->i_data.a_ops = &def_blk_aops;
963 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 spin_lock(&bdev_lock);
965 list_add(&bdev->bd_list, &all_bdevs);
966 spin_unlock(&bdev_lock);
967 unlock_new_inode(inode);
968 }
969 return bdev;
970}
971
972EXPORT_SYMBOL(bdget);
973
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200974/**
975 * bdgrab -- Grab a reference to an already referenced block device
976 * @bdev: Block device to grab a reference to.
977 */
978struct block_device *bdgrab(struct block_device *bdev)
979{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400980 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200981 return bdev;
982}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700983EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200984
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985long nr_blockdev_pages(void)
986{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700987 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 long ret = 0;
989 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700990 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 ret += bdev->bd_inode->i_mapping->nrpages;
992 }
993 spin_unlock(&bdev_lock);
994 return ret;
995}
996
997void bdput(struct block_device *bdev)
998{
999 iput(bdev->bd_inode);
1000}
1001
1002EXPORT_SYMBOL(bdput);
1003
1004static struct block_device *bd_acquire(struct inode *inode)
1005{
1006 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 spin_lock(&bdev_lock);
1009 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +01001010 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001011 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 spin_unlock(&bdev_lock);
1013 return bdev;
1014 }
1015 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001016
Jan Karacccd9fb2017-02-21 18:09:48 +01001017 /*
1018 * i_bdev references block device inode that was already shut down
1019 * (corresponding device got removed). Remove the reference and look
1020 * up block device inode again just in case new device got
1021 * reestablished under the same device number.
1022 */
1023 if (bdev)
1024 bd_forget(inode);
1025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 bdev = bdget(inode->i_rdev);
1027 if (bdev) {
1028 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001029 if (!inode->i_bdev) {
1030 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -04001031 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001032 * and it's released in clear_inode() of inode.
1033 * So, we can access it via ->i_mapping always
1034 * without igrab().
1035 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001036 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001037 inode->i_bdev = bdev;
1038 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 spin_unlock(&bdev_lock);
1041 }
1042 return bdev;
1043}
1044
1045/* Call when you free inode */
1046
1047void bd_forget(struct inode *inode)
1048{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001049 struct block_device *bdev = NULL;
1050
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001052 if (!sb_is_blkdev_sb(inode->i_sb))
1053 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001054 inode->i_bdev = NULL;
1055 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001057
1058 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001059 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001062/**
1063 * bd_may_claim - test whether a block device can be claimed
1064 * @bdev: block device of interest
1065 * @whole: whole block device containing @bdev, may equal @bdev
1066 * @holder: holder trying to claim @bdev
1067 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001068 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001069 *
1070 * CONTEXT:
1071 * spin_lock(&bdev_lock).
1072 *
1073 * RETURNS:
1074 * %true if @bdev can be claimed, %false otherwise.
1075 */
1076static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1077 void *holder)
1078{
1079 if (bdev->bd_holder == holder)
1080 return true; /* already a holder */
1081 else if (bdev->bd_holder != NULL)
1082 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001083 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001084 return true; /* is a whole device which isn't held */
1085
Tejun Heoe525fd82010-11-13 11:55:17 +01001086 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001087 return true; /* is a partition of a device that is being partitioned */
1088 else if (whole->bd_holder != NULL)
1089 return false; /* is a partition of a held device */
1090 else
1091 return true; /* is a partition of an un-held device */
1092}
1093
1094/**
Tejun Heo6b4517a2010-04-07 18:53:59 +09001095 * bd_prepare_to_claim - prepare to claim a block device
1096 * @bdev: block device of interest
1097 * @whole: the whole device containing @bdev, may equal @bdev
1098 * @holder: holder trying to claim @bdev
1099 *
1100 * Prepare to claim @bdev. This function fails if @bdev is already
1101 * claimed by another holder and waits if another claiming is in
1102 * progress. This function doesn't actually claim. On successful
1103 * return, the caller has ownership of bd_claiming and bd_holder[s].
1104 *
1105 * CONTEXT:
1106 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1107 * it multiple times.
1108 *
1109 * RETURNS:
1110 * 0 if @bdev can be claimed, -EBUSY otherwise.
1111 */
1112static int bd_prepare_to_claim(struct block_device *bdev,
1113 struct block_device *whole, void *holder)
1114{
1115retry:
1116 /* if someone else claimed, fail */
1117 if (!bd_may_claim(bdev, whole, holder))
1118 return -EBUSY;
1119
Tejun Heoe75aa852010-08-04 17:59:39 +02001120 /* if claiming is already in progress, wait for it to finish */
1121 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001122 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1123 DEFINE_WAIT(wait);
1124
1125 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1126 spin_unlock(&bdev_lock);
1127 schedule();
1128 finish_wait(wq, &wait);
1129 spin_lock(&bdev_lock);
1130 goto retry;
1131 }
1132
1133 /* yay, all mine */
1134 return 0;
1135}
1136
Jan Kara560e7cb2018-02-26 13:01:42 +01001137static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1138{
1139 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1140
1141 if (!disk)
1142 return NULL;
1143 /*
1144 * Now that we hold gendisk reference we make sure bdev we looked up is
1145 * not stale. If it is, it means device got removed and created before
1146 * we looked up gendisk and we fail open in such case. Associating
1147 * unhashed bdev with newly created gendisk could lead to two bdevs
1148 * (and thus two independent caches) being associated with one device
1149 * which is bad.
1150 */
1151 if (inode_unhashed(bdev->bd_inode)) {
1152 put_disk_and_module(disk);
1153 return NULL;
1154 }
1155 return disk;
1156}
1157
Tejun Heo6b4517a2010-04-07 18:53:59 +09001158/**
1159 * bd_start_claiming - start claiming a block device
1160 * @bdev: block device of interest
1161 * @holder: holder trying to claim @bdev
1162 *
1163 * @bdev is about to be opened exclusively. Check @bdev can be opened
1164 * exclusively and mark that an exclusive open is in progress. Each
1165 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +10001166 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1167 * fail).
1168 *
1169 * This function is used to gain exclusive access to the block device
1170 * without actually causing other exclusive open attempts to fail. It
1171 * should be used when the open sequence itself requires exclusive
1172 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +09001173 *
1174 * CONTEXT:
1175 * Might sleep.
1176 *
1177 * RETURNS:
1178 * Pointer to the block device containing @bdev on success, ERR_PTR()
1179 * value on failure.
1180 */
Jan Kara89e524c02019-07-30 13:10:14 +02001181struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001182{
1183 struct gendisk *disk;
1184 struct block_device *whole;
1185 int partno, err;
1186
1187 might_sleep();
1188
1189 /*
1190 * @bdev might not have been initialized properly yet, look up
1191 * and grab the outer block device the hard way.
1192 */
Jan Kara560e7cb2018-02-26 13:01:42 +01001193 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001194 if (!disk)
1195 return ERR_PTR(-ENXIO);
1196
Tejun Heod4c208b2011-06-13 12:45:48 +02001197 /*
1198 * Normally, @bdev should equal what's returned from bdget_disk()
1199 * if partno is 0; however, some drivers (floppy) use multiple
1200 * bdev's for the same physical device and @bdev may be one of the
1201 * aliases. Keep @bdev if partno is 0. This means claimer
1202 * tracking is broken for those devices but it has always been that
1203 * way.
1204 */
1205 if (partno)
1206 whole = bdget_disk(disk, 0);
1207 else
1208 whole = bdgrab(bdev);
1209
Jan Kara9df6c292018-02-26 13:01:39 +01001210 put_disk_and_module(disk);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001211 if (!whole)
1212 return ERR_PTR(-ENOMEM);
1213
1214 /* prepare to claim, if successful, mark claiming in progress */
1215 spin_lock(&bdev_lock);
1216
1217 err = bd_prepare_to_claim(bdev, whole, holder);
1218 if (err == 0) {
1219 whole->bd_claiming = holder;
1220 spin_unlock(&bdev_lock);
1221 return whole;
1222 } else {
1223 spin_unlock(&bdev_lock);
1224 bdput(whole);
1225 return ERR_PTR(err);
1226 }
1227}
Jan Kara89e524c02019-07-30 13:10:14 +02001228EXPORT_SYMBOL(bd_start_claiming);
1229
1230static void bd_clear_claiming(struct block_device *whole, void *holder)
1231{
1232 lockdep_assert_held(&bdev_lock);
1233 /* tell others that we're done */
1234 BUG_ON(whole->bd_claiming != holder);
1235 whole->bd_claiming = NULL;
1236 wake_up_bit(&whole->bd_claiming, 0);
1237}
1238
1239/**
1240 * bd_finish_claiming - finish claiming of a block device
1241 * @bdev: block device of interest
1242 * @whole: whole block device (returned from bd_start_claiming())
1243 * @holder: holder that has claimed @bdev
1244 *
1245 * Finish exclusive open of a block device. Mark the device as exlusively
1246 * open by the holder and wake up all waiters for exclusive open to finish.
1247 */
1248void bd_finish_claiming(struct block_device *bdev, struct block_device *whole,
1249 void *holder)
1250{
1251 spin_lock(&bdev_lock);
1252 BUG_ON(!bd_may_claim(bdev, whole, holder));
1253 /*
1254 * Note that for a whole device bd_holders will be incremented twice,
1255 * and bd_holder will be set to bd_may_claim before being set to holder
1256 */
1257 whole->bd_holders++;
1258 whole->bd_holder = bd_may_claim;
1259 bdev->bd_holders++;
1260 bdev->bd_holder = holder;
1261 bd_clear_claiming(whole, holder);
1262 spin_unlock(&bdev_lock);
1263}
1264EXPORT_SYMBOL(bd_finish_claiming);
1265
1266/**
1267 * bd_abort_claiming - abort claiming of a block device
1268 * @bdev: block device of interest
1269 * @whole: whole block device (returned from bd_start_claiming())
1270 * @holder: holder that has claimed @bdev
1271 *
1272 * Abort claiming of a block device when the exclusive open failed. This can be
1273 * also used when exclusive open is not actually desired and we just needed
1274 * to block other exclusive openers for a while.
1275 */
1276void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1277 void *holder)
1278{
1279 spin_lock(&bdev_lock);
1280 bd_clear_claiming(whole, holder);
1281 spin_unlock(&bdev_lock);
1282}
1283EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001284
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001285#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001286struct bd_holder_disk {
1287 struct list_head list;
1288 struct gendisk *disk;
1289 int refcnt;
1290};
1291
1292static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1293 struct gendisk *disk)
1294{
1295 struct bd_holder_disk *holder;
1296
1297 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1298 if (holder->disk == disk)
1299 return holder;
1300 return NULL;
1301}
1302
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001303static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001304{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001305 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001306}
1307
1308static void del_symlink(struct kobject *from, struct kobject *to)
1309{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001310 sysfs_remove_link(from, kobject_name(to));
1311}
1312
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001313/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001314 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1315 * @bdev: the claimed slave bdev
1316 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001317 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001318 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1319 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001320 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001321 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001322 * - from "slaves" directory of the holder @disk to the claimed @bdev
1323 * - from "holders" directory of the @bdev to the holder @disk
1324 *
1325 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1326 * passed to bd_link_disk_holder(), then:
1327 *
1328 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1329 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1330 *
1331 * The caller must have claimed @bdev before calling this function and
1332 * ensure that both @bdev and @disk are valid during the creation and
1333 * lifetime of these symlinks.
1334 *
1335 * CONTEXT:
1336 * Might sleep.
1337 *
1338 * RETURNS:
1339 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001340 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001341int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001342{
Tejun Heo49731ba2011-01-14 18:43:57 +01001343 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001344 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001345
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001346 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001347
Tejun Heo49731ba2011-01-14 18:43:57 +01001348 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001349
Tejun Heoe09b4572010-11-13 11:55:17 +01001350 /* FIXME: remove the following once add_disk() handles errors */
1351 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1352 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001353
Tejun Heo49731ba2011-01-14 18:43:57 +01001354 holder = bd_find_holder_disk(bdev, disk);
1355 if (holder) {
1356 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001357 goto out_unlock;
1358 }
1359
Tejun Heo49731ba2011-01-14 18:43:57 +01001360 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1361 if (!holder) {
1362 ret = -ENOMEM;
1363 goto out_unlock;
1364 }
1365
1366 INIT_LIST_HEAD(&holder->list);
1367 holder->disk = disk;
1368 holder->refcnt = 1;
1369
1370 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1371 if (ret)
1372 goto out_free;
1373
1374 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1375 if (ret)
1376 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001377 /*
1378 * bdev could be deleted beneath us which would implicitly destroy
1379 * the holder directory. Hold on to it.
1380 */
1381 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001382
1383 list_add(&holder->list, &bdev->bd_holder_disks);
1384 goto out_unlock;
1385
1386out_del:
1387 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1388out_free:
1389 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001390out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001391 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001392 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001393}
Tejun Heoe09b4572010-11-13 11:55:17 +01001394EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001395
Tejun Heo49731ba2011-01-14 18:43:57 +01001396/**
1397 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1398 * @bdev: the calimed slave bdev
1399 * @disk: the holding disk
1400 *
1401 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1402 *
1403 * CONTEXT:
1404 * Might sleep.
1405 */
1406void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001407{
Tejun Heo49731ba2011-01-14 18:43:57 +01001408 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001409
Tejun Heo49731ba2011-01-14 18:43:57 +01001410 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001411
Tejun Heo49731ba2011-01-14 18:43:57 +01001412 holder = bd_find_holder_disk(bdev, disk);
1413
1414 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1415 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1416 del_symlink(bdev->bd_part->holder_dir,
1417 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001418 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001419 list_del_init(&holder->list);
1420 kfree(holder);
1421 }
1422
1423 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001424}
Tejun Heo49731ba2011-01-14 18:43:57 +01001425EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001426#endif
1427
Andrew Patterson0c002c22008-09-04 14:27:20 -06001428/**
Andrew Patterson56ade442008-09-04 14:27:40 -06001429 * flush_disk - invalidates all buffer-cache entries on a disk
1430 *
1431 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -08001432 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -06001433 *
1434 * Invalidates all buffer-cache entries on a disk. It should be called
1435 * when a disk has been changed -- either by a media change or online
1436 * resize.
1437 */
NeilBrown93b270f2011-02-24 17:25:47 +11001438static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -06001439{
NeilBrown93b270f2011-02-24 17:25:47 +11001440 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -06001441 printk(KERN_WARNING "VFS: busy inodes on changed media or "
Dmitry Monakhov424081f2015-04-13 16:31:34 +04001442 "resized disk %s\n",
1443 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
Andrew Patterson56ade442008-09-04 14:27:40 -06001444 }
1445
1446 if (!bdev->bd_disk)
1447 return;
Tejun Heod27769e2011-08-23 20:01:04 +02001448 if (disk_part_scan_enabled(bdev->bd_disk))
Andrew Patterson56ade442008-09-04 14:27:40 -06001449 bdev->bd_invalidated = 1;
1450}
1451
1452/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001453 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001454 * @disk: struct gendisk to check
1455 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001456 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001457 *
1458 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001459 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1460 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001461 */
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001462void check_disk_size_change(struct gendisk *disk, struct block_device *bdev,
1463 bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001464{
1465 loff_t disk_size, bdev_size;
1466
1467 disk_size = (loff_t)get_capacity(disk) << 9;
1468 bdev_size = i_size_read(bdev->bd_inode);
1469 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001470 if (verbose) {
1471 printk(KERN_INFO
1472 "%s: detected capacity change from %lld to %lld\n",
1473 disk->disk_name, bdev_size, disk_size);
1474 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001475 i_size_write(bdev->bd_inode, disk_size);
shunki-fujita849cf552018-04-05 16:20:07 -07001476 if (bdev_size > disk_size)
1477 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001478 }
1479}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001480
1481/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001482 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001483 * @disk: struct gendisk to be revalidated
1484 *
1485 * This routine is a wrapper for lower-level driver's revalidate_disk
1486 * call-backs. It is used to do common pre and post operations needed
1487 * for all revalidate_disk operations.
1488 */
1489int revalidate_disk(struct gendisk *disk)
1490{
1491 int ret = 0;
1492
1493 if (disk->fops->revalidate_disk)
1494 ret = disk->fops->revalidate_disk(disk);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001495
Jan Kara31cb1d62019-05-15 08:57:40 +02001496 /*
1497 * Hidden disks don't have associated bdev so there's no point in
1498 * revalidating it.
1499 */
1500 if (!(disk->flags & GENHD_FL_HIDDEN)) {
1501 struct block_device *bdev = bdget_disk(disk, 0);
1502
1503 if (!bdev)
1504 return ret;
1505
1506 mutex_lock(&bdev->bd_mutex);
1507 check_disk_size_change(disk, bdev, ret == 0);
1508 bdev->bd_invalidated = 0;
1509 mutex_unlock(&bdev->bd_mutex);
1510 bdput(bdev);
1511 }
Andrew Patterson0c002c22008-09-04 14:27:20 -06001512 return ret;
1513}
1514EXPORT_SYMBOL(revalidate_disk);
1515
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516/*
1517 * This routine checks whether a removable media has been changed,
1518 * and invalidates all buffer-cache-entries in that case. This
1519 * is a relatively slow routine, so we have to try to minimize using
1520 * it. Thus it is called only upon a 'mount' or 'open'. This
1521 * is the best way of combining speed and utility, I think.
1522 * People changing diskettes in the middle of an operation deserve
1523 * to lose :-)
1524 */
1525int check_disk_change(struct block_device *bdev)
1526{
1527 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001528 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001529 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Tejun Heo77ea8872010-12-08 20:57:37 +01001531 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1532 DISK_EVENT_EJECT_REQUEST);
1533 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 return 0;
1535
NeilBrown93b270f2011-02-24 17:25:47 +11001536 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 if (bdops->revalidate_disk)
1538 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 return 1;
1540}
1541
1542EXPORT_SYMBOL(check_disk_change);
1543
1544void bd_set_size(struct block_device *bdev, loff_t size)
1545{
Al Viro59551022016-01-22 15:40:57 -05001546 inode_lock(bdev->bd_inode);
Guo Chaod646a022013-02-21 15:16:42 -08001547 i_size_write(bdev->bd_inode, size);
Al Viro59551022016-01-22 15:40:57 -05001548 inode_unlock(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550EXPORT_SYMBOL(bd_set_size);
1551
Al Viro4385bab2013-05-05 22:11:03 -04001552static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001553
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001554/*
1555 * bd_mutex locking:
1556 *
1557 * mutex_lock(part->bd_mutex)
1558 * mutex_lock_nested(whole->bd_mutex, 1)
1559 */
1560
Al Viro572c4892007-10-08 13:24:05 -04001561static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001564 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001565 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001566 int perm = 0;
Jan Kara89736652018-02-26 13:01:40 +01001567 bool first_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Al Viro572c4892007-10-08 13:24:05 -04001569 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001570 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001571 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001572 perm |= MAY_WRITE;
1573 /*
1574 * hooks: /n/, see "layering violations".
1575 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001576 if (!for_part) {
1577 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1578 if (ret != 0) {
1579 bdput(bdev);
1580 return ret;
1581 }
Al Viro82666022008-08-01 05:32:04 -04001582 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001583
NeilBrownd3374822009-01-09 08:31:10 +11001584 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001585
Tejun Heo89f97492008-11-05 10:21:06 +01001586 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001587 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001588 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001589 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Tejun Heo69e02c52011-03-09 19:54:27 +01001591 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001592 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001594 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 bdev->bd_disk = disk;
Andi Kleen87192a22012-01-12 17:20:34 -08001596 bdev->bd_queue = disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001598 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001599
Tejun Heocf771cb2008-09-03 09:01:09 +02001600 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001601 ret = -ENXIO;
1602 bdev->bd_part = disk_get_part(disk, partno);
1603 if (!bdev->bd_part)
1604 goto out_clear;
1605
Tejun Heo1196f8b2011-04-21 20:54:45 +02001606 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001608 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001609 if (ret == -ERESTARTSYS) {
1610 /* Lost a race with 'disk' being
1611 * deleted, try again.
1612 * See md.c
1613 */
1614 disk_put_part(bdev->bd_part);
1615 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001616 bdev->bd_disk = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001617 bdev->bd_queue = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001618 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001619 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001620 put_disk_and_module(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001621 goto restart;
1622 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 }
Tejun Heo7e697232011-05-23 13:26:07 +02001624
Jan Kara04906b22019-01-14 09:48:10 +01001625 if (!ret) {
Tejun Heo7e697232011-05-23 13:26:07 +02001626 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
Jan Kara04906b22019-01-14 09:48:10 +01001627 set_init_blocksize(bdev);
1628 }
Tejun Heo7e697232011-05-23 13:26:07 +02001629
Tejun Heo1196f8b2011-04-21 20:54:45 +02001630 /*
1631 * If the device is invalidated, rescan partition
1632 * if open succeeded or failed with -ENOMEDIUM.
1633 * The latter is necessary to prevent ghost
1634 * partitions on a removed medium.
1635 */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001636 if (bdev->bd_invalidated) {
1637 if (!ret)
1638 rescan_partitions(disk, bdev);
1639 else if (ret == -ENOMEDIUM)
1640 invalidate_partitions(disk, bdev);
1641 }
Dan Williams5a023cd2015-11-30 10:20:29 -08001642
Tejun Heo1196f8b2011-04-21 20:54:45 +02001643 if (ret)
1644 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 struct block_device *whole;
1647 whole = bdget_disk(disk, 0);
1648 ret = -ENOMEM;
1649 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001650 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001651 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001652 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001654 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 bdev->bd_contains = whole;
Tejun Heo89f97492008-11-05 10:21:06 +01001656 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001657 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001658 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001660 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
Tejun Heo89f97492008-11-05 10:21:06 +01001662 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Jan Kara04906b22019-01-14 09:48:10 +01001663 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Jan Kara03e26272017-03-23 01:36:53 +01001665
1666 if (bdev->bd_bdi == &noop_backing_dev_info)
1667 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001670 ret = 0;
1671 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001672 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001673 /* the same as first opener case, read comment there */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001674 if (bdev->bd_invalidated) {
1675 if (!ret)
1676 rescan_partitions(bdev->bd_disk, bdev);
1677 else if (ret == -ENOMEDIUM)
1678 invalidate_partitions(bdev->bd_disk, bdev);
1679 }
Tejun Heo1196f8b2011-04-21 20:54:45 +02001680 if (ret)
1681 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 }
1683 }
1684 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001685 if (for_part)
1686 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001687 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001688 disk_unblock_events(disk);
Jan Kara89736652018-02-26 13:01:40 +01001689 /* only one opener holds refs to the module and disk */
1690 if (!first_open)
1691 put_disk_and_module(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 return 0;
1693
Tejun Heo0762b8b2008-08-25 19:56:12 +09001694 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001695 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001697 bdev->bd_part = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001698 bdev->bd_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001700 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001702 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001703 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001704 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001705 put_disk_and_module(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001706 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001707 bdput(bdev);
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 return ret;
1710}
1711
Tejun Heod4d77622010-11-13 11:55:18 +01001712/**
1713 * blkdev_get - open a block device
1714 * @bdev: block_device to open
1715 * @mode: FMODE_* mask
1716 * @holder: exclusive holder identifier
1717 *
1718 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1719 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1720 * @holder is invalid. Exclusive opens may nest for the same @holder.
1721 *
1722 * On success, the reference count of @bdev is unchanged. On failure,
1723 * @bdev is put.
1724 *
1725 * CONTEXT:
1726 * Might sleep.
1727 *
1728 * RETURNS:
1729 * 0 on success, -errno on failure.
1730 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001731int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732{
Tejun Heoe525fd82010-11-13 11:55:17 +01001733 struct block_device *whole = NULL;
1734 int res;
1735
1736 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1737
1738 if ((mode & FMODE_EXCL) && holder) {
1739 whole = bd_start_claiming(bdev, holder);
1740 if (IS_ERR(whole)) {
1741 bdput(bdev);
1742 return PTR_ERR(whole);
1743 }
1744 }
1745
1746 res = __blkdev_get(bdev, mode, 0);
1747
1748 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001749 struct gendisk *disk = whole->bd_disk;
1750
Tejun Heo6a027ef2010-11-13 11:55:17 +01001751 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001752 mutex_lock(&bdev->bd_mutex);
Jan Karae91455b2019-08-07 11:36:47 +02001753 if (!res)
1754 bd_finish_claiming(bdev, whole, holder);
1755 else
1756 bd_abort_claiming(bdev, whole, holder);
Tejun Heo77ea8872010-12-08 20:57:37 +01001757 /*
Tejun Heod4dc2102011-04-21 20:54:46 +02001758 * Block event polling for write claims if requested. Any
1759 * write holder makes the write_holder state stick until
1760 * all are released. This is good enough and tracking
1761 * individual writeable reference is too fragile given the
1762 * way @mode is used in blkdev_get/put().
Tejun Heo77ea8872010-12-08 20:57:37 +01001763 */
Tejun Heo4c49ff32011-06-01 08:27:41 +02001764 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1765 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001766 bdev->bd_write_holder = true;
Tejun Heod4dc2102011-04-21 20:54:46 +02001767 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001768 }
1769
1770 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001771 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001772 }
1773
1774 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001775}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776EXPORT_SYMBOL(blkdev_get);
1777
Tejun Heod4d77622010-11-13 11:55:18 +01001778/**
1779 * blkdev_get_by_path - open a block device by name
1780 * @path: path to the block device to open
1781 * @mode: FMODE_* mask
1782 * @holder: exclusive holder identifier
1783 *
1784 * Open the blockdevice described by the device file at @path. @mode
1785 * and @holder are identical to blkdev_get().
1786 *
1787 * On success, the returned block_device has reference count of one.
1788 *
1789 * CONTEXT:
1790 * Might sleep.
1791 *
1792 * RETURNS:
1793 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1794 */
1795struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1796 void *holder)
1797{
1798 struct block_device *bdev;
1799 int err;
1800
1801 bdev = lookup_bdev(path);
1802 if (IS_ERR(bdev))
1803 return bdev;
1804
1805 err = blkdev_get(bdev, mode, holder);
1806 if (err)
1807 return ERR_PTR(err);
1808
Chuck Ebberte51900f2011-02-16 18:11:53 -05001809 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1810 blkdev_put(bdev, mode);
1811 return ERR_PTR(-EACCES);
1812 }
1813
Tejun Heod4d77622010-11-13 11:55:18 +01001814 return bdev;
1815}
1816EXPORT_SYMBOL(blkdev_get_by_path);
1817
1818/**
1819 * blkdev_get_by_dev - open a block device by device number
1820 * @dev: device number of block device to open
1821 * @mode: FMODE_* mask
1822 * @holder: exclusive holder identifier
1823 *
1824 * Open the blockdevice described by device number @dev. @mode and
1825 * @holder are identical to blkdev_get().
1826 *
1827 * Use it ONLY if you really do not have anything better - i.e. when
1828 * you are behind a truly sucky interface and all you are given is a
1829 * device number. _Never_ to be used for internal purposes. If you
1830 * ever need it - reconsider your API.
1831 *
1832 * On success, the returned block_device has reference count of one.
1833 *
1834 * CONTEXT:
1835 * Might sleep.
1836 *
1837 * RETURNS:
1838 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1839 */
1840struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1841{
1842 struct block_device *bdev;
1843 int err;
1844
1845 bdev = bdget(dev);
1846 if (!bdev)
1847 return ERR_PTR(-ENOMEM);
1848
1849 err = blkdev_get(bdev, mode, holder);
1850 if (err)
1851 return ERR_PTR(err);
1852
1853 return bdev;
1854}
1855EXPORT_SYMBOL(blkdev_get_by_dev);
1856
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857static int blkdev_open(struct inode * inode, struct file * filp)
1858{
1859 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860
1861 /*
1862 * Preserve backwards compatibility and allow large file access
1863 * even if userspace doesn't ask for it explicitly. Some mkfs
1864 * binary needs it. We might want to drop this workaround
1865 * during an unstable branch.
1866 */
1867 filp->f_flags |= O_LARGEFILE;
1868
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001869 filp->f_mode |= FMODE_NOWAIT;
1870
Al Viro572c4892007-10-08 13:24:05 -04001871 if (filp->f_flags & O_NDELAY)
1872 filp->f_mode |= FMODE_NDELAY;
1873 if (filp->f_flags & O_EXCL)
1874 filp->f_mode |= FMODE_EXCL;
1875 if ((filp->f_flags & O_ACCMODE) == 3)
1876 filp->f_mode |= FMODE_WRITE_IOCTL;
1877
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001879 if (bdev == NULL)
1880 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Al Viro572c4892007-10-08 13:24:05 -04001882 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001883 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Al Viro572c4892007-10-08 13:24:05 -04001884
Tejun Heoe525fd82010-11-13 11:55:17 +01001885 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886}
1887
Al Viro4385bab2013-05-05 22:11:03 -04001888static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001889{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001890 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001891 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001892
NeilBrown6796bf52006-12-08 02:36:16 -08001893 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001894 if (for_part)
1895 bdev->bd_part_count--;
1896
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001897 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001898 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001899 sync_blockdev(bdev);
1900 kill_bdev(bdev);
Ilya Dryomov43d1c0e2015-11-20 22:22:34 +01001901
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001902 bdev_write_inode(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001903 }
1904 if (bdev->bd_contains == bdev) {
1905 if (disk->fops->release)
Al Virodb2a1442013-05-05 21:52:57 -04001906 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001907 }
1908 if (!bdev->bd_openers) {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001909 disk_put_part(bdev->bd_part);
1910 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001911 bdev->bd_disk = NULL;
NeilBrown37be4122006-12-08 02:36:16 -08001912 if (bdev != bdev->bd_contains)
1913 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001914 bdev->bd_contains = NULL;
Tejun Heo523e1d32011-10-19 14:31:07 +02001915
Jan Kara9df6c292018-02-26 13:01:39 +01001916 put_disk_and_module(disk);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001917 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001918 mutex_unlock(&bdev->bd_mutex);
1919 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001920 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001921 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001922}
1923
Al Viro4385bab2013-05-05 22:11:03 -04001924void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001925{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001926 mutex_lock(&bdev->bd_mutex);
1927
Tejun Heoe525fd82010-11-13 11:55:17 +01001928 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001929 bool bdev_free;
1930
1931 /*
1932 * Release a claim on the device. The holder fields
1933 * are protected with bdev_lock. bd_mutex is to
1934 * synchronize disk_holder unlinking.
1935 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001936 spin_lock(&bdev_lock);
1937
1938 WARN_ON_ONCE(--bdev->bd_holders < 0);
1939 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1940
1941 /* bd_contains might point to self, check in a separate step */
1942 if ((bdev_free = !bdev->bd_holders))
1943 bdev->bd_holder = NULL;
1944 if (!bdev->bd_contains->bd_holders)
1945 bdev->bd_contains->bd_holder = NULL;
1946
1947 spin_unlock(&bdev_lock);
1948
Tejun Heo77ea8872010-12-08 20:57:37 +01001949 /*
1950 * If this was the last claim, remove holder link and
1951 * unblock evpoll if it was a write holder.
1952 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001953 if (bdev_free && bdev->bd_write_holder) {
1954 disk_unblock_events(bdev->bd_disk);
1955 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001956 }
Tejun Heo69362172011-03-09 19:54:27 +01001957 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001958
Tejun Heo85ef06d2011-07-01 16:17:47 +02001959 /*
1960 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1961 * event. This is to ensure detection of media removal commanded
1962 * from userland - e.g. eject(1).
1963 */
1964 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1965
1966 mutex_unlock(&bdev->bd_mutex);
1967
Al Viro4385bab2013-05-05 22:11:03 -04001968 __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001969}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001970EXPORT_SYMBOL(blkdev_put);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static int blkdev_close(struct inode * inode, struct file * filp)
1973{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001974 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001975 blkdev_put(bdev, filp->f_mode);
1976 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977}
1978
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001979static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001981 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001982 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001983
1984 /*
1985 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1986 * to updated it before every ioctl.
1987 */
Al Viro56b26ad2008-09-19 03:17:36 -04001988 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001989 mode |= FMODE_NDELAY;
1990 else
1991 mode &= ~FMODE_NDELAY;
1992
Al Viro56b26ad2008-09-19 03:17:36 -04001993 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001996/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001997 * Write data to the block device. Only intended for the block device itself
1998 * and the raw driver which basically is a fake block device.
1999 *
2000 * Does not take i_mutex for the write and thus is not for general purpose
2001 * use.
2002 */
Al Viro1456c0a2014-04-03 03:21:50 -04002003ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02002004{
2005 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002006 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04002007 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002008 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02002009 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04002010
Al Viro7ec7b942015-04-07 11:35:14 -04002011 if (bdev_read_only(I_BDEV(bd_inode)))
2012 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04002013
Al Viro7ec7b942015-04-07 11:35:14 -04002014 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04002015 return 0;
2016
Al Viro7ec7b942015-04-07 11:35:14 -04002017 if (iocb->ki_pos >= size)
2018 return -ENOSPC;
2019
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02002020 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
2021 return -EOPNOTSUPP;
2022
Al Viro7ec7b942015-04-07 11:35:14 -04002023 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002024
Jianpeng Ma53362a02012-08-02 09:50:39 +02002025 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04002026 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07002027 if (ret > 0)
2028 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02002029 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002030 return ret;
2031}
Al Viro1456c0a2014-04-03 03:21:50 -04002032EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02002033
David Jefferyb2de5252014-09-29 10:21:10 -04002034ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002035{
2036 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09002037 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002038 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04002039 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002040
2041 if (pos >= size)
2042 return 0;
2043
2044 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04002045 iov_iter_truncate(to, size);
2046 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002047}
David Jefferyb2de5252014-09-29 10:21:10 -04002048EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08002049
Christoph Hellwigeef99382009-08-20 17:43:41 +02002050/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002051 * Try to release a page associated with block device when the system
2052 * is under memory pressure.
2053 */
2054static int blkdev_releasepage(struct page *page, gfp_t wait)
2055{
2056 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
2057
2058 if (super && super->s_op->bdev_try_to_free_page)
2059 return super->s_op->bdev_try_to_free_page(super, page, wait);
2060
2061 return try_to_free_buffers(page);
2062}
2063
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002064static int blkdev_writepages(struct address_space *mapping,
2065 struct writeback_control *wbc)
2066{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002067 return generic_writepages(mapping, wbc);
2068}
2069
Adrian Bunk4c54ac62008-02-18 13:48:31 +01002070static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 .readpage = blkdev_readpage,
Akinobu Mita447f05b2014-10-09 15:26:58 -07002072 .readpages = blkdev_readpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07002074 .write_begin = blkdev_write_begin,
2075 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002076 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002077 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08002079 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07002080 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081};
2082
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002083#define BLKDEV_FALLOC_FL_SUPPORTED \
2084 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
2085 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
2086
2087static long blkdev_fallocate(struct file *file, int mode, loff_t start,
2088 loff_t len)
2089{
2090 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002091 struct address_space *mapping;
2092 loff_t end = start + len - 1;
2093 loff_t isize;
2094 int error;
2095
2096 /* Fail if we don't recognize the flags. */
2097 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
2098 return -EOPNOTSUPP;
2099
2100 /* Don't go off the end of the device. */
2101 isize = i_size_read(bdev->bd_inode);
2102 if (start >= isize)
2103 return -EINVAL;
2104 if (end >= isize) {
2105 if (mode & FALLOC_FL_KEEP_SIZE) {
2106 len = isize - start;
2107 end = start + len - 1;
2108 } else
2109 return -EINVAL;
2110 }
2111
2112 /*
2113 * Don't allow IO that isn't aligned to logical block size.
2114 */
2115 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
2116 return -EINVAL;
2117
2118 /* Invalidate the page cache, including dirty pages. */
2119 mapping = bdev->bd_inode->i_mapping;
2120 truncate_inode_pages_range(mapping, start, end);
2121
2122 switch (mode) {
2123 case FALLOC_FL_ZERO_RANGE:
2124 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
2125 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02002126 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002127 break;
2128 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02002129 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2130 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002131 break;
2132 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002133 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2134 GFP_KERNEL, 0);
2135 break;
2136 default:
2137 return -EOPNOTSUPP;
2138 }
2139 if (error)
2140 return error;
2141
2142 /*
2143 * Invalidate again; if someone wandered in and dirtied a page,
2144 * the caller will be given -EBUSY. The third argument is
2145 * inclusive, so the rounding here is safe.
2146 */
2147 return invalidate_inode_pages2_range(mapping,
2148 start >> PAGE_SHIFT,
2149 end >> PAGE_SHIFT);
2150}
2151
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002152const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 .open = blkdev_open,
2154 .release = blkdev_close,
2155 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04002156 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04002157 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07002158 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07002159 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07002160 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07002161 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162#ifdef CONFIG_COMPAT
2163 .compat_ioctl = compat_blkdev_ioctl,
2164#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08002165 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002166 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002167 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168};
2169
2170int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
2171{
2172 int res;
2173 mm_segment_t old_fs = get_fs();
2174 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04002175 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 set_fs(old_fs);
2177 return res;
2178}
2179
2180EXPORT_SYMBOL(ioctl_by_bdev);
2181
2182/**
2183 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08002184 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02002186 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 * namespace if possible and return it. Return ERR_PTR(error)
2188 * otherwise.
2189 */
Al Viro421748e2008-08-02 01:04:36 -04002190struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191{
2192 struct block_device *bdev;
2193 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04002194 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 int error;
2196
Al Viro421748e2008-08-02 01:04:36 -04002197 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return ERR_PTR(-EINVAL);
2199
Al Viro421748e2008-08-02 01:04:36 -04002200 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 if (error)
2202 return ERR_PTR(error);
2203
David Howellsbb6687342015-03-17 22:26:21 +00002204 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205 error = -ENOTBLK;
2206 if (!S_ISBLK(inode->i_mode))
2207 goto fail;
2208 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002209 if (!may_open_dev(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto fail;
2211 error = -ENOMEM;
2212 bdev = bd_acquire(inode);
2213 if (!bdev)
2214 goto fail;
2215out:
Al Viro421748e2008-08-02 01:04:36 -04002216 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 return bdev;
2218fail:
2219 bdev = ERR_PTR(error);
2220 goto out;
2221}
Al Virod5686b42008-08-01 05:00:11 -04002222EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
NeilBrown93b270f2011-02-24 17:25:47 +11002224int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01002225{
2226 struct super_block *sb = get_super(bdev);
2227 int res = 0;
2228
2229 if (sb) {
2230 /*
2231 * no need to lock the super, get_super holds the
2232 * read mutex so the filesystem cannot go away
2233 * under us (->put_super runs with the write lock
2234 * hold).
2235 */
2236 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11002237 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01002238 drop_super(sb);
2239 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002240 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01002241 return res;
2242}
2243EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002244
2245void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2246{
2247 struct inode *inode, *old_inode = NULL;
2248
Dave Chinner74278da2015-03-04 12:37:22 -05002249 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002250 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2251 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01002252 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02002253
2254 spin_lock(&inode->i_lock);
2255 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2256 mapping->nrpages == 0) {
2257 spin_unlock(&inode->i_lock);
2258 continue;
2259 }
2260 __iget(inode);
2261 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05002262 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002263 /*
2264 * We hold a reference to 'inode' so it couldn't have been
2265 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05002266 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02002267 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05002268 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02002269 * later.
2270 */
2271 iput(old_inode);
2272 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01002273 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002274
Rabin Vincentaf309222016-12-01 09:18:28 +01002275 mutex_lock(&bdev->bd_mutex);
2276 if (bdev->bd_openers)
2277 func(bdev, arg);
2278 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002279
Dave Chinner74278da2015-03-04 12:37:22 -05002280 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002281 }
Dave Chinner74278da2015-03-04 12:37:22 -05002282 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002283 iput(old_inode);
2284}