block: split __blkdev_put
[linux-block.git] / fs / block_dev.c
CommitLineData
457c8996 1// SPDX-License-Identifier: GPL-2.0-only
1da177e4 2/*
1da177e4
LT
3 * Copyright (C) 1991, 1992 Linus Torvalds
4 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
7b51e703 5 * Copyright (C) 2016 - 2020 Christoph Hellwig
1da177e4
LT
6 */
7
1da177e4
LT
8#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>
7db9cfd3 14#include <linux/device_cgroup.h>
1da177e4
LT
15#include <linux/highmem.h>
16#include <linux/blkdev.h>
66114cad 17#include <linux/backing-dev.h>
1da177e4
LT
18#include <linux/module.h>
19#include <linux/blkpg.h>
b502bd11 20#include <linux/magic.h>
1da177e4 21#include <linux/buffer_head.h>
ff01bb48 22#include <linux/swap.h>
585d3bc0 23#include <linux/pagevec.h>
811d736f 24#include <linux/writeback.h>
1da177e4
LT
25#include <linux/mpage.h>
26#include <linux/mount.h>
9030d16e 27#include <linux/pseudo_fs.h>
1da177e4
LT
28#include <linux/uio.h>
29#include <linux/namei.h>
1368c4f2 30#include <linux/log2.h>
ff01bb48 31#include <linux/cleancache.h>
189ce2b9 32#include <linux/task_io_accounting_ops.h>
25f4c414 33#include <linux/falloc.h>
15e3d2c5 34#include <linux/part_stat.h>
7c0f6ba6 35#include <linux/uaccess.h>
56939e01 36#include <linux/suspend.h>
07f3f05c 37#include "internal.h"
1da177e4
LT
38
39struct bdev_inode {
40 struct block_device bdev;
41 struct inode vfs_inode;
42};
43
4c54ac62
AB
44static const struct address_space_operations def_blk_aops;
45
1da177e4
LT
46static inline struct bdev_inode *BDEV_I(struct inode *inode)
47{
48 return container_of(inode, struct bdev_inode, vfs_inode);
49}
50
ff5053f6 51struct block_device *I_BDEV(struct inode *inode)
1da177e4
LT
52{
53 return &BDEV_I(inode)->bdev;
54}
1da177e4
LT
55EXPORT_SYMBOL(I_BDEV);
56
dbd3ca50 57static void bdev_write_inode(struct block_device *bdev)
564f00f6 58{
dbd3ca50
VG
59 struct inode *inode = bdev->bd_inode;
60 int ret;
61
564f00f6
CH
62 spin_lock(&inode->i_lock);
63 while (inode->i_state & I_DIRTY) {
64 spin_unlock(&inode->i_lock);
dbd3ca50
VG
65 ret = write_inode_now(inode, true);
66 if (ret) {
67 char name[BDEVNAME_SIZE];
68 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
69 "for block device %s (err=%d).\n",
70 bdevname(bdev, name), ret);
71 }
564f00f6
CH
72 spin_lock(&inode->i_lock);
73 }
74 spin_unlock(&inode->i_lock);
75}
76
f9a14399 77/* Kill _all_ buffers and pagecache , dirty or not.. */
3373a346 78static void kill_bdev(struct block_device *bdev)
1da177e4 79{
ff01bb48
AV
80 struct address_space *mapping = bdev->bd_inode->i_mapping;
81
7716506a 82 if (mapping_empty(mapping))
f9a14399 83 return;
ff01bb48 84
f9a14399 85 invalidate_bh_lrus();
ff01bb48 86 truncate_inode_pages(mapping, 0);
3373a346 87}
ff01bb48
AV
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
a5f6a6a9
AR
94 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 }
ff01bb48
AV
99 /* 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 */
3167760f 102 cleancache_invalidate_inode(mapping);
ff01bb48
AV
103}
104EXPORT_SYMBOL(invalidate_bdev);
1da177e4 105
384d87ef
JK
106/*
107 * Drop all buffers & page cache for given bdev range. This function bails
108 * with error if bdev has other exclusive owner (such as filesystem).
109 */
110int truncate_bdev_range(struct block_device *bdev, fmode_t mode,
111 loff_t lstart, loff_t lend)
112{
384d87ef
JK
113 /*
114 * If we don't hold exclusive handle for the device, upgrade to it
115 * while we discard the buffer cache to avoid discarding buffers
116 * under live filesystem.
117 */
118 if (!(mode & FMODE_EXCL)) {
37c3fc9a 119 int err = bd_prepare_to_claim(bdev, truncate_bdev_range);
384d87ef 120 if (err)
56887cff 121 goto invalidate;
384d87ef 122 }
37c3fc9a 123
384d87ef 124 truncate_inode_pages_range(bdev->bd_inode->i_mapping, lstart, lend);
37c3fc9a
CH
125 if (!(mode & FMODE_EXCL))
126 bd_abort_claiming(bdev, truncate_bdev_range);
384d87ef 127 return 0;
56887cff
JK
128
129invalidate:
130 /*
131 * Someone else has handle exclusively open. Try invalidating instead.
132 * The 'end' argument is inclusive so the rounding is safe.
133 */
134 return invalidate_inode_pages2_range(bdev->bd_inode->i_mapping,
135 lstart >> PAGE_SHIFT,
136 lend >> PAGE_SHIFT);
384d87ef 137}
384d87ef 138
04906b2f
JK
139static void set_init_blocksize(struct block_device *bdev)
140{
8dc932d3
MM
141 unsigned int bsize = bdev_logical_block_size(bdev);
142 loff_t size = i_size_read(bdev->bd_inode);
143
144 while (bsize < PAGE_SIZE) {
145 if (size & bsize)
146 break;
147 bsize <<= 1;
148 }
149 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
04906b2f
JK
150}
151
1da177e4
LT
152int set_blocksize(struct block_device *bdev, int size)
153{
154 /* Size must be a power of two, and between 512 and PAGE_SIZE */
1368c4f2 155 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
1da177e4
LT
156 return -EINVAL;
157
158 /* Size cannot be smaller than the size supported by the device */
e1defc4f 159 if (size < bdev_logical_block_size(bdev))
1da177e4
LT
160 return -EINVAL;
161
162 /* Don't change the size if it is same as current */
6b7b181b 163 if (bdev->bd_inode->i_blkbits != blksize_bits(size)) {
1da177e4 164 sync_blockdev(bdev);
1da177e4
LT
165 bdev->bd_inode->i_blkbits = blksize_bits(size);
166 kill_bdev(bdev);
167 }
168 return 0;
169}
170
171EXPORT_SYMBOL(set_blocksize);
172
173int sb_set_blocksize(struct super_block *sb, int size)
174{
1da177e4
LT
175 if (set_blocksize(sb->s_bdev, size))
176 return 0;
177 /* If we get here, we know size is power of two
178 * and it's value is between 512 and PAGE_SIZE */
179 sb->s_blocksize = size;
38885bd4 180 sb->s_blocksize_bits = blksize_bits(size);
1da177e4
LT
181 return sb->s_blocksize;
182}
183
184EXPORT_SYMBOL(sb_set_blocksize);
185
186int sb_min_blocksize(struct super_block *sb, int size)
187{
e1defc4f 188 int minsize = bdev_logical_block_size(sb->s_bdev);
1da177e4
LT
189 if (size < minsize)
190 size = minsize;
191 return sb_set_blocksize(sb, size);
192}
193
194EXPORT_SYMBOL(sb_min_blocksize);
195
196static int
197blkdev_get_block(struct inode *inode, sector_t iblock,
198 struct buffer_head *bh, int create)
199{
1da177e4
LT
200 bh->b_bdev = I_BDEV(inode);
201 bh->b_blocknr = iblock;
202 set_buffer_mapped(bh);
203 return 0;
204}
205
4ebb16ca
DW
206static struct inode *bdev_file_inode(struct file *file)
207{
208 return file->f_mapping->host;
209}
210
78250c02
JA
211static unsigned int dio_bio_write_op(struct kiocb *iocb)
212{
213 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
214
215 /* avoid the need for a I/O completion work item */
216 if (iocb->ki_flags & IOCB_DSYNC)
217 op |= REQ_FUA;
218 return op;
219}
220
189ce2b9
CH
221#define DIO_INLINE_BIO_VECS 4
222
223static void blkdev_bio_end_io_simple(struct bio *bio)
224{
225 struct task_struct *waiter = bio->bi_private;
226
227 WRITE_ONCE(bio->bi_private, NULL);
0619317f 228 blk_wake_io_task(waiter);
189ce2b9
CH
229}
230
231static ssize_t
232__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
5f7136db 233 unsigned int nr_pages)
189ce2b9
CH
234{
235 struct file *file = iocb->ki_filp;
236 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
9fec4a21 237 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs;
189ce2b9
CH
238 loff_t pos = iocb->ki_pos;
239 bool should_dirty = false;
240 struct bio bio;
241 ssize_t ret;
242 blk_qc_t qc;
189ce2b9 243
9a794fb9
JA
244 if ((pos | iov_iter_alignment(iter)) &
245 (bdev_logical_block_size(bdev) - 1))
189ce2b9
CH
246 return -EINVAL;
247
72ecad22
JA
248 if (nr_pages <= DIO_INLINE_BIO_VECS)
249 vecs = inline_vecs;
250 else {
6da2ec56
KC
251 vecs = kmalloc_array(nr_pages, sizeof(struct bio_vec),
252 GFP_KERNEL);
72ecad22
JA
253 if (!vecs)
254 return -ENOMEM;
255 }
256
3a83f467 257 bio_init(&bio, vecs, nr_pages);
74d46992 258 bio_set_dev(&bio, bdev);
4d1a4765 259 bio.bi_iter.bi_sector = pos >> 9;
45d06cf7 260 bio.bi_write_hint = iocb->ki_hint;
189ce2b9
CH
261 bio.bi_private = current;
262 bio.bi_end_io = blkdev_bio_end_io_simple;
074111ca 263 bio.bi_ioprio = iocb->ki_ioprio;
189ce2b9
CH
264
265 ret = bio_iov_iter_get_pages(&bio, iter);
266 if (unlikely(ret))
9362dd11 267 goto out;
189ce2b9
CH
268 ret = bio.bi_iter.bi_size;
269
270 if (iov_iter_rw(iter) == READ) {
78250c02 271 bio.bi_opf = REQ_OP_READ;
189ce2b9
CH
272 if (iter_is_iovec(iter))
273 should_dirty = true;
274 } else {
78250c02 275 bio.bi_opf = dio_bio_write_op(iocb);
189ce2b9
CH
276 task_io_account_write(ret);
277 }
f8b78caf
PB
278 if (iocb->ki_flags & IOCB_NOWAIT)
279 bio.bi_opf |= REQ_NOWAIT;
d1e36282 280 if (iocb->ki_flags & IOCB_HIPRI)
0bbb280d 281 bio_set_polled(&bio, iocb);
189ce2b9
CH
282
283 qc = submit_bio(&bio);
284 for (;;) {
1ac5cd49 285 set_current_state(TASK_UNINTERRUPTIBLE);
189ce2b9
CH
286 if (!READ_ONCE(bio.bi_private))
287 break;
288 if (!(iocb->ki_flags & IOCB_HIPRI) ||
0a1b8b87 289 !blk_poll(bdev_get_queue(bdev), qc, true))
e6249cdd 290 blk_io_schedule();
189ce2b9
CH
291 }
292 __set_current_state(TASK_RUNNING);
293
9fec4a21 294 bio_release_pages(&bio, should_dirty);
4e4cbee9 295 if (unlikely(bio.bi_status))
c6b1e36c 296 ret = blk_status_to_errno(bio.bi_status);
9ae3b3f5 297
9362dd11
MW
298out:
299 if (vecs != inline_vecs)
300 kfree(vecs);
301
9ae3b3f5
JA
302 bio_uninit(&bio);
303
189ce2b9
CH
304 return ret;
305}
306
542ff7bf
CH
307struct blkdev_dio {
308 union {
309 struct kiocb *iocb;
310 struct task_struct *waiter;
311 };
312 size_t size;
313 atomic_t ref;
314 bool multi_bio : 1;
315 bool should_dirty : 1;
316 bool is_sync : 1;
317 struct bio bio;
318};
319
52190f8a 320static struct bio_set blkdev_dio_pool;
542ff7bf 321
eae83ce1
CH
322static int blkdev_iopoll(struct kiocb *kiocb, bool wait)
323{
324 struct block_device *bdev = I_BDEV(kiocb->ki_filp->f_mapping->host);
325 struct request_queue *q = bdev_get_queue(bdev);
326
327 return blk_poll(q, READ_ONCE(kiocb->ki_cookie), wait);
328}
329
542ff7bf
CH
330static void blkdev_bio_end_io(struct bio *bio)
331{
332 struct blkdev_dio *dio = bio->bi_private;
333 bool should_dirty = dio->should_dirty;
334
a89afe58
JY
335 if (bio->bi_status && !dio->bio.bi_status)
336 dio->bio.bi_status = bio->bi_status;
337
338 if (!dio->multi_bio || atomic_dec_and_test(&dio->ref)) {
542ff7bf
CH
339 if (!dio->is_sync) {
340 struct kiocb *iocb = dio->iocb;
4e4cbee9 341 ssize_t ret;
542ff7bf 342
4e4cbee9 343 if (likely(!dio->bio.bi_status)) {
542ff7bf
CH
344 ret = dio->size;
345 iocb->ki_pos += ret;
4e4cbee9
CH
346 } else {
347 ret = blk_status_to_errno(dio->bio.bi_status);
542ff7bf
CH
348 }
349
350 dio->iocb->ki_complete(iocb, ret, 0);
531724ab
CH
351 if (dio->multi_bio)
352 bio_put(&dio->bio);
542ff7bf
CH
353 } else {
354 struct task_struct *waiter = dio->waiter;
355
356 WRITE_ONCE(dio->waiter, NULL);
0619317f 357 blk_wake_io_task(waiter);
542ff7bf
CH
358 }
359 }
360
361 if (should_dirty) {
362 bio_check_pages_dirty(bio);
363 } else {
57dfe3ce 364 bio_release_pages(bio, false);
542ff7bf
CH
365 bio_put(bio);
366 }
367}
368
5f7136db
MWO
369static ssize_t __blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter,
370 unsigned int nr_pages)
b2e895db
AM
371{
372 struct file *file = iocb->ki_filp;
4ebb16ca 373 struct inode *inode = bdev_file_inode(file);
542ff7bf 374 struct block_device *bdev = I_BDEV(inode);
64d656a1 375 struct blk_plug plug;
542ff7bf
CH
376 struct blkdev_dio *dio;
377 struct bio *bio;
cb700eb3 378 bool is_poll = (iocb->ki_flags & IOCB_HIPRI) != 0;
690e5325 379 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
542ff7bf
CH
380 loff_t pos = iocb->ki_pos;
381 blk_qc_t qc = BLK_QC_T_NONE;
7b6620d7 382 int ret = 0;
542ff7bf 383
9a794fb9
JA
384 if ((pos | iov_iter_alignment(iter)) &
385 (bdev_logical_block_size(bdev) - 1))
542ff7bf
CH
386 return -EINVAL;
387
7b6620d7 388 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, &blkdev_dio_pool);
542ff7bf
CH
389
390 dio = container_of(bio, struct blkdev_dio, bio);
690e5325 391 dio->is_sync = is_sync = is_sync_kiocb(iocb);
531724ab 392 if (dio->is_sync) {
542ff7bf 393 dio->waiter = current;
531724ab
CH
394 bio_get(bio);
395 } else {
542ff7bf 396 dio->iocb = iocb;
531724ab 397 }
542ff7bf
CH
398
399 dio->size = 0;
400 dio->multi_bio = false;
00e23707 401 dio->should_dirty = is_read && iter_is_iovec(iter);
542ff7bf 402
cb700eb3
JA
403 /*
404 * Don't plug for HIPRI/polled IO, as those should go straight
405 * to issue
406 */
407 if (!is_poll)
408 blk_start_plug(&plug);
409
542ff7bf 410 for (;;) {
74d46992 411 bio_set_dev(bio, bdev);
4d1a4765 412 bio->bi_iter.bi_sector = pos >> 9;
45d06cf7 413 bio->bi_write_hint = iocb->ki_hint;
542ff7bf
CH
414 bio->bi_private = dio;
415 bio->bi_end_io = blkdev_bio_end_io;
074111ca 416 bio->bi_ioprio = iocb->ki_ioprio;
542ff7bf 417
e15c2ffa
JA
418 ret = bio_iov_iter_get_pages(bio, iter);
419 if (unlikely(ret)) {
4e4cbee9 420 bio->bi_status = BLK_STS_IOERR;
542ff7bf
CH
421 bio_endio(bio);
422 break;
423 }
424
425 if (is_read) {
426 bio->bi_opf = REQ_OP_READ;
427 if (dio->should_dirty)
428 bio_set_pages_dirty(bio);
429 } else {
430 bio->bi_opf = dio_bio_write_op(iocb);
431 task_io_account_write(bio->bi_iter.bi_size);
432 }
f8b78caf
PB
433 if (iocb->ki_flags & IOCB_NOWAIT)
434 bio->bi_opf |= REQ_NOWAIT;
542ff7bf 435
7b6620d7 436 dio->size += bio->bi_iter.bi_size;
542ff7bf
CH
437 pos += bio->bi_iter.bi_size;
438
a8affc03 439 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS);
542ff7bf 440 if (!nr_pages) {
eae83ce1
CH
441 bool polled = false;
442
443 if (iocb->ki_flags & IOCB_HIPRI) {
0bbb280d 444 bio_set_polled(bio, iocb);
eae83ce1
CH
445 polled = true;
446 }
d34513d3 447
542ff7bf 448 qc = submit_bio(bio);
eae83ce1
CH
449
450 if (polled)
451 WRITE_ONCE(iocb->ki_cookie, qc);
542ff7bf
CH
452 break;
453 }
454
455 if (!dio->multi_bio) {
531724ab
CH
456 /*
457 * AIO needs an extra reference to ensure the dio
458 * structure which is embedded into the first bio
459 * stays around.
460 */
461 if (!is_sync)
462 bio_get(bio);
542ff7bf
CH
463 dio->multi_bio = true;
464 atomic_set(&dio->ref, 2);
465 } else {
466 atomic_inc(&dio->ref);
467 }
468
7b6620d7
JA
469 submit_bio(bio);
470 bio = bio_alloc(GFP_KERNEL, nr_pages);
542ff7bf 471 }
cb700eb3
JA
472
473 if (!is_poll)
474 blk_finish_plug(&plug);
542ff7bf 475
690e5325 476 if (!is_sync)
542ff7bf
CH
477 return -EIOCBQUEUED;
478
479 for (;;) {
1ac5cd49 480 set_current_state(TASK_UNINTERRUPTIBLE);
542ff7bf
CH
481 if (!READ_ONCE(dio->waiter))
482 break;
483
484 if (!(iocb->ki_flags & IOCB_HIPRI) ||
0a1b8b87 485 !blk_poll(bdev_get_queue(bdev), qc, true))
e6249cdd 486 blk_io_schedule();
542ff7bf
CH
487 }
488 __set_current_state(TASK_RUNNING);
489
36ffc6c1 490 if (!ret)
4e4cbee9 491 ret = blk_status_to_errno(dio->bio.bi_status);
e15c2ffa
JA
492 if (likely(!ret))
493 ret = dio->size;
542ff7bf
CH
494
495 bio_put(&dio->bio);
496 return ret;
497}
498
499static ssize_t
500blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
501{
5f7136db 502 unsigned int nr_pages;
b2e895db 503
3e1a88ec 504 if (!iov_iter_count(iter))
189ce2b9 505 return 0;
3e1a88ec 506
a8affc03
CH
507 nr_pages = bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS + 1);
508 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_VECS)
189ce2b9 509 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
542ff7bf 510
5f7136db 511 return __blkdev_direct_IO(iocb, iter, bio_max_segs(nr_pages));
542ff7bf
CH
512}
513
514static __init int blkdev_init(void)
515{
52190f8a 516 return bioset_init(&blkdev_dio_pool, 4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
b2e895db 517}
542ff7bf 518module_init(blkdev_init);
b2e895db 519
5cee5815
JK
520int __sync_blockdev(struct block_device *bdev, int wait)
521{
522 if (!bdev)
523 return 0;
524 if (!wait)
525 return filemap_flush(bdev->bd_inode->i_mapping);
526 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
527}
528
585d3bc0
NP
529/*
530 * Write out and wait upon all the dirty data associated with a block
531 * device via its mapping. Does not take the superblock lock.
532 */
533int sync_blockdev(struct block_device *bdev)
534{
5cee5815 535 return __sync_blockdev(bdev, 1);
585d3bc0
NP
536}
537EXPORT_SYMBOL(sync_blockdev);
538
539/*
540 * Write out and wait upon all dirty data associated with this
541 * device. Filesystem data as well as the underlying block
542 * device. Takes the superblock lock.
543 */
544int fsync_bdev(struct block_device *bdev)
545{
546 struct super_block *sb = get_super(bdev);
547 if (sb) {
60b0680f 548 int res = sync_filesystem(sb);
585d3bc0
NP
549 drop_super(sb);
550 return res;
551 }
552 return sync_blockdev(bdev);
553}
47e4491b 554EXPORT_SYMBOL(fsync_bdev);
585d3bc0
NP
555
556/**
557 * freeze_bdev -- lock a filesystem and force it into a consistent state
558 * @bdev: blockdevice to lock
559 *
585d3bc0
NP
560 * If a superblock is found on this device, we take the s_umount semaphore
561 * on it to make sure nobody unmounts until the snapshot creation is done.
562 * The reference counter (bd_fsfreeze_count) guarantees that only the last
563 * unfreeze process can unfreeze the frozen filesystem actually when multiple
564 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
565 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
566 * actually.
567 */
040f04bd 568int freeze_bdev(struct block_device *bdev)
585d3bc0
NP
569{
570 struct super_block *sb;
571 int error = 0;
572
573 mutex_lock(&bdev->bd_fsfreeze_mutex);
040f04bd
CH
574 if (++bdev->bd_fsfreeze_count > 1)
575 goto done;
4504230a
CH
576
577 sb = get_active_super(bdev);
578 if (!sb)
040f04bd 579 goto sync;
48b6bca6
BM
580 if (sb->s_op->freeze_super)
581 error = sb->s_op->freeze_super(sb);
582 else
583 error = freeze_super(sb);
040f04bd
CH
584 deactivate_super(sb);
585
18e9e510 586 if (error) {
18e9e510 587 bdev->bd_fsfreeze_count--;
040f04bd 588 goto done;
585d3bc0 589 }
040f04bd
CH
590 bdev->bd_fsfreeze_sb = sb;
591
592sync:
585d3bc0 593 sync_blockdev(bdev);
040f04bd 594done:
585d3bc0 595 mutex_unlock(&bdev->bd_fsfreeze_mutex);
040f04bd 596 return error;
585d3bc0
NP
597}
598EXPORT_SYMBOL(freeze_bdev);
599
600/**
601 * thaw_bdev -- unlock filesystem
602 * @bdev: blockdevice to unlock
585d3bc0
NP
603 *
604 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
605 */
040f04bd 606int thaw_bdev(struct block_device *bdev)
585d3bc0 607{
040f04bd 608 struct super_block *sb;
4504230a 609 int error = -EINVAL;
585d3bc0
NP
610
611 mutex_lock(&bdev->bd_fsfreeze_mutex);
4504230a 612 if (!bdev->bd_fsfreeze_count)
18e9e510 613 goto out;
4504230a
CH
614
615 error = 0;
616 if (--bdev->bd_fsfreeze_count > 0)
18e9e510 617 goto out;
4504230a 618
040f04bd 619 sb = bdev->bd_fsfreeze_sb;
4504230a 620 if (!sb)
18e9e510 621 goto out;
4504230a 622
48b6bca6
BM
623 if (sb->s_op->thaw_super)
624 error = sb->s_op->thaw_super(sb);
625 else
626 error = thaw_super(sb);
997198ba 627 if (error)
18e9e510 628 bdev->bd_fsfreeze_count++;
04a6a536
ST
629 else
630 bdev->bd_fsfreeze_sb = NULL;
18e9e510 631out:
585d3bc0 632 mutex_unlock(&bdev->bd_fsfreeze_mutex);
997198ba 633 return error;
585d3bc0
NP
634}
635EXPORT_SYMBOL(thaw_bdev);
636
1da177e4
LT
637static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
638{
639 return block_write_full_page(page, blkdev_get_block, wbc);
640}
641
642static int blkdev_readpage(struct file * file, struct page * page)
643{
644 return block_read_full_page(page, blkdev_get_block);
645}
646
d4388340 647static void blkdev_readahead(struct readahead_control *rac)
447f05bb 648{
d4388340 649 mpage_readahead(rac, blkdev_get_block);
447f05bb
AM
650}
651
6272b5a5
NP
652static int blkdev_write_begin(struct file *file, struct address_space *mapping,
653 loff_t pos, unsigned len, unsigned flags,
654 struct page **pagep, void **fsdata)
1da177e4 655{
155130a4
CH
656 return block_write_begin(mapping, pos, len, flags, pagep,
657 blkdev_get_block);
1da177e4
LT
658}
659
6272b5a5
NP
660static int blkdev_write_end(struct file *file, struct address_space *mapping,
661 loff_t pos, unsigned len, unsigned copied,
662 struct page *page, void *fsdata)
1da177e4 663{
6272b5a5
NP
664 int ret;
665 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
666
667 unlock_page(page);
09cbfeaf 668 put_page(page);
6272b5a5
NP
669
670 return ret;
1da177e4
LT
671}
672
673/*
674 * private llseek:
496ad9aa 675 * for a block special file file_inode(file)->i_size is zero
1da177e4
LT
676 * so we compute the size by hand (just as in block_read/write above)
677 */
965c8e59 678static loff_t block_llseek(struct file *file, loff_t offset, int whence)
1da177e4 679{
4ebb16ca 680 struct inode *bd_inode = bdev_file_inode(file);
1da177e4
LT
681 loff_t retval;
682
5955102c 683 inode_lock(bd_inode);
5d48f3a2 684 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
5955102c 685 inode_unlock(bd_inode);
1da177e4
LT
686 return retval;
687}
688
02c24a82 689int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
1da177e4 690{
4ebb16ca 691 struct inode *bd_inode = bdev_file_inode(filp);
b8af67e2 692 struct block_device *bdev = I_BDEV(bd_inode);
ab0a9735 693 int error;
da5aa861 694
372cf243 695 error = file_write_and_wait_range(filp, start, end);
da5aa861
RW
696 if (error)
697 return error;
ab0a9735 698
b8af67e2
AB
699 /*
700 * There is no need to serialise calls to blkdev_issue_flush with
701 * i_mutex and doing so causes performance issues with concurrent
702 * O_SYNC writers to a block device.
703 */
c6bf3f0e 704 error = blkdev_issue_flush(bdev);
ab0a9735
CH
705 if (error == -EOPNOTSUPP)
706 error = 0;
b8af67e2 707
ab0a9735 708 return error;
1da177e4 709}
b1dd3b28 710EXPORT_SYMBOL(blkdev_fsync);
1da177e4 711
47a191fd
MW
712/**
713 * bdev_read_page() - Start reading a page from a block device
714 * @bdev: The device to read the page from
715 * @sector: The offset on the device to read the page to (need not be aligned)
716 * @page: The page to read
717 *
718 * On entry, the page should be locked. It will be unlocked when the page
719 * has been read. If the block driver implements rw_page synchronously,
720 * that will be true on exit from this function, but it need not be.
721 *
722 * Errors returned by this function are usually "soft", eg out of memory, or
723 * queue full; callers should try a different route to read this page rather
724 * than propagate an error back up the stack.
725 *
726 * Return: negative errno if an error occurs, 0 if submission was successful.
727 */
728int bdev_read_page(struct block_device *bdev, sector_t sector,
729 struct page *page)
730{
731 const struct block_device_operations *ops = bdev->bd_disk->fops;
2e6edc95
DW
732 int result = -EOPNOTSUPP;
733
f68eb1e7 734 if (!ops->rw_page || bdev_get_integrity(bdev))
2e6edc95
DW
735 return result;
736
e556f6ba 737 result = blk_queue_enter(bdev->bd_disk->queue, 0);
2e6edc95
DW
738 if (result)
739 return result;
3f289dcb
TH
740 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
741 REQ_OP_READ);
e556f6ba 742 blk_queue_exit(bdev->bd_disk->queue);
2e6edc95 743 return result;
47a191fd 744}
47a191fd
MW
745
746/**
747 * bdev_write_page() - Start writing a page to a block device
748 * @bdev: The device to write the page to
749 * @sector: The offset on the device to write the page to (need not be aligned)
750 * @page: The page to write
751 * @wbc: The writeback_control for the write
752 *
753 * On entry, the page should be locked and not currently under writeback.
754 * On exit, if the write started successfully, the page will be unlocked and
755 * under writeback. If the write failed already (eg the driver failed to
756 * queue the page to the device), the page will still be locked. If the
757 * caller is a ->writepage implementation, it will need to unlock the page.
758 *
759 * Errors returned by this function are usually "soft", eg out of memory, or
760 * queue full; callers should try a different route to write this page rather
761 * than propagate an error back up the stack.
762 *
763 * Return: negative errno if an error occurs, 0 if submission was successful.
764 */
765int bdev_write_page(struct block_device *bdev, sector_t sector,
766 struct page *page, struct writeback_control *wbc)
767{
768 int result;
47a191fd 769 const struct block_device_operations *ops = bdev->bd_disk->fops;
2e6edc95 770
f68eb1e7 771 if (!ops->rw_page || bdev_get_integrity(bdev))
47a191fd 772 return -EOPNOTSUPP;
e556f6ba 773 result = blk_queue_enter(bdev->bd_disk->queue, 0);
2e6edc95
DW
774 if (result)
775 return result;
776
47a191fd 777 set_page_writeback(page);
3f289dcb
TH
778 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page,
779 REQ_OP_WRITE);
f892760a 780 if (result) {
47a191fd 781 end_page_writeback(page);
f892760a
MW
782 } else {
783 clean_page_buffers(page);
47a191fd 784 unlock_page(page);
f892760a 785 }
e556f6ba 786 blk_queue_exit(bdev->bd_disk->queue);
47a191fd
MW
787 return result;
788}
47a191fd 789
1da177e4
LT
790/*
791 * pseudo-fs
792 */
793
794static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
e18b890b 795static struct kmem_cache * bdev_cachep __read_mostly;
1da177e4
LT
796
797static struct inode *bdev_alloc_inode(struct super_block *sb)
798{
e94b1766 799 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
2d2f6f1b 800
1da177e4
LT
801 if (!ei)
802 return NULL;
2d2f6f1b
CH
803 memset(&ei->bdev, 0, sizeof(ei->bdev));
804 ei->bdev.bd_bdi = &noop_backing_dev_info;
1da177e4
LT
805 return &ei->vfs_inode;
806}
807
41149cb0 808static void bdev_free_inode(struct inode *inode)
1da177e4 809{
15e3d2c5
CH
810 struct block_device *bdev = I_BDEV(inode);
811
812 free_percpu(bdev->bd_stats);
231926db 813 kfree(bdev->bd_meta_info);
15e3d2c5 814
41149cb0 815 kmem_cache_free(bdev_cachep, BDEV_I(inode));
fa0d7e3d
NP
816}
817
e6cb5382 818static void init_once(void *data)
1da177e4 819{
e6cb5382 820 struct bdev_inode *ei = data;
1da177e4 821
a35afb83 822 inode_init_once(&ei->vfs_inode);
1da177e4
LT
823}
824
b57922d9 825static void bdev_evict_inode(struct inode *inode)
1da177e4
LT
826{
827 struct block_device *bdev = &BDEV_I(inode)->bdev;
91b0abe3 828 truncate_inode_pages_final(&inode->i_data);
b57922d9 829 invalidate_inode_buffers(inode); /* is it needed here? */
dbd5768f 830 clear_inode(inode);
f759741d
JK
831 /* Detach inode from wb early as bdi_put() may free bdi->wb */
832 inode_detach_wb(inode);
a5a79d00 833 if (bdev->bd_bdi != &noop_backing_dev_info) {
b1d2dc56 834 bdi_put(bdev->bd_bdi);
a5a79d00
JK
835 bdev->bd_bdi = &noop_backing_dev_info;
836 }
1da177e4
LT
837}
838
ee9b6d61 839static const struct super_operations bdev_sops = {
1da177e4
LT
840 .statfs = simple_statfs,
841 .alloc_inode = bdev_alloc_inode,
41149cb0 842 .free_inode = bdev_free_inode,
1da177e4 843 .drop_inode = generic_delete_inode,
b57922d9 844 .evict_inode = bdev_evict_inode,
1da177e4
LT
845};
846
9030d16e 847static int bd_init_fs_context(struct fs_context *fc)
1da177e4 848{
9030d16e
DH
849 struct pseudo_fs_context *ctx = init_pseudo(fc, BDEVFS_MAGIC);
850 if (!ctx)
851 return -ENOMEM;
852 fc->s_iflags |= SB_I_CGROUPWB;
853 ctx->ops = &bdev_sops;
854 return 0;
1da177e4
LT
855}
856
857static struct file_system_type bd_type = {
858 .name = "bdev",
9030d16e 859 .init_fs_context = bd_init_fs_context,
1da177e4
LT
860 .kill_sb = kill_anon_super,
861};
862
a212b105
TH
863struct super_block *blockdev_superblock __read_mostly;
864EXPORT_SYMBOL_GPL(blockdev_superblock);
1da177e4
LT
865
866void __init bdev_cache_init(void)
867{
868 int err;
ace8577a 869 static struct vfsmount *bd_mnt;
c2acf7b9 870
1da177e4 871 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
fffb60f9 872 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
5d097056 873 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
20c2df83 874 init_once);
1da177e4
LT
875 err = register_filesystem(&bd_type);
876 if (err)
877 panic("Cannot register bdev pseudo-fs");
878 bd_mnt = kern_mount(&bd_type);
1da177e4
LT
879 if (IS_ERR(bd_mnt))
880 panic("Cannot create bdev pseudo-fs");
ace8577a 881 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
1da177e4
LT
882}
883
22ae8ce8 884struct block_device *bdev_alloc(struct gendisk *disk, u8 partno)
1da177e4
LT
885{
886 struct block_device *bdev;
887 struct inode *inode;
888
22ae8ce8 889 inode = new_inode(blockdev_superblock);
1da177e4
LT
890 if (!inode)
891 return NULL;
22ae8ce8
CH
892 inode->i_mode = S_IFBLK;
893 inode->i_rdev = 0;
894 inode->i_data.a_ops = &def_blk_aops;
895 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
896
897 bdev = I_BDEV(inode);
e6cb5382 898 mutex_init(&bdev->bd_fsfreeze_mutex);
22ae8ce8
CH
899 spin_lock_init(&bdev->bd_size_lock);
900 bdev->bd_disk = disk;
901 bdev->bd_partno = partno;
22ae8ce8 902 bdev->bd_inode = inode;
e6cb5382
CH
903#ifdef CONFIG_SYSFS
904 INIT_LIST_HEAD(&bdev->bd_holder_disks);
905#endif
15e3d2c5
CH
906 bdev->bd_stats = alloc_percpu(struct disk_stats);
907 if (!bdev->bd_stats) {
908 iput(inode);
909 return NULL;
910 }
22ae8ce8
CH
911 return bdev;
912}
1da177e4 913
22ae8ce8
CH
914void bdev_add(struct block_device *bdev, dev_t dev)
915{
916 bdev->bd_dev = dev;
917 bdev->bd_inode->i_rdev = dev;
918 bdev->bd_inode->i_ino = dev;
919 insert_inode_hash(bdev->bd_inode);
920}
1da177e4 921
22ae8ce8
CH
922static struct block_device *bdget(dev_t dev)
923{
924 struct inode *inode;
925
926 inode = ilookup(blockdev_superblock, dev);
927 if (!inode)
928 return NULL;
929 return &BDEV_I(inode)->bdev;
1da177e4
LT
930}
931
dddac6a7
AJ
932/**
933 * bdgrab -- Grab a reference to an already referenced block device
934 * @bdev: Block device to grab a reference to.
3a4174e6
CH
935 *
936 * Returns the block_device with an additional reference when successful,
937 * or NULL if the inode is already beeing freed.
dddac6a7
AJ
938 */
939struct block_device *bdgrab(struct block_device *bdev)
940{
3a4174e6
CH
941 if (!igrab(bdev->bd_inode))
942 return NULL;
dddac6a7
AJ
943 return bdev;
944}
c1681bf8 945EXPORT_SYMBOL(bdgrab);
dddac6a7 946
1da177e4
LT
947long nr_blockdev_pages(void)
948{
1008fe6d 949 struct inode *inode;
1da177e4 950 long ret = 0;
1008fe6d
CH
951
952 spin_lock(&blockdev_superblock->s_inode_list_lock);
953 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list)
954 ret += inode->i_mapping->nrpages;
955 spin_unlock(&blockdev_superblock->s_inode_list_lock);
956
1da177e4
LT
957 return ret;
958}
959
960void bdput(struct block_device *bdev)
961{
962 iput(bdev->bd_inode);
963}
1da177e4
LT
964EXPORT_SYMBOL(bdput);
965
1a3cbbc5
TH
966/**
967 * bd_may_claim - test whether a block device can be claimed
968 * @bdev: block device of interest
969 * @whole: whole block device containing @bdev, may equal @bdev
970 * @holder: holder trying to claim @bdev
971 *
25985edc 972 * Test whether @bdev can be claimed by @holder.
1a3cbbc5
TH
973 *
974 * CONTEXT:
975 * spin_lock(&bdev_lock).
976 *
977 * RETURNS:
978 * %true if @bdev can be claimed, %false otherwise.
979 */
980static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
981 void *holder)
1da177e4 982{
1da177e4 983 if (bdev->bd_holder == holder)
1a3cbbc5 984 return true; /* already a holder */
1da177e4 985 else if (bdev->bd_holder != NULL)
1a3cbbc5 986 return false; /* held by someone else */
bcc7f5b4 987 else if (whole == bdev)
1a3cbbc5 988 return true; /* is a whole device which isn't held */
1da177e4 989
e525fd89 990 else if (whole->bd_holder == bd_may_claim)
1a3cbbc5
TH
991 return true; /* is a partition of a device that is being partitioned */
992 else if (whole->bd_holder != NULL)
993 return false; /* is a partition of a held device */
1da177e4 994 else
1a3cbbc5
TH
995 return true; /* is a partition of an un-held device */
996}
997
6b4517a7 998/**
58e46ed9 999 * bd_prepare_to_claim - claim a block device
6b4517a7 1000 * @bdev: block device of interest
6b4517a7
TH
1001 * @holder: holder trying to claim @bdev
1002 *
58e46ed9
CH
1003 * Claim @bdev. This function fails if @bdev is already claimed by another
1004 * holder and waits if another claiming is in progress. return, the caller
1005 * has ownership of bd_claiming and bd_holder[s].
6b4517a7
TH
1006 *
1007 * RETURNS:
1008 * 0 if @bdev can be claimed, -EBUSY otherwise.
1009 */
37c3fc9a 1010int bd_prepare_to_claim(struct block_device *bdev, void *holder)
6b4517a7 1011{
37c3fc9a
CH
1012 struct block_device *whole = bdev_whole(bdev);
1013
1014 if (WARN_ON_ONCE(!holder))
1015 return -EINVAL;
6b4517a7 1016retry:
58e46ed9 1017 spin_lock(&bdev_lock);
6b4517a7 1018 /* if someone else claimed, fail */
58e46ed9
CH
1019 if (!bd_may_claim(bdev, whole, holder)) {
1020 spin_unlock(&bdev_lock);
6b4517a7 1021 return -EBUSY;
58e46ed9 1022 }
6b4517a7 1023
e75aa858
TH
1024 /* if claiming is already in progress, wait for it to finish */
1025 if (whole->bd_claiming) {
6b4517a7
TH
1026 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1027 DEFINE_WAIT(wait);
1028
1029 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1030 spin_unlock(&bdev_lock);
1031 schedule();
1032 finish_wait(wq, &wait);
6b4517a7
TH
1033 goto retry;
1034 }
1035
1036 /* yay, all mine */
58e46ed9
CH
1037 whole->bd_claiming = holder;
1038 spin_unlock(&bdev_lock);
6b4517a7
TH
1039 return 0;
1040}
ecbe6bc0 1041EXPORT_SYMBOL_GPL(bd_prepare_to_claim); /* only for the loop driver */
6b4517a7 1042
89e524c0
JK
1043static void bd_clear_claiming(struct block_device *whole, void *holder)
1044{
1045 lockdep_assert_held(&bdev_lock);
1046 /* tell others that we're done */
1047 BUG_ON(whole->bd_claiming != holder);
1048 whole->bd_claiming = NULL;
1049 wake_up_bit(&whole->bd_claiming, 0);
1050}
1051
1052/**
1053 * bd_finish_claiming - finish claiming of a block device
1054 * @bdev: block device of interest
89e524c0
JK
1055 * @holder: holder that has claimed @bdev
1056 *
1057 * Finish exclusive open of a block device. Mark the device as exlusively
1058 * open by the holder and wake up all waiters for exclusive open to finish.
1059 */
37c3fc9a 1060static void bd_finish_claiming(struct block_device *bdev, void *holder)
89e524c0 1061{
37c3fc9a
CH
1062 struct block_device *whole = bdev_whole(bdev);
1063
89e524c0
JK
1064 spin_lock(&bdev_lock);
1065 BUG_ON(!bd_may_claim(bdev, whole, holder));
1066 /*
1067 * Note that for a whole device bd_holders will be incremented twice,
1068 * and bd_holder will be set to bd_may_claim before being set to holder
1069 */
1070 whole->bd_holders++;
1071 whole->bd_holder = bd_may_claim;
1072 bdev->bd_holders++;
1073 bdev->bd_holder = holder;
1074 bd_clear_claiming(whole, holder);
1075 spin_unlock(&bdev_lock);
1076}
89e524c0
JK
1077
1078/**
1079 * bd_abort_claiming - abort claiming of a block device
1080 * @bdev: block device of interest
89e524c0
JK
1081 * @holder: holder that has claimed @bdev
1082 *
1083 * Abort claiming of a block device when the exclusive open failed. This can be
1084 * also used when exclusive open is not actually desired and we just needed
1085 * to block other exclusive openers for a while.
1086 */
37c3fc9a 1087void bd_abort_claiming(struct block_device *bdev, void *holder)
89e524c0
JK
1088{
1089 spin_lock(&bdev_lock);
37c3fc9a 1090 bd_clear_claiming(bdev_whole(bdev), holder);
89e524c0
JK
1091 spin_unlock(&bdev_lock);
1092}
1093EXPORT_SYMBOL(bd_abort_claiming);
6b4517a7 1094
641dc636 1095#ifdef CONFIG_SYSFS
49731baa
TH
1096struct bd_holder_disk {
1097 struct list_head list;
1098 struct gendisk *disk;
1099 int refcnt;
1100};
1101
1102static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1103 struct gendisk *disk)
1104{
1105 struct bd_holder_disk *holder;
1106
1107 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1108 if (holder->disk == disk)
1109 return holder;
1110 return NULL;
1111}
1112
4d7dd8fd 1113static int add_symlink(struct kobject *from, struct kobject *to)
641dc636 1114{
4d7dd8fd 1115 return sysfs_create_link(from, to, kobject_name(to));
641dc636
JN
1116}
1117
1118static void del_symlink(struct kobject *from, struct kobject *to)
1119{
641dc636
JN
1120 sysfs_remove_link(from, kobject_name(to));
1121}
1122
df6c0cd9 1123/**
e09b457b
TH
1124 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1125 * @bdev: the claimed slave bdev
1126 * @disk: the holding disk
df6c0cd9 1127 *
49731baa
TH
1128 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1129 *
e09b457b 1130 * This functions creates the following sysfs symlinks.
641dc636 1131 *
e09b457b
TH
1132 * - from "slaves" directory of the holder @disk to the claimed @bdev
1133 * - from "holders" directory of the @bdev to the holder @disk
641dc636 1134 *
e09b457b
TH
1135 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1136 * passed to bd_link_disk_holder(), then:
641dc636 1137 *
e09b457b
TH
1138 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1139 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
641dc636 1140 *
e09b457b
TH
1141 * The caller must have claimed @bdev before calling this function and
1142 * ensure that both @bdev and @disk are valid during the creation and
1143 * lifetime of these symlinks.
641dc636 1144 *
e09b457b
TH
1145 * CONTEXT:
1146 * Might sleep.
641dc636 1147 *
e09b457b
TH
1148 * RETURNS:
1149 * 0 on success, -errno on failure.
641dc636 1150 */
e09b457b 1151int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 1152{
49731baa 1153 struct bd_holder_disk *holder;
e09b457b 1154 int ret = 0;
641dc636 1155
a8698707 1156 mutex_lock(&bdev->bd_disk->open_mutex);
df6c0cd9 1157
49731baa 1158 WARN_ON_ONCE(!bdev->bd_holder);
4e91672c 1159
e09b457b 1160 /* FIXME: remove the following once add_disk() handles errors */
1bdd5ae0 1161 if (WARN_ON(!disk->slave_dir || !bdev->bd_holder_dir))
e09b457b 1162 goto out_unlock;
4e91672c 1163
49731baa
TH
1164 holder = bd_find_holder_disk(bdev, disk);
1165 if (holder) {
1166 holder->refcnt++;
e09b457b 1167 goto out_unlock;
49731baa 1168 }
641dc636 1169
49731baa
TH
1170 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1171 if (!holder) {
1172 ret = -ENOMEM;
e09b457b
TH
1173 goto out_unlock;
1174 }
641dc636 1175
49731baa
TH
1176 INIT_LIST_HEAD(&holder->list);
1177 holder->disk = disk;
1178 holder->refcnt = 1;
1179
8d65269f 1180 ret = add_symlink(disk->slave_dir, bdev_kobj(bdev));
49731baa
TH
1181 if (ret)
1182 goto out_free;
1183
1bdd5ae0 1184 ret = add_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
49731baa
TH
1185 if (ret)
1186 goto out_del;
e7407d16
TH
1187 /*
1188 * bdev could be deleted beneath us which would implicitly destroy
1189 * the holder directory. Hold on to it.
1190 */
1bdd5ae0 1191 kobject_get(bdev->bd_holder_dir);
49731baa
TH
1192
1193 list_add(&holder->list, &bdev->bd_holder_disks);
1194 goto out_unlock;
1195
1196out_del:
8d65269f 1197 del_symlink(disk->slave_dir, bdev_kobj(bdev));
49731baa
TH
1198out_free:
1199 kfree(holder);
e09b457b 1200out_unlock:
a8698707 1201 mutex_unlock(&bdev->bd_disk->open_mutex);
e09b457b 1202 return ret;
641dc636 1203}
e09b457b 1204EXPORT_SYMBOL_GPL(bd_link_disk_holder);
641dc636 1205
49731baa
TH
1206/**
1207 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1208 * @bdev: the calimed slave bdev
1209 * @disk: the holding disk
1210 *
1211 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1212 *
1213 * CONTEXT:
1214 * Might sleep.
1215 */
1216void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 1217{
49731baa 1218 struct bd_holder_disk *holder;
641dc636 1219
a8698707 1220 mutex_lock(&bdev->bd_disk->open_mutex);
641dc636 1221
49731baa
TH
1222 holder = bd_find_holder_disk(bdev, disk);
1223
1224 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
8d65269f 1225 del_symlink(disk->slave_dir, bdev_kobj(bdev));
1bdd5ae0
CH
1226 del_symlink(bdev->bd_holder_dir, &disk_to_dev(disk)->kobj);
1227 kobject_put(bdev->bd_holder_dir);
49731baa
TH
1228 list_del_init(&holder->list);
1229 kfree(holder);
1230 }
1231
a8698707 1232 mutex_unlock(&bdev->bd_disk->open_mutex);
1da177e4 1233}
49731baa 1234EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
641dc636 1235#endif
1da177e4 1236
c8276b95
CH
1237static void blkdev_flush_mapping(struct block_device *bdev)
1238{
1239 WARN_ON_ONCE(bdev->bd_holders);
1240 sync_blockdev(bdev);
1241 kill_bdev(bdev);
1242 bdev_write_inode(bdev);
1243}
37be4124 1244
142fe8f4 1245int bdev_disk_changed(struct block_device *bdev, bool invalidate)
a1548b67 1246{
142fe8f4 1247 struct gendisk *disk = bdev->bd_disk;
d173b65a 1248 int ret = 0;
a1548b67 1249
a8698707 1250 lockdep_assert_held(&disk->open_mutex);
f0b870df 1251
bc6a3851
GM
1252 if (!(disk->flags & GENHD_FL_UP))
1253 return -ENXIO;
1254
a1548b67 1255rescan:
d3c4a43d
CH
1256 if (bdev->bd_part_count)
1257 return -EBUSY;
1258 sync_blockdev(bdev);
1259 invalidate_bdev(bdev);
1260 blk_drop_partitions(disk);
a1548b67 1261
51167840
CC
1262 clear_bit(GD_NEED_PART_SCAN, &disk->state);
1263
d981cb5b
CH
1264 /*
1265 * Historically we only set the capacity to zero for devices that
1266 * support partitions (independ of actually having partitions created).
1267 * Doing that is rather inconsistent, but changing it broke legacy
1268 * udisks polling for legacy ide-cdrom devices. Use the crude check
1269 * below to get the sane behavior for most device while not breaking
1270 * userspace for this particular setup.
1271 */
1272 if (invalidate) {
1273 if (disk_part_scan_enabled(disk) ||
1274 !(disk->flags & GENHD_FL_REMOVABLE))
1275 set_capacity(disk, 0);
d981cb5b 1276 }
a1548b67 1277
142fe8f4
CH
1278 if (get_capacity(disk)) {
1279 ret = blk_add_partitions(disk, bdev);
1280 if (ret == -EAGAIN)
1281 goto rescan;
490547ca 1282 } else if (invalidate) {
a1548b67
CH
1283 /*
1284 * Tell userspace that the media / partition table may have
1285 * changed.
1286 */
1287 kobject_uevent(&disk_to_dev(disk)->kobj, KOBJ_CHANGE);
a1548b67
CH
1288 }
1289
a1548b67
CH
1290 return ret;
1291}
f0b870df 1292/*
3d742d4b 1293 * Only exported for loop and dasd for historic reasons. Don't use in new
f0b870df
CH
1294 * code!
1295 */
1296EXPORT_SYMBOL_GPL(bdev_disk_changed);
a1548b67 1297
362529d9 1298static int blkdev_get_whole(struct block_device *bdev, fmode_t mode)
1da177e4 1299{
22ae8ce8 1300 struct gendisk *disk = bdev->bd_disk;
cb8432d6 1301 int ret = 0;
5b642d8b 1302
362529d9
CH
1303 if (disk->fops->open) {
1304 ret = disk->fops->open(bdev, mode);
1305 if (ret) {
1306 /* avoid ghost partitions on a removed medium */
1307 if (ret == -ENOMEDIUM &&
1308 test_bit(GD_NEED_PART_SCAN, &disk->state))
1309 bdev_disk_changed(bdev, true);
1310 return ret;
1311 }
1312 }
6c60ff04 1313
1da177e4 1314 if (!bdev->bd_openers) {
362529d9
CH
1315 set_init_blocksize(bdev);
1316 if (bdev->bd_bdi == &noop_backing_dev_info)
1317 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1318 }
1319 if (test_bit(GD_NEED_PART_SCAN, &disk->state))
1320 bdev_disk_changed(bdev, false);
1321 bdev->bd_openers++;
1322 return 0;;
1323}
7e69723f 1324
c8276b95
CH
1325static void blkdev_put_whole(struct block_device *bdev, fmode_t mode)
1326{
1327 if (!--bdev->bd_openers)
1328 blkdev_flush_mapping(bdev);
1329 if (bdev->bd_disk->fops->release)
1330 bdev->bd_disk->fops->release(bdev->bd_disk, mode);
1331}
1332
362529d9
CH
1333static int blkdev_get_part(struct block_device *part, fmode_t mode)
1334{
1335 struct gendisk *disk = part->bd_disk;
1336 struct block_device *whole;
1337 int ret;
7e69723f 1338
362529d9
CH
1339 if (part->bd_openers)
1340 goto done;
5a023cdb 1341
362529d9 1342 whole = bdgrab(disk->part0);
362529d9 1343 ret = blkdev_get_whole(whole, mode);
a8698707 1344 if (ret)
362529d9 1345 goto out_put_whole;
5b56b6ed 1346
362529d9
CH
1347 ret = -ENXIO;
1348 if (!bdev_nr_sectors(part))
1349 goto out_blkdev_put;
03e26279 1350
e54069ac 1351 whole->bd_part_count++;
362529d9
CH
1352 set_init_blocksize(part);
1353 if (part->bd_bdi == &noop_backing_dev_info)
1354 part->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1355done:
1356 part->bd_openers++;
1da177e4 1357 return 0;
362529d9
CH
1358
1359out_blkdev_put:
c8276b95 1360 blkdev_put_whole(whole, mode);
362529d9
CH
1361out_put_whole:
1362 bdput(whole);
1363 return ret;
1da177e4
LT
1364}
1365
c8276b95
CH
1366static void blkdev_put_part(struct block_device *part, fmode_t mode)
1367{
1368 struct block_device *whole = bdev_whole(part);
1369
1370 if (--part->bd_openers)
1371 return;
1372 blkdev_flush_mapping(part);
1373 whole->bd_part_count--;
1374 blkdev_put_whole(whole, mode);
1375 bdput(whole);
1376}
1377
22ae8ce8
CH
1378struct block_device *blkdev_get_no_open(dev_t dev)
1379{
1380 struct block_device *bdev;
1381 struct gendisk *disk;
1382
22ae8ce8
CH
1383 bdev = bdget(dev);
1384 if (!bdev) {
22ae8ce8 1385 blk_request_module(dev);
22ae8ce8
CH
1386 bdev = bdget(dev);
1387 if (!bdev)
6c60ff04 1388 return NULL;
22ae8ce8
CH
1389 }
1390
1391 disk = bdev->bd_disk;
1392 if (!kobject_get_unless_zero(&disk_to_dev(disk)->kobj))
1393 goto bdput;
1394 if ((disk->flags & (GENHD_FL_UP | GENHD_FL_HIDDEN)) != GENHD_FL_UP)
1395 goto put_disk;
1396 if (!try_module_get(bdev->bd_disk->fops->owner))
1397 goto put_disk;
22ae8ce8
CH
1398 return bdev;
1399put_disk:
1400 put_disk(disk);
1401bdput:
1402 bdput(bdev);
22ae8ce8
CH
1403 return NULL;
1404}
1405
1406void blkdev_put_no_open(struct block_device *bdev)
1407{
1408 module_put(bdev->bd_disk->fops->owner);
1409 put_disk(bdev->bd_disk);
1410 bdput(bdev);
1411}
1412
d4d77629 1413/**
4e7b5671
CH
1414 * blkdev_get_by_dev - open a block device by device number
1415 * @dev: device number of block device to open
d4d77629
TH
1416 * @mode: FMODE_* mask
1417 * @holder: exclusive holder identifier
1418 *
4e7b5671
CH
1419 * Open the block device described by device number @dev. If @mode includes
1420 * %FMODE_EXCL, the block device is opened with exclusive access. Specifying
1421 * %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may nest for
1422 * the same @holder.
d4d77629 1423 *
4e7b5671
CH
1424 * Use this interface ONLY if you really do not have anything better - i.e. when
1425 * you are behind a truly sucky interface and all you are given is a device
1426 * number. Everything else should use blkdev_get_by_path().
d4d77629
TH
1427 *
1428 * CONTEXT:
1429 * Might sleep.
1430 *
1431 * RETURNS:
4e7b5671 1432 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
d4d77629 1433 */
4e7b5671 1434struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1da177e4 1435{
5b56b6ed 1436 bool unblock_events = true;
4e7b5671 1437 struct block_device *bdev;
5b56b6ed 1438 struct gendisk *disk;
5b56b6ed 1439 int ret;
e525fd89 1440
7918f0f6 1441 ret = devcgroup_check_permission(DEVCG_DEV_BLOCK,
4e7b5671 1442 MAJOR(dev), MINOR(dev),
7918f0f6
CH
1443 ((mode & FMODE_READ) ? DEVCG_ACC_READ : 0) |
1444 ((mode & FMODE_WRITE) ? DEVCG_ACC_WRITE : 0));
e5c7fb40 1445 if (ret)
4e7b5671
CH
1446 return ERR_PTR(ret);
1447
22ae8ce8
CH
1448 bdev = blkdev_get_no_open(dev);
1449 if (!bdev)
1450 return ERR_PTR(-ENXIO);
1451 disk = bdev->bd_disk;
e5c7fb40 1452
5b56b6ed 1453 if (mode & FMODE_EXCL) {
37c3fc9a 1454 ret = bd_prepare_to_claim(bdev, holder);
5b56b6ed 1455 if (ret)
37c3fc9a 1456 goto put_blkdev;
5b56b6ed
CH
1457 }
1458
1459 disk_block_events(disk);
1460
a8698707 1461 mutex_lock(&disk->open_mutex);
362529d9
CH
1462 ret = -ENXIO;
1463 if (!(disk->flags & GENHD_FL_UP))
1464 goto abort_claiming;
1465 if (bdev_is_partition(bdev))
1466 ret = blkdev_get_part(bdev, mode);
1467 else
1468 ret = blkdev_get_whole(bdev, mode);
22ae8ce8
CH
1469 if (ret)
1470 goto abort_claiming;
1471 if (mode & FMODE_EXCL) {
37c3fc9a 1472 bd_finish_claiming(bdev, holder);
5b56b6ed
CH
1473
1474 /*
1475 * Block event polling for write claims if requested. Any write
1476 * holder makes the write_holder state stick until all are
1477 * released. This is good enough and tracking individual
1478 * writeable reference is too fragile given the way @mode is
1479 * used in blkdev_get/put().
1480 */
1481 if ((mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1482 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1483 bdev->bd_write_holder = true;
1484 unblock_events = false;
1485 }
1486 }
a8698707 1487 mutex_unlock(&disk->open_mutex);
5b56b6ed
CH
1488
1489 if (unblock_events)
1490 disk_unblock_events(disk);
22ae8ce8 1491 return bdev;
5b56b6ed 1492
22ae8ce8
CH
1493abort_claiming:
1494 if (mode & FMODE_EXCL)
37c3fc9a 1495 bd_abort_claiming(bdev, holder);
a8698707 1496 mutex_unlock(&disk->open_mutex);
22ae8ce8 1497 disk_unblock_events(disk);
22ae8ce8
CH
1498put_blkdev:
1499 blkdev_put_no_open(bdev);
22ae8ce8 1500 return ERR_PTR(ret);
37be4124 1501}
4e7b5671 1502EXPORT_SYMBOL(blkdev_get_by_dev);
1da177e4 1503
d4d77629
TH
1504/**
1505 * blkdev_get_by_path - open a block device by name
1506 * @path: path to the block device to open
1507 * @mode: FMODE_* mask
1508 * @holder: exclusive holder identifier
1509 *
4e7b5671
CH
1510 * Open the block device described by the device file at @path. If @mode
1511 * includes %FMODE_EXCL, the block device is opened with exclusive access.
1512 * Specifying %FMODE_EXCL with a %NULL @holder is invalid. Exclusive opens may
1513 * nest for the same @holder.
d4d77629
TH
1514 *
1515 * CONTEXT:
1516 * Might sleep.
1517 *
1518 * RETURNS:
4e7b5671 1519 * Reference to the block_device on success, ERR_PTR(-errno) on failure.
d4d77629
TH
1520 */
1521struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1522 void *holder)
1523{
1524 struct block_device *bdev;
4e7b5671
CH
1525 dev_t dev;
1526 int error;
d4d77629 1527
4e7b5671
CH
1528 error = lookup_bdev(path, &dev);
1529 if (error)
1530 return ERR_PTR(error);
d4d77629 1531
4e7b5671
CH
1532 bdev = blkdev_get_by_dev(dev, mode, holder);
1533 if (!IS_ERR(bdev) && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
e51900f7
CE
1534 blkdev_put(bdev, mode);
1535 return ERR_PTR(-EACCES);
1536 }
1537
d4d77629
TH
1538 return bdev;
1539}
1540EXPORT_SYMBOL(blkdev_get_by_path);
1541
1da177e4
LT
1542static int blkdev_open(struct inode * inode, struct file * filp)
1543{
1544 struct block_device *bdev;
1da177e4
LT
1545
1546 /*
1547 * Preserve backwards compatibility and allow large file access
1548 * even if userspace doesn't ask for it explicitly. Some mkfs
1549 * binary needs it. We might want to drop this workaround
1550 * during an unstable branch.
1551 */
1552 filp->f_flags |= O_LARGEFILE;
1553
a304f074 1554 filp->f_mode |= FMODE_NOWAIT | FMODE_BUF_RASYNC;
c35fc7a5 1555
572c4892
AV
1556 if (filp->f_flags & O_NDELAY)
1557 filp->f_mode |= FMODE_NDELAY;
1558 if (filp->f_flags & O_EXCL)
1559 filp->f_mode |= FMODE_EXCL;
1560 if ((filp->f_flags & O_ACCMODE) == 3)
1561 filp->f_mode |= FMODE_WRITE_IOCTL;
1562
4e7b5671
CH
1563 bdev = blkdev_get_by_dev(inode->i_rdev, filp->f_mode, filp);
1564 if (IS_ERR(bdev))
1565 return PTR_ERR(bdev);
572c4892 1566 filp->f_mapping = bdev->bd_inode->i_mapping;
5660e13d 1567 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
4e7b5671 1568 return 0;
1da177e4
LT
1569}
1570
4385bab1 1571void blkdev_put(struct block_device *bdev, fmode_t mode)
37be4124 1572{
5b56b6ed
CH
1573 struct gendisk *disk = bdev->bd_disk;
1574
210a6d75
CH
1575 /*
1576 * Sync early if it looks like we're the last one. If someone else
1577 * opens the block device between now and the decrement of bd_openers
1578 * then we did a sync that we didn't need to, but that's not the end
1579 * of the world and we want to avoid long (could be several minute)
1580 * syncs while holding the mutex.
1581 */
1582 if (bdev->bd_openers == 1)
1583 sync_blockdev(bdev);
1584
a8698707 1585 mutex_lock(&disk->open_mutex);
e525fd89 1586 if (mode & FMODE_EXCL) {
a954ea81 1587 struct block_device *whole = bdev_whole(bdev);
6a027eff
TH
1588 bool bdev_free;
1589
1590 /*
1591 * Release a claim on the device. The holder fields
a8698707 1592 * are protected with bdev_lock. open_mutex is to
6a027eff
TH
1593 * synchronize disk_holder unlinking.
1594 */
6a027eff
TH
1595 spin_lock(&bdev_lock);
1596
1597 WARN_ON_ONCE(--bdev->bd_holders < 0);
a954ea81 1598 WARN_ON_ONCE(--whole->bd_holders < 0);
6a027eff 1599
6a027eff
TH
1600 if ((bdev_free = !bdev->bd_holders))
1601 bdev->bd_holder = NULL;
a954ea81
CH
1602 if (!whole->bd_holders)
1603 whole->bd_holder = NULL;
6a027eff
TH
1604
1605 spin_unlock(&bdev_lock);
1606
77ea887e
TH
1607 /*
1608 * If this was the last claim, remove holder link and
1609 * unblock evpoll if it was a write holder.
1610 */
85ef06d1 1611 if (bdev_free && bdev->bd_write_holder) {
5b56b6ed 1612 disk_unblock_events(disk);
85ef06d1 1613 bdev->bd_write_holder = false;
77ea887e 1614 }
6936217c 1615 }
77ea887e 1616
85ef06d1
TH
1617 /*
1618 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1619 * event. This is to ensure detection of media removal commanded
1620 * from userland - e.g. eject(1).
1621 */
5b56b6ed 1622 disk_flush_events(disk, DISK_EVENT_MEDIA_CHANGE);
85ef06d1 1623
c8276b95
CH
1624 if (bdev_is_partition(bdev))
1625 blkdev_put_part(bdev, mode);
1626 else
1627 blkdev_put_whole(bdev, mode);
a8698707
CH
1628 mutex_unlock(&disk->open_mutex);
1629
22ae8ce8 1630 blkdev_put_no_open(bdev);
37be4124 1631}
2e7b651d
PZ
1632EXPORT_SYMBOL(blkdev_put);
1633
1da177e4
LT
1634static int blkdev_close(struct inode * inode, struct file * filp)
1635{
4ebb16ca 1636 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
4385bab1
AV
1637 blkdev_put(bdev, filp->f_mode);
1638 return 0;
1da177e4
LT
1639}
1640
bb93e3a5 1641static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1da177e4 1642{
4ebb16ca 1643 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
56b26add 1644 fmode_t mode = file->f_mode;
fd4ce1ac
CH
1645
1646 /*
1647 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1648 * to updated it before every ioctl.
1649 */
56b26add 1650 if (file->f_flags & O_NDELAY)
fd4ce1ac
CH
1651 mode |= FMODE_NDELAY;
1652 else
1653 mode &= ~FMODE_NDELAY;
1654
56b26add 1655 return blkdev_ioctl(bdev, mode, cmd, arg);
1da177e4
LT
1656}
1657
eef99380
CH
1658/*
1659 * Write data to the block device. Only intended for the block device itself
1660 * and the raw driver which basically is a fake block device.
1661 *
1662 * Does not take i_mutex for the write and thus is not for general purpose
1663 * use.
1664 */
1456c0a8 1665ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
eef99380
CH
1666{
1667 struct file *file = iocb->ki_filp;
4ebb16ca 1668 struct inode *bd_inode = bdev_file_inode(file);
7ec7b94a 1669 loff_t size = i_size_read(bd_inode);
53362a05 1670 struct blk_plug plug;
cf7b39a0 1671 size_t shorted = 0;
eef99380 1672 ssize_t ret;
5f380c7f 1673
7ec7b94a
AV
1674 if (bdev_read_only(I_BDEV(bd_inode)))
1675 return -EPERM;
5f380c7f 1676
bb3247a3 1677 if (IS_SWAPFILE(bd_inode) && !is_hibernate_resume_dev(bd_inode->i_rdev))
dc617f29
DW
1678 return -ETXTBSY;
1679
7ec7b94a 1680 if (!iov_iter_count(from))
5f380c7f
AV
1681 return 0;
1682
7ec7b94a
AV
1683 if (iocb->ki_pos >= size)
1684 return -ENOSPC;
1685
c35fc7a5
CH
1686 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1687 return -EOPNOTSUPP;
1688
cf7b39a0 1689 size -= iocb->ki_pos;
1690 if (iov_iter_count(from) > size) {
1691 shorted = iov_iter_count(from) - size;
1692 iov_iter_truncate(from, size);
1693 }
eef99380 1694
53362a05 1695 blk_start_plug(&plug);
1456c0a8 1696 ret = __generic_file_write_iter(iocb, from);
e2592217
CH
1697 if (ret > 0)
1698 ret = generic_write_sync(iocb, ret);
cf7b39a0 1699 iov_iter_reexpand(from, iov_iter_count(from) + shorted);
53362a05 1700 blk_finish_plug(&plug);
eef99380
CH
1701 return ret;
1702}
1456c0a8 1703EXPORT_SYMBOL_GPL(blkdev_write_iter);
eef99380 1704
b2de525f 1705ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
684c9aae
LT
1706{
1707 struct file *file = iocb->ki_filp;
4ebb16ca 1708 struct inode *bd_inode = bdev_file_inode(file);
684c9aae 1709 loff_t size = i_size_read(bd_inode);
a886038b 1710 loff_t pos = iocb->ki_pos;
cf7b39a0 1711 size_t shorted = 0;
1712 ssize_t ret;
684c9aae
LT
1713
1714 if (pos >= size)
1715 return 0;
1716
1717 size -= pos;
cf7b39a0 1718 if (iov_iter_count(to) > size) {
1719 shorted = iov_iter_count(to) - size;
1720 iov_iter_truncate(to, size);
1721 }
1722
1723 ret = generic_file_read_iter(iocb, to);
1724 iov_iter_reexpand(to, iov_iter_count(to) + shorted);
1725 return ret;
684c9aae 1726}
b2de525f 1727EXPORT_SYMBOL_GPL(blkdev_read_iter);
684c9aae 1728
87d8fe1e
TT
1729/*
1730 * Try to release a page associated with block device when the system
1731 * is under memory pressure.
1732 */
1733static int blkdev_releasepage(struct page *page, gfp_t wait)
1734{
1735 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1736
1737 if (super && super->s_op->bdev_try_to_free_page)
1738 return super->s_op->bdev_try_to_free_page(super, page, wait);
1739
1740 return try_to_free_buffers(page);
1741}
1742
7f6d5b52
RZ
1743static int blkdev_writepages(struct address_space *mapping,
1744 struct writeback_control *wbc)
1745{
7f6d5b52
RZ
1746 return generic_writepages(mapping, wbc);
1747}
1748
4c54ac62 1749static const struct address_space_operations def_blk_aops = {
1da177e4 1750 .readpage = blkdev_readpage,
d4388340 1751 .readahead = blkdev_readahead,
1da177e4 1752 .writepage = blkdev_writepage,
6272b5a5
NP
1753 .write_begin = blkdev_write_begin,
1754 .write_end = blkdev_write_end,
7f6d5b52 1755 .writepages = blkdev_writepages,
87d8fe1e 1756 .releasepage = blkdev_releasepage,
1da177e4 1757 .direct_IO = blkdev_direct_IO,
88dbcbb3 1758 .migratepage = buffer_migrate_page_norefs,
b4597226 1759 .is_dirty_writeback = buffer_check_dirty_writeback,
1da177e4
LT
1760};
1761
25f4c414
DW
1762#define BLKDEV_FALLOC_FL_SUPPORTED \
1763 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1764 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1765
1766static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1767 loff_t len)
1768{
1769 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
25f4c414
DW
1770 loff_t end = start + len - 1;
1771 loff_t isize;
1772 int error;
1773
1774 /* Fail if we don't recognize the flags. */
1775 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1776 return -EOPNOTSUPP;
1777
1778 /* Don't go off the end of the device. */
1779 isize = i_size_read(bdev->bd_inode);
1780 if (start >= isize)
1781 return -EINVAL;
1782 if (end >= isize) {
1783 if (mode & FALLOC_FL_KEEP_SIZE) {
1784 len = isize - start;
1785 end = start + len - 1;
1786 } else
1787 return -EINVAL;
1788 }
1789
1790 /*
1791 * Don't allow IO that isn't aligned to logical block size.
1792 */
1793 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
1794 return -EINVAL;
1795
1796 /* Invalidate the page cache, including dirty pages. */
384d87ef
JK
1797 error = truncate_bdev_range(bdev, file->f_mode, start, end);
1798 if (error)
1799 return error;
25f4c414
DW
1800
1801 switch (mode) {
1802 case FALLOC_FL_ZERO_RANGE:
1803 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
1804 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
ee472d83 1805 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
25f4c414
DW
1806 break;
1807 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
34045129
CH
1808 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
1809 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
25f4c414
DW
1810 break;
1811 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
25f4c414
DW
1812 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
1813 GFP_KERNEL, 0);
1814 break;
1815 default:
1816 return -EOPNOTSUPP;
1817 }
1818 if (error)
1819 return error;
1820
1821 /*
767630c6
JK
1822 * Invalidate the page cache again; if someone wandered in and dirtied
1823 * a page, we just discard it - userspace has no way of knowing whether
1824 * the write happened before or after discard completing...
25f4c414 1825 */
767630c6 1826 return truncate_bdev_range(bdev, file->f_mode, start, end);
25f4c414
DW
1827}
1828
4b6f5d20 1829const struct file_operations def_blk_fops = {
1da177e4
LT
1830 .open = blkdev_open,
1831 .release = blkdev_close,
1832 .llseek = block_llseek,
a886038b 1833 .read_iter = blkdev_read_iter,
1456c0a8 1834 .write_iter = blkdev_write_iter,
eae83ce1 1835 .iopoll = blkdev_iopoll,
acc93d30 1836 .mmap = generic_file_mmap,
b1dd3b28 1837 .fsync = blkdev_fsync,
bb93e3a5 1838 .unlocked_ioctl = block_ioctl,
1da177e4
LT
1839#ifdef CONFIG_COMPAT
1840 .compat_ioctl = compat_blkdev_ioctl,
1841#endif
1e8b3332 1842 .splice_read = generic_file_splice_read,
8d020765 1843 .splice_write = iter_file_splice_write,
25f4c414 1844 .fallocate = blkdev_fallocate,
1da177e4
LT
1845};
1846
1da177e4
LT
1847/**
1848 * lookup_bdev - lookup a struct block_device by name
94e2959e 1849 * @pathname: special file representing the block device
875b2376 1850 * @dev: return value of the block device's dev_t
1da177e4 1851 *
57d1b536 1852 * Get a reference to the blockdevice at @pathname in the current
1da177e4
LT
1853 * namespace if possible and return it. Return ERR_PTR(error)
1854 * otherwise.
1855 */
4e7b5671 1856int lookup_bdev(const char *pathname, dev_t *dev)
1da177e4 1857{
1da177e4 1858 struct inode *inode;
421748ec 1859 struct path path;
1da177e4
LT
1860 int error;
1861
421748ec 1862 if (!pathname || !*pathname)
4e7b5671 1863 return -EINVAL;
1da177e4 1864
421748ec 1865 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1da177e4 1866 if (error)
4e7b5671 1867 return error;
1da177e4 1868
bb668734 1869 inode = d_backing_inode(path.dentry);
1da177e4
LT
1870 error = -ENOTBLK;
1871 if (!S_ISBLK(inode->i_mode))
4e7b5671 1872 goto out_path_put;
1da177e4 1873 error = -EACCES;
a2982cc9 1874 if (!may_open_dev(&path))
4e7b5671
CH
1875 goto out_path_put;
1876
1877 *dev = inode->i_rdev;
1878 error = 0;
1879out_path_put:
421748ec 1880 path_put(&path);
4e7b5671 1881 return error;
1da177e4 1882}
d5686b44 1883EXPORT_SYMBOL(lookup_bdev);
1da177e4 1884
93b270f7 1885int __invalidate_device(struct block_device *bdev, bool kill_dirty)
b71e8a4c
DH
1886{
1887 struct super_block *sb = get_super(bdev);
1888 int res = 0;
1889
1890 if (sb) {
1891 /*
1892 * no need to lock the super, get_super holds the
1893 * read mutex so the filesystem cannot go away
1894 * under us (->put_super runs with the write lock
1895 * hold).
1896 */
1897 shrink_dcache_sb(sb);
93b270f7 1898 res = invalidate_inodes(sb, kill_dirty);
b71e8a4c
DH
1899 drop_super(sb);
1900 }
f98393a6 1901 invalidate_bdev(bdev);
b71e8a4c
DH
1902 return res;
1903}
1904EXPORT_SYMBOL(__invalidate_device);
5c0d6b60
JK
1905
1906void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
1907{
1908 struct inode *inode, *old_inode = NULL;
1909
74278da9 1910 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
1911 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
1912 struct address_space *mapping = inode->i_mapping;
af309226 1913 struct block_device *bdev;
5c0d6b60
JK
1914
1915 spin_lock(&inode->i_lock);
1916 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
1917 mapping->nrpages == 0) {
1918 spin_unlock(&inode->i_lock);
1919 continue;
1920 }
1921 __iget(inode);
1922 spin_unlock(&inode->i_lock);
74278da9 1923 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
1924 /*
1925 * We hold a reference to 'inode' so it couldn't have been
1926 * removed from s_inodes list while we dropped the
74278da9 1927 * s_inode_list_lock We cannot iput the inode now as we can
5c0d6b60 1928 * be holding the last reference and we cannot iput it under
74278da9 1929 * s_inode_list_lock. So we keep the reference and iput it
5c0d6b60
JK
1930 * later.
1931 */
1932 iput(old_inode);
1933 old_inode = inode;
af309226 1934 bdev = I_BDEV(inode);
5c0d6b60 1935
a8698707 1936 mutex_lock(&bdev->bd_disk->open_mutex);
af309226
RV
1937 if (bdev->bd_openers)
1938 func(bdev, arg);
a8698707 1939 mutex_unlock(&bdev->bd_disk->open_mutex);
5c0d6b60 1940
74278da9 1941 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60 1942 }
74278da9 1943 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
1944 iput(old_inode);
1945}