diff options
Diffstat (limited to 'fs/ubifs/dir.c')
-rw-r--r-- | fs/ubifs/dir.c | 92 |
1 files changed, 36 insertions, 56 deletions
diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c index d29b771cce45..f55d523c52bb 100644 --- a/fs/ubifs/dir.c +++ b/fs/ubifs/dir.c | |||
@@ -482,30 +482,29 @@ static int ubifs_dir_release(struct inode *dir, struct file *file) | |||
482 | } | 482 | } |
483 | 483 | ||
484 | /** | 484 | /** |
485 | * lock_2_inodes - lock two UBIFS inodes. | 485 | * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
486 | * @inode1: first inode | 486 | * @inode1: first inode |
487 | * @inode2: second inode | 487 | * @inode2: second inode |
488 | * | ||
489 | * We do not implement any tricks to guarantee strict lock ordering, because | ||
490 | * VFS has already done it for us on the @i_mutex. So this is just a simple | ||
491 | * wrapper function. | ||
488 | */ | 492 | */ |
489 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) | 493 | static void lock_2_inodes(struct inode *inode1, struct inode *inode2) |
490 | { | 494 | { |
491 | if (inode1->i_ino < inode2->i_ino) { | 495 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
492 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_2); | 496 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
493 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_3); | ||
494 | } else { | ||
495 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); | ||
496 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_3); | ||
497 | } | ||
498 | } | 497 | } |
499 | 498 | ||
500 | /** | 499 | /** |
501 | * unlock_2_inodes - unlock two UBIFS inodes inodes. | 500 | * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
502 | * @inode1: first inode | 501 | * @inode1: first inode |
503 | * @inode2: second inode | 502 | * @inode2: second inode |
504 | */ | 503 | */ |
505 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) | 504 | static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) |
506 | { | 505 | { |
507 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | ||
508 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | 506 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
507 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | ||
509 | } | 508 | } |
510 | 509 | ||
511 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, | 510 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, |
@@ -527,6 +526,8 @@ static int ubifs_link(struct dentry *old_dentry, struct inode *dir, | |||
527 | dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu", | 526 | dbg_gen("dent '%.*s' to ino %lu (nlink %d) in dir ino %lu", |
528 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, | 527 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, |
529 | inode->i_nlink, dir->i_ino); | 528 | inode->i_nlink, dir->i_ino); |
529 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); | ||
530 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | ||
530 | err = dbg_check_synced_i_size(inode); | 531 | err = dbg_check_synced_i_size(inode); |
531 | if (err) | 532 | if (err) |
532 | return err; | 533 | return err; |
@@ -580,6 +581,8 @@ static int ubifs_unlink(struct inode *dir, struct dentry *dentry) | |||
580 | dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu", | 581 | dbg_gen("dent '%.*s' from ino %lu (nlink %d) in dir ino %lu", |
581 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, | 582 | dentry->d_name.len, dentry->d_name.name, inode->i_ino, |
582 | inode->i_nlink, dir->i_ino); | 583 | inode->i_nlink, dir->i_ino); |
584 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); | ||
585 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | ||
583 | err = dbg_check_synced_i_size(inode); | 586 | err = dbg_check_synced_i_size(inode); |
584 | if (err) | 587 | if (err) |
585 | return err; | 588 | return err; |
@@ -667,7 +670,8 @@ static int ubifs_rmdir(struct inode *dir, struct dentry *dentry) | |||
667 | 670 | ||
668 | dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len, | 671 | dbg_gen("directory '%.*s', ino %lu in dir ino %lu", dentry->d_name.len, |
669 | dentry->d_name.name, inode->i_ino, dir->i_ino); | 672 | dentry->d_name.name, inode->i_ino, dir->i_ino); |
670 | 673 | ubifs_assert(mutex_is_locked(&dir->i_mutex)); | |
674 | ubifs_assert(mutex_is_locked(&inode->i_mutex)); | ||
671 | err = check_dir_empty(c, dentry->d_inode); | 675 | err = check_dir_empty(c, dentry->d_inode); |
672 | if (err) | 676 | if (err) |
673 | return err; | 677 | return err; |
@@ -922,59 +926,30 @@ out_budg: | |||
922 | } | 926 | } |
923 | 927 | ||
924 | /** | 928 | /** |
925 | * lock_3_inodes - lock three UBIFS inodes for rename. | 929 | * lock_3_inodes - a wrapper for locking three UBIFS inodes. |
926 | * @inode1: first inode | 930 | * @inode1: first inode |
927 | * @inode2: second inode | 931 | * @inode2: second inode |
928 | * @inode3: third inode | 932 | * @inode3: third inode |
929 | * | 933 | * |
930 | * For 'ubifs_rename()', @inode1 may be the same as @inode2 whereas @inode3 may | 934 | * This function is used for 'ubifs_rename()' and @inode1 may be the same as |
931 | * be null. | 935 | * @inode2 whereas @inode3 may be %NULL. |
936 | * | ||
937 | * We do not implement any tricks to guarantee strict lock ordering, because | ||
938 | * VFS has already done it for us on the @i_mutex. So this is just a simple | ||
939 | * wrapper function. | ||
932 | */ | 940 | */ |
933 | static void lock_3_inodes(struct inode *inode1, struct inode *inode2, | 941 | static void lock_3_inodes(struct inode *inode1, struct inode *inode2, |
934 | struct inode *inode3) | 942 | struct inode *inode3) |
935 | { | 943 | { |
936 | struct inode *i1, *i2, *i3; | 944 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
937 | 945 | if (inode2 != inode1) | |
938 | if (!inode3) { | 946 | mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
939 | if (inode1 != inode2) { | 947 | if (inode3) |
940 | lock_2_inodes(inode1, inode2); | 948 | mutex_lock_nested(&ubifs_inode(inode3)->ui_mutex, WB_MUTEX_3); |
941 | return; | ||
942 | } | ||
943 | mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); | ||
944 | return; | ||
945 | } | ||
946 | |||
947 | if (inode1 == inode2) { | ||
948 | lock_2_inodes(inode1, inode3); | ||
949 | return; | ||
950 | } | ||
951 | |||
952 | /* 3 different inodes */ | ||
953 | if (inode1 < inode2) { | ||
954 | i3 = inode2; | ||
955 | if (inode1 < inode3) { | ||
956 | i1 = inode1; | ||
957 | i2 = inode3; | ||
958 | } else { | ||
959 | i1 = inode3; | ||
960 | i2 = inode1; | ||
961 | } | ||
962 | } else { | ||
963 | i3 = inode1; | ||
964 | if (inode2 < inode3) { | ||
965 | i1 = inode2; | ||
966 | i2 = inode3; | ||
967 | } else { | ||
968 | i1 = inode3; | ||
969 | i2 = inode2; | ||
970 | } | ||
971 | } | ||
972 | mutex_lock_nested(&ubifs_inode(i1)->ui_mutex, WB_MUTEX_1); | ||
973 | lock_2_inodes(i2, i3); | ||
974 | } | 949 | } |
975 | 950 | ||
976 | /** | 951 | /** |
977 | * unlock_3_inodes - unlock three UBIFS inodes for rename. | 952 | * unlock_3_inodes - a wrapper for unlocking three UBIFS inodes for rename. |
978 | * @inode1: first inode | 953 | * @inode1: first inode |
979 | * @inode2: second inode | 954 | * @inode2: second inode |
980 | * @inode3: third inode | 955 | * @inode3: third inode |
@@ -982,11 +957,11 @@ static void lock_3_inodes(struct inode *inode1, struct inode *inode2, | |||
982 | static void unlock_3_inodes(struct inode *inode1, struct inode *inode2, | 957 | static void unlock_3_inodes(struct inode *inode1, struct inode *inode2, |
983 | struct inode *inode3) | 958 | struct inode *inode3) |
984 | { | 959 | { |
985 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | ||
986 | if (inode1 != inode2) | ||
987 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | ||
988 | if (inode3) | 960 | if (inode3) |
989 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); | 961 | mutex_unlock(&ubifs_inode(inode3)->ui_mutex); |
962 | if (inode1 != inode2) | ||
963 | mutex_unlock(&ubifs_inode(inode2)->ui_mutex); | ||
964 | mutex_unlock(&ubifs_inode(inode1)->ui_mutex); | ||
990 | } | 965 | } |
991 | 966 | ||
992 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, | 967 | static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, |
@@ -1020,6 +995,11 @@ static int ubifs_rename(struct inode *old_dir, struct dentry *old_dentry, | |||
1020 | "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name, | 995 | "dir ino %lu", old_dentry->d_name.len, old_dentry->d_name.name, |
1021 | old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len, | 996 | old_inode->i_ino, old_dir->i_ino, new_dentry->d_name.len, |
1022 | new_dentry->d_name.name, new_dir->i_ino); | 997 | new_dentry->d_name.name, new_dir->i_ino); |
998 | ubifs_assert(mutex_is_locked(&old_dir->i_mutex)); | ||
999 | ubifs_assert(mutex_is_locked(&new_dir->i_mutex)); | ||
1000 | if (unlink) | ||
1001 | ubifs_assert(mutex_is_locked(&new_inode->i_mutex)); | ||
1002 | |||
1023 | 1003 | ||
1024 | if (unlink && is_dir) { | 1004 | if (unlink && is_dir) { |
1025 | err = check_dir_empty(c, new_inode); | 1005 | err = check_dir_empty(c, new_inode); |