developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 1 | From d08d34636d1384d5ddb141e0bb72afa591c12118 Mon Sep 17 00:00:00 2001 |
| 2 | From: Sam Shih <sam.shih@mediatek.com> |
| 3 | Date: Fri, 2 Jun 2023 13:06:22 +0800 |
| 4 | Subject: [PATCH] |
| 5 | [spi-and-storage][999-2380-fix-dirty-race-between-do_tmpfile.patch] |
| 6 | |
| 7 | --- |
| 8 | fs/ubifs/dir.c | 60 +++++++++++++++++++++++++------------------------- |
| 9 | 1 file changed, 30 insertions(+), 30 deletions(-) |
| 10 | |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 11 | diff --git a/fs/ubifs/dir.c b/fs/ubifs/dir.c |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 12 | index 332c0b02a..97b231c43 100644 |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 13 | --- a/fs/ubifs/dir.c |
| 14 | +++ b/fs/ubifs/dir.c |
| 15 | @@ -356,6 +356,32 @@ out_budg: |
| 16 | return err; |
| 17 | } |
| 18 | |
| 19 | +/** |
| 20 | + * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
| 21 | + * @inode1: first inode |
| 22 | + * @inode2: second inode |
| 23 | + * |
| 24 | + * We do not implement any tricks to guarantee strict lock ordering, because |
| 25 | + * VFS has already done it for us on the @i_mutex. So this is just a simple |
| 26 | + * wrapper function. |
| 27 | + */ |
| 28 | +static void lock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 29 | +{ |
| 30 | + mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
| 31 | + mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
| 36 | + * @inode1: first inode |
| 37 | + * @inode2: second inode |
| 38 | + */ |
| 39 | +static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 40 | +{ |
| 41 | + mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
| 42 | + mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
| 43 | +} |
| 44 | + |
| 45 | static int do_tmpfile(struct inode *dir, struct dentry *dentry, |
| 46 | umode_t mode, struct inode **whiteout) |
| 47 | { |
developer | 0608635 | 2023-06-19 17:55:36 +0800 | [diff] [blame] | 48 | @@ -364,7 +390,7 @@ static int do_tmpfile(struct inode *dir, |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 49 | struct ubifs_budget_req req = { .new_ino = 1, .new_dent = 1, |
| 50 | .dirtied_ino = 1}; |
| 51 | struct ubifs_budget_req ino_req = { .dirtied_ino = 1 }; |
| 52 | - struct ubifs_inode *ui, *dir_ui = ubifs_inode(dir); |
| 53 | + struct ubifs_inode *ui; |
| 54 | int err, instantiated = 0; |
| 55 | struct fscrypt_name nm; |
| 56 | |
developer | 0608635 | 2023-06-19 17:55:36 +0800 | [diff] [blame] | 57 | @@ -426,11 +452,11 @@ static int do_tmpfile(struct inode *dir, |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 58 | instantiated = 1; |
| 59 | mutex_unlock(&ui->ui_mutex); |
| 60 | |
| 61 | - mutex_lock(&dir_ui->ui_mutex); |
| 62 | + lock_2_inodes(dir, inode); |
| 63 | err = ubifs_jnl_update(c, dir, &nm, inode, 1, 0); |
| 64 | if (err) |
| 65 | goto out_cancel; |
| 66 | - mutex_unlock(&dir_ui->ui_mutex); |
| 67 | + unlock_2_inodes(dir, inode); |
| 68 | |
| 69 | ubifs_release_budget(c, &req); |
developer | 0608635 | 2023-06-19 17:55:36 +0800 | [diff] [blame] | 70 | fscrypt_free_filename(&nm); |
| 71 | @@ -438,7 +464,7 @@ static int do_tmpfile(struct inode *dir, |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 72 | return 0; |
| 73 | |
| 74 | out_cancel: |
| 75 | - mutex_unlock(&dir_ui->ui_mutex); |
| 76 | + unlock_2_inodes(dir, inode); |
| 77 | out_inode: |
| 78 | make_bad_inode(inode); |
| 79 | if (!instantiated) |
developer | 0608635 | 2023-06-19 17:55:36 +0800 | [diff] [blame] | 80 | @@ -673,32 +699,6 @@ static int ubifs_dir_release(struct inod |
developer | 2142509 | 2022-06-08 17:05:12 +0800 | [diff] [blame] | 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | -/** |
| 85 | - * lock_2_inodes - a wrapper for locking two UBIFS inodes. |
| 86 | - * @inode1: first inode |
| 87 | - * @inode2: second inode |
| 88 | - * |
| 89 | - * We do not implement any tricks to guarantee strict lock ordering, because |
| 90 | - * VFS has already done it for us on the @i_mutex. So this is just a simple |
| 91 | - * wrapper function. |
| 92 | - */ |
| 93 | -static void lock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 94 | -{ |
| 95 | - mutex_lock_nested(&ubifs_inode(inode1)->ui_mutex, WB_MUTEX_1); |
| 96 | - mutex_lock_nested(&ubifs_inode(inode2)->ui_mutex, WB_MUTEX_2); |
| 97 | -} |
| 98 | - |
| 99 | -/** |
| 100 | - * unlock_2_inodes - a wrapper for unlocking two UBIFS inodes. |
| 101 | - * @inode1: first inode |
| 102 | - * @inode2: second inode |
| 103 | - */ |
| 104 | -static void unlock_2_inodes(struct inode *inode1, struct inode *inode2) |
| 105 | -{ |
| 106 | - mutex_unlock(&ubifs_inode(inode2)->ui_mutex); |
| 107 | - mutex_unlock(&ubifs_inode(inode1)->ui_mutex); |
| 108 | -} |
| 109 | - |
| 110 | static int ubifs_link(struct dentry *old_dentry, struct inode *dir, |
| 111 | struct dentry *dentry) |
| 112 | { |
developer | 5d148cb | 2023-06-02 13:08:11 +0800 | [diff] [blame] | 113 | -- |
| 114 | 2.34.1 |
| 115 | |