aboutsummaryrefslogtreecommitdiffstats
path: root/fs/block_dev.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/block_dev.c')
-rw-r--r--fs/block_dev.c741
1 files changed, 251 insertions, 490 deletions
diff --git a/fs/block_dev.c b/fs/block_dev.c
index 88da70355aa3..fe3f59c14a02 100644
--- a/fs/block_dev.c
+++ b/fs/block_dev.c
@@ -432,9 +432,6 @@ static void init_once(void *foo)
432 mutex_init(&bdev->bd_mutex); 432 mutex_init(&bdev->bd_mutex);
433 INIT_LIST_HEAD(&bdev->bd_inodes); 433 INIT_LIST_HEAD(&bdev->bd_inodes);
434 INIT_LIST_HEAD(&bdev->bd_list); 434 INIT_LIST_HEAD(&bdev->bd_list);
435#ifdef CONFIG_SYSFS
436 INIT_LIST_HEAD(&bdev->bd_holder_list);
437#endif
438 inode_init_once(&ei->vfs_inode); 435 inode_init_once(&ei->vfs_inode);
439 /* Initialize mutex for freeze. */ 436 /* Initialize mutex for freeze. */
440 mutex_init(&bdev->bd_fsfreeze_mutex); 437 mutex_init(&bdev->bd_fsfreeze_mutex);
@@ -669,7 +666,7 @@ static bool bd_may_claim(struct block_device *bdev, struct block_device *whole,
669 else if (bdev->bd_contains == bdev) 666 else if (bdev->bd_contains == bdev)
670 return true; /* is a whole device which isn't held */ 667 return true; /* is a whole device which isn't held */
671 668
672 else if (whole->bd_holder == bd_claim) 669 else if (whole->bd_holder == bd_may_claim)
673 return true; /* is a partition of a device that is being partitioned */ 670 return true; /* is a partition of a device that is being partitioned */
674 else if (whole->bd_holder != NULL) 671 else if (whole->bd_holder != NULL)
675 return false; /* is a partition of a held device */ 672 return false; /* is a partition of a held device */
@@ -781,439 +778,87 @@ static struct block_device *bd_start_claiming(struct block_device *bdev,
781 } 778 }
782} 779}
783 780
784/* releases bdev_lock */
785static void __bd_abort_claiming(struct block_device *whole, void *holder)
786{
787 BUG_ON(whole->bd_claiming != holder);
788 whole->bd_claiming = NULL;
789 wake_up_bit(&whole->bd_claiming, 0);
790
791 spin_unlock(&bdev_lock);
792 bdput(whole);
793}
794
795/**
796 * bd_abort_claiming - abort claiming a block device
797 * @whole: whole block device returned by bd_start_claiming()
798 * @holder: holder trying to claim @bdev
799 *
800 * Abort a claiming block started by bd_start_claiming(). Note that
801 * @whole is not the block device to be claimed but the whole device
802 * returned by bd_start_claiming().
803 *
804 * CONTEXT:
805 * Grabs and releases bdev_lock.
806 */
807static void bd_abort_claiming(struct block_device *whole, void *holder)
808{
809 spin_lock(&bdev_lock);
810 __bd_abort_claiming(whole, holder); /* releases bdev_lock */
811}
812
813/* increment holders when we have a legitimate claim. requires bdev_lock */
814static void __bd_claim(struct block_device *bdev, struct block_device *whole,
815 void *holder)
816{
817 /* note that for a whole device bd_holders
818 * will be incremented twice, and bd_holder will
819 * be set to bd_claim before being set to holder
820 */
821 whole->bd_holders++;
822 whole->bd_holder = bd_claim;
823 bdev->bd_holders++;
824 bdev->bd_holder = holder;
825}
826
827/**
828 * bd_finish_claiming - finish claiming a block device
829 * @bdev: block device of interest (passed to bd_start_claiming())
830 * @whole: whole block device returned by bd_start_claiming()
831 * @holder: holder trying to claim @bdev
832 *
833 * Finish a claiming block started by bd_start_claiming().
834 *
835 * CONTEXT:
836 * Grabs and releases bdev_lock.
837 */
838static void bd_finish_claiming(struct block_device *bdev,
839 struct block_device *whole, void *holder)
840{
841 spin_lock(&bdev_lock);
842 BUG_ON(!bd_may_claim(bdev, whole, holder));
843 __bd_claim(bdev, whole, holder);
844 __bd_abort_claiming(whole, holder); /* not actually an abort */
845}
846
847/**
848 * bd_claim - claim a block device
849 * @bdev: block device to claim
850 * @holder: holder trying to claim @bdev
851 *
852 * Try to claim @bdev which must have been opened successfully.
853 *
854 * CONTEXT:
855 * Might sleep.
856 *
857 * RETURNS:
858 * 0 if successful, -EBUSY if @bdev is already claimed.
859 */
860int bd_claim(struct block_device *bdev, void *holder)
861{
862 struct block_device *whole = bdev->bd_contains;
863 int res;
864
865 might_sleep();
866
867 spin_lock(&bdev_lock);
868 res = bd_prepare_to_claim(bdev, whole, holder);
869 if (res == 0)
870 __bd_claim(bdev, whole, holder);
871 spin_unlock(&bdev_lock);
872
873 return res;
874}
875EXPORT_SYMBOL(bd_claim);
876
877void bd_release(struct block_device *bdev)
878{
879 spin_lock(&bdev_lock);
880 if (!--bdev->bd_contains->bd_holders)
881 bdev->bd_contains->bd_holder = NULL;
882 if (!--bdev->bd_holders)
883 bdev->bd_holder = NULL;
884 spin_unlock(&bdev_lock);
885}
886
887EXPORT_SYMBOL(bd_release);
888
889#ifdef CONFIG_SYSFS 781#ifdef CONFIG_SYSFS
890/*
891 * Functions for bd_claim_by_kobject / bd_release_from_kobject
892 *
893 * If a kobject is passed to bd_claim_by_kobject()
894 * and the kobject has a parent directory,
895 * following symlinks are created:
896 * o from the kobject to the claimed bdev
897 * o from "holders" directory of the bdev to the parent of the kobject
898 * bd_release_from_kobject() removes these symlinks.
899 *
900 * Example:
901 * If /dev/dm-0 maps to /dev/sda, kobject corresponding to
902 * /sys/block/dm-0/slaves is passed to bd_claim_by_kobject(), then:
903 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
904 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
905 */
906
907static int add_symlink(struct kobject *from, struct kobject *to) 782static int add_symlink(struct kobject *from, struct kobject *to)
908{ 783{
909 if (!from || !to)
910 return 0;
911 return sysfs_create_link(from, to, kobject_name(to)); 784 return sysfs_create_link(from, to, kobject_name(to));
912} 785}
913 786
914static void del_symlink(struct kobject *from, struct kobject *to) 787static void del_symlink(struct kobject *from, struct kobject *to)
915{ 788{
916 if (!from || !to)
917 return;
918 sysfs_remove_link(from, kobject_name(to)); 789 sysfs_remove_link(from, kobject_name(to));
919} 790}
920 791
921/*
922 * 'struct bd_holder' contains pointers to kobjects symlinked by
923 * bd_claim_by_kobject.
924 * It's connected to bd_holder_list which is protected by bdev->bd_sem.
925 */
926struct bd_holder {
927 struct list_head list; /* chain of holders of the bdev */
928 int count; /* references from the holder */
929 struct kobject *sdir; /* holder object, e.g. "/block/dm-0/slaves" */
930 struct kobject *hdev; /* e.g. "/block/dm-0" */
931 struct kobject *hdir; /* e.g. "/block/sda/holders" */
932 struct kobject *sdev; /* e.g. "/block/sda" */
933};
934
935/*
936 * Get references of related kobjects at once.
937 * Returns 1 on success. 0 on failure.
938 *
939 * Should call bd_holder_release_dirs() after successful use.
940 */
941static int bd_holder_grab_dirs(struct block_device *bdev,
942 struct bd_holder *bo)
943{
944 if (!bdev || !bo)
945 return 0;
946
947 bo->sdir = kobject_get(bo->sdir);
948 if (!bo->sdir)
949 return 0;
950
951 bo->hdev = kobject_get(bo->sdir->parent);
952 if (!bo->hdev)
953 goto fail_put_sdir;
954
955 bo->sdev = kobject_get(&part_to_dev(bdev->bd_part)->kobj);
956 if (!bo->sdev)
957 goto fail_put_hdev;
958
959 bo->hdir = kobject_get(bdev->bd_part->holder_dir);
960 if (!bo->hdir)
961 goto fail_put_sdev;
962
963 return 1;
964
965fail_put_sdev:
966 kobject_put(bo->sdev);
967fail_put_hdev:
968 kobject_put(bo->hdev);
969fail_put_sdir:
970 kobject_put(bo->sdir);
971
972 return 0;
973}
974
975/* Put references of related kobjects at once. */
976static void bd_holder_release_dirs(struct bd_holder *bo)
977{
978 kobject_put(bo->hdir);
979 kobject_put(bo->sdev);
980 kobject_put(bo->hdev);
981 kobject_put(bo->sdir);
982}
983
984static struct bd_holder *alloc_bd_holder(struct kobject *kobj)
985{
986 struct bd_holder *bo;
987
988 bo = kzalloc(sizeof(*bo), GFP_KERNEL);
989 if (!bo)
990 return NULL;
991
992 bo->count = 1;
993 bo->sdir = kobj;
994
995 return bo;
996}
997
998static void free_bd_holder(struct bd_holder *bo)
999{
1000 kfree(bo);
1001}
1002
1003/** 792/**
1004 * find_bd_holder - find matching struct bd_holder from the block device 793 * bd_link_disk_holder - create symlinks between holding disk and slave bdev
794 * @bdev: the claimed slave bdev
795 * @disk: the holding disk
1005 * 796 *
1006 * @bdev: struct block device to be searched 797 * This functions creates the following sysfs symlinks.
1007 * @bo: target struct bd_holder
1008 *
1009 * Returns matching entry with @bo in @bdev->bd_holder_list.
1010 * If found, increment the reference count and return the pointer.
1011 * If not found, returns NULL.
1012 */
1013static struct bd_holder *find_bd_holder(struct block_device *bdev,
1014 struct bd_holder *bo)
1015{
1016 struct bd_holder *tmp;
1017
1018 list_for_each_entry(tmp, &bdev->bd_holder_list, list)
1019 if (tmp->sdir == bo->sdir) {
1020 tmp->count++;
1021 return tmp;
1022 }
1023
1024 return NULL;
1025}
1026
1027/**
1028 * add_bd_holder - create sysfs symlinks for bd_claim() relationship
1029 *
1030 * @bdev: block device to be bd_claimed
1031 * @bo: preallocated and initialized by alloc_bd_holder()
1032 *
1033 * Add @bo to @bdev->bd_holder_list, create symlinks.
1034 *
1035 * Returns 0 if symlinks are created.
1036 * Returns -ve if something fails.
1037 */
1038static int add_bd_holder(struct block_device *bdev, struct bd_holder *bo)
1039{
1040 int err;
1041
1042 if (!bo)
1043 return -EINVAL;
1044
1045 if (!bd_holder_grab_dirs(bdev, bo))
1046 return -EBUSY;
1047
1048 err = add_symlink(bo->sdir, bo->sdev);
1049 if (err)
1050 return err;
1051
1052 err = add_symlink(bo->hdir, bo->hdev);
1053 if (err) {
1054 del_symlink(bo->sdir, bo->sdev);
1055 return err;
1056 }
1057
1058 list_add_tail(&bo->list, &bdev->bd_holder_list);
1059 return 0;
1060}
1061
1062/**
1063 * del_bd_holder - delete sysfs symlinks for bd_claim() relationship
1064 * 798 *
1065 * @bdev: block device to be bd_claimed 799 * - from "slaves" directory of the holder @disk to the claimed @bdev
1066 * @kobj: holder's kobject 800 * - from "holders" directory of the @bdev to the holder @disk
1067 * 801 *
1068 * If there is matching entry with @kobj in @bdev->bd_holder_list 802 * For example, if /dev/dm-0 maps to /dev/sda and disk for dm-0 is
1069 * and no other bd_claim() from the same kobject, 803 * passed to bd_link_disk_holder(), then:
1070 * remove the struct bd_holder from the list, delete symlinks for it.
1071 * 804 *
1072 * Returns a pointer to the struct bd_holder when it's removed from the list 805 * /sys/block/dm-0/slaves/sda --> /sys/block/sda
1073 * and ready to be freed. 806 * /sys/block/sda/holders/dm-0 --> /sys/block/dm-0
1074 * Returns NULL if matching claim isn't found or there is other bd_claim()
1075 * by the same kobject.
1076 */
1077static struct bd_holder *del_bd_holder(struct block_device *bdev,
1078 struct kobject *kobj)
1079{
1080 struct bd_holder *bo;
1081
1082 list_for_each_entry(bo, &bdev->bd_holder_list, list) {
1083 if (bo->sdir == kobj) {
1084 bo->count--;
1085 BUG_ON(bo->count < 0);
1086 if (!bo->count) {
1087 list_del(&bo->list);
1088 del_symlink(bo->sdir, bo->sdev);
1089 del_symlink(bo->hdir, bo->hdev);
1090 bd_holder_release_dirs(bo);
1091 return bo;
1092 }
1093 break;
1094 }
1095 }
1096
1097 return NULL;
1098}
1099
1100/**
1101 * bd_claim_by_kobject - bd_claim() with additional kobject signature
1102 * 807 *
1103 * @bdev: block device to be claimed 808 * The caller must have claimed @bdev before calling this function and
1104 * @holder: holder's signature 809 * ensure that both @bdev and @disk are valid during the creation and
1105 * @kobj: holder's kobject 810 * lifetime of these symlinks.
1106 * 811 *
1107 * Do bd_claim() and if it succeeds, create sysfs symlinks between 812 * CONTEXT:
1108 * the bdev and the holder's kobject. 813 * Might sleep.
1109 * Use bd_release_from_kobject() when relesing the claimed bdev.
1110 * 814 *
1111 * Returns 0 on success. (same as bd_claim()) 815 * RETURNS:
1112 * Returns errno on failure. 816 * 0 on success, -errno on failure.
1113 */ 817 */
1114static int bd_claim_by_kobject(struct block_device *bdev, void *holder, 818int bd_link_disk_holder(struct block_device *bdev, struct gendisk *disk)
1115 struct kobject *kobj)
1116{ 819{
1117 int err; 820 int ret = 0;
1118 struct bd_holder *bo, *found;
1119
1120 if (!kobj)
1121 return -EINVAL;
1122
1123 bo = alloc_bd_holder(kobj);
1124 if (!bo)
1125 return -ENOMEM;
1126 821
1127 mutex_lock(&bdev->bd_mutex); 822 mutex_lock(&bdev->bd_mutex);
1128 823
1129 err = bd_claim(bdev, holder); 824 WARN_ON_ONCE(!bdev->bd_holder || bdev->bd_holder_disk);
1130 if (err)
1131 goto fail;
1132 825
1133 found = find_bd_holder(bdev, bo); 826 /* FIXME: remove the following once add_disk() handles errors */
1134 if (found) 827 if (WARN_ON(!disk->slave_dir || !bdev->bd_part->holder_dir))
1135 goto fail; 828 goto out_unlock;
1136 829
1137 err = add_bd_holder(bdev, bo); 830 ret = add_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1138 if (err) 831 if (ret)
1139 bd_release(bdev); 832 goto out_unlock;
1140 else
1141 bo = NULL;
1142fail:
1143 mutex_unlock(&bdev->bd_mutex);
1144 free_bd_holder(bo);
1145 return err;
1146}
1147 833
1148/** 834 ret = add_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1149 * bd_release_from_kobject - bd_release() with additional kobject signature 835 if (ret) {
1150 * 836 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1151 * @bdev: block device to be released 837 goto out_unlock;
1152 * @kobj: holder's kobject 838 }
1153 *
1154 * Do bd_release() and remove sysfs symlinks created by bd_claim_by_kobject().
1155 */
1156static void bd_release_from_kobject(struct block_device *bdev,
1157 struct kobject *kobj)
1158{
1159 if (!kobj)
1160 return;
1161 839
1162 mutex_lock(&bdev->bd_mutex); 840 bdev->bd_holder_disk = disk;
1163 bd_release(bdev); 841out_unlock:
1164 free_bd_holder(del_bd_holder(bdev, kobj));
1165 mutex_unlock(&bdev->bd_mutex); 842 mutex_unlock(&bdev->bd_mutex);
843 return ret;
1166} 844}
845EXPORT_SYMBOL_GPL(bd_link_disk_holder);
1167 846
1168/** 847static void bd_unlink_disk_holder(struct block_device *bdev)
1169 * bd_claim_by_disk - wrapper function for bd_claim_by_kobject()
1170 *
1171 * @bdev: block device to be claimed
1172 * @holder: holder's signature
1173 * @disk: holder's gendisk
1174 *
1175 * Call bd_claim_by_kobject() with getting @disk->slave_dir.
1176 */
1177int bd_claim_by_disk(struct block_device *bdev, void *holder,
1178 struct gendisk *disk)
1179{ 848{
1180 return bd_claim_by_kobject(bdev, holder, kobject_get(disk->slave_dir)); 849 struct gendisk *disk = bdev->bd_holder_disk;
1181}
1182EXPORT_SYMBOL_GPL(bd_claim_by_disk);
1183 850
1184/** 851 bdev->bd_holder_disk = NULL;
1185 * bd_release_from_disk - wrapper function for bd_release_from_kobject() 852 if (!disk)
1186 * 853 return;
1187 * @bdev: block device to be claimed
1188 * @disk: holder's gendisk
1189 *
1190 * Call bd_release_from_kobject() and put @disk->slave_dir.
1191 */
1192void bd_release_from_disk(struct block_device *bdev, struct gendisk *disk)
1193{
1194 bd_release_from_kobject(bdev, disk->slave_dir);
1195 kobject_put(disk->slave_dir);
1196}
1197EXPORT_SYMBOL_GPL(bd_release_from_disk);
1198#endif
1199 854
1200/* 855 del_symlink(disk->slave_dir, &part_to_dev(bdev->bd_part)->kobj);
1201 * Tries to open block device by device number. Use it ONLY if you 856 del_symlink(bdev->bd_part->holder_dir, &disk_to_dev(disk)->kobj);
1202 * really do not have anything better - i.e. when you are behind a
1203 * truly sucky interface and all you are given is a device number. _Never_
1204 * to be used for internal purposes. If you ever need it - reconsider
1205 * your API.
1206 */
1207struct block_device *open_by_devnum(dev_t dev, fmode_t mode)
1208{
1209 struct block_device *bdev = bdget(dev);
1210 int err = -ENOMEM;
1211 if (bdev)
1212 err = blkdev_get(bdev, mode);
1213 return err ? ERR_PTR(err) : bdev;
1214} 857}
1215 858#else
1216EXPORT_SYMBOL(open_by_devnum); 859static inline void bd_unlink_disk_holder(struct block_device *bdev)
860{ }
861#endif
1217 862
1218/** 863/**
1219 * flush_disk - invalidates all buffer-cache entries on a disk 864 * flush_disk - invalidates all buffer-cache entries on a disk
@@ -1309,10 +954,11 @@ int check_disk_change(struct block_device *bdev)
1309{ 954{
1310 struct gendisk *disk = bdev->bd_disk; 955 struct gendisk *disk = bdev->bd_disk;
1311 const struct block_device_operations *bdops = disk->fops; 956 const struct block_device_operations *bdops = disk->fops;
957 unsigned int events;
1312 958
1313 if (!bdops->media_changed) 959 events = disk_clear_events(disk, DISK_EVENT_MEDIA_CHANGE |
1314 return 0; 960 DISK_EVENT_EJECT_REQUEST);
1315 if (!bdops->media_changed(bdev->bd_disk)) 961 if (!(events & DISK_EVENT_MEDIA_CHANGE))
1316 return 0; 962 return 0;
1317 963
1318 flush_disk(bdev); 964 flush_disk(bdev);
@@ -1475,17 +1121,171 @@ static int __blkdev_get(struct block_device *bdev, fmode_t mode, int for_part)
1475 return ret; 1121 return ret;
1476} 1122}
1477 1123
1478int blkdev_get(struct block_device *bdev, fmode_t mode) 1124/**
1125 * blkdev_get - open a block device
1126 * @bdev: block_device to open
1127 * @mode: FMODE_* mask
1128 * @holder: exclusive holder identifier
1129 *
1130 * Open @bdev with @mode. If @mode includes %FMODE_EXCL, @bdev is
1131 * open with exclusive access. Specifying %FMODE_EXCL with %NULL
1132 * @holder is invalid. Exclusive opens may nest for the same @holder.
1133 *
1134 * On success, the reference count of @bdev is unchanged. On failure,
1135 * @bdev is put.
1136 *
1137 * CONTEXT:
1138 * Might sleep.
1139 *
1140 * RETURNS:
1141 * 0 on success, -errno on failure.
1142 */
1143int blkdev_get(struct block_device *bdev, fmode_t mode, void *holder)
1479{ 1144{
1480 return __blkdev_get(bdev, mode, 0); 1145 struct block_device *whole = NULL;
1146 int res;
1147
1148 WARN_ON_ONCE((mode & FMODE_EXCL) && !holder);
1149
1150 if ((mode & FMODE_EXCL) && holder) {
1151 whole = bd_start_claiming(bdev, holder);
1152 if (IS_ERR(whole)) {
1153 bdput(bdev);
1154 return PTR_ERR(whole);
1155 }
1156 }
1157
1158 res = __blkdev_get(bdev, mode, 0);
1159
1160 /* __blkdev_get() may alter read only status, check it afterwards */
1161 if (!res && (mode & FMODE_WRITE) && bdev_read_only(bdev)) {
1162 __blkdev_put(bdev, mode, 0);
1163 res = -EACCES;
1164 }
1165
1166 if (whole) {
1167 /* finish claiming */
1168 mutex_lock(&bdev->bd_mutex);
1169 spin_lock(&bdev_lock);
1170
1171 if (!res) {
1172 BUG_ON(!bd_may_claim(bdev, whole, holder));
1173 /*
1174 * Note that for a whole device bd_holders
1175 * will be incremented twice, and bd_holder
1176 * will be set to bd_may_claim before being
1177 * set to holder
1178 */
1179 whole->bd_holders++;
1180 whole->bd_holder = bd_may_claim;
1181 bdev->bd_holders++;
1182 bdev->bd_holder = holder;
1183 }
1184
1185 /* tell others that we're done */
1186 BUG_ON(whole->bd_claiming != holder);
1187 whole->bd_claiming = NULL;
1188 wake_up_bit(&whole->bd_claiming, 0);
1189
1190 spin_unlock(&bdev_lock);
1191
1192 /*
1193 * Block event polling for write claims. Any write
1194 * holder makes the write_holder state stick until all
1195 * are released. This is good enough and tracking
1196 * individual writeable reference is too fragile given
1197 * the way @mode is used in blkdev_get/put().
1198 */
1199 if (!res && (mode & FMODE_WRITE) && !bdev->bd_write_holder) {
1200 bdev->bd_write_holder = true;
1201 disk_block_events(bdev->bd_disk);
1202 }
1203
1204 mutex_unlock(&bdev->bd_mutex);
1205 bdput(whole);
1206 }
1207
1208 return res;
1481} 1209}
1482EXPORT_SYMBOL(blkdev_get); 1210EXPORT_SYMBOL(blkdev_get);
1483 1211
1212/**
1213 * blkdev_get_by_path - open a block device by name
1214 * @path: path to the block device to open
1215 * @mode: FMODE_* mask
1216 * @holder: exclusive holder identifier
1217 *
1218 * Open the blockdevice described by the device file at @path. @mode
1219 * and @holder are identical to blkdev_get().
1220 *
1221 * On success, the returned block_device has reference count of one.
1222 *
1223 * CONTEXT:
1224 * Might sleep.
1225 *
1226 * RETURNS:
1227 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1228 */
1229struct block_device *blkdev_get_by_path(const char *path, fmode_t mode,
1230 void *holder)
1231{
1232 struct block_device *bdev;
1233 int err;
1234
1235 bdev = lookup_bdev(path);
1236 if (IS_ERR(bdev))
1237 return bdev;
1238
1239 err = blkdev_get(bdev, mode, holder);
1240 if (err)
1241 return ERR_PTR(err);
1242
1243 return bdev;
1244}
1245EXPORT_SYMBOL(blkdev_get_by_path);
1246
1247/**
1248 * blkdev_get_by_dev - open a block device by device number
1249 * @dev: device number of block device to open
1250 * @mode: FMODE_* mask
1251 * @holder: exclusive holder identifier
1252 *
1253 * Open the blockdevice described by device number @dev. @mode and
1254 * @holder are identical to blkdev_get().
1255 *
1256 * Use it ONLY if you really do not have anything better - i.e. when
1257 * you are behind a truly sucky interface and all you are given is a
1258 * device number. _Never_ to be used for internal purposes. If you
1259 * ever need it - reconsider your API.
1260 *
1261 * On success, the returned block_device has reference count of one.
1262 *
1263 * CONTEXT:
1264 * Might sleep.
1265 *
1266 * RETURNS:
1267 * Pointer to block_device on success, ERR_PTR(-errno) on failure.
1268 */
1269struct block_device *blkdev_get_by_dev(dev_t dev, fmode_t mode, void *holder)
1270{
1271 struct block_device *bdev;
1272 int err;
1273
1274 bdev = bdget(dev);
1275 if (!bdev)
1276 return ERR_PTR(-ENOMEM);
1277
1278 err = blkdev_get(bdev, mode, holder);
1279 if (err)
1280 return ERR_PTR(err);
1281
1282 return bdev;
1283}
1284EXPORT_SYMBOL(blkdev_get_by_dev);
1285
1484static int blkdev_open(struct inode * inode, struct file * filp) 1286static int blkdev_open(struct inode * inode, struct file * filp)
1485{ 1287{
1486 struct block_device *whole = NULL;
1487 struct block_device *bdev; 1288 struct block_device *bdev;
1488 int res;
1489 1289
1490 /* 1290 /*
1491 * Preserve backwards compatibility and allow large file access 1291 * Preserve backwards compatibility and allow large file access
@@ -1506,26 +1306,9 @@ static int blkdev_open(struct inode * inode, struct file * filp)
1506 if (bdev == NULL) 1306 if (bdev == NULL)
1507 return -ENOMEM; 1307 return -ENOMEM;
1508 1308
1509 if (filp->f_mode & FMODE_EXCL) {
1510 whole = bd_start_claiming(bdev, filp);
1511 if (IS_ERR(whole)) {
1512 bdput(bdev);
1513 return PTR_ERR(whole);
1514 }
1515 }
1516
1517 filp->f_mapping = bdev->bd_inode->i_mapping; 1309 filp->f_mapping = bdev->bd_inode->i_mapping;
1518 1310
1519 res = blkdev_get(bdev, filp->f_mode); 1311 return blkdev_get(bdev, filp->f_mode, filp);
1520
1521 if (whole) {
1522 if (res == 0)
1523 bd_finish_claiming(bdev, whole, filp);
1524 else
1525 bd_abort_claiming(whole, filp);
1526 }
1527
1528 return res;
1529} 1312}
1530 1313
1531static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part) 1314static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
@@ -1539,6 +1322,7 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1539 bdev->bd_part_count--; 1322 bdev->bd_part_count--;
1540 1323
1541 if (!--bdev->bd_openers) { 1324 if (!--bdev->bd_openers) {
1325 WARN_ON_ONCE(bdev->bd_holders);
1542 sync_blockdev(bdev); 1326 sync_blockdev(bdev);
1543 kill_bdev(bdev); 1327 kill_bdev(bdev);
1544 } 1328 }
@@ -1569,6 +1353,45 @@ static int __blkdev_put(struct block_device *bdev, fmode_t mode, int for_part)
1569 1353
1570int blkdev_put(struct block_device *bdev, fmode_t mode) 1354int blkdev_put(struct block_device *bdev, fmode_t mode)
1571{ 1355{
1356 if (mode & FMODE_EXCL) {
1357 bool bdev_free;
1358
1359 /*
1360 * Release a claim on the device. The holder fields
1361 * are protected with bdev_lock. bd_mutex is to
1362 * synchronize disk_holder unlinking.
1363 */
1364 mutex_lock(&bdev->bd_mutex);
1365 spin_lock(&bdev_lock);
1366
1367 WARN_ON_ONCE(--bdev->bd_holders < 0);
1368 WARN_ON_ONCE(--bdev->bd_contains->bd_holders < 0);
1369
1370 /* bd_contains might point to self, check in a separate step */
1371 if ((bdev_free = !bdev->bd_holders))
1372 bdev->bd_holder = NULL;
1373 if (!bdev->bd_contains->bd_holders)
1374 bdev->bd_contains->bd_holder = NULL;
1375
1376 spin_unlock(&bdev_lock);
1377
1378 /*
1379 * If this was the last claim, remove holder link and
1380 * unblock evpoll if it was a write holder.
1381 */
1382 if (bdev_free) {
1383 bd_unlink_disk_holder(bdev);
1384 if (bdev->bd_write_holder) {
1385 disk_unblock_events(bdev->bd_disk);
1386 bdev->bd_write_holder = false;
1387 } else
1388 disk_check_events(bdev->bd_disk);
1389 }
1390
1391 mutex_unlock(&bdev->bd_mutex);
1392 } else
1393 disk_check_events(bdev->bd_disk);
1394
1572 return __blkdev_put(bdev, mode, 0); 1395 return __blkdev_put(bdev, mode, 0);
1573} 1396}
1574EXPORT_SYMBOL(blkdev_put); 1397EXPORT_SYMBOL(blkdev_put);
@@ -1576,8 +1399,7 @@ EXPORT_SYMBOL(blkdev_put);
1576static int blkdev_close(struct inode * inode, struct file * filp) 1399static int blkdev_close(struct inode * inode, struct file * filp)
1577{ 1400{
1578 struct block_device *bdev = I_BDEV(filp->f_mapping->host); 1401 struct block_device *bdev = I_BDEV(filp->f_mapping->host);
1579 if (bdev->bd_holder == filp) 1402
1580 bd_release(bdev);
1581 return blkdev_put(bdev, filp->f_mode); 1403 return blkdev_put(bdev, filp->f_mode);
1582} 1404}
1583 1405
@@ -1722,67 +1544,6 @@ fail:
1722} 1544}
1723EXPORT_SYMBOL(lookup_bdev); 1545EXPORT_SYMBOL(lookup_bdev);
1724 1546
1725/**
1726 * open_bdev_exclusive - open a block device by name and set it up for use
1727 *
1728 * @path: special file representing the block device
1729 * @mode: FMODE_... combination to pass be used
1730 * @holder: owner for exclusion
1731 *
1732 * Open the blockdevice described by the special file at @path, claim it
1733 * for the @holder.
1734 */
1735struct block_device *open_bdev_exclusive(const char *path, fmode_t mode, void *holder)
1736{
1737 struct block_device *bdev, *whole;
1738 int error;
1739
1740 bdev = lookup_bdev(path);
1741 if (IS_ERR(bdev))
1742 return bdev;
1743
1744 whole = bd_start_claiming(bdev, holder);
1745 if (IS_ERR(whole)) {
1746 bdput(bdev);
1747 return whole;
1748 }
1749
1750 error = blkdev_get(bdev, mode);
1751 if (error)
1752 goto out_abort_claiming;
1753
1754 error = -EACCES;
1755 if ((mode & FMODE_WRITE) && bdev_read_only(bdev))
1756 goto out_blkdev_put;
1757
1758 bd_finish_claiming(bdev, whole, holder);
1759 return bdev;
1760
1761out_blkdev_put:
1762 blkdev_put(bdev, mode);
1763out_abort_claiming:
1764 bd_abort_claiming(whole, holder);
1765 return ERR_PTR(error);
1766}
1767
1768EXPORT_SYMBOL(open_bdev_exclusive);
1769
1770/**
1771 * close_bdev_exclusive - close a blockdevice opened by open_bdev_exclusive()
1772 *
1773 * @bdev: blockdevice to close
1774 * @mode: mode, must match that used to open.
1775 *
1776 * This is the counterpart to open_bdev_exclusive().
1777 */
1778void close_bdev_exclusive(struct block_device *bdev, fmode_t mode)
1779{
1780 bd_release(bdev);
1781 blkdev_put(bdev, mode);
1782}
1783
1784EXPORT_SYMBOL(close_bdev_exclusive);
1785
1786int __invalidate_device(struct block_device *bdev) 1547int __invalidate_device(struct block_device *bdev)
1787{ 1548{
1788 struct super_block *sb = get_super(bdev); 1549 struct super_block *sb = get_super(bdev);