10 #include <linux/fcntl.h>
11 #include <linux/slab.h>
17 #include <linux/module.h>
19 #include <linux/magic.h>
26 #include <linux/uio.h>
30 #include <asm/uaccess.h>
47 return &BDEV_I(inode)->bdev;
56 static void bdev_inode_switch_bdi(
struct inode *
inode,
68 spin_unlock(&inode->
i_lock);
69 spin_unlock(&old->
wb.list_lock);
70 spin_unlock(&dst->
wb.list_lock);
100 cleancache_invalidate_inode(mapping);
111 if (size < bdev_logical_block_size(bdev))
118 bdev->
bd_inode->i_blkbits = blksize_bits(size);
150 blkdev_get_block(
struct inode *inode,
sector_t iblock,
151 struct buffer_head *bh,
int create)
153 bh->b_bdev =
I_BDEV(inode);
154 bh->b_blocknr = iblock;
155 set_buffer_mapped(bh);
161 loff_t
offset,
unsigned long nr_segs)
164 struct inode *inode = file->
f_mapping->host;
167 nr_segs, blkdev_get_block,
NULL,
NULL, 0);
244 return ERR_PTR(error);
293 static int blkdev_readpage(
struct file * file,
struct page *
page)
299 loff_t
pos,
unsigned len,
unsigned flags,
300 struct page **pagep,
void **fsdata)
307 loff_t
pos,
unsigned len,
unsigned copied,
324 static loff_t block_llseek(
struct file *file, loff_t offset,
int origin)
326 struct inode *bd_inode = file->
f_mapping->host;
331 size = i_size_read(bd_inode);
339 offset += file->
f_pos;
345 if (offset >= 0 && offset <= size) {
346 if (offset != file->
f_pos) {
358 struct inode *bd_inode = filp->
f_mapping->host;
386 static struct inode *bdev_alloc_inode(
struct super_block *
sb)
402 static void bdev_destroy_inode(
struct inode *inode)
407 static void init_once(
void *
foo)
412 memset(bdev, 0,
sizeof(*bdev));
415 INIT_LIST_HEAD(&bdev->
bd_list);
417 INIT_LIST_HEAD(&bdev->bd_holder_disks);
424 static inline void __bd_forget(
struct inode *inode)
431 static void bdev_evict_inode(
struct inode *inode)
438 spin_lock(&bdev_lock);
440 __bd_forget(
list_entry(p,
struct inode, i_devices));
443 spin_unlock(&bdev_lock);
448 .alloc_inode = bdev_alloc_inode,
449 .destroy_inode = bdev_destroy_inode,
451 .evict_inode = bdev_evict_inode,
455 int flags,
const char *dev_name,
void *
data)
479 panic(
"Cannot register bdev pseudo-fs");
482 panic(
"Cannot create bdev pseudo-fs");
483 blockdev_superblock = bd_mnt->
mnt_sb;
496 static int bdev_test(
struct inode *inode,
void *
data)
498 return BDEV_I(inode)->bdev.bd_dev == *(
dev_t *)data;
501 static int bdev_set(
struct inode *inode,
void *
data)
503 BDEV_I(inode)->bdev.bd_dev = *(
dev_t *)data;
515 bdev_test, bdev_set, &dev);
520 bdev = &BDEV_I(inode)->bdev;
532 inode->
i_data.a_ops = &def_blk_aops;
535 spin_lock(&bdev_lock);
536 list_add(&bdev->
bd_list, &all_bdevs);
537 spin_unlock(&bdev_lock);
559 spin_lock(&bdev_lock);
561 ret += bdev->
bd_inode->i_mapping->nrpages;
563 spin_unlock(&bdev_lock);
574 static struct block_device *bd_acquire(
struct inode *inode)
578 spin_lock(&bdev_lock);
582 spin_unlock(&bdev_lock);
585 spin_unlock(&bdev_lock);
589 spin_lock(&bdev_lock);
602 spin_unlock(&bdev_lock);
607 static inline int sb_is_blkdev_sb(
struct super_block *sb)
609 return sb == blockdev_superblock;
618 spin_lock(&bdev_lock);
620 if (!sb_is_blkdev_sb(inode->
i_sb))
624 spin_unlock(&bdev_lock);
654 else if (whole->
bd_holder == bd_may_claim)
680 static int bd_prepare_to_claim(
struct block_device *bdev,
685 if (!bd_may_claim(bdev, whole, holder))
694 spin_unlock(&bdev_lock);
697 spin_lock(&bdev_lock);
731 struct gendisk *disk;
743 return ERR_PTR(-
ENXIO);
758 module_put(disk->fops->owner);
764 spin_lock(&bdev_lock);
766 err = bd_prepare_to_claim(bdev, whole, holder);
769 spin_unlock(&bdev_lock);
772 spin_unlock(&bdev_lock);
779 struct bd_holder_disk {
781 struct gendisk *disk;
785 static struct bd_holder_disk *bd_find_holder_disk(
struct block_device *bdev,
786 struct gendisk *disk)
788 struct bd_holder_disk *holder;
791 if (holder->disk == disk)
834 int bd_link_disk_holder(
struct block_device *bdev,
struct gendisk *disk)
836 struct bd_holder_disk *holder;
847 holder = bd_find_holder_disk(bdev, disk);
853 holder = kzalloc(
sizeof(*holder),
GFP_KERNEL);
859 INIT_LIST_HEAD(&holder->list);
863 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->
bd_part)->kobj);
867 ret = add_symlink(bdev->
bd_part->holder_dir, &disk_to_dev(disk)->kobj);
876 list_add(&holder->list, &bdev->bd_holder_disks);
880 del_symlink(disk->slave_dir, &part_to_dev(bdev->
bd_part)->kobj);
899 void bd_unlink_disk_holder(
struct block_device *bdev,
struct gendisk *disk)
901 struct bd_holder_disk *holder;
905 holder = bd_find_holder_disk(bdev, disk);
907 if (!
WARN_ON_ONCE(holder == NULL) && !--holder->refcnt) {
908 del_symlink(disk->slave_dir, &part_to_dev(bdev->
bd_part)->kobj);
909 del_symlink(bdev->
bd_part->holder_dir,
910 &disk_to_dev(disk)->kobj);
912 list_del_init(&holder->list);
931 static void flush_disk(
struct block_device *bdev,
bool kill_dirty)
939 "resized disk %s\n", name);
944 if (disk_part_scan_enabled(bdev->
bd_disk))
958 loff_t disk_size, bdev_size;
960 disk_size = (loff_t)get_capacity(disk) << 9;
961 bdev_size = i_size_read(bdev->
bd_inode);
962 if (disk_size != bdev_size) {
967 "%s: detected capacity change from %lld to %lld\n",
968 name, bdev_size, disk_size);
969 i_size_write(bdev->
bd_inode, disk_size);
970 flush_disk(bdev,
false);
988 if (disk->fops->revalidate_disk)
989 ret = disk->fops->revalidate_disk(disk);
1014 struct gendisk *disk = bdev->
bd_disk;
1015 const struct block_device_operations *bdops = disk->fops;
1019 DISK_EVENT_EJECT_REQUEST);
1020 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1023 flush_disk(bdev,
true);
1024 if (bdops->revalidate_disk)
1025 bdops->revalidate_disk(bdev->
bd_disk);
1033 unsigned bsize = bdev_logical_block_size(bdev);
1042 bdev->
bd_inode->i_blkbits = blksize_bits(bsize);
1057 struct gendisk *disk;
1071 ret = devcgroup_inode_permission(bdev->
bd_inode, perm);
1084 owner = disk->fops->owner;
1101 if (disk->fops->open) {
1102 ret = disk->fops->open(bdev, mode);
1125 bdev_inode_switch_bdi(bdev->
bd_inode, bdi);
1149 ret = __blkdev_get(whole, mode, 1);
1153 bdev_inode_switch_bdi(bdev->
bd_inode,
1154 whole->
bd_inode->i_data.backing_dev_info);
1156 if (!(disk->flags & GENHD_FL_UP) ||
1166 if (bdev->
bd_disk->fops->open)
1167 ret = bdev->
bd_disk->fops->open(bdev, mode);
1176 goto out_unlock_bdev;
1235 if ((mode & FMODE_EXCL) && holder) {
1236 whole = bd_start_claiming(bdev, holder);
1237 if (IS_ERR(whole)) {
1239 return PTR_ERR(whole);
1243 res = __blkdev_get(bdev, mode, 0);
1246 struct gendisk *disk = whole->
bd_disk;
1250 spin_lock(&bdev_lock);
1253 BUG_ON(!bd_may_claim(bdev, whole, holder));
1271 spin_unlock(&bdev_lock);
1281 (disk->flags & GENHD_FL_BLOCK_EVENTS_ON_EXCL_WRITE)) {
1323 return ERR_PTR(err);
1367 return ERR_PTR(err);
1373 static int blkdev_open(
struct inode * inode,
struct file * filp)
1392 bdev = bd_acquire(inode);
1404 struct gendisk *disk = bdev->
bd_disk;
1418 bdev_inode_switch_bdi(bdev->
bd_inode,
1422 if (disk->fops->release)
1423 ret = disk->fops->release(disk, mode);
1426 struct module *owner = disk->fops->owner;
1441 __blkdev_put(victim, mode, 1);
1457 spin_lock(&bdev_lock);
1468 spin_unlock(&bdev_lock);
1489 return __blkdev_put(bdev, mode, 0);
1493 static int blkdev_close(
struct inode * inode,
struct file * filp)
1500 static long block_ioctl(
struct file *file,
unsigned cmd,
unsigned long arg)
1525 unsigned long nr_segs, loff_t
pos)
1527 struct file *file = iocb->
ki_filp;
1539 if (err < 0 && ret > 0)
1548 unsigned long nr_segs, loff_t
pos)
1550 struct file *file = iocb->
ki_filp;
1551 struct inode *bd_inode = file->
f_mapping->host;
1552 loff_t size = i_size_read(bd_inode);
1571 if (super && super->
s_op->bdev_try_to_free_page)
1572 return super->
s_op->bdev_try_to_free_page(super, page, wait);
1578 .readpage = blkdev_readpage,
1579 .writepage = blkdev_writepage,
1580 .write_begin = blkdev_write_begin,
1581 .write_end = blkdev_write_end,
1583 .releasepage = blkdev_releasepage,
1584 .direct_IO = blkdev_direct_IO,
1588 .open = blkdev_open,
1589 .release = blkdev_close,
1590 .llseek = block_llseek,
1593 .aio_read = blkdev_aio_read,
1597 .unlocked_ioctl = block_ioctl,
1598 #ifdef CONFIG_COMPAT
1628 struct inode *
inode;
1632 if (!pathname || !*pathname)
1637 return ERR_PTR(error);
1639 inode = path.
dentry->d_inode;
1647 bdev = bd_acquire(inode);
1654 bdev = ERR_PTR(error);
1688 spin_lock(&inode->
i_lock);
1691 spin_unlock(&inode->
i_lock);
1695 spin_unlock(&inode->
i_lock);