blob: 94ce9bd5e2f786c6956837032e94ef4586ee17ef [file] [log] [blame]
developer21425092022-06-08 17:05:12 +08001diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c
2--- a/fs/ubifs/dir.c
3+++ b/fs/ubifs/dir.c
4@@ -356,6 +356,32 @@ out_budg:
5 return err;
6 }
7
8+/**
9+ * lock_2_inodes - a wrapper for locking two UBIFS inodes.
10+ * @inode1: first inode
11+ * @inode2: second inode
12+ *
13+ * We do not implement any tricks to guarantee strict lock ordering, because
14+ * VFS has already done it for us on the @i_mutex. So this is just a simple
15+ * wrapper function.
16+ */
17+static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
18+{
19+ mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
20+ mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
21+}
22+
23+/**
24+ * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
25+ * @inode1: first inode
26+ * @inode2: second inode
27+ */
28+static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
29+{
30+ mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
31+ mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
32+}
33+
34 static int do_tmpfile(struct inode *dir, struct dentry *dentry,
35 umode_t mode, struct inode **whiteout)
36 {
37@@ -364,7 +390,7 @@ static int do_tmpfile(struct inode *dir, struct dentry *dentry,
38 struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1,
39 .dirtied_ino = 1};
40 struct ubifs_budget_req ino_req = { .dirtied_ino = 1 };
41- struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir);
42+ struct ubifs_inode *ui;
43 int err, instantiated = 0;
44 struct fscrypt_name nm;
45
46@@ -426,18 +452,18 @@ static int do_tmpfile(struct inode *dir, struct dentry *dentry,
47 instantiated = 1;
48 mutex_unlock(&ui->ui_mutex);
49
50- mutex_lock(&dir_ui->ui_mutex);
51+ lock_2_inodes(dir, inode);
52 err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0);
53 if (err)
54 goto out_cancel;
55- mutex_unlock(&dir_ui->ui_mutex);
56+ unlock_2_inodes(dir, inode);
57
58 ubifs_release_budget(c, &req);
59
60 return 0;
61
62 out_cancel:
63- mutex_unlock(&dir_ui->ui_mutex);
64+ unlock_2_inodes(dir, inode);
65 out_inode:
66 make_bad_inode(inode);
67 if (!instantiated)
68@@ -672,32 +698,6 @@ static int ubifs_dir_release(struct inode *dir, struct file *file)
69 return 0;
70 }
71
72-/**
73- * lock_2_inodes - a wrapper for locking two UBIFS inodes.
74- * @inode1: first inode
75- * @inode2: second inode
76- *
77- * We do not implement any tricks to guarantee strict lock ordering, because
78- * VFS has already done it for us on the @i_mutex. So this is just a simple
79- * wrapper function.
80- */
81-static void lock_2_inodes(struct inode *inode1, struct inode *inode2)
82-{
83- mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1);
84- mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2);
85-}
86-
87-/**
88- * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes.
89- * @inode1: first inode
90- * @inode2: second inode
91- */
92-static void unlock_2_inodes(struct inode *inode1, struct inode *inode2)
93-{
94- mutex_unlock(&ubifs_inode(inode2)->ui_mutex);
95- mutex_unlock(&ubifs_inode(inode1)->ui_mutex);
96-}
97-
98 static int ubifs_link(struct dentry *old_dentry, struct inode *dir,
99 struct dentry *dentry)
100 {