blob: f80045048bb7c00686a4fc34bf8632735c672baf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/block_dev.c
3 *
4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
6 */
7
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/init.h>
9#include <linux/mm.h>
10#include <linux/fcntl.h>
11#include <linux/slab.h>
12#include <linux/kmod.h>
13#include <linux/major.h>
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -070014#include <linux/device_cgroup.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/highmem.h>
16#include <linux/blkdev.h>
Tejun Heo66114ca2015-05-22 17:13:32 -040017#include <linux/backing-dev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/module.h>
19#include <linux/blkpg.h>
Muthu Kumarb502bd12012-03-23 15:01:50 -070020#include <linux/magic.h>
Dan Williamsb0686262017-01-26 20:37:35 -080021#include <linux/dax.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/buffer_head.h>
Al Viroff01bb42011-09-16 02:31:11 -040023#include <linux/swap.h>
Nick Piggin585d3bc2009-02-25 10:44:19 +010024#include <linux/pagevec.h>
David Howells811d7362006-08-29 19:06:09 +010025#include <linux/writeback.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/mpage.h>
27#include <linux/mount.h>
28#include <linux/uio.h>
29#include <linux/namei.h>
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -070030#include <linux/log2.h>
Al Viroff01bb42011-09-16 02:31:11 -040031#include <linux/cleancache.h>
Matthew Wilcoxc94c2ac2015-09-08 14:58:40 -070032#include <linux/dax.h>
Christoph Hellwig189ce2b2016-10-31 11:59:25 -060033#include <linux/task_io_accounting_ops.h>
Darrick J. Wong25f4c412016-10-11 13:51:11 -070034#include <linux/falloc.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080035#include <linux/uaccess.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38struct bdev_inode {
39 struct block_device bdev;
40 struct inode vfs_inode;
41};
42
Adrian Bunk4c54ac62008-02-18 13:48:31 +010043static const struct address_space_operations def_blk_aops;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045static inline struct bdev_inode *BDEV_I(struct inode *inode)
46{
47 return container_of(inode, struct bdev_inode, vfs_inode);
48}
49
Geert Uytterhoevenff5053f2015-06-26 13:58:32 +020050struct block_device *I_BDEV(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
52 return &BDEV_I(inode)->bdev;
53}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054EXPORT_SYMBOL(I_BDEV);
55
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070056static void bdev_write_inode(struct block_device *bdev)
Christoph Hellwig564f00f2015-01-14 10:42:33 +010057{
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070058 struct inode *inode = bdev->bd_inode;
59 int ret;
60
Christoph Hellwig564f00f2015-01-14 10:42:33 +010061 spin_lock(&inode->i_lock);
62 while (inode->i_state & I_DIRTY) {
63 spin_unlock(&inode->i_lock);
Vivek Goyaldbd3ca52015-11-09 09:23:40 -070064 ret = write_inode_now(inode, true);
65 if (ret) {
66 char name[BDEVNAME_SIZE];
67 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
68 "for block device %s (err=%d).\n",
69 bdevname(bdev, name), ret);
70 }
Christoph Hellwig564f00f2015-01-14 10:42:33 +010071 spin_lock(&inode->i_lock);
72 }
73 spin_unlock(&inode->i_lock);
74}
75
Peter Zijlstraf9a14392007-05-06 14:49:55 -070076/* Kill _all_ buffers and pagecache , dirty or not.. */
Al Viroff01bb42011-09-16 02:31:11 -040077void kill_bdev(struct block_device *bdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Al Viroff01bb42011-09-16 02:31:11 -040079 struct address_space *mapping = bdev->bd_inode->i_mapping;
80
Ross Zwislerf9fe48b2016-01-22 15:10:40 -080081 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
Peter Zijlstraf9a14392007-05-06 14:49:55 -070082 return;
Al Viroff01bb42011-09-16 02:31:11 -040083
Peter Zijlstraf9a14392007-05-06 14:49:55 -070084 invalidate_bh_lrus();
Al Viroff01bb42011-09-16 02:31:11 -040085 truncate_inode_pages(mapping, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Al Viroff01bb42011-09-16 02:31:11 -040087EXPORT_SYMBOL(kill_bdev);
88
89/* Invalidate clean unused buffers and pagecache. */
90void invalidate_bdev(struct block_device *bdev)
91{
92 struct address_space *mapping = bdev->bd_inode->i_mapping;
93
Andrey Ryabinina5f6a6a2017-05-03 14:56:02 -070094 if (mapping->nrpages) {
95 invalidate_bh_lrus();
96 lru_add_drain_all(); /* make sure all lru add caches are flushed */
97 invalidate_mapping_pages(mapping, 0, -1);
98 }
Al Viroff01bb42011-09-16 02:31:11 -040099 /* 99% of the time, we don't need to flush the cleancache on the bdev.
100 * But, for the strange corners, lets be cautious
101 */
Dan Magenheimer31677602011-09-21 11:56:28 -0400102 cleancache_invalidate_inode(mapping);
Al Viroff01bb42011-09-16 02:31:11 -0400103}
104EXPORT_SYMBOL(invalidate_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jan Kara04906b22019-01-14 09:48:10 +0100106static void set_init_blocksize(struct block_device *bdev)
107{
108 unsigned bsize = bdev_logical_block_size(bdev);
109 loff_t size = i_size_read(bdev->bd_inode);
110
111 while (bsize < PAGE_SIZE) {
112 if (size & bsize)
113 break;
114 bsize <<= 1;
115 }
116 bdev->bd_block_size = bsize;
117 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
118}
119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120int set_blocksize(struct block_device *bdev, int size)
121{
122 /* Size must be a power of two, and between 512 and PAGE_SIZE */
Vignesh Babu BM1368c4f2007-05-08 00:24:32 -0700123 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return -EINVAL;
125
126 /* Size cannot be smaller than the size supported by the device */
Martin K. Petersene1defc42009-05-22 17:17:49 -0400127 if (size < bdev_logical_block_size(bdev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 return -EINVAL;
129
130 /* Don't change the size if it is same as current */
131 if (bdev->bd_block_size != size) {
132 sync_blockdev(bdev);
133 bdev->bd_block_size = size;
134 bdev->bd_inode->i_blkbits = blksize_bits(size);
135 kill_bdev(bdev);
136 }
137 return 0;
138}
139
140EXPORT_SYMBOL(set_blocksize);
141
142int sb_set_blocksize(struct super_block *sb, int size)
143{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (set_blocksize(sb->s_bdev, size))
145 return 0;
146 /* If we get here, we know size is power of two
147 * and it's value is between 512 and PAGE_SIZE */
148 sb->s_blocksize = size;
Coywolf Qi Hunt38885bd2006-03-24 03:18:05 -0800149 sb->s_blocksize_bits = blksize_bits(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return sb->s_blocksize;
151}
152
153EXPORT_SYMBOL(sb_set_blocksize);
154
155int sb_min_blocksize(struct super_block *sb, int size)
156{
Martin K. Petersene1defc42009-05-22 17:17:49 -0400157 int minsize = bdev_logical_block_size(sb->s_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (size < minsize)
159 size = minsize;
160 return sb_set_blocksize(sb, size);
161}
162
163EXPORT_SYMBOL(sb_min_blocksize);
164
165static int
166blkdev_get_block(struct inode *inode, sector_t iblock,
167 struct buffer_head *bh, int create)
168{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 bh->b_bdev = I_BDEV(inode);
170 bh->b_blocknr = iblock;
171 set_buffer_mapped(bh);
172 return 0;
173}
174
Dan Williams4ebb16c2015-10-28 07:48:19 +0900175static struct inode *bdev_file_inode(struct file *file)
176{
177 return file->f_mapping->host;
178}
179
Jens Axboe78250c02016-11-17 17:50:47 +0100180static unsigned int dio_bio_write_op(struct kiocb *iocb)
181{
182 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
183
184 /* avoid the need for a I/O completion work item */
185 if (iocb->ki_flags & IOCB_DSYNC)
186 op |= REQ_FUA;
187 return op;
188}
189
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600190#define DIO_INLINE_BIO_VECS 4
191
192static void blkdev_bio_end_io_simple(struct bio *bio)
193{
194 struct task_struct *waiter = bio->bi_private;
195
196 WRITE_ONCE(bio->bi_private, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700197 blk_wake_io_task(waiter);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600198}
199
200static ssize_t
201__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
202 int nr_pages)
203{
204 struct file *file = iocb->ki_filp;
205 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Jens Axboe72ecad22016-11-16 23:11:42 -0700206 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs, *bvec;
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600207 loff_t pos = iocb->ki_pos;
208 bool should_dirty = false;
209 struct bio bio;
210 ssize_t ret;
211 blk_qc_t qc;
Ming Lei6dc4f102019-02-15 19:13:19 +0800212 struct bvec_iter_all iter_all;
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 Hellwig2b070cf2019-04-25 09:03:00 +0200262 bio_for_each_segment_all(bvec, &bio, iter_all) {
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600263 if (should_dirty && !PageCompound(bvec->bv_page))
264 set_page_dirty_lock(bvec->bv_page);
Ming Lei60a27b92019-04-26 18:45:20 +0800265 if (!bio_flagged(&bio, BIO_NO_PAGE_REF))
266 put_page(bvec->bv_page);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600267 }
268
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200269 if (unlikely(bio.bi_status))
Linus Torvaldsc6b1e362017-07-03 10:34:51 -0700270 ret = blk_status_to_errno(bio.bi_status);
Jens Axboe9ae3b3f2017-06-28 15:30:13 -0600271
Martin Wilck9362dd12018-07-25 23:15:08 +0200272out:
273 if (vecs != inline_vecs)
274 kfree(vecs);
275
Jens Axboe9ae3b3f2017-06-28 15:30:13 -0600276 bio_uninit(&bio);
277
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600278 return ret;
279}
280
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700281struct blkdev_dio {
282 union {
283 struct kiocb *iocb;
284 struct task_struct *waiter;
285 };
286 size_t size;
287 atomic_t ref;
288 bool multi_bio : 1;
289 bool should_dirty : 1;
290 bool is_sync : 1;
291 struct bio bio;
292};
293
Kent Overstreet52190f82018-05-20 18:25:55 -0400294static struct bio_set blkdev_dio_pool;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700295
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700296static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
297{
298 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
299 struct request_queue *q = bdev_get_queue(bdev);
300
301 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
302}
303
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700304static void blkdev_bio_end_io(struct bio *bio)
305{
306 struct blkdev_dio *dio = bio->bi_private;
307 bool should_dirty = dio->should_dirty;
308
Jason Yana89afe52019-04-12 10:09:16 +0800309 if (bio->bi_status && !dio->bio.bi_status)
310 dio->bio.bi_status = bio->bi_status;
311
312 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700313 if (!dio->is_sync) {
314 struct kiocb *iocb = dio->iocb;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200315 ssize_t ret;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700316
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200317 if (likely(!dio->bio.bi_status)) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700318 ret = dio->size;
319 iocb->ki_pos += ret;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200320 } else {
321 ret = blk_status_to_errno(dio->bio.bi_status);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700322 }
323
324 dio->iocb->ki_complete(iocb, ret, 0);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100325 if (dio->multi_bio)
326 bio_put(&dio->bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700327 } else {
328 struct task_struct *waiter = dio->waiter;
329
330 WRITE_ONCE(dio->waiter, NULL);
Jens Axboe06193172018-11-13 21:16:54 -0700331 blk_wake_io_task(waiter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700332 }
333 }
334
335 if (should_dirty) {
336 bio_check_pages_dirty(bio);
337 } else {
Jens Axboe399254a2019-02-27 13:13:23 -0700338 if (!bio_flagged(bio, BIO_NO_PAGE_REF)) {
339 struct bvec_iter_all iter_all;
340 struct bio_vec *bvec;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700341
Christoph Hellwig2b070cf2019-04-25 09:03:00 +0200342 bio_for_each_segment_all(bvec, bio, iter_all)
Jens Axboe399254a2019-02-27 13:13:23 -0700343 put_page(bvec->bv_page);
344 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700345 bio_put(bio);
346 }
347}
348
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800349static ssize_t
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700350__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800351{
352 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +0900353 struct inode *inode = bdev_file_inode(file);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700354 struct block_device *bdev = I_BDEV(inode);
Christoph Hellwig64d656a2016-12-22 19:20:45 +0100355 struct blk_plug plug;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700356 struct blkdev_dio *dio;
357 struct bio *bio;
Jens Axboecb700eb2018-11-15 19:56:53 -0700358 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
Christoph Hellwig690e5322017-01-24 14:50:19 +0100359 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700360 loff_t pos = iocb->ki_pos;
361 blk_qc_t qc = BLK_QC_T_NONE;
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200362 int ret = 0;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700363
Jens Axboe9a794fb2016-11-22 08:12:39 -0700364 if ((pos | iov_iter_alignment(iter)) &
365 (bdev_logical_block_size(bdev) - 1))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700366 return -EINVAL;
367
Kent Overstreet52190f82018-05-20 18:25:55 -0400368 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700369
370 dio = container_of(bio, struct blkdev_dio, bio);
Christoph Hellwig690e5322017-01-24 14:50:19 +0100371 dio->is_sync = is_sync = is_sync_kiocb(iocb);
Christoph Hellwig531724a2018-11-30 09:23:48 +0100372 if (dio->is_sync) {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700373 dio->waiter = current;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100374 bio_get(bio);
375 } else {
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700376 dio->iocb = iocb;
Christoph Hellwig531724a2018-11-30 09:23:48 +0100377 }
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700378
379 dio->size = 0;
380 dio->multi_bio = false;
David Howells00e23702018-10-22 13:07:28 +0100381 dio->should_dirty = is_read && iter_is_iovec(iter);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700382
Jens Axboecb700eb2018-11-15 19:56:53 -0700383 /*
384 * Don't plug for HIPRI/polled IO, as those should go straight
385 * to issue
386 */
387 if (!is_poll)
388 blk_start_plug(&plug);
389
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700390 for (;;) {
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
398 ret = bio_iov_iter_get_pages(bio, iter);
399 if (unlikely(ret)) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200400 bio->bi_status = BLK_STS_IOERR;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700401 bio_endio(bio);
402 break;
403 }
404
405 if (is_read) {
406 bio->bi_opf = REQ_OP_READ;
407 if (dio->should_dirty)
408 bio_set_pages_dirty(bio);
409 } else {
410 bio->bi_opf = dio_bio_write_op(iocb);
411 task_io_account_write(bio->bi_iter.bi_size);
412 }
413
414 dio->size += bio->bi_iter.bi_size;
415 pos += bio->bi_iter.bi_size;
416
417 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
418 if (!nr_pages) {
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700419 bool polled = false;
420
421 if (iocb->ki_flags & IOCB_HIPRI) {
Jens Axboe0bbb2802018-12-21 09:10:46 -0700422 bio_set_polled(bio, iocb);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700423 polled = true;
424 }
Jens Axboed34513d2018-11-06 14:29:11 -0700425
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700426 qc = submit_bio(bio);
Christoph Hellwigeae83ce2018-11-30 08:31:52 -0700427
428 if (polled)
429 WRITE_ONCE(iocb->ki_cookie, qc);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700430 break;
431 }
432
433 if (!dio->multi_bio) {
Christoph Hellwig531724a2018-11-30 09:23:48 +0100434 /*
435 * AIO needs an extra reference to ensure the dio
436 * structure which is embedded into the first bio
437 * stays around.
438 */
439 if (!is_sync)
440 bio_get(bio);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700441 dio->multi_bio = true;
442 atomic_set(&dio->ref, 2);
443 } else {
444 atomic_inc(&dio->ref);
445 }
446
447 submit_bio(bio);
448 bio = bio_alloc(GFP_KERNEL, nr_pages);
449 }
Jens Axboecb700eb2018-11-15 19:56:53 -0700450
451 if (!is_poll)
452 blk_finish_plug(&plug);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700453
Christoph Hellwig690e5322017-01-24 14:50:19 +0100454 if (!is_sync)
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700455 return -EIOCBQUEUED;
456
457 for (;;) {
Linus Torvalds1ac5cd42019-01-02 10:46:03 -0800458 set_current_state(TASK_UNINTERRUPTIBLE);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700459 if (!READ_ONCE(dio->waiter))
460 break;
461
462 if (!(iocb->ki_flags & IOCB_HIPRI) ||
Jens Axboe0a1b8b82018-11-26 08:24:43 -0700463 !blk_poll(bdev_get_queue(bdev), qc, true))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700464 io_schedule();
465 }
466 __set_current_state(TASK_RUNNING);
467
Christoph Hellwig36ffc6c2017-06-03 09:38:00 +0200468 if (!ret)
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +0200469 ret = blk_status_to_errno(dio->bio.bi_status);
Shaohua Li7a62a522016-12-13 19:07:55 -0800470 if (likely(!ret))
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700471 ret = dio->size;
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700472
473 bio_put(&dio->bio);
474 return ret;
475}
476
477static ssize_t
478blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
479{
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600480 int nr_pages;
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800481
Jens Axboe72ecad22016-11-16 23:11:42 -0700482 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600483 if (!nr_pages)
484 return 0;
Jens Axboe72ecad22016-11-16 23:11:42 -0700485 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
Christoph Hellwig189ce2b2016-10-31 11:59:25 -0600486 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700487
488 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
Andrew Mortonb2e895d2007-02-03 01:14:01 -0800489}
490
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700491static __init int blkdev_init(void)
492{
Kent Overstreet52190f82018-05-20 18:25:55 -0400493 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
Christoph Hellwig542ff7b2016-11-16 23:14:22 -0700494}
495module_init(blkdev_init);
496
Jan Kara5cee5812009-04-27 16:43:51 +0200497int __sync_blockdev(struct block_device *bdev, int wait)
498{
499 if (!bdev)
500 return 0;
501 if (!wait)
502 return filemap_flush(bdev->bd_inode->i_mapping);
503 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
504}
505
Nick Piggin585d3bc2009-02-25 10:44:19 +0100506/*
507 * Write out and wait upon all the dirty data associated with a block
508 * device via its mapping. Does not take the superblock lock.
509 */
510int sync_blockdev(struct block_device *bdev)
511{
Jan Kara5cee5812009-04-27 16:43:51 +0200512 return __sync_blockdev(bdev, 1);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100513}
514EXPORT_SYMBOL(sync_blockdev);
515
516/*
517 * Write out and wait upon all dirty data associated with this
518 * device. Filesystem data as well as the underlying block
519 * device. Takes the superblock lock.
520 */
521int fsync_bdev(struct block_device *bdev)
522{
523 struct super_block *sb = get_super(bdev);
524 if (sb) {
Jan Kara60b06802009-04-27 16:43:53 +0200525 int res = sync_filesystem(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100526 drop_super(sb);
527 return res;
528 }
529 return sync_blockdev(bdev);
530}
Al Viro47e44912009-04-01 07:07:16 -0400531EXPORT_SYMBOL(fsync_bdev);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100532
533/**
534 * freeze_bdev -- lock a filesystem and force it into a consistent state
535 * @bdev: blockdevice to lock
536 *
Nick Piggin585d3bc2009-02-25 10:44:19 +0100537 * If a superblock is found on this device, we take the s_umount semaphore
538 * on it to make sure nobody unmounts until the snapshot creation is done.
539 * The reference counter (bd_fsfreeze_count) guarantees that only the last
540 * unfreeze process can unfreeze the frozen filesystem actually when multiple
541 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
542 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
543 * actually.
544 */
545struct super_block *freeze_bdev(struct block_device *bdev)
546{
547 struct super_block *sb;
548 int error = 0;
549
550 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200551 if (++bdev->bd_fsfreeze_count > 1) {
552 /*
553 * We don't even need to grab a reference - the first call
554 * to freeze_bdev grab an active reference and only the last
555 * thaw_bdev drops it.
556 */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100557 sb = get_super(bdev);
Andrey Ryabinin5bb53c02016-08-23 18:55:31 +0300558 if (sb)
559 drop_super(sb);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100560 mutex_unlock(&bdev->bd_fsfreeze_mutex);
561 return sb;
562 }
Nick Piggin585d3bc2009-02-25 10:44:19 +0100563
Christoph Hellwig45042302009-08-03 23:28:35 +0200564 sb = get_active_super(bdev);
565 if (!sb)
566 goto out;
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600567 if (sb->s_op->freeze_super)
568 error = sb->s_op->freeze_super(sb);
569 else
570 error = freeze_super(sb);
Josef Bacik18e9e512010-03-23 10:34:56 -0400571 if (error) {
572 deactivate_super(sb);
573 bdev->bd_fsfreeze_count--;
Christoph Hellwig45042302009-08-03 23:28:35 +0200574 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Josef Bacik18e9e512010-03-23 10:34:56 -0400575 return ERR_PTR(error);
Nick Piggin585d3bc2009-02-25 10:44:19 +0100576 }
Josef Bacik18e9e512010-03-23 10:34:56 -0400577 deactivate_super(sb);
Christoph Hellwig45042302009-08-03 23:28:35 +0200578 out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100579 sync_blockdev(bdev);
580 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig4fadd7b2009-08-03 23:28:06 +0200581 return sb; /* thaw_bdev releases s->s_umount */
Nick Piggin585d3bc2009-02-25 10:44:19 +0100582}
583EXPORT_SYMBOL(freeze_bdev);
584
585/**
586 * thaw_bdev -- unlock filesystem
587 * @bdev: blockdevice to unlock
588 * @sb: associated superblock
589 *
590 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
591 */
592int thaw_bdev(struct block_device *bdev, struct super_block *sb)
593{
Christoph Hellwig45042302009-08-03 23:28:35 +0200594 int error = -EINVAL;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100595
596 mutex_lock(&bdev->bd_fsfreeze_mutex);
Christoph Hellwig45042302009-08-03 23:28:35 +0200597 if (!bdev->bd_fsfreeze_count)
Josef Bacik18e9e512010-03-23 10:34:56 -0400598 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100599
Christoph Hellwig45042302009-08-03 23:28:35 +0200600 error = 0;
601 if (--bdev->bd_fsfreeze_count > 0)
Josef Bacik18e9e512010-03-23 10:34:56 -0400602 goto out;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100603
Christoph Hellwig45042302009-08-03 23:28:35 +0200604 if (!sb)
Josef Bacik18e9e512010-03-23 10:34:56 -0400605 goto out;
Christoph Hellwig45042302009-08-03 23:28:35 +0200606
Benjamin Marzinski48b6bca2014-11-13 20:42:03 -0600607 if (sb->s_op->thaw_super)
608 error = sb->s_op->thaw_super(sb);
609 else
610 error = thaw_super(sb);
Pierre Morel997198b2016-10-04 10:53:40 +0200611 if (error)
Josef Bacik18e9e512010-03-23 10:34:56 -0400612 bdev->bd_fsfreeze_count++;
Josef Bacik18e9e512010-03-23 10:34:56 -0400613out:
Nick Piggin585d3bc2009-02-25 10:44:19 +0100614 mutex_unlock(&bdev->bd_fsfreeze_mutex);
Pierre Morel997198b2016-10-04 10:53:40 +0200615 return error;
Nick Piggin585d3bc2009-02-25 10:44:19 +0100616}
617EXPORT_SYMBOL(thaw_bdev);
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
620{
621 return block_write_full_page(page, blkdev_get_block, wbc);
622}
623
624static int blkdev_readpage(struct file * file, struct page * page)
625{
626 return block_read_full_page(page, blkdev_get_block);
627}
628
Akinobu Mita447f05b2014-10-09 15:26:58 -0700629static int blkdev_readpages(struct file *file, struct address_space *mapping,
630 struct list_head *pages, unsigned nr_pages)
631{
632 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
633}
634
Nick Piggin6272b5a2007-10-16 01:25:04 -0700635static int blkdev_write_begin(struct file *file, struct address_space *mapping,
636 loff_t pos, unsigned len, unsigned flags,
637 struct page **pagep, void **fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638{
Christoph Hellwig155130a2010-06-04 11:29:58 +0200639 return block_write_begin(mapping, pos, len, flags, pagep,
640 blkdev_get_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641}
642
Nick Piggin6272b5a2007-10-16 01:25:04 -0700643static int blkdev_write_end(struct file *file, struct address_space *mapping,
644 loff_t pos, unsigned len, unsigned copied,
645 struct page *page, void *fsdata)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Nick Piggin6272b5a2007-10-16 01:25:04 -0700647 int ret;
648 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
649
650 unlock_page(page);
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +0300651 put_page(page);
Nick Piggin6272b5a2007-10-16 01:25:04 -0700652
653 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656/*
657 * private llseek:
Al Viro496ad9a2013-01-23 17:07:38 -0500658 * for a block special file file_inode(file)->i_size is zero
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 * so we compute the size by hand (just as in block_read/write above)
660 */
Andrew Morton965c8e52012-12-17 15:59:39 -0800661static loff_t block_llseek(struct file *file, loff_t offset, int whence)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900663 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 loff_t retval;
665
Al Viro59551022016-01-22 15:40:57 -0500666 inode_lock(bd_inode);
Al Viro5d48f3a2013-06-23 21:34:45 +0400667 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
Al Viro59551022016-01-22 15:40:57 -0500668 inode_unlock(bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 return retval;
670}
671
Josef Bacik02c24a82011-07-16 20:44:56 -0400672int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Dan Williams4ebb16c2015-10-28 07:48:19 +0900674 struct inode *bd_inode = bdev_file_inode(filp);
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400675 struct block_device *bdev = I_BDEV(bd_inode);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100676 int error;
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200677
Jeff Layton372cf242017-07-06 07:02:28 -0400678 error = file_write_and_wait_range(filp, start, end);
Rafael J. Wysockida5aa862011-08-02 02:17:48 +0200679 if (error)
680 return error;
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100681
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400682 /*
683 * There is no need to serialise calls to blkdev_issue_flush with
684 * i_mutex and doing so causes performance issues with concurrent
685 * O_SYNC writers to a block device.
686 */
Christoph Hellwigdd3932e2010-09-16 20:51:46 +0200687 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100688 if (error == -EOPNOTSUPP)
689 error = 0;
Anton Blanchardb8af67e2010-04-23 13:18:06 -0400690
Christoph Hellwigab0a9732009-10-29 14:14:04 +0100691 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692}
Andrew Mortonb1dd3b22010-04-06 14:35:00 -0700693EXPORT_SYMBOL(blkdev_fsync);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700695/**
696 * bdev_read_page() - Start reading a page from a block device
697 * @bdev: The device to read the page from
698 * @sector: The offset on the device to read the page to (need not be aligned)
699 * @page: The page to read
700 *
701 * On entry, the page should be locked. It will be unlocked when the page
702 * has been read. If the block driver implements rw_page synchronously,
703 * that will be true on exit from this function, but it need not be.
704 *
705 * Errors returned by this function are usually "soft", eg out of memory, or
706 * queue full; callers should try a different route to read this page rather
707 * than propagate an error back up the stack.
708 *
709 * Return: negative errno if an error occurs, 0 if submission was successful.
710 */
711int bdev_read_page(struct block_device *bdev, sector_t sector,
712 struct page *page)
713{
714 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc92015-11-19 13:29:28 -0800715 int result = -EOPNOTSUPP;
716
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400717 if (!ops->rw_page || bdev_get_integrity(bdev))
Dan Williams2e6edc92015-11-19 13:29:28 -0800718 return result;
719
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800720 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc92015-11-19 13:29:28 -0800721 if (result)
722 return result;
Tejun Heo3f289dc2018-07-18 04:47:36 -0700723 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
724 REQ_OP_READ);
Dan Williams2e6edc92015-11-19 13:29:28 -0800725 blk_queue_exit(bdev->bd_queue);
726 return result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700727}
728EXPORT_SYMBOL_GPL(bdev_read_page);
729
730/**
731 * bdev_write_page() - Start writing a page to a block device
732 * @bdev: The device to write the page to
733 * @sector: The offset on the device to write the page to (need not be aligned)
734 * @page: The page to write
735 * @wbc: The writeback_control for the write
736 *
737 * On entry, the page should be locked and not currently under writeback.
738 * On exit, if the write started successfully, the page will be unlocked and
739 * under writeback. If the write failed already (eg the driver failed to
740 * queue the page to the device), the page will still be locked. If the
741 * caller is a ->writepage implementation, it will need to unlock the page.
742 *
743 * Errors returned by this function are usually "soft", eg out of memory, or
744 * queue full; callers should try a different route to write this page rather
745 * than propagate an error back up the stack.
746 *
747 * Return: negative errno if an error occurs, 0 if submission was successful.
748 */
749int bdev_write_page(struct block_device *bdev, sector_t sector,
750 struct page *page, struct writeback_control *wbc)
751{
752 int result;
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700753 const struct block_device_operations *ops = bdev->bd_disk->fops;
Dan Williams2e6edc92015-11-19 13:29:28 -0800754
Vishal Vermaf68eb1e2015-05-12 13:48:53 -0400755 if (!ops->rw_page || bdev_get_integrity(bdev))
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700756 return -EOPNOTSUPP;
Bart Van Assche3a0a5292017-11-09 10:49:58 -0800757 result = blk_queue_enter(bdev->bd_queue, 0);
Dan Williams2e6edc92015-11-19 13:29:28 -0800758 if (result)
759 return result;
760
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700761 set_page_writeback(page);
Tejun Heo3f289dc2018-07-18 04:47:36 -0700762 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
763 REQ_OP_WRITE);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700764 if (result) {
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700765 end_page_writeback(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700766 } else {
767 clean_page_buffers(page);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700768 unlock_page(page);
Matthew Wilcoxf8927602017-10-13 15:58:15 -0700769 }
Dan Williams2e6edc92015-11-19 13:29:28 -0800770 blk_queue_exit(bdev->bd_queue);
Matthew Wilcox47a191f2014-06-04 16:07:46 -0700771 return result;
772}
773EXPORT_SYMBOL_GPL(bdev_write_page);
774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/*
776 * pseudo-fs
777 */
778
779static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
Christoph Lametere18b8902006-12-06 20:33:20 -0800780static struct kmem_cache * bdev_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782static struct inode *bdev_alloc_inode(struct super_block *sb)
783{
Christoph Lametere94b1762006-12-06 20:33:17 -0800784 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!ei)
786 return NULL;
787 return &ei->vfs_inode;
788}
789
Al Viro41149cb2019-04-10 15:12:38 -0400790static void bdev_free_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Al Viro41149cb2019-04-10 15:12:38 -0400792 kmem_cache_free(bdev_cachep, BDEV_I(inode));
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100793}
794
Alexey Dobriyan51cc5062008-07-25 19:45:34 -0700795static void init_once(void *foo)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct bdev_inode *ei = (struct bdev_inode *) foo;
798 struct block_device *bdev = &ei->bdev;
799
Christoph Lametera35afb82007-05-16 22:10:57 -0700800 memset(bdev, 0, sizeof(*bdev));
801 mutex_init(&bdev->bd_mutex);
Christoph Lametera35afb82007-05-16 22:10:57 -0700802 INIT_LIST_HEAD(&bdev->bd_list);
Tejun Heo49731ba2011-01-14 18:43:57 +0100803#ifdef CONFIG_SYSFS
804 INIT_LIST_HEAD(&bdev->bd_holder_disks);
805#endif
Jan Karaa5a79d02017-03-02 16:50:13 +0100806 bdev->bd_bdi = &noop_backing_dev_info;
Christoph Lametera35afb82007-05-16 22:10:57 -0700807 inode_init_once(&ei->vfs_inode);
Takashi Satofcccf502009-01-09 16:40:59 -0800808 /* Initialize mutex for freeze. */
809 mutex_init(&bdev->bd_fsfreeze_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
Al Virob57922d2010-06-07 14:34:48 -0400812static void bdev_evict_inode(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 struct block_device *bdev = &BDEV_I(inode)->bdev;
Johannes Weiner91b0abe2014-04-03 14:47:49 -0700815 truncate_inode_pages_final(&inode->i_data);
Al Virob57922d2010-06-07 14:34:48 -0400816 invalidate_inode_buffers(inode); /* is it needed here? */
Jan Karadbd57682012-05-03 14:48:02 +0200817 clear_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_lock(&bdev_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 list_del_init(&bdev->bd_list);
820 spin_unlock(&bdev_lock);
Jan Karaf7597412017-03-23 01:37:00 +0100821 /* Detach inode from wb early as bdi_put() may free bdi->wb */
822 inode_detach_wb(inode);
Jan Karaa5a79d02017-03-02 16:50:13 +0100823 if (bdev->bd_bdi != &noop_backing_dev_info) {
Jan Karab1d2dc562017-02-02 15:56:52 +0100824 bdi_put(bdev->bd_bdi);
Jan Karaa5a79d02017-03-02 16:50:13 +0100825 bdev->bd_bdi = &noop_backing_dev_info;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Josef 'Jeff' Sipekee9b6d62007-02-12 00:55:41 -0800829static const struct super_operations bdev_sops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 .statfs = simple_statfs,
831 .alloc_inode = bdev_alloc_inode,
Al Viro41149cb2019-04-10 15:12:38 -0400832 .free_inode = bdev_free_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 .drop_inode = generic_delete_inode,
Al Virob57922d2010-06-07 14:34:48 -0400834 .evict_inode = bdev_evict_inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835};
836
Al Viro51139ad2010-07-25 23:47:46 +0400837static struct dentry *bd_mount(struct file_system_type *fs_type,
838 int flags, const char *dev_name, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Shaohua Li3684aa72016-02-22 15:27:40 -0700840 struct dentry *dent;
841 dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
Vegard Nossume9e5e3f2016-08-22 12:47:43 +0200842 if (!IS_ERR(dent))
Shaohua Li3684aa72016-02-22 15:27:40 -0700843 dent->d_sb->s_iflags |= SB_I_CGROUPWB;
844 return dent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
847static struct file_system_type bd_type = {
848 .name = "bdev",
Al Viro51139ad2010-07-25 23:47:46 +0400849 .mount = bd_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 .kill_sb = kill_anon_super,
851};
852
Tejun Heoa212b102015-05-22 17:13:33 -0400853struct super_block *blockdev_superblock __read_mostly;
854EXPORT_SYMBOL_GPL(blockdev_superblock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856void __init bdev_cache_init(void)
857{
858 int err;
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300859 static struct vfsmount *bd_mnt;
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800860
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
Paul Jacksonfffb60f2006-03-24 03:16:06 -0800862 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
Vladimir Davydov5d097052016-01-14 15:18:21 -0800863 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
Paul Mundt20c2df82007-07-20 10:11:58 +0900864 init_once);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 err = register_filesystem(&bd_type);
866 if (err)
867 panic("Cannot register bdev pseudo-fs");
868 bd_mnt = kern_mount(&bd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (IS_ERR(bd_mnt))
870 panic("Cannot create bdev pseudo-fs");
Sergey Senozhatskyace85772012-01-10 02:43:59 +0300871 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
873
874/*
875 * Most likely _very_ bad one - but then it's hardly critical for small
876 * /dev and can be fixed when somebody will need really large one.
877 * Keep in mind that it will be fed through icache hash function too.
878 */
879static inline unsigned long hash(dev_t dev)
880{
881 return MAJOR(dev)+MINOR(dev);
882}
883
884static int bdev_test(struct inode *inode, void *data)
885{
886 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
887}
888
889static int bdev_set(struct inode *inode, void *data)
890{
891 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
892 return 0;
893}
894
895static LIST_HEAD(all_bdevs);
896
Jan Karaf44f1ab2017-02-02 15:56:49 +0100897/*
898 * If there is a bdev inode for this device, unhash it so that it gets evicted
899 * as soon as last inode reference is dropped.
900 */
901void bdev_unhash_inode(dev_t dev)
902{
903 struct inode *inode;
904
905 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
906 if (inode) {
907 remove_inode_hash(inode);
908 iput(inode);
909 }
910}
911
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912struct block_device *bdget(dev_t dev)
913{
914 struct block_device *bdev;
915 struct inode *inode;
916
Denis ChengRqc2acf7b2008-12-01 14:34:56 -0800917 inode = iget5_locked(blockdev_superblock, hash(dev),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 bdev_test, bdev_set, &dev);
919
920 if (!inode)
921 return NULL;
922
923 bdev = &BDEV_I(inode)->bdev;
924
925 if (inode->i_state & I_NEW) {
926 bdev->bd_contains = NULL;
Lachlan McIlroy782b94c2011-06-30 11:01:45 +1000927 bdev->bd_super = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 bdev->bd_inode = inode;
Fabian Frederick93407472017-02-27 14:28:32 -0800929 bdev->bd_block_size = i_blocksize(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 bdev->bd_part_count = 0;
931 bdev->bd_invalidated = 0;
932 inode->i_mode = S_IFBLK;
933 inode->i_rdev = dev;
934 inode->i_bdev = bdev;
935 inode->i_data.a_ops = &def_blk_aops;
936 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 spin_lock(&bdev_lock);
938 list_add(&bdev->bd_list, &all_bdevs);
939 spin_unlock(&bdev_lock);
940 unlock_new_inode(inode);
941 }
942 return bdev;
943}
944
945EXPORT_SYMBOL(bdget);
946
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200947/**
948 * bdgrab -- Grab a reference to an already referenced block device
949 * @bdev: Block device to grab a reference to.
950 */
951struct block_device *bdgrab(struct block_device *bdev)
952{
Al Viro7de9c6ee2010-10-23 11:11:40 -0400953 ihold(bdev->bd_inode);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200954 return bdev;
955}
Anatol Pomozovc1681bf2013-04-01 09:47:56 -0700956EXPORT_SYMBOL(bdgrab);
Alan Jenkinsdddac6a2009-07-29 21:07:55 +0200957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958long nr_blockdev_pages(void)
959{
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700960 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 long ret = 0;
962 spin_lock(&bdev_lock);
Matthias Kaehlcke203a2932007-07-15 23:40:23 -0700963 list_for_each_entry(bdev, &all_bdevs, bd_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ret += bdev->bd_inode->i_mapping->nrpages;
965 }
966 spin_unlock(&bdev_lock);
967 return ret;
968}
969
970void bdput(struct block_device *bdev)
971{
972 iput(bdev->bd_inode);
973}
974
975EXPORT_SYMBOL(bdput);
976
977static struct block_device *bd_acquire(struct inode *inode)
978{
979 struct block_device *bdev;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700980
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 spin_lock(&bdev_lock);
982 bdev = inode->i_bdev;
Jan Karacccd9fb2017-02-21 18:09:48 +0100983 if (bdev && !inode_unhashed(bdev->bd_inode)) {
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +0100984 bdgrab(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 spin_unlock(&bdev_lock);
986 return bdev;
987 }
988 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -0700989
Jan Karacccd9fb2017-02-21 18:09:48 +0100990 /*
991 * i_bdev references block device inode that was already shut down
992 * (corresponding device got removed). Remove the reference and look
993 * up block device inode again just in case new device got
994 * reestablished under the same device number.
995 */
996 if (bdev)
997 bd_forget(inode);
998
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 bdev = bdget(inode->i_rdev);
1000 if (bdev) {
1001 spin_lock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001002 if (!inode->i_bdev) {
1003 /*
Al Viro7de9c6ee2010-10-23 11:11:40 -04001004 * We take an additional reference to bd_inode,
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001005 * and it's released in clear_inode() of inode.
1006 * So, we can access it via ->i_mapping always
1007 * without igrab().
1008 */
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001009 bdgrab(bdev);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001010 inode->i_bdev = bdev;
1011 inode->i_mapping = bdev->bd_inode->i_mapping;
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 spin_unlock(&bdev_lock);
1014 }
1015 return bdev;
1016}
1017
1018/* Call when you free inode */
1019
1020void bd_forget(struct inode *inode)
1021{
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001022 struct block_device *bdev = NULL;
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 spin_lock(&bdev_lock);
Yan Hongb4ea2ea2013-04-30 15:26:47 -07001025 if (!sb_is_blkdev_sb(inode->i_sb))
1026 bdev = inode->i_bdev;
Al Viroa4a4f942016-07-19 13:16:52 -04001027 inode->i_bdev = NULL;
1028 inode->i_mapping = &inode->i_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 spin_unlock(&bdev_lock);
OGAWA Hirofumi09d967c2006-06-22 14:47:21 -07001030
1031 if (bdev)
Ilya Dryomoved8a9d2c2015-11-20 22:18:43 +01001032 bdput(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001035/**
1036 * bd_may_claim - test whether a block device can be claimed
1037 * @bdev: block device of interest
1038 * @whole: whole block device containing @bdev, may equal @bdev
1039 * @holder: holder trying to claim @bdev
1040 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001041 * Test whether @bdev can be claimed by @holder.
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001042 *
1043 * CONTEXT:
1044 * spin_lock(&bdev_lock).
1045 *
1046 * RETURNS:
1047 * %true if @bdev can be claimed, %false otherwise.
1048 */
1049static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1050 void *holder)
1051{
1052 if (bdev->bd_holder == holder)
1053 return true; /* already a holder */
1054 else if (bdev->bd_holder != NULL)
1055 return false; /* held by someone else */
NeilBrownbcc7f5b2016-12-12 08:21:51 -07001056 else if (whole == bdev)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001057 return true; /* is a whole device which isn't held */
1058
Tejun Heoe525fd82010-11-13 11:55:17 +01001059 else if (whole->bd_holder == bd_may_claim)
Tejun Heo1a3cbbc2010-04-07 18:52:29 +09001060 return true; /* is a partition of a device that is being partitioned */
1061 else if (whole->bd_holder != NULL)
1062 return false; /* is a partition of a held device */
1063 else
1064 return true; /* is a partition of an un-held device */
1065}
1066
1067/**
Tejun Heo6b4517a2010-04-07 18:53:59 +09001068 * bd_prepare_to_claim - prepare to claim a block device
1069 * @bdev: block device of interest
1070 * @whole: the whole device containing @bdev, may equal @bdev
1071 * @holder: holder trying to claim @bdev
1072 *
1073 * Prepare to claim @bdev. This function fails if @bdev is already
1074 * claimed by another holder and waits if another claiming is in
1075 * progress. This function doesn't actually claim. On successful
1076 * return, the caller has ownership of bd_claiming and bd_holder[s].
1077 *
1078 * CONTEXT:
1079 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1080 * it multiple times.
1081 *
1082 * RETURNS:
1083 * 0 if @bdev can be claimed, -EBUSY otherwise.
1084 */
1085static int bd_prepare_to_claim(struct block_device *bdev,
1086 struct block_device *whole, void *holder)
1087{
1088retry:
1089 /* if someone else claimed, fail */
1090 if (!bd_may_claim(bdev, whole, holder))
1091 return -EBUSY;
1092
Tejun Heoe75aa852010-08-04 17:59:39 +02001093 /* if claiming is already in progress, wait for it to finish */
1094 if (whole->bd_claiming) {
Tejun Heo6b4517a2010-04-07 18:53:59 +09001095 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1096 DEFINE_WAIT(wait);
1097
1098 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1099 spin_unlock(&bdev_lock);
1100 schedule();
1101 finish_wait(wq, &wait);
1102 spin_lock(&bdev_lock);
1103 goto retry;
1104 }
1105
1106 /* yay, all mine */
1107 return 0;
1108}
1109
Jan Kara560e7cb2018-02-26 13:01:42 +01001110static struct gendisk *bdev_get_gendisk(struct block_device *bdev, int *partno)
1111{
1112 struct gendisk *disk = get_gendisk(bdev->bd_dev, partno);
1113
1114 if (!disk)
1115 return NULL;
1116 /*
1117 * Now that we hold gendisk reference we make sure bdev we looked up is
1118 * not stale. If it is, it means device got removed and created before
1119 * we looked up gendisk and we fail open in such case. Associating
1120 * unhashed bdev with newly created gendisk could lead to two bdevs
1121 * (and thus two independent caches) being associated with one device
1122 * which is bad.
1123 */
1124 if (inode_unhashed(bdev->bd_inode)) {
1125 put_disk_and_module(disk);
1126 return NULL;
1127 }
1128 return disk;
1129}
1130
Tejun Heo6b4517a2010-04-07 18:53:59 +09001131/**
1132 * bd_start_claiming - start claiming a block device
1133 * @bdev: block device of interest
1134 * @holder: holder trying to claim @bdev
1135 *
1136 * @bdev is about to be opened exclusively. Check @bdev can be opened
1137 * exclusively and mark that an exclusive open is in progress. Each
1138 * successful call to this function must be matched with a call to
Nick Pigginb0018362010-05-26 01:51:19 +10001139 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1140 * fail).
1141 *
1142 * This function is used to gain exclusive access to the block device
1143 * without actually causing other exclusive open attempts to fail. It
1144 * should be used when the open sequence itself requires exclusive
1145 * access but may subsequently fail.
Tejun Heo6b4517a2010-04-07 18:53:59 +09001146 *
1147 * CONTEXT:
1148 * Might sleep.
1149 *
1150 * RETURNS:
1151 * Pointer to the block device containing @bdev on success, ERR_PTR()
1152 * value on failure.
1153 */
1154static struct block_device *bd_start_claiming(struct block_device *bdev,
1155 void *holder)
1156{
1157 struct gendisk *disk;
1158 struct block_device *whole;
1159 int partno, err;
1160
1161 might_sleep();
1162
1163 /*
1164 * @bdev might not have been initialized properly yet, look up
1165 * and grab the outer block device the hard way.
1166 */
Jan Kara560e7cb2018-02-26 13:01:42 +01001167 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001168 if (!disk)
1169 return ERR_PTR(-ENXIO);
1170
Tejun Heod4c208b2011-06-13 12:45:48 +02001171 /*
1172 * Normally, @bdev should equal what's returned from bdget_disk()
1173 * if partno is 0; however, some drivers (floppy) use multiple
1174 * bdev's for the same physical device and @bdev may be one of the
1175 * aliases. Keep @bdev if partno is 0. This means claimer
1176 * tracking is broken for those devices but it has always been that
1177 * way.
1178 */
1179 if (partno)
1180 whole = bdget_disk(disk, 0);
1181 else
1182 whole = bdgrab(bdev);
1183
Jan Kara9df6c292018-02-26 13:01:39 +01001184 put_disk_and_module(disk);
Tejun Heo6b4517a2010-04-07 18:53:59 +09001185 if (!whole)
1186 return ERR_PTR(-ENOMEM);
1187
1188 /* prepare to claim, if successful, mark claiming in progress */
1189 spin_lock(&bdev_lock);
1190
1191 err = bd_prepare_to_claim(bdev, whole, holder);
1192 if (err == 0) {
1193 whole->bd_claiming = holder;
1194 spin_unlock(&bdev_lock);
1195 return whole;
1196 } else {
1197 spin_unlock(&bdev_lock);
1198 bdput(whole);
1199 return ERR_PTR(err);
1200 }
1201}
1202
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001203#ifdef CONFIG_SYSFS
Tejun Heo49731ba2011-01-14 18:43:57 +01001204struct bd_holder_disk {
1205 struct list_head list;
1206 struct gendisk *disk;
1207 int refcnt;
1208};
1209
1210static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1211 struct gendisk *disk)
1212{
1213 struct bd_holder_disk *holder;
1214
1215 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1216 if (holder->disk == disk)
1217 return holder;
1218 return NULL;
1219}
1220
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001221static int add_symlink(struct kobject *from, struct kobject *to)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001222{
Andrew Morton4d7dd8fd2006-09-29 01:58:56 -07001223 return sysfs_create_link(from, to, kobject_name(to));
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001224}
1225
1226static void del_symlink(struct kobject *from, struct kobject *to)
1227{
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001228 sysfs_remove_link(from, kobject_name(to));
1229}
1230
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001231/**
Tejun Heoe09b4572010-11-13 11:55:17 +01001232 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1233 * @bdev: the claimed slave bdev
1234 * @disk: the holding disk
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001235 *
Tejun Heo49731ba2011-01-14 18:43:57 +01001236 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1237 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001238 * This functions creates the following sysfs symlinks.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001239 *
Tejun Heoe09b4572010-11-13 11:55:17 +01001240 * - from "slaves" directory of the holder @disk to the claimed @bdev
1241 * - from "holders" directory of the @bdev to the holder @disk
1242 *
1243 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1244 * passed to bd_link_disk_holder(), then:
1245 *
1246 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1247 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1248 *
1249 * The caller must have claimed @bdev before calling this function and
1250 * ensure that both @bdev and @disk are valid during the creation and
1251 * lifetime of these symlinks.
1252 *
1253 * CONTEXT:
1254 * Might sleep.
1255 *
1256 * RETURNS:
1257 * 0 on success, -errno on failure.
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001258 */
Tejun Heoe09b4572010-11-13 11:55:17 +01001259int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001260{
Tejun Heo49731ba2011-01-14 18:43:57 +01001261 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001262 int ret = 0;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001263
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001264 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomuradf6c0cd2006-10-30 16:23:56 -05001265
Tejun Heo49731ba2011-01-14 18:43:57 +01001266 WARN_ON_ONCE(!bdev->bd_holder);
Johannes Weiner4e916722007-07-15 23:41:25 -07001267
Tejun Heoe09b4572010-11-13 11:55:17 +01001268 /* FIXME: remove the following once add_disk() handles errors */
1269 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1270 goto out_unlock;
Johannes Weiner4e916722007-07-15 23:41:25 -07001271
Tejun Heo49731ba2011-01-14 18:43:57 +01001272 holder = bd_find_holder_disk(bdev, disk);
1273 if (holder) {
1274 holder->refcnt++;
Tejun Heoe09b4572010-11-13 11:55:17 +01001275 goto out_unlock;
1276 }
1277
Tejun Heo49731ba2011-01-14 18:43:57 +01001278 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1279 if (!holder) {
1280 ret = -ENOMEM;
1281 goto out_unlock;
1282 }
1283
1284 INIT_LIST_HEAD(&holder->list);
1285 holder->disk = disk;
1286 holder->refcnt = 1;
1287
1288 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1289 if (ret)
1290 goto out_free;
1291
1292 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1293 if (ret)
1294 goto out_del;
Tejun Heoe7407d12011-02-24 09:56:32 +01001295 /*
1296 * bdev could be deleted beneath us which would implicitly destroy
1297 * the holder directory. Hold on to it.
1298 */
1299 kobject_get(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001300
1301 list_add(&holder->list, &bdev->bd_holder_disks);
1302 goto out_unlock;
1303
1304out_del:
1305 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1306out_free:
1307 kfree(holder);
Tejun Heoe09b4572010-11-13 11:55:17 +01001308out_unlock:
Jun'ichi Nomurab4cf1b72006-03-27 01:18:00 -08001309 mutex_unlock(&bdev->bd_mutex);
Tejun Heoe09b4572010-11-13 11:55:17 +01001310 return ret;
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001311}
Tejun Heoe09b4572010-11-13 11:55:17 +01001312EXPORT_SYMBOL_GPL(bd_link_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001313
Tejun Heo49731ba2011-01-14 18:43:57 +01001314/**
1315 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1316 * @bdev: the calimed slave bdev
1317 * @disk: the holding disk
1318 *
1319 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1320 *
1321 * CONTEXT:
1322 * Might sleep.
1323 */
1324void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001325{
Tejun Heo49731ba2011-01-14 18:43:57 +01001326 struct bd_holder_disk *holder;
Tejun Heoe09b4572010-11-13 11:55:17 +01001327
Tejun Heo49731ba2011-01-14 18:43:57 +01001328 mutex_lock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001329
Tejun Heo49731ba2011-01-14 18:43:57 +01001330 holder = bd_find_holder_disk(bdev, disk);
1331
1332 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1333 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1334 del_symlink(bdev->bd_part->holder_dir,
1335 &disk_to_dev(disk)->kobj);
Tejun Heoe7407d12011-02-24 09:56:32 +01001336 kobject_put(bdev->bd_part->holder_dir);
Tejun Heo49731ba2011-01-14 18:43:57 +01001337 list_del_init(&holder->list);
1338 kfree(holder);
1339 }
1340
1341 mutex_unlock(&bdev->bd_mutex);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001342}
Tejun Heo49731ba2011-01-14 18:43:57 +01001343EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
Jun'ichi Nomura641dc632006-03-27 01:17:57 -08001344#endif
1345
Andrew Patterson0c002c22008-09-04 14:27:20 -06001346/**
Andrew Patterson56ade442008-09-04 14:27:40 -06001347 * flush_disk - invalidates all buffer-cache entries on a disk
1348 *
1349 * @bdev: struct block device to be flushed
Randy Dunlape6eb5ce2011-02-26 10:54:00 -08001350 * @kill_dirty: flag to guide handling of dirty inodes
Andrew Patterson56ade442008-09-04 14:27:40 -06001351 *
1352 * Invalidates all buffer-cache entries on a disk. It should be called
1353 * when a disk has been changed -- either by a media change or online
1354 * resize.
1355 */
NeilBrown93b270f2011-02-24 17:25:47 +11001356static void flush_disk(struct block_device *bdev, bool kill_dirty)
Andrew Patterson56ade442008-09-04 14:27:40 -06001357{
NeilBrown93b270f2011-02-24 17:25:47 +11001358 if (__invalidate_device(bdev, kill_dirty)) {
Andrew Patterson56ade442008-09-04 14:27:40 -06001359 printk(KERN_WARNING "VFS: busy inodes on changed media or "
Dmitry Monakhov424081f2015-04-13 16:31:34 +04001360 "resized disk %s\n",
1361 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
Andrew Patterson56ade442008-09-04 14:27:40 -06001362 }
1363
1364 if (!bdev->bd_disk)
1365 return;
Tejun Heod27769e2011-08-23 20:01:04 +02001366 if (disk_part_scan_enabled(bdev->bd_disk))
Andrew Patterson56ade442008-09-04 14:27:40 -06001367 bdev->bd_invalidated = 1;
1368}
1369
1370/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001371 * check_disk_size_change - checks for disk size change and adjusts bdev size.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001372 * @disk: struct gendisk to check
1373 * @bdev: struct bdev to adjust.
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001374 * @verbose: if %true log a message about a size change if there is any
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001375 *
1376 * This routine checks to see if the bdev size does not match the disk size
shunki-fujita849cf552018-04-05 16:20:07 -07001377 * and adjusts it if it differs. When shrinking the bdev size, its all caches
1378 * are freed.
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001379 */
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001380void check_disk_size_change(struct gendisk *disk, struct block_device *bdev,
1381 bool verbose)
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001382{
1383 loff_t disk_size, bdev_size;
1384
1385 disk_size = (loff_t)get_capacity(disk) << 9;
1386 bdev_size = i_size_read(bdev->bd_inode);
1387 if (disk_size != bdev_size) {
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001388 if (verbose) {
1389 printk(KERN_INFO
1390 "%s: detected capacity change from %lld to %lld\n",
1391 disk->disk_name, bdev_size, disk_size);
1392 }
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001393 i_size_write(bdev->bd_inode, disk_size);
shunki-fujita849cf552018-04-05 16:20:07 -07001394 if (bdev_size > disk_size)
1395 flush_disk(bdev, false);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001396 }
1397}
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001398
1399/**
Randy Dunlap57d1b532008-10-09 10:42:38 +02001400 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
Andrew Patterson0c002c22008-09-04 14:27:20 -06001401 * @disk: struct gendisk to be revalidated
1402 *
1403 * This routine is a wrapper for lower-level driver's revalidate_disk
1404 * call-backs. It is used to do common pre and post operations needed
1405 * for all revalidate_disk operations.
1406 */
1407int revalidate_disk(struct gendisk *disk)
1408{
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001409 struct block_device *bdev;
Andrew Patterson0c002c22008-09-04 14:27:20 -06001410 int ret = 0;
1411
1412 if (disk->fops->revalidate_disk)
1413 ret = disk->fops->revalidate_disk(disk);
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001414 bdev = bdget_disk(disk, 0);
1415 if (!bdev)
1416 return ret;
1417
1418 mutex_lock(&bdev->bd_mutex);
Christoph Hellwig5afb7832018-05-29 16:42:59 +02001419 check_disk_size_change(disk, bdev, ret == 0);
MITSUNARI Shigeo7630b662013-02-21 16:42:01 -08001420 bdev->bd_invalidated = 0;
Andrew Pattersonc3279d12008-09-04 14:27:25 -06001421 mutex_unlock(&bdev->bd_mutex);
1422 bdput(bdev);
Andrew Patterson0c002c22008-09-04 14:27:20 -06001423 return ret;
1424}
1425EXPORT_SYMBOL(revalidate_disk);
1426
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427/*
1428 * This routine checks whether a removable media has been changed,
1429 * and invalidates all buffer-cache-entries in that case. This
1430 * is a relatively slow routine, so we have to try to minimize using
1431 * it. Thus it is called only upon a 'mount' or 'open'. This
1432 * is the best way of combining speed and utility, I think.
1433 * People changing diskettes in the middle of an operation deserve
1434 * to lose :-)
1435 */
1436int check_disk_change(struct block_device *bdev)
1437{
1438 struct gendisk *disk = bdev->bd_disk;
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -07001439 const struct block_device_operations *bdops = disk->fops;
Tejun Heo77ea8872010-12-08 20:57:37 +01001440 unsigned int events;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441
Tejun Heo77ea8872010-12-08 20:57:37 +01001442 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1443 DISK_EVENT_EJECT_REQUEST);
1444 if (!(events & DISK_EVENT_MEDIA_CHANGE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return 0;
1446
NeilBrown93b270f2011-02-24 17:25:47 +11001447 flush_disk(bdev, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 if (bdops->revalidate_disk)
1449 bdops->revalidate_disk(bdev->bd_disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 return 1;
1451}
1452
1453EXPORT_SYMBOL(check_disk_change);
1454
1455void bd_set_size(struct block_device *bdev, loff_t size)
1456{
Al Viro59551022016-01-22 15:40:57 -05001457 inode_lock(bdev->bd_inode);
Guo Chaod646a022013-02-21 15:16:42 -08001458 i_size_write(bdev->bd_inode, size);
Al Viro59551022016-01-22 15:40:57 -05001459 inode_unlock(bdev->bd_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460}
1461EXPORT_SYMBOL(bd_set_size);
1462
Al Viro4385bab2013-05-05 22:11:03 -04001463static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001464
Peter Zijlstra6d740cd2007-02-20 13:58:18 -08001465/*
1466 * bd_mutex locking:
1467 *
1468 * mutex_lock(part->bd_mutex)
1469 * mutex_lock_nested(whole->bd_mutex, 1)
1470 */
1471
Al Viro572c4892007-10-08 13:24:05 -04001472static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 struct gendisk *disk;
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001475 int ret;
Tejun Heocf771cb2008-09-03 09:01:09 +02001476 int partno;
Al Virofe6e9c12008-06-23 08:30:55 -04001477 int perm = 0;
Jan Kara89736652018-02-26 13:01:40 +01001478 bool first_open = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Al Viro572c4892007-10-08 13:24:05 -04001480 if (mode & FMODE_READ)
Al Virofe6e9c12008-06-23 08:30:55 -04001481 perm |= MAY_READ;
Al Viro572c4892007-10-08 13:24:05 -04001482 if (mode & FMODE_WRITE)
Al Virofe6e9c12008-06-23 08:30:55 -04001483 perm |= MAY_WRITE;
1484 /*
1485 * hooks: /n/, see "layering violations".
1486 */
Chris Wrightb7300b72010-08-10 18:02:55 -07001487 if (!for_part) {
1488 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1489 if (ret != 0) {
1490 bdput(bdev);
1491 return ret;
1492 }
Al Viro82666022008-08-01 05:32:04 -04001493 }
Pavel Emelyanov7db9cfd2008-06-05 22:46:27 -07001494
NeilBrownd3374822009-01-09 08:31:10 +11001495 restart:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001496
Tejun Heo89f97492008-11-05 10:21:06 +01001497 ret = -ENXIO;
Jan Kara560e7cb2018-02-26 13:01:42 +01001498 disk = bdev_get_gendisk(bdev, &partno);
Tejun Heo0762b8b2008-08-25 19:56:12 +09001499 if (!disk)
Arnd Bergmann6e9624b2010-08-07 18:25:34 +02001500 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Tejun Heo69e02c52011-03-09 19:54:27 +01001502 disk_block_events(disk);
NeilBrown6796bf52006-12-08 02:36:16 -08001503 mutex_lock_nested(&bdev->bd_mutex, for_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 if (!bdev->bd_openers) {
Jan Kara89736652018-02-26 13:01:40 +01001505 first_open = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 bdev->bd_disk = disk;
Andi Kleen87192a22012-01-12 17:20:34 -08001507 bdev->bd_queue = disk->queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 bdev->bd_contains = bdev;
Christoph Hellwigc2ee0702017-08-23 19:10:31 +02001509 bdev->bd_partno = partno;
Dan Williams03cdadb2016-02-26 15:19:43 -08001510
Tejun Heocf771cb2008-09-03 09:01:09 +02001511 if (!partno) {
Tejun Heo89f97492008-11-05 10:21:06 +01001512 ret = -ENXIO;
1513 bdev->bd_part = disk_get_part(disk, partno);
1514 if (!bdev->bd_part)
1515 goto out_clear;
1516
Tejun Heo1196f8b2011-04-21 20:54:45 +02001517 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (disk->fops->open) {
Al Viro572c4892007-10-08 13:24:05 -04001519 ret = disk->fops->open(bdev, mode);
NeilBrownd3374822009-01-09 08:31:10 +11001520 if (ret == -ERESTARTSYS) {
1521 /* Lost a race with 'disk' being
1522 * deleted, try again.
1523 * See md.c
1524 */
1525 disk_put_part(bdev->bd_part);
1526 bdev->bd_part = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001527 bdev->bd_disk = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001528 bdev->bd_queue = NULL;
NeilBrownd3374822009-01-09 08:31:10 +11001529 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001530 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001531 put_disk_and_module(disk);
NeilBrownd3374822009-01-09 08:31:10 +11001532 goto restart;
1533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Tejun Heo7e697232011-05-23 13:26:07 +02001535
Jan Kara04906b22019-01-14 09:48:10 +01001536 if (!ret) {
Tejun Heo7e697232011-05-23 13:26:07 +02001537 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
Jan Kara04906b22019-01-14 09:48:10 +01001538 set_init_blocksize(bdev);
1539 }
Tejun Heo7e697232011-05-23 13:26:07 +02001540
Tejun Heo1196f8b2011-04-21 20:54:45 +02001541 /*
1542 * If the device is invalidated, rescan partition
1543 * if open succeeded or failed with -ENOMEDIUM.
1544 * The latter is necessary to prevent ghost
1545 * partitions on a removed medium.
1546 */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001547 if (bdev->bd_invalidated) {
1548 if (!ret)
1549 rescan_partitions(disk, bdev);
1550 else if (ret == -ENOMEDIUM)
1551 invalidate_partitions(disk, bdev);
1552 }
Dan Williams5a023cd2015-11-30 10:20:29 -08001553
Tejun Heo1196f8b2011-04-21 20:54:45 +02001554 if (ret)
1555 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct block_device *whole;
1558 whole = bdget_disk(disk, 0);
1559 ret = -ENOMEM;
1560 if (!whole)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001561 goto out_clear;
NeilBrown37be4122006-12-08 02:36:16 -08001562 BUG_ON(for_part);
Al Viro572c4892007-10-08 13:24:05 -04001563 ret = __blkdev_get(whole, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 if (ret)
Tejun Heo0762b8b2008-08-25 19:56:12 +09001565 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 bdev->bd_contains = whole;
Tejun Heo89f97492008-11-05 10:21:06 +01001567 bdev->bd_part = disk_get_part(disk, partno);
Tejun Heoe71bf0d2008-09-03 09:03:02 +02001568 if (!(disk->flags & GENHD_FL_UP) ||
Tejun Heo89f97492008-11-05 10:21:06 +01001569 !bdev->bd_part || !bdev->bd_part->nr_sects) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 ret = -ENXIO;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001571 goto out_clear;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 }
Tejun Heo89f97492008-11-05 10:21:06 +01001573 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
Jan Kara04906b22019-01-14 09:48:10 +01001574 set_init_blocksize(bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
Jan Kara03e26272017-03-23 01:36:53 +01001576
1577 if (bdev->bd_bdi == &noop_backing_dev_info)
1578 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 if (bdev->bd_contains == bdev) {
Tejun Heo1196f8b2011-04-21 20:54:45 +02001581 ret = 0;
1582 if (bdev->bd_disk->fops->open)
Al Viro572c4892007-10-08 13:24:05 -04001583 ret = bdev->bd_disk->fops->open(bdev, mode);
Tejun Heo1196f8b2011-04-21 20:54:45 +02001584 /* the same as first opener case, read comment there */
Jun'ichi Nomurafe316bf2012-03-02 10:38:33 +01001585 if (bdev->bd_invalidated) {
1586 if (!ret)
1587 rescan_partitions(bdev->bd_disk, bdev);
1588 else if (ret == -ENOMEDIUM)
1589 invalidate_partitions(bdev->bd_disk, bdev);
1590 }
Tejun Heo1196f8b2011-04-21 20:54:45 +02001591 if (ret)
1592 goto out_unlock_bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 }
1594 }
1595 bdev->bd_openers++;
NeilBrown37be4122006-12-08 02:36:16 -08001596 if (for_part)
1597 bdev->bd_part_count++;
Arjan van de Venc039e312006-03-23 03:00:28 -08001598 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001599 disk_unblock_events(disk);
Jan Kara89736652018-02-26 13:01:40 +01001600 /* only one opener holds refs to the module and disk */
1601 if (!first_open)
1602 put_disk_and_module(disk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 return 0;
1604
Tejun Heo0762b8b2008-08-25 19:56:12 +09001605 out_clear:
Tejun Heo89f97492008-11-05 10:21:06 +01001606 disk_put_part(bdev->bd_part);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 bdev->bd_disk = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001608 bdev->bd_part = NULL;
Andi Kleen87192a22012-01-12 17:20:34 -08001609 bdev->bd_queue = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (bdev != bdev->bd_contains)
Al Viro572c4892007-10-08 13:24:05 -04001611 __blkdev_put(bdev->bd_contains, mode, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 bdev->bd_contains = NULL;
Tejun Heo0762b8b2008-08-25 19:56:12 +09001613 out_unlock_bdev:
Arjan van de Venc039e312006-03-23 03:00:28 -08001614 mutex_unlock(&bdev->bd_mutex);
Tejun Heo69e02c52011-03-09 19:54:27 +01001615 disk_unblock_events(disk);
Jan Kara9df6c292018-02-26 13:01:39 +01001616 put_disk_and_module(disk);
Dan Carpenter4345cab2011-03-19 13:53:31 +01001617 out:
Tejun Heo0762b8b2008-08-25 19:56:12 +09001618 bdput(bdev);
1619
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 return ret;
1621}
1622
Tejun Heod4d77622010-11-13 11:55:18 +01001623/**
1624 * blkdev_get - open a block device
1625 * @bdev: block_device to open
1626 * @mode: FMODE_* mask
1627 * @holder: exclusive holder identifier
1628 *
1629 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1630 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1631 * @holder is invalid. Exclusive opens may nest for the same @holder.
1632 *
1633 * On success, the reference count of @bdev is unchanged. On failure,
1634 * @bdev is put.
1635 *
1636 * CONTEXT:
1637 * Might sleep.
1638 *
1639 * RETURNS:
1640 * 0 on success, -errno on failure.
1641 */
Tejun Heoe525fd82010-11-13 11:55:17 +01001642int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
Tejun Heoe525fd82010-11-13 11:55:17 +01001644 struct block_device *whole = NULL;
1645 int res;
1646
1647 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1648
1649 if ((mode & FMODE_EXCL) && holder) {
1650 whole = bd_start_claiming(bdev, holder);
1651 if (IS_ERR(whole)) {
1652 bdput(bdev);
1653 return PTR_ERR(whole);
1654 }
1655 }
1656
1657 res = __blkdev_get(bdev, mode, 0);
1658
1659 if (whole) {
Tejun Heod4dc2102011-04-21 20:54:46 +02001660 struct gendisk *disk = whole->bd_disk;
1661
Tejun Heo6a027ef2010-11-13 11:55:17 +01001662 /* finish claiming */
Tejun Heo77ea8872010-12-08 20:57:37 +01001663 mutex_lock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001664 spin_lock(&bdev_lock);
1665
Tejun Heo77ea8872010-12-08 20:57:37 +01001666 if (!res) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001667 BUG_ON(!bd_may_claim(bdev, whole, holder));
1668 /*
1669 * Note that for a whole device bd_holders
1670 * will be incremented twice, and bd_holder
1671 * will be set to bd_may_claim before being
1672 * set to holder
1673 */
1674 whole->bd_holders++;
1675 whole->bd_holder = bd_may_claim;
1676 bdev->bd_holders++;
1677 bdev->bd_holder = holder;
1678 }
1679
1680 /* tell others that we're done */
1681 BUG_ON(whole->bd_claiming != holder);
1682 whole->bd_claiming = NULL;
1683 wake_up_bit(&whole->bd_claiming, 0);
1684
1685 spin_unlock(&bdev_lock);
Tejun Heo77ea8872010-12-08 20:57:37 +01001686
1687 /*
Tejun Heod4dc2102011-04-21 20:54:46 +02001688 * Block event polling for write claims if requested. Any
1689 * write holder makes the write_holder state stick until
1690 * all are released. This is good enough and tracking
1691 * individual writeable reference is too fragile given the
1692 * way @mode is used in blkdev_get/put().
Tejun Heo77ea8872010-12-08 20:57:37 +01001693 */
Tejun Heo4c49ff32011-06-01 08:27:41 +02001694 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1695 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
Tejun Heo77ea8872010-12-08 20:57:37 +01001696 bdev->bd_write_holder = true;
Tejun Heod4dc2102011-04-21 20:54:46 +02001697 disk_block_events(disk);
Tejun Heo77ea8872010-12-08 20:57:37 +01001698 }
1699
1700 mutex_unlock(&bdev->bd_mutex);
Tejun Heo6a027ef2010-11-13 11:55:17 +01001701 bdput(whole);
Tejun Heoe525fd82010-11-13 11:55:17 +01001702 }
1703
1704 return res;
NeilBrown37be4122006-12-08 02:36:16 -08001705}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706EXPORT_SYMBOL(blkdev_get);
1707
Tejun Heod4d77622010-11-13 11:55:18 +01001708/**
1709 * blkdev_get_by_path - open a block device by name
1710 * @path: path to the block device to open
1711 * @mode: FMODE_* mask
1712 * @holder: exclusive holder identifier
1713 *
1714 * Open the blockdevice described by the device file at @path. @mode
1715 * and @holder are identical to blkdev_get().
1716 *
1717 * On success, the returned block_device has reference count of one.
1718 *
1719 * CONTEXT:
1720 * Might sleep.
1721 *
1722 * RETURNS:
1723 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1724 */
1725struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1726 void *holder)
1727{
1728 struct block_device *bdev;
1729 int err;
1730
1731 bdev = lookup_bdev(path);
1732 if (IS_ERR(bdev))
1733 return bdev;
1734
1735 err = blkdev_get(bdev, mode, holder);
1736 if (err)
1737 return ERR_PTR(err);
1738
Chuck Ebberte51900f2011-02-16 18:11:53 -05001739 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1740 blkdev_put(bdev, mode);
1741 return ERR_PTR(-EACCES);
1742 }
1743
Tejun Heod4d77622010-11-13 11:55:18 +01001744 return bdev;
1745}
1746EXPORT_SYMBOL(blkdev_get_by_path);
1747
1748/**
1749 * blkdev_get_by_dev - open a block device by device number
1750 * @dev: device number of block device to open
1751 * @mode: FMODE_* mask
1752 * @holder: exclusive holder identifier
1753 *
1754 * Open the blockdevice described by device number @dev. @mode and
1755 * @holder are identical to blkdev_get().
1756 *
1757 * Use it ONLY if you really do not have anything better - i.e. when
1758 * you are behind a truly sucky interface and all you are given is a
1759 * device number. _Never_ to be used for internal purposes. If you
1760 * ever need it - reconsider your API.
1761 *
1762 * On success, the returned block_device has reference count of one.
1763 *
1764 * CONTEXT:
1765 * Might sleep.
1766 *
1767 * RETURNS:
1768 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1769 */
1770struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1771{
1772 struct block_device *bdev;
1773 int err;
1774
1775 bdev = bdget(dev);
1776 if (!bdev)
1777 return ERR_PTR(-ENOMEM);
1778
1779 err = blkdev_get(bdev, mode, holder);
1780 if (err)
1781 return ERR_PTR(err);
1782
1783 return bdev;
1784}
1785EXPORT_SYMBOL(blkdev_get_by_dev);
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static int blkdev_open(struct inode * inode, struct file * filp)
1788{
1789 struct block_device *bdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
1791 /*
1792 * Preserve backwards compatibility and allow large file access
1793 * even if userspace doesn't ask for it explicitly. Some mkfs
1794 * binary needs it. We might want to drop this workaround
1795 * during an unstable branch.
1796 */
1797 filp->f_flags |= O_LARGEFILE;
1798
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001799 filp->f_mode |= FMODE_NOWAIT;
1800
Al Viro572c4892007-10-08 13:24:05 -04001801 if (filp->f_flags & O_NDELAY)
1802 filp->f_mode |= FMODE_NDELAY;
1803 if (filp->f_flags & O_EXCL)
1804 filp->f_mode |= FMODE_EXCL;
1805 if ((filp->f_flags & O_ACCMODE) == 3)
1806 filp->f_mode |= FMODE_WRITE_IOCTL;
1807
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 bdev = bd_acquire(inode);
Pavel Emelianov6a2aae02006-10-28 10:38:33 -07001809 if (bdev == NULL)
1810 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Al Viro572c4892007-10-08 13:24:05 -04001812 filp->f_mapping = bdev->bd_inode->i_mapping;
Jeff Layton5660e132017-07-06 07:02:25 -04001813 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
Al Viro572c4892007-10-08 13:24:05 -04001814
Tejun Heoe525fd82010-11-13 11:55:17 +01001815 return blkdev_get(bdev, filp->f_mode, filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Al Viro4385bab2013-05-05 22:11:03 -04001818static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001819{
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001820 struct gendisk *disk = bdev->bd_disk;
NeilBrown37be4122006-12-08 02:36:16 -08001821 struct block_device *victim = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001822
NeilBrown6796bf52006-12-08 02:36:16 -08001823 mutex_lock_nested(&bdev->bd_mutex, for_part);
NeilBrown37be4122006-12-08 02:36:16 -08001824 if (for_part)
1825 bdev->bd_part_count--;
1826
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001827 if (!--bdev->bd_openers) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001828 WARN_ON_ONCE(bdev->bd_holders);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001829 sync_blockdev(bdev);
1830 kill_bdev(bdev);
Ilya Dryomov43d1c0e2015-11-20 22:22:34 +01001831
Vivek Goyaldbd3ca52015-11-09 09:23:40 -07001832 bdev_write_inode(bdev);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001833 }
1834 if (bdev->bd_contains == bdev) {
1835 if (disk->fops->release)
Al Virodb2a1442013-05-05 21:52:57 -04001836 disk->fops->release(disk, mode);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001837 }
1838 if (!bdev->bd_openers) {
Tejun Heo0762b8b2008-08-25 19:56:12 +09001839 disk_put_part(bdev->bd_part);
1840 bdev->bd_part = NULL;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001841 bdev->bd_disk = NULL;
NeilBrown37be4122006-12-08 02:36:16 -08001842 if (bdev != bdev->bd_contains)
1843 victim = bdev->bd_contains;
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001844 bdev->bd_contains = NULL;
Tejun Heo523e1d32011-10-19 14:31:07 +02001845
Jan Kara9df6c292018-02-26 13:01:39 +01001846 put_disk_and_module(disk);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001847 }
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001848 mutex_unlock(&bdev->bd_mutex);
1849 bdput(bdev);
NeilBrown37be4122006-12-08 02:36:16 -08001850 if (victim)
Al Viro9a1c3542008-02-22 20:40:24 -05001851 __blkdev_put(victim, mode, 1);
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001852}
1853
Al Viro4385bab2013-05-05 22:11:03 -04001854void blkdev_put(struct block_device *bdev, fmode_t mode)
NeilBrown37be4122006-12-08 02:36:16 -08001855{
Tejun Heo85ef06d2011-07-01 16:17:47 +02001856 mutex_lock(&bdev->bd_mutex);
1857
Tejun Heoe525fd82010-11-13 11:55:17 +01001858 if (mode & FMODE_EXCL) {
Tejun Heo6a027ef2010-11-13 11:55:17 +01001859 bool bdev_free;
1860
1861 /*
1862 * Release a claim on the device. The holder fields
1863 * are protected with bdev_lock. bd_mutex is to
1864 * synchronize disk_holder unlinking.
1865 */
Tejun Heo6a027ef2010-11-13 11:55:17 +01001866 spin_lock(&bdev_lock);
1867
1868 WARN_ON_ONCE(--bdev->bd_holders < 0);
1869 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1870
1871 /* bd_contains might point to self, check in a separate step */
1872 if ((bdev_free = !bdev->bd_holders))
1873 bdev->bd_holder = NULL;
1874 if (!bdev->bd_contains->bd_holders)
1875 bdev->bd_contains->bd_holder = NULL;
1876
1877 spin_unlock(&bdev_lock);
1878
Tejun Heo77ea8872010-12-08 20:57:37 +01001879 /*
1880 * If this was the last claim, remove holder link and
1881 * unblock evpoll if it was a write holder.
1882 */
Tejun Heo85ef06d2011-07-01 16:17:47 +02001883 if (bdev_free && bdev->bd_write_holder) {
1884 disk_unblock_events(bdev->bd_disk);
1885 bdev->bd_write_holder = false;
Tejun Heo77ea8872010-12-08 20:57:37 +01001886 }
Tejun Heo69362172011-03-09 19:54:27 +01001887 }
Tejun Heo77ea8872010-12-08 20:57:37 +01001888
Tejun Heo85ef06d2011-07-01 16:17:47 +02001889 /*
1890 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1891 * event. This is to ensure detection of media removal commanded
1892 * from userland - e.g. eject(1).
1893 */
1894 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1895
1896 mutex_unlock(&bdev->bd_mutex);
1897
Al Viro4385bab2013-05-05 22:11:03 -04001898 __blkdev_put(bdev, mode, 0);
NeilBrown37be4122006-12-08 02:36:16 -08001899}
Peter Zijlstra2e7b6512006-12-08 02:36:13 -08001900EXPORT_SYMBOL(blkdev_put);
1901
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902static int blkdev_close(struct inode * inode, struct file * filp)
1903{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001904 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
Al Viro4385bab2013-05-05 22:11:03 -04001905 blkdev_put(bdev, filp->f_mode);
1906 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907}
1908
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07001909static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
Dan Williams4ebb16c2015-10-28 07:48:19 +09001911 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Al Viro56b26ad2008-09-19 03:17:36 -04001912 fmode_t mode = file->f_mode;
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001913
1914 /*
1915 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1916 * to updated it before every ioctl.
1917 */
Al Viro56b26ad2008-09-19 03:17:36 -04001918 if (file->f_flags & O_NDELAY)
Christoph Hellwigfd4ce1a2008-11-05 14:58:42 +01001919 mode |= FMODE_NDELAY;
1920 else
1921 mode &= ~FMODE_NDELAY;
1922
Al Viro56b26ad2008-09-19 03:17:36 -04001923 return blkdev_ioctl(bdev, mode, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001926/*
Christoph Hellwigeef99382009-08-20 17:43:41 +02001927 * Write data to the block device. Only intended for the block device itself
1928 * and the raw driver which basically is a fake block device.
1929 *
1930 * Does not take i_mutex for the write and thus is not for general purpose
1931 * use.
1932 */
Al Viro1456c0a2014-04-03 03:21:50 -04001933ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
Christoph Hellwigeef99382009-08-20 17:43:41 +02001934{
1935 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001936 struct inode *bd_inode = bdev_file_inode(file);
Al Viro7ec7b942015-04-07 11:35:14 -04001937 loff_t size = i_size_read(bd_inode);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001938 struct blk_plug plug;
Christoph Hellwigeef99382009-08-20 17:43:41 +02001939 ssize_t ret;
Al Viro5f380c72015-04-07 11:28:12 -04001940
Al Viro7ec7b942015-04-07 11:35:14 -04001941 if (bdev_read_only(I_BDEV(bd_inode)))
1942 return -EPERM;
Al Viro5f380c72015-04-07 11:28:12 -04001943
Al Viro7ec7b942015-04-07 11:35:14 -04001944 if (!iov_iter_count(from))
Al Viro5f380c72015-04-07 11:28:12 -04001945 return 0;
1946
Al Viro7ec7b942015-04-07 11:35:14 -04001947 if (iocb->ki_pos >= size)
1948 return -ENOSPC;
1949
Christoph Hellwigc35fc7a2017-08-29 16:13:21 +02001950 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1951 return -EOPNOTSUPP;
1952
Al Viro7ec7b942015-04-07 11:35:14 -04001953 iov_iter_truncate(from, size - iocb->ki_pos);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001954
Jianpeng Ma53362a02012-08-02 09:50:39 +02001955 blk_start_plug(&plug);
Al Viro1456c0a2014-04-03 03:21:50 -04001956 ret = __generic_file_write_iter(iocb, from);
Christoph Hellwige2592212016-04-07 08:52:01 -07001957 if (ret > 0)
1958 ret = generic_write_sync(iocb, ret);
Jianpeng Ma53362a02012-08-02 09:50:39 +02001959 blk_finish_plug(&plug);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001960 return ret;
1961}
Al Viro1456c0a2014-04-03 03:21:50 -04001962EXPORT_SYMBOL_GPL(blkdev_write_iter);
Christoph Hellwigeef99382009-08-20 17:43:41 +02001963
David Jefferyb2de5252014-09-29 10:21:10 -04001964ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001965{
1966 struct file *file = iocb->ki_filp;
Dan Williams4ebb16c2015-10-28 07:48:19 +09001967 struct inode *bd_inode = bdev_file_inode(file);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001968 loff_t size = i_size_read(bd_inode);
Al Viroa8860382014-04-02 20:02:21 -04001969 loff_t pos = iocb->ki_pos;
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001970
1971 if (pos >= size)
1972 return 0;
1973
1974 size -= pos;
Al Viroa8860382014-04-02 20:02:21 -04001975 iov_iter_truncate(to, size);
1976 return generic_file_read_iter(iocb, to);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001977}
David Jefferyb2de5252014-09-29 10:21:10 -04001978EXPORT_SYMBOL_GPL(blkdev_read_iter);
Linus Torvalds684c9aa2012-12-07 16:48:39 -08001979
Christoph Hellwigeef99382009-08-20 17:43:41 +02001980/*
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05001981 * Try to release a page associated with block device when the system
1982 * is under memory pressure.
1983 */
1984static int blkdev_releasepage(struct page *page, gfp_t wait)
1985{
1986 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1987
1988 if (super && super->s_op->bdev_try_to_free_page)
1989 return super->s_op->bdev_try_to_free_page(super, page, wait);
1990
1991 return try_to_free_buffers(page);
1992}
1993
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001994static int blkdev_writepages(struct address_space *mapping,
1995 struct writeback_control *wbc)
1996{
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08001997 return generic_writepages(mapping, wbc);
1998}
1999
Adrian Bunk4c54ac62008-02-18 13:48:31 +01002000static const struct address_space_operations def_blk_aops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 .readpage = blkdev_readpage,
Akinobu Mita447f05b2014-10-09 15:26:58 -07002002 .readpages = blkdev_readpages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 .writepage = blkdev_writepage,
Nick Piggin6272b5a2007-10-16 01:25:04 -07002004 .write_begin = blkdev_write_begin,
2005 .write_end = blkdev_write_end,
Ross Zwisler7f6d5b52016-02-26 15:19:55 -08002006 .writepages = blkdev_writepages,
Theodore Ts'o87d8fe12009-01-03 09:47:09 -05002007 .releasepage = blkdev_releasepage,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 .direct_IO = blkdev_direct_IO,
Jan Kara88dbcbb2018-12-28 00:39:16 -08002009 .migratepage = buffer_migrate_page_norefs,
Mel Gormanb4597222013-07-03 15:02:05 -07002010 .is_dirty_writeback = buffer_check_dirty_writeback,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011};
2012
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002013#define BLKDEV_FALLOC_FL_SUPPORTED \
2014 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
2015 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
2016
2017static long blkdev_fallocate(struct file *file, int mode, loff_t start,
2018 loff_t len)
2019{
2020 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002021 struct address_space *mapping;
2022 loff_t end = start + len - 1;
2023 loff_t isize;
2024 int error;
2025
2026 /* Fail if we don't recognize the flags. */
2027 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
2028 return -EOPNOTSUPP;
2029
2030 /* Don't go off the end of the device. */
2031 isize = i_size_read(bdev->bd_inode);
2032 if (start >= isize)
2033 return -EINVAL;
2034 if (end >= isize) {
2035 if (mode & FALLOC_FL_KEEP_SIZE) {
2036 len = isize - start;
2037 end = start + len - 1;
2038 } else
2039 return -EINVAL;
2040 }
2041
2042 /*
2043 * Don't allow IO that isn't aligned to logical block size.
2044 */
2045 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
2046 return -EINVAL;
2047
2048 /* Invalidate the page cache, including dirty pages. */
2049 mapping = bdev->bd_inode->i_mapping;
2050 truncate_inode_pages_range(mapping, start, end);
2051
2052 switch (mode) {
2053 case FALLOC_FL_ZERO_RANGE:
2054 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
2055 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
Christoph Hellwigee472d82017-04-05 19:21:08 +02002056 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002057 break;
2058 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
Christoph Hellwig34045122017-04-05 19:21:11 +02002059 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2060 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002061 break;
2062 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002063 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2064 GFP_KERNEL, 0);
2065 break;
2066 default:
2067 return -EOPNOTSUPP;
2068 }
2069 if (error)
2070 return error;
2071
2072 /*
2073 * Invalidate again; if someone wandered in and dirtied a page,
2074 * the caller will be given -EBUSY. The third argument is
2075 * inclusive, so the rounding here is safe.
2076 */
2077 return invalidate_inode_pages2_range(mapping,
2078 start >> PAGE_SHIFT,
2079 end >> PAGE_SHIFT);
2080}
2081
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08002082const struct file_operations def_blk_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 .open = blkdev_open,
2084 .release = blkdev_close,
2085 .llseek = block_llseek,
Al Viroa8860382014-04-02 20:02:21 -04002086 .read_iter = blkdev_read_iter,
Al Viro1456c0a2014-04-03 03:21:50 -04002087 .write_iter = blkdev_write_iter,
Christoph Hellwigeae83ce2018-11-30 08:31:52 -07002088 .iopoll = blkdev_iopoll,
Dan Williamsacc93d32016-05-07 11:40:28 -07002089 .mmap = generic_file_mmap,
Andrew Mortonb1dd3b22010-04-06 14:35:00 -07002090 .fsync = blkdev_fsync,
Arnd Bergmannbb93e3a2005-06-23 00:10:15 -07002091 .unlocked_ioctl = block_ioctl,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092#ifdef CONFIG_COMPAT
2093 .compat_ioctl = compat_blkdev_ioctl,
2094#endif
Linus Torvalds1e8b3332012-11-29 10:49:50 -08002095 .splice_read = generic_file_splice_read,
Al Viro8d020762014-04-05 04:27:08 -04002096 .splice_write = iter_file_splice_write,
Darrick J. Wong25f4c412016-10-11 13:51:11 -07002097 .fallocate = blkdev_fallocate,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098};
2099
2100int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
2101{
2102 int res;
2103 mm_segment_t old_fs = get_fs();
2104 set_fs(KERNEL_DS);
Al Viro56b26ad2008-09-19 03:17:36 -04002105 res = blkdev_ioctl(bdev, 0, cmd, arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 set_fs(old_fs);
2107 return res;
2108}
2109
2110EXPORT_SYMBOL(ioctl_by_bdev);
2111
2112/**
2113 * lookup_bdev - lookup a struct block_device by name
Randy Dunlap94e29592009-01-06 14:41:15 -08002114 * @pathname: special file representing the block device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 *
Randy Dunlap57d1b532008-10-09 10:42:38 +02002116 * Get a reference to the blockdevice at @pathname in the current
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 * namespace if possible and return it. Return ERR_PTR(error)
2118 * otherwise.
2119 */
Al Viro421748e2008-08-02 01:04:36 -04002120struct block_device *lookup_bdev(const char *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 struct block_device *bdev;
2123 struct inode *inode;
Al Viro421748e2008-08-02 01:04:36 -04002124 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 int error;
2126
Al Viro421748e2008-08-02 01:04:36 -04002127 if (!pathname || !*pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 return ERR_PTR(-EINVAL);
2129
Al Viro421748e2008-08-02 01:04:36 -04002130 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (error)
2132 return ERR_PTR(error);
2133
David Howellsbb6687342015-03-17 22:26:21 +00002134 inode = d_backing_inode(path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 error = -ENOTBLK;
2136 if (!S_ISBLK(inode->i_mode))
2137 goto fail;
2138 error = -EACCES;
Eric W. Biedermana2982cc2016-06-09 15:34:02 -05002139 if (!may_open_dev(&path))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 goto fail;
2141 error = -ENOMEM;
2142 bdev = bd_acquire(inode);
2143 if (!bdev)
2144 goto fail;
2145out:
Al Viro421748e2008-08-02 01:04:36 -04002146 path_put(&path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 return bdev;
2148fail:
2149 bdev = ERR_PTR(error);
2150 goto out;
2151}
Al Virod5686b42008-08-01 05:00:11 -04002152EXPORT_SYMBOL(lookup_bdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
NeilBrown93b270f2011-02-24 17:25:47 +11002154int __invalidate_device(struct block_device *bdev, bool kill_dirty)
David Howellsb71e8a42006-08-29 19:06:11 +01002155{
2156 struct super_block *sb = get_super(bdev);
2157 int res = 0;
2158
2159 if (sb) {
2160 /*
2161 * no need to lock the super, get_super holds the
2162 * read mutex so the filesystem cannot go away
2163 * under us (->put_super runs with the write lock
2164 * hold).
2165 */
2166 shrink_dcache_sb(sb);
NeilBrown93b270f2011-02-24 17:25:47 +11002167 res = invalidate_inodes(sb, kill_dirty);
David Howellsb71e8a42006-08-29 19:06:11 +01002168 drop_super(sb);
2169 }
Peter Zijlstraf98393a2007-05-06 14:49:54 -07002170 invalidate_bdev(bdev);
David Howellsb71e8a42006-08-29 19:06:11 +01002171 return res;
2172}
2173EXPORT_SYMBOL(__invalidate_device);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002174
2175void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2176{
2177 struct inode *inode, *old_inode = NULL;
2178
Dave Chinner74278da2015-03-04 12:37:22 -05002179 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002180 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2181 struct address_space *mapping = inode->i_mapping;
Rabin Vincentaf309222016-12-01 09:18:28 +01002182 struct block_device *bdev;
Jan Kara5c0d6b62012-07-03 16:45:31 +02002183
2184 spin_lock(&inode->i_lock);
2185 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2186 mapping->nrpages == 0) {
2187 spin_unlock(&inode->i_lock);
2188 continue;
2189 }
2190 __iget(inode);
2191 spin_unlock(&inode->i_lock);
Dave Chinner74278da2015-03-04 12:37:22 -05002192 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002193 /*
2194 * We hold a reference to 'inode' so it couldn't have been
2195 * removed from s_inodes list while we dropped the
Dave Chinner74278da2015-03-04 12:37:22 -05002196 * s_inode_list_lock We cannot iput the inode now as we can
Jan Kara5c0d6b62012-07-03 16:45:31 +02002197 * be holding the last reference and we cannot iput it under
Dave Chinner74278da2015-03-04 12:37:22 -05002198 * s_inode_list_lock. So we keep the reference and iput it
Jan Kara5c0d6b62012-07-03 16:45:31 +02002199 * later.
2200 */
2201 iput(old_inode);
2202 old_inode = inode;
Rabin Vincentaf309222016-12-01 09:18:28 +01002203 bdev = I_BDEV(inode);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002204
Rabin Vincentaf309222016-12-01 09:18:28 +01002205 mutex_lock(&bdev->bd_mutex);
2206 if (bdev->bd_openers)
2207 func(bdev, arg);
2208 mutex_unlock(&bdev->bd_mutex);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002209
Dave Chinner74278da2015-03-04 12:37:22 -05002210 spin_lock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002211 }
Dave Chinner74278da2015-03-04 12:37:22 -05002212 spin_unlock(&blockdev_superblock->s_inode_list_lock);
Jan Kara5c0d6b62012-07-03 16:45:31 +02002213 iput(old_inode);
2214}