4 * Copyright (C) 1991, 1992 Linus Torvalds
5 * Copyright (C) 2001 Andrea Arcangeli <andrea@suse.de> SuSE
8 #include <linux/init.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>
32 struct block_device bdev
;
33 struct inode vfs_inode
;
36 static const struct address_space_operations def_blk_aops
;
38 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
40 return container_of(inode
, struct bdev_inode
, vfs_inode
);
43 inline struct block_device
*I_BDEV(struct inode
*inode
)
45 return &BDEV_I(inode
)->bdev
;
48 EXPORT_SYMBOL(I_BDEV
);
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
55 static void bdev_inode_switch_bdi(struct inode
*inode
,
56 struct backing_dev_info
*dst
)
58 spin_lock(&inode_wb_list_lock
);
59 spin_lock(&inode
->i_lock
);
60 inode
->i_data
.backing_dev_info
= dst
;
61 if (inode
->i_state
& I_DIRTY
)
62 list_move(&inode
->i_wb_list
, &dst
->wb
.b_dirty
);
63 spin_unlock(&inode
->i_lock
);
64 spin_unlock(&inode_wb_list_lock
);
67 static sector_t
max_block(struct block_device
*bdev
)
69 sector_t retval
= ~((sector_t
)0);
70 loff_t sz
= i_size_read(bdev
->bd_inode
);
73 unsigned int size
= block_size(bdev
);
74 unsigned int sizebits
= blksize_bits(size
);
75 retval
= (sz
>> sizebits
);
80 /* Kill _all_ buffers and pagecache , dirty or not.. */
81 static void kill_bdev(struct block_device
*bdev
)
83 if (bdev
->bd_inode
->i_mapping
->nrpages
== 0)
86 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
89 int set_blocksize(struct block_device
*bdev
, int size
)
91 /* Size must be a power of two, and between 512 and PAGE_SIZE */
92 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
95 /* Size cannot be smaller than the size supported by the device */
96 if (size
< bdev_logical_block_size(bdev
))
99 /* Don't change the size if it is same as current */
100 if (bdev
->bd_block_size
!= size
) {
102 bdev
->bd_block_size
= size
;
103 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
109 EXPORT_SYMBOL(set_blocksize
);
111 int sb_set_blocksize(struct super_block
*sb
, int size
)
113 if (set_blocksize(sb
->s_bdev
, size
))
115 /* If we get here, we know size is power of two
116 * and it's value is between 512 and PAGE_SIZE */
117 sb
->s_blocksize
= size
;
118 sb
->s_blocksize_bits
= blksize_bits(size
);
119 return sb
->s_blocksize
;
122 EXPORT_SYMBOL(sb_set_blocksize
);
124 int sb_min_blocksize(struct super_block
*sb
, int size
)
126 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
129 return sb_set_blocksize(sb
, size
);
132 EXPORT_SYMBOL(sb_min_blocksize
);
135 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
136 struct buffer_head
*bh
, int create
)
138 if (iblock
>= max_block(I_BDEV(inode
))) {
143 * for reads, we're just trying to fill a partial page.
144 * return a hole, they will have to call get_block again
145 * before they can fill it, and they will get -EIO at that
150 bh
->b_bdev
= I_BDEV(inode
);
151 bh
->b_blocknr
= iblock
;
152 set_buffer_mapped(bh
);
157 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
158 struct buffer_head
*bh
, int create
)
160 sector_t end_block
= max_block(I_BDEV(inode
));
161 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
163 if ((iblock
+ max_blocks
) > end_block
) {
164 max_blocks
= end_block
- iblock
;
165 if ((long)max_blocks
<= 0) {
167 return -EIO
; /* write fully beyond EOF */
169 * It is a read which is fully beyond EOF. We return
170 * a !buffer_mapped buffer
176 bh
->b_bdev
= I_BDEV(inode
);
177 bh
->b_blocknr
= iblock
;
178 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
180 set_buffer_mapped(bh
);
185 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
186 loff_t offset
, unsigned long nr_segs
)
188 struct file
*file
= iocb
->ki_filp
;
189 struct inode
*inode
= file
->f_mapping
->host
;
191 return __blockdev_direct_IO(rw
, iocb
, inode
, I_BDEV(inode
), iov
, offset
,
192 nr_segs
, blkdev_get_blocks
, NULL
, NULL
, 0);
195 int __sync_blockdev(struct block_device
*bdev
, int wait
)
200 return filemap_flush(bdev
->bd_inode
->i_mapping
);
201 return filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
205 * Write out and wait upon all the dirty data associated with a block
206 * device via its mapping. Does not take the superblock lock.
208 int sync_blockdev(struct block_device
*bdev
)
210 return __sync_blockdev(bdev
, 1);
212 EXPORT_SYMBOL(sync_blockdev
);
215 * Write out and wait upon all dirty data associated with this
216 * device. Filesystem data as well as the underlying block
217 * device. Takes the superblock lock.
219 int fsync_bdev(struct block_device
*bdev
)
221 struct super_block
*sb
= get_super(bdev
);
223 int res
= sync_filesystem(sb
);
227 return sync_blockdev(bdev
);
229 EXPORT_SYMBOL(fsync_bdev
);
232 * freeze_bdev -- lock a filesystem and force it into a consistent state
233 * @bdev: blockdevice to lock
235 * If a superblock is found on this device, we take the s_umount semaphore
236 * on it to make sure nobody unmounts until the snapshot creation is done.
237 * The reference counter (bd_fsfreeze_count) guarantees that only the last
238 * unfreeze process can unfreeze the frozen filesystem actually when multiple
239 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
240 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
243 struct super_block
*freeze_bdev(struct block_device
*bdev
)
245 struct super_block
*sb
;
248 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
249 if (++bdev
->bd_fsfreeze_count
> 1) {
251 * We don't even need to grab a reference - the first call
252 * to freeze_bdev grab an active reference and only the last
253 * thaw_bdev drops it.
255 sb
= get_super(bdev
);
257 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
261 sb
= get_active_super(bdev
);
264 error
= freeze_super(sb
);
266 deactivate_super(sb
);
267 bdev
->bd_fsfreeze_count
--;
268 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
269 return ERR_PTR(error
);
271 deactivate_super(sb
);
274 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
275 return sb
; /* thaw_bdev releases s->s_umount */
277 EXPORT_SYMBOL(freeze_bdev
);
280 * thaw_bdev -- unlock filesystem
281 * @bdev: blockdevice to unlock
282 * @sb: associated superblock
284 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
286 int thaw_bdev(struct block_device
*bdev
, struct super_block
*sb
)
290 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
291 if (!bdev
->bd_fsfreeze_count
)
295 if (--bdev
->bd_fsfreeze_count
> 0)
301 error
= thaw_super(sb
);
303 bdev
->bd_fsfreeze_count
++;
304 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
308 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
311 EXPORT_SYMBOL(thaw_bdev
);
313 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
315 return block_write_full_page(page
, blkdev_get_block
, wbc
);
318 static int blkdev_readpage(struct file
* file
, struct page
* page
)
320 return block_read_full_page(page
, blkdev_get_block
);
323 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
324 loff_t pos
, unsigned len
, unsigned flags
,
325 struct page
**pagep
, void **fsdata
)
327 return block_write_begin(mapping
, pos
, len
, flags
, pagep
,
331 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
332 loff_t pos
, unsigned len
, unsigned copied
,
333 struct page
*page
, void *fsdata
)
336 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
339 page_cache_release(page
);
346 * for a block special file file->f_path.dentry->d_inode->i_size is zero
347 * so we compute the size by hand (just as in block_read/write above)
349 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
351 struct inode
*bd_inode
= file
->f_mapping
->host
;
355 mutex_lock(&bd_inode
->i_mutex
);
356 size
= i_size_read(bd_inode
);
363 offset
+= file
->f_pos
;
366 if (offset
>= 0 && offset
<= size
) {
367 if (offset
!= file
->f_pos
) {
368 file
->f_pos
= offset
;
372 mutex_unlock(&bd_inode
->i_mutex
);
376 int blkdev_fsync(struct file
*filp
, int datasync
)
378 struct inode
*bd_inode
= filp
->f_mapping
->host
;
379 struct block_device
*bdev
= I_BDEV(bd_inode
);
383 * There is no need to serialise calls to blkdev_issue_flush with
384 * i_mutex and doing so causes performance issues with concurrent
385 * O_SYNC writers to a block device.
387 mutex_unlock(&bd_inode
->i_mutex
);
389 error
= blkdev_issue_flush(bdev
, GFP_KERNEL
, NULL
);
390 if (error
== -EOPNOTSUPP
)
393 mutex_lock(&bd_inode
->i_mutex
);
397 EXPORT_SYMBOL(blkdev_fsync
);
403 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
404 static struct kmem_cache
* bdev_cachep __read_mostly
;
406 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
408 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
411 return &ei
->vfs_inode
;
414 static void bdev_i_callback(struct rcu_head
*head
)
416 struct inode
*inode
= container_of(head
, struct inode
, i_rcu
);
417 struct bdev_inode
*bdi
= BDEV_I(inode
);
419 INIT_LIST_HEAD(&inode
->i_dentry
);
420 kmem_cache_free(bdev_cachep
, bdi
);
423 static void bdev_destroy_inode(struct inode
*inode
)
425 call_rcu(&inode
->i_rcu
, bdev_i_callback
);
428 static void init_once(void *foo
)
430 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
431 struct block_device
*bdev
= &ei
->bdev
;
433 memset(bdev
, 0, sizeof(*bdev
));
434 mutex_init(&bdev
->bd_mutex
);
435 INIT_LIST_HEAD(&bdev
->bd_inodes
);
436 INIT_LIST_HEAD(&bdev
->bd_list
);
438 INIT_LIST_HEAD(&bdev
->bd_holder_disks
);
440 inode_init_once(&ei
->vfs_inode
);
441 /* Initialize mutex for freeze. */
442 mutex_init(&bdev
->bd_fsfreeze_mutex
);
445 static inline void __bd_forget(struct inode
*inode
)
447 list_del_init(&inode
->i_devices
);
448 inode
->i_bdev
= NULL
;
449 inode
->i_mapping
= &inode
->i_data
;
452 static void bdev_evict_inode(struct inode
*inode
)
454 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
456 truncate_inode_pages(&inode
->i_data
, 0);
457 invalidate_inode_buffers(inode
); /* is it needed here? */
458 end_writeback(inode
);
459 spin_lock(&bdev_lock
);
460 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
461 __bd_forget(list_entry(p
, struct inode
, i_devices
));
463 list_del_init(&bdev
->bd_list
);
464 spin_unlock(&bdev_lock
);
467 static const struct super_operations bdev_sops
= {
468 .statfs
= simple_statfs
,
469 .alloc_inode
= bdev_alloc_inode
,
470 .destroy_inode
= bdev_destroy_inode
,
471 .drop_inode
= generic_delete_inode
,
472 .evict_inode
= bdev_evict_inode
,
475 static struct dentry
*bd_mount(struct file_system_type
*fs_type
,
476 int flags
, const char *dev_name
, void *data
)
478 return mount_pseudo(fs_type
, "bdev:", &bdev_sops
, NULL
, 0x62646576);
481 static struct file_system_type bd_type
= {
484 .kill_sb
= kill_anon_super
,
487 struct super_block
*blockdev_superblock __read_mostly
;
489 void __init
bdev_cache_init(void)
492 struct vfsmount
*bd_mnt
;
494 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
495 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
496 SLAB_MEM_SPREAD
|SLAB_PANIC
),
498 err
= register_filesystem(&bd_type
);
500 panic("Cannot register bdev pseudo-fs");
501 bd_mnt
= kern_mount(&bd_type
);
503 panic("Cannot create bdev pseudo-fs");
505 * This vfsmount structure is only used to obtain the
506 * blockdev_superblock, so tell kmemleak not to report it.
508 kmemleak_not_leak(bd_mnt
);
509 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
513 * Most likely _very_ bad one - but then it's hardly critical for small
514 * /dev and can be fixed when somebody will need really large one.
515 * Keep in mind that it will be fed through icache hash function too.
517 static inline unsigned long hash(dev_t dev
)
519 return MAJOR(dev
)+MINOR(dev
);
522 static int bdev_test(struct inode
*inode
, void *data
)
524 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
527 static int bdev_set(struct inode
*inode
, void *data
)
529 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
533 static LIST_HEAD(all_bdevs
);
535 struct block_device
*bdget(dev_t dev
)
537 struct block_device
*bdev
;
540 inode
= iget5_locked(blockdev_superblock
, hash(dev
),
541 bdev_test
, bdev_set
, &dev
);
546 bdev
= &BDEV_I(inode
)->bdev
;
548 if (inode
->i_state
& I_NEW
) {
549 bdev
->bd_contains
= NULL
;
550 bdev
->bd_inode
= inode
;
551 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
552 bdev
->bd_part_count
= 0;
553 bdev
->bd_invalidated
= 0;
554 inode
->i_mode
= S_IFBLK
;
556 inode
->i_bdev
= bdev
;
557 inode
->i_data
.a_ops
= &def_blk_aops
;
558 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
559 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
560 spin_lock(&bdev_lock
);
561 list_add(&bdev
->bd_list
, &all_bdevs
);
562 spin_unlock(&bdev_lock
);
563 unlock_new_inode(inode
);
568 EXPORT_SYMBOL(bdget
);
571 * bdgrab -- Grab a reference to an already referenced block device
572 * @bdev: Block device to grab a reference to.
574 struct block_device
*bdgrab(struct block_device
*bdev
)
576 ihold(bdev
->bd_inode
);
580 long nr_blockdev_pages(void)
582 struct block_device
*bdev
;
584 spin_lock(&bdev_lock
);
585 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
586 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
588 spin_unlock(&bdev_lock
);
592 void bdput(struct block_device
*bdev
)
594 iput(bdev
->bd_inode
);
597 EXPORT_SYMBOL(bdput
);
599 static struct block_device
*bd_acquire(struct inode
*inode
)
601 struct block_device
*bdev
;
603 spin_lock(&bdev_lock
);
604 bdev
= inode
->i_bdev
;
606 ihold(bdev
->bd_inode
);
607 spin_unlock(&bdev_lock
);
610 spin_unlock(&bdev_lock
);
612 bdev
= bdget(inode
->i_rdev
);
614 spin_lock(&bdev_lock
);
615 if (!inode
->i_bdev
) {
617 * We take an additional reference to bd_inode,
618 * and it's released in clear_inode() of inode.
619 * So, we can access it via ->i_mapping always
622 ihold(bdev
->bd_inode
);
623 inode
->i_bdev
= bdev
;
624 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
625 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
627 spin_unlock(&bdev_lock
);
632 /* Call when you free inode */
634 void bd_forget(struct inode
*inode
)
636 struct block_device
*bdev
= NULL
;
638 spin_lock(&bdev_lock
);
640 if (!sb_is_blkdev_sb(inode
->i_sb
))
641 bdev
= inode
->i_bdev
;
644 spin_unlock(&bdev_lock
);
647 iput(bdev
->bd_inode
);
651 * bd_may_claim - test whether a block device can be claimed
652 * @bdev: block device of interest
653 * @whole: whole block device containing @bdev, may equal @bdev
654 * @holder: holder trying to claim @bdev
656 * Test whether @bdev can be claimed by @holder.
659 * spin_lock(&bdev_lock).
662 * %true if @bdev can be claimed, %false otherwise.
664 static bool bd_may_claim(struct block_device
*bdev
, struct block_device
*whole
,
667 if (bdev
->bd_holder
== holder
)
668 return true; /* already a holder */
669 else if (bdev
->bd_holder
!= NULL
)
670 return false; /* held by someone else */
671 else if (bdev
->bd_contains
== bdev
)
672 return true; /* is a whole device which isn't held */
674 else if (whole
->bd_holder
== bd_may_claim
)
675 return true; /* is a partition of a device that is being partitioned */
676 else if (whole
->bd_holder
!= NULL
)
677 return false; /* is a partition of a held device */
679 return true; /* is a partition of an un-held device */
683 * bd_prepare_to_claim - prepare to claim a block device
684 * @bdev: block device of interest
685 * @whole: the whole device containing @bdev, may equal @bdev
686 * @holder: holder trying to claim @bdev
688 * Prepare to claim @bdev. This function fails if @bdev is already
689 * claimed by another holder and waits if another claiming is in
690 * progress. This function doesn't actually claim. On successful
691 * return, the caller has ownership of bd_claiming and bd_holder[s].
694 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
698 * 0 if @bdev can be claimed, -EBUSY otherwise.
700 static int bd_prepare_to_claim(struct block_device
*bdev
,
701 struct block_device
*whole
, void *holder
)
704 /* if someone else claimed, fail */
705 if (!bd_may_claim(bdev
, whole
, holder
))
708 /* if claiming is already in progress, wait for it to finish */
709 if (whole
->bd_claiming
) {
710 wait_queue_head_t
*wq
= bit_waitqueue(&whole
->bd_claiming
, 0);
713 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
714 spin_unlock(&bdev_lock
);
716 finish_wait(wq
, &wait
);
717 spin_lock(&bdev_lock
);
726 * bd_start_claiming - start claiming a block device
727 * @bdev: block device of interest
728 * @holder: holder trying to claim @bdev
730 * @bdev is about to be opened exclusively. Check @bdev can be opened
731 * exclusively and mark that an exclusive open is in progress. Each
732 * successful call to this function must be matched with a call to
733 * either bd_finish_claiming() or bd_abort_claiming() (which do not
736 * This function is used to gain exclusive access to the block device
737 * without actually causing other exclusive open attempts to fail. It
738 * should be used when the open sequence itself requires exclusive
739 * access but may subsequently fail.
745 * Pointer to the block device containing @bdev on success, ERR_PTR()
748 static struct block_device
*bd_start_claiming(struct block_device
*bdev
,
751 struct gendisk
*disk
;
752 struct block_device
*whole
;
758 * @bdev might not have been initialized properly yet, look up
759 * and grab the outer block device the hard way.
761 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
763 return ERR_PTR(-ENXIO
);
766 * Normally, @bdev should equal what's returned from bdget_disk()
767 * if partno is 0; however, some drivers (floppy) use multiple
768 * bdev's for the same physical device and @bdev may be one of the
769 * aliases. Keep @bdev if partno is 0. This means claimer
770 * tracking is broken for those devices but it has always been that
774 whole
= bdget_disk(disk
, 0);
776 whole
= bdgrab(bdev
);
778 module_put(disk
->fops
->owner
);
781 return ERR_PTR(-ENOMEM
);
783 /* prepare to claim, if successful, mark claiming in progress */
784 spin_lock(&bdev_lock
);
786 err
= bd_prepare_to_claim(bdev
, whole
, holder
);
788 whole
->bd_claiming
= holder
;
789 spin_unlock(&bdev_lock
);
792 spin_unlock(&bdev_lock
);
799 struct bd_holder_disk
{
800 struct list_head list
;
801 struct gendisk
*disk
;
805 static struct bd_holder_disk
*bd_find_holder_disk(struct block_device
*bdev
,
806 struct gendisk
*disk
)
808 struct bd_holder_disk
*holder
;
810 list_for_each_entry(holder
, &bdev
->bd_holder_disks
, list
)
811 if (holder
->disk
== disk
)
816 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
818 return sysfs_create_link(from
, to
, kobject_name(to
));
821 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
823 sysfs_remove_link(from
, kobject_name(to
));
827 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
828 * @bdev: the claimed slave bdev
829 * @disk: the holding disk
831 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
833 * This functions creates the following sysfs symlinks.
835 * - from "slaves" directory of the holder @disk to the claimed @bdev
836 * - from "holders" directory of the @bdev to the holder @disk
838 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
839 * passed to bd_link_disk_holder(), then:
841 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
842 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
844 * The caller must have claimed @bdev before calling this function and
845 * ensure that both @bdev and @disk are valid during the creation and
846 * lifetime of these symlinks.
852 * 0 on success, -errno on failure.
854 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
856 struct bd_holder_disk
*holder
;
859 mutex_lock(&bdev
->bd_mutex
);
861 WARN_ON_ONCE(!bdev
->bd_holder
);
863 /* FIXME: remove the following once add_disk() handles errors */
864 if (WARN_ON(!disk
->slave_dir
|| !bdev
->bd_part
->holder_dir
))
867 holder
= bd_find_holder_disk(bdev
, disk
);
873 holder
= kzalloc(sizeof(*holder
), GFP_KERNEL
);
879 INIT_LIST_HEAD(&holder
->list
);
883 ret
= add_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
887 ret
= add_symlink(bdev
->bd_part
->holder_dir
, &disk_to_dev(disk
)->kobj
);
891 * bdev could be deleted beneath us which would implicitly destroy
892 * the holder directory. Hold on to it.
894 kobject_get(bdev
->bd_part
->holder_dir
);
896 list_add(&holder
->list
, &bdev
->bd_holder_disks
);
900 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
904 mutex_unlock(&bdev
->bd_mutex
);
907 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
910 * bd_unlink_disk_holder - destroy symlinks created by bd_link_disk_holder()
911 * @bdev: the calimed slave bdev
912 * @disk: the holding disk
914 * DON'T USE THIS UNLESS YOU'RE ALREADY USING IT.
919 void bd_unlink_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
921 struct bd_holder_disk
*holder
;
923 mutex_lock(&bdev
->bd_mutex
);
925 holder
= bd_find_holder_disk(bdev
, disk
);
927 if (!WARN_ON_ONCE(holder
== NULL
) && !--holder
->refcnt
) {
928 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
929 del_symlink(bdev
->bd_part
->holder_dir
,
930 &disk_to_dev(disk
)->kobj
);
931 kobject_put(bdev
->bd_part
->holder_dir
);
932 list_del_init(&holder
->list
);
936 mutex_unlock(&bdev
->bd_mutex
);
938 EXPORT_SYMBOL_GPL(bd_unlink_disk_holder
);
942 * flush_disk - invalidates all buffer-cache entries on a disk
944 * @bdev: struct block device to be flushed
945 * @kill_dirty: flag to guide handling of dirty inodes
947 * Invalidates all buffer-cache entries on a disk. It should be called
948 * when a disk has been changed -- either by a media change or online
951 static void flush_disk(struct block_device
*bdev
, bool kill_dirty
)
953 if (__invalidate_device(bdev
, kill_dirty
)) {
954 char name
[BDEVNAME_SIZE
] = "";
957 disk_name(bdev
->bd_disk
, 0, name
);
958 printk(KERN_WARNING
"VFS: busy inodes on changed media or "
959 "resized disk %s\n", name
);
964 if (disk_partitionable(bdev
->bd_disk
))
965 bdev
->bd_invalidated
= 1;
969 * check_disk_size_change - checks for disk size change and adjusts bdev size.
970 * @disk: struct gendisk to check
971 * @bdev: struct bdev to adjust.
973 * This routine checks to see if the bdev size does not match the disk size
974 * and adjusts it if it differs.
976 void check_disk_size_change(struct gendisk
*disk
, struct block_device
*bdev
)
978 loff_t disk_size
, bdev_size
;
980 disk_size
= (loff_t
)get_capacity(disk
) << 9;
981 bdev_size
= i_size_read(bdev
->bd_inode
);
982 if (disk_size
!= bdev_size
) {
983 char name
[BDEVNAME_SIZE
];
985 disk_name(disk
, 0, name
);
987 "%s: detected capacity change from %lld to %lld\n",
988 name
, bdev_size
, disk_size
);
989 i_size_write(bdev
->bd_inode
, disk_size
);
990 flush_disk(bdev
, false);
993 EXPORT_SYMBOL(check_disk_size_change
);
996 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
997 * @disk: struct gendisk to be revalidated
999 * This routine is a wrapper for lower-level driver's revalidate_disk
1000 * call-backs. It is used to do common pre and post operations needed
1001 * for all revalidate_disk operations.
1003 int revalidate_disk(struct gendisk
*disk
)
1005 struct block_device
*bdev
;
1008 if (disk
->fops
->revalidate_disk
)
1009 ret
= disk
->fops
->revalidate_disk(disk
);
1011 bdev
= bdget_disk(disk
, 0);
1015 mutex_lock(&bdev
->bd_mutex
);
1016 check_disk_size_change(disk
, bdev
);
1017 mutex_unlock(&bdev
->bd_mutex
);
1021 EXPORT_SYMBOL(revalidate_disk
);
1024 * This routine checks whether a removable media has been changed,
1025 * and invalidates all buffer-cache-entries in that case. This
1026 * is a relatively slow routine, so we have to try to minimize using
1027 * it. Thus it is called only upon a 'mount' or 'open'. This
1028 * is the best way of combining speed and utility, I think.
1029 * People changing diskettes in the middle of an operation deserve
1032 int check_disk_change(struct block_device
*bdev
)
1034 struct gendisk
*disk
= bdev
->bd_disk
;
1035 const struct block_device_operations
*bdops
= disk
->fops
;
1036 unsigned int events
;
1038 events
= disk_clear_events(disk
, DISK_EVENT_MEDIA_CHANGE
|
1039 DISK_EVENT_EJECT_REQUEST
);
1040 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
1043 flush_disk(bdev
, true);
1044 if (bdops
->revalidate_disk
)
1045 bdops
->revalidate_disk(bdev
->bd_disk
);
1049 EXPORT_SYMBOL(check_disk_change
);
1051 void bd_set_size(struct block_device
*bdev
, loff_t size
)
1053 unsigned bsize
= bdev_logical_block_size(bdev
);
1055 bdev
->bd_inode
->i_size
= size
;
1056 while (bsize
< PAGE_CACHE_SIZE
) {
1061 bdev
->bd_block_size
= bsize
;
1062 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
1064 EXPORT_SYMBOL(bd_set_size
);
1066 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
1071 * mutex_lock(part->bd_mutex)
1072 * mutex_lock_nested(whole->bd_mutex, 1)
1075 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1077 struct gendisk
*disk
;
1082 if (mode
& FMODE_READ
)
1084 if (mode
& FMODE_WRITE
)
1087 * hooks: /n/, see "layering violations".
1090 ret
= devcgroup_inode_permission(bdev
->bd_inode
, perm
);
1100 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
1104 disk_block_events(disk
);
1105 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1106 if (!bdev
->bd_openers
) {
1107 bdev
->bd_disk
= disk
;
1108 bdev
->bd_contains
= bdev
;
1110 struct backing_dev_info
*bdi
;
1113 bdev
->bd_part
= disk_get_part(disk
, partno
);
1118 if (disk
->fops
->open
) {
1119 ret
= disk
->fops
->open(bdev
, mode
);
1120 if (ret
== -ERESTARTSYS
) {
1121 /* Lost a race with 'disk' being
1122 * deleted, try again.
1125 disk_put_part(bdev
->bd_part
);
1126 bdev
->bd_part
= NULL
;
1127 bdev
->bd_disk
= NULL
;
1128 mutex_unlock(&bdev
->bd_mutex
);
1129 disk_unblock_events(disk
);
1130 module_put(disk
->fops
->owner
);
1136 if (!ret
&& !bdev
->bd_openers
) {
1137 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1138 bdi
= blk_get_backing_dev_info(bdev
);
1140 bdi
= &default_backing_dev_info
;
1141 bdev_inode_switch_bdi(bdev
->bd_inode
, bdi
);
1145 * If the device is invalidated, rescan partition
1146 * if open succeeded or failed with -ENOMEDIUM.
1147 * The latter is necessary to prevent ghost
1148 * partitions on a removed medium.
1150 if (bdev
->bd_invalidated
&& (!ret
|| ret
== -ENOMEDIUM
))
1151 rescan_partitions(disk
, bdev
);
1155 struct block_device
*whole
;
1156 whole
= bdget_disk(disk
, 0);
1161 ret
= __blkdev_get(whole
, mode
, 1);
1164 bdev
->bd_contains
= whole
;
1165 bdev_inode_switch_bdi(bdev
->bd_inode
,
1166 whole
->bd_inode
->i_data
.backing_dev_info
);
1167 bdev
->bd_part
= disk_get_part(disk
, partno
);
1168 if (!(disk
->flags
& GENHD_FL_UP
) ||
1169 !bdev
->bd_part
|| !bdev
->bd_part
->nr_sects
) {
1173 bd_set_size(bdev
, (loff_t
)bdev
->bd_part
->nr_sects
<< 9);
1176 if (bdev
->bd_contains
== bdev
) {
1178 if (bdev
->bd_disk
->fops
->open
)
1179 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1180 /* the same as first opener case, read comment there */
1181 if (bdev
->bd_invalidated
&& (!ret
|| ret
== -ENOMEDIUM
))
1182 rescan_partitions(bdev
->bd_disk
, bdev
);
1184 goto out_unlock_bdev
;
1186 /* only one opener holds refs to the module and disk */
1187 module_put(disk
->fops
->owner
);
1192 bdev
->bd_part_count
++;
1193 mutex_unlock(&bdev
->bd_mutex
);
1194 disk_unblock_events(disk
);
1198 disk_put_part(bdev
->bd_part
);
1199 bdev
->bd_disk
= NULL
;
1200 bdev
->bd_part
= NULL
;
1201 bdev_inode_switch_bdi(bdev
->bd_inode
, &default_backing_dev_info
);
1202 if (bdev
!= bdev
->bd_contains
)
1203 __blkdev_put(bdev
->bd_contains
, mode
, 1);
1204 bdev
->bd_contains
= NULL
;
1206 mutex_unlock(&bdev
->bd_mutex
);
1207 disk_unblock_events(disk
);
1208 module_put(disk
->fops
->owner
);
1217 * blkdev_get - open a block device
1218 * @bdev: block_device to open
1219 * @mode: FMODE_* mask
1220 * @holder: exclusive holder identifier
1222 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1223 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1224 * @holder is invalid. Exclusive opens may nest for the same @holder.
1226 * On success, the reference count of @bdev is unchanged. On failure,
1233 * 0 on success, -errno on failure.
1235 int blkdev_get(struct block_device
*bdev
, fmode_t mode
, void *holder
)
1237 struct block_device
*whole
= NULL
;
1240 WARN_ON_ONCE((mode
& FMODE_EXCL
) && !holder
);
1242 if ((mode
& FMODE_EXCL
) && holder
) {
1243 whole
= bd_start_claiming(bdev
, holder
);
1244 if (IS_ERR(whole
)) {
1246 return PTR_ERR(whole
);
1250 res
= __blkdev_get(bdev
, mode
, 0);
1253 struct gendisk
*disk
= whole
->bd_disk
;
1255 /* finish claiming */
1256 mutex_lock(&bdev
->bd_mutex
);
1257 spin_lock(&bdev_lock
);
1260 BUG_ON(!bd_may_claim(bdev
, whole
, holder
));
1262 * Note that for a whole device bd_holders
1263 * will be incremented twice, and bd_holder
1264 * will be set to bd_may_claim before being
1267 whole
->bd_holders
++;
1268 whole
->bd_holder
= bd_may_claim
;
1270 bdev
->bd_holder
= holder
;
1273 /* tell others that we're done */
1274 BUG_ON(whole
->bd_claiming
!= holder
);
1275 whole
->bd_claiming
= NULL
;
1276 wake_up_bit(&whole
->bd_claiming
, 0);
1278 spin_unlock(&bdev_lock
);
1281 * Block event polling for write claims if requested. Any
1282 * write holder makes the write_holder state stick until
1283 * all are released. This is good enough and tracking
1284 * individual writeable reference is too fragile given the
1285 * way @mode is used in blkdev_get/put().
1287 if (!res
&& (mode
& FMODE_WRITE
) && !bdev
->bd_write_holder
&&
1288 (disk
->flags
& GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE
)) {
1289 bdev
->bd_write_holder
= true;
1290 disk_block_events(disk
);
1293 mutex_unlock(&bdev
->bd_mutex
);
1299 EXPORT_SYMBOL(blkdev_get
);
1302 * blkdev_get_by_path - open a block device by name
1303 * @path: path to the block device to open
1304 * @mode: FMODE_* mask
1305 * @holder: exclusive holder identifier
1307 * Open the blockdevice described by the device file at @path. @mode
1308 * and @holder are identical to blkdev_get().
1310 * On success, the returned block_device has reference count of one.
1316 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1318 struct block_device
*blkdev_get_by_path(const char *path
, fmode_t mode
,
1321 struct block_device
*bdev
;
1324 bdev
= lookup_bdev(path
);
1328 err
= blkdev_get(bdev
, mode
, holder
);
1330 return ERR_PTR(err
);
1332 if ((mode
& FMODE_WRITE
) && bdev_read_only(bdev
)) {
1333 blkdev_put(bdev
, mode
);
1334 return ERR_PTR(-EACCES
);
1339 EXPORT_SYMBOL(blkdev_get_by_path
);
1342 * blkdev_get_by_dev - open a block device by device number
1343 * @dev: device number of block device to open
1344 * @mode: FMODE_* mask
1345 * @holder: exclusive holder identifier
1347 * Open the blockdevice described by device number @dev. @mode and
1348 * @holder are identical to blkdev_get().
1350 * Use it ONLY if you really do not have anything better - i.e. when
1351 * you are behind a truly sucky interface and all you are given is a
1352 * device number. _Never_ to be used for internal purposes. If you
1353 * ever need it - reconsider your API.
1355 * On success, the returned block_device has reference count of one.
1361 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1363 struct block_device
*blkdev_get_by_dev(dev_t dev
, fmode_t mode
, void *holder
)
1365 struct block_device
*bdev
;
1370 return ERR_PTR(-ENOMEM
);
1372 err
= blkdev_get(bdev
, mode
, holder
);
1374 return ERR_PTR(err
);
1378 EXPORT_SYMBOL(blkdev_get_by_dev
);
1380 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1382 struct block_device
*bdev
;
1385 * Preserve backwards compatibility and allow large file access
1386 * even if userspace doesn't ask for it explicitly. Some mkfs
1387 * binary needs it. We might want to drop this workaround
1388 * during an unstable branch.
1390 filp
->f_flags
|= O_LARGEFILE
;
1392 if (filp
->f_flags
& O_NDELAY
)
1393 filp
->f_mode
|= FMODE_NDELAY
;
1394 if (filp
->f_flags
& O_EXCL
)
1395 filp
->f_mode
|= FMODE_EXCL
;
1396 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1397 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1399 bdev
= bd_acquire(inode
);
1403 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1405 return blkdev_get(bdev
, filp
->f_mode
, filp
);
1408 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1411 struct gendisk
*disk
= bdev
->bd_disk
;
1412 struct block_device
*victim
= NULL
;
1414 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1416 bdev
->bd_part_count
--;
1418 if (!--bdev
->bd_openers
) {
1419 WARN_ON_ONCE(bdev
->bd_holders
);
1420 sync_blockdev(bdev
);
1423 if (bdev
->bd_contains
== bdev
) {
1424 if (disk
->fops
->release
)
1425 ret
= disk
->fops
->release(disk
, mode
);
1427 if (!bdev
->bd_openers
) {
1428 struct module
*owner
= disk
->fops
->owner
;
1432 disk_put_part(bdev
->bd_part
);
1433 bdev
->bd_part
= NULL
;
1434 bdev
->bd_disk
= NULL
;
1435 bdev_inode_switch_bdi(bdev
->bd_inode
,
1436 &default_backing_dev_info
);
1437 if (bdev
!= bdev
->bd_contains
)
1438 victim
= bdev
->bd_contains
;
1439 bdev
->bd_contains
= NULL
;
1441 mutex_unlock(&bdev
->bd_mutex
);
1444 __blkdev_put(victim
, mode
, 1);
1448 int blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1450 if (mode
& FMODE_EXCL
) {
1454 * Release a claim on the device. The holder fields
1455 * are protected with bdev_lock. bd_mutex is to
1456 * synchronize disk_holder unlinking.
1458 mutex_lock(&bdev
->bd_mutex
);
1459 spin_lock(&bdev_lock
);
1461 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
1462 WARN_ON_ONCE(--bdev
->bd_contains
->bd_holders
< 0);
1464 /* bd_contains might point to self, check in a separate step */
1465 if ((bdev_free
= !bdev
->bd_holders
))
1466 bdev
->bd_holder
= NULL
;
1467 if (!bdev
->bd_contains
->bd_holders
)
1468 bdev
->bd_contains
->bd_holder
= NULL
;
1470 spin_unlock(&bdev_lock
);
1473 * If this was the last claim, remove holder link and
1474 * unblock evpoll if it was a write holder.
1477 if (bdev
->bd_write_holder
) {
1478 disk_unblock_events(bdev
->bd_disk
);
1479 disk_check_events(bdev
->bd_disk
);
1480 bdev
->bd_write_holder
= false;
1484 mutex_unlock(&bdev
->bd_mutex
);
1487 return __blkdev_put(bdev
, mode
, 0);
1489 EXPORT_SYMBOL(blkdev_put
);
1491 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1493 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1495 return blkdev_put(bdev
, filp
->f_mode
);
1498 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1500 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
1501 fmode_t mode
= file
->f_mode
;
1504 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1505 * to updated it before every ioctl.
1507 if (file
->f_flags
& O_NDELAY
)
1508 mode
|= FMODE_NDELAY
;
1510 mode
&= ~FMODE_NDELAY
;
1512 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1516 * Write data to the block device. Only intended for the block device itself
1517 * and the raw driver which basically is a fake block device.
1519 * Does not take i_mutex for the write and thus is not for general purpose
1522 ssize_t
blkdev_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1523 unsigned long nr_segs
, loff_t pos
)
1525 struct file
*file
= iocb
->ki_filp
;
1528 BUG_ON(iocb
->ki_pos
!= pos
);
1530 ret
= __generic_file_aio_write(iocb
, iov
, nr_segs
, &iocb
->ki_pos
);
1531 if (ret
> 0 || ret
== -EIOCBQUEUED
) {
1534 err
= generic_write_sync(file
, pos
, ret
);
1535 if (err
< 0 && ret
> 0)
1540 EXPORT_SYMBOL_GPL(blkdev_aio_write
);
1543 * Try to release a page associated with block device when the system
1544 * is under memory pressure.
1546 static int blkdev_releasepage(struct page
*page
, gfp_t wait
)
1548 struct super_block
*super
= BDEV_I(page
->mapping
->host
)->bdev
.bd_super
;
1550 if (super
&& super
->s_op
->bdev_try_to_free_page
)
1551 return super
->s_op
->bdev_try_to_free_page(super
, page
, wait
);
1553 return try_to_free_buffers(page
);
1556 static const struct address_space_operations def_blk_aops
= {
1557 .readpage
= blkdev_readpage
,
1558 .writepage
= blkdev_writepage
,
1559 .write_begin
= blkdev_write_begin
,
1560 .write_end
= blkdev_write_end
,
1561 .writepages
= generic_writepages
,
1562 .releasepage
= blkdev_releasepage
,
1563 .direct_IO
= blkdev_direct_IO
,
1566 const struct file_operations def_blk_fops
= {
1567 .open
= blkdev_open
,
1568 .release
= blkdev_close
,
1569 .llseek
= block_llseek
,
1570 .read
= do_sync_read
,
1571 .write
= do_sync_write
,
1572 .aio_read
= generic_file_aio_read
,
1573 .aio_write
= blkdev_aio_write
,
1574 .mmap
= generic_file_mmap
,
1575 .fsync
= blkdev_fsync
,
1576 .unlocked_ioctl
= block_ioctl
,
1577 #ifdef CONFIG_COMPAT
1578 .compat_ioctl
= compat_blkdev_ioctl
,
1580 .splice_read
= generic_file_splice_read
,
1581 .splice_write
= generic_file_splice_write
,
1584 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1587 mm_segment_t old_fs
= get_fs();
1589 res
= blkdev_ioctl(bdev
, 0, cmd
, arg
);
1594 EXPORT_SYMBOL(ioctl_by_bdev
);
1597 * lookup_bdev - lookup a struct block_device by name
1598 * @pathname: special file representing the block device
1600 * Get a reference to the blockdevice at @pathname in the current
1601 * namespace if possible and return it. Return ERR_PTR(error)
1604 struct block_device
*lookup_bdev(const char *pathname
)
1606 struct block_device
*bdev
;
1607 struct inode
*inode
;
1611 if (!pathname
|| !*pathname
)
1612 return ERR_PTR(-EINVAL
);
1614 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1616 return ERR_PTR(error
);
1618 inode
= path
.dentry
->d_inode
;
1620 if (!S_ISBLK(inode
->i_mode
))
1623 if (path
.mnt
->mnt_flags
& MNT_NODEV
)
1626 bdev
= bd_acquire(inode
);
1633 bdev
= ERR_PTR(error
);
1636 EXPORT_SYMBOL(lookup_bdev
);
1638 int __invalidate_device(struct block_device
*bdev
, bool kill_dirty
)
1640 struct super_block
*sb
= get_super(bdev
);
1645 * no need to lock the super, get_super holds the
1646 * read mutex so the filesystem cannot go away
1647 * under us (->put_super runs with the write lock
1650 shrink_dcache_sb(sb
);
1651 res
= invalidate_inodes(sb
, kill_dirty
);
1654 invalidate_bdev(bdev
);
1657 EXPORT_SYMBOL(__invalidate_device
);