]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - fs/block_dev.c
f2fs: move dir data flush to write checkpoint process
[thirdparty/kernel/stable.git] / fs / block_dev.c
CommitLineData
1da177e4
LT
1/*
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
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>
b0686260 21#include <linux/dax.h>
1da177e4 22#include <linux/buffer_head.h>
ff01bb48 23#include <linux/swap.h>
585d3bc0 24#include <linux/pagevec.h>
811d736f 25#include <linux/writeback.h>
1da177e4
LT
26#include <linux/mpage.h>
27#include <linux/mount.h>
28#include <linux/uio.h>
29#include <linux/namei.h>
1368c4f2 30#include <linux/log2.h>
ff01bb48 31#include <linux/cleancache.h>
c94c2acf 32#include <linux/dax.h>
acc93d30 33#include <linux/badblocks.h>
189ce2b9 34#include <linux/task_io_accounting_ops.h>
25f4c414 35#include <linux/falloc.h>
7c0f6ba6 36#include <linux/uaccess.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
2af3a815
TK
57void __vfs_msg(struct super_block *sb, const char *prefix, const char *fmt, ...)
58{
59 struct va_format vaf;
60 va_list args;
61
62 va_start(args, fmt);
63 vaf.fmt = fmt;
64 vaf.va = &args;
65 printk_ratelimited("%sVFS (%s): %pV\n", prefix, sb->s_id, &vaf);
66 va_end(args);
67}
68
dbd3ca50 69static void bdev_write_inode(struct block_device *bdev)
564f00f6 70{
dbd3ca50
VG
71 struct inode *inode = bdev->bd_inode;
72 int ret;
73
564f00f6
CH
74 spin_lock(&inode->i_lock);
75 while (inode->i_state & I_DIRTY) {
76 spin_unlock(&inode->i_lock);
dbd3ca50
VG
77 ret = write_inode_now(inode, true);
78 if (ret) {
79 char name[BDEVNAME_SIZE];
80 pr_warn_ratelimited("VFS: Dirty inode writeback failed "
81 "for block device %s (err=%d).\n",
82 bdevname(bdev, name), ret);
83 }
564f00f6
CH
84 spin_lock(&inode->i_lock);
85 }
86 spin_unlock(&inode->i_lock);
87}
88
f9a14399 89/* Kill _all_ buffers and pagecache , dirty or not.. */
ff01bb48 90void kill_bdev(struct block_device *bdev)
1da177e4 91{
ff01bb48
AV
92 struct address_space *mapping = bdev->bd_inode->i_mapping;
93
f9fe48be 94 if (mapping->nrpages == 0 && mapping->nrexceptional == 0)
f9a14399 95 return;
ff01bb48 96
f9a14399 97 invalidate_bh_lrus();
ff01bb48 98 truncate_inode_pages(mapping, 0);
1da177e4 99}
ff01bb48
AV
100EXPORT_SYMBOL(kill_bdev);
101
102/* Invalidate clean unused buffers and pagecache. */
103void invalidate_bdev(struct block_device *bdev)
104{
105 struct address_space *mapping = bdev->bd_inode->i_mapping;
106
a5f6a6a9
AR
107 if (mapping->nrpages) {
108 invalidate_bh_lrus();
109 lru_add_drain_all(); /* make sure all lru add caches are flushed */
110 invalidate_mapping_pages(mapping, 0, -1);
111 }
ff01bb48
AV
112 /* 99% of the time, we don't need to flush the cleancache on the bdev.
113 * But, for the strange corners, lets be cautious
114 */
3167760f 115 cleancache_invalidate_inode(mapping);
ff01bb48
AV
116}
117EXPORT_SYMBOL(invalidate_bdev);
1da177e4 118
0fb89795
JK
119static void set_init_blocksize(struct block_device *bdev)
120{
121 unsigned bsize = bdev_logical_block_size(bdev);
122 loff_t size = i_size_read(bdev->bd_inode);
123
124 while (bsize < PAGE_SIZE) {
125 if (size & bsize)
126 break;
127 bsize <<= 1;
128 }
129 bdev->bd_block_size = bsize;
130 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
131}
132
1da177e4
LT
133int set_blocksize(struct block_device *bdev, int size)
134{
135 /* Size must be a power of two, and between 512 and PAGE_SIZE */
1368c4f2 136 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
1da177e4
LT
137 return -EINVAL;
138
139 /* Size cannot be smaller than the size supported by the device */
e1defc4f 140 if (size < bdev_logical_block_size(bdev))
1da177e4
LT
141 return -EINVAL;
142
143 /* Don't change the size if it is same as current */
144 if (bdev->bd_block_size != size) {
145 sync_blockdev(bdev);
146 bdev->bd_block_size = size;
147 bdev->bd_inode->i_blkbits = blksize_bits(size);
148 kill_bdev(bdev);
149 }
150 return 0;
151}
152
153EXPORT_SYMBOL(set_blocksize);
154
155int sb_set_blocksize(struct super_block *sb, int size)
156{
1da177e4
LT
157 if (set_blocksize(sb->s_bdev, size))
158 return 0;
159 /* If we get here, we know size is power of two
160 * and it's value is between 512 and PAGE_SIZE */
161 sb->s_blocksize = size;
38885bd4 162 sb->s_blocksize_bits = blksize_bits(size);
1da177e4
LT
163 return sb->s_blocksize;
164}
165
166EXPORT_SYMBOL(sb_set_blocksize);
167
168int sb_min_blocksize(struct super_block *sb, int size)
169{
e1defc4f 170 int minsize = bdev_logical_block_size(sb->s_bdev);
1da177e4
LT
171 if (size < minsize)
172 size = minsize;
173 return sb_set_blocksize(sb, size);
174}
175
176EXPORT_SYMBOL(sb_min_blocksize);
177
178static int
179blkdev_get_block(struct inode *inode, sector_t iblock,
180 struct buffer_head *bh, int create)
181{
1da177e4
LT
182 bh->b_bdev = I_BDEV(inode);
183 bh->b_blocknr = iblock;
184 set_buffer_mapped(bh);
185 return 0;
186}
187
4ebb16ca
DW
188static struct inode *bdev_file_inode(struct file *file)
189{
190 return file->f_mapping->host;
191}
192
78250c02
JA
193static unsigned int dio_bio_write_op(struct kiocb *iocb)
194{
195 unsigned int op = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
196
197 /* avoid the need for a I/O completion work item */
198 if (iocb->ki_flags & IOCB_DSYNC)
199 op |= REQ_FUA;
200 return op;
201}
202
189ce2b9
CH
203#define DIO_INLINE_BIO_VECS 4
204
205static void blkdev_bio_end_io_simple(struct bio *bio)
206{
207 struct task_struct *waiter = bio->bi_private;
208
209 WRITE_ONCE(bio->bi_private, NULL);
210 wake_up_process(waiter);
211}
212
213static ssize_t
214__blkdev_direct_IO_simple(struct kiocb *iocb, struct iov_iter *iter,
215 int nr_pages)
216{
217 struct file *file = iocb->ki_filp;
218 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
72ecad22 219 struct bio_vec inline_vecs[DIO_INLINE_BIO_VECS], *vecs, *bvec;
189ce2b9
CH
220 loff_t pos = iocb->ki_pos;
221 bool should_dirty = false;
222 struct bio bio;
223 ssize_t ret;
224 blk_qc_t qc;
225 int i;
226
9a794fb9
JA
227 if ((pos | iov_iter_alignment(iter)) &
228 (bdev_logical_block_size(bdev) - 1))
189ce2b9
CH
229 return -EINVAL;
230
72ecad22
JA
231 if (nr_pages <= DIO_INLINE_BIO_VECS)
232 vecs = inline_vecs;
233 else {
234 vecs = kmalloc(nr_pages * sizeof(struct bio_vec), GFP_KERNEL);
235 if (!vecs)
236 return -ENOMEM;
237 }
238
3a83f467 239 bio_init(&bio, vecs, nr_pages);
74d46992 240 bio_set_dev(&bio, bdev);
4d1a4765 241 bio.bi_iter.bi_sector = pos >> 9;
45d06cf7 242 bio.bi_write_hint = iocb->ki_hint;
189ce2b9
CH
243 bio.bi_private = current;
244 bio.bi_end_io = blkdev_bio_end_io_simple;
245
246 ret = bio_iov_iter_get_pages(&bio, iter);
247 if (unlikely(ret))
cc5d7097 248 goto out;
189ce2b9
CH
249 ret = bio.bi_iter.bi_size;
250
251 if (iov_iter_rw(iter) == READ) {
78250c02 252 bio.bi_opf = REQ_OP_READ;
189ce2b9
CH
253 if (iter_is_iovec(iter))
254 should_dirty = true;
255 } else {
78250c02 256 bio.bi_opf = dio_bio_write_op(iocb);
189ce2b9
CH
257 task_io_account_write(ret);
258 }
259
260 qc = submit_bio(&bio);
261 for (;;) {
262 set_current_state(TASK_UNINTERRUPTIBLE);
263 if (!READ_ONCE(bio.bi_private))
264 break;
265 if (!(iocb->ki_flags & IOCB_HIPRI) ||
266 !blk_mq_poll(bdev_get_queue(bdev), qc))
267 io_schedule();
268 }
269 __set_current_state(TASK_RUNNING);
270
271 bio_for_each_segment_all(bvec, &bio, i) {
272 if (should_dirty && !PageCompound(bvec->bv_page))
273 set_page_dirty_lock(bvec->bv_page);
274 put_page(bvec->bv_page);
275 }
276
4e4cbee9 277 if (unlikely(bio.bi_status))
c6b1e36c 278 ret = blk_status_to_errno(bio.bi_status);
9ae3b3f5 279
cc5d7097
MW
280out:
281 if (vecs != inline_vecs)
282 kfree(vecs);
283
9ae3b3f5
JA
284 bio_uninit(&bio);
285
189ce2b9
CH
286 return ret;
287}
288
542ff7bf
CH
289struct blkdev_dio {
290 union {
291 struct kiocb *iocb;
292 struct task_struct *waiter;
293 };
294 size_t size;
295 atomic_t ref;
296 bool multi_bio : 1;
297 bool should_dirty : 1;
298 bool is_sync : 1;
299 struct bio bio;
300};
301
302static struct bio_set *blkdev_dio_pool __read_mostly;
303
304static 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
309 if (dio->multi_bio && !atomic_dec_and_test(&dio->ref)) {
4e4cbee9
CH
310 if (bio->bi_status && !dio->bio.bi_status)
311 dio->bio.bi_status = bio->bi_status;
542ff7bf
CH
312 } else {
313 if (!dio->is_sync) {
314 struct kiocb *iocb = dio->iocb;
4e4cbee9 315 ssize_t ret;
542ff7bf 316
4e4cbee9 317 if (likely(!dio->bio.bi_status)) {
542ff7bf
CH
318 ret = dio->size;
319 iocb->ki_pos += ret;
4e4cbee9
CH
320 } else {
321 ret = blk_status_to_errno(dio->bio.bi_status);
542ff7bf
CH
322 }
323
324 dio->iocb->ki_complete(iocb, ret, 0);
325 bio_put(&dio->bio);
326 } else {
327 struct task_struct *waiter = dio->waiter;
328
329 WRITE_ONCE(dio->waiter, NULL);
330 wake_up_process(waiter);
331 }
332 }
333
334 if (should_dirty) {
335 bio_check_pages_dirty(bio);
336 } else {
337 struct bio_vec *bvec;
338 int i;
339
340 bio_for_each_segment_all(bvec, bio, i)
341 put_page(bvec->bv_page);
342 bio_put(bio);
343 }
344}
345
b2e895db 346static ssize_t
542ff7bf 347__blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter, int nr_pages)
b2e895db
AM
348{
349 struct file *file = iocb->ki_filp;
4ebb16ca 350 struct inode *inode = bdev_file_inode(file);
542ff7bf 351 struct block_device *bdev = I_BDEV(inode);
64d656a1 352 struct blk_plug plug;
542ff7bf
CH
353 struct blkdev_dio *dio;
354 struct bio *bio;
690e5325 355 bool is_read = (iov_iter_rw(iter) == READ), is_sync;
542ff7bf
CH
356 loff_t pos = iocb->ki_pos;
357 blk_qc_t qc = BLK_QC_T_NONE;
36ffc6c1 358 int ret = 0;
542ff7bf 359
9a794fb9
JA
360 if ((pos | iov_iter_alignment(iter)) &
361 (bdev_logical_block_size(bdev) - 1))
542ff7bf
CH
362 return -EINVAL;
363
364 bio = bio_alloc_bioset(GFP_KERNEL, nr_pages, blkdev_dio_pool);
365 bio_get(bio); /* extra ref for the completion handler */
366
367 dio = container_of(bio, struct blkdev_dio, bio);
690e5325 368 dio->is_sync = is_sync = is_sync_kiocb(iocb);
542ff7bf
CH
369 if (dio->is_sync)
370 dio->waiter = current;
371 else
372 dio->iocb = iocb;
373
374 dio->size = 0;
375 dio->multi_bio = false;
376 dio->should_dirty = is_read && (iter->type == ITER_IOVEC);
377
64d656a1 378 blk_start_plug(&plug);
542ff7bf 379 for (;;) {
74d46992 380 bio_set_dev(bio, bdev);
4d1a4765 381 bio->bi_iter.bi_sector = pos >> 9;
45d06cf7 382 bio->bi_write_hint = iocb->ki_hint;
542ff7bf
CH
383 bio->bi_private = dio;
384 bio->bi_end_io = blkdev_bio_end_io;
385
386 ret = bio_iov_iter_get_pages(bio, iter);
387 if (unlikely(ret)) {
4e4cbee9 388 bio->bi_status = BLK_STS_IOERR;
542ff7bf
CH
389 bio_endio(bio);
390 break;
391 }
392
393 if (is_read) {
394 bio->bi_opf = REQ_OP_READ;
395 if (dio->should_dirty)
396 bio_set_pages_dirty(bio);
397 } else {
398 bio->bi_opf = dio_bio_write_op(iocb);
399 task_io_account_write(bio->bi_iter.bi_size);
400 }
401
402 dio->size += bio->bi_iter.bi_size;
403 pos += bio->bi_iter.bi_size;
404
405 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES);
406 if (!nr_pages) {
407 qc = submit_bio(bio);
408 break;
409 }
410
411 if (!dio->multi_bio) {
412 dio->multi_bio = true;
413 atomic_set(&dio->ref, 2);
414 } else {
415 atomic_inc(&dio->ref);
416 }
417
418 submit_bio(bio);
419 bio = bio_alloc(GFP_KERNEL, nr_pages);
420 }
64d656a1 421 blk_finish_plug(&plug);
542ff7bf 422
690e5325 423 if (!is_sync)
542ff7bf
CH
424 return -EIOCBQUEUED;
425
426 for (;;) {
427 set_current_state(TASK_UNINTERRUPTIBLE);
428 if (!READ_ONCE(dio->waiter))
429 break;
430
431 if (!(iocb->ki_flags & IOCB_HIPRI) ||
432 !blk_mq_poll(bdev_get_queue(bdev), qc))
433 io_schedule();
434 }
435 __set_current_state(TASK_RUNNING);
436
36ffc6c1 437 if (!ret)
4e4cbee9 438 ret = blk_status_to_errno(dio->bio.bi_status);
7a62a523 439 if (likely(!ret))
542ff7bf 440 ret = dio->size;
542ff7bf
CH
441
442 bio_put(&dio->bio);
443 return ret;
444}
445
446static ssize_t
447blkdev_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
448{
189ce2b9 449 int nr_pages;
b2e895db 450
72ecad22 451 nr_pages = iov_iter_npages(iter, BIO_MAX_PAGES + 1);
189ce2b9
CH
452 if (!nr_pages)
453 return 0;
72ecad22 454 if (is_sync_kiocb(iocb) && nr_pages <= BIO_MAX_PAGES)
189ce2b9 455 return __blkdev_direct_IO_simple(iocb, iter, nr_pages);
542ff7bf
CH
456
457 return __blkdev_direct_IO(iocb, iter, min(nr_pages, BIO_MAX_PAGES));
458}
459
460static __init int blkdev_init(void)
461{
011067b0 462 blkdev_dio_pool = bioset_create(4, offsetof(struct blkdev_dio, bio), BIOSET_NEED_BVECS);
542ff7bf
CH
463 if (!blkdev_dio_pool)
464 return -ENOMEM;
465 return 0;
b2e895db 466}
542ff7bf 467module_init(blkdev_init);
b2e895db 468
5cee5815
JK
469int __sync_blockdev(struct block_device *bdev, int wait)
470{
471 if (!bdev)
472 return 0;
473 if (!wait)
474 return filemap_flush(bdev->bd_inode->i_mapping);
475 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
476}
477
585d3bc0
NP
478/*
479 * Write out and wait upon all the dirty data associated with a block
480 * device via its mapping. Does not take the superblock lock.
481 */
482int sync_blockdev(struct block_device *bdev)
483{
5cee5815 484 return __sync_blockdev(bdev, 1);
585d3bc0
NP
485}
486EXPORT_SYMBOL(sync_blockdev);
487
488/*
489 * Write out and wait upon all dirty data associated with this
490 * device. Filesystem data as well as the underlying block
491 * device. Takes the superblock lock.
492 */
493int fsync_bdev(struct block_device *bdev)
494{
495 struct super_block *sb = get_super(bdev);
496 if (sb) {
60b0680f 497 int res = sync_filesystem(sb);
585d3bc0
NP
498 drop_super(sb);
499 return res;
500 }
501 return sync_blockdev(bdev);
502}
47e4491b 503EXPORT_SYMBOL(fsync_bdev);
585d3bc0
NP
504
505/**
506 * freeze_bdev -- lock a filesystem and force it into a consistent state
507 * @bdev: blockdevice to lock
508 *
585d3bc0
NP
509 * If a superblock is found on this device, we take the s_umount semaphore
510 * on it to make sure nobody unmounts until the snapshot creation is done.
511 * The reference counter (bd_fsfreeze_count) guarantees that only the last
512 * unfreeze process can unfreeze the frozen filesystem actually when multiple
513 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
514 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
515 * actually.
516 */
517struct super_block *freeze_bdev(struct block_device *bdev)
518{
519 struct super_block *sb;
520 int error = 0;
521
522 mutex_lock(&bdev->bd_fsfreeze_mutex);
4504230a
CH
523 if (++bdev->bd_fsfreeze_count > 1) {
524 /*
525 * We don't even need to grab a reference - the first call
526 * to freeze_bdev grab an active reference and only the last
527 * thaw_bdev drops it.
528 */
585d3bc0 529 sb = get_super(bdev);
5bb53c0f
AR
530 if (sb)
531 drop_super(sb);
4504230a
CH
532 mutex_unlock(&bdev->bd_fsfreeze_mutex);
533 return sb;
534 }
535
536 sb = get_active_super(bdev);
537 if (!sb)
538 goto out;
48b6bca6
BM
539 if (sb->s_op->freeze_super)
540 error = sb->s_op->freeze_super(sb);
541 else
542 error = freeze_super(sb);
18e9e510
JB
543 if (error) {
544 deactivate_super(sb);
545 bdev->bd_fsfreeze_count--;
585d3bc0 546 mutex_unlock(&bdev->bd_fsfreeze_mutex);
18e9e510 547 return ERR_PTR(error);
585d3bc0 548 }
18e9e510 549 deactivate_super(sb);
4504230a 550 out:
585d3bc0
NP
551 sync_blockdev(bdev);
552 mutex_unlock(&bdev->bd_fsfreeze_mutex);
4fadd7bb 553 return sb; /* thaw_bdev releases s->s_umount */
585d3bc0
NP
554}
555EXPORT_SYMBOL(freeze_bdev);
556
557/**
558 * thaw_bdev -- unlock filesystem
559 * @bdev: blockdevice to unlock
560 * @sb: associated superblock
561 *
562 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
563 */
564int thaw_bdev(struct block_device *bdev, struct super_block *sb)
565{
4504230a 566 int error = -EINVAL;
585d3bc0
NP
567
568 mutex_lock(&bdev->bd_fsfreeze_mutex);
4504230a 569 if (!bdev->bd_fsfreeze_count)
18e9e510 570 goto out;
4504230a
CH
571
572 error = 0;
573 if (--bdev->bd_fsfreeze_count > 0)
18e9e510 574 goto out;
4504230a
CH
575
576 if (!sb)
18e9e510 577 goto out;
4504230a 578
48b6bca6
BM
579 if (sb->s_op->thaw_super)
580 error = sb->s_op->thaw_super(sb);
581 else
582 error = thaw_super(sb);
997198ba 583 if (error)
18e9e510 584 bdev->bd_fsfreeze_count++;
18e9e510 585out:
585d3bc0 586 mutex_unlock(&bdev->bd_fsfreeze_mutex);
997198ba 587 return error;
585d3bc0
NP
588}
589EXPORT_SYMBOL(thaw_bdev);
590
1da177e4
LT
591static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
592{
593 return block_write_full_page(page, blkdev_get_block, wbc);
594}
595
596static int blkdev_readpage(struct file * file, struct page * page)
597{
598 return block_read_full_page(page, blkdev_get_block);
599}
600
447f05bb
AM
601static int blkdev_readpages(struct file *file, struct address_space *mapping,
602 struct list_head *pages, unsigned nr_pages)
603{
604 return mpage_readpages(mapping, pages, nr_pages, blkdev_get_block);
605}
606
6272b5a5
NP
607static int blkdev_write_begin(struct file *file, struct address_space *mapping,
608 loff_t pos, unsigned len, unsigned flags,
609 struct page **pagep, void **fsdata)
1da177e4 610{
155130a4
CH
611 return block_write_begin(mapping, pos, len, flags, pagep,
612 blkdev_get_block);
1da177e4
LT
613}
614
6272b5a5
NP
615static int blkdev_write_end(struct file *file, struct address_space *mapping,
616 loff_t pos, unsigned len, unsigned copied,
617 struct page *page, void *fsdata)
1da177e4 618{
6272b5a5
NP
619 int ret;
620 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
621
622 unlock_page(page);
09cbfeaf 623 put_page(page);
6272b5a5
NP
624
625 return ret;
1da177e4
LT
626}
627
628/*
629 * private llseek:
496ad9aa 630 * for a block special file file_inode(file)->i_size is zero
1da177e4
LT
631 * so we compute the size by hand (just as in block_read/write above)
632 */
965c8e59 633static loff_t block_llseek(struct file *file, loff_t offset, int whence)
1da177e4 634{
4ebb16ca 635 struct inode *bd_inode = bdev_file_inode(file);
1da177e4
LT
636 loff_t retval;
637
5955102c 638 inode_lock(bd_inode);
5d48f3a2 639 retval = fixed_size_llseek(file, offset, whence, i_size_read(bd_inode));
5955102c 640 inode_unlock(bd_inode);
1da177e4
LT
641 return retval;
642}
643
02c24a82 644int blkdev_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
1da177e4 645{
4ebb16ca 646 struct inode *bd_inode = bdev_file_inode(filp);
b8af67e2 647 struct block_device *bdev = I_BDEV(bd_inode);
ab0a9735 648 int error;
da5aa861 649
372cf243 650 error = file_write_and_wait_range(filp, start, end);
da5aa861
RW
651 if (error)
652 return error;
ab0a9735 653
b8af67e2
AB
654 /*
655 * There is no need to serialise calls to blkdev_issue_flush with
656 * i_mutex and doing so causes performance issues with concurrent
657 * O_SYNC writers to a block device.
658 */
dd3932ed 659 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
ab0a9735
CH
660 if (error == -EOPNOTSUPP)
661 error = 0;
b8af67e2 662
ab0a9735 663 return error;
1da177e4 664}
b1dd3b28 665EXPORT_SYMBOL(blkdev_fsync);
1da177e4 666
47a191fd
MW
667/**
668 * bdev_read_page() - Start reading a page from a block device
669 * @bdev: The device to read the page from
670 * @sector: The offset on the device to read the page to (need not be aligned)
671 * @page: The page to read
672 *
673 * On entry, the page should be locked. It will be unlocked when the page
674 * has been read. If the block driver implements rw_page synchronously,
675 * that will be true on exit from this function, but it need not be.
676 *
677 * Errors returned by this function are usually "soft", eg out of memory, or
678 * queue full; callers should try a different route to read this page rather
679 * than propagate an error back up the stack.
680 *
681 * Return: negative errno if an error occurs, 0 if submission was successful.
682 */
683int bdev_read_page(struct block_device *bdev, sector_t sector,
684 struct page *page)
685{
686 const struct block_device_operations *ops = bdev->bd_disk->fops;
2e6edc95
DW
687 int result = -EOPNOTSUPP;
688
f68eb1e7 689 if (!ops->rw_page || bdev_get_integrity(bdev))
2e6edc95
DW
690 return result;
691
6f3b0e8b 692 result = blk_queue_enter(bdev->bd_queue, false);
2e6edc95
DW
693 if (result)
694 return result;
c11f0c0b 695 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, false);
2e6edc95
DW
696 blk_queue_exit(bdev->bd_queue);
697 return result;
47a191fd
MW
698}
699EXPORT_SYMBOL_GPL(bdev_read_page);
700
701/**
702 * bdev_write_page() - Start writing a page to a block device
703 * @bdev: The device to write the page to
704 * @sector: The offset on the device to write the page to (need not be aligned)
705 * @page: The page to write
706 * @wbc: The writeback_control for the write
707 *
708 * On entry, the page should be locked and not currently under writeback.
709 * On exit, if the write started successfully, the page will be unlocked and
710 * under writeback. If the write failed already (eg the driver failed to
711 * queue the page to the device), the page will still be locked. If the
712 * caller is a ->writepage implementation, it will need to unlock the page.
713 *
714 * Errors returned by this function are usually "soft", eg out of memory, or
715 * queue full; callers should try a different route to write this page rather
716 * than propagate an error back up the stack.
717 *
718 * Return: negative errno if an error occurs, 0 if submission was successful.
719 */
720int bdev_write_page(struct block_device *bdev, sector_t sector,
721 struct page *page, struct writeback_control *wbc)
722{
723 int result;
47a191fd 724 const struct block_device_operations *ops = bdev->bd_disk->fops;
2e6edc95 725
f68eb1e7 726 if (!ops->rw_page || bdev_get_integrity(bdev))
47a191fd 727 return -EOPNOTSUPP;
6f3b0e8b 728 result = blk_queue_enter(bdev->bd_queue, false);
2e6edc95
DW
729 if (result)
730 return result;
731
47a191fd 732 set_page_writeback(page);
c11f0c0b 733 result = ops->rw_page(bdev, sector + get_start_sect(bdev), page, true);
f892760a 734 if (result) {
47a191fd 735 end_page_writeback(page);
f892760a
MW
736 } else {
737 clean_page_buffers(page);
47a191fd 738 unlock_page(page);
f892760a 739 }
2e6edc95 740 blk_queue_exit(bdev->bd_queue);
47a191fd
MW
741 return result;
742}
743EXPORT_SYMBOL_GPL(bdev_write_page);
744
1da177e4
LT
745/*
746 * pseudo-fs
747 */
748
749static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
e18b890b 750static struct kmem_cache * bdev_cachep __read_mostly;
1da177e4
LT
751
752static struct inode *bdev_alloc_inode(struct super_block *sb)
753{
e94b1766 754 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
1da177e4
LT
755 if (!ei)
756 return NULL;
757 return &ei->vfs_inode;
758}
759
fa0d7e3d 760static void bdev_i_callback(struct rcu_head *head)
1da177e4 761{
fa0d7e3d 762 struct inode *inode = container_of(head, struct inode, i_rcu);
1da177e4
LT
763 struct bdev_inode *bdi = BDEV_I(inode);
764
1da177e4
LT
765 kmem_cache_free(bdev_cachep, bdi);
766}
767
fa0d7e3d
NP
768static void bdev_destroy_inode(struct inode *inode)
769{
770 call_rcu(&inode->i_rcu, bdev_i_callback);
771}
772
51cc5068 773static void init_once(void *foo)
1da177e4
LT
774{
775 struct bdev_inode *ei = (struct bdev_inode *) foo;
776 struct block_device *bdev = &ei->bdev;
777
a35afb83
CL
778 memset(bdev, 0, sizeof(*bdev));
779 mutex_init(&bdev->bd_mutex);
a35afb83 780 INIT_LIST_HEAD(&bdev->bd_list);
49731baa
TH
781#ifdef CONFIG_SYSFS
782 INIT_LIST_HEAD(&bdev->bd_holder_disks);
783#endif
a5a79d00 784 bdev->bd_bdi = &noop_backing_dev_info;
a35afb83 785 inode_init_once(&ei->vfs_inode);
fcccf502
TS
786 /* Initialize mutex for freeze. */
787 mutex_init(&bdev->bd_fsfreeze_mutex);
1da177e4
LT
788}
789
b57922d9 790static void bdev_evict_inode(struct inode *inode)
1da177e4
LT
791{
792 struct block_device *bdev = &BDEV_I(inode)->bdev;
91b0abe3 793 truncate_inode_pages_final(&inode->i_data);
b57922d9 794 invalidate_inode_buffers(inode); /* is it needed here? */
dbd5768f 795 clear_inode(inode);
1da177e4 796 spin_lock(&bdev_lock);
1da177e4
LT
797 list_del_init(&bdev->bd_list);
798 spin_unlock(&bdev_lock);
f759741d
JK
799 /* Detach inode from wb early as bdi_put() may free bdi->wb */
800 inode_detach_wb(inode);
a5a79d00 801 if (bdev->bd_bdi != &noop_backing_dev_info) {
b1d2dc56 802 bdi_put(bdev->bd_bdi);
a5a79d00
JK
803 bdev->bd_bdi = &noop_backing_dev_info;
804 }
1da177e4
LT
805}
806
ee9b6d61 807static const struct super_operations bdev_sops = {
1da177e4
LT
808 .statfs = simple_statfs,
809 .alloc_inode = bdev_alloc_inode,
810 .destroy_inode = bdev_destroy_inode,
811 .drop_inode = generic_delete_inode,
b57922d9 812 .evict_inode = bdev_evict_inode,
1da177e4
LT
813};
814
51139ada
AV
815static struct dentry *bd_mount(struct file_system_type *fs_type,
816 int flags, const char *dev_name, void *data)
1da177e4 817{
3684aa70
SL
818 struct dentry *dent;
819 dent = mount_pseudo(fs_type, "bdev:", &bdev_sops, NULL, BDEVFS_MAGIC);
e9e5e3fa 820 if (!IS_ERR(dent))
3684aa70
SL
821 dent->d_sb->s_iflags |= SB_I_CGROUPWB;
822 return dent;
1da177e4
LT
823}
824
825static struct file_system_type bd_type = {
826 .name = "bdev",
51139ada 827 .mount = bd_mount,
1da177e4
LT
828 .kill_sb = kill_anon_super,
829};
830
a212b105
TH
831struct super_block *blockdev_superblock __read_mostly;
832EXPORT_SYMBOL_GPL(blockdev_superblock);
1da177e4
LT
833
834void __init bdev_cache_init(void)
835{
836 int err;
ace8577a 837 static struct vfsmount *bd_mnt;
c2acf7b9 838
1da177e4 839 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
fffb60f9 840 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
5d097056 841 SLAB_MEM_SPREAD|SLAB_ACCOUNT|SLAB_PANIC),
20c2df83 842 init_once);
1da177e4
LT
843 err = register_filesystem(&bd_type);
844 if (err)
845 panic("Cannot register bdev pseudo-fs");
846 bd_mnt = kern_mount(&bd_type);
1da177e4
LT
847 if (IS_ERR(bd_mnt))
848 panic("Cannot create bdev pseudo-fs");
ace8577a 849 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
1da177e4
LT
850}
851
852/*
853 * Most likely _very_ bad one - but then it's hardly critical for small
854 * /dev and can be fixed when somebody will need really large one.
855 * Keep in mind that it will be fed through icache hash function too.
856 */
857static inline unsigned long hash(dev_t dev)
858{
859 return MAJOR(dev)+MINOR(dev);
860}
861
862static int bdev_test(struct inode *inode, void *data)
863{
864 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
865}
866
867static int bdev_set(struct inode *inode, void *data)
868{
869 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
870 return 0;
871}
872
873static LIST_HEAD(all_bdevs);
874
f44f1ab5
JK
875/*
876 * If there is a bdev inode for this device, unhash it so that it gets evicted
877 * as soon as last inode reference is dropped.
878 */
879void bdev_unhash_inode(dev_t dev)
880{
881 struct inode *inode;
882
883 inode = ilookup5(blockdev_superblock, hash(dev), bdev_test, &dev);
884 if (inode) {
885 remove_inode_hash(inode);
886 iput(inode);
887 }
888}
889
1da177e4
LT
890struct block_device *bdget(dev_t dev)
891{
892 struct block_device *bdev;
893 struct inode *inode;
894
c2acf7b9 895 inode = iget5_locked(blockdev_superblock, hash(dev),
1da177e4
LT
896 bdev_test, bdev_set, &dev);
897
898 if (!inode)
899 return NULL;
900
901 bdev = &BDEV_I(inode)->bdev;
902
903 if (inode->i_state & I_NEW) {
904 bdev->bd_contains = NULL;
782b94cd 905 bdev->bd_super = NULL;
1da177e4 906 bdev->bd_inode = inode;
93407472 907 bdev->bd_block_size = i_blocksize(inode);
1da177e4
LT
908 bdev->bd_part_count = 0;
909 bdev->bd_invalidated = 0;
910 inode->i_mode = S_IFBLK;
911 inode->i_rdev = dev;
912 inode->i_bdev = bdev;
913 inode->i_data.a_ops = &def_blk_aops;
914 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
1da177e4
LT
915 spin_lock(&bdev_lock);
916 list_add(&bdev->bd_list, &all_bdevs);
917 spin_unlock(&bdev_lock);
918 unlock_new_inode(inode);
919 }
920 return bdev;
921}
922
923EXPORT_SYMBOL(bdget);
924
dddac6a7
AJ
925/**
926 * bdgrab -- Grab a reference to an already referenced block device
927 * @bdev: Block device to grab a reference to.
928 */
929struct block_device *bdgrab(struct block_device *bdev)
930{
7de9c6ee 931 ihold(bdev->bd_inode);
dddac6a7
AJ
932 return bdev;
933}
c1681bf8 934EXPORT_SYMBOL(bdgrab);
dddac6a7 935
1da177e4
LT
936long nr_blockdev_pages(void)
937{
203a2935 938 struct block_device *bdev;
1da177e4
LT
939 long ret = 0;
940 spin_lock(&bdev_lock);
203a2935 941 list_for_each_entry(bdev, &all_bdevs, bd_list) {
1da177e4
LT
942 ret += bdev->bd_inode->i_mapping->nrpages;
943 }
944 spin_unlock(&bdev_lock);
945 return ret;
946}
947
948void bdput(struct block_device *bdev)
949{
950 iput(bdev->bd_inode);
951}
952
953EXPORT_SYMBOL(bdput);
954
955static struct block_device *bd_acquire(struct inode *inode)
956{
957 struct block_device *bdev;
09d967c6 958
1da177e4
LT
959 spin_lock(&bdev_lock);
960 bdev = inode->i_bdev;
cccd9fb9 961 if (bdev && !inode_unhashed(bdev->bd_inode)) {
ed8a9d2c 962 bdgrab(bdev);
1da177e4
LT
963 spin_unlock(&bdev_lock);
964 return bdev;
965 }
966 spin_unlock(&bdev_lock);
09d967c6 967
cccd9fb9
JK
968 /*
969 * i_bdev references block device inode that was already shut down
970 * (corresponding device got removed). Remove the reference and look
971 * up block device inode again just in case new device got
972 * reestablished under the same device number.
973 */
974 if (bdev)
975 bd_forget(inode);
976
1da177e4
LT
977 bdev = bdget(inode->i_rdev);
978 if (bdev) {
979 spin_lock(&bdev_lock);
09d967c6
OH
980 if (!inode->i_bdev) {
981 /*
7de9c6ee 982 * We take an additional reference to bd_inode,
09d967c6
OH
983 * and it's released in clear_inode() of inode.
984 * So, we can access it via ->i_mapping always
985 * without igrab().
986 */
ed8a9d2c 987 bdgrab(bdev);
09d967c6
OH
988 inode->i_bdev = bdev;
989 inode->i_mapping = bdev->bd_inode->i_mapping;
09d967c6 990 }
1da177e4
LT
991 spin_unlock(&bdev_lock);
992 }
993 return bdev;
994}
995
996/* Call when you free inode */
997
998void bd_forget(struct inode *inode)
999{
09d967c6
OH
1000 struct block_device *bdev = NULL;
1001
1da177e4 1002 spin_lock(&bdev_lock);
b4ea2eaa
YH
1003 if (!sb_is_blkdev_sb(inode->i_sb))
1004 bdev = inode->i_bdev;
a4a4f943
AV
1005 inode->i_bdev = NULL;
1006 inode->i_mapping = &inode->i_data;
1da177e4 1007 spin_unlock(&bdev_lock);
09d967c6
OH
1008
1009 if (bdev)
ed8a9d2c 1010 bdput(bdev);
1da177e4
LT
1011}
1012
1a3cbbc5
TH
1013/**
1014 * bd_may_claim - test whether a block device can be claimed
1015 * @bdev: block device of interest
1016 * @whole: whole block device containing @bdev, may equal @bdev
1017 * @holder: holder trying to claim @bdev
1018 *
25985edc 1019 * Test whether @bdev can be claimed by @holder.
1a3cbbc5
TH
1020 *
1021 * CONTEXT:
1022 * spin_lock(&bdev_lock).
1023 *
1024 * RETURNS:
1025 * %true if @bdev can be claimed, %false otherwise.
1026 */
1027static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
1028 void *holder)
1da177e4 1029{
1da177e4 1030 if (bdev->bd_holder == holder)
1a3cbbc5 1031 return true; /* already a holder */
1da177e4 1032 else if (bdev->bd_holder != NULL)
1a3cbbc5 1033 return false; /* held by someone else */
bcc7f5b4 1034 else if (whole == bdev)
1a3cbbc5 1035 return true; /* is a whole device which isn't held */
1da177e4 1036
e525fd89 1037 else if (whole->bd_holder == bd_may_claim)
1a3cbbc5
TH
1038 return true; /* is a partition of a device that is being partitioned */
1039 else if (whole->bd_holder != NULL)
1040 return false; /* is a partition of a held device */
1da177e4 1041 else
1a3cbbc5
TH
1042 return true; /* is a partition of an un-held device */
1043}
1044
6b4517a7
TH
1045/**
1046 * bd_prepare_to_claim - prepare to claim a block device
1047 * @bdev: block device of interest
1048 * @whole: the whole device containing @bdev, may equal @bdev
1049 * @holder: holder trying to claim @bdev
1050 *
1051 * Prepare to claim @bdev. This function fails if @bdev is already
1052 * claimed by another holder and waits if another claiming is in
1053 * progress. This function doesn't actually claim. On successful
1054 * return, the caller has ownership of bd_claiming and bd_holder[s].
1055 *
1056 * CONTEXT:
1057 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
1058 * it multiple times.
1059 *
1060 * RETURNS:
1061 * 0 if @bdev can be claimed, -EBUSY otherwise.
1062 */
1063static int bd_prepare_to_claim(struct block_device *bdev,
1064 struct block_device *whole, void *holder)
1065{
1066retry:
1067 /* if someone else claimed, fail */
1068 if (!bd_may_claim(bdev, whole, holder))
1069 return -EBUSY;
1070
e75aa858
TH
1071 /* if claiming is already in progress, wait for it to finish */
1072 if (whole->bd_claiming) {
6b4517a7
TH
1073 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
1074 DEFINE_WAIT(wait);
1075
1076 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
1077 spin_unlock(&bdev_lock);
1078 schedule();
1079 finish_wait(wq, &wait);
1080 spin_lock(&bdev_lock);
1081 goto retry;
1082 }
1083
1084 /* yay, all mine */
1085 return 0;
1086}
1087
1088/**
1089 * bd_start_claiming - start claiming a block device
1090 * @bdev: block device of interest
1091 * @holder: holder trying to claim @bdev
1092 *
1093 * @bdev is about to be opened exclusively. Check @bdev can be opened
1094 * exclusively and mark that an exclusive open is in progress. Each
1095 * successful call to this function must be matched with a call to
b0018361
NP
1096 * either bd_finish_claiming() or bd_abort_claiming() (which do not
1097 * fail).
1098 *
1099 * This function is used to gain exclusive access to the block device
1100 * without actually causing other exclusive open attempts to fail. It
1101 * should be used when the open sequence itself requires exclusive
1102 * access but may subsequently fail.
6b4517a7
TH
1103 *
1104 * CONTEXT:
1105 * Might sleep.
1106 *
1107 * RETURNS:
1108 * Pointer to the block device containing @bdev on success, ERR_PTR()
1109 * value on failure.
1110 */
1111static struct block_device *bd_start_claiming(struct block_device *bdev,
1112 void *holder)
1113{
1114 struct gendisk *disk;
1115 struct block_device *whole;
1116 int partno, err;
1117
1118 might_sleep();
1119
1120 /*
1121 * @bdev might not have been initialized properly yet, look up
1122 * and grab the outer block device the hard way.
1123 */
1124 disk = get_gendisk(bdev->bd_dev, &partno);
1125 if (!disk)
1126 return ERR_PTR(-ENXIO);
1127
d4c208b8
TH
1128 /*
1129 * Normally, @bdev should equal what's returned from bdget_disk()
1130 * if partno is 0; however, some drivers (floppy) use multiple
1131 * bdev's for the same physical device and @bdev may be one of the
1132 * aliases. Keep @bdev if partno is 0. This means claimer
1133 * tracking is broken for those devices but it has always been that
1134 * way.
1135 */
1136 if (partno)
1137 whole = bdget_disk(disk, 0);
1138 else
1139 whole = bdgrab(bdev);
1140
cf342570 1141 module_put(disk->fops->owner);
6b4517a7
TH
1142 put_disk(disk);
1143 if (!whole)
1144 return ERR_PTR(-ENOMEM);
1145
1146 /* prepare to claim, if successful, mark claiming in progress */
1147 spin_lock(&bdev_lock);
1148
1149 err = bd_prepare_to_claim(bdev, whole, holder);
1150 if (err == 0) {
1151 whole->bd_claiming = holder;
1152 spin_unlock(&bdev_lock);
1153 return whole;
1154 } else {
1155 spin_unlock(&bdev_lock);
1156 bdput(whole);
1157 return ERR_PTR(err);
1158 }
1159}
1160
641dc636 1161#ifdef CONFIG_SYSFS
49731baa
TH
1162struct bd_holder_disk {
1163 struct list_head list;
1164 struct gendisk *disk;
1165 int refcnt;
1166};
1167
1168static struct bd_holder_disk *bd_find_holder_disk(struct block_device *bdev,
1169 struct gendisk *disk)
1170{
1171 struct bd_holder_disk *holder;
1172
1173 list_for_each_entry(holder, &bdev->bd_holder_disks, list)
1174 if (holder->disk == disk)
1175 return holder;
1176 return NULL;
1177}
1178
4d7dd8fd 1179static int add_symlink(struct kobject *from, struct kobject *to)
641dc636 1180{
4d7dd8fd 1181 return sysfs_create_link(from, to, kobject_name(to));
641dc636
JN
1182}
1183
1184static void del_symlink(struct kobject *from, struct kobject *to)
1185{
641dc636
JN
1186 sysfs_remove_link(from, kobject_name(to));
1187}
1188
df6c0cd9 1189/**
e09b457b
TH
1190 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1191 * @bdev: the claimed slave bdev
1192 * @disk: the holding disk
df6c0cd9 1193 *
49731baa
TH
1194 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1195 *
e09b457b 1196 * This functions creates the following sysfs symlinks.
641dc636 1197 *
e09b457b
TH
1198 * - from "slaves" directory of the holder @disk to the claimed @bdev
1199 * - from "holders" directory of the @bdev to the holder @disk
641dc636 1200 *
e09b457b
TH
1201 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1202 * passed to bd_link_disk_holder(), then:
641dc636 1203 *
e09b457b
TH
1204 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1205 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
641dc636 1206 *
e09b457b
TH
1207 * The caller must have claimed @bdev before calling this function and
1208 * ensure that both @bdev and @disk are valid during the creation and
1209 * lifetime of these symlinks.
641dc636 1210 *
e09b457b
TH
1211 * CONTEXT:
1212 * Might sleep.
641dc636 1213 *
e09b457b
TH
1214 * RETURNS:
1215 * 0 on success, -errno on failure.
641dc636 1216 */
e09b457b 1217int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 1218{
49731baa 1219 struct bd_holder_disk *holder;
e09b457b 1220 int ret = 0;
641dc636 1221
2e7b651d 1222 mutex_lock(&bdev->bd_mutex);
df6c0cd9 1223
49731baa 1224 WARN_ON_ONCE(!bdev->bd_holder);
4e91672c 1225
e09b457b
TH
1226 /* FIXME: remove the following once add_disk() handles errors */
1227 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1228 goto out_unlock;
4e91672c 1229
49731baa
TH
1230 holder = bd_find_holder_disk(bdev, disk);
1231 if (holder) {
1232 holder->refcnt++;
e09b457b 1233 goto out_unlock;
49731baa 1234 }
641dc636 1235
49731baa
TH
1236 holder = kzalloc(sizeof(*holder), GFP_KERNEL);
1237 if (!holder) {
1238 ret = -ENOMEM;
e09b457b
TH
1239 goto out_unlock;
1240 }
641dc636 1241
49731baa
TH
1242 INIT_LIST_HEAD(&holder->list);
1243 holder->disk = disk;
1244 holder->refcnt = 1;
1245
1246 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1247 if (ret)
1248 goto out_free;
1249
1250 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1251 if (ret)
1252 goto out_del;
e7407d16
TH
1253 /*
1254 * bdev could be deleted beneath us which would implicitly destroy
1255 * the holder directory. Hold on to it.
1256 */
1257 kobject_get(bdev->bd_part->holder_dir);
49731baa
TH
1258
1259 list_add(&holder->list, &bdev->bd_holder_disks);
1260 goto out_unlock;
1261
1262out_del:
1263 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1264out_free:
1265 kfree(holder);
e09b457b 1266out_unlock:
b4cf1b72 1267 mutex_unlock(&bdev->bd_mutex);
e09b457b 1268 return ret;
641dc636 1269}
e09b457b 1270EXPORT_SYMBOL_GPL(bd_link_disk_holder);
641dc636 1271
49731baa
TH
1272/**
1273 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
1274 * @bdev: the calimed slave bdev
1275 * @disk: the holding disk
1276 *
1277 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
1278 *
1279 * CONTEXT:
1280 * Might sleep.
1281 */
1282void bd_unlink_disk_holder(struct block_device *bdev, struct gendisk *disk)
641dc636 1283{
49731baa 1284 struct bd_holder_disk *holder;
641dc636 1285
49731baa 1286 mutex_lock(&bdev->bd_mutex);
641dc636 1287
49731baa
TH
1288 holder = bd_find_holder_disk(bdev, disk);
1289
1290 if (!WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
1291 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1292 del_symlink(bdev->bd_part->holder_dir,
1293 &disk_to_dev(disk)->kobj);
e7407d16 1294 kobject_put(bdev->bd_part->holder_dir);
49731baa
TH
1295 list_del_init(&holder->list);
1296 kfree(holder);
1297 }
1298
1299 mutex_unlock(&bdev->bd_mutex);
1da177e4 1300}
49731baa 1301EXPORT_SYMBOL_GPL(bd_unlink_disk_holder);
641dc636 1302#endif
1da177e4 1303
56ade44b
AP
1304/**
1305 * flush_disk - invalidates all buffer-cache entries on a disk
1306 *
1307 * @bdev: struct block device to be flushed
e6eb5ce1 1308 * @kill_dirty: flag to guide handling of dirty inodes
56ade44b
AP
1309 *
1310 * Invalidates all buffer-cache entries on a disk. It should be called
1311 * when a disk has been changed -- either by a media change or online
1312 * resize.
1313 */
93b270f7 1314static void flush_disk(struct block_device *bdev, bool kill_dirty)
56ade44b 1315{
93b270f7 1316 if (__invalidate_device(bdev, kill_dirty)) {
56ade44b 1317 printk(KERN_WARNING "VFS: busy inodes on changed media or "
424081f3
DM
1318 "resized disk %s\n",
1319 bdev->bd_disk ? bdev->bd_disk->disk_name : "");
56ade44b
AP
1320 }
1321
1322 if (!bdev->bd_disk)
1323 return;
d27769ec 1324 if (disk_part_scan_enabled(bdev->bd_disk))
56ade44b
AP
1325 bdev->bd_invalidated = 1;
1326}
1327
c3279d14 1328/**
57d1b536 1329 * check_disk_size_change - checks for disk size change and adjusts bdev size.
c3279d14
AP
1330 * @disk: struct gendisk to check
1331 * @bdev: struct bdev to adjust.
1332 *
1333 * This routine checks to see if the bdev size does not match the disk size
1334 * and adjusts it if it differs.
1335 */
1336void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1337{
1338 loff_t disk_size, bdev_size;
1339
1340 disk_size = (loff_t)get_capacity(disk) << 9;
1341 bdev_size = i_size_read(bdev->bd_inode);
1342 if (disk_size != bdev_size) {
c3279d14
AP
1343 printk(KERN_INFO
1344 "%s: detected capacity change from %lld to %lld\n",
424081f3 1345 disk->disk_name, bdev_size, disk_size);
c3279d14 1346 i_size_write(bdev->bd_inode, disk_size);
93b270f7 1347 flush_disk(bdev, false);
c3279d14
AP
1348 }
1349}
1350EXPORT_SYMBOL(check_disk_size_change);
1351
0c002c2f 1352/**
57d1b536 1353 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
0c002c2f
AP
1354 * @disk: struct gendisk to be revalidated
1355 *
1356 * This routine is a wrapper for lower-level driver's revalidate_disk
1357 * call-backs. It is used to do common pre and post operations needed
1358 * for all revalidate_disk operations.
1359 */
1360int revalidate_disk(struct gendisk *disk)
1361{
c3279d14 1362 struct block_device *bdev;
0c002c2f
AP
1363 int ret = 0;
1364
1365 if (disk->fops->revalidate_disk)
1366 ret = disk->fops->revalidate_disk(disk);
c3279d14
AP
1367 bdev = bdget_disk(disk, 0);
1368 if (!bdev)
1369 return ret;
1370
1371 mutex_lock(&bdev->bd_mutex);
1372 check_disk_size_change(disk, bdev);
7630b661 1373 bdev->bd_invalidated = 0;
c3279d14
AP
1374 mutex_unlock(&bdev->bd_mutex);
1375 bdput(bdev);
0c002c2f
AP
1376 return ret;
1377}
1378EXPORT_SYMBOL(revalidate_disk);
1379
1da177e4
LT
1380/*
1381 * This routine checks whether a removable media has been changed,
1382 * and invalidates all buffer-cache-entries in that case. This
1383 * is a relatively slow routine, so we have to try to minimize using
1384 * it. Thus it is called only upon a 'mount' or 'open'. This
1385 * is the best way of combining speed and utility, I think.
1386 * People changing diskettes in the middle of an operation deserve
1387 * to lose :-)
1388 */
1389int check_disk_change(struct block_device *bdev)
1390{
1391 struct gendisk *disk = bdev->bd_disk;
83d5cde4 1392 const struct block_device_operations *bdops = disk->fops;
77ea887e 1393 unsigned int events;
1da177e4 1394
77ea887e
TH
1395 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1396 DISK_EVENT_EJECT_REQUEST);
1397 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1da177e4
LT
1398 return 0;
1399
93b270f7 1400 flush_disk(bdev, true);
1da177e4
LT
1401 if (bdops->revalidate_disk)
1402 bdops->revalidate_disk(bdev->bd_disk);
1da177e4
LT
1403 return 1;
1404}
1405
1406EXPORT_SYMBOL(check_disk_change);
1407
1408void bd_set_size(struct block_device *bdev, loff_t size)
1409{
5955102c 1410 inode_lock(bdev->bd_inode);
d646a02a 1411 i_size_write(bdev->bd_inode, size);
5955102c 1412 inode_unlock(bdev->bd_inode);
1da177e4
LT
1413}
1414EXPORT_SYMBOL(bd_set_size);
1415
4385bab1 1416static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
37be4124 1417
6d740cd5
PZ
1418/*
1419 * bd_mutex locking:
1420 *
1421 * mutex_lock(part->bd_mutex)
1422 * mutex_lock_nested(whole->bd_mutex, 1)
1423 */
1424
572c4892 1425static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1da177e4 1426{
1da177e4 1427 struct gendisk *disk;
523e1d39 1428 struct module *owner;
7db9cfd3 1429 int ret;
cf771cb5 1430 int partno;
fe6e9c1f
AV
1431 int perm = 0;
1432
572c4892 1433 if (mode & FMODE_READ)
fe6e9c1f 1434 perm |= MAY_READ;
572c4892 1435 if (mode & FMODE_WRITE)
fe6e9c1f
AV
1436 perm |= MAY_WRITE;
1437 /*
1438 * hooks: /n/, see "layering violations".
1439 */
b7300b78
CW
1440 if (!for_part) {
1441 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1442 if (ret != 0) {
1443 bdput(bdev);
1444 return ret;
1445 }
82666020 1446 }
7db9cfd3 1447
d3374825 1448 restart:
0762b8bd 1449
89f97496 1450 ret = -ENXIO;
cf771cb5 1451 disk = get_gendisk(bdev->bd_dev, &partno);
0762b8bd 1452 if (!disk)
6e9624b8 1453 goto out;
523e1d39 1454 owner = disk->fops->owner;
1da177e4 1455
69e02c59 1456 disk_block_events(disk);
6796bf54 1457 mutex_lock_nested(&bdev->bd_mutex, for_part);
1da177e4
LT
1458 if (!bdev->bd_openers) {
1459 bdev->bd_disk = disk;
87192a2a 1460 bdev->bd_queue = disk->queue;
1da177e4 1461 bdev->bd_contains = bdev;
c2ee070f 1462 bdev->bd_partno = partno;
03cdadb0 1463
cf771cb5 1464 if (!partno) {
89f97496
TH
1465 ret = -ENXIO;
1466 bdev->bd_part = disk_get_part(disk, partno);
1467 if (!bdev->bd_part)
1468 goto out_clear;
1469
1196f8b8 1470 ret = 0;
1da177e4 1471 if (disk->fops->open) {
572c4892 1472 ret = disk->fops->open(bdev, mode);
d3374825
N
1473 if (ret == -ERESTARTSYS) {
1474 /* Lost a race with 'disk' being
1475 * deleted, try again.
1476 * See md.c
1477 */
1478 disk_put_part(bdev->bd_part);
1479 bdev->bd_part = NULL;
d3374825 1480 bdev->bd_disk = NULL;
87192a2a 1481 bdev->bd_queue = NULL;
d3374825 1482 mutex_unlock(&bdev->bd_mutex);
69e02c59 1483 disk_unblock_events(disk);
69e02c59 1484 put_disk(disk);
523e1d39 1485 module_put(owner);
d3374825
N
1486 goto restart;
1487 }
1da177e4 1488 }
7e69723f 1489
0fb89795 1490 if (!ret) {
7e69723f 1491 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
0fb89795
JK
1492 set_init_blocksize(bdev);
1493 }
7e69723f 1494
1196f8b8
TH
1495 /*
1496 * If the device is invalidated, rescan partition
1497 * if open succeeded or failed with -ENOMEDIUM.
1498 * The latter is necessary to prevent ghost
1499 * partitions on a removed medium.
1500 */
fe316bf2
JN
1501 if (bdev->bd_invalidated) {
1502 if (!ret)
1503 rescan_partitions(disk, bdev);
1504 else if (ret == -ENOMEDIUM)
1505 invalidate_partitions(disk, bdev);
1506 }
5a023cdb 1507
1196f8b8
TH
1508 if (ret)
1509 goto out_clear;
1da177e4 1510 } else {
1da177e4
LT
1511 struct block_device *whole;
1512 whole = bdget_disk(disk, 0);
1513 ret = -ENOMEM;
1514 if (!whole)
0762b8bd 1515 goto out_clear;
37be4124 1516 BUG_ON(for_part);
572c4892 1517 ret = __blkdev_get(whole, mode, 1);
1da177e4 1518 if (ret)
0762b8bd 1519 goto out_clear;
1da177e4 1520 bdev->bd_contains = whole;
89f97496 1521 bdev->bd_part = disk_get_part(disk, partno);
e71bf0d0 1522 if (!(disk->flags & GENHD_FL_UP) ||
89f97496 1523 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1da177e4 1524 ret = -ENXIO;
0762b8bd 1525 goto out_clear;
1da177e4 1526 }
89f97496 1527 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
0fb89795 1528 set_init_blocksize(bdev);
1da177e4 1529 }
03e26279
JK
1530
1531 if (bdev->bd_bdi == &noop_backing_dev_info)
1532 bdev->bd_bdi = bdi_get(disk->queue->backing_dev_info);
1da177e4 1533 } else {
1da177e4 1534 if (bdev->bd_contains == bdev) {
1196f8b8
TH
1535 ret = 0;
1536 if (bdev->bd_disk->fops->open)
572c4892 1537 ret = bdev->bd_disk->fops->open(bdev, mode);
1196f8b8 1538 /* the same as first opener case, read comment there */
fe316bf2
JN
1539 if (bdev->bd_invalidated) {
1540 if (!ret)
1541 rescan_partitions(bdev->bd_disk, bdev);
1542 else if (ret == -ENOMEDIUM)
1543 invalidate_partitions(bdev->bd_disk, bdev);
1544 }
1196f8b8
TH
1545 if (ret)
1546 goto out_unlock_bdev;
1da177e4 1547 }
69e02c59 1548 /* only one opener holds refs to the module and disk */
69e02c59 1549 put_disk(disk);
523e1d39 1550 module_put(owner);
1da177e4
LT
1551 }
1552 bdev->bd_openers++;
37be4124
N
1553 if (for_part)
1554 bdev->bd_part_count++;
c039e313 1555 mutex_unlock(&bdev->bd_mutex);
69e02c59 1556 disk_unblock_events(disk);
1da177e4
LT
1557 return 0;
1558
0762b8bd 1559 out_clear:
89f97496 1560 disk_put_part(bdev->bd_part);
1da177e4 1561 bdev->bd_disk = NULL;
0762b8bd 1562 bdev->bd_part = NULL;
87192a2a 1563 bdev->bd_queue = NULL;
1da177e4 1564 if (bdev != bdev->bd_contains)
572c4892 1565 __blkdev_put(bdev->bd_contains, mode, 1);
1da177e4 1566 bdev->bd_contains = NULL;
0762b8bd 1567 out_unlock_bdev:
c039e313 1568 mutex_unlock(&bdev->bd_mutex);
69e02c59 1569 disk_unblock_events(disk);
0762b8bd 1570 put_disk(disk);
523e1d39 1571 module_put(owner);
4345caba 1572 out:
0762b8bd
TH
1573 bdput(bdev);
1574
1da177e4
LT
1575 return ret;
1576}
1577
d4d77629
TH
1578/**
1579 * blkdev_get - open a block device
1580 * @bdev: block_device to open
1581 * @mode: FMODE_* mask
1582 * @holder: exclusive holder identifier
1583 *
1584 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1585 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1586 * @holder is invalid. Exclusive opens may nest for the same @holder.
1587 *
1588 * On success, the reference count of @bdev is unchanged. On failure,
1589 * @bdev is put.
1590 *
1591 * CONTEXT:
1592 * Might sleep.
1593 *
1594 * RETURNS:
1595 * 0 on success, -errno on failure.
1596 */
e525fd89 1597int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1da177e4 1598{
e525fd89
TH
1599 struct block_device *whole = NULL;
1600 int res;
1601
1602 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1603
1604 if ((mode & FMODE_EXCL) && holder) {
1605 whole = bd_start_claiming(bdev, holder);
1606 if (IS_ERR(whole)) {
1607 bdput(bdev);
1608 return PTR_ERR(whole);
1609 }
1610 }
1611
1612 res = __blkdev_get(bdev, mode, 0);
1613
1614 if (whole) {
d4dc210f
TH
1615 struct gendisk *disk = whole->bd_disk;
1616
6a027eff 1617 /* finish claiming */
77ea887e 1618 mutex_lock(&bdev->bd_mutex);
6a027eff
TH
1619 spin_lock(&bdev_lock);
1620
77ea887e 1621 if (!res) {
6a027eff
TH
1622 BUG_ON(!bd_may_claim(bdev, whole, holder));
1623 /*
1624 * Note that for a whole device bd_holders
1625 * will be incremented twice, and bd_holder
1626 * will be set to bd_may_claim before being
1627 * set to holder
1628 */
1629 whole->bd_holders++;
1630 whole->bd_holder = bd_may_claim;
1631 bdev->bd_holders++;
1632 bdev->bd_holder = holder;
1633 }
1634
1635 /* tell others that we're done */
1636 BUG_ON(whole->bd_claiming != holder);
1637 whole->bd_claiming = NULL;
1638 wake_up_bit(&whole->bd_claiming, 0);
1639
1640 spin_unlock(&bdev_lock);
77ea887e
TH
1641
1642 /*
d4dc210f
TH
1643 * Block event polling for write claims if requested. Any
1644 * write holder makes the write_holder state stick until
1645 * all are released. This is good enough and tracking
1646 * individual writeable reference is too fragile given the
1647 * way @mode is used in blkdev_get/put().
77ea887e 1648 */
4c49ff3f
TH
1649 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder &&
1650 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
77ea887e 1651 bdev->bd_write_holder = true;
d4dc210f 1652 disk_block_events(disk);
77ea887e
TH
1653 }
1654
1655 mutex_unlock(&bdev->bd_mutex);
6a027eff 1656 bdput(whole);
e525fd89
TH
1657 }
1658
1659 return res;
37be4124 1660}
1da177e4
LT
1661EXPORT_SYMBOL(blkdev_get);
1662
d4d77629
TH
1663/**
1664 * blkdev_get_by_path - open a block device by name
1665 * @path: path to the block device to open
1666 * @mode: FMODE_* mask
1667 * @holder: exclusive holder identifier
1668 *
1669 * Open the blockdevice described by the device file at @path. @mode
1670 * and @holder are identical to blkdev_get().
1671 *
1672 * On success, the returned block_device has reference count of one.
1673 *
1674 * CONTEXT:
1675 * Might sleep.
1676 *
1677 * RETURNS:
1678 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1679 */
1680struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1681 void *holder)
1682{
1683 struct block_device *bdev;
1684 int err;
1685
1686 bdev = lookup_bdev(path);
1687 if (IS_ERR(bdev))
1688 return bdev;
1689
1690 err = blkdev_get(bdev, mode, holder);
1691 if (err)
1692 return ERR_PTR(err);
1693
e51900f7
CE
1694 if ((mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1695 blkdev_put(bdev, mode);
1696 return ERR_PTR(-EACCES);
1697 }
1698
d4d77629
TH
1699 return bdev;
1700}
1701EXPORT_SYMBOL(blkdev_get_by_path);
1702
1703/**
1704 * blkdev_get_by_dev - open a block device by device number
1705 * @dev: device number of block device to open
1706 * @mode: FMODE_* mask
1707 * @holder: exclusive holder identifier
1708 *
1709 * Open the blockdevice described by device number @dev. @mode and
1710 * @holder are identical to blkdev_get().
1711 *
1712 * Use it ONLY if you really do not have anything better - i.e. when
1713 * you are behind a truly sucky interface and all you are given is a
1714 * device number. _Never_ to be used for internal purposes. If you
1715 * ever need it - reconsider your API.
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_dev(dev_t dev, fmode_t mode, void *holder)
1726{
1727 struct block_device *bdev;
1728 int err;
1729
1730 bdev = bdget(dev);
1731 if (!bdev)
1732 return ERR_PTR(-ENOMEM);
1733
1734 err = blkdev_get(bdev, mode, holder);
1735 if (err)
1736 return ERR_PTR(err);
1737
1738 return bdev;
1739}
1740EXPORT_SYMBOL(blkdev_get_by_dev);
1741
1da177e4
LT
1742static int blkdev_open(struct inode * inode, struct file * filp)
1743{
1744 struct block_device *bdev;
1da177e4
LT
1745
1746 /*
1747 * Preserve backwards compatibility and allow large file access
1748 * even if userspace doesn't ask for it explicitly. Some mkfs
1749 * binary needs it. We might want to drop this workaround
1750 * during an unstable branch.
1751 */
1752 filp->f_flags |= O_LARGEFILE;
1753
c35fc7a5
CH
1754 filp->f_mode |= FMODE_NOWAIT;
1755
572c4892
AV
1756 if (filp->f_flags & O_NDELAY)
1757 filp->f_mode |= FMODE_NDELAY;
1758 if (filp->f_flags & O_EXCL)
1759 filp->f_mode |= FMODE_EXCL;
1760 if ((filp->f_flags & O_ACCMODE) == 3)
1761 filp->f_mode |= FMODE_WRITE_IOCTL;
1762
1da177e4 1763 bdev = bd_acquire(inode);
6a2aae06
PE
1764 if (bdev == NULL)
1765 return -ENOMEM;
1da177e4 1766
572c4892 1767 filp->f_mapping = bdev->bd_inode->i_mapping;
5660e13d 1768 filp->f_wb_err = filemap_sample_wb_err(filp->f_mapping);
572c4892 1769
e525fd89 1770 return blkdev_get(bdev, filp->f_mode, filp);
1da177e4
LT
1771}
1772
4385bab1 1773static void __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
2e7b651d 1774{
2e7b651d 1775 struct gendisk *disk = bdev->bd_disk;
37be4124 1776 struct block_device *victim = NULL;
2e7b651d 1777
6796bf54 1778 mutex_lock_nested(&bdev->bd_mutex, for_part);
37be4124
N
1779 if (for_part)
1780 bdev->bd_part_count--;
1781
2e7b651d 1782 if (!--bdev->bd_openers) {
6a027eff 1783 WARN_ON_ONCE(bdev->bd_holders);
2e7b651d
PZ
1784 sync_blockdev(bdev);
1785 kill_bdev(bdev);
43d1c0eb
ID
1786
1787 bdev_write_inode(bdev);
2e7b651d
PZ
1788 }
1789 if (bdev->bd_contains == bdev) {
1790 if (disk->fops->release)
db2a144b 1791 disk->fops->release(disk, mode);
2e7b651d
PZ
1792 }
1793 if (!bdev->bd_openers) {
1794 struct module *owner = disk->fops->owner;
1795
0762b8bd
TH
1796 disk_put_part(bdev->bd_part);
1797 bdev->bd_part = NULL;
2e7b651d 1798 bdev->bd_disk = NULL;
37be4124
N
1799 if (bdev != bdev->bd_contains)
1800 victim = bdev->bd_contains;
2e7b651d 1801 bdev->bd_contains = NULL;
523e1d39
TH
1802
1803 put_disk(disk);
1804 module_put(owner);
2e7b651d 1805 }
2e7b651d
PZ
1806 mutex_unlock(&bdev->bd_mutex);
1807 bdput(bdev);
37be4124 1808 if (victim)
9a1c3542 1809 __blkdev_put(victim, mode, 1);
2e7b651d
PZ
1810}
1811
4385bab1 1812void blkdev_put(struct block_device *bdev, fmode_t mode)
37be4124 1813{
85ef06d1
TH
1814 mutex_lock(&bdev->bd_mutex);
1815
e525fd89 1816 if (mode & FMODE_EXCL) {
6a027eff
TH
1817 bool bdev_free;
1818
1819 /*
1820 * Release a claim on the device. The holder fields
1821 * are protected with bdev_lock. bd_mutex is to
1822 * synchronize disk_holder unlinking.
1823 */
6a027eff
TH
1824 spin_lock(&bdev_lock);
1825
1826 WARN_ON_ONCE(--bdev->bd_holders < 0);
1827 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1828
1829 /* bd_contains might point to self, check in a separate step */
1830 if ((bdev_free = !bdev->bd_holders))
1831 bdev->bd_holder = NULL;
1832 if (!bdev->bd_contains->bd_holders)
1833 bdev->bd_contains->bd_holder = NULL;
1834
1835 spin_unlock(&bdev_lock);
1836
77ea887e
TH
1837 /*
1838 * If this was the last claim, remove holder link and
1839 * unblock evpoll if it was a write holder.
1840 */
85ef06d1
TH
1841 if (bdev_free && bdev->bd_write_holder) {
1842 disk_unblock_events(bdev->bd_disk);
1843 bdev->bd_write_holder = false;
77ea887e 1844 }
6936217c 1845 }
77ea887e 1846
85ef06d1
TH
1847 /*
1848 * Trigger event checking and tell drivers to flush MEDIA_CHANGE
1849 * event. This is to ensure detection of media removal commanded
1850 * from userland - e.g. eject(1).
1851 */
1852 disk_flush_events(bdev->bd_disk, DISK_EVENT_MEDIA_CHANGE);
1853
1854 mutex_unlock(&bdev->bd_mutex);
1855
4385bab1 1856 __blkdev_put(bdev, mode, 0);
37be4124 1857}
2e7b651d
PZ
1858EXPORT_SYMBOL(blkdev_put);
1859
1da177e4
LT
1860static int blkdev_close(struct inode * inode, struct file * filp)
1861{
4ebb16ca 1862 struct block_device *bdev = I_BDEV(bdev_file_inode(filp));
4385bab1
AV
1863 blkdev_put(bdev, filp->f_mode);
1864 return 0;
1da177e4
LT
1865}
1866
bb93e3a5 1867static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1da177e4 1868{
4ebb16ca 1869 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
56b26add 1870 fmode_t mode = file->f_mode;
fd4ce1ac
CH
1871
1872 /*
1873 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1874 * to updated it before every ioctl.
1875 */
56b26add 1876 if (file->f_flags & O_NDELAY)
fd4ce1ac
CH
1877 mode |= FMODE_NDELAY;
1878 else
1879 mode &= ~FMODE_NDELAY;
1880
56b26add 1881 return blkdev_ioctl(bdev, mode, cmd, arg);
1da177e4
LT
1882}
1883
eef99380
CH
1884/*
1885 * Write data to the block device. Only intended for the block device itself
1886 * and the raw driver which basically is a fake block device.
1887 *
1888 * Does not take i_mutex for the write and thus is not for general purpose
1889 * use.
1890 */
1456c0a8 1891ssize_t blkdev_write_iter(struct kiocb *iocb, struct iov_iter *from)
eef99380
CH
1892{
1893 struct file *file = iocb->ki_filp;
4ebb16ca 1894 struct inode *bd_inode = bdev_file_inode(file);
7ec7b94a 1895 loff_t size = i_size_read(bd_inode);
53362a05 1896 struct blk_plug plug;
eef99380 1897 ssize_t ret;
5f380c7f 1898
7ec7b94a
AV
1899 if (bdev_read_only(I_BDEV(bd_inode)))
1900 return -EPERM;
5f380c7f 1901
7ec7b94a 1902 if (!iov_iter_count(from))
5f380c7f
AV
1903 return 0;
1904
7ec7b94a
AV
1905 if (iocb->ki_pos >= size)
1906 return -ENOSPC;
1907
c35fc7a5
CH
1908 if ((iocb->ki_flags & (IOCB_NOWAIT | IOCB_DIRECT)) == IOCB_NOWAIT)
1909 return -EOPNOTSUPP;
1910
7ec7b94a 1911 iov_iter_truncate(from, size - iocb->ki_pos);
eef99380 1912
53362a05 1913 blk_start_plug(&plug);
1456c0a8 1914 ret = __generic_file_write_iter(iocb, from);
e2592217
CH
1915 if (ret > 0)
1916 ret = generic_write_sync(iocb, ret);
53362a05 1917 blk_finish_plug(&plug);
eef99380
CH
1918 return ret;
1919}
1456c0a8 1920EXPORT_SYMBOL_GPL(blkdev_write_iter);
eef99380 1921
b2de525f 1922ssize_t blkdev_read_iter(struct kiocb *iocb, struct iov_iter *to)
684c9aae
LT
1923{
1924 struct file *file = iocb->ki_filp;
4ebb16ca 1925 struct inode *bd_inode = bdev_file_inode(file);
684c9aae 1926 loff_t size = i_size_read(bd_inode);
a886038b 1927 loff_t pos = iocb->ki_pos;
684c9aae
LT
1928
1929 if (pos >= size)
1930 return 0;
1931
1932 size -= pos;
a886038b
AV
1933 iov_iter_truncate(to, size);
1934 return generic_file_read_iter(iocb, to);
684c9aae 1935}
b2de525f 1936EXPORT_SYMBOL_GPL(blkdev_read_iter);
684c9aae 1937
87d8fe1e
TT
1938/*
1939 * Try to release a page associated with block device when the system
1940 * is under memory pressure.
1941 */
1942static int blkdev_releasepage(struct page *page, gfp_t wait)
1943{
1944 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1945
1946 if (super && super->s_op->bdev_try_to_free_page)
1947 return super->s_op->bdev_try_to_free_page(super, page, wait);
1948
1949 return try_to_free_buffers(page);
1950}
1951
7f6d5b52
RZ
1952static int blkdev_writepages(struct address_space *mapping,
1953 struct writeback_control *wbc)
1954{
1955 if (dax_mapping(mapping)) {
1956 struct block_device *bdev = I_BDEV(mapping->host);
1957
1958 return dax_writeback_mapping_range(mapping, bdev, wbc);
1959 }
1960 return generic_writepages(mapping, wbc);
1961}
1962
4c54ac62 1963static const struct address_space_operations def_blk_aops = {
1da177e4 1964 .readpage = blkdev_readpage,
447f05bb 1965 .readpages = blkdev_readpages,
1da177e4 1966 .writepage = blkdev_writepage,
6272b5a5
NP
1967 .write_begin = blkdev_write_begin,
1968 .write_end = blkdev_write_end,
7f6d5b52 1969 .writepages = blkdev_writepages,
87d8fe1e 1970 .releasepage = blkdev_releasepage,
1da177e4 1971 .direct_IO = blkdev_direct_IO,
b4597226 1972 .is_dirty_writeback = buffer_check_dirty_writeback,
1da177e4
LT
1973};
1974
25f4c414
DW
1975#define BLKDEV_FALLOC_FL_SUPPORTED \
1976 (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE | \
1977 FALLOC_FL_ZERO_RANGE | FALLOC_FL_NO_HIDE_STALE)
1978
1979static long blkdev_fallocate(struct file *file, int mode, loff_t start,
1980 loff_t len)
1981{
1982 struct block_device *bdev = I_BDEV(bdev_file_inode(file));
25f4c414
DW
1983 struct address_space *mapping;
1984 loff_t end = start + len - 1;
1985 loff_t isize;
1986 int error;
1987
1988 /* Fail if we don't recognize the flags. */
1989 if (mode & ~BLKDEV_FALLOC_FL_SUPPORTED)
1990 return -EOPNOTSUPP;
1991
1992 /* Don't go off the end of the device. */
1993 isize = i_size_read(bdev->bd_inode);
1994 if (start >= isize)
1995 return -EINVAL;
1996 if (end >= isize) {
1997 if (mode & FALLOC_FL_KEEP_SIZE) {
1998 len = isize - start;
1999 end = start + len - 1;
2000 } else
2001 return -EINVAL;
2002 }
2003
2004 /*
2005 * Don't allow IO that isn't aligned to logical block size.
2006 */
2007 if ((start | len) & (bdev_logical_block_size(bdev) - 1))
2008 return -EINVAL;
2009
2010 /* Invalidate the page cache, including dirty pages. */
2011 mapping = bdev->bd_inode->i_mapping;
2012 truncate_inode_pages_range(mapping, start, end);
2013
2014 switch (mode) {
2015 case FALLOC_FL_ZERO_RANGE:
2016 case FALLOC_FL_ZERO_RANGE | FALLOC_FL_KEEP_SIZE:
2017 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
ee472d83 2018 GFP_KERNEL, BLKDEV_ZERO_NOUNMAP);
25f4c414
DW
2019 break;
2020 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE:
34045129
CH
2021 error = blkdev_issue_zeroout(bdev, start >> 9, len >> 9,
2022 GFP_KERNEL, BLKDEV_ZERO_NOFALLBACK);
25f4c414
DW
2023 break;
2024 case FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE | FALLOC_FL_NO_HIDE_STALE:
25f4c414
DW
2025 error = blkdev_issue_discard(bdev, start >> 9, len >> 9,
2026 GFP_KERNEL, 0);
2027 break;
2028 default:
2029 return -EOPNOTSUPP;
2030 }
2031 if (error)
2032 return error;
2033
2034 /*
2035 * Invalidate again; if someone wandered in and dirtied a page,
2036 * the caller will be given -EBUSY. The third argument is
2037 * inclusive, so the rounding here is safe.
2038 */
2039 return invalidate_inode_pages2_range(mapping,
2040 start >> PAGE_SHIFT,
2041 end >> PAGE_SHIFT);
2042}
2043
4b6f5d20 2044const struct file_operations def_blk_fops = {
1da177e4
LT
2045 .open = blkdev_open,
2046 .release = blkdev_close,
2047 .llseek = block_llseek,
a886038b 2048 .read_iter = blkdev_read_iter,
1456c0a8 2049 .write_iter = blkdev_write_iter,
acc93d30 2050 .mmap = generic_file_mmap,
b1dd3b28 2051 .fsync = blkdev_fsync,
bb93e3a5 2052 .unlocked_ioctl = block_ioctl,
1da177e4
LT
2053#ifdef CONFIG_COMPAT
2054 .compat_ioctl = compat_blkdev_ioctl,
2055#endif
1e8b3332 2056 .splice_read = generic_file_splice_read,
8d020765 2057 .splice_write = iter_file_splice_write,
25f4c414 2058 .fallocate = blkdev_fallocate,
1da177e4
LT
2059};
2060
2061int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
2062{
2063 int res;
2064 mm_segment_t old_fs = get_fs();
2065 set_fs(KERNEL_DS);
56b26add 2066 res = blkdev_ioctl(bdev, 0, cmd, arg);
1da177e4
LT
2067 set_fs(old_fs);
2068 return res;
2069}
2070
2071EXPORT_SYMBOL(ioctl_by_bdev);
2072
2073/**
2074 * lookup_bdev - lookup a struct block_device by name
94e2959e 2075 * @pathname: special file representing the block device
1da177e4 2076 *
57d1b536 2077 * Get a reference to the blockdevice at @pathname in the current
1da177e4
LT
2078 * namespace if possible and return it. Return ERR_PTR(error)
2079 * otherwise.
2080 */
421748ec 2081struct block_device *lookup_bdev(const char *pathname)
1da177e4
LT
2082{
2083 struct block_device *bdev;
2084 struct inode *inode;
421748ec 2085 struct path path;
1da177e4
LT
2086 int error;
2087
421748ec 2088 if (!pathname || !*pathname)
1da177e4
LT
2089 return ERR_PTR(-EINVAL);
2090
421748ec 2091 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1da177e4
LT
2092 if (error)
2093 return ERR_PTR(error);
2094
bb668734 2095 inode = d_backing_inode(path.dentry);
1da177e4
LT
2096 error = -ENOTBLK;
2097 if (!S_ISBLK(inode->i_mode))
2098 goto fail;
2099 error = -EACCES;
a2982cc9 2100 if (!may_open_dev(&path))
1da177e4
LT
2101 goto fail;
2102 error = -ENOMEM;
2103 bdev = bd_acquire(inode);
2104 if (!bdev)
2105 goto fail;
2106out:
421748ec 2107 path_put(&path);
1da177e4
LT
2108 return bdev;
2109fail:
2110 bdev = ERR_PTR(error);
2111 goto out;
2112}
d5686b44 2113EXPORT_SYMBOL(lookup_bdev);
1da177e4 2114
93b270f7 2115int __invalidate_device(struct block_device *bdev, bool kill_dirty)
b71e8a4c
DH
2116{
2117 struct super_block *sb = get_super(bdev);
2118 int res = 0;
2119
2120 if (sb) {
2121 /*
2122 * no need to lock the super, get_super holds the
2123 * read mutex so the filesystem cannot go away
2124 * under us (->put_super runs with the write lock
2125 * hold).
2126 */
2127 shrink_dcache_sb(sb);
93b270f7 2128 res = invalidate_inodes(sb, kill_dirty);
b71e8a4c
DH
2129 drop_super(sb);
2130 }
f98393a6 2131 invalidate_bdev(bdev);
b71e8a4c
DH
2132 return res;
2133}
2134EXPORT_SYMBOL(__invalidate_device);
5c0d6b60
JK
2135
2136void iterate_bdevs(void (*func)(struct block_device *, void *), void *arg)
2137{
2138 struct inode *inode, *old_inode = NULL;
2139
74278da9 2140 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
2141 list_for_each_entry(inode, &blockdev_superblock->s_inodes, i_sb_list) {
2142 struct address_space *mapping = inode->i_mapping;
af309226 2143 struct block_device *bdev;
5c0d6b60
JK
2144
2145 spin_lock(&inode->i_lock);
2146 if (inode->i_state & (I_FREEING|I_WILL_FREE|I_NEW) ||
2147 mapping->nrpages == 0) {
2148 spin_unlock(&inode->i_lock);
2149 continue;
2150 }
2151 __iget(inode);
2152 spin_unlock(&inode->i_lock);
74278da9 2153 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
2154 /*
2155 * We hold a reference to 'inode' so it couldn't have been
2156 * removed from s_inodes list while we dropped the
74278da9 2157 * s_inode_list_lock We cannot iput the inode now as we can
5c0d6b60 2158 * be holding the last reference and we cannot iput it under
74278da9 2159 * s_inode_list_lock. So we keep the reference and iput it
5c0d6b60
JK
2160 * later.
2161 */
2162 iput(old_inode);
2163 old_inode = inode;
af309226 2164 bdev = I_BDEV(inode);
5c0d6b60 2165
af309226
RV
2166 mutex_lock(&bdev->bd_mutex);
2167 if (bdev->bd_openers)
2168 func(bdev, arg);
2169 mutex_unlock(&bdev->bd_mutex);
5c0d6b60 2170
74278da9 2171 spin_lock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60 2172 }
74278da9 2173 spin_unlock(&blockdev_superblock->s_inode_list_lock);
5c0d6b60
JK
2174 iput(old_inode);
2175}