]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/block_dev.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[mirror_ubuntu-bionic-kernel.git] / fs / block_dev.c
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
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>
14 #include <linux/device_cgroup.h>
15 #include <linux/highmem.h>
16 #include <linux/blkdev.h>
17 #include <linux/module.h>
18 #include <linux/blkpg.h>
19 #include <linux/buffer_head.h>
20 #include <linux/pagevec.h>
21 #include <linux/writeback.h>
22 #include <linux/mpage.h>
23 #include <linux/mount.h>
24 #include <linux/uio.h>
25 #include <linux/namei.h>
26 #include <linux/log2.h>
27 #include <linux/kmemleak.h>
28 #include <asm/uaccess.h>
29 #include "internal.h"
30
31 struct bdev_inode {
32 struct block_device bdev;
33 struct inode vfs_inode;
34 };
35
36 static const struct address_space_operations def_blk_aops;
37
38 static inline struct bdev_inode *BDEV_I(struct inode *inode)
39 {
40 return container_of(inode, struct bdev_inode, vfs_inode);
41 }
42
43 inline struct block_device *I_BDEV(struct inode *inode)
44 {
45 return &BDEV_I(inode)->bdev;
46 }
47
48 EXPORT_SYMBOL(I_BDEV);
49
50 /*
51 * move the inode from it's current bdi to the a new bdi. if the inode is dirty
52 * we need to move it onto the dirty list of @dst so that the inode is always
53 * on the right list.
54 */
55 static void bdev_inode_switch_bdi(struct inode *inode,
56 struct backing_dev_info *dst)
57 {
58 spin_lock(&inode_lock);
59 inode->i_data.backing_dev_info = dst;
60 if (inode->i_state & I_DIRTY)
61 list_move(&inode->i_wb_list, &dst->wb.b_dirty);
62 spin_unlock(&inode_lock);
63 }
64
65 static sector_t max_block(struct block_device *bdev)
66 {
67 sector_t retval = ~((sector_t)0);
68 loff_t sz = i_size_read(bdev->bd_inode);
69
70 if (sz) {
71 unsigned int size = block_size(bdev);
72 unsigned int sizebits = blksize_bits(size);
73 retval = (sz >> sizebits);
74 }
75 return retval;
76 }
77
78 /* Kill _all_ buffers and pagecache , dirty or not.. */
79 static void kill_bdev(struct block_device *bdev)
80 {
81 if (bdev->bd_inode->i_mapping->nrpages == 0)
82 return;
83 invalidate_bh_lrus();
84 truncate_inode_pages(bdev->bd_inode->i_mapping, 0);
85 }
86
87 int set_blocksize(struct block_device *bdev, int size)
88 {
89 /* Size must be a power of two, and between 512 and PAGE_SIZE */
90 if (size > PAGE_SIZE || size < 512 || !is_power_of_2(size))
91 return -EINVAL;
92
93 /* Size cannot be smaller than the size supported by the device */
94 if (size < bdev_logical_block_size(bdev))
95 return -EINVAL;
96
97 /* Don't change the size if it is same as current */
98 if (bdev->bd_block_size != size) {
99 sync_blockdev(bdev);
100 bdev->bd_block_size = size;
101 bdev->bd_inode->i_blkbits = blksize_bits(size);
102 kill_bdev(bdev);
103 }
104 return 0;
105 }
106
107 EXPORT_SYMBOL(set_blocksize);
108
109 int sb_set_blocksize(struct super_block *sb, int size)
110 {
111 if (set_blocksize(sb->s_bdev, size))
112 return 0;
113 /* If we get here, we know size is power of two
114 * and it's value is between 512 and PAGE_SIZE */
115 sb->s_blocksize = size;
116 sb->s_blocksize_bits = blksize_bits(size);
117 return sb->s_blocksize;
118 }
119
120 EXPORT_SYMBOL(sb_set_blocksize);
121
122 int sb_min_blocksize(struct super_block *sb, int size)
123 {
124 int minsize = bdev_logical_block_size(sb->s_bdev);
125 if (size < minsize)
126 size = minsize;
127 return sb_set_blocksize(sb, size);
128 }
129
130 EXPORT_SYMBOL(sb_min_blocksize);
131
132 static int
133 blkdev_get_block(struct inode *inode, sector_t iblock,
134 struct buffer_head *bh, int create)
135 {
136 if (iblock >= max_block(I_BDEV(inode))) {
137 if (create)
138 return -EIO;
139
140 /*
141 * for reads, we're just trying to fill a partial page.
142 * return a hole, they will have to call get_block again
143 * before they can fill it, and they will get -EIO at that
144 * time
145 */
146 return 0;
147 }
148 bh->b_bdev = I_BDEV(inode);
149 bh->b_blocknr = iblock;
150 set_buffer_mapped(bh);
151 return 0;
152 }
153
154 static int
155 blkdev_get_blocks(struct inode *inode, sector_t iblock,
156 struct buffer_head *bh, int create)
157 {
158 sector_t end_block = max_block(I_BDEV(inode));
159 unsigned long max_blocks = bh->b_size >> inode->i_blkbits;
160
161 if ((iblock + max_blocks) > end_block) {
162 max_blocks = end_block - iblock;
163 if ((long)max_blocks <= 0) {
164 if (create)
165 return -EIO; /* write fully beyond EOF */
166 /*
167 * It is a read which is fully beyond EOF. We return
168 * a !buffer_mapped buffer
169 */
170 max_blocks = 0;
171 }
172 }
173
174 bh->b_bdev = I_BDEV(inode);
175 bh->b_blocknr = iblock;
176 bh->b_size = max_blocks << inode->i_blkbits;
177 if (max_blocks)
178 set_buffer_mapped(bh);
179 return 0;
180 }
181
182 static ssize_t
183 blkdev_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
184 loff_t offset, unsigned long nr_segs)
185 {
186 struct file *file = iocb->ki_filp;
187 struct inode *inode = file->f_mapping->host;
188
189 return __blockdev_direct_IO(rw, iocb, inode, I_BDEV(inode), iov, offset,
190 nr_segs, blkdev_get_blocks, NULL, NULL, 0);
191 }
192
193 int __sync_blockdev(struct block_device *bdev, int wait)
194 {
195 if (!bdev)
196 return 0;
197 if (!wait)
198 return filemap_flush(bdev->bd_inode->i_mapping);
199 return filemap_write_and_wait(bdev->bd_inode->i_mapping);
200 }
201
202 /*
203 * Write out and wait upon all the dirty data associated with a block
204 * device via its mapping. Does not take the superblock lock.
205 */
206 int sync_blockdev(struct block_device *bdev)
207 {
208 return __sync_blockdev(bdev, 1);
209 }
210 EXPORT_SYMBOL(sync_blockdev);
211
212 /*
213 * Write out and wait upon all dirty data associated with this
214 * device. Filesystem data as well as the underlying block
215 * device. Takes the superblock lock.
216 */
217 int fsync_bdev(struct block_device *bdev)
218 {
219 struct super_block *sb = get_super(bdev);
220 if (sb) {
221 int res = sync_filesystem(sb);
222 drop_super(sb);
223 return res;
224 }
225 return sync_blockdev(bdev);
226 }
227 EXPORT_SYMBOL(fsync_bdev);
228
229 /**
230 * freeze_bdev -- lock a filesystem and force it into a consistent state
231 * @bdev: blockdevice to lock
232 *
233 * If a superblock is found on this device, we take the s_umount semaphore
234 * on it to make sure nobody unmounts until the snapshot creation is done.
235 * The reference counter (bd_fsfreeze_count) guarantees that only the last
236 * unfreeze process can unfreeze the frozen filesystem actually when multiple
237 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
238 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
239 * actually.
240 */
241 struct super_block *freeze_bdev(struct block_device *bdev)
242 {
243 struct super_block *sb;
244 int error = 0;
245
246 mutex_lock(&bdev->bd_fsfreeze_mutex);
247 if (++bdev->bd_fsfreeze_count > 1) {
248 /*
249 * We don't even need to grab a reference - the first call
250 * to freeze_bdev grab an active reference and only the last
251 * thaw_bdev drops it.
252 */
253 sb = get_super(bdev);
254 drop_super(sb);
255 mutex_unlock(&bdev->bd_fsfreeze_mutex);
256 return sb;
257 }
258
259 sb = get_active_super(bdev);
260 if (!sb)
261 goto out;
262 error = freeze_super(sb);
263 if (error) {
264 deactivate_super(sb);
265 bdev->bd_fsfreeze_count--;
266 mutex_unlock(&bdev->bd_fsfreeze_mutex);
267 return ERR_PTR(error);
268 }
269 deactivate_super(sb);
270 out:
271 sync_blockdev(bdev);
272 mutex_unlock(&bdev->bd_fsfreeze_mutex);
273 return sb; /* thaw_bdev releases s->s_umount */
274 }
275 EXPORT_SYMBOL(freeze_bdev);
276
277 /**
278 * thaw_bdev -- unlock filesystem
279 * @bdev: blockdevice to unlock
280 * @sb: associated superblock
281 *
282 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
283 */
284 int thaw_bdev(struct block_device *bdev, struct super_block *sb)
285 {
286 int error = -EINVAL;
287
288 mutex_lock(&bdev->bd_fsfreeze_mutex);
289 if (!bdev->bd_fsfreeze_count)
290 goto out;
291
292 error = 0;
293 if (--bdev->bd_fsfreeze_count > 0)
294 goto out;
295
296 if (!sb)
297 goto out;
298
299 error = thaw_super(sb);
300 if (error) {
301 bdev->bd_fsfreeze_count++;
302 mutex_unlock(&bdev->bd_fsfreeze_mutex);
303 return error;
304 }
305 out:
306 mutex_unlock(&bdev->bd_fsfreeze_mutex);
307 return 0;
308 }
309 EXPORT_SYMBOL(thaw_bdev);
310
311 static int blkdev_writepage(struct page *page, struct writeback_control *wbc)
312 {
313 return block_write_full_page(page, blkdev_get_block, wbc);
314 }
315
316 static int blkdev_readpage(struct file * file, struct page * page)
317 {
318 return block_read_full_page(page, blkdev_get_block);
319 }
320
321 static int blkdev_write_begin(struct file *file, struct address_space *mapping,
322 loff_t pos, unsigned len, unsigned flags,
323 struct page **pagep, void **fsdata)
324 {
325 return block_write_begin(mapping, pos, len, flags, pagep,
326 blkdev_get_block);
327 }
328
329 static int blkdev_write_end(struct file *file, struct address_space *mapping,
330 loff_t pos, unsigned len, unsigned copied,
331 struct page *page, void *fsdata)
332 {
333 int ret;
334 ret = block_write_end(file, mapping, pos, len, copied, page, fsdata);
335
336 unlock_page(page);
337 page_cache_release(page);
338
339 return ret;
340 }
341
342 /*
343 * private llseek:
344 * for a block special file file->f_path.dentry->d_inode->i_size is zero
345 * so we compute the size by hand (just as in block_read/write above)
346 */
347 static loff_t block_llseek(struct file *file, loff_t offset, int origin)
348 {
349 struct inode *bd_inode = file->f_mapping->host;
350 loff_t size;
351 loff_t retval;
352
353 mutex_lock(&bd_inode->i_mutex);
354 size = i_size_read(bd_inode);
355
356 switch (origin) {
357 case 2:
358 offset += size;
359 break;
360 case 1:
361 offset += file->f_pos;
362 }
363 retval = -EINVAL;
364 if (offset >= 0 && offset <= size) {
365 if (offset != file->f_pos) {
366 file->f_pos = offset;
367 }
368 retval = offset;
369 }
370 mutex_unlock(&bd_inode->i_mutex);
371 return retval;
372 }
373
374 int blkdev_fsync(struct file *filp, int datasync)
375 {
376 struct inode *bd_inode = filp->f_mapping->host;
377 struct block_device *bdev = I_BDEV(bd_inode);
378 int error;
379
380 /*
381 * There is no need to serialise calls to blkdev_issue_flush with
382 * i_mutex and doing so causes performance issues with concurrent
383 * O_SYNC writers to a block device.
384 */
385 mutex_unlock(&bd_inode->i_mutex);
386
387 error = blkdev_issue_flush(bdev, GFP_KERNEL, NULL);
388 if (error == -EOPNOTSUPP)
389 error = 0;
390
391 mutex_lock(&bd_inode->i_mutex);
392
393 return error;
394 }
395 EXPORT_SYMBOL(blkdev_fsync);
396
397 /*
398 * pseudo-fs
399 */
400
401 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(bdev_lock);
402 static struct kmem_cache * bdev_cachep __read_mostly;
403
404 static struct inode *bdev_alloc_inode(struct super_block *sb)
405 {
406 struct bdev_inode *ei = kmem_cache_alloc(bdev_cachep, GFP_KERNEL);
407 if (!ei)
408 return NULL;
409 return &ei->vfs_inode;
410 }
411
412 static void bdev_i_callback(struct rcu_head *head)
413 {
414 struct inode *inode = container_of(head, struct inode, i_rcu);
415 struct bdev_inode *bdi = BDEV_I(inode);
416
417 INIT_LIST_HEAD(&inode->i_dentry);
418 kmem_cache_free(bdev_cachep, bdi);
419 }
420
421 static void bdev_destroy_inode(struct inode *inode)
422 {
423 call_rcu(&inode->i_rcu, bdev_i_callback);
424 }
425
426 static void init_once(void *foo)
427 {
428 struct bdev_inode *ei = (struct bdev_inode *) foo;
429 struct block_device *bdev = &ei->bdev;
430
431 memset(bdev, 0, sizeof(*bdev));
432 mutex_init(&bdev->bd_mutex);
433 INIT_LIST_HEAD(&bdev->bd_inodes);
434 INIT_LIST_HEAD(&bdev->bd_list);
435 #ifdef CONFIG_SYSFS
436 INIT_LIST_HEAD(&bdev->bd_holder_list);
437 #endif
438 inode_init_once(&ei->vfs_inode);
439 /* Initialize mutex for freeze. */
440 mutex_init(&bdev->bd_fsfreeze_mutex);
441 }
442
443 static inline void __bd_forget(struct inode *inode)
444 {
445 list_del_init(&inode->i_devices);
446 inode->i_bdev = NULL;
447 inode->i_mapping = &inode->i_data;
448 }
449
450 static void bdev_evict_inode(struct inode *inode)
451 {
452 struct block_device *bdev = &BDEV_I(inode)->bdev;
453 struct list_head *p;
454 truncate_inode_pages(&inode->i_data, 0);
455 invalidate_inode_buffers(inode); /* is it needed here? */
456 end_writeback(inode);
457 spin_lock(&bdev_lock);
458 while ( (p = bdev->bd_inodes.next) != &bdev->bd_inodes ) {
459 __bd_forget(list_entry(p, struct inode, i_devices));
460 }
461 list_del_init(&bdev->bd_list);
462 spin_unlock(&bdev_lock);
463 }
464
465 static const struct super_operations bdev_sops = {
466 .statfs = simple_statfs,
467 .alloc_inode = bdev_alloc_inode,
468 .destroy_inode = bdev_destroy_inode,
469 .drop_inode = generic_delete_inode,
470 .evict_inode = bdev_evict_inode,
471 };
472
473 static struct dentry *bd_mount(struct file_system_type *fs_type,
474 int flags, const char *dev_name, void *data)
475 {
476 return mount_pseudo(fs_type, "bdev:", &bdev_sops, 0x62646576);
477 }
478
479 static struct file_system_type bd_type = {
480 .name = "bdev",
481 .mount = bd_mount,
482 .kill_sb = kill_anon_super,
483 };
484
485 struct super_block *blockdev_superblock __read_mostly;
486
487 void __init bdev_cache_init(void)
488 {
489 int err;
490 struct vfsmount *bd_mnt;
491
492 bdev_cachep = kmem_cache_create("bdev_cache", sizeof(struct bdev_inode),
493 0, (SLAB_HWCACHE_ALIGN|SLAB_RECLAIM_ACCOUNT|
494 SLAB_MEM_SPREAD|SLAB_PANIC),
495 init_once);
496 err = register_filesystem(&bd_type);
497 if (err)
498 panic("Cannot register bdev pseudo-fs");
499 bd_mnt = kern_mount(&bd_type);
500 if (IS_ERR(bd_mnt))
501 panic("Cannot create bdev pseudo-fs");
502 /*
503 * This vfsmount structure is only used to obtain the
504 * blockdev_superblock, so tell kmemleak not to report it.
505 */
506 kmemleak_not_leak(bd_mnt);
507 blockdev_superblock = bd_mnt->mnt_sb; /* For writeback */
508 }
509
510 /*
511 * Most likely _very_ bad one - but then it's hardly critical for small
512 * /dev and can be fixed when somebody will need really large one.
513 * Keep in mind that it will be fed through icache hash function too.
514 */
515 static inline unsigned long hash(dev_t dev)
516 {
517 return MAJOR(dev)+MINOR(dev);
518 }
519
520 static int bdev_test(struct inode *inode, void *data)
521 {
522 return BDEV_I(inode)->bdev.bd_dev == *(dev_t *)data;
523 }
524
525 static int bdev_set(struct inode *inode, void *data)
526 {
527 BDEV_I(inode)->bdev.bd_dev = *(dev_t *)data;
528 return 0;
529 }
530
531 static LIST_HEAD(all_bdevs);
532
533 struct block_device *bdget(dev_t dev)
534 {
535 struct block_device *bdev;
536 struct inode *inode;
537
538 inode = iget5_locked(blockdev_superblock, hash(dev),
539 bdev_test, bdev_set, &dev);
540
541 if (!inode)
542 return NULL;
543
544 bdev = &BDEV_I(inode)->bdev;
545
546 if (inode->i_state & I_NEW) {
547 bdev->bd_contains = NULL;
548 bdev->bd_inode = inode;
549 bdev->bd_block_size = (1 << inode->i_blkbits);
550 bdev->bd_part_count = 0;
551 bdev->bd_invalidated = 0;
552 inode->i_mode = S_IFBLK;
553 inode->i_rdev = dev;
554 inode->i_bdev = bdev;
555 inode->i_data.a_ops = &def_blk_aops;
556 mapping_set_gfp_mask(&inode->i_data, GFP_USER);
557 inode->i_data.backing_dev_info = &default_backing_dev_info;
558 spin_lock(&bdev_lock);
559 list_add(&bdev->bd_list, &all_bdevs);
560 spin_unlock(&bdev_lock);
561 unlock_new_inode(inode);
562 }
563 return bdev;
564 }
565
566 EXPORT_SYMBOL(bdget);
567
568 /**
569 * bdgrab -- Grab a reference to an already referenced block device
570 * @bdev: Block device to grab a reference to.
571 */
572 struct block_device *bdgrab(struct block_device *bdev)
573 {
574 ihold(bdev->bd_inode);
575 return bdev;
576 }
577
578 long nr_blockdev_pages(void)
579 {
580 struct block_device *bdev;
581 long ret = 0;
582 spin_lock(&bdev_lock);
583 list_for_each_entry(bdev, &all_bdevs, bd_list) {
584 ret += bdev->bd_inode->i_mapping->nrpages;
585 }
586 spin_unlock(&bdev_lock);
587 return ret;
588 }
589
590 void bdput(struct block_device *bdev)
591 {
592 iput(bdev->bd_inode);
593 }
594
595 EXPORT_SYMBOL(bdput);
596
597 static struct block_device *bd_acquire(struct inode *inode)
598 {
599 struct block_device *bdev;
600
601 spin_lock(&bdev_lock);
602 bdev = inode->i_bdev;
603 if (bdev) {
604 ihold(bdev->bd_inode);
605 spin_unlock(&bdev_lock);
606 return bdev;
607 }
608 spin_unlock(&bdev_lock);
609
610 bdev = bdget(inode->i_rdev);
611 if (bdev) {
612 spin_lock(&bdev_lock);
613 if (!inode->i_bdev) {
614 /*
615 * We take an additional reference to bd_inode,
616 * and it's released in clear_inode() of inode.
617 * So, we can access it via ->i_mapping always
618 * without igrab().
619 */
620 ihold(bdev->bd_inode);
621 inode->i_bdev = bdev;
622 inode->i_mapping = bdev->bd_inode->i_mapping;
623 list_add(&inode->i_devices, &bdev->bd_inodes);
624 }
625 spin_unlock(&bdev_lock);
626 }
627 return bdev;
628 }
629
630 /* Call when you free inode */
631
632 void bd_forget(struct inode *inode)
633 {
634 struct block_device *bdev = NULL;
635
636 spin_lock(&bdev_lock);
637 if (inode->i_bdev) {
638 if (!sb_is_blkdev_sb(inode->i_sb))
639 bdev = inode->i_bdev;
640 __bd_forget(inode);
641 }
642 spin_unlock(&bdev_lock);
643
644 if (bdev)
645 iput(bdev->bd_inode);
646 }
647
648 /**
649 * bd_may_claim - test whether a block device can be claimed
650 * @bdev: block device of interest
651 * @whole: whole block device containing @bdev, may equal @bdev
652 * @holder: holder trying to claim @bdev
653 *
654 * Test whther @bdev can be claimed by @holder.
655 *
656 * CONTEXT:
657 * spin_lock(&bdev_lock).
658 *
659 * RETURNS:
660 * %true if @bdev can be claimed, %false otherwise.
661 */
662 static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
663 void *holder)
664 {
665 if (bdev->bd_holder == holder)
666 return true; /* already a holder */
667 else if (bdev->bd_holder != NULL)
668 return false; /* held by someone else */
669 else if (bdev->bd_contains == bdev)
670 return true; /* is a whole device which isn't held */
671
672 else if (whole->bd_holder == bd_claim)
673 return true; /* is a partition of a device that is being partitioned */
674 else if (whole->bd_holder != NULL)
675 return false; /* is a partition of a held device */
676 else
677 return true; /* is a partition of an un-held device */
678 }
679
680 /**
681 * bd_prepare_to_claim - prepare to claim a block device
682 * @bdev: block device of interest
683 * @whole: the whole device containing @bdev, may equal @bdev
684 * @holder: holder trying to claim @bdev
685 *
686 * Prepare to claim @bdev. This function fails if @bdev is already
687 * claimed by another holder and waits if another claiming is in
688 * progress. This function doesn't actually claim. On successful
689 * return, the caller has ownership of bd_claiming and bd_holder[s].
690 *
691 * CONTEXT:
692 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
693 * it multiple times.
694 *
695 * RETURNS:
696 * 0 if @bdev can be claimed, -EBUSY otherwise.
697 */
698 static int bd_prepare_to_claim(struct block_device *bdev,
699 struct block_device *whole, void *holder)
700 {
701 retry:
702 /* if someone else claimed, fail */
703 if (!bd_may_claim(bdev, whole, holder))
704 return -EBUSY;
705
706 /* if claiming is already in progress, wait for it to finish */
707 if (whole->bd_claiming) {
708 wait_queue_head_t *wq = bit_waitqueue(&whole->bd_claiming, 0);
709 DEFINE_WAIT(wait);
710
711 prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
712 spin_unlock(&bdev_lock);
713 schedule();
714 finish_wait(wq, &wait);
715 spin_lock(&bdev_lock);
716 goto retry;
717 }
718
719 /* yay, all mine */
720 return 0;
721 }
722
723 /**
724 * bd_start_claiming - start claiming a block device
725 * @bdev: block device of interest
726 * @holder: holder trying to claim @bdev
727 *
728 * @bdev is about to be opened exclusively. Check @bdev can be opened
729 * exclusively and mark that an exclusive open is in progress. Each
730 * successful call to this function must be matched with a call to
731 * either bd_finish_claiming() or bd_abort_claiming() (which do not
732 * fail).
733 *
734 * This function is used to gain exclusive access to the block device
735 * without actually causing other exclusive open attempts to fail. It
736 * should be used when the open sequence itself requires exclusive
737 * access but may subsequently fail.
738 *
739 * CONTEXT:
740 * Might sleep.
741 *
742 * RETURNS:
743 * Pointer to the block device containing @bdev on success, ERR_PTR()
744 * value on failure.
745 */
746 static struct block_device *bd_start_claiming(struct block_device *bdev,
747 void *holder)
748 {
749 struct gendisk *disk;
750 struct block_device *whole;
751 int partno, err;
752
753 might_sleep();
754
755 /*
756 * @bdev might not have been initialized properly yet, look up
757 * and grab the outer block device the hard way.
758 */
759 disk = get_gendisk(bdev->bd_dev, &partno);
760 if (!disk)
761 return ERR_PTR(-ENXIO);
762
763 whole = bdget_disk(disk, 0);
764 module_put(disk->fops->owner);
765 put_disk(disk);
766 if (!whole)
767 return ERR_PTR(-ENOMEM);
768
769 /* prepare to claim, if successful, mark claiming in progress */
770 spin_lock(&bdev_lock);
771
772 err = bd_prepare_to_claim(bdev, whole, holder);
773 if (err == 0) {
774 whole->bd_claiming = holder;
775 spin_unlock(&bdev_lock);
776 return whole;
777 } else {
778 spin_unlock(&bdev_lock);
779 bdput(whole);
780 return ERR_PTR(err);
781 }
782 }
783
784 /* releases bdev_lock */
785 static void __bd_abort_claiming(struct block_device *whole, void *holder)
786 {
787 BUG_ON(whole->bd_claiming != holder);
788 whole->bd_claiming = NULL;
789 wake_up_bit(&whole->bd_claiming, 0);
790
791 spin_unlock(&bdev_lock);
792 bdput(whole);
793 }
794
795 /**
796 * bd_abort_claiming - abort claiming a block device
797 * @whole: whole block device returned by bd_start_claiming()
798 * @holder: holder trying to claim @bdev
799 *
800 * Abort a claiming block started by bd_start_claiming(). Note that
801 * @whole is not the block device to be claimed but the whole device
802 * returned by bd_start_claiming().
803 *
804 * CONTEXT:
805 * Grabs and releases bdev_lock.
806 */
807 static void bd_abort_claiming(struct block_device *whole, void *holder)
808 {
809 spin_lock(&bdev_lock);
810 __bd_abort_claiming(whole, holder); /* releases bdev_lock */
811 }
812
813 /* increment holders when we have a legitimate claim. requires bdev_lock */
814 static void __bd_claim(struct block_device *bdev, struct block_device *whole,
815 void *holder)
816 {
817 /* note that for a whole device bd_holders
818 * will be incremented twice, and bd_holder will
819 * be set to bd_claim before being set to holder
820 */
821 whole->bd_holders++;
822 whole->bd_holder = bd_claim;
823 bdev->bd_holders++;
824 bdev->bd_holder = holder;
825 }
826
827 /**
828 * bd_finish_claiming - finish claiming a block device
829 * @bdev: block device of interest (passed to bd_start_claiming())
830 * @whole: whole block device returned by bd_start_claiming()
831 * @holder: holder trying to claim @bdev
832 *
833 * Finish a claiming block started by bd_start_claiming().
834 *
835 * CONTEXT:
836 * Grabs and releases bdev_lock.
837 */
838 static void bd_finish_claiming(struct block_device *bdev,
839 struct block_device *whole, void *holder)
840 {
841 spin_lock(&bdev_lock);
842 BUG_ON(!bd_may_claim(bdev, whole, holder));
843 __bd_claim(bdev, whole, holder);
844 __bd_abort_claiming(whole, holder); /* not actually an abort */
845 }
846
847 /**
848 * bd_claim - claim a block device
849 * @bdev: block device to claim
850 * @holder: holder trying to claim @bdev
851 *
852 * Try to claim @bdev which must have been opened successfully.
853 *
854 * CONTEXT:
855 * Might sleep.
856 *
857 * RETURNS:
858 * 0 if successful, -EBUSY if @bdev is already claimed.
859 */
860 int bd_claim(struct block_device *bdev, void *holder)
861 {
862 struct block_device *whole = bdev->bd_contains;
863 int res;
864
865 might_sleep();
866
867 spin_lock(&bdev_lock);
868 res = bd_prepare_to_claim(bdev, whole, holder);
869 if (res == 0)
870 __bd_claim(bdev, whole, holder);
871 spin_unlock(&bdev_lock);
872
873 return res;
874 }
875 EXPORT_SYMBOL(bd_claim);
876
877 void bd_release(struct block_device *bdev)
878 {
879 spin_lock(&bdev_lock);
880 if (!--bdev->bd_contains->bd_holders)
881 bdev->bd_contains->bd_holder = NULL;
882 if (!--bdev->bd_holders)
883 bdev->bd_holder = NULL;
884 spin_unlock(&bdev_lock);
885 }
886
887 EXPORT_SYMBOL(bd_release);
888
889 #ifdef CONFIG_SYSFS
890 /*
891 * Functions for bd_claim_by_kobject / bd_release_from_kobject
892 *
893 * If a kobject is passed to bd_claim_by_kobject()
894 * and the kobject has a parent directory,
895 * following symlinks are created:
896 * o from the kobject to the claimed bdev
897 * o from "holders" directory of the bdev to the parent of the kobject
898 * bd_release_from_kobject() removes these symlinks.
899 *
900 * Example:
901 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
902 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
903 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
904 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
905 */
906
907 static int add_symlink(struct kobject *from, struct kobject *to)
908 {
909 if (!from || !to)
910 return 0;
911 return sysfs_create_link(from, to, kobject_name(to));
912 }
913
914 static void del_symlink(struct kobject *from, struct kobject *to)
915 {
916 if (!from || !to)
917 return;
918 sysfs_remove_link(from, kobject_name(to));
919 }
920
921 /*
922 * 'struct bd_holder' contains pointers to kobjects symlinked by
923 * bd_claim_by_kobject.
924 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
925 */
926 struct bd_holder {
927 struct list_head list; /* chain of holders of the bdev */
928 int count; /* references from the holder */
929 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
930 struct kobject *hdev; /* e.g. "/block/dm-0" */
931 struct kobject *hdir; /* e.g. "/block/sda/holders" */
932 struct kobject *sdev; /* e.g. "/block/sda" */
933 };
934
935 /*
936 * Get references of related kobjects at once.
937 * Returns 1 on success. 0 on failure.
938 *
939 * Should call bd_holder_release_dirs() after successful use.
940 */
941 static int bd_holder_grab_dirs(struct block_device *bdev,
942 struct bd_holder *bo)
943 {
944 if (!bdev || !bo)
945 return 0;
946
947 bo->sdir = kobject_get(bo->sdir);
948 if (!bo->sdir)
949 return 0;
950
951 bo->hdev = kobject_get(bo->sdir->parent);
952 if (!bo->hdev)
953 goto fail_put_sdir;
954
955 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
956 if (!bo->sdev)
957 goto fail_put_hdev;
958
959 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
960 if (!bo->hdir)
961 goto fail_put_sdev;
962
963 return 1;
964
965 fail_put_sdev:
966 kobject_put(bo->sdev);
967 fail_put_hdev:
968 kobject_put(bo->hdev);
969 fail_put_sdir:
970 kobject_put(bo->sdir);
971
972 return 0;
973 }
974
975 /* Put references of related kobjects at once. */
976 static void bd_holder_release_dirs(struct bd_holder *bo)
977 {
978 kobject_put(bo->hdir);
979 kobject_put(bo->sdev);
980 kobject_put(bo->hdev);
981 kobject_put(bo->sdir);
982 }
983
984 static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
985 {
986 struct bd_holder *bo;
987
988 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
989 if (!bo)
990 return NULL;
991
992 bo->count = 1;
993 bo->sdir = kobj;
994
995 return bo;
996 }
997
998 static void free_bd_holder(struct bd_holder *bo)
999 {
1000 kfree(bo);
1001 }
1002
1003 /**
1004 * find_bd_holder - find matching struct bd_holder from the block device
1005 *
1006 * @bdev: struct block device to be searched
1007 * @bo: target struct bd_holder
1008 *
1009 * Returns matching entry with @bo in @bdev->bd_holder_list.
1010 * If found, increment the reference count and return the pointer.
1011 * If not found, returns NULL.
1012 */
1013 static struct bd_holder *find_bd_holder(struct block_device *bdev,
1014 struct bd_holder *bo)
1015 {
1016 struct bd_holder *tmp;
1017
1018 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
1019 if (tmp->sdir == bo->sdir) {
1020 tmp->count++;
1021 return tmp;
1022 }
1023
1024 return NULL;
1025 }
1026
1027 /**
1028 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
1029 *
1030 * @bdev: block device to be bd_claimed
1031 * @bo: preallocated and initialized by alloc_bd_holder()
1032 *
1033 * Add @bo to @bdev->bd_holder_list, create symlinks.
1034 *
1035 * Returns 0 if symlinks are created.
1036 * Returns -ve if something fails.
1037 */
1038 static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
1039 {
1040 int err;
1041
1042 if (!bo)
1043 return -EINVAL;
1044
1045 if (!bd_holder_grab_dirs(bdev, bo))
1046 return -EBUSY;
1047
1048 err = add_symlink(bo->sdir, bo->sdev);
1049 if (err)
1050 return err;
1051
1052 err = add_symlink(bo->hdir, bo->hdev);
1053 if (err) {
1054 del_symlink(bo->sdir, bo->sdev);
1055 return err;
1056 }
1057
1058 list_add_tail(&bo->list, &bdev->bd_holder_list);
1059 return 0;
1060 }
1061
1062 /**
1063 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
1064 *
1065 * @bdev: block device to be bd_claimed
1066 * @kobj: holder's kobject
1067 *
1068 * If there is matching entry with @kobj in @bdev->bd_holder_list
1069 * and no other bd_claim() from the same kobject,
1070 * remove the struct bd_holder from the list, delete symlinks for it.
1071 *
1072 * Returns a pointer to the struct bd_holder when it's removed from the list
1073 * and ready to be freed.
1074 * Returns NULL if matching claim isn't found or there is other bd_claim()
1075 * by the same kobject.
1076 */
1077 static struct bd_holder *del_bd_holder(struct block_device *bdev,
1078 struct kobject *kobj)
1079 {
1080 struct bd_holder *bo;
1081
1082 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
1083 if (bo->sdir == kobj) {
1084 bo->count--;
1085 BUG_ON(bo->count < 0);
1086 if (!bo->count) {
1087 list_del(&bo->list);
1088 del_symlink(bo->sdir, bo->sdev);
1089 del_symlink(bo->hdir, bo->hdev);
1090 bd_holder_release_dirs(bo);
1091 return bo;
1092 }
1093 break;
1094 }
1095 }
1096
1097 return NULL;
1098 }
1099
1100 /**
1101 * bd_claim_by_kobject - bd_claim() with additional kobject signature
1102 *
1103 * @bdev: block device to be claimed
1104 * @holder: holder's signature
1105 * @kobj: holder's kobject
1106 *
1107 * Do bd_claim() and if it succeeds, create sysfs symlinks between
1108 * the bdev and the holder's kobject.
1109 * Use bd_release_from_kobject() when relesing the claimed bdev.
1110 *
1111 * Returns 0 on success. (same as bd_claim())
1112 * Returns errno on failure.
1113 */
1114 static int bd_claim_by_kobject(struct block_device *bdev, void *holder,
1115 struct kobject *kobj)
1116 {
1117 int err;
1118 struct bd_holder *bo, *found;
1119
1120 if (!kobj)
1121 return -EINVAL;
1122
1123 bo = alloc_bd_holder(kobj);
1124 if (!bo)
1125 return -ENOMEM;
1126
1127 mutex_lock(&bdev->bd_mutex);
1128
1129 err = bd_claim(bdev, holder);
1130 if (err)
1131 goto fail;
1132
1133 found = find_bd_holder(bdev, bo);
1134 if (found)
1135 goto fail;
1136
1137 err = add_bd_holder(bdev, bo);
1138 if (err)
1139 bd_release(bdev);
1140 else
1141 bo = NULL;
1142 fail:
1143 mutex_unlock(&bdev->bd_mutex);
1144 free_bd_holder(bo);
1145 return err;
1146 }
1147
1148 /**
1149 * bd_release_from_kobject - bd_release() with additional kobject signature
1150 *
1151 * @bdev: block device to be released
1152 * @kobj: holder's kobject
1153 *
1154 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1155 */
1156 static void bd_release_from_kobject(struct block_device *bdev,
1157 struct kobject *kobj)
1158 {
1159 if (!kobj)
1160 return;
1161
1162 mutex_lock(&bdev->bd_mutex);
1163 bd_release(bdev);
1164 free_bd_holder(del_bd_holder(bdev, kobj));
1165 mutex_unlock(&bdev->bd_mutex);
1166 }
1167
1168 /**
1169 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1170 *
1171 * @bdev: block device to be claimed
1172 * @holder: holder's signature
1173 * @disk: holder's gendisk
1174 *
1175 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1176 */
1177 int bd_claim_by_disk(struct block_device *bdev, void *holder,
1178 struct gendisk *disk)
1179 {
1180 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir));
1181 }
1182 EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1183
1184 /**
1185 * bd_release_from_disk - wrapper function for bd_release_from_kobject()
1186 *
1187 * @bdev: block device to be claimed
1188 * @disk: holder's gendisk
1189 *
1190 * Call bd_release_from_kobject() and put @disk->slave_dir.
1191 */
1192 void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1193 {
1194 bd_release_from_kobject(bdev, disk->slave_dir);
1195 kobject_put(disk->slave_dir);
1196 }
1197 EXPORT_SYMBOL_GPL(bd_release_from_disk);
1198 #endif
1199
1200 /*
1201 * Tries to open block device by device number. Use it ONLY if you
1202 * really do not have anything better - i.e. when you are behind a
1203 * truly sucky interface and all you are given is a device number. _Never_
1204 * to be used for internal purposes. If you ever need it - reconsider
1205 * your API.
1206 */
1207 struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1208 {
1209 struct block_device *bdev = bdget(dev);
1210 int err = -ENOMEM;
1211 if (bdev)
1212 err = blkdev_get(bdev, mode);
1213 return err ? ERR_PTR(err) : bdev;
1214 }
1215
1216 EXPORT_SYMBOL(open_by_devnum);
1217
1218 /**
1219 * flush_disk - invalidates all buffer-cache entries on a disk
1220 *
1221 * @bdev: struct block device to be flushed
1222 *
1223 * Invalidates all buffer-cache entries on a disk. It should be called
1224 * when a disk has been changed -- either by a media change or online
1225 * resize.
1226 */
1227 static void flush_disk(struct block_device *bdev)
1228 {
1229 if (__invalidate_device(bdev)) {
1230 char name[BDEVNAME_SIZE] = "";
1231
1232 if (bdev->bd_disk)
1233 disk_name(bdev->bd_disk, 0, name);
1234 printk(KERN_WARNING "VFS: busy inodes on changed media or "
1235 "resized disk %s\n", name);
1236 }
1237
1238 if (!bdev->bd_disk)
1239 return;
1240 if (disk_partitionable(bdev->bd_disk))
1241 bdev->bd_invalidated = 1;
1242 }
1243
1244 /**
1245 * check_disk_size_change - checks for disk size change and adjusts bdev size.
1246 * @disk: struct gendisk to check
1247 * @bdev: struct bdev to adjust.
1248 *
1249 * This routine checks to see if the bdev size does not match the disk size
1250 * and adjusts it if it differs.
1251 */
1252 void check_disk_size_change(struct gendisk *disk, struct block_device *bdev)
1253 {
1254 loff_t disk_size, bdev_size;
1255
1256 disk_size = (loff_t)get_capacity(disk) << 9;
1257 bdev_size = i_size_read(bdev->bd_inode);
1258 if (disk_size != bdev_size) {
1259 char name[BDEVNAME_SIZE];
1260
1261 disk_name(disk, 0, name);
1262 printk(KERN_INFO
1263 "%s: detected capacity change from %lld to %lld\n",
1264 name, bdev_size, disk_size);
1265 i_size_write(bdev->bd_inode, disk_size);
1266 flush_disk(bdev);
1267 }
1268 }
1269 EXPORT_SYMBOL(check_disk_size_change);
1270
1271 /**
1272 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
1273 * @disk: struct gendisk to be revalidated
1274 *
1275 * This routine is a wrapper for lower-level driver's revalidate_disk
1276 * call-backs. It is used to do common pre and post operations needed
1277 * for all revalidate_disk operations.
1278 */
1279 int revalidate_disk(struct gendisk *disk)
1280 {
1281 struct block_device *bdev;
1282 int ret = 0;
1283
1284 if (disk->fops->revalidate_disk)
1285 ret = disk->fops->revalidate_disk(disk);
1286
1287 bdev = bdget_disk(disk, 0);
1288 if (!bdev)
1289 return ret;
1290
1291 mutex_lock(&bdev->bd_mutex);
1292 check_disk_size_change(disk, bdev);
1293 mutex_unlock(&bdev->bd_mutex);
1294 bdput(bdev);
1295 return ret;
1296 }
1297 EXPORT_SYMBOL(revalidate_disk);
1298
1299 /*
1300 * This routine checks whether a removable media has been changed,
1301 * and invalidates all buffer-cache-entries in that case. This
1302 * is a relatively slow routine, so we have to try to minimize using
1303 * it. Thus it is called only upon a 'mount' or 'open'. This
1304 * is the best way of combining speed and utility, I think.
1305 * People changing diskettes in the middle of an operation deserve
1306 * to lose :-)
1307 */
1308 int check_disk_change(struct block_device *bdev)
1309 {
1310 struct gendisk *disk = bdev->bd_disk;
1311 const struct block_device_operations *bdops = disk->fops;
1312
1313 if (!bdops->media_changed)
1314 return 0;
1315 if (!bdops->media_changed(bdev->bd_disk))
1316 return 0;
1317
1318 flush_disk(bdev);
1319 if (bdops->revalidate_disk)
1320 bdops->revalidate_disk(bdev->bd_disk);
1321 return 1;
1322 }
1323
1324 EXPORT_SYMBOL(check_disk_change);
1325
1326 void bd_set_size(struct block_device *bdev, loff_t size)
1327 {
1328 unsigned bsize = bdev_logical_block_size(bdev);
1329
1330 bdev->bd_inode->i_size = size;
1331 while (bsize < PAGE_CACHE_SIZE) {
1332 if (size & bsize)
1333 break;
1334 bsize <<= 1;
1335 }
1336 bdev->bd_block_size = bsize;
1337 bdev->bd_inode->i_blkbits = blksize_bits(bsize);
1338 }
1339 EXPORT_SYMBOL(bd_set_size);
1340
1341 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part);
1342
1343 /*
1344 * bd_mutex locking:
1345 *
1346 * mutex_lock(part->bd_mutex)
1347 * mutex_lock_nested(whole->bd_mutex, 1)
1348 */
1349
1350 static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1351 {
1352 struct gendisk *disk;
1353 int ret;
1354 int partno;
1355 int perm = 0;
1356
1357 if (mode & FMODE_READ)
1358 perm |= MAY_READ;
1359 if (mode & FMODE_WRITE)
1360 perm |= MAY_WRITE;
1361 /*
1362 * hooks: /n/, see "layering violations".
1363 */
1364 if (!for_part) {
1365 ret = devcgroup_inode_permission(bdev->bd_inode, perm);
1366 if (ret != 0) {
1367 bdput(bdev);
1368 return ret;
1369 }
1370 }
1371
1372 restart:
1373
1374 ret = -ENXIO;
1375 disk = get_gendisk(bdev->bd_dev, &partno);
1376 if (!disk)
1377 goto out;
1378
1379 mutex_lock_nested(&bdev->bd_mutex, for_part);
1380 if (!bdev->bd_openers) {
1381 bdev->bd_disk = disk;
1382 bdev->bd_contains = bdev;
1383 if (!partno) {
1384 struct backing_dev_info *bdi;
1385
1386 ret = -ENXIO;
1387 bdev->bd_part = disk_get_part(disk, partno);
1388 if (!bdev->bd_part)
1389 goto out_clear;
1390
1391 if (disk->fops->open) {
1392 ret = disk->fops->open(bdev, mode);
1393 if (ret == -ERESTARTSYS) {
1394 /* Lost a race with 'disk' being
1395 * deleted, try again.
1396 * See md.c
1397 */
1398 disk_put_part(bdev->bd_part);
1399 bdev->bd_part = NULL;
1400 module_put(disk->fops->owner);
1401 put_disk(disk);
1402 bdev->bd_disk = NULL;
1403 mutex_unlock(&bdev->bd_mutex);
1404 goto restart;
1405 }
1406 if (ret)
1407 goto out_clear;
1408 }
1409 if (!bdev->bd_openers) {
1410 bd_set_size(bdev,(loff_t)get_capacity(disk)<<9);
1411 bdi = blk_get_backing_dev_info(bdev);
1412 if (bdi == NULL)
1413 bdi = &default_backing_dev_info;
1414 bdev_inode_switch_bdi(bdev->bd_inode, bdi);
1415 }
1416 if (bdev->bd_invalidated)
1417 rescan_partitions(disk, bdev);
1418 } else {
1419 struct block_device *whole;
1420 whole = bdget_disk(disk, 0);
1421 ret = -ENOMEM;
1422 if (!whole)
1423 goto out_clear;
1424 BUG_ON(for_part);
1425 ret = __blkdev_get(whole, mode, 1);
1426 if (ret)
1427 goto out_clear;
1428 bdev->bd_contains = whole;
1429 bdev_inode_switch_bdi(bdev->bd_inode,
1430 whole->bd_inode->i_data.backing_dev_info);
1431 bdev->bd_part = disk_get_part(disk, partno);
1432 if (!(disk->flags & GENHD_FL_UP) ||
1433 !bdev->bd_part || !bdev->bd_part->nr_sects) {
1434 ret = -ENXIO;
1435 goto out_clear;
1436 }
1437 bd_set_size(bdev, (loff_t)bdev->bd_part->nr_sects << 9);
1438 }
1439 } else {
1440 module_put(disk->fops->owner);
1441 put_disk(disk);
1442 disk = NULL;
1443 if (bdev->bd_contains == bdev) {
1444 if (bdev->bd_disk->fops->open) {
1445 ret = bdev->bd_disk->fops->open(bdev, mode);
1446 if (ret)
1447 goto out_unlock_bdev;
1448 }
1449 if (bdev->bd_invalidated)
1450 rescan_partitions(bdev->bd_disk, bdev);
1451 }
1452 }
1453 bdev->bd_openers++;
1454 if (for_part)
1455 bdev->bd_part_count++;
1456 mutex_unlock(&bdev->bd_mutex);
1457 return 0;
1458
1459 out_clear:
1460 disk_put_part(bdev->bd_part);
1461 bdev->bd_disk = NULL;
1462 bdev->bd_part = NULL;
1463 bdev_inode_switch_bdi(bdev->bd_inode, &default_backing_dev_info);
1464 if (bdev != bdev->bd_contains)
1465 __blkdev_put(bdev->bd_contains, mode, 1);
1466 bdev->bd_contains = NULL;
1467 out_unlock_bdev:
1468 mutex_unlock(&bdev->bd_mutex);
1469 out:
1470 if (disk)
1471 module_put(disk->fops->owner);
1472 put_disk(disk);
1473 bdput(bdev);
1474
1475 return ret;
1476 }
1477
1478 int blkdev_get(struct block_device *bdev, fmode_t mode)
1479 {
1480 return __blkdev_get(bdev, mode, 0);
1481 }
1482 EXPORT_SYMBOL(blkdev_get);
1483
1484 static int blkdev_open(struct inode * inode, struct file * filp)
1485 {
1486 struct block_device *whole = NULL;
1487 struct block_device *bdev;
1488 int res;
1489
1490 /*
1491 * Preserve backwards compatibility and allow large file access
1492 * even if userspace doesn't ask for it explicitly. Some mkfs
1493 * binary needs it. We might want to drop this workaround
1494 * during an unstable branch.
1495 */
1496 filp->f_flags |= O_LARGEFILE;
1497
1498 if (filp->f_flags & O_NDELAY)
1499 filp->f_mode |= FMODE_NDELAY;
1500 if (filp->f_flags & O_EXCL)
1501 filp->f_mode |= FMODE_EXCL;
1502 if ((filp->f_flags & O_ACCMODE) == 3)
1503 filp->f_mode |= FMODE_WRITE_IOCTL;
1504
1505 bdev = bd_acquire(inode);
1506 if (bdev == NULL)
1507 return -ENOMEM;
1508
1509 if (filp->f_mode & FMODE_EXCL) {
1510 whole = bd_start_claiming(bdev, filp);
1511 if (IS_ERR(whole)) {
1512 bdput(bdev);
1513 return PTR_ERR(whole);
1514 }
1515 }
1516
1517 filp->f_mapping = bdev->bd_inode->i_mapping;
1518
1519 res = blkdev_get(bdev, filp->f_mode);
1520
1521 if (whole) {
1522 if (res == 0)
1523 bd_finish_claiming(bdev, whole, filp);
1524 else
1525 bd_abort_claiming(whole, filp);
1526 }
1527
1528 return res;
1529 }
1530
1531 static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1532 {
1533 int ret = 0;
1534 struct gendisk *disk = bdev->bd_disk;
1535 struct block_device *victim = NULL;
1536
1537 mutex_lock_nested(&bdev->bd_mutex, for_part);
1538 if (for_part)
1539 bdev->bd_part_count--;
1540
1541 if (!--bdev->bd_openers) {
1542 sync_blockdev(bdev);
1543 kill_bdev(bdev);
1544 }
1545 if (bdev->bd_contains == bdev) {
1546 if (disk->fops->release)
1547 ret = disk->fops->release(disk, mode);
1548 }
1549 if (!bdev->bd_openers) {
1550 struct module *owner = disk->fops->owner;
1551
1552 put_disk(disk);
1553 module_put(owner);
1554 disk_put_part(bdev->bd_part);
1555 bdev->bd_part = NULL;
1556 bdev->bd_disk = NULL;
1557 bdev_inode_switch_bdi(bdev->bd_inode,
1558 &default_backing_dev_info);
1559 if (bdev != bdev->bd_contains)
1560 victim = bdev->bd_contains;
1561 bdev->bd_contains = NULL;
1562 }
1563 mutex_unlock(&bdev->bd_mutex);
1564 bdput(bdev);
1565 if (victim)
1566 __blkdev_put(victim, mode, 1);
1567 return ret;
1568 }
1569
1570 int blkdev_put(struct block_device *bdev, fmode_t mode)
1571 {
1572 return __blkdev_put(bdev, mode, 0);
1573 }
1574 EXPORT_SYMBOL(blkdev_put);
1575
1576 static int blkdev_close(struct inode * inode, struct file * filp)
1577 {
1578 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1579 if (bdev->bd_holder == filp)
1580 bd_release(bdev);
1581 return blkdev_put(bdev, filp->f_mode);
1582 }
1583
1584 static long block_ioctl(struct file *file, unsigned cmd, unsigned long arg)
1585 {
1586 struct block_device *bdev = I_BDEV(file->f_mapping->host);
1587 fmode_t mode = file->f_mode;
1588
1589 /*
1590 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1591 * to updated it before every ioctl.
1592 */
1593 if (file->f_flags & O_NDELAY)
1594 mode |= FMODE_NDELAY;
1595 else
1596 mode &= ~FMODE_NDELAY;
1597
1598 return blkdev_ioctl(bdev, mode, cmd, arg);
1599 }
1600
1601 /*
1602 * Write data to the block device. Only intended for the block device itself
1603 * and the raw driver which basically is a fake block device.
1604 *
1605 * Does not take i_mutex for the write and thus is not for general purpose
1606 * use.
1607 */
1608 ssize_t blkdev_aio_write(struct kiocb *iocb, const struct iovec *iov,
1609 unsigned long nr_segs, loff_t pos)
1610 {
1611 struct file *file = iocb->ki_filp;
1612 ssize_t ret;
1613
1614 BUG_ON(iocb->ki_pos != pos);
1615
1616 ret = __generic_file_aio_write(iocb, iov, nr_segs, &iocb->ki_pos);
1617 if (ret > 0 || ret == -EIOCBQUEUED) {
1618 ssize_t err;
1619
1620 err = generic_write_sync(file, pos, ret);
1621 if (err < 0 && ret > 0)
1622 ret = err;
1623 }
1624 return ret;
1625 }
1626 EXPORT_SYMBOL_GPL(blkdev_aio_write);
1627
1628 /*
1629 * Try to release a page associated with block device when the system
1630 * is under memory pressure.
1631 */
1632 static int blkdev_releasepage(struct page *page, gfp_t wait)
1633 {
1634 struct super_block *super = BDEV_I(page->mapping->host)->bdev.bd_super;
1635
1636 if (super && super->s_op->bdev_try_to_free_page)
1637 return super->s_op->bdev_try_to_free_page(super, page, wait);
1638
1639 return try_to_free_buffers(page);
1640 }
1641
1642 static const struct address_space_operations def_blk_aops = {
1643 .readpage = blkdev_readpage,
1644 .writepage = blkdev_writepage,
1645 .sync_page = block_sync_page,
1646 .write_begin = blkdev_write_begin,
1647 .write_end = blkdev_write_end,
1648 .writepages = generic_writepages,
1649 .releasepage = blkdev_releasepage,
1650 .direct_IO = blkdev_direct_IO,
1651 };
1652
1653 const struct file_operations def_blk_fops = {
1654 .open = blkdev_open,
1655 .release = blkdev_close,
1656 .llseek = block_llseek,
1657 .read = do_sync_read,
1658 .write = do_sync_write,
1659 .aio_read = generic_file_aio_read,
1660 .aio_write = blkdev_aio_write,
1661 .mmap = generic_file_mmap,
1662 .fsync = blkdev_fsync,
1663 .unlocked_ioctl = block_ioctl,
1664 #ifdef CONFIG_COMPAT
1665 .compat_ioctl = compat_blkdev_ioctl,
1666 #endif
1667 .splice_read = generic_file_splice_read,
1668 .splice_write = generic_file_splice_write,
1669 };
1670
1671 int ioctl_by_bdev(struct block_device *bdev, unsigned cmd, unsigned long arg)
1672 {
1673 int res;
1674 mm_segment_t old_fs = get_fs();
1675 set_fs(KERNEL_DS);
1676 res = blkdev_ioctl(bdev, 0, cmd, arg);
1677 set_fs(old_fs);
1678 return res;
1679 }
1680
1681 EXPORT_SYMBOL(ioctl_by_bdev);
1682
1683 /**
1684 * lookup_bdev - lookup a struct block_device by name
1685 * @pathname: special file representing the block device
1686 *
1687 * Get a reference to the blockdevice at @pathname in the current
1688 * namespace if possible and return it. Return ERR_PTR(error)
1689 * otherwise.
1690 */
1691 struct block_device *lookup_bdev(const char *pathname)
1692 {
1693 struct block_device *bdev;
1694 struct inode *inode;
1695 struct path path;
1696 int error;
1697
1698 if (!pathname || !*pathname)
1699 return ERR_PTR(-EINVAL);
1700
1701 error = kern_path(pathname, LOOKUP_FOLLOW, &path);
1702 if (error)
1703 return ERR_PTR(error);
1704
1705 inode = path.dentry->d_inode;
1706 error = -ENOTBLK;
1707 if (!S_ISBLK(inode->i_mode))
1708 goto fail;
1709 error = -EACCES;
1710 if (path.mnt->mnt_flags & MNT_NODEV)
1711 goto fail;
1712 error = -ENOMEM;
1713 bdev = bd_acquire(inode);
1714 if (!bdev)
1715 goto fail;
1716 out:
1717 path_put(&path);
1718 return bdev;
1719 fail:
1720 bdev = ERR_PTR(error);
1721 goto out;
1722 }
1723 EXPORT_SYMBOL(lookup_bdev);
1724
1725 /**
1726 * open_bdev_exclusive - open a block device by name and set it up for use
1727 *
1728 * @path: special file representing the block device
1729 * @mode: FMODE_... combination to pass be used
1730 * @holder: owner for exclusion
1731 *
1732 * Open the blockdevice described by the special file at @path, claim it
1733 * for the @holder.
1734 */
1735 struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1736 {
1737 struct block_device *bdev, *whole;
1738 int error;
1739
1740 bdev = lookup_bdev(path);
1741 if (IS_ERR(bdev))
1742 return bdev;
1743
1744 whole = bd_start_claiming(bdev, holder);
1745 if (IS_ERR(whole)) {
1746 bdput(bdev);
1747 return whole;
1748 }
1749
1750 error = blkdev_get(bdev, mode);
1751 if (error)
1752 goto out_abort_claiming;
1753
1754 error = -EACCES;
1755 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1756 goto out_blkdev_put;
1757
1758 bd_finish_claiming(bdev, whole, holder);
1759 return bdev;
1760
1761 out_blkdev_put:
1762 blkdev_put(bdev, mode);
1763 out_abort_claiming:
1764 bd_abort_claiming(whole, holder);
1765 return ERR_PTR(error);
1766 }
1767
1768 EXPORT_SYMBOL(open_bdev_exclusive);
1769
1770 /**
1771 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1772 *
1773 * @bdev: blockdevice to close
1774 * @mode: mode, must match that used to open.
1775 *
1776 * This is the counterpart to open_bdev_exclusive().
1777 */
1778 void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1779 {
1780 bd_release(bdev);
1781 blkdev_put(bdev, mode);
1782 }
1783
1784 EXPORT_SYMBOL(close_bdev_exclusive);
1785
1786 int __invalidate_device(struct block_device *bdev)
1787 {
1788 struct super_block *sb = get_super(bdev);
1789 int res = 0;
1790
1791 if (sb) {
1792 /*
1793 * no need to lock the super, get_super holds the
1794 * read mutex so the filesystem cannot go away
1795 * under us (->put_super runs with the write lock
1796 * hold).
1797 */
1798 shrink_dcache_sb(sb);
1799 res = invalidate_inodes(sb);
1800 drop_super(sb);
1801 }
1802 invalidate_bdev(bdev);
1803 return res;
1804 }
1805 EXPORT_SYMBOL(__invalidate_device);