aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--block/ioctl.c5
-rw-r--r--drivers/block/drbd/drbd_int.h2
-rw-r--r--drivers/block/drbd/drbd_main.c7
-rw-r--r--drivers/block/drbd/drbd_nl.c103
-rw-r--r--drivers/block/pktcdvd.c22
-rw-r--r--drivers/char/raw.c14
-rw-r--r--drivers/md/dm-table.c20
-rw-r--r--drivers/md/md.c16
-rw-r--r--drivers/mtd/devices/block2mtd.c10
-rw-r--r--drivers/s390/block/dasd_genhd.c2
-rw-r--r--drivers/usb/gadget/storage_common.c7
-rw-r--r--fs/block_dev.c708
-rw-r--r--fs/btrfs/volumes.c28
-rw-r--r--fs/btrfs/volumes.h2
-rw-r--r--fs/ext3/super.c12
-rw-r--r--fs/ext4/super.c12
-rw-r--r--fs/gfs2/ops_fstype.c8
-rw-r--r--fs/jfs/jfs_logmgr.c17
-rw-r--r--fs/logfs/dev_bdev.c7
-rw-r--r--fs/nilfs2/super.c8
-rw-r--r--fs/ocfs2/cluster/heartbeat.c2
-rw-r--r--fs/partitions/check.c2
-rw-r--r--fs/reiserfs/journal.c21
-rw-r--r--fs/super.c19
-rw-r--r--fs/xfs/linux-2.6/xfs_super.c5
-rw-r--r--include/linux/fs.h25
-rw-r--r--kernel/power/swap.c5
-rw-r--r--mm/swapfile.c7
28 files changed, 377 insertions, 719 deletions
diff --git a/block/ioctl.c b/block/ioctl.c
index 3d866d0037f2..fefa9a496708 100644
--- a/block/ioctl.c
+++ b/block/ioctl.c
@@ -295,11 +295,12 @@ int blkdev_ioctl(struct block_device *bdev, fmode_t mode, unsigned cmd,
295 return -EINVAL; 295 return -EINVAL;
296 if (get_user(n, (int __user *) arg)) 296 if (get_user(n, (int __user *) arg))
297 return -EFAULT; 297 return -EFAULT;
298 if (!(mode & FMODE_EXCL) && bd_claim(bdev, &bdev) < 0) 298 if (!(mode & FMODE_EXCL) &&
299 blkdev_get(bdev, mode | FMODE_EXCL, &bdev) < 0)
299 return -EBUSY; 300 return -EBUSY;
300 ret = set_blocksize(bdev, n); 301 ret = set_blocksize(bdev, n);
301 if (!(mode & FMODE_EXCL)) 302 if (!(mode & FMODE_EXCL))
302 bd_release(bdev); 303 blkdev_put(bdev, mode | FMODE_EXCL);
303 return ret; 304 return ret;
304 case BLKPG: 305 case BLKPG:
305 ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg); 306 ret = blkpg_ioctl(bdev, (struct blkpg_ioctl_arg __user *) arg);
diff --git a/drivers/block/drbd/drbd_int.h b/drivers/block/drbd/drbd_int.h
index 1ea1a34e78b2..3803a0348937 100644
--- a/drivers/block/drbd/drbd_int.h
+++ b/drivers/block/drbd/drbd_int.h
@@ -911,8 +911,6 @@ struct drbd_md {
911struct drbd_backing_dev { 911struct drbd_backing_dev {
912 struct block_device *backing_bdev; 912 struct block_device *backing_bdev;
913 struct block_device *md_bdev; 913 struct block_device *md_bdev;
914 struct file *lo_file;
915 struct file *md_file;
916 struct drbd_md md; 914 struct drbd_md md;
917 struct disk_conf dc; /* The user provided config... */ 915 struct disk_conf dc; /* The user provided config... */
918 sector_t known_size; /* last known size of that backing device */ 916 sector_t known_size; /* last known size of that backing device */
diff --git a/drivers/block/drbd/drbd_main.c b/drivers/block/drbd/drbd_main.c
index 6be5401d0e88..29cd0dc9fe4f 100644
--- a/drivers/block/drbd/drbd_main.c
+++ b/drivers/block/drbd/drbd_main.c
@@ -3372,11 +3372,8 @@ void drbd_free_bc(struct drbd_backing_dev *ldev)
3372 if (ldev == NULL) 3372 if (ldev == NULL)
3373 return; 3373 return;
3374 3374
3375 bd_release(ldev->backing_bdev); 3375 blkdev_put(ldev->backing_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
3376 bd_release(ldev->md_bdev); 3376 blkdev_put(ldev->md_bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
3377
3378 fput(ldev->lo_file);
3379 fput(ldev->md_file);
3380 3377
3381 kfree(ldev); 3378 kfree(ldev);
3382} 3379}
diff --git a/drivers/block/drbd/drbd_nl.c b/drivers/block/drbd/drbd_nl.c
index 29e5c70e4e26..8cbfaa687d72 100644
--- a/drivers/block/drbd/drbd_nl.c
+++ b/drivers/block/drbd/drbd_nl.c
@@ -855,7 +855,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
855 sector_t max_possible_sectors; 855 sector_t max_possible_sectors;
856 sector_t min_md_device_sectors; 856 sector_t min_md_device_sectors;
857 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */ 857 struct drbd_backing_dev *nbc = NULL; /* new_backing_conf */
858 struct inode *inode, *inode2; 858 struct block_device *bdev;
859 struct lru_cache *resync_lru = NULL; 859 struct lru_cache *resync_lru = NULL;
860 union drbd_state ns, os; 860 union drbd_state ns, os;
861 unsigned int max_seg_s; 861 unsigned int max_seg_s;
@@ -907,46 +907,40 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
907 } 907 }
908 } 908 }
909 909
910 nbc->lo_file = filp_open(nbc->dc.backing_dev, O_RDWR, 0); 910 bdev = blkdev_get_by_path(nbc->dc.backing_dev,
911 if (IS_ERR(nbc->lo_file)) { 911 FMODE_READ | FMODE_WRITE | FMODE_EXCL, mdev);
912 if (IS_ERR(bdev)) {
912 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev, 913 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.backing_dev,
913 PTR_ERR(nbc->lo_file)); 914 PTR_ERR(bdev));
914 nbc->lo_file = NULL;
915 retcode = ERR_OPEN_DISK; 915 retcode = ERR_OPEN_DISK;
916 goto fail; 916 goto fail;
917 } 917 }
918 nbc->backing_bdev = bdev;
918 919
919 inode = nbc->lo_file->f_dentry->d_inode; 920 /*
920 921 * meta_dev_idx >= 0: external fixed size, possibly multiple
921 if (!S_ISBLK(inode->i_mode)) { 922 * drbd sharing one meta device. TODO in that case, paranoia
922 retcode = ERR_DISK_NOT_BDEV; 923 * check that [md_bdev, meta_dev_idx] is not yet used by some
923 goto fail; 924 * other drbd minor! (if you use drbd.conf + drbdadm, that
924 } 925 * should check it for you already; but if you don't, or
925 926 * someone fooled it, we need to double check here)
926 nbc->md_file = filp_open(nbc->dc.meta_dev, O_RDWR, 0); 927 */
927 if (IS_ERR(nbc->md_file)) { 928 bdev = blkdev_get_by_path(nbc->dc.meta_dev,
929 FMODE_READ | FMODE_WRITE | FMODE_EXCL,
930 (nbc->dc.meta_dev_idx < 0) ?
931 (void *)mdev : (void *)drbd_m_holder);
932 if (IS_ERR(bdev)) {
928 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev, 933 dev_err(DEV, "open(\"%s\") failed with %ld\n", nbc->dc.meta_dev,
929 PTR_ERR(nbc->md_file)); 934 PTR_ERR(bdev));
930 nbc->md_file = NULL;
931 retcode = ERR_OPEN_MD_DISK; 935 retcode = ERR_OPEN_MD_DISK;
932 goto fail; 936 goto fail;
933 } 937 }
938 nbc->md_bdev = bdev;
934 939
935 inode2 = nbc->md_file->f_dentry->d_inode; 940 if ((nbc->backing_bdev == nbc->md_bdev) !=
936 941 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
937 if (!S_ISBLK(inode2->i_mode)) { 942 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
938 retcode = ERR_MD_NOT_BDEV; 943 retcode = ERR_MD_IDX_INVALID;
939 goto fail;
940 }
941
942 nbc->backing_bdev = inode->i_bdev;
943 if (bd_claim(nbc->backing_bdev, mdev)) {
944 printk(KERN_ERR "drbd: bd_claim(%p,%p); failed [%p;%p;%u]\n",
945 nbc->backing_bdev, mdev,
946 nbc->backing_bdev->bd_holder,
947 nbc->backing_bdev->bd_contains->bd_holder,
948 nbc->backing_bdev->bd_holders);
949 retcode = ERR_BDCLAIM_DISK;
950 goto fail; 944 goto fail;
951 } 945 }
952 946
@@ -955,28 +949,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
955 offsetof(struct bm_extent, lce)); 949 offsetof(struct bm_extent, lce));
956 if (!resync_lru) { 950 if (!resync_lru) {
957 retcode = ERR_NOMEM; 951 retcode = ERR_NOMEM;
958 goto release_bdev_fail; 952 goto fail;
959 }
960
961 /* meta_dev_idx >= 0: external fixed size,
962 * possibly multiple drbd sharing one meta device.
963 * TODO in that case, paranoia check that [md_bdev, meta_dev_idx] is
964 * not yet used by some other drbd minor!
965 * (if you use drbd.conf + drbdadm,
966 * that should check it for you already; but if you don't, or someone
967 * fooled it, we need to double check here) */
968 nbc->md_bdev = inode2->i_bdev;
969 if (bd_claim(nbc->md_bdev, (nbc->dc.meta_dev_idx < 0) ? (void *)mdev
970 : (void *) drbd_m_holder)) {
971 retcode = ERR_BDCLAIM_MD_DISK;
972 goto release_bdev_fail;
973 }
974
975 if ((nbc->backing_bdev == nbc->md_bdev) !=
976 (nbc->dc.meta_dev_idx == DRBD_MD_INDEX_INTERNAL ||
977 nbc->dc.meta_dev_idx == DRBD_MD_INDEX_FLEX_INT)) {
978 retcode = ERR_MD_IDX_INVALID;
979 goto release_bdev2_fail;
980 } 953 }
981 954
982 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */ 955 /* RT - for drbd_get_max_capacity() DRBD_MD_INDEX_FLEX_INT */
@@ -987,7 +960,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
987 (unsigned long long) drbd_get_max_capacity(nbc), 960 (unsigned long long) drbd_get_max_capacity(nbc),
988 (unsigned long long) nbc->dc.disk_size); 961 (unsigned long long) nbc->dc.disk_size);
989 retcode = ERR_DISK_TO_SMALL; 962 retcode = ERR_DISK_TO_SMALL;
990 goto release_bdev2_fail; 963 goto fail;
991 } 964 }
992 965
993 if (nbc->dc.meta_dev_idx < 0) { 966 if (nbc->dc.meta_dev_idx < 0) {
@@ -1004,7 +977,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
1004 dev_warn(DEV, "refusing attach: md-device too small, " 977 dev_warn(DEV, "refusing attach: md-device too small, "
1005 "at least %llu sectors needed for this meta-disk type\n", 978 "at least %llu sectors needed for this meta-disk type\n",
1006 (unsigned long long) min_md_device_sectors); 979 (unsigned long long) min_md_device_sectors);
1007 goto release_bdev2_fail; 980 goto fail;
1008 } 981 }
1009 982
1010 /* Make sure the new disk is big enough 983 /* Make sure the new disk is big enough
@@ -1012,7 +985,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
1012 if (drbd_get_max_capacity(nbc) < 985 if (drbd_get_max_capacity(nbc) <
1013 drbd_get_capacity(mdev->this_bdev)) { 986 drbd_get_capacity(mdev->this_bdev)) {
1014 retcode = ERR_DISK_TO_SMALL; 987 retcode = ERR_DISK_TO_SMALL;
1015 goto release_bdev2_fail; 988 goto fail;
1016 } 989 }
1017 990
1018 nbc->known_size = drbd_get_capacity(nbc->backing_bdev); 991 nbc->known_size = drbd_get_capacity(nbc->backing_bdev);
@@ -1035,7 +1008,7 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
1035 retcode = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE); 1008 retcode = _drbd_request_state(mdev, NS(disk, D_ATTACHING), CS_VERBOSE);
1036 drbd_resume_io(mdev); 1009 drbd_resume_io(mdev);
1037 if (retcode < SS_SUCCESS) 1010 if (retcode < SS_SUCCESS)
1038 goto release_bdev2_fail; 1011 goto fail;
1039 1012
1040 if (!get_ldev_if_state(mdev, D_ATTACHING)) 1013 if (!get_ldev_if_state(mdev, D_ATTACHING))
1041 goto force_diskless; 1014 goto force_diskless;
@@ -1269,18 +1242,14 @@ static int drbd_nl_disk_conf(struct drbd_conf *mdev, struct drbd_nl_cfg_req *nlp
1269 force_diskless: 1242 force_diskless:
1270 drbd_force_state(mdev, NS(disk, D_FAILED)); 1243 drbd_force_state(mdev, NS(disk, D_FAILED));
1271 drbd_md_sync(mdev); 1244 drbd_md_sync(mdev);
1272 release_bdev2_fail:
1273 if (nbc)
1274 bd_release(nbc->md_bdev);
1275 release_bdev_fail:
1276 if (nbc)
1277 bd_release(nbc->backing_bdev);
1278 fail: 1245 fail:
1279 if (nbc) { 1246 if (nbc) {
1280 if (nbc->lo_file) 1247 if (nbc->backing_bdev)
1281 fput(nbc->lo_file); 1248 blkdev_put(nbc->backing_bdev,
1282 if (nbc->md_file) 1249 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1283 fput(nbc->md_file); 1250 if (nbc->md_bdev)
1251 blkdev_put(nbc->md_bdev,
1252 FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1284 kfree(nbc); 1253 kfree(nbc);
1285 } 1254 }
1286 lc_destroy(resync_lru); 1255 lc_destroy(resync_lru);
diff --git a/drivers/block/pktcdvd.c b/drivers/block/pktcdvd.c
index 19b3568e9326..77d70eebb6b2 100644
--- a/drivers/block/pktcdvd.c
+++ b/drivers/block/pktcdvd.c
@@ -2296,15 +2296,12 @@ static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
2296 * so bdget() can't fail. 2296 * so bdget() can't fail.
2297 */ 2297 */
2298 bdget(pd->bdev->bd_dev); 2298 bdget(pd->bdev->bd_dev);
2299 if ((ret = blkdev_get(pd->bdev, FMODE_READ))) 2299 if ((ret = blkdev_get(pd->bdev, FMODE_READ | FMODE_EXCL, pd)))
2300 goto out; 2300 goto out;
2301 2301
2302 if ((ret = bd_claim(pd->bdev, pd)))
2303 goto out_putdev;
2304
2305 if ((ret = pkt_get_last_written(pd, &lba))) { 2302 if ((ret = pkt_get_last_written(pd, &lba))) {
2306 printk(DRIVER_NAME": pkt_get_last_written failed\n"); 2303 printk(DRIVER_NAME": pkt_get_last_written failed\n");
2307 goto out_unclaim; 2304 goto out_putdev;
2308 } 2305 }
2309 2306
2310 set_capacity(pd->disk, lba << 2); 2307 set_capacity(pd->disk, lba << 2);
@@ -2314,7 +2311,7 @@ static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
2314 q = bdev_get_queue(pd->bdev); 2311 q = bdev_get_queue(pd->bdev);
2315 if (write) { 2312 if (write) {
2316 if ((ret = pkt_open_write(pd))) 2313 if ((ret = pkt_open_write(pd)))
2317 goto out_unclaim; 2314 goto out_putdev;
2318 /* 2315 /*
2319 * Some CDRW drives can not handle writes larger than one packet, 2316 * Some CDRW drives can not handle writes larger than one packet,
2320 * even if the size is a multiple of the packet size. 2317 * even if the size is a multiple of the packet size.
@@ -2329,23 +2326,21 @@ static int pkt_open_dev(struct pktcdvd_device *pd, fmode_t write)
2329 } 2326 }
2330 2327
2331 if ((ret = pkt_set_segment_merging(pd, q))) 2328 if ((ret = pkt_set_segment_merging(pd, q)))
2332 goto out_unclaim; 2329 goto out_putdev;
2333 2330
2334 if (write) { 2331 if (write) {
2335 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) { 2332 if (!pkt_grow_pktlist(pd, CONFIG_CDROM_PKTCDVD_BUFFERS)) {
2336 printk(DRIVER_NAME": not enough memory for buffers\n"); 2333 printk(DRIVER_NAME": not enough memory for buffers\n");
2337 ret = -ENOMEM; 2334 ret = -ENOMEM;
2338 goto out_unclaim; 2335 goto out_putdev;
2339 } 2336 }
2340 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1); 2337 printk(DRIVER_NAME": %lukB available on disc\n", lba << 1);
2341 } 2338 }
2342 2339
2343 return 0; 2340 return 0;
2344 2341
2345out_unclaim:
2346 bd_release(pd->bdev);
2347out_putdev: 2342out_putdev:
2348 blkdev_put(pd->bdev, FMODE_READ); 2343 blkdev_put(pd->bdev, FMODE_READ | FMODE_EXCL);
2349out: 2344out:
2350 return ret; 2345 return ret;
2351} 2346}
@@ -2362,8 +2357,7 @@ static void pkt_release_dev(struct pktcdvd_device *pd, int flush)
2362 pkt_lock_door(pd, 0); 2357 pkt_lock_door(pd, 0);
2363 2358
2364 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED); 2359 pkt_set_speed(pd, MAX_SPEED, MAX_SPEED);
2365 bd_release(pd->bdev); 2360 blkdev_put(pd->bdev, FMODE_READ | FMODE_EXCL);
2366 blkdev_put(pd->bdev, FMODE_READ);
2367 2361
2368 pkt_shrink_pktlist(pd); 2362 pkt_shrink_pktlist(pd);
2369} 2363}
@@ -2733,7 +2727,7 @@ static int pkt_new_dev(struct pktcdvd_device *pd, dev_t dev)
2733 bdev = bdget(dev); 2727 bdev = bdget(dev);
2734 if (!bdev) 2728 if (!bdev)
2735 return -ENOMEM; 2729 return -ENOMEM;
2736 ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY); 2730 ret = blkdev_get(bdev, FMODE_READ | FMODE_NDELAY, NULL);
2737 if (ret) 2731 if (ret)
2738 return ret; 2732 return ret;
2739 2733
diff --git a/drivers/char/raw.c b/drivers/char/raw.c
index bfe25ea9766b..b4b9d5a47885 100644
--- a/drivers/char/raw.c
+++ b/drivers/char/raw.c
@@ -65,15 +65,12 @@ static int raw_open(struct inode *inode, struct file *filp)
65 if (!bdev) 65 if (!bdev)
66 goto out; 66 goto out;
67 igrab(bdev->bd_inode); 67 igrab(bdev->bd_inode);
68 err = blkdev_get(bdev, filp->f_mode); 68 err = blkdev_get(bdev, filp->f_mode | FMODE_EXCL, raw_open);
69 if (err) 69 if (err)
70 goto out; 70 goto out;
71 err = bd_claim(bdev, raw_open);
72 if (err)
73 goto out1;
74 err = set_blocksize(bdev, bdev_logical_block_size(bdev)); 71 err = set_blocksize(bdev, bdev_logical_block_size(bdev));
75 if (err) 72 if (err)
76 goto out2; 73 goto out1;
77 filp->f_flags |= O_DIRECT; 74 filp->f_flags |= O_DIRECT;
78 filp->f_mapping = bdev->bd_inode->i_mapping; 75 filp->f_mapping = bdev->bd_inode->i_mapping;
79 if (++raw_devices[minor].inuse == 1) 76 if (++raw_devices[minor].inuse == 1)
@@ -83,10 +80,8 @@ static int raw_open(struct inode *inode, struct file *filp)
83 mutex_unlock(&raw_mutex); 80 mutex_unlock(&raw_mutex);
84 return 0; 81 return 0;
85 82
86out2:
87 bd_release(bdev);
88out1: 83out1:
89 blkdev_put(bdev, filp->f_mode); 84 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
90out: 85out:
91 mutex_unlock(&raw_mutex); 86 mutex_unlock(&raw_mutex);
92 return err; 87 return err;
@@ -110,8 +105,7 @@ static int raw_release(struct inode *inode, struct file *filp)
110 } 105 }
111 mutex_unlock(&raw_mutex); 106 mutex_unlock(&raw_mutex);
112 107
113 bd_release(bdev); 108 blkdev_put(bdev, filp->f_mode | FMODE_EXCL);
114 blkdev_put(bdev, filp->f_mode);
115 return 0; 109 return 0;
116} 110}
117 111
diff --git a/drivers/md/dm-table.c b/drivers/md/dm-table.c
index 90267f8d64ee..67150c32986c 100644
--- a/drivers/md/dm-table.c
+++ b/drivers/md/dm-table.c
@@ -325,15 +325,18 @@ static int open_dev(struct dm_dev_internal *d, dev_t dev,
325 325
326 BUG_ON(d->dm_dev.bdev); 326 BUG_ON(d->dm_dev.bdev);
327 327
328 bdev = open_by_devnum(dev, d->dm_dev.mode); 328 bdev = blkdev_get_by_dev(dev, d->dm_dev.mode | FMODE_EXCL, _claim_ptr);
329 if (IS_ERR(bdev)) 329 if (IS_ERR(bdev))
330 return PTR_ERR(bdev); 330 return PTR_ERR(bdev);
331 r = bd_claim_by_disk(bdev, _claim_ptr, dm_disk(md)); 331
332 if (r) 332 r = bd_link_disk_holder(bdev, dm_disk(md));
333 blkdev_put(bdev, d->dm_dev.mode); 333 if (r) {
334 else 334 blkdev_put(bdev, d->dm_dev.mode | FMODE_EXCL);
335 d->dm_dev.bdev = bdev; 335 return r;
336 return r; 336 }
337
338 d->dm_dev.bdev = bdev;
339 return 0;
337} 340}
338 341
339/* 342/*
@@ -344,8 +347,7 @@ static void close_dev(struct dm_dev_internal *d, struct mapped_device *md)
344 if (!d->dm_dev.bdev) 347 if (!d->dm_dev.bdev)
345 return; 348 return;
346 349
347 bd_release_from_disk(d->dm_dev.bdev, dm_disk(md)); 350 blkdev_put(d->dm_dev.bdev, d->dm_dev.mode | FMODE_EXCL);
348 blkdev_put(d->dm_dev.bdev, d->dm_dev.mode);
349 d->dm_dev.bdev = NULL; 351 d->dm_dev.bdev = NULL;
350} 352}
351 353
diff --git a/drivers/md/md.c b/drivers/md/md.c
index 324a3663fcda..3bacccab1b8c 100644
--- a/drivers/md/md.c
+++ b/drivers/md/md.c
@@ -1880,7 +1880,7 @@ static int bind_rdev_to_array(mdk_rdev_t * rdev, mddev_t * mddev)
1880 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state"); 1880 rdev->sysfs_state = sysfs_get_dirent_safe(rdev->kobj.sd, "state");
1881 1881
1882 list_add_rcu(&rdev->same_set, &mddev->disks); 1882 list_add_rcu(&rdev->same_set, &mddev->disks);
1883 bd_claim_by_disk(rdev->bdev, rdev->bdev->bd_holder, mddev->gendisk); 1883 bd_link_disk_holder(rdev->bdev, mddev->gendisk);
1884 1884
1885 /* May as well allow recovery to be retried once */ 1885 /* May as well allow recovery to be retried once */
1886 mddev->recovery_disabled = 0; 1886 mddev->recovery_disabled = 0;
@@ -1907,7 +1907,6 @@ static void unbind_rdev_from_array(mdk_rdev_t * rdev)
1907 MD_BUG(); 1907 MD_BUG();
1908 return; 1908 return;
1909 } 1909 }
1910 bd_release_from_disk(rdev->bdev, rdev->mddev->gendisk);
1911 list_del_rcu(&rdev->same_set); 1910 list_del_rcu(&rdev->same_set);
1912 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b)); 1911 printk(KERN_INFO "md: unbind<%s>\n", bdevname(rdev->bdev,b));
1913 rdev->mddev = NULL; 1912 rdev->mddev = NULL;
@@ -1935,19 +1934,13 @@ static int lock_rdev(mdk_rdev_t *rdev, dev_t dev, int shared)
1935 struct block_device *bdev; 1934 struct block_device *bdev;
1936 char b[BDEVNAME_SIZE]; 1935 char b[BDEVNAME_SIZE];
1937 1936
1938 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); 1937 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1938 shared ? (mdk_rdev_t *)lock_rdev : rdev);
1939 if (IS_ERR(bdev)) { 1939 if (IS_ERR(bdev)) {
1940 printk(KERN_ERR "md: could not open %s.\n", 1940 printk(KERN_ERR "md: could not open %s.\n",
1941 __bdevname(dev, b)); 1941 __bdevname(dev, b));
1942 return PTR_ERR(bdev); 1942 return PTR_ERR(bdev);
1943 } 1943 }
1944 err = bd_claim(bdev, shared ? (mdk_rdev_t *)lock_rdev : rdev);
1945 if (err) {
1946 printk(KERN_ERR "md: could not bd_claim %s.\n",
1947 bdevname(bdev, b));
1948 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1949 return err;
1950 }
1951 if (!shared) 1944 if (!shared)
1952 set_bit(AllReserved, &rdev->flags); 1945 set_bit(AllReserved, &rdev->flags);
1953 rdev->bdev = bdev; 1946 rdev->bdev = bdev;
@@ -1960,8 +1953,7 @@ static void unlock_rdev(mdk_rdev_t *rdev)
1960 rdev->bdev = NULL; 1953 rdev->bdev = NULL;
1961 if (!bdev) 1954 if (!bdev)
1962 MD_BUG(); 1955 MD_BUG();
1963 bd_release(bdev); 1956 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1964 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1965} 1957}
1966 1958
1967void md_autodetect_dev(dev_t dev); 1959void md_autodetect_dev(dev_t dev);
diff --git a/drivers/mtd/devices/block2mtd.c b/drivers/mtd/devices/block2mtd.c
index 2cf0cc6a4189..f29a6f9df6e7 100644
--- a/drivers/mtd/devices/block2mtd.c
+++ b/drivers/mtd/devices/block2mtd.c
@@ -224,7 +224,7 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
224 if (dev->blkdev) { 224 if (dev->blkdev) {
225 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping, 225 invalidate_mapping_pages(dev->blkdev->bd_inode->i_mapping,
226 0, -1); 226 0, -1);
227 close_bdev_exclusive(dev->blkdev, FMODE_READ|FMODE_WRITE); 227 blkdev_put(dev->blkdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
228 } 228 }
229 229
230 kfree(dev); 230 kfree(dev);
@@ -234,6 +234,7 @@ static void block2mtd_free_device(struct block2mtd_dev *dev)
234/* FIXME: ensure that mtd->size % erase_size == 0 */ 234/* FIXME: ensure that mtd->size % erase_size == 0 */
235static struct block2mtd_dev *add_device(char *devname, int erase_size) 235static struct block2mtd_dev *add_device(char *devname, int erase_size)
236{ 236{
237 const fmode_t mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
237 struct block_device *bdev; 238 struct block_device *bdev;
238 struct block2mtd_dev *dev; 239 struct block2mtd_dev *dev;
239 char *name; 240 char *name;
@@ -246,7 +247,7 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
246 return NULL; 247 return NULL;
247 248
248 /* Get a handle on the device */ 249 /* Get a handle on the device */
249 bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, NULL); 250 bdev = blkdev_get_by_path(devname, mode, dev);
250#ifndef MODULE 251#ifndef MODULE
251 if (IS_ERR(bdev)) { 252 if (IS_ERR(bdev)) {
252 253
@@ -254,9 +255,8 @@ static struct block2mtd_dev *add_device(char *devname, int erase_size)
254 to resolve the device name by other means. */ 255 to resolve the device name by other means. */
255 256
256 dev_t devt = name_to_dev_t(devname); 257 dev_t devt = name_to_dev_t(devname);
257 if (devt) { 258 if (devt)
258 bdev = open_by_devnum(devt, FMODE_WRITE | FMODE_READ); 259 bdev = blkdev_get_by_dev(devt, mode, dev);
259 }
260 } 260 }
261#endif 261#endif
262 262
diff --git a/drivers/s390/block/dasd_genhd.c b/drivers/s390/block/dasd_genhd.c
index 30a1ca3d08b7..5505bc07e1e7 100644
--- a/drivers/s390/block/dasd_genhd.c
+++ b/drivers/s390/block/dasd_genhd.c
@@ -103,7 +103,7 @@ int dasd_scan_partitions(struct dasd_block *block)
103 struct block_device *bdev; 103 struct block_device *bdev;
104 104
105 bdev = bdget_disk(block->gdp, 0); 105 bdev = bdget_disk(block->gdp, 0);
106 if (!bdev || blkdev_get(bdev, FMODE_READ) < 0) 106 if (!bdev || blkdev_get(bdev, FMODE_READ, NULL) < 0)
107 return -ENODEV; 107 return -ENODEV;
108 /* 108 /*
109 * See fs/partition/check.c:register_disk,rescan_partitions 109 * See fs/partition/check.c:register_disk,rescan_partitions
diff --git a/drivers/usb/gadget/storage_common.c b/drivers/usb/gadget/storage_common.c
index 3b513bafaf2a..b015561fd602 100644
--- a/drivers/usb/gadget/storage_common.c
+++ b/drivers/usb/gadget/storage_common.c
@@ -543,7 +543,7 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
543 ro = curlun->initially_ro; 543 ro = curlun->initially_ro;
544 if (!ro) { 544 if (!ro) {
545 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0); 545 filp = filp_open(filename, O_RDWR | O_LARGEFILE, 0);
546 if (-EROFS == PTR_ERR(filp)) 546 if (PTR_ERR(filp) == -EROFS || PTR_ERR(filp) == -EACCES)
547 ro = 1; 547 ro = 1;
548 } 548 }
549 if (ro) 549 if (ro)
@@ -558,10 +558,7 @@ static int fsg_lun_open(struct fsg_lun *curlun, const char *filename)
558 558
559 if (filp->f_path.dentry) 559 if (filp->f_path.dentry)
560 inode = filp->f_path.dentry->d_inode; 560 inode = filp->f_path.dentry->d_inode;
561 if (inode && S_ISBLK(inode->i_mode)) { 561 if (!inode || (!S_ISREG(inode->i_mode) && !S_ISBLK(inode->i_mode))) {
562 if (bdev_read_only(inode->i_bdev))
563 ro = 1;
564 } else if (!inode || !S_ISREG(inode->i_mode)) {
565 LINFO(curlun, "invalid file type: %s\n", filename); 562 LINFO(curlun, "invalid file type: %s\n", filename);
566 goto out; 563 goto out;
567 } 564 }
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 06e8ff12b97c..c1c1b8c3fb99 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -426,9 +426,6 @@ static void init_once(void *foo)
426 mutex_init(&bdev->bd_mutex); 426 mutex_init(&bdev->bd_mutex);
427 INIT_LIST_HEAD(&bdev->bd_inodes); 427 INIT_LIST_HEAD(&bdev->bd_inodes);
428 INIT_LIST_HEAD(&bdev->bd_list); 428 INIT_LIST_HEAD(&bdev->bd_list);
429#ifdef CONFIG_SYSFS
430 INIT_LIST_HEAD(&bdev->bd_holder_list);
431#endif
432 inode_init_once(&ei->vfs_inode); 429 inode_init_once(&ei->vfs_inode);
433 /* Initialize mutex for freeze. */ 430 /* Initialize mutex for freeze. */
434 mutex_init(&bdev->bd_fsfreeze_mutex); 431 mutex_init(&bdev->bd_fsfreeze_mutex);
@@ -663,7 +660,7 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
663 else if (bdev->bd_contains == bdev) 660 else if (bdev->bd_contains == bdev)
664 return true; /* is a whole device which isn't held */ 661 return true; /* is a whole device which isn't held */
665 662
666 else if (whole->bd_holder == bd_claim) 663 else if (whole->bd_holder == bd_may_claim)
667 return true; /* is a partition of a device that is being partitioned */ 664 return true; /* is a partition of a device that is being partitioned */
668 else if (whole->bd_holder != NULL) 665 else if (whole->bd_holder != NULL)
669 return false; /* is a partition of a held device */ 666 return false; /* is a partition of a held device */
@@ -775,439 +772,87 @@ static struct block_device *bd_start_claiming(struct block_device *bdev,
775 } 772 }
776} 773}
777 774
778/* releases bdev_lock */
779static void __bd_abort_claiming(struct block_device *whole, void *holder)
780{
781 BUG_ON(whole->bd_claiming != holder);
782 whole->bd_claiming = NULL;
783 wake_up_bit(&whole->bd_claiming, 0);
784
785 spin_unlock(&bdev_lock);
786 bdput(whole);
787}
788
789/**
790 * bd_abort_claiming - abort claiming a block device
791 * @whole: whole block device returned by bd_start_claiming()
792 * @holder: holder trying to claim @bdev
793 *
794 * Abort a claiming block started by bd_start_claiming(). Note that
795 * @whole is not the block device to be claimed but the whole device
796 * returned by bd_start_claiming().
797 *
798 * CONTEXT:
799 * Grabs and releases bdev_lock.
800 */
801static void bd_abort_claiming(struct block_device *whole, void *holder)
802{
803 spin_lock(&bdev_lock);
804 __bd_abort_claiming(whole, holder); /* releases bdev_lock */
805}
806
807/* increment holders when we have a legitimate claim. requires bdev_lock */
808static void __bd_claim(struct block_device *bdev, struct block_device *whole,
809 void *holder)
810{
811 /* note that for a whole device bd_holders
812 * will be incremented twice, and bd_holder will
813 * be set to bd_claim before being set to holder
814 */
815 whole->bd_holders++;
816 whole->bd_holder = bd_claim;
817 bdev->bd_holders++;
818 bdev->bd_holder = holder;
819}
820
821/**
822 * bd_finish_claiming - finish claiming a block device
823 * @bdev: block device of interest (passed to bd_start_claiming())
824 * @whole: whole block device returned by bd_start_claiming()
825 * @holder: holder trying to claim @bdev
826 *
827 * Finish a claiming block started by bd_start_claiming().
828 *
829 * CONTEXT:
830 * Grabs and releases bdev_lock.
831 */
832static void bd_finish_claiming(struct block_device *bdev,
833 struct block_device *whole, void *holder)
834{
835 spin_lock(&bdev_lock);
836 BUG_ON(!bd_may_claim(bdev, whole, holder));
837 __bd_claim(bdev, whole, holder);
838 __bd_abort_claiming(whole, holder); /* not actually an abort */
839}
840
841/**
842 * bd_claim - claim a block device
843 * @bdev: block device to claim
844 * @holder: holder trying to claim @bdev
845 *
846 * Try to claim @bdev which must have been opened successfully.
847 *
848 * CONTEXT:
849 * Might sleep.
850 *
851 * RETURNS:
852 * 0 if successful, -EBUSY if @bdev is already claimed.
853 */
854int bd_claim(struct block_device *bdev, void *holder)
855{
856 struct block_device *whole = bdev->bd_contains;
857 int res;
858
859 might_sleep();
860
861 spin_lock(&bdev_lock);
862 res = bd_prepare_to_claim(bdev, whole, holder);
863 if (res == 0)
864 __bd_claim(bdev, whole, holder);
865 spin_unlock(&bdev_lock);
866
867 return res;
868}
869EXPORT_SYMBOL(bd_claim);
870
871void bd_release(struct block_device *bdev)
872{
873 spin_lock(&bdev_lock);
874 if (!--bdev->bd_contains->bd_holders)
875 bdev->bd_contains->bd_holder = NULL;
876 if (!--bdev->bd_holders)
877 bdev->bd_holder = NULL;
878 spin_unlock(&bdev_lock);
879}
880
881EXPORT_SYMBOL(bd_release);
882
883#ifdef CONFIG_SYSFS 775#ifdef CONFIG_SYSFS
884/*
885 * Functions for bd_claim_by_kobject / bd_release_from_kobject
886 *
887 * If a kobject is passed to bd_claim_by_kobject()
888 * and the kobject has a parent directory,
889 * following symlinks are created:
890 * o from the kobject to the claimed bdev
891 * o from "holders" directory of the bdev to the parent of the kobject
892 * bd_release_from_kobject() removes these symlinks.
893 *
894 * Example:
895 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
896 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
897 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
898 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
899 */
900
901static int add_symlink(struct kobject *from, struct kobject *to) 776static int add_symlink(struct kobject *from, struct kobject *to)
902{ 777{
903 if (!from || !to)
904 return 0;
905 return sysfs_create_link(from, to, kobject_name(to)); 778 return sysfs_create_link(from, to, kobject_name(to));
906} 779}
907 780
908static void del_symlink(struct kobject *from, struct kobject *to) 781static void del_symlink(struct kobject *from, struct kobject *to)
909{ 782{
910 if (!from || !to)
911 return;
912 sysfs_remove_link(from, kobject_name(to)); 783 sysfs_remove_link(from, kobject_name(to));
913} 784}
914 785
915/*
916 * 'struct bd_holder' contains pointers to kobjects symlinked by
917 * bd_claim_by_kobject.
918 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
919 */
920struct bd_holder {
921 struct list_head list; /* chain of holders of the bdev */
922 int count; /* references from the holder */
923 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
924 struct kobject *hdev; /* e.g. "/block/dm-0" */
925 struct kobject *hdir; /* e.g. "/block/sda/holders" */
926 struct kobject *sdev; /* e.g. "/block/sda" */
927};
928
929/*
930 * Get references of related kobjects at once.
931 * Returns 1 on success. 0 on failure.
932 *
933 * Should call bd_holder_release_dirs() after successful use.
934 */
935static int bd_holder_grab_dirs(struct block_device *bdev,
936 struct bd_holder *bo)
937{
938 if (!bdev || !bo)
939 return 0;
940
941 bo->sdir = kobject_get(bo->sdir);
942 if (!bo->sdir)
943 return 0;
944
945 bo->hdev = kobject_get(bo->sdir->parent);
946 if (!bo->hdev)
947 goto fail_put_sdir;
948
949 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
950 if (!bo->sdev)
951 goto fail_put_hdev;
952
953 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
954 if (!bo->hdir)
955 goto fail_put_sdev;
956
957 return 1;
958
959fail_put_sdev:
960 kobject_put(bo->sdev);
961fail_put_hdev:
962 kobject_put(bo->hdev);
963fail_put_sdir:
964 kobject_put(bo->sdir);
965
966 return 0;
967}
968
969/* Put references of related kobjects at once. */
970static void bd_holder_release_dirs(struct bd_holder *bo)
971{
972 kobject_put(bo->hdir);
973 kobject_put(bo->sdev);
974 kobject_put(bo->hdev);
975 kobject_put(bo->sdir);
976}
977
978static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
979{
980 struct bd_holder *bo;
981
982 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
983 if (!bo)
984 return NULL;
985
986 bo->count = 1;
987 bo->sdir = kobj;
988
989 return bo;
990}
991
992static void free_bd_holder(struct bd_holder *bo)
993{
994 kfree(bo);
995}
996
997/**
998 * find_bd_holder - find matching struct bd_holder from the block device
999 *
1000 * @bdev: struct block device to be searched
1001 * @bo: target struct bd_holder
1002 *
1003 * Returns matching entry with @bo in @bdev->bd_holder_list.
1004 * If found, increment the reference count and return the pointer.
1005 * If not found, returns NULL.
1006 */
1007static struct bd_holder *find_bd_holder(struct block_device *bdev,
1008 struct bd_holder *bo)
1009{
1010 struct bd_holder *tmp;
1011
1012 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
1013 if (tmp->sdir == bo->sdir) {
1014 tmp->count++;
1015 return tmp;
1016 }
1017
1018 return NULL;
1019}
1020
1021/** 786/**
1022 * add_bd_holder - create sysfs symlinks for bd_claim() relationship 787 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
1023 * 788 * @bdev: the claimed slave bdev
1024 * @bdev: block device to be bd_claimed 789 * @disk: the holding disk
1025 * @bo: preallocated and initialized by alloc_bd_holder()
1026 * 790 *
1027 * Add @bo to @bdev->bd_holder_list, create symlinks. 791 * This functions creates the following sysfs symlinks.
1028 *
1029 * Returns 0 if symlinks are created.
1030 * Returns -ve if something fails.
1031 */
1032static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
1033{
1034 int err;
1035
1036 if (!bo)
1037 return -EINVAL;
1038
1039 if (!bd_holder_grab_dirs(bdev, bo))
1040 return -EBUSY;
1041
1042 err = add_symlink(bo->sdir, bo->sdev);
1043 if (err)
1044 return err;
1045
1046 err = add_symlink(bo->hdir, bo->hdev);
1047 if (err) {
1048 del_symlink(bo->sdir, bo->sdev);
1049 return err;
1050 }
1051
1052 list_add_tail(&bo->list, &bdev->bd_holder_list);
1053 return 0;
1054}
1055
1056/**
1057 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
1058 * 792 *
1059 * @bdev: block device to be bd_claimed 793 * - from "slaves" directory of the holder @disk to the claimed @bdev
1060 * @kobj: holder's kobject 794 * - from "holders" directory of the @bdev to the holder @disk
1061 * 795 *
1062 * If there is matching entry with @kobj in @bdev->bd_holder_list 796 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1063 * and no other bd_claim() from the same kobject, 797 * passed to bd_link_disk_holder(), then:
1064 * remove the struct bd_holder from the list, delete symlinks for it.
1065 * 798 *
1066 * Returns a pointer to the struct bd_holder when it's removed from the list 799 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1067 * and ready to be freed. 800 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1068 * Returns NULL if matching claim isn't found or there is other bd_claim()
1069 * by the same kobject.
1070 */
1071static struct bd_holder *del_bd_holder(struct block_device *bdev,
1072 struct kobject *kobj)
1073{
1074 struct bd_holder *bo;
1075
1076 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
1077 if (bo->sdir == kobj) {
1078 bo->count--;
1079 BUG_ON(bo->count < 0);
1080 if (!bo->count) {
1081 list_del(&bo->list);
1082 del_symlink(bo->sdir, bo->sdev);
1083 del_symlink(bo->hdir, bo->hdev);
1084 bd_holder_release_dirs(bo);
1085 return bo;
1086 }
1087 break;
1088 }
1089 }
1090
1091 return NULL;
1092}
1093
1094/**
1095 * bd_claim_by_kobject - bd_claim() with additional kobject signature
1096 * 801 *
1097 * @bdev: block device to be claimed 802 * The caller must have claimed @bdev before calling this function and
1098 * @holder: holder's signature 803 * ensure that both @bdev and @disk are valid during the creation and
1099 * @kobj: holder's kobject 804 * lifetime of these symlinks.
1100 * 805 *
1101 * Do bd_claim() and if it succeeds, create sysfs symlinks between 806 * CONTEXT:
1102 * the bdev and the holder's kobject. 807 * Might sleep.
1103 * Use bd_release_from_kobject() when relesing the claimed bdev.
1104 * 808 *
1105 * Returns 0 on success. (same as bd_claim()) 809 * RETURNS:
1106 * Returns errno on failure. 810 * 0 on success, -errno on failure.
1107 */ 811 */
1108static int bd_claim_by_kobject(struct block_device *bdev, void *holder, 812int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
1109 struct kobject *kobj)
1110{ 813{
1111 int err; 814 int ret = 0;
1112 struct bd_holder *bo, *found;
1113
1114 if (!kobj)
1115 return -EINVAL;
1116
1117 bo = alloc_bd_holder(kobj);
1118 if (!bo)
1119 return -ENOMEM;
1120 815
1121 mutex_lock(&bdev->bd_mutex); 816 mutex_lock(&bdev->bd_mutex);
1122 817
1123 err = bd_claim(bdev, holder); 818 WARN_ON_ONCE(!bdev->bd_holder || bdev->bd_holder_disk);
1124 if (err)
1125 goto fail;
1126 819
1127 found = find_bd_holder(bdev, bo); 820 /* FIXME: remove the following once add_disk() handles errors */
1128 if (found) 821 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1129 goto fail; 822 goto out_unlock;
1130 823
1131 err = add_bd_holder(bdev, bo); 824 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1132 if (err) 825 if (ret)
1133 bd_release(bdev); 826 goto out_unlock;
1134 else
1135 bo = NULL;
1136fail:
1137 mutex_unlock(&bdev->bd_mutex);
1138 free_bd_holder(bo);
1139 return err;
1140}
1141 827
1142/** 828 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1143 * bd_release_from_kobject - bd_release() with additional kobject signature 829 if (ret) {
1144 * 830 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1145 * @bdev: block device to be released 831 goto out_unlock;
1146 * @kobj: holder's kobject 832 }
1147 *
1148 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1149 */
1150static void bd_release_from_kobject(struct block_device *bdev,
1151 struct kobject *kobj)
1152{
1153 if (!kobj)
1154 return;
1155 833
1156 mutex_lock(&bdev->bd_mutex); 834 bdev->bd_holder_disk = disk;
1157 bd_release(bdev); 835out_unlock:
1158 free_bd_holder(del_bd_holder(bdev, kobj));
1159 mutex_unlock(&bdev->bd_mutex); 836 mutex_unlock(&bdev->bd_mutex);
837 return ret;
1160} 838}
839EXPORT_SYMBOL_GPL(bd_link_disk_holder);
1161 840
1162/** 841static void bd_unlink_disk_holder(struct block_device *bdev)
1163 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1164 *
1165 * @bdev: block device to be claimed
1166 * @holder: holder's signature
1167 * @disk: holder's gendisk
1168 *
1169 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1170 */
1171int bd_claim_by_disk(struct block_device *bdev, void *holder,
1172 struct gendisk *disk)
1173{ 842{
1174 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); 843 struct gendisk *disk = bdev->bd_holder_disk;
1175}
1176EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1177 844
1178/** 845 bdev->bd_holder_disk = NULL;
1179 * bd_release_from_disk - wrapper function for bd_release_from_kobject() 846 if (!disk)
1180 * 847 return;
1181 * @bdev: block device to be claimed
1182 * @disk: holder's gendisk
1183 *
1184 * Call bd_release_from_kobject() and put @disk->slave_dir.
1185 */
1186void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1187{
1188 bd_release_from_kobject(bdev, disk->slave_dir);
1189 kobject_put(disk->slave_dir);
1190}
1191EXPORT_SYMBOL_GPL(bd_release_from_disk);
1192#endif
1193 848
1194/* 849 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1195 * Tries to open block device by device number. Use it ONLY if you 850 del_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1196 * really do not have anything better - i.e. when you are behind a
1197 * truly sucky interface and all you are given is a device number. _Never_
1198 * to be used for internal purposes. If you ever need it - reconsider
1199 * your API.
1200 */
1201struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1202{
1203 struct block_device *bdev = bdget(dev);
1204 int err = -ENOMEM;
1205 if (bdev)
1206 err = blkdev_get(bdev, mode);
1207 return err ? ERR_PTR(err) : bdev;
1208} 851}
1209 852#else
1210EXPORT_SYMBOL(open_by_devnum); 853static inline void bd_unlink_disk_holder(struct block_device *bdev)
854{ }
855#endif
1211 856
1212/** 857/**
1213 * flush_disk - invalidates all buffer-cache entries on a disk 858 * flush_disk - invalidates all buffer-cache entries on a disk
@@ -1469,17 +1114,156 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1469 return ret; 1114 return ret;
1470} 1115}
1471 1116
1472int blkdev_get(struct block_device *bdev, fmode_t mode) 1117/**
1118 * blkdev_get - open a block device
1119 * @bdev: block_device to open
1120 * @mode: FMODE_* mask
1121 * @holder: exclusive holder identifier
1122 *
1123 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1124 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1125 * @holder is invalid. Exclusive opens may nest for the same @holder.
1126 *
1127 * On success, the reference count of @bdev is unchanged. On failure,
1128 * @bdev is put.
1129 *
1130 * CONTEXT:
1131 * Might sleep.
1132 *
1133 * RETURNS:
1134 * 0 on success, -errno on failure.
1135 */
1136int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1473{ 1137{
1474 return __blkdev_get(bdev, mode, 0); 1138 struct block_device *whole = NULL;
1139 int res;
1140
1141 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1142
1143 if ((mode & FMODE_EXCL) && holder) {
1144 whole = bd_start_claiming(bdev, holder);
1145 if (IS_ERR(whole)) {
1146 bdput(bdev);
1147 return PTR_ERR(whole);
1148 }
1149 }
1150
1151 res = __blkdev_get(bdev, mode, 0);
1152
1153 /* __blkdev_get() may alter read only status, check it afterwards */
1154 if (!res && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1155 __blkdev_put(bdev, mode, 0);
1156 res = -EACCES;
1157 }
1158
1159 if (whole) {
1160 /* finish claiming */
1161 spin_lock(&bdev_lock);
1162
1163 if (res == 0) {
1164 BUG_ON(!bd_may_claim(bdev, whole, holder));
1165 /*
1166 * Note that for a whole device bd_holders
1167 * will be incremented twice, and bd_holder
1168 * will be set to bd_may_claim before being
1169 * set to holder
1170 */
1171 whole->bd_holders++;
1172 whole->bd_holder = bd_may_claim;
1173 bdev->bd_holders++;
1174 bdev->bd_holder = holder;
1175 }
1176
1177 /* tell others that we're done */
1178 BUG_ON(whole->bd_claiming != holder);
1179 whole->bd_claiming = NULL;
1180 wake_up_bit(&whole->bd_claiming, 0);
1181
1182 spin_unlock(&bdev_lock);
1183 bdput(whole);
1184 }
1185
1186 return res;
1475} 1187}
1476EXPORT_SYMBOL(blkdev_get); 1188EXPORT_SYMBOL(blkdev_get);
1477 1189
1190/**
1191 * blkdev_get_by_path - open a block device by name
1192 * @path: path to the block device to open
1193 * @mode: FMODE_* mask
1194 * @holder: exclusive holder identifier
1195 *
1196 * Open the blockdevice described by the device file at @path. @mode
1197 * and @holder are identical to blkdev_get().
1198 *
1199 * On success, the returned block_device has reference count of one.
1200 *
1201 * CONTEXT:
1202 * Might sleep.
1203 *
1204 * RETURNS:
1205 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1206 */
1207struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1208 void *holder)
1209{
1210 struct block_device *bdev;
1211 int err;
1212
1213 bdev = lookup_bdev(path);
1214 if (IS_ERR(bdev))
1215 return bdev;
1216
1217 err = blkdev_get(bdev, mode, holder);
1218 if (err)
1219 return ERR_PTR(err);
1220
1221 return bdev;
1222}
1223EXPORT_SYMBOL(blkdev_get_by_path);
1224
1225/**
1226 * blkdev_get_by_dev - open a block device by device number
1227 * @dev: device number of block device to open
1228 * @mode: FMODE_* mask
1229 * @holder: exclusive holder identifier
1230 *
1231 * Open the blockdevice described by device number @dev. @mode and
1232 * @holder are identical to blkdev_get().
1233 *
1234 * Use it ONLY if you really do not have anything better - i.e. when
1235 * you are behind a truly sucky interface and all you are given is a
1236 * device number. _Never_ to be used for internal purposes. If you
1237 * ever need it - reconsider your API.
1238 *
1239 * On success, the returned block_device has reference count of one.
1240 *
1241 * CONTEXT:
1242 * Might sleep.
1243 *
1244 * RETURNS:
1245 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1246 */
1247struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1248{
1249 struct block_device *bdev;
1250 int err;
1251
1252 bdev = bdget(dev);
1253 if (!bdev)
1254 return ERR_PTR(-ENOMEM);
1255
1256 err = blkdev_get(bdev, mode, holder);
1257 if (err)
1258 return ERR_PTR(err);
1259
1260 return bdev;
1261}
1262EXPORT_SYMBOL(blkdev_get_by_dev);
1263
1478static int blkdev_open(struct inode * inode, struct file * filp) 1264static int blkdev_open(struct inode * inode, struct file * filp)
1479{ 1265{
1480 struct block_device *whole = NULL;
1481 struct block_device *bdev; 1266 struct block_device *bdev;
1482 int res;
1483 1267
1484 /* 1268 /*
1485 * Preserve backwards compatibility and allow large file access 1269 * Preserve backwards compatibility and allow large file access
@@ -1500,26 +1284,9 @@ static int blkdev_open(struct inode * inode, struct file * filp)
1500 if (bdev == NULL) 1284 if (bdev == NULL)
1501 return -ENOMEM; 1285 return -ENOMEM;
1502 1286
1503 if (filp->f_mode & FMODE_EXCL) {
1504 whole = bd_start_claiming(bdev, filp);
1505 if (IS_ERR(whole)) {
1506 bdput(bdev);
1507 return PTR_ERR(whole);
1508 }
1509 }
1510
1511 filp->f_mapping = bdev->bd_inode->i_mapping; 1287 filp->f_mapping = bdev->bd_inode->i_mapping;
1512 1288
1513 res = blkdev_get(bdev, filp->f_mode); 1289 return blkdev_get(bdev, filp->f_mode, filp);
1514
1515 if (whole) {
1516 if (res == 0)
1517 bd_finish_claiming(bdev, whole, filp);
1518 else
1519 bd_abort_claiming(whole, filp);
1520 }
1521
1522 return res;
1523} 1290}
1524 1291
1525static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) 1292static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
@@ -1533,6 +1300,7 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1533 bdev->bd_part_count--; 1300 bdev->bd_part_count--;
1534 1301
1535 if (!--bdev->bd_openers) { 1302 if (!--bdev->bd_openers) {
1303 WARN_ON_ONCE(bdev->bd_holders);
1536 sync_blockdev(bdev); 1304 sync_blockdev(bdev);
1537 kill_bdev(bdev); 1305 kill_bdev(bdev);
1538 } 1306 }
@@ -1563,6 +1331,34 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1563 1331
1564int blkdev_put(struct block_device *bdev, fmode_t mode) 1332int blkdev_put(struct block_device *bdev, fmode_t mode)
1565{ 1333{
1334 if (mode & FMODE_EXCL) {
1335 bool bdev_free;
1336
1337 /*
1338 * Release a claim on the device. The holder fields
1339 * are protected with bdev_lock. bd_mutex is to
1340 * synchronize disk_holder unlinking.
1341 */
1342 mutex_lock(&bdev->bd_mutex);
1343 spin_lock(&bdev_lock);
1344
1345 WARN_ON_ONCE(--bdev->bd_holders < 0);
1346 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1347
1348 /* bd_contains might point to self, check in a separate step */
1349 if ((bdev_free = !bdev->bd_holders))
1350 bdev->bd_holder = NULL;
1351 if (!bdev->bd_contains->bd_holders)
1352 bdev->bd_contains->bd_holder = NULL;
1353
1354 spin_unlock(&bdev_lock);
1355
1356 /* if this was the last claim, holder link should go too */
1357 if (bdev_free)
1358 bd_unlink_disk_holder(bdev);
1359
1360 mutex_unlock(&bdev->bd_mutex);
1361 }
1566 return __blkdev_put(bdev, mode, 0); 1362 return __blkdev_put(bdev, mode, 0);
1567} 1363}
1568EXPORT_SYMBOL(blkdev_put); 1364EXPORT_SYMBOL(blkdev_put);
@@ -1570,8 +1366,7 @@ EXPORT_SYMBOL(blkdev_put);
1570static int blkdev_close(struct inode * inode, struct file * filp) 1366static int blkdev_close(struct inode * inode, struct file * filp)
1571{ 1367{
1572 struct block_device *bdev = I_BDEV(filp->f_mapping->host); 1368 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1573 if (bdev->bd_holder == filp) 1369
1574 bd_release(bdev);
1575 return blkdev_put(bdev, filp->f_mode); 1370 return blkdev_put(bdev, filp->f_mode);
1576} 1371}
1577 1372
@@ -1716,67 +1511,6 @@ fail:
1716} 1511}
1717EXPORT_SYMBOL(lookup_bdev); 1512EXPORT_SYMBOL(lookup_bdev);
1718 1513
1719/**
1720 * open_bdev_exclusive - open a block device by name and set it up for use
1721 *
1722 * @path: special file representing the block device
1723 * @mode: FMODE_... combination to pass be used
1724 * @holder: owner for exclusion
1725 *
1726 * Open the blockdevice described by the special file at @path, claim it
1727 * for the @holder.
1728 */
1729struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1730{
1731 struct block_device *bdev, *whole;
1732 int error;
1733
1734 bdev = lookup_bdev(path);
1735 if (IS_ERR(bdev))
1736 return bdev;
1737
1738 whole = bd_start_claiming(bdev, holder);
1739 if (IS_ERR(whole)) {
1740 bdput(bdev);
1741 return whole;
1742 }
1743
1744 error = blkdev_get(bdev, mode);
1745 if (error)
1746 goto out_abort_claiming;
1747
1748 error = -EACCES;
1749 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1750 goto out_blkdev_put;
1751
1752 bd_finish_claiming(bdev, whole, holder);
1753 return bdev;
1754
1755out_blkdev_put:
1756 blkdev_put(bdev, mode);
1757out_abort_claiming:
1758 bd_abort_claiming(whole, holder);
1759 return ERR_PTR(error);
1760}
1761
1762EXPORT_SYMBOL(open_bdev_exclusive);
1763
1764/**
1765 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1766 *
1767 * @bdev: blockdevice to close
1768 * @mode: mode, must match that used to open.
1769 *
1770 * This is the counterpart to open_bdev_exclusive().
1771 */
1772void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1773{
1774 bd_release(bdev);
1775 blkdev_put(bdev, mode);
1776}
1777
1778EXPORT_SYMBOL(close_bdev_exclusive);
1779
1780int __invalidate_device(struct block_device *bdev) 1514int __invalidate_device(struct block_device *bdev)
1781{ 1515{
1782 struct super_block *sb = get_super(bdev); 1516 struct super_block *sb = get_super(bdev);
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index cc04dc1445d6..95324e9f9280 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -489,7 +489,7 @@ again:
489 continue; 489 continue;
490 490
491 if (device->bdev) { 491 if (device->bdev) {
492 close_bdev_exclusive(device->bdev, device->mode); 492 blkdev_put(device->bdev, device->mode);
493 device->bdev = NULL; 493 device->bdev = NULL;
494 fs_devices->open_devices--; 494 fs_devices->open_devices--;
495 } 495 }
@@ -523,7 +523,7 @@ static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
523 523
524 list_for_each_entry(device, &fs_devices->devices, dev_list) { 524 list_for_each_entry(device, &fs_devices->devices, dev_list) {
525 if (device->bdev) { 525 if (device->bdev) {
526 close_bdev_exclusive(device->bdev, device->mode); 526 blkdev_put(device->bdev, device->mode);
527 fs_devices->open_devices--; 527 fs_devices->open_devices--;
528 } 528 }
529 if (device->writeable) { 529 if (device->writeable) {
@@ -580,13 +580,15 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
580 int seeding = 1; 580 int seeding = 1;
581 int ret = 0; 581 int ret = 0;
582 582
583 flags |= FMODE_EXCL;
584
583 list_for_each_entry(device, head, dev_list) { 585 list_for_each_entry(device, head, dev_list) {
584 if (device->bdev) 586 if (device->bdev)
585 continue; 587 continue;
586 if (!device->name) 588 if (!device->name)
587 continue; 589 continue;
588 590
589 bdev = open_bdev_exclusive(device->name, flags, holder); 591 bdev = blkdev_get_by_path(device->name, flags, holder);
590 if (IS_ERR(bdev)) { 592 if (IS_ERR(bdev)) {
591 printk(KERN_INFO "open %s failed\n", device->name); 593 printk(KERN_INFO "open %s failed\n", device->name);
592 goto error; 594 goto error;
@@ -638,7 +640,7 @@ static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
638error_brelse: 640error_brelse:
639 brelse(bh); 641 brelse(bh);
640error_close: 642error_close:
641 close_bdev_exclusive(bdev, FMODE_READ); 643 blkdev_put(bdev, flags);
642error: 644error:
643 continue; 645 continue;
644 } 646 }
@@ -684,7 +686,8 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
684 686
685 mutex_lock(&uuid_mutex); 687 mutex_lock(&uuid_mutex);
686 688
687 bdev = open_bdev_exclusive(path, flags, holder); 689 flags |= FMODE_EXCL;
690 bdev = blkdev_get_by_path(path, flags, holder);
688 691
689 if (IS_ERR(bdev)) { 692 if (IS_ERR(bdev)) {
690 ret = PTR_ERR(bdev); 693 ret = PTR_ERR(bdev);
@@ -716,7 +719,7 @@ int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
716 719
717 brelse(bh); 720 brelse(bh);
718error_close: 721error_close:
719 close_bdev_exclusive(bdev, flags); 722 blkdev_put(bdev, flags);
720error: 723error:
721 mutex_unlock(&uuid_mutex); 724 mutex_unlock(&uuid_mutex);
722 return ret; 725 return ret;
@@ -1179,8 +1182,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1179 goto out; 1182 goto out;
1180 } 1183 }
1181 } else { 1184 } else {
1182 bdev = open_bdev_exclusive(device_path, FMODE_READ, 1185 bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
1183 root->fs_info->bdev_holder); 1186 root->fs_info->bdev_holder);
1184 if (IS_ERR(bdev)) { 1187 if (IS_ERR(bdev)) {
1185 ret = PTR_ERR(bdev); 1188 ret = PTR_ERR(bdev);
1186 goto out; 1189 goto out;
@@ -1244,7 +1247,7 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
1244 root->fs_info->fs_devices->latest_bdev = next_device->bdev; 1247 root->fs_info->fs_devices->latest_bdev = next_device->bdev;
1245 1248
1246 if (device->bdev) { 1249 if (device->bdev) {
1247 close_bdev_exclusive(device->bdev, device->mode); 1250 blkdev_put(device->bdev, device->mode);
1248 device->bdev = NULL; 1251 device->bdev = NULL;
1249 device->fs_devices->open_devices--; 1252 device->fs_devices->open_devices--;
1250 } 1253 }
@@ -1287,7 +1290,7 @@ error_brelse:
1287 brelse(bh); 1290 brelse(bh);
1288error_close: 1291error_close:
1289 if (bdev) 1292 if (bdev)
1290 close_bdev_exclusive(bdev, FMODE_READ); 1293 blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
1291out: 1294out:
1292 mutex_unlock(&root->fs_info->volume_mutex); 1295 mutex_unlock(&root->fs_info->volume_mutex);
1293 mutex_unlock(&uuid_mutex); 1296 mutex_unlock(&uuid_mutex);
@@ -1439,7 +1442,8 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1439 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding) 1442 if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
1440 return -EINVAL; 1443 return -EINVAL;
1441 1444
1442 bdev = open_bdev_exclusive(device_path, 0, root->fs_info->bdev_holder); 1445 bdev = blkdev_get_by_path(device_path, FMODE_EXCL,
1446 root->fs_info->bdev_holder);
1443 if (IS_ERR(bdev)) 1447 if (IS_ERR(bdev))
1444 return PTR_ERR(bdev); 1448 return PTR_ERR(bdev);
1445 1449
@@ -1565,7 +1569,7 @@ out:
1565 mutex_unlock(&root->fs_info->volume_mutex); 1569 mutex_unlock(&root->fs_info->volume_mutex);
1566 return ret; 1570 return ret;
1567error: 1571error:
1568 close_bdev_exclusive(bdev, 0); 1572 blkdev_put(bdev, FMODE_EXCL);
1569 if (seeding_dev) { 1573 if (seeding_dev) {
1570 mutex_unlock(&uuid_mutex); 1574 mutex_unlock(&uuid_mutex);
1571 up_write(&sb->s_umount); 1575 up_write(&sb->s_umount);
diff --git a/fs/btrfs/volumes.h b/fs/btrfs/volumes.h
index 2b638b6e4eea..856e75770304 100644
--- a/fs/btrfs/volumes.h
+++ b/fs/btrfs/volumes.h
@@ -49,7 +49,7 @@ struct btrfs_device {
49 49
50 struct block_device *bdev; 50 struct block_device *bdev;
51 51
52 /* the mode sent to open_bdev_exclusive */ 52 /* the mode sent to blkdev_get */
53 fmode_t mode; 53 fmode_t mode;
54 54
55 char *name; 55 char *name;
diff --git a/fs/ext3/super.c b/fs/ext3/super.c
index 2fedaf8b5012..123720ba786d 100644
--- a/fs/ext3/super.c
+++ b/fs/ext3/super.c
@@ -347,7 +347,7 @@ static struct block_device *ext3_blkdev_get(dev_t dev, struct super_block *sb)
347 struct block_device *bdev; 347 struct block_device *bdev;
348 char b[BDEVNAME_SIZE]; 348 char b[BDEVNAME_SIZE];
349 349
350 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); 350 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
351 if (IS_ERR(bdev)) 351 if (IS_ERR(bdev))
352 goto fail; 352 goto fail;
353 return bdev; 353 return bdev;
@@ -364,8 +364,7 @@ fail:
364 */ 364 */
365static int ext3_blkdev_put(struct block_device *bdev) 365static int ext3_blkdev_put(struct block_device *bdev)
366{ 366{
367 bd_release(bdev); 367 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
368 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
369} 368}
370 369
371static int ext3_blkdev_remove(struct ext3_sb_info *sbi) 370static int ext3_blkdev_remove(struct ext3_sb_info *sbi)
@@ -2136,13 +2135,6 @@ static journal_t *ext3_get_dev_journal(struct super_block *sb,
2136 if (bdev == NULL) 2135 if (bdev == NULL)
2137 return NULL; 2136 return NULL;
2138 2137
2139 if (bd_claim(bdev, sb)) {
2140 ext3_msg(sb, KERN_ERR,
2141 "error: failed to claim external journal device");
2142 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
2143 return NULL;
2144 }
2145
2146 blocksize = sb->s_blocksize; 2138 blocksize = sb->s_blocksize;
2147 hblock = bdev_logical_block_size(bdev); 2139 hblock = bdev_logical_block_size(bdev);
2148 if (blocksize < hblock) { 2140 if (blocksize < hblock) {
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 61182fe6254e..bd63e6927219 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -647,7 +647,7 @@ static struct block_device *ext4_blkdev_get(dev_t dev, struct super_block *sb)
647 struct block_device *bdev; 647 struct block_device *bdev;
648 char b[BDEVNAME_SIZE]; 648 char b[BDEVNAME_SIZE];
649 649
650 bdev = open_by_devnum(dev, FMODE_READ|FMODE_WRITE); 650 bdev = blkdev_get_by_dev(dev, FMODE_READ|FMODE_WRITE|FMODE_EXCL, sb);
651 if (IS_ERR(bdev)) 651 if (IS_ERR(bdev))
652 goto fail; 652 goto fail;
653 return bdev; 653 return bdev;
@@ -663,8 +663,7 @@ fail:
663 */ 663 */
664static int ext4_blkdev_put(struct block_device *bdev) 664static int ext4_blkdev_put(struct block_device *bdev)
665{ 665{
666 bd_release(bdev); 666 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
667 return blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
668} 667}
669 668
670static int ext4_blkdev_remove(struct ext4_sb_info *sbi) 669static int ext4_blkdev_remove(struct ext4_sb_info *sbi)
@@ -3758,13 +3757,6 @@ static journal_t *ext4_get_dev_journal(struct super_block *sb,
3758 if (bdev == NULL) 3757 if (bdev == NULL)
3759 return NULL; 3758 return NULL;
3760 3759
3761 if (bd_claim(bdev, sb)) {
3762 ext4_msg(sb, KERN_ERR,
3763 "failed to claim external journal device");
3764 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
3765 return NULL;
3766 }
3767
3768 blocksize = sb->s_blocksize; 3760 blocksize = sb->s_blocksize;
3769 hblock = bdev_logical_block_size(bdev); 3761 hblock = bdev_logical_block_size(bdev);
3770 if (blocksize < hblock) { 3762 if (blocksize < hblock) {
diff --git a/fs/gfs2/ops_fstype.c b/fs/gfs2/ops_fstype.c
index 3eb1393f7b81..bc56ccf98ffd 100644
--- a/fs/gfs2/ops_fstype.c
+++ b/fs/gfs2/ops_fstype.c
@@ -1268,7 +1268,7 @@ static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
1268{ 1268{
1269 struct block_device *bdev; 1269 struct block_device *bdev;
1270 struct super_block *s; 1270 struct super_block *s;
1271 fmode_t mode = FMODE_READ; 1271 fmode_t mode = FMODE_READ | FMODE_EXCL;
1272 int error; 1272 int error;
1273 struct gfs2_args args; 1273 struct gfs2_args args;
1274 struct gfs2_sbd *sdp; 1274 struct gfs2_sbd *sdp;
@@ -1276,7 +1276,7 @@ static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
1276 if (!(flags & MS_RDONLY)) 1276 if (!(flags & MS_RDONLY))
1277 mode |= FMODE_WRITE; 1277 mode |= FMODE_WRITE;
1278 1278
1279 bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1279 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1280 if (IS_ERR(bdev)) 1280 if (IS_ERR(bdev))
1281 return ERR_CAST(bdev); 1281 return ERR_CAST(bdev);
1282 1282
@@ -1298,7 +1298,7 @@ static struct dentry *gfs2_mount(struct file_system_type *fs_type, int flags,
1298 goto error_bdev; 1298 goto error_bdev;
1299 1299
1300 if (s->s_root) 1300 if (s->s_root)
1301 close_bdev_exclusive(bdev, mode); 1301 blkdev_put(bdev, mode);
1302 1302
1303 memset(&args, 0, sizeof(args)); 1303 memset(&args, 0, sizeof(args));
1304 args.ar_quota = GFS2_QUOTA_DEFAULT; 1304 args.ar_quota = GFS2_QUOTA_DEFAULT;
@@ -1342,7 +1342,7 @@ error_super:
1342 deactivate_locked_super(s); 1342 deactivate_locked_super(s);
1343 return ERR_PTR(error); 1343 return ERR_PTR(error);
1344error_bdev: 1344error_bdev:
1345 close_bdev_exclusive(bdev, mode); 1345 blkdev_put(bdev, mode);
1346 return ERR_PTR(error); 1346 return ERR_PTR(error);
1347} 1347}
1348 1348
diff --git a/fs/jfs/jfs_logmgr.c b/fs/jfs/jfs_logmgr.c
index e1b8493b9aaa..278e3fb40b71 100644
--- a/fs/jfs/jfs_logmgr.c
+++ b/fs/jfs/jfs_logmgr.c
@@ -1120,16 +1120,13 @@ int lmLogOpen(struct super_block *sb)
1120 * file systems to log may have n-to-1 relationship; 1120 * file systems to log may have n-to-1 relationship;
1121 */ 1121 */
1122 1122
1123 bdev = open_by_devnum(sbi->logdev, FMODE_READ|FMODE_WRITE); 1123 bdev = blkdev_get_by_dev(sbi->logdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
1124 log);
1124 if (IS_ERR(bdev)) { 1125 if (IS_ERR(bdev)) {
1125 rc = -PTR_ERR(bdev); 1126 rc = -PTR_ERR(bdev);
1126 goto free; 1127 goto free;
1127 } 1128 }
1128 1129
1129 if ((rc = bd_claim(bdev, log))) {
1130 goto close;
1131 }
1132
1133 log->bdev = bdev; 1130 log->bdev = bdev;
1134 memcpy(log->uuid, sbi->loguuid, sizeof(log->uuid)); 1131 memcpy(log->uuid, sbi->loguuid, sizeof(log->uuid));
1135 1132
@@ -1137,7 +1134,7 @@ int lmLogOpen(struct super_block *sb)
1137 * initialize log: 1134 * initialize log:
1138 */ 1135 */
1139 if ((rc = lmLogInit(log))) 1136 if ((rc = lmLogInit(log)))
1140 goto unclaim; 1137 goto close;
1141 1138
1142 list_add(&log->journal_list, &jfs_external_logs); 1139 list_add(&log->journal_list, &jfs_external_logs);
1143 1140
@@ -1163,11 +1160,8 @@ journal_found:
1163 list_del(&log->journal_list); 1160 list_del(&log->journal_list);
1164 lbmLogShutdown(log); 1161 lbmLogShutdown(log);
1165 1162
1166 unclaim:
1167 bd_release(bdev);
1168
1169 close: /* close external log device */ 1163 close: /* close external log device */
1170 blkdev_put(bdev, FMODE_READ|FMODE_WRITE); 1164 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1171 1165
1172 free: /* free log descriptor */ 1166 free: /* free log descriptor */
1173 mutex_unlock(&jfs_log_mutex); 1167 mutex_unlock(&jfs_log_mutex);
@@ -1512,8 +1506,7 @@ int lmLogClose(struct super_block *sb)
1512 bdev = log->bdev; 1506 bdev = log->bdev;
1513 rc = lmLogShutdown(log); 1507 rc = lmLogShutdown(log);
1514 1508
1515 bd_release(bdev); 1509 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
1516 blkdev_put(bdev, FMODE_READ|FMODE_WRITE);
1517 1510
1518 kfree(log); 1511 kfree(log);
1519 1512
diff --git a/fs/logfs/dev_bdev.c b/fs/logfs/dev_bdev.c
index 92ca6fbe09bd..723bc5bca09a 100644
--- a/fs/logfs/dev_bdev.c
+++ b/fs/logfs/dev_bdev.c
@@ -300,7 +300,7 @@ static int bdev_write_sb(struct super_block *sb, struct page *page)
300 300
301static void bdev_put_device(struct logfs_super *s) 301static void bdev_put_device(struct logfs_super *s)
302{ 302{
303 close_bdev_exclusive(s->s_bdev, FMODE_READ|FMODE_WRITE); 303 blkdev_put(s->s_bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
304} 304}
305 305
306static int bdev_can_write_buf(struct super_block *sb, u64 ofs) 306static int bdev_can_write_buf(struct super_block *sb, u64 ofs)
@@ -325,13 +325,14 @@ int logfs_get_sb_bdev(struct logfs_super *p, struct file_system_type *type,
325{ 325{
326 struct block_device *bdev; 326 struct block_device *bdev;
327 327
328 bdev = open_bdev_exclusive(devname, FMODE_READ|FMODE_WRITE, type); 328 bdev = blkdev_get_by_path(devname, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
329 type);
329 if (IS_ERR(bdev)) 330 if (IS_ERR(bdev))
330 return PTR_ERR(bdev); 331 return PTR_ERR(bdev);
331 332
332 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) { 333 if (MAJOR(bdev->bd_dev) == MTD_BLOCK_MAJOR) {
333 int mtdnr = MINOR(bdev->bd_dev); 334 int mtdnr = MINOR(bdev->bd_dev);
334 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE); 335 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
335 return logfs_get_sb_mtd(p, mtdnr); 336 return logfs_get_sb_mtd(p, mtdnr);
336 } 337 }
337 338
diff --git a/fs/nilfs2/super.c b/fs/nilfs2/super.c
index f804d41ec9d3..0030640e2d72 100644
--- a/fs/nilfs2/super.c
+++ b/fs/nilfs2/super.c
@@ -1147,14 +1147,14 @@ nilfs_mount(struct file_system_type *fs_type, int flags,
1147{ 1147{
1148 struct nilfs_super_data sd; 1148 struct nilfs_super_data sd;
1149 struct super_block *s; 1149 struct super_block *s;
1150 fmode_t mode = FMODE_READ; 1150 fmode_t mode = FMODE_READ | FMODE_EXCL;
1151 struct dentry *root_dentry; 1151 struct dentry *root_dentry;
1152 int err, s_new = false; 1152 int err, s_new = false;
1153 1153
1154 if (!(flags & MS_RDONLY)) 1154 if (!(flags & MS_RDONLY))
1155 mode |= FMODE_WRITE; 1155 mode |= FMODE_WRITE;
1156 1156
1157 sd.bdev = open_bdev_exclusive(dev_name, mode, fs_type); 1157 sd.bdev = blkdev_get_by_path(dev_name, mode, fs_type);
1158 if (IS_ERR(sd.bdev)) 1158 if (IS_ERR(sd.bdev))
1159 return ERR_CAST(sd.bdev); 1159 return ERR_CAST(sd.bdev);
1160 1160
@@ -1233,7 +1233,7 @@ nilfs_mount(struct file_system_type *fs_type, int flags,
1233 } 1233 }
1234 1234
1235 if (!s_new) 1235 if (!s_new)
1236 close_bdev_exclusive(sd.bdev, mode); 1236 blkdev_put(sd.bdev, mode);
1237 1237
1238 return root_dentry; 1238 return root_dentry;
1239 1239
@@ -1242,7 +1242,7 @@ nilfs_mount(struct file_system_type *fs_type, int flags,
1242 1242
1243 failed: 1243 failed:
1244 if (!s_new) 1244 if (!s_new)
1245 close_bdev_exclusive(sd.bdev, mode); 1245 blkdev_put(sd.bdev, mode);
1246 return ERR_PTR(err); 1246 return ERR_PTR(err);
1247} 1247}
1248 1248
diff --git a/fs/ocfs2/cluster/heartbeat.c b/fs/ocfs2/cluster/heartbeat.c
index 52c7557f3e25..d0a2721eaceb 100644
--- a/fs/ocfs2/cluster/heartbeat.c
+++ b/fs/ocfs2/cluster/heartbeat.c
@@ -1674,7 +1674,7 @@ static ssize_t o2hb_region_dev_write(struct o2hb_region *reg,
1674 goto out; 1674 goto out;
1675 1675
1676 reg->hr_bdev = I_BDEV(filp->f_mapping->host); 1676 reg->hr_bdev = I_BDEV(filp->f_mapping->host);
1677 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ); 1677 ret = blkdev_get(reg->hr_bdev, FMODE_WRITE | FMODE_READ, NULL);
1678 if (ret) { 1678 if (ret) {
1679 reg->hr_bdev = NULL; 1679 reg->hr_bdev = NULL;
1680 goto out; 1680 goto out;
diff --git a/fs/partitions/check.c b/fs/partitions/check.c
index 12213f7ce7a2..bdf8d3cc95a4 100644
--- a/fs/partitions/check.c
+++ b/fs/partitions/check.c
@@ -558,7 +558,7 @@ void register_disk(struct gendisk *disk)
558 goto exit; 558 goto exit;
559 559
560 bdev->bd_invalidated = 1; 560 bdev->bd_invalidated = 1;
561 err = blkdev_get(bdev, FMODE_READ); 561 err = blkdev_get(bdev, FMODE_READ, NULL);
562 if (err < 0) 562 if (err < 0)
563 goto exit; 563 goto exit;
564 blkdev_put(bdev, FMODE_READ); 564 blkdev_put(bdev, FMODE_READ);
diff --git a/fs/reiserfs/journal.c b/fs/reiserfs/journal.c
index 076c8b194682..e2fce519c0f2 100644
--- a/fs/reiserfs/journal.c
+++ b/fs/reiserfs/journal.c
@@ -2552,8 +2552,6 @@ static int release_journal_dev(struct super_block *super,
2552 result = 0; 2552 result = 0;
2553 2553
2554 if (journal->j_dev_bd != NULL) { 2554 if (journal->j_dev_bd != NULL) {
2555 if (journal->j_dev_bd->bd_dev != super->s_dev)
2556 bd_release(journal->j_dev_bd);
2557 result = blkdev_put(journal->j_dev_bd, journal->j_dev_mode); 2555 result = blkdev_put(journal->j_dev_bd, journal->j_dev_mode);
2558 journal->j_dev_bd = NULL; 2556 journal->j_dev_bd = NULL;
2559 } 2557 }
@@ -2571,7 +2569,7 @@ static int journal_init_dev(struct super_block *super,
2571{ 2569{
2572 int result; 2570 int result;
2573 dev_t jdev; 2571 dev_t jdev;
2574 fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE; 2572 fmode_t blkdev_mode = FMODE_READ | FMODE_WRITE | FMODE_EXCL;
2575 char b[BDEVNAME_SIZE]; 2573 char b[BDEVNAME_SIZE];
2576 2574
2577 result = 0; 2575 result = 0;
@@ -2585,7 +2583,10 @@ static int journal_init_dev(struct super_block *super,
2585 2583
2586 /* there is no "jdev" option and journal is on separate device */ 2584 /* there is no "jdev" option and journal is on separate device */
2587 if ((!jdev_name || !jdev_name[0])) { 2585 if ((!jdev_name || !jdev_name[0])) {
2588 journal->j_dev_bd = open_by_devnum(jdev, blkdev_mode); 2586 if (jdev == super->s_dev)
2587 blkdev_mode &= ~FMODE_EXCL;
2588 journal->j_dev_bd = blkdev_get_by_dev(jdev, blkdev_mode,
2589 journal);
2589 journal->j_dev_mode = blkdev_mode; 2590 journal->j_dev_mode = blkdev_mode;
2590 if (IS_ERR(journal->j_dev_bd)) { 2591 if (IS_ERR(journal->j_dev_bd)) {
2591 result = PTR_ERR(journal->j_dev_bd); 2592 result = PTR_ERR(journal->j_dev_bd);
@@ -2594,22 +2595,14 @@ static int journal_init_dev(struct super_block *super,
2594 "cannot init journal device '%s': %i", 2595 "cannot init journal device '%s': %i",
2595 __bdevname(jdev, b), result); 2596 __bdevname(jdev, b), result);
2596 return result; 2597 return result;
2597 } else if (jdev != super->s_dev) { 2598 } else if (jdev != super->s_dev)
2598 result = bd_claim(journal->j_dev_bd, journal);
2599 if (result) {
2600 blkdev_put(journal->j_dev_bd, blkdev_mode);
2601 return result;
2602 }
2603
2604 set_blocksize(journal->j_dev_bd, super->s_blocksize); 2599 set_blocksize(journal->j_dev_bd, super->s_blocksize);
2605 }
2606 2600
2607 return 0; 2601 return 0;
2608 } 2602 }
2609 2603
2610 journal->j_dev_mode = blkdev_mode; 2604 journal->j_dev_mode = blkdev_mode;
2611 journal->j_dev_bd = open_bdev_exclusive(jdev_name, 2605 journal->j_dev_bd = blkdev_get_by_path(jdev_name, blkdev_mode, journal);
2612 blkdev_mode, journal);
2613 if (IS_ERR(journal->j_dev_bd)) { 2606 if (IS_ERR(journal->j_dev_bd)) {
2614 result = PTR_ERR(journal->j_dev_bd); 2607 result = PTR_ERR(journal->j_dev_bd);
2615 journal->j_dev_bd = NULL; 2608 journal->j_dev_bd = NULL;
diff --git a/fs/super.c b/fs/super.c
index ca696155cd9a..5d9a4497849a 100644
--- a/fs/super.c
+++ b/fs/super.c
@@ -766,13 +766,13 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
766{ 766{
767 struct block_device *bdev; 767 struct block_device *bdev;
768 struct super_block *s; 768 struct super_block *s;
769 fmode_t mode = FMODE_READ; 769 fmode_t mode = FMODE_READ | FMODE_EXCL;
770 int error = 0; 770 int error = 0;
771 771
772 if (!(flags & MS_RDONLY)) 772 if (!(flags & MS_RDONLY))
773 mode |= FMODE_WRITE; 773 mode |= FMODE_WRITE;
774 774
775 bdev = open_bdev_exclusive(dev_name, mode, fs_type); 775 bdev = blkdev_get_by_path(dev_name, mode, fs_type);
776 if (IS_ERR(bdev)) 776 if (IS_ERR(bdev))
777 return ERR_CAST(bdev); 777 return ERR_CAST(bdev);
778 778
@@ -801,13 +801,13 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
801 801
802 /* 802 /*
803 * s_umount nests inside bd_mutex during 803 * s_umount nests inside bd_mutex during
804 * __invalidate_device(). close_bdev_exclusive() 804 * __invalidate_device(). blkdev_put() acquires
805 * acquires bd_mutex and can't be called under 805 * bd_mutex and can't be called under s_umount. Drop
806 * s_umount. Drop s_umount temporarily. This is safe 806 * s_umount temporarily. This is safe as we're
807 * as we're holding an active reference. 807 * holding an active reference.
808 */ 808 */
809 up_write(&s->s_umount); 809 up_write(&s->s_umount);
810 close_bdev_exclusive(bdev, mode); 810 blkdev_put(bdev, mode);
811 down_write(&s->s_umount); 811 down_write(&s->s_umount);
812 } else { 812 } else {
813 char b[BDEVNAME_SIZE]; 813 char b[BDEVNAME_SIZE];
@@ -831,7 +831,7 @@ struct dentry *mount_bdev(struct file_system_type *fs_type,
831error_s: 831error_s:
832 error = PTR_ERR(s); 832 error = PTR_ERR(s);
833error_bdev: 833error_bdev:
834 close_bdev_exclusive(bdev, mode); 834 blkdev_put(bdev, mode);
835error: 835error:
836 return ERR_PTR(error); 836 return ERR_PTR(error);
837} 837}
@@ -862,7 +862,8 @@ void kill_block_super(struct super_block *sb)
862 bdev->bd_super = NULL; 862 bdev->bd_super = NULL;
863 generic_shutdown_super(sb); 863 generic_shutdown_super(sb);
864 sync_blockdev(bdev); 864 sync_blockdev(bdev);
865 close_bdev_exclusive(bdev, mode); 865 WARN_ON_ONCE(!(mode & FMODE_EXCL));
866 blkdev_put(bdev, mode | FMODE_EXCL);
866} 867}
867 868
868EXPORT_SYMBOL(kill_block_super); 869EXPORT_SYMBOL(kill_block_super);
diff --git a/fs/xfs/linux-2.6/xfs_super.c b/fs/xfs/linux-2.6/xfs_super.c
index 064f964d4f3c..2d2ce7f651a7 100644
--- a/fs/xfs/linux-2.6/xfs_super.c
+++ b/fs/xfs/linux-2.6/xfs_super.c
@@ -606,7 +606,8 @@ xfs_blkdev_get(
606{ 606{
607 int error = 0; 607 int error = 0;
608 608
609 *bdevp = open_bdev_exclusive(name, FMODE_READ|FMODE_WRITE, mp); 609 *bdevp = blkdev_get_by_path(name, FMODE_READ|FMODE_WRITE|FMODE_EXCL,
610 mp);
610 if (IS_ERR(*bdevp)) { 611 if (IS_ERR(*bdevp)) {
611 error = PTR_ERR(*bdevp); 612 error = PTR_ERR(*bdevp);
612 printk("XFS: Invalid device [%s], error=%d\n", name, error); 613 printk("XFS: Invalid device [%s], error=%d\n", name, error);
@@ -620,7 +621,7 @@ xfs_blkdev_put(
620 struct block_device *bdev) 621 struct block_device *bdev)
621{ 622{
622 if (bdev) 623 if (bdev)
623 close_bdev_exclusive(bdev, FMODE_READ|FMODE_WRITE); 624 blkdev_put(bdev, FMODE_READ|FMODE_WRITE|FMODE_EXCL);
624} 625}
625 626
626/* 627/*
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 334d68a17108..f48501563917 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -663,7 +663,7 @@ struct block_device {
663 void * bd_holder; 663 void * bd_holder;
664 int bd_holders; 664 int bd_holders;
665#ifdef CONFIG_SYSFS 665#ifdef CONFIG_SYSFS
666 struct list_head bd_holder_list; 666 struct gendisk * bd_holder_disk; /* for sysfs slave linkng */
667#endif 667#endif
668 struct block_device * bd_contains; 668 struct block_device * bd_contains;
669 unsigned bd_block_size; 669 unsigned bd_block_size;
@@ -2006,7 +2006,6 @@ extern struct block_device *bdgrab(struct block_device *bdev);
2006extern void bd_set_size(struct block_device *, loff_t size); 2006extern void bd_set_size(struct block_device *, loff_t size);
2007extern void bd_forget(struct inode *inode); 2007extern void bd_forget(struct inode *inode);
2008extern void bdput(struct block_device *); 2008extern void bdput(struct block_device *);
2009extern struct block_device *open_by_devnum(dev_t, fmode_t);
2010extern void invalidate_bdev(struct block_device *); 2009extern void invalidate_bdev(struct block_device *);
2011extern int sync_blockdev(struct block_device *bdev); 2010extern int sync_blockdev(struct block_device *bdev);
2012extern struct super_block *freeze_bdev(struct block_device *); 2011extern struct super_block *freeze_bdev(struct block_device *);
@@ -2037,16 +2036,20 @@ extern const struct file_operations def_fifo_fops;
2037extern int ioctl_by_bdev(struct block_device *, unsigned, unsigned long); 2036extern int ioctl_by_bdev(struct block_device *, unsigned, unsigned long);
2038extern int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long); 2037extern int blkdev_ioctl(struct block_device *, fmode_t, unsigned, unsigned long);
2039extern long compat_blkdev_ioctl(struct file *, unsigned, unsigned long); 2038extern long compat_blkdev_ioctl(struct file *, unsigned, unsigned long);
2040extern int blkdev_get(struct block_device *, fmode_t); 2039extern int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder);
2041extern int blkdev_put(struct block_device *, fmode_t); 2040extern struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
2042extern int bd_claim(struct block_device *, void *); 2041 void *holder);
2043extern void bd_release(struct block_device *); 2042extern struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode,
2043 void *holder);
2044extern int blkdev_put(struct block_device *bdev, fmode_t mode);
2044#ifdef CONFIG_SYSFS 2045#ifdef CONFIG_SYSFS
2045extern int bd_claim_by_disk(struct block_device *, void *, struct gendisk *); 2046extern int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk);
2046extern void bd_release_from_disk(struct block_device *, struct gendisk *);
2047#else 2047#else
2048#define bd_claim_by_disk(bdev, holder, disk) bd_claim(bdev, holder) 2048static inline int bd_link_disk_holder(struct block_device *bdev,
2049#define bd_release_from_disk(bdev, disk) bd_release(bdev) 2049 struct gendisk *disk)
2050{
2051 return 0;
2052}
2050#endif 2053#endif
2051#endif 2054#endif
2052 2055
@@ -2082,8 +2085,6 @@ static inline void unregister_chrdev(unsigned int major, const char *name)
2082extern const char *__bdevname(dev_t, char *buffer); 2085extern const char *__bdevname(dev_t, char *buffer);
2083extern const char *bdevname(struct block_device *bdev, char *buffer); 2086extern const char *bdevname(struct block_device *bdev, char *buffer);
2084extern struct block_device *lookup_bdev(const char *); 2087extern struct block_device *lookup_bdev(const char *);
2085extern struct block_device *open_bdev_exclusive(const char *, fmode_t, void *);
2086extern void close_bdev_exclusive(struct block_device *, fmode_t);
2087extern void blkdev_show(struct seq_file *,off_t); 2088extern void blkdev_show(struct seq_file *,off_t);
2088 2089
2089#else 2090#else
diff --git a/kernel/power/swap.c b/kernel/power/swap.c
index a0e4a86ccf94..b019609d1b45 100644
--- a/kernel/power/swap.c
+++ b/kernel/power/swap.c
@@ -223,7 +223,7 @@ static int swsusp_swap_check(void)
223 return res; 223 return res;
224 224
225 root_swap = res; 225 root_swap = res;
226 res = blkdev_get(hib_resume_bdev, FMODE_WRITE); 226 res = blkdev_get(hib_resume_bdev, FMODE_WRITE, NULL);
227 if (res) 227 if (res)
228 return res; 228 return res;
229 229
@@ -907,7 +907,8 @@ int swsusp_check(void)
907{ 907{
908 int error; 908 int error;
909 909
910 hib_resume_bdev = open_by_devnum(swsusp_resume_device, FMODE_READ); 910 hib_resume_bdev = blkdev_get_by_dev(swsusp_resume_device,
911 FMODE_READ, NULL);
911 if (!IS_ERR(hib_resume_bdev)) { 912 if (!IS_ERR(hib_resume_bdev)) {
912 set_blocksize(hib_resume_bdev, PAGE_SIZE); 913 set_blocksize(hib_resume_bdev, PAGE_SIZE);
913 clear_page(swsusp_header); 914 clear_page(swsusp_header);
diff --git a/mm/swapfile.c b/mm/swapfile.c
index 67ddaaf98c74..b6adcfbf6f48 100644
--- a/mm/swapfile.c
+++ b/mm/swapfile.c
@@ -1677,7 +1677,7 @@ SYSCALL_DEFINE1(swapoff, const char __user *, specialfile)
1677 if (S_ISBLK(inode->i_mode)) { 1677 if (S_ISBLK(inode->i_mode)) {
1678 struct block_device *bdev = I_BDEV(inode); 1678 struct block_device *bdev = I_BDEV(inode);
1679 set_blocksize(bdev, p->old_block_size); 1679 set_blocksize(bdev, p->old_block_size);
1680 bd_release(bdev); 1680 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
1681 } else { 1681 } else {
1682 mutex_lock(&inode->i_mutex); 1682 mutex_lock(&inode->i_mutex);
1683 inode->i_flags &= ~S_SWAPFILE; 1683 inode->i_flags &= ~S_SWAPFILE;
@@ -1939,7 +1939,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
1939 error = -EINVAL; 1939 error = -EINVAL;
1940 if (S_ISBLK(inode->i_mode)) { 1940 if (S_ISBLK(inode->i_mode)) {
1941 bdev = I_BDEV(inode); 1941 bdev = I_BDEV(inode);
1942 error = bd_claim(bdev, sys_swapon); 1942 error = blkdev_get(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL,
1943 sys_swapon);
1943 if (error < 0) { 1944 if (error < 0) {
1944 bdev = NULL; 1945 bdev = NULL;
1945 error = -EINVAL; 1946 error = -EINVAL;
@@ -2136,7 +2137,7 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
2136bad_swap: 2137bad_swap:
2137 if (bdev) { 2138 if (bdev) {
2138 set_blocksize(bdev, p->old_block_size); 2139 set_blocksize(bdev, p->old_block_size);
2139 bd_release(bdev); 2140 blkdev_put(bdev, FMODE_READ | FMODE_WRITE | FMODE_EXCL);
2140 } 2141 }
2141 destroy_swap_extents(p); 2142 destroy_swap_extents(p);
2142 swap_cgroup_swapoff(type); 2143 swap_cgroup_swapoff(type);