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/smp_lock.h>
15 #include <linux/device_cgroup.h>
16 #include <linux/highmem.h>
17 #include <linux/blkdev.h>
18 #include <linux/module.h>
19 #include <linux/blkpg.h>
20 #include <linux/buffer_head.h>
21 #include <linux/pagevec.h>
22 #include <linux/writeback.h>
23 #include <linux/mpage.h>
24 #include <linux/mount.h>
25 #include <linux/uio.h>
26 #include <linux/namei.h>
27 #include <linux/log2.h>
28 #include <linux/kmemleak.h>
29 #include <asm/uaccess.h>
33 struct block_device bdev
;
34 struct inode vfs_inode
;
37 static const struct address_space_operations def_blk_aops
;
39 static inline struct bdev_inode
*BDEV_I(struct inode
*inode
)
41 return container_of(inode
, struct bdev_inode
, vfs_inode
);
44 inline struct block_device
*I_BDEV(struct inode
*inode
)
46 return &BDEV_I(inode
)->bdev
;
49 EXPORT_SYMBOL(I_BDEV
);
52 * move the inode from it's current bdi to the a new bdi. if the inode is dirty
53 * we need to move it onto the dirty list of @dst so that the inode is always
56 static void bdev_inode_switch_bdi(struct inode
*inode
,
57 struct backing_dev_info
*dst
)
59 spin_lock(&inode_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_lock
);
66 static sector_t
max_block(struct block_device
*bdev
)
68 sector_t retval
= ~((sector_t
)0);
69 loff_t sz
= i_size_read(bdev
->bd_inode
);
72 unsigned int size
= block_size(bdev
);
73 unsigned int sizebits
= blksize_bits(size
);
74 retval
= (sz
>> sizebits
);
79 /* Kill _all_ buffers and pagecache , dirty or not.. */
80 static void kill_bdev(struct block_device
*bdev
)
82 if (bdev
->bd_inode
->i_mapping
->nrpages
== 0)
85 truncate_inode_pages(bdev
->bd_inode
->i_mapping
, 0);
88 int set_blocksize(struct block_device
*bdev
, int size
)
90 /* Size must be a power of two, and between 512 and PAGE_SIZE */
91 if (size
> PAGE_SIZE
|| size
< 512 || !is_power_of_2(size
))
94 /* Size cannot be smaller than the size supported by the device */
95 if (size
< bdev_logical_block_size(bdev
))
98 /* Don't change the size if it is same as current */
99 if (bdev
->bd_block_size
!= size
) {
101 bdev
->bd_block_size
= size
;
102 bdev
->bd_inode
->i_blkbits
= blksize_bits(size
);
108 EXPORT_SYMBOL(set_blocksize
);
110 int sb_set_blocksize(struct super_block
*sb
, int size
)
112 if (set_blocksize(sb
->s_bdev
, size
))
114 /* If we get here, we know size is power of two
115 * and it's value is between 512 and PAGE_SIZE */
116 sb
->s_blocksize
= size
;
117 sb
->s_blocksize_bits
= blksize_bits(size
);
118 return sb
->s_blocksize
;
121 EXPORT_SYMBOL(sb_set_blocksize
);
123 int sb_min_blocksize(struct super_block
*sb
, int size
)
125 int minsize
= bdev_logical_block_size(sb
->s_bdev
);
128 return sb_set_blocksize(sb
, size
);
131 EXPORT_SYMBOL(sb_min_blocksize
);
134 blkdev_get_block(struct inode
*inode
, sector_t iblock
,
135 struct buffer_head
*bh
, int create
)
137 if (iblock
>= max_block(I_BDEV(inode
))) {
142 * for reads, we're just trying to fill a partial page.
143 * return a hole, they will have to call get_block again
144 * before they can fill it, and they will get -EIO at that
149 bh
->b_bdev
= I_BDEV(inode
);
150 bh
->b_blocknr
= iblock
;
151 set_buffer_mapped(bh
);
156 blkdev_get_blocks(struct inode
*inode
, sector_t iblock
,
157 struct buffer_head
*bh
, int create
)
159 sector_t end_block
= max_block(I_BDEV(inode
));
160 unsigned long max_blocks
= bh
->b_size
>> inode
->i_blkbits
;
162 if ((iblock
+ max_blocks
) > end_block
) {
163 max_blocks
= end_block
- iblock
;
164 if ((long)max_blocks
<= 0) {
166 return -EIO
; /* write fully beyond EOF */
168 * It is a read which is fully beyond EOF. We return
169 * a !buffer_mapped buffer
175 bh
->b_bdev
= I_BDEV(inode
);
176 bh
->b_blocknr
= iblock
;
177 bh
->b_size
= max_blocks
<< inode
->i_blkbits
;
179 set_buffer_mapped(bh
);
184 blkdev_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
185 loff_t offset
, unsigned long nr_segs
)
187 struct file
*file
= iocb
->ki_filp
;
188 struct inode
*inode
= file
->f_mapping
->host
;
190 return __blockdev_direct_IO(rw
, iocb
, inode
, I_BDEV(inode
), iov
, offset
,
191 nr_segs
, blkdev_get_blocks
, NULL
, NULL
, 0);
194 int __sync_blockdev(struct block_device
*bdev
, int wait
)
199 return filemap_flush(bdev
->bd_inode
->i_mapping
);
200 return filemap_write_and_wait(bdev
->bd_inode
->i_mapping
);
204 * Write out and wait upon all the dirty data associated with a block
205 * device via its mapping. Does not take the superblock lock.
207 int sync_blockdev(struct block_device
*bdev
)
209 return __sync_blockdev(bdev
, 1);
211 EXPORT_SYMBOL(sync_blockdev
);
214 * Write out and wait upon all dirty data associated with this
215 * device. Filesystem data as well as the underlying block
216 * device. Takes the superblock lock.
218 int fsync_bdev(struct block_device
*bdev
)
220 struct super_block
*sb
= get_super(bdev
);
222 int res
= sync_filesystem(sb
);
226 return sync_blockdev(bdev
);
228 EXPORT_SYMBOL(fsync_bdev
);
231 * freeze_bdev -- lock a filesystem and force it into a consistent state
232 * @bdev: blockdevice to lock
234 * If a superblock is found on this device, we take the s_umount semaphore
235 * on it to make sure nobody unmounts until the snapshot creation is done.
236 * The reference counter (bd_fsfreeze_count) guarantees that only the last
237 * unfreeze process can unfreeze the frozen filesystem actually when multiple
238 * freeze requests arrive simultaneously. It counts up in freeze_bdev() and
239 * count down in thaw_bdev(). When it becomes 0, thaw_bdev() will unfreeze
242 struct super_block
*freeze_bdev(struct block_device
*bdev
)
244 struct super_block
*sb
;
247 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
248 if (++bdev
->bd_fsfreeze_count
> 1) {
250 * We don't even need to grab a reference - the first call
251 * to freeze_bdev grab an active reference and only the last
252 * thaw_bdev drops it.
254 sb
= get_super(bdev
);
256 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
260 sb
= get_active_super(bdev
);
263 error
= freeze_super(sb
);
265 deactivate_super(sb
);
266 bdev
->bd_fsfreeze_count
--;
267 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
268 return ERR_PTR(error
);
270 deactivate_super(sb
);
273 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
274 return sb
; /* thaw_bdev releases s->s_umount */
276 EXPORT_SYMBOL(freeze_bdev
);
279 * thaw_bdev -- unlock filesystem
280 * @bdev: blockdevice to unlock
281 * @sb: associated superblock
283 * Unlocks the filesystem and marks it writeable again after freeze_bdev().
285 int thaw_bdev(struct block_device
*bdev
, struct super_block
*sb
)
289 mutex_lock(&bdev
->bd_fsfreeze_mutex
);
290 if (!bdev
->bd_fsfreeze_count
)
294 if (--bdev
->bd_fsfreeze_count
> 0)
300 error
= thaw_super(sb
);
302 bdev
->bd_fsfreeze_count
++;
303 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
307 mutex_unlock(&bdev
->bd_fsfreeze_mutex
);
310 EXPORT_SYMBOL(thaw_bdev
);
312 static int blkdev_writepage(struct page
*page
, struct writeback_control
*wbc
)
314 return block_write_full_page(page
, blkdev_get_block
, wbc
);
317 static int blkdev_readpage(struct file
* file
, struct page
* page
)
319 return block_read_full_page(page
, blkdev_get_block
);
322 static int blkdev_write_begin(struct file
*file
, struct address_space
*mapping
,
323 loff_t pos
, unsigned len
, unsigned flags
,
324 struct page
**pagep
, void **fsdata
)
326 return block_write_begin(mapping
, pos
, len
, flags
, pagep
,
330 static int blkdev_write_end(struct file
*file
, struct address_space
*mapping
,
331 loff_t pos
, unsigned len
, unsigned copied
,
332 struct page
*page
, void *fsdata
)
335 ret
= block_write_end(file
, mapping
, pos
, len
, copied
, page
, fsdata
);
338 page_cache_release(page
);
345 * for a block special file file->f_path.dentry->d_inode->i_size is zero
346 * so we compute the size by hand (just as in block_read/write above)
348 static loff_t
block_llseek(struct file
*file
, loff_t offset
, int origin
)
350 struct inode
*bd_inode
= file
->f_mapping
->host
;
354 mutex_lock(&bd_inode
->i_mutex
);
355 size
= i_size_read(bd_inode
);
362 offset
+= file
->f_pos
;
365 if (offset
>= 0 && offset
<= size
) {
366 if (offset
!= file
->f_pos
) {
367 file
->f_pos
= offset
;
371 mutex_unlock(&bd_inode
->i_mutex
);
375 int blkdev_fsync(struct file
*filp
, int datasync
)
377 struct inode
*bd_inode
= filp
->f_mapping
->host
;
378 struct block_device
*bdev
= I_BDEV(bd_inode
);
382 * There is no need to serialise calls to blkdev_issue_flush with
383 * i_mutex and doing so causes performance issues with concurrent
384 * O_SYNC writers to a block device.
386 mutex_unlock(&bd_inode
->i_mutex
);
388 error
= blkdev_issue_flush(bdev
, GFP_KERNEL
, NULL
);
389 if (error
== -EOPNOTSUPP
)
392 mutex_lock(&bd_inode
->i_mutex
);
396 EXPORT_SYMBOL(blkdev_fsync
);
402 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(bdev_lock
);
403 static struct kmem_cache
* bdev_cachep __read_mostly
;
405 static struct inode
*bdev_alloc_inode(struct super_block
*sb
)
407 struct bdev_inode
*ei
= kmem_cache_alloc(bdev_cachep
, GFP_KERNEL
);
410 return &ei
->vfs_inode
;
413 static void bdev_destroy_inode(struct inode
*inode
)
415 struct bdev_inode
*bdi
= BDEV_I(inode
);
417 kmem_cache_free(bdev_cachep
, bdi
);
420 static void init_once(void *foo
)
422 struct bdev_inode
*ei
= (struct bdev_inode
*) foo
;
423 struct block_device
*bdev
= &ei
->bdev
;
425 memset(bdev
, 0, sizeof(*bdev
));
426 mutex_init(&bdev
->bd_mutex
);
427 INIT_LIST_HEAD(&bdev
->bd_inodes
);
428 INIT_LIST_HEAD(&bdev
->bd_list
);
429 inode_init_once(&ei
->vfs_inode
);
430 /* Initialize mutex for freeze. */
431 mutex_init(&bdev
->bd_fsfreeze_mutex
);
434 static inline void __bd_forget(struct inode
*inode
)
436 list_del_init(&inode
->i_devices
);
437 inode
->i_bdev
= NULL
;
438 inode
->i_mapping
= &inode
->i_data
;
441 static void bdev_evict_inode(struct inode
*inode
)
443 struct block_device
*bdev
= &BDEV_I(inode
)->bdev
;
445 truncate_inode_pages(&inode
->i_data
, 0);
446 invalidate_inode_buffers(inode
); /* is it needed here? */
447 end_writeback(inode
);
448 spin_lock(&bdev_lock
);
449 while ( (p
= bdev
->bd_inodes
.next
) != &bdev
->bd_inodes
) {
450 __bd_forget(list_entry(p
, struct inode
, i_devices
));
452 list_del_init(&bdev
->bd_list
);
453 spin_unlock(&bdev_lock
);
456 static const struct super_operations bdev_sops
= {
457 .statfs
= simple_statfs
,
458 .alloc_inode
= bdev_alloc_inode
,
459 .destroy_inode
= bdev_destroy_inode
,
460 .drop_inode
= generic_delete_inode
,
461 .evict_inode
= bdev_evict_inode
,
464 static struct dentry
*bd_mount(struct file_system_type
*fs_type
,
465 int flags
, const char *dev_name
, void *data
)
467 return mount_pseudo(fs_type
, "bdev:", &bdev_sops
, 0x62646576);
470 static struct file_system_type bd_type
= {
473 .kill_sb
= kill_anon_super
,
476 struct super_block
*blockdev_superblock __read_mostly
;
478 void __init
bdev_cache_init(void)
481 struct vfsmount
*bd_mnt
;
483 bdev_cachep
= kmem_cache_create("bdev_cache", sizeof(struct bdev_inode
),
484 0, (SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
|
485 SLAB_MEM_SPREAD
|SLAB_PANIC
),
487 err
= register_filesystem(&bd_type
);
489 panic("Cannot register bdev pseudo-fs");
490 bd_mnt
= kern_mount(&bd_type
);
492 panic("Cannot create bdev pseudo-fs");
494 * This vfsmount structure is only used to obtain the
495 * blockdev_superblock, so tell kmemleak not to report it.
497 kmemleak_not_leak(bd_mnt
);
498 blockdev_superblock
= bd_mnt
->mnt_sb
; /* For writeback */
502 * Most likely _very_ bad one - but then it's hardly critical for small
503 * /dev and can be fixed when somebody will need really large one.
504 * Keep in mind that it will be fed through icache hash function too.
506 static inline unsigned long hash(dev_t dev
)
508 return MAJOR(dev
)+MINOR(dev
);
511 static int bdev_test(struct inode
*inode
, void *data
)
513 return BDEV_I(inode
)->bdev
.bd_dev
== *(dev_t
*)data
;
516 static int bdev_set(struct inode
*inode
, void *data
)
518 BDEV_I(inode
)->bdev
.bd_dev
= *(dev_t
*)data
;
522 static LIST_HEAD(all_bdevs
);
524 struct block_device
*bdget(dev_t dev
)
526 struct block_device
*bdev
;
529 inode
= iget5_locked(blockdev_superblock
, hash(dev
),
530 bdev_test
, bdev_set
, &dev
);
535 bdev
= &BDEV_I(inode
)->bdev
;
537 if (inode
->i_state
& I_NEW
) {
538 bdev
->bd_contains
= NULL
;
539 bdev
->bd_inode
= inode
;
540 bdev
->bd_block_size
= (1 << inode
->i_blkbits
);
541 bdev
->bd_part_count
= 0;
542 bdev
->bd_invalidated
= 0;
543 inode
->i_mode
= S_IFBLK
;
545 inode
->i_bdev
= bdev
;
546 inode
->i_data
.a_ops
= &def_blk_aops
;
547 mapping_set_gfp_mask(&inode
->i_data
, GFP_USER
);
548 inode
->i_data
.backing_dev_info
= &default_backing_dev_info
;
549 spin_lock(&bdev_lock
);
550 list_add(&bdev
->bd_list
, &all_bdevs
);
551 spin_unlock(&bdev_lock
);
552 unlock_new_inode(inode
);
557 EXPORT_SYMBOL(bdget
);
560 * bdgrab -- Grab a reference to an already referenced block device
561 * @bdev: Block device to grab a reference to.
563 struct block_device
*bdgrab(struct block_device
*bdev
)
565 ihold(bdev
->bd_inode
);
569 long nr_blockdev_pages(void)
571 struct block_device
*bdev
;
573 spin_lock(&bdev_lock
);
574 list_for_each_entry(bdev
, &all_bdevs
, bd_list
) {
575 ret
+= bdev
->bd_inode
->i_mapping
->nrpages
;
577 spin_unlock(&bdev_lock
);
581 void bdput(struct block_device
*bdev
)
583 iput(bdev
->bd_inode
);
586 EXPORT_SYMBOL(bdput
);
588 static struct block_device
*bd_acquire(struct inode
*inode
)
590 struct block_device
*bdev
;
592 spin_lock(&bdev_lock
);
593 bdev
= inode
->i_bdev
;
595 ihold(bdev
->bd_inode
);
596 spin_unlock(&bdev_lock
);
599 spin_unlock(&bdev_lock
);
601 bdev
= bdget(inode
->i_rdev
);
603 spin_lock(&bdev_lock
);
604 if (!inode
->i_bdev
) {
606 * We take an additional reference to bd_inode,
607 * and it's released in clear_inode() of inode.
608 * So, we can access it via ->i_mapping always
611 ihold(bdev
->bd_inode
);
612 inode
->i_bdev
= bdev
;
613 inode
->i_mapping
= bdev
->bd_inode
->i_mapping
;
614 list_add(&inode
->i_devices
, &bdev
->bd_inodes
);
616 spin_unlock(&bdev_lock
);
621 /* Call when you free inode */
623 void bd_forget(struct inode
*inode
)
625 struct block_device
*bdev
= NULL
;
627 spin_lock(&bdev_lock
);
629 if (!sb_is_blkdev_sb(inode
->i_sb
))
630 bdev
= inode
->i_bdev
;
633 spin_unlock(&bdev_lock
);
636 iput(bdev
->bd_inode
);
640 * bd_may_claim - test whether a block device can be claimed
641 * @bdev: block device of interest
642 * @whole: whole block device containing @bdev, may equal @bdev
643 * @holder: holder trying to claim @bdev
645 * Test whther @bdev can be claimed by @holder.
648 * spin_lock(&bdev_lock).
651 * %true if @bdev can be claimed, %false otherwise.
653 static bool bd_may_claim(struct block_device
*bdev
, struct block_device
*whole
,
656 if (bdev
->bd_holder
== holder
)
657 return true; /* already a holder */
658 else if (bdev
->bd_holder
!= NULL
)
659 return false; /* held by someone else */
660 else if (bdev
->bd_contains
== bdev
)
661 return true; /* is a whole device which isn't held */
663 else if (whole
->bd_holder
== bd_may_claim
)
664 return true; /* is a partition of a device that is being partitioned */
665 else if (whole
->bd_holder
!= NULL
)
666 return false; /* is a partition of a held device */
668 return true; /* is a partition of an un-held device */
672 * bd_prepare_to_claim - prepare to claim a block device
673 * @bdev: block device of interest
674 * @whole: the whole device containing @bdev, may equal @bdev
675 * @holder: holder trying to claim @bdev
677 * Prepare to claim @bdev. This function fails if @bdev is already
678 * claimed by another holder and waits if another claiming is in
679 * progress. This function doesn't actually claim. On successful
680 * return, the caller has ownership of bd_claiming and bd_holder[s].
683 * spin_lock(&bdev_lock). Might release bdev_lock, sleep and regrab
687 * 0 if @bdev can be claimed, -EBUSY otherwise.
689 static int bd_prepare_to_claim(struct block_device
*bdev
,
690 struct block_device
*whole
, void *holder
)
693 /* if someone else claimed, fail */
694 if (!bd_may_claim(bdev
, whole
, holder
))
697 /* if claiming is already in progress, wait for it to finish */
698 if (whole
->bd_claiming
) {
699 wait_queue_head_t
*wq
= bit_waitqueue(&whole
->bd_claiming
, 0);
702 prepare_to_wait(wq
, &wait
, TASK_UNINTERRUPTIBLE
);
703 spin_unlock(&bdev_lock
);
705 finish_wait(wq
, &wait
);
706 spin_lock(&bdev_lock
);
715 * bd_start_claiming - start claiming a block device
716 * @bdev: block device of interest
717 * @holder: holder trying to claim @bdev
719 * @bdev is about to be opened exclusively. Check @bdev can be opened
720 * exclusively and mark that an exclusive open is in progress. Each
721 * successful call to this function must be matched with a call to
722 * either bd_finish_claiming() or bd_abort_claiming() (which do not
725 * This function is used to gain exclusive access to the block device
726 * without actually causing other exclusive open attempts to fail. It
727 * should be used when the open sequence itself requires exclusive
728 * access but may subsequently fail.
734 * Pointer to the block device containing @bdev on success, ERR_PTR()
737 static struct block_device
*bd_start_claiming(struct block_device
*bdev
,
740 struct gendisk
*disk
;
741 struct block_device
*whole
;
747 * @bdev might not have been initialized properly yet, look up
748 * and grab the outer block device the hard way.
750 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
752 return ERR_PTR(-ENXIO
);
754 whole
= bdget_disk(disk
, 0);
755 module_put(disk
->fops
->owner
);
758 return ERR_PTR(-ENOMEM
);
760 /* prepare to claim, if successful, mark claiming in progress */
761 spin_lock(&bdev_lock
);
763 err
= bd_prepare_to_claim(bdev
, whole
, holder
);
765 whole
->bd_claiming
= holder
;
766 spin_unlock(&bdev_lock
);
769 spin_unlock(&bdev_lock
);
776 static int add_symlink(struct kobject
*from
, struct kobject
*to
)
778 return sysfs_create_link(from
, to
, kobject_name(to
));
781 static void del_symlink(struct kobject
*from
, struct kobject
*to
)
783 sysfs_remove_link(from
, kobject_name(to
));
787 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
788 * @bdev: the claimed slave bdev
789 * @disk: the holding disk
791 * This functions creates the following sysfs symlinks.
793 * - from "slaves" directory of the holder @disk to the claimed @bdev
794 * - from "holders" directory of the @bdev to the holder @disk
796 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
797 * passed to bd_link_disk_holder(), then:
799 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
800 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
802 * The caller must have claimed @bdev before calling this function and
803 * ensure that both @bdev and @disk are valid during the creation and
804 * lifetime of these symlinks.
810 * 0 on success, -errno on failure.
812 int bd_link_disk_holder(struct block_device
*bdev
, struct gendisk
*disk
)
816 mutex_lock(&bdev
->bd_mutex
);
818 WARN_ON_ONCE(!bdev
->bd_holder
|| bdev
->bd_holder_disk
);
820 /* FIXME: remove the following once add_disk() handles errors */
821 if (WARN_ON(!disk
->slave_dir
|| !bdev
->bd_part
->holder_dir
))
824 ret
= add_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
828 ret
= add_symlink(bdev
->bd_part
->holder_dir
, &disk_to_dev(disk
)->kobj
);
830 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
834 bdev
->bd_holder_disk
= disk
;
836 mutex_unlock(&bdev
->bd_mutex
);
839 EXPORT_SYMBOL_GPL(bd_link_disk_holder
);
841 static void bd_unlink_disk_holder(struct block_device
*bdev
)
843 struct gendisk
*disk
= bdev
->bd_holder_disk
;
845 bdev
->bd_holder_disk
= NULL
;
849 del_symlink(disk
->slave_dir
, &part_to_dev(bdev
->bd_part
)->kobj
);
850 del_symlink(bdev
->bd_part
->holder_dir
, &disk_to_dev(disk
)->kobj
);
853 static inline void bd_unlink_disk_holder(struct block_device
*bdev
)
858 * flush_disk - invalidates all buffer-cache entries on a disk
860 * @bdev: struct block device to be flushed
862 * Invalidates all buffer-cache entries on a disk. It should be called
863 * when a disk has been changed -- either by a media change or online
866 static void flush_disk(struct block_device
*bdev
)
868 if (__invalidate_device(bdev
)) {
869 char name
[BDEVNAME_SIZE
] = "";
872 disk_name(bdev
->bd_disk
, 0, name
);
873 printk(KERN_WARNING
"VFS: busy inodes on changed media or "
874 "resized disk %s\n", name
);
879 if (disk_partitionable(bdev
->bd_disk
))
880 bdev
->bd_invalidated
= 1;
884 * check_disk_size_change - checks for disk size change and adjusts bdev size.
885 * @disk: struct gendisk to check
886 * @bdev: struct bdev to adjust.
888 * This routine checks to see if the bdev size does not match the disk size
889 * and adjusts it if it differs.
891 void check_disk_size_change(struct gendisk
*disk
, struct block_device
*bdev
)
893 loff_t disk_size
, bdev_size
;
895 disk_size
= (loff_t
)get_capacity(disk
) << 9;
896 bdev_size
= i_size_read(bdev
->bd_inode
);
897 if (disk_size
!= bdev_size
) {
898 char name
[BDEVNAME_SIZE
];
900 disk_name(disk
, 0, name
);
902 "%s: detected capacity change from %lld to %lld\n",
903 name
, bdev_size
, disk_size
);
904 i_size_write(bdev
->bd_inode
, disk_size
);
908 EXPORT_SYMBOL(check_disk_size_change
);
911 * revalidate_disk - wrapper for lower-level driver's revalidate_disk call-back
912 * @disk: struct gendisk to be revalidated
914 * This routine is a wrapper for lower-level driver's revalidate_disk
915 * call-backs. It is used to do common pre and post operations needed
916 * for all revalidate_disk operations.
918 int revalidate_disk(struct gendisk
*disk
)
920 struct block_device
*bdev
;
923 if (disk
->fops
->revalidate_disk
)
924 ret
= disk
->fops
->revalidate_disk(disk
);
926 bdev
= bdget_disk(disk
, 0);
930 mutex_lock(&bdev
->bd_mutex
);
931 check_disk_size_change(disk
, bdev
);
932 mutex_unlock(&bdev
->bd_mutex
);
936 EXPORT_SYMBOL(revalidate_disk
);
939 * This routine checks whether a removable media has been changed,
940 * and invalidates all buffer-cache-entries in that case. This
941 * is a relatively slow routine, so we have to try to minimize using
942 * it. Thus it is called only upon a 'mount' or 'open'. This
943 * is the best way of combining speed and utility, I think.
944 * People changing diskettes in the middle of an operation deserve
947 int check_disk_change(struct block_device
*bdev
)
949 struct gendisk
*disk
= bdev
->bd_disk
;
950 const struct block_device_operations
*bdops
= disk
->fops
;
953 events
= disk_clear_events(disk
, DISK_EVENT_MEDIA_CHANGE
|
954 DISK_EVENT_EJECT_REQUEST
);
955 if (!(events
& DISK_EVENT_MEDIA_CHANGE
))
959 if (bdops
->revalidate_disk
)
960 bdops
->revalidate_disk(bdev
->bd_disk
);
964 EXPORT_SYMBOL(check_disk_change
);
966 void bd_set_size(struct block_device
*bdev
, loff_t size
)
968 unsigned bsize
= bdev_logical_block_size(bdev
);
970 bdev
->bd_inode
->i_size
= size
;
971 while (bsize
< PAGE_CACHE_SIZE
) {
976 bdev
->bd_block_size
= bsize
;
977 bdev
->bd_inode
->i_blkbits
= blksize_bits(bsize
);
979 EXPORT_SYMBOL(bd_set_size
);
981 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
);
986 * mutex_lock(part->bd_mutex)
987 * mutex_lock_nested(whole->bd_mutex, 1)
990 static int __blkdev_get(struct block_device
*bdev
, fmode_t mode
, int for_part
)
992 struct gendisk
*disk
;
997 if (mode
& FMODE_READ
)
999 if (mode
& FMODE_WRITE
)
1002 * hooks: /n/, see "layering violations".
1005 ret
= devcgroup_inode_permission(bdev
->bd_inode
, perm
);
1015 disk
= get_gendisk(bdev
->bd_dev
, &partno
);
1019 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1020 if (!bdev
->bd_openers
) {
1021 bdev
->bd_disk
= disk
;
1022 bdev
->bd_contains
= bdev
;
1024 struct backing_dev_info
*bdi
;
1027 bdev
->bd_part
= disk_get_part(disk
, partno
);
1031 if (disk
->fops
->open
) {
1032 ret
= disk
->fops
->open(bdev
, mode
);
1033 if (ret
== -ERESTARTSYS
) {
1034 /* Lost a race with 'disk' being
1035 * deleted, try again.
1038 disk_put_part(bdev
->bd_part
);
1039 bdev
->bd_part
= NULL
;
1040 module_put(disk
->fops
->owner
);
1042 bdev
->bd_disk
= NULL
;
1043 mutex_unlock(&bdev
->bd_mutex
);
1049 if (!bdev
->bd_openers
) {
1050 bd_set_size(bdev
,(loff_t
)get_capacity(disk
)<<9);
1051 bdi
= blk_get_backing_dev_info(bdev
);
1053 bdi
= &default_backing_dev_info
;
1054 bdev_inode_switch_bdi(bdev
->bd_inode
, bdi
);
1056 if (bdev
->bd_invalidated
)
1057 rescan_partitions(disk
, bdev
);
1059 struct block_device
*whole
;
1060 whole
= bdget_disk(disk
, 0);
1065 ret
= __blkdev_get(whole
, mode
, 1);
1068 bdev
->bd_contains
= whole
;
1069 bdev_inode_switch_bdi(bdev
->bd_inode
,
1070 whole
->bd_inode
->i_data
.backing_dev_info
);
1071 bdev
->bd_part
= disk_get_part(disk
, partno
);
1072 if (!(disk
->flags
& GENHD_FL_UP
) ||
1073 !bdev
->bd_part
|| !bdev
->bd_part
->nr_sects
) {
1077 bd_set_size(bdev
, (loff_t
)bdev
->bd_part
->nr_sects
<< 9);
1080 module_put(disk
->fops
->owner
);
1083 if (bdev
->bd_contains
== bdev
) {
1084 if (bdev
->bd_disk
->fops
->open
) {
1085 ret
= bdev
->bd_disk
->fops
->open(bdev
, mode
);
1087 goto out_unlock_bdev
;
1089 if (bdev
->bd_invalidated
)
1090 rescan_partitions(bdev
->bd_disk
, bdev
);
1095 bdev
->bd_part_count
++;
1096 mutex_unlock(&bdev
->bd_mutex
);
1100 disk_put_part(bdev
->bd_part
);
1101 bdev
->bd_disk
= NULL
;
1102 bdev
->bd_part
= NULL
;
1103 bdev_inode_switch_bdi(bdev
->bd_inode
, &default_backing_dev_info
);
1104 if (bdev
!= bdev
->bd_contains
)
1105 __blkdev_put(bdev
->bd_contains
, mode
, 1);
1106 bdev
->bd_contains
= NULL
;
1108 mutex_unlock(&bdev
->bd_mutex
);
1111 module_put(disk
->fops
->owner
);
1119 * blkdev_get - open a block device
1120 * @bdev: block_device to open
1121 * @mode: FMODE_* mask
1122 * @holder: exclusive holder identifier
1124 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1125 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1126 * @holder is invalid. Exclusive opens may nest for the same @holder.
1128 * On success, the reference count of @bdev is unchanged. On failure,
1135 * 0 on success, -errno on failure.
1137 int blkdev_get(struct block_device
*bdev
, fmode_t mode
, void *holder
)
1139 struct block_device
*whole
= NULL
;
1142 WARN_ON_ONCE((mode
& FMODE_EXCL
) && !holder
);
1144 if ((mode
& FMODE_EXCL
) && holder
) {
1145 whole
= bd_start_claiming(bdev
, holder
);
1146 if (IS_ERR(whole
)) {
1148 return PTR_ERR(whole
);
1152 res
= __blkdev_get(bdev
, mode
, 0);
1154 /* __blkdev_get() may alter read only status, check it afterwards */
1155 if (!res
&& (mode
& FMODE_WRITE
) && bdev_read_only(bdev
)) {
1156 __blkdev_put(bdev
, mode
, 0);
1161 /* finish claiming */
1162 mutex_lock(&bdev
->bd_mutex
);
1163 spin_lock(&bdev_lock
);
1166 BUG_ON(!bd_may_claim(bdev
, whole
, holder
));
1168 * Note that for a whole device bd_holders
1169 * will be incremented twice, and bd_holder
1170 * will be set to bd_may_claim before being
1173 whole
->bd_holders
++;
1174 whole
->bd_holder
= bd_may_claim
;
1176 bdev
->bd_holder
= holder
;
1179 /* tell others that we're done */
1180 BUG_ON(whole
->bd_claiming
!= holder
);
1181 whole
->bd_claiming
= NULL
;
1182 wake_up_bit(&whole
->bd_claiming
, 0);
1184 spin_unlock(&bdev_lock
);
1187 * Block event polling for write claims. Any write
1188 * holder makes the write_holder state stick until all
1189 * are released. This is good enough and tracking
1190 * individual writeable reference is too fragile given
1191 * the way @mode is used in blkdev_get/put().
1193 if (!res
&& (mode
& FMODE_WRITE
) && !bdev
->bd_write_holder
) {
1194 bdev
->bd_write_holder
= true;
1195 disk_block_events(bdev
->bd_disk
);
1198 mutex_unlock(&bdev
->bd_mutex
);
1204 EXPORT_SYMBOL(blkdev_get
);
1207 * blkdev_get_by_path - open a block device by name
1208 * @path: path to the block device to open
1209 * @mode: FMODE_* mask
1210 * @holder: exclusive holder identifier
1212 * Open the blockdevice described by the device file at @path. @mode
1213 * and @holder are identical to blkdev_get().
1215 * On success, the returned block_device has reference count of one.
1221 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1223 struct block_device
*blkdev_get_by_path(const char *path
, fmode_t mode
,
1226 struct block_device
*bdev
;
1229 bdev
= lookup_bdev(path
);
1233 err
= blkdev_get(bdev
, mode
, holder
);
1235 return ERR_PTR(err
);
1239 EXPORT_SYMBOL(blkdev_get_by_path
);
1242 * blkdev_get_by_dev - open a block device by device number
1243 * @dev: device number of block device to open
1244 * @mode: FMODE_* mask
1245 * @holder: exclusive holder identifier
1247 * Open the blockdevice described by device number @dev. @mode and
1248 * @holder are identical to blkdev_get().
1250 * Use it ONLY if you really do not have anything better - i.e. when
1251 * you are behind a truly sucky interface and all you are given is a
1252 * device number. _Never_ to be used for internal purposes. If you
1253 * ever need it - reconsider your API.
1255 * On success, the returned block_device has reference count of one.
1261 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1263 struct block_device
*blkdev_get_by_dev(dev_t dev
, fmode_t mode
, void *holder
)
1265 struct block_device
*bdev
;
1270 return ERR_PTR(-ENOMEM
);
1272 err
= blkdev_get(bdev
, mode
, holder
);
1274 return ERR_PTR(err
);
1278 EXPORT_SYMBOL(blkdev_get_by_dev
);
1280 static int blkdev_open(struct inode
* inode
, struct file
* filp
)
1282 struct block_device
*bdev
;
1285 * Preserve backwards compatibility and allow large file access
1286 * even if userspace doesn't ask for it explicitly. Some mkfs
1287 * binary needs it. We might want to drop this workaround
1288 * during an unstable branch.
1290 filp
->f_flags
|= O_LARGEFILE
;
1292 if (filp
->f_flags
& O_NDELAY
)
1293 filp
->f_mode
|= FMODE_NDELAY
;
1294 if (filp
->f_flags
& O_EXCL
)
1295 filp
->f_mode
|= FMODE_EXCL
;
1296 if ((filp
->f_flags
& O_ACCMODE
) == 3)
1297 filp
->f_mode
|= FMODE_WRITE_IOCTL
;
1299 bdev
= bd_acquire(inode
);
1303 filp
->f_mapping
= bdev
->bd_inode
->i_mapping
;
1305 return blkdev_get(bdev
, filp
->f_mode
, filp
);
1308 static int __blkdev_put(struct block_device
*bdev
, fmode_t mode
, int for_part
)
1311 struct gendisk
*disk
= bdev
->bd_disk
;
1312 struct block_device
*victim
= NULL
;
1314 mutex_lock_nested(&bdev
->bd_mutex
, for_part
);
1316 bdev
->bd_part_count
--;
1318 if (!--bdev
->bd_openers
) {
1319 WARN_ON_ONCE(bdev
->bd_holders
);
1320 sync_blockdev(bdev
);
1323 if (bdev
->bd_contains
== bdev
) {
1324 if (disk
->fops
->release
)
1325 ret
= disk
->fops
->release(disk
, mode
);
1327 if (!bdev
->bd_openers
) {
1328 struct module
*owner
= disk
->fops
->owner
;
1332 disk_put_part(bdev
->bd_part
);
1333 bdev
->bd_part
= NULL
;
1334 bdev
->bd_disk
= NULL
;
1335 bdev_inode_switch_bdi(bdev
->bd_inode
,
1336 &default_backing_dev_info
);
1337 if (bdev
!= bdev
->bd_contains
)
1338 victim
= bdev
->bd_contains
;
1339 bdev
->bd_contains
= NULL
;
1341 mutex_unlock(&bdev
->bd_mutex
);
1344 __blkdev_put(victim
, mode
, 1);
1348 int blkdev_put(struct block_device
*bdev
, fmode_t mode
)
1350 if (mode
& FMODE_EXCL
) {
1354 * Release a claim on the device. The holder fields
1355 * are protected with bdev_lock. bd_mutex is to
1356 * synchronize disk_holder unlinking.
1358 mutex_lock(&bdev
->bd_mutex
);
1359 spin_lock(&bdev_lock
);
1361 WARN_ON_ONCE(--bdev
->bd_holders
< 0);
1362 WARN_ON_ONCE(--bdev
->bd_contains
->bd_holders
< 0);
1364 /* bd_contains might point to self, check in a separate step */
1365 if ((bdev_free
= !bdev
->bd_holders
))
1366 bdev
->bd_holder
= NULL
;
1367 if (!bdev
->bd_contains
->bd_holders
)
1368 bdev
->bd_contains
->bd_holder
= NULL
;
1370 spin_unlock(&bdev_lock
);
1373 * If this was the last claim, remove holder link and
1374 * unblock evpoll if it was a write holder.
1377 bd_unlink_disk_holder(bdev
);
1378 if (bdev
->bd_write_holder
) {
1379 disk_unblock_events(bdev
->bd_disk
);
1380 bdev
->bd_write_holder
= false;
1382 disk_check_events(bdev
->bd_disk
);
1385 mutex_unlock(&bdev
->bd_mutex
);
1387 disk_check_events(bdev
->bd_disk
);
1389 return __blkdev_put(bdev
, mode
, 0);
1391 EXPORT_SYMBOL(blkdev_put
);
1393 static int blkdev_close(struct inode
* inode
, struct file
* filp
)
1395 struct block_device
*bdev
= I_BDEV(filp
->f_mapping
->host
);
1397 return blkdev_put(bdev
, filp
->f_mode
);
1400 static long block_ioctl(struct file
*file
, unsigned cmd
, unsigned long arg
)
1402 struct block_device
*bdev
= I_BDEV(file
->f_mapping
->host
);
1403 fmode_t mode
= file
->f_mode
;
1406 * O_NDELAY can be altered using fcntl(.., F_SETFL, ..), so we have
1407 * to updated it before every ioctl.
1409 if (file
->f_flags
& O_NDELAY
)
1410 mode
|= FMODE_NDELAY
;
1412 mode
&= ~FMODE_NDELAY
;
1414 return blkdev_ioctl(bdev
, mode
, cmd
, arg
);
1418 * Write data to the block device. Only intended for the block device itself
1419 * and the raw driver which basically is a fake block device.
1421 * Does not take i_mutex for the write and thus is not for general purpose
1424 ssize_t
blkdev_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1425 unsigned long nr_segs
, loff_t pos
)
1427 struct file
*file
= iocb
->ki_filp
;
1430 BUG_ON(iocb
->ki_pos
!= pos
);
1432 ret
= __generic_file_aio_write(iocb
, iov
, nr_segs
, &iocb
->ki_pos
);
1433 if (ret
> 0 || ret
== -EIOCBQUEUED
) {
1436 err
= generic_write_sync(file
, pos
, ret
);
1437 if (err
< 0 && ret
> 0)
1442 EXPORT_SYMBOL_GPL(blkdev_aio_write
);
1445 * Try to release a page associated with block device when the system
1446 * is under memory pressure.
1448 static int blkdev_releasepage(struct page
*page
, gfp_t wait
)
1450 struct super_block
*super
= BDEV_I(page
->mapping
->host
)->bdev
.bd_super
;
1452 if (super
&& super
->s_op
->bdev_try_to_free_page
)
1453 return super
->s_op
->bdev_try_to_free_page(super
, page
, wait
);
1455 return try_to_free_buffers(page
);
1458 static const struct address_space_operations def_blk_aops
= {
1459 .readpage
= blkdev_readpage
,
1460 .writepage
= blkdev_writepage
,
1461 .sync_page
= block_sync_page
,
1462 .write_begin
= blkdev_write_begin
,
1463 .write_end
= blkdev_write_end
,
1464 .writepages
= generic_writepages
,
1465 .releasepage
= blkdev_releasepage
,
1466 .direct_IO
= blkdev_direct_IO
,
1469 const struct file_operations def_blk_fops
= {
1470 .open
= blkdev_open
,
1471 .release
= blkdev_close
,
1472 .llseek
= block_llseek
,
1473 .read
= do_sync_read
,
1474 .write
= do_sync_write
,
1475 .aio_read
= generic_file_aio_read
,
1476 .aio_write
= blkdev_aio_write
,
1477 .mmap
= generic_file_mmap
,
1478 .fsync
= blkdev_fsync
,
1479 .unlocked_ioctl
= block_ioctl
,
1480 #ifdef CONFIG_COMPAT
1481 .compat_ioctl
= compat_blkdev_ioctl
,
1483 .splice_read
= generic_file_splice_read
,
1484 .splice_write
= generic_file_splice_write
,
1487 int ioctl_by_bdev(struct block_device
*bdev
, unsigned cmd
, unsigned long arg
)
1490 mm_segment_t old_fs
= get_fs();
1492 res
= blkdev_ioctl(bdev
, 0, cmd
, arg
);
1497 EXPORT_SYMBOL(ioctl_by_bdev
);
1500 * lookup_bdev - lookup a struct block_device by name
1501 * @pathname: special file representing the block device
1503 * Get a reference to the blockdevice at @pathname in the current
1504 * namespace if possible and return it. Return ERR_PTR(error)
1507 struct block_device
*lookup_bdev(const char *pathname
)
1509 struct block_device
*bdev
;
1510 struct inode
*inode
;
1514 if (!pathname
|| !*pathname
)
1515 return ERR_PTR(-EINVAL
);
1517 error
= kern_path(pathname
, LOOKUP_FOLLOW
, &path
);
1519 return ERR_PTR(error
);
1521 inode
= path
.dentry
->d_inode
;
1523 if (!S_ISBLK(inode
->i_mode
))
1526 if (path
.mnt
->mnt_flags
& MNT_NODEV
)
1529 bdev
= bd_acquire(inode
);
1536 bdev
= ERR_PTR(error
);
1539 EXPORT_SYMBOL(lookup_bdev
);
1541 int __invalidate_device(struct block_device
*bdev
)
1543 struct super_block
*sb
= get_super(bdev
);
1548 * no need to lock the super, get_super holds the
1549 * read mutex so the filesystem cannot go away
1550 * under us (->put_super runs with the write lock
1553 shrink_dcache_sb(sb
);
1554 res
= invalidate_inodes(sb
);
1557 invalidate_bdev(bdev
);
1560 EXPORT_SYMBOL(__invalidate_device
);