blob: 22591bad9353f4dbce4001986a088d45f594fdeb [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;
352 ssize_t 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 (;;) {
Jens Axboe6a430742019-07-16 13:56:42 -0600389 int err;
390
Christoph Hellwig74d46992017-08-23 19:10:32 +0200391 bio_set_dev(bio, bdev);
Damien Le Moal4d1a4762016-11-22 15:38:49 +0900392 bio->bi_iter.bi_sector = pos >> 9;
Jens Axboe45d06cf2017-06-27 11:01:22 -0600393 bio->bi_write_hint = iocb->ki_hint;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700394 bio->bi_private = dio;
395 bio->bi_end_io = blkdev_bio_end_io;
Adam Manzanares074111c2018-05-22 10:52:20 -0700396 bio->bi_ioprio = iocb->ki_ioprio;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700397
Jens Axboe6a430742019-07-16 13:56:42 -0600398 err = bio_iov_iter_get_pages(bio, iter);
399 if (unlikely(err)) {
400 if (!ret)
401 ret = err;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200402 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700403 bio_endio(bio);
404 break;
405 }
406
407 if (is_read) {
408 bio->bi_opf = REQ_OP_READ;
409 if (dio->should_dirty)
410 bio_set_pages_dirty(bio);
411 } else {
412 bio->bi_opf = dio_bio_write_op(iocb);
413 task_io_account_write(bio->bi_iter.bi_size);
414 }
415
Jens Axboe6a430742019-07-16 13:56:42 -0600416 /*
417 * Tell underlying layer to not block for resource shortage.
418 * And if we would have blocked, return error inline instead
419 * of through the bio->bi_end_io() callback.
420 */
421 if (nowait)
422 bio->bi_opf |= (REQ_NOWAIT | REQ_NOWAIT_INLINE);
423
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700424 dio->size += bio->bi_iter.bi_size;
425 pos += bio->bi_iter.bi_size;
426
427 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
428 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700429 bool polled = false;
430
431 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700432 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700433 polled = true;
434 }
Jens Axboed34513d2018-11-06 14:29:11 -0700435
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700436 qc = submit_bio(bio);
Jens Axboe6a430742019-07-16 13:56:42 -0600437 if (qc == BLK_QC_T_EAGAIN) {
438 if (!ret)
439 ret = -EAGAIN;
440 goto error;
441 }
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700442
443 if (polled)
444 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700445 break;
446 }
447
448 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100449 /*
450 * AIO needs an extra reference to ensure the dio
451 * structure which is embedded into the first bio
452 * stays around.
453 */
454 if (!is_sync)
455 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700456 dio->multi_bio = true;
457 atomic_set(&dio->ref, 2);
458 } else {
459 atomic_inc(&dio->ref);
460 }
461
Jens Axboe6a430742019-07-16 13:56:42 -0600462 qc = submit_bio(bio);
463 if (qc == BLK_QC_T_EAGAIN) {
464 if (!ret)
465 ret = -EAGAIN;
466 goto error;
467 }
468 ret += bio->bi_iter.bi_size;
469
470 bio = bio_alloc(gfp, nr_pages);
471 if (!bio) {
472 if (!ret)
473 ret = -EAGAIN;
474 goto error;
475 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700476 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700477
478 if (!is_poll)
479 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700480
Christoph Hellwig690e5322017-01-24 14:50:19 +0100481 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700482 return -EIOCBQUEUED;
483
484 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800485 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700486 if (!READ_ONCE(dio->waiter))
487 break;
488
489 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700490 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700491 io_schedule();
492 }
493 __set_current_state(TASK_RUNNING);
494
Jens Axboe6a430742019-07-16 13:56:42 -0600495out:
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200496 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200497 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700498
499 bio_put(&dio->bio);
500 return ret;
Jens Axboe6a430742019-07-16 13:56:42 -0600501error:
502 if (!is_poll)
503 blk_finish_plug(&plug);
504 goto out;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700505}
506
507static ssize_t
508blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
509{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600510 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800511
Jens Axboe72ecad22016-11-16 23:11:42 -0700512 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600513 if (!nr_pages)
514 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700515 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600516 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700517
518 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800519}
520
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700521static __init int blkdev_init(void)
522{
Kent Overstreet52190f82018-05-20 18:25:55 -0400523 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700524}
525module_init(blkdev_init);
526
Jan Kara5cee5812009-04-27 16:43:51 +0200527int __sync_blockdev(struct block_device *bdev, int wait)
528{
529 if (!bdev)
530 return 0;
531 if (!wait)
532 return filemap_flush(bdev->bd_inode->i_mapping);
533 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
534}
535
Nick Piggin585d3bc2009-02-25 10:44:19 +0100536/*
537 * Write out and wait upon all the dirty data associated with a block
538 * device via its mapping. Does not take the superblock lock.
539 */
540int sync_blockdev(struct block_device *bdev)
541{
Jan Kara5cee5812009-04-27 16:43:51 +0200542 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100543}
544EXPORT_SYMBOL(sync_blockdev);
545
546/*
547 * Write out and wait upon all dirty data associated with this
548 * device. Filesystem data as well as the underlying block
549 * device. Takes the superblock lock.
550 */
551int fsync_bdev(struct block_device *bdev)
552{
553 struct super_block *sb = get_super(bdev);
554 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200555 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100556 drop_super(sb);
557 return res;
558 }
559 return sync_blockdev(bdev);
560}
Al Viro47e44912009-04-01 07:07:16 -0400561EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100562
563/**
564 * freeze_bdev -- lock a filesystem and force it into a consistent state
565 * @bdev: blockdevice to lock
566 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100567 * If a superblock is found on this device, we take the s_umount semaphore
568 * on it to make sure nobody unmounts until the snapshot creation is done.
569 * The reference counter (bd_fsfreeze_count) guarantees that only the last
570 * unfreeze process can unfreeze the frozen filesystem actually when multiple
571 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
572 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
573 * actually.
574 */
575struct super_block *freeze_bdev(struct block_device *bdev)
576{
577 struct super_block *sb;
578 int error = 0;
579
580 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200581 if (++bdev->bd_fsfreeze_count > 1) {
582 /*
583 * We don't even need to grab a reference - the first call
584 * to freeze_bdev grab an active reference and only the last
585 * thaw_bdev drops it.
586 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100587 sb = get_super(bdev);
Andrey Ryabinin5bb53c02016-08-23 18:55:31 +0300588 if (sb)
589 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100590 mutex_unlock(&bdev->bd_fsfreeze_mutex);
591 return sb;
592 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100593
Christoph Hellwig45042302009-08-03 23:28:35 +0200594 sb = get_active_super(bdev);
595 if (!sb)
596 goto out;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600597 if (sb->s_op->freeze_super)
598 error = sb->s_op->freeze_super(sb);
599 else
600 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400601 if (error) {
602 deactivate_super(sb);
603 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200604 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400605 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100606 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400607 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200608 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100609 sync_blockdev(bdev);
610 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200611 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100612}
613EXPORT_SYMBOL(freeze_bdev);
614
615/**
616 * thaw_bdev -- unlock filesystem
617 * @bdev: blockdevice to unlock
618 * @sb: associated superblock
619 *
620 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
621 */
622int thaw_bdev(struct block_device *bdev, struct super_block *sb)
623{
Christoph Hellwig45042302009-08-03 23:28:35 +0200624 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100625
626 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200627 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400628 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100629
Christoph Hellwig45042302009-08-03 23:28:35 +0200630 error = 0;
631 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400632 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100633
Christoph Hellwig45042302009-08-03 23:28:35 +0200634 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400635 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200636
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600637 if (sb->s_op->thaw_super)
638 error = sb->s_op->thaw_super(sb);
639 else
640 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200641 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400642 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400643out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100644 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200645 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100646}
647EXPORT_SYMBOL(thaw_bdev);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
650{
651 return block_write_full_page(page, blkdev_get_block, wbc);
652}
653
654static int blkdev_readpage(struct file * file, struct page * page)
655{
656 return block_read_full_page(page, blkdev_get_block);
657}
658
Akinobu Mita447f05b2014-10-09 15:26:58 -0700659static int blkdev_readpages(struct file *file, struct address_space *mapping,
660 struct list_head *pages, unsigned nr_pages)
661{
662 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
663}
664
Nick Piggin6272b5a2007-10-16 01:25:04 -0700665static int blkdev_write_begin(struct file *file, struct address_space *mapping,
666 loff_t pos, unsigned len, unsigned flags,
667 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200669 return block_write_begin(mapping, pos, len, flags, pagep,
670 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Nick Piggin6272b5a2007-10-16 01:25:04 -0700673static int blkdev_write_end(struct file *file, struct address_space *mapping,
674 loff_t pos, unsigned len, unsigned copied,
675 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700677 int ret;
678 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
679
680 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300681 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700682
683 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684}
685
686/*
687 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500688 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * so we compute the size by hand (just as in block_read/write above)
690 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800691static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900693 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 loff_t retval;
695
Al Viro59551022016-01-22 15:40:57 -0500696 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400697 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500698 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return retval;
700}
701
Josef Bacik02c24a82011-07-16 20:44:56 -0400702int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900704 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400705 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100706 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200707
Jeff Layton372cf242017-07-06 07:02:28 -0400708 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200709 if (error)
710 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100711
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400712 /*
713 * There is no need to serialise calls to blkdev_issue_flush with
714 * i_mutex and doing so causes performance issues with concurrent
715 * O_SYNC writers to a block device.
716 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200717 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100718 if (error == -EOPNOTSUPP)
719 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400720
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100721 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700723EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700725/**
726 * bdev_read_page() - Start reading a page from a block device
727 * @bdev: The device to read the page from
728 * @sector: The offset on the device to read the page to (need not be aligned)
729 * @page: The page to read
730 *
731 * On entry, the page should be locked. It will be unlocked when the page
732 * has been read. If the block driver implements rw_page synchronously,
733 * that will be true on exit from this function, but it need not be.
734 *
735 * Errors returned by this function are usually "soft", eg out of memory, or
736 * queue full; callers should try a different route to read this page rather
737 * than propagate an error back up the stack.
738 *
739 * Return: negative errno if an error occurs, 0 if submission was successful.
740 */
741int bdev_read_page(struct block_device *bdev, sector_t sector,
742 struct page *page)
743{
744 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800745 int result = -EOPNOTSUPP;
746
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400747 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc952015-11-19 13:29:28 -0800748 return result;
749
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800750 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800751 if (result)
752 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700753 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
754 REQ_OP_READ);
Dan Williams2e6edc952015-11-19 13:29:28 -0800755 blk_queue_exit(bdev->bd_queue);
756 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700757}
758EXPORT_SYMBOL_GPL(bdev_read_page);
759
760/**
761 * bdev_write_page() - Start writing a page to a block device
762 * @bdev: The device to write the page to
763 * @sector: The offset on the device to write the page to (need not be aligned)
764 * @page: The page to write
765 * @wbc: The writeback_control for the write
766 *
767 * On entry, the page should be locked and not currently under writeback.
768 * On exit, if the write started successfully, the page will be unlocked and
769 * under writeback. If the write failed already (eg the driver failed to
770 * queue the page to the device), the page will still be locked. If the
771 * caller is a ->writepage implementation, it will need to unlock the page.
772 *
773 * Errors returned by this function are usually "soft", eg out of memory, or
774 * queue full; callers should try a different route to write this page rather
775 * than propagate an error back up the stack.
776 *
777 * Return: negative errno if an error occurs, 0 if submission was successful.
778 */
779int bdev_write_page(struct block_device *bdev, sector_t sector,
780 struct page *page, struct writeback_control *wbc)
781{
782 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700783 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc952015-11-19 13:29:28 -0800784
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400785 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700786 return -EOPNOTSUPP;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800787 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc952015-11-19 13:29:28 -0800788 if (result)
789 return result;
790
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700791 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700792 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
793 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700794 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700795 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700796 } else {
797 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700798 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700799 }
Dan Williams2e6edc952015-11-19 13:29:28 -0800800 blk_queue_exit(bdev->bd_queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700801 return result;
802}
803EXPORT_SYMBOL_GPL(bdev_write_page);
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805/*
806 * pseudo-fs
807 */
808
809static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800810static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812static struct inode *bdev_alloc_inode(struct super_block *sb)
813{
Christoph Lametere94b1762006-12-06 20:33:17 -0800814 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!ei)
816 return NULL;
817 return &ei->vfs_inode;
818}
819
Al Viro41149cb2019-04-10 15:12:38 -0400820static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
Al Viro41149cb2019-04-10 15:12:38 -0400822 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100823}
824
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700825static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
827 struct bdev_inode *ei = (struct bdev_inode *) foo;
828 struct block_device *bdev = &ei->bdev;
829
Christoph Lametera35afb82007-05-16 22:10:57 -0700830 memset(bdev, 0, sizeof(*bdev));
831 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700832 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100833#ifdef CONFIG_SYSFS
834 INIT_LIST_HEAD(&bdev->bd_holder_disks);
835#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100836 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700837 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800838 /* Initialize mutex for freeze. */
839 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
841
Al Virob57922d2010-06-07 14:34:48 -0400842static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
844 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700845 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400846 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200847 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 spin_lock(&bdev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 list_del_init(&bdev->bd_list);
850 spin_unlock(&bdev_lock);
Jan Karaf7597412017-03-23 01:37:00 +0100851 /* Detach inode from wb early as bdi_put() may free bdi->wb */
852 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100853 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100854 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100855 bdev->bd_bdi = &noop_backing_dev_info;
856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800859static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 .statfs = simple_statfs,
861 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400862 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400864 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865};
866
David Howells9030d162019-03-25 16:38:23 +0000867static int bd_init_fs_context(struct fs_context *fc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868{
David Howells9030d162019-03-25 16:38:23 +0000869 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
870 if (!ctx)
871 return -ENOMEM;
872 fc->s_iflags |= SB_I_CGROUPWB;
873 ctx->ops = &bdev_sops;
874 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877static struct file_system_type bd_type = {
878 .name = "bdev",
David Howells9030d162019-03-25 16:38:23 +0000879 .init_fs_context = bd_init_fs_context,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 .kill_sb = kill_anon_super,
881};
882
Tejun Heoa212b102015-05-22 17:13:33 -0400883struct super_block *blockdev_superblock __read_mostly;
884EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886void __init bdev_cache_init(void)
887{
888 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300889 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800892 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800893 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900894 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 err = register_filesystem(&bd_type);
896 if (err)
897 panic("Cannot register bdev pseudo-fs");
898 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 if (IS_ERR(bd_mnt))
900 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300901 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
904/*
905 * Most likely _very_ bad one - but then it's hardly critical for small
906 * /dev and can be fixed when somebody will need really large one.
907 * Keep in mind that it will be fed through icache hash function too.
908 */
909static inline unsigned long hash(dev_t dev)
910{
911 return MAJOR(dev)+MINOR(dev);
912}
913
914static int bdev_test(struct inode *inode, void *data)
915{
916 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
917}
918
919static int bdev_set(struct inode *inode, void *data)
920{
921 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
922 return 0;
923}
924
925static LIST_HEAD(all_bdevs);
926
Jan Karaf44f1ab2017-02-02 15:56:49 +0100927/*
928 * If there is a bdev inode for this device, unhash it so that it gets evicted
929 * as soon as last inode reference is dropped.
930 */
931void bdev_unhash_inode(dev_t dev)
932{
933 struct inode *inode;
934
935 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
936 if (inode) {
937 remove_inode_hash(inode);
938 iput(inode);
939 }
940}
941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942struct block_device *bdget(dev_t dev)
943{
944 struct block_device *bdev;
945 struct inode *inode;
946
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800947 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 bdev_test, bdev_set, &dev);
949
950 if (!inode)
951 return NULL;
952
953 bdev = &BDEV_I(inode)->bdev;
954
955 if (inode->i_state & I_NEW) {
956 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000957 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 bdev->bd_inode = inode;
Fabian Frederick93407472017-02-27 14:28:32 -0800959 bdev->bd_block_size = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 bdev->bd_part_count = 0;
961 bdev->bd_invalidated = 0;
962 inode->i_mode = S_IFBLK;
963 inode->i_rdev = dev;
964 inode->i_bdev = bdev;
965 inode->i_data.a_ops = &def_blk_aops;
966 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 spin_lock(&bdev_lock);
968 list_add(&bdev->bd_list, &all_bdevs);
969 spin_unlock(&bdev_lock);
970 unlock_new_inode(inode);
971 }
972 return bdev;
973}
974
975EXPORT_SYMBOL(bdget);
976
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200977/**
978 * bdgrab -- Grab a reference to an already referenced block device
979 * @bdev: Block device to grab a reference to.
980 */
981struct block_device *bdgrab(struct block_device *bdev)
982{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400983 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200984 return bdev;
985}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700986EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200987
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988long nr_blockdev_pages(void)
989{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700990 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 long ret = 0;
992 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700993 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 ret += bdev->bd_inode->i_mapping->nrpages;
995 }
996 spin_unlock(&bdev_lock);
997 return ret;
998}
999
1000void bdput(struct block_device *bdev)
1001{
1002 iput(bdev->bd_inode);
1003}
1004
1005EXPORT_SYMBOL(bdput);
1006
1007static struct block_device *bd_acquire(struct inode *inode)
1008{
1009 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001010
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 spin_lock(&bdev_lock);
1012 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +01001013 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001014 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 spin_unlock(&bdev_lock);
1016 return bdev;
1017 }
1018 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001019
Jan Karacccd9fb2017-02-21 18:09:48 +01001020 /*
1021 * i_bdev references block device inode that was already shut down
1022 * (corresponding device got removed). Remove the reference and look
1023 * up block device inode again just in case new device got
1024 * reestablished under the same device number.
1025 */
1026 if (bdev)
1027 bd_forget(inode);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 bdev = bdget(inode->i_rdev);
1030 if (bdev) {
1031 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001032 if (!inode->i_bdev) {
1033 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -04001034 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001035 * and it's released in clear_inode() of inode.
1036 * So, we can access it via ->i_mapping always
1037 * without igrab().
1038 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001039 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001040 inode->i_bdev = bdev;
1041 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 spin_unlock(&bdev_lock);
1044 }
1045 return bdev;
1046}
1047
1048/* Call when you free inode */
1049
1050void bd_forget(struct inode *inode)
1051{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001052 struct block_device *bdev = NULL;
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001055 if (!sb_is_blkdev_sb(inode->i_sb))
1056 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001057 inode->i_bdev = NULL;
1058 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001060
1061 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001062 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063}
1064
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001065/**
1066 * bd_may_claim - test whether a block device can be claimed
1067 * @bdev: block device of interest
1068 * @whole: whole block device containing @bdev, may equal @bdev
1069 * @holder: holder trying to claim @bdev
1070 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001071 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001072 *
1073 * CONTEXT:
1074 * spin_lock(&bdev_lock).
1075 *
1076 * RETURNS:
1077 * %true if @bdev can be claimed, %false otherwise.
1078 */
1079static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1080 void *holder)
1081{
1082 if (bdev->bd_holder == holder)
1083 return true; /* already a holder */
1084 else if (bdev->bd_holder != NULL)
1085 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001086 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001087 return true; /* is a whole device which isn't held */
1088
Tejun Heoe525fd82010-11-13 11:55:17 +01001089 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001090 return true; /* is a partition of a device that is being partitioned */
1091 else if (whole->bd_holder != NULL)
1092 return false; /* is a partition of a held device */
1093 else
1094 return true; /* is a partition of an un-held device */
1095}
1096
1097/**
Tejun Heo6b4517a2010-04-07 18:53:59 +09001098 * bd_prepare_to_claim - prepare to claim a block device
1099 * @bdev: block device of interest
1100 * @whole: the whole device containing @bdev, may equal @bdev
1101 * @holder: holder trying to claim @bdev
1102 *
1103 * Prepare to claim @bdev. This function fails if @bdev is already
1104 * claimed by another holder and waits if another claiming is in
1105 * progress. This function doesn't actually claim. On successful
1106 * return, the caller has ownership of bd_claiming and bd_holder[s].
1107 *
1108 * CONTEXT:
1109 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1110 * it multiple times.
1111 *
1112 * RETURNS:
1113 * 0 if @bdev can be claimed, -EBUSY otherwise.
1114 */
1115static int bd_prepare_to_claim(struct block_device *bdev,
1116 struct block_device *whole, void *holder)
1117{
1118retry:
1119 /* if someone else claimed, fail */
1120 if (!bd_may_claim(bdev, whole, holder))
1121 return -EBUSY;
1122
Tejun Heoe75aa852010-08-04 17:59:39 +02001123 /* if claiming is already in progress, wait for it to finish */
1124 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001125 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1126 DEFINE_WAIT(wait);
1127
1128 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1129 spin_unlock(&bdev_lock);
1130 schedule();
1131 finish_wait(wq, &wait);
1132 spin_lock(&bdev_lock);
1133 goto retry;
1134 }
1135
1136 /* yay, all mine */
1137 return 0;
1138}
1139
Jan Kara560e7cb2018-02-26 13:01:42 +01001140static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1141{
1142 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1143
1144 if (!disk)
1145 return NULL;
1146 /*
1147 * Now that we hold gendisk reference we make sure bdev we looked up is
1148 * not stale. If it is, it means device got removed and created before
1149 * we looked up gendisk and we fail open in such case. Associating
1150 * unhashed bdev with newly created gendisk could lead to two bdevs
1151 * (and thus two independent caches) being associated with one device
1152 * which is bad.
1153 */
1154 if (inode_unhashed(bdev->bd_inode)) {
1155 put_disk_and_module(disk);
1156 return NULL;
1157 }
1158 return disk;
1159}
1160
Tejun Heo6b4517a2010-04-07 18:53:59 +09001161/**
1162 * bd_start_claiming - start claiming a block device
1163 * @bdev: block device of interest
1164 * @holder: holder trying to claim @bdev
1165 *
1166 * @bdev is about to be opened exclusively. Check @bdev can be opened
1167 * exclusively and mark that an exclusive open is in progress. Each
1168 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +10001169 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1170 * fail).
1171 *
1172 * This function is used to gain exclusive access to the block device
1173 * without actually causing other exclusive open attempts to fail. It
1174 * should be used when the open sequence itself requires exclusive
1175 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +09001176 *
1177 * CONTEXT:
1178 * Might sleep.
1179 *
1180 * RETURNS:
1181 * Pointer to the block device containing @bdev on success, ERR_PTR()
1182 * value on failure.
1183 */
Jan Kara89e524c02019-07-30 13:10:14 +02001184struct block_device *bd_start_claiming(struct block_device *bdev, void *holder)
Tejun Heo6b4517a2010-04-07 18:53:59 +09001185{
1186 struct gendisk *disk;
1187 struct block_device *whole;
1188 int partno, err;
1189
1190 might_sleep();
1191
1192 /*
1193 * @bdev might not have been initialized properly yet, look up
1194 * and grab the outer block device the hard way.
1195 */
Jan Kara560e7cb2018-02-26 13:01:42 +01001196 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001197 if (!disk)
1198 return ERR_PTR(-ENXIO);
1199
Tejun Heod4c208b2011-06-13 12:45:48 +02001200 /*
1201 * Normally, @bdev should equal what's returned from bdget_disk()
1202 * if partno is 0; however, some drivers (floppy) use multiple
1203 * bdev's for the same physical device and @bdev may be one of the
1204 * aliases. Keep @bdev if partno is 0. This means claimer
1205 * tracking is broken for those devices but it has always been that
1206 * way.
1207 */
1208 if (partno)
1209 whole = bdget_disk(disk, 0);
1210 else
1211 whole = bdgrab(bdev);
1212
Jan Kara9df6c292018-02-26 13:01:39 +01001213 put_disk_and_module(disk);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001214 if (!whole)
1215 return ERR_PTR(-ENOMEM);
1216
1217 /* prepare to claim, if successful, mark claiming in progress */
1218 spin_lock(&bdev_lock);
1219
1220 err = bd_prepare_to_claim(bdev, whole, holder);
1221 if (err == 0) {
1222 whole->bd_claiming = holder;
1223 spin_unlock(&bdev_lock);
1224 return whole;
1225 } else {
1226 spin_unlock(&bdev_lock);
1227 bdput(whole);
1228 return ERR_PTR(err);
1229 }
1230}
Jan Kara89e524c02019-07-30 13:10:14 +02001231EXPORT_SYMBOL(bd_start_claiming);
1232
1233static void bd_clear_claiming(struct block_device *whole, void *holder)
1234{
1235 lockdep_assert_held(&bdev_lock);
1236 /* tell others that we're done */
1237 BUG_ON(whole->bd_claiming != holder);
1238 whole->bd_claiming = NULL;
1239 wake_up_bit(&whole->bd_claiming, 0);
1240}
1241
1242/**
1243 * bd_finish_claiming - finish claiming of a block device
1244 * @bdev: block device of interest
1245 * @whole: whole block device (returned from bd_start_claiming())
1246 * @holder: holder that has claimed @bdev
1247 *
1248 * Finish exclusive open of a block device. Mark the device as exlusively
1249 * open by the holder and wake up all waiters for exclusive open to finish.
1250 */
1251void bd_finish_claiming(struct block_device *bdev, struct block_device *whole,
1252 void *holder)
1253{
1254 spin_lock(&bdev_lock);
1255 BUG_ON(!bd_may_claim(bdev, whole, holder));
1256 /*
1257 * Note that for a whole device bd_holders will be incremented twice,
1258 * and bd_holder will be set to bd_may_claim before being set to holder
1259 */
1260 whole->bd_holders++;
1261 whole->bd_holder = bd_may_claim;
1262 bdev->bd_holders++;
1263 bdev->bd_holder = holder;
1264 bd_clear_claiming(whole, holder);
1265 spin_unlock(&bdev_lock);
1266}
1267EXPORT_SYMBOL(bd_finish_claiming);
1268
1269/**
1270 * bd_abort_claiming - abort claiming of a block device
1271 * @bdev: block device of interest
1272 * @whole: whole block device (returned from bd_start_claiming())
1273 * @holder: holder that has claimed @bdev
1274 *
1275 * Abort claiming of a block device when the exclusive open failed. This can be
1276 * also used when exclusive open is not actually desired and we just needed
1277 * to block other exclusive openers for a while.
1278 */
1279void bd_abort_claiming(struct block_device *bdev, struct block_device *whole,
1280 void *holder)
1281{
1282 spin_lock(&bdev_lock);
1283 bd_clear_claiming(whole, holder);
1284 spin_unlock(&bdev_lock);
1285}
1286EXPORT_SYMBOL(bd_abort_claiming);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001287
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001288#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001289struct bd_holder_disk {
1290 struct list_head list;
1291 struct gendisk *disk;
1292 int refcnt;
1293};
1294
1295static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1296 struct gendisk *disk)
1297{
1298 struct bd_holder_disk *holder;
1299
1300 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1301 if (holder->disk == disk)
1302 return holder;
1303 return NULL;
1304}
1305
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001306static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001307{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001308 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001309}
1310
1311static void del_symlink(struct kobject *from, struct kobject *to)
1312{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001313 sysfs_remove_link(from, kobject_name(to));
1314}
1315
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001316/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001317 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1318 * @bdev: the claimed slave bdev
1319 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001320 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001321 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1322 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001323 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001324 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001325 * - from "slaves" directory of the holder @disk to the claimed @bdev
1326 * - from "holders" directory of the @bdev to the holder @disk
1327 *
1328 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1329 * passed to bd_link_disk_holder(), then:
1330 *
1331 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1332 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1333 *
1334 * The caller must have claimed @bdev before calling this function and
1335 * ensure that both @bdev and @disk are valid during the creation and
1336 * lifetime of these symlinks.
1337 *
1338 * CONTEXT:
1339 * Might sleep.
1340 *
1341 * RETURNS:
1342 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001343 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001344int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001345{
Tejun Heo49731ba2011-01-14 18:43:57 +01001346 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001347 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001348
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001349 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001350
Tejun Heo49731ba2011-01-14 18:43:57 +01001351 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001352
Tejun Heoe09b4572010-11-13 11:55:17 +01001353 /* FIXME: remove the following once add_disk() handles errors */
1354 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1355 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001356
Tejun Heo49731ba2011-01-14 18:43:57 +01001357 holder = bd_find_holder_disk(bdev, disk);
1358 if (holder) {
1359 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001360 goto out_unlock;
1361 }
1362
Tejun Heo49731ba2011-01-14 18:43:57 +01001363 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1364 if (!holder) {
1365 ret = -ENOMEM;
1366 goto out_unlock;
1367 }
1368
1369 INIT_LIST_HEAD(&holder->list);
1370 holder->disk = disk;
1371 holder->refcnt = 1;
1372
1373 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1374 if (ret)
1375 goto out_free;
1376
1377 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1378 if (ret)
1379 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001380 /*
1381 * bdev could be deleted beneath us which would implicitly destroy
1382 * the holder directory. Hold on to it.
1383 */
1384 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001385
1386 list_add(&holder->list, &bdev->bd_holder_disks);
1387 goto out_unlock;
1388
1389out_del:
1390 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1391out_free:
1392 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001393out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001394 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001395 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001396}
Tejun Heoe09b4572010-11-13 11:55:17 +01001397EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001398
Tejun Heo49731ba2011-01-14 18:43:57 +01001399/**
1400 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1401 * @bdev: the calimed slave bdev
1402 * @disk: the holding disk
1403 *
1404 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1405 *
1406 * CONTEXT:
1407 * Might sleep.
1408 */
1409void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001410{
Tejun Heo49731ba2011-01-14 18:43:57 +01001411 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001412
Tejun Heo49731ba2011-01-14 18:43:57 +01001413 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001414
Tejun Heo49731ba2011-01-14 18:43:57 +01001415 holder = bd_find_holder_disk(bdev, disk);
1416
1417 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1418 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1419 del_symlink(bdev->bd_part->holder_dir,
1420 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001421 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001422 list_del_init(&holder->list);
1423 kfree(holder);
1424 }
1425
1426 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001427}
Tejun Heo49731ba2011-01-14 18:43:57 +01001428EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001429#endif
1430
Andrew Patterson0c002c22008-09-04 14:27:20 -06001431/**
Andrew Patterson56ade442008-09-04 14:27:40 -06001432 * flush_disk - invalidates all buffer-cache entries on a disk
1433 *
1434 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -08001435 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -06001436 *
1437 * Invalidates all buffer-cache entries on a disk. It should be called
1438 * when a disk has been changed -- either by a media change or online
1439 * resize.
1440 */
NeilBrown93b270f2011-02-24 17:25:47 +11001441static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -06001442{
NeilBrown93b270f2011-02-24 17:25:47 +11001443 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -06001444 printk(KERN_WARNING "VFS: busy inodes on changed media or "
Dmitry Monakhov424081f2015-04-13 16:31:34 +04001445 "resized disk %s\n",
1446 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
Andrew Patterson56ade442008-09-04 14:27:40 -06001447 }
1448
1449 if (!bdev->bd_disk)
1450 return;
Tejun Heod27769e2011-08-23 20:01:04 +02001451 if (disk_part_scan_enabled(bdev->bd_disk))
Andrew Patterson56ade442008-09-04 14:27:40 -06001452 bdev->bd_invalidated = 1;
1453}
1454
1455/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001456 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001457 * @disk: struct gendisk to check
1458 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001459 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001460 *
1461 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001462 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1463 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001464 */
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001465void check_disk_size_change(struct gendisk *disk, struct block_device *bdev,
1466 bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001467{
1468 loff_t disk_size, bdev_size;
1469
1470 disk_size = (loff_t)get_capacity(disk) << 9;
1471 bdev_size = i_size_read(bdev->bd_inode);
1472 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001473 if (verbose) {
1474 printk(KERN_INFO
1475 "%s: detected capacity change from %lld to %lld\n",
1476 disk->disk_name, bdev_size, disk_size);
1477 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001478 i_size_write(bdev->bd_inode, disk_size);
shunki-fujita849cf552018-04-05 16:20:07 -07001479 if (bdev_size > disk_size)
1480 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001481 }
1482}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001483
1484/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001485 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001486 * @disk: struct gendisk to be revalidated
1487 *
1488 * This routine is a wrapper for lower-level driver's revalidate_disk
1489 * call-backs. It is used to do common pre and post operations needed
1490 * for all revalidate_disk operations.
1491 */
1492int revalidate_disk(struct gendisk *disk)
1493{
1494 int ret = 0;
1495
1496 if (disk->fops->revalidate_disk)
1497 ret = disk->fops->revalidate_disk(disk);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001498
Jan Kara31cb1d62019-05-15 08:57:40 +02001499 /*
1500 * Hidden disks don't have associated bdev so there's no point in
1501 * revalidating it.
1502 */
1503 if (!(disk->flags & GENHD_FL_HIDDEN)) {
1504 struct block_device *bdev = bdget_disk(disk, 0);
1505
1506 if (!bdev)
1507 return ret;
1508
1509 mutex_lock(&bdev->bd_mutex);
1510 check_disk_size_change(disk, bdev, ret == 0);
1511 bdev->bd_invalidated = 0;
1512 mutex_unlock(&bdev->bd_mutex);
1513 bdput(bdev);
1514 }
Andrew Patterson0c002c22008-09-04 14:27:20 -06001515 return ret;
1516}
1517EXPORT_SYMBOL(revalidate_disk);
1518
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519/*
1520 * This routine checks whether a removable media has been changed,
1521 * and invalidates all buffer-cache-entries in that case. This
1522 * is a relatively slow routine, so we have to try to minimize using
1523 * it. Thus it is called only upon a 'mount' or 'open'. This
1524 * is the best way of combining speed and utility, I think.
1525 * People changing diskettes in the middle of an operation deserve
1526 * to lose :-)
1527 */
1528int check_disk_change(struct block_device *bdev)
1529{
1530 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001531 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001532 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Tejun Heo77ea8872010-12-08 20:57:37 +01001534 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1535 DISK_EVENT_EJECT_REQUEST);
1536 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 return 0;
1538
NeilBrown93b270f2011-02-24 17:25:47 +11001539 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (bdops->revalidate_disk)
1541 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 return 1;
1543}
1544
1545EXPORT_SYMBOL(check_disk_change);
1546
1547void bd_set_size(struct block_device *bdev, loff_t size)
1548{
Al Viro59551022016-01-22 15:40:57 -05001549 inode_lock(bdev->bd_inode);
Guo Chaod646a022013-02-21 15:16:42 -08001550 i_size_write(bdev->bd_inode, size);
Al Viro59551022016-01-22 15:40:57 -05001551 inode_unlock(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552}
1553EXPORT_SYMBOL(bd_set_size);
1554
Al Viro4385bab2013-05-05 22:11:03 -04001555static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001556
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001557/*
1558 * bd_mutex locking:
1559 *
1560 * mutex_lock(part->bd_mutex)
1561 * mutex_lock_nested(whole->bd_mutex, 1)
1562 */
1563
Al Viro572c4892007-10-08 13:24:05 -04001564static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001567 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001568 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001569 int perm = 0;
Jan Kara89736652018-02-26 13:01:40 +01001570 bool first_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571
Al Viro572c4892007-10-08 13:24:05 -04001572 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001573 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001574 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001575 perm |= MAY_WRITE;
1576 /*
1577 * hooks: /n/, see "layering violations".
1578 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001579 if (!for_part) {
1580 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1581 if (ret != 0) {
1582 bdput(bdev);
1583 return ret;
1584 }
Al Viro82666022008-08-01 05:32:04 -04001585 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001586
NeilBrownd3374822009-01-09 08:31:10 +11001587 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001588
Tejun Heo89f97492008-11-05 10:21:06 +01001589 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001590 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001591 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001592 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
Tejun Heo69e02c52011-03-09 19:54:27 +01001594 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001595 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001597 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 bdev->bd_disk = disk;
Andi Kleen87192a22012-01-12 17:20:34 -08001599 bdev->bd_queue = disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001601 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001602
Tejun Heocf771cb2008-09-03 09:01:09 +02001603 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001604 ret = -ENXIO;
1605 bdev->bd_part = disk_get_part(disk, partno);
1606 if (!bdev->bd_part)
1607 goto out_clear;
1608
Tejun Heo1196f8b2011-04-21 20:54:45 +02001609 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001611 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001612 if (ret == -ERESTARTSYS) {
1613 /* Lost a race with 'disk' being
1614 * deleted, try again.
1615 * See md.c
1616 */
1617 disk_put_part(bdev->bd_part);
1618 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001619 bdev->bd_disk = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001620 bdev->bd_queue = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001621 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001622 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001623 put_disk_and_module(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001624 goto restart;
1625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Tejun Heo7e697232011-05-23 13:26:07 +02001627
Jan Kara04906b22019-01-14 09:48:10 +01001628 if (!ret) {
Tejun Heo7e697232011-05-23 13:26:07 +02001629 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
Jan Kara04906b22019-01-14 09:48:10 +01001630 set_init_blocksize(bdev);
1631 }
Tejun Heo7e697232011-05-23 13:26:07 +02001632
Tejun Heo1196f8b2011-04-21 20:54:45 +02001633 /*
1634 * If the device is invalidated, rescan partition
1635 * if open succeeded or failed with -ENOMEDIUM.
1636 * The latter is necessary to prevent ghost
1637 * partitions on a removed medium.
1638 */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001639 if (bdev->bd_invalidated) {
1640 if (!ret)
1641 rescan_partitions(disk, bdev);
1642 else if (ret == -ENOMEDIUM)
1643 invalidate_partitions(disk, bdev);
1644 }
Dan Williams5a023cd2015-11-30 10:20:29 -08001645
Tejun Heo1196f8b2011-04-21 20:54:45 +02001646 if (ret)
1647 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 struct block_device *whole;
1650 whole = bdget_disk(disk, 0);
1651 ret = -ENOMEM;
1652 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001653 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001654 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001655 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001657 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 bdev->bd_contains = whole;
Tejun Heo89f97492008-11-05 10:21:06 +01001659 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001660 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001661 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001663 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 }
Tejun Heo89f97492008-11-05 10:21:06 +01001665 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Jan Kara04906b22019-01-14 09:48:10 +01001666 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 }
Jan Kara03e26272017-03-23 01:36:53 +01001668
1669 if (bdev->bd_bdi == &noop_backing_dev_info)
1670 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001673 ret = 0;
1674 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001675 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001676 /* the same as first opener case, read comment there */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001677 if (bdev->bd_invalidated) {
1678 if (!ret)
1679 rescan_partitions(bdev->bd_disk, bdev);
1680 else if (ret == -ENOMEDIUM)
1681 invalidate_partitions(bdev->bd_disk, bdev);
1682 }
Tejun Heo1196f8b2011-04-21 20:54:45 +02001683 if (ret)
1684 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686 }
1687 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001688 if (for_part)
1689 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001690 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001691 disk_unblock_events(disk);
Jan Kara89736652018-02-26 13:01:40 +01001692 /* only one opener holds refs to the module and disk */
1693 if (!first_open)
1694 put_disk_and_module(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 return 0;
1696
Tejun Heo0762b8b2008-08-25 19:56:12 +09001697 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001698 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001700 bdev->bd_part = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001701 bdev->bd_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001703 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001705 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001706 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001707 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001708 put_disk_and_module(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001709 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001710 bdput(bdev);
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 return ret;
1713}
1714
Tejun Heod4d77622010-11-13 11:55:18 +01001715/**
1716 * blkdev_get - open a block device
1717 * @bdev: block_device to open
1718 * @mode: FMODE_* mask
1719 * @holder: exclusive holder identifier
1720 *
1721 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1722 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1723 * @holder is invalid. Exclusive opens may nest for the same @holder.
1724 *
1725 * On success, the reference count of @bdev is unchanged. On failure,
1726 * @bdev is put.
1727 *
1728 * CONTEXT:
1729 * Might sleep.
1730 *
1731 * RETURNS:
1732 * 0 on success, -errno on failure.
1733 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001734int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735{
Tejun Heoe525fd82010-11-13 11:55:17 +01001736 struct block_device *whole = NULL;
1737 int res;
1738
1739 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1740
1741 if ((mode & FMODE_EXCL) && holder) {
1742 whole = bd_start_claiming(bdev, holder);
1743 if (IS_ERR(whole)) {
1744 bdput(bdev);
1745 return PTR_ERR(whole);
1746 }
1747 }
1748
1749 res = __blkdev_get(bdev, mode, 0);
1750
1751 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001752 struct gendisk *disk = whole->bd_disk;
1753
Tejun Heo6a027ef2010-11-13 11:55:17 +01001754 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001755 mutex_lock(&bdev->bd_mutex);
Jan Kara89e524c02019-07-30 13:10:14 +02001756 bd_finish_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}