Update jan's s_orphan scalability patches to v3
[ext4-patch-queue.git] / reduce-contention-on-s_orphan_lock
blobdcf7b809e8fe345191040a09e3e6dbeb872209c0
1 ext4: reduce contention on s_orphan_lock
3 From: Jan Kara <jack@suse.cz>
5 Shuffle code around in ext4_orphan_add() and ext4_orphan_del() so that
6 we avoid taking global s_orphan_lock in some cases and hold it for
7 shorter time in other cases.
9 Signed-off-by: Jan Kara <jack@suse.cz>
10 Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
11 ---
12  fs/ext4/namei.c | 109 +++++++++++++++++++++++++++++++++++------------------------
13  1 file changed, 65 insertions(+), 44 deletions(-)
15 diff --git a/fs/ext4/namei.c b/fs/ext4/namei.c
16 index eb61584..3520ab8 100644
17 --- a/fs/ext4/namei.c
18 +++ b/fs/ext4/namei.c
19 @@ -2540,13 +2540,17 @@ static int empty_dir(struct inode *inode)
20         return 1;
21  }
23 -/* ext4_orphan_add() links an unlinked or truncated inode into a list of
24 +/*
25 + * ext4_orphan_add() links an unlinked or truncated inode into a list of
26   * such inodes, starting at the superblock, in case we crash before the
27   * file is closed/deleted, or in case the inode truncate spans multiple
28   * transactions and the last transaction is not recovered after a crash.
29   *
30   * At filesystem recovery time, we walk this list deleting unlinked
31   * inodes and truncating linked inodes in ext4_orphan_cleanup().
32 + *
33 + * Orphan list manipulation functions must be called under i_mutex unless
34 + * we are just creating the inode or deleting it.
35   */
36  int ext4_orphan_add(handle_t *handle, struct inode *inode)
37  {
38 @@ -2554,13 +2558,19 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
39         struct ext4_sb_info *sbi = EXT4_SB(sb);
40         struct ext4_iloc iloc;
41         int err = 0, rc;
42 +       bool dirty = false;
44         if (!sbi->s_journal)
45                 return 0;
47 -       mutex_lock(&sbi->s_orphan_lock);
48 +       WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
49 +                    !mutex_is_locked(&inode->i_mutex));
50 +       /*
51 +        * Exit early if inode already is on orphan list. This is a big speedup
52 +        * since we don't have to contend on the global s_orphan_lock.
53 +        */
54         if (!list_empty(&EXT4_I(inode)->i_orphan))
55 -               goto out_unlock;
56 +               return 0;
58         /*
59          * Orphan handling is only valid for files with data blocks
60 @@ -2574,44 +2584,47 @@ int ext4_orphan_add(handle_t *handle, struct inode *inode)
61         BUFFER_TRACE(sbi->s_sbh, "get_write_access");
62         err = ext4_journal_get_write_access(handle, sbi->s_sbh);
63         if (err)
64 -               goto out_unlock;
65 +               goto out;
67         err = ext4_reserve_inode_write(handle, inode, &iloc);
68         if (err)
69 -               goto out_unlock;
70 +               goto out;
72 +       mutex_lock(&sbi->s_orphan_lock);
73         /*
74          * Due to previous errors inode may be already a part of on-disk
75          * orphan list. If so skip on-disk list modification.
76          */
77 -       if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <=
78 -               (le32_to_cpu(sbi->s_es->s_inodes_count)))
79 -                       goto mem_insert;
81 -       /* Insert this inode at the head of the on-disk orphan list... */
82 -       NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
83 -       sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
84 -       err = ext4_handle_dirty_super(handle, sb);
85 -       rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
86 -       if (!err)
87 -               err = rc;
89 -       /* Only add to the head of the in-memory list if all the
90 -        * previous operations succeeded.  If the orphan_add is going to
91 -        * fail (possibly taking the journal offline), we can't risk
92 -        * leaving the inode on the orphan list: stray orphan-list
93 -        * entries can cause panics at unmount time.
94 -        *
95 -        * This is safe: on error we're going to ignore the orphan list
96 -        * anyway on the next recovery. */
97 -mem_insert:
98 -       if (!err)
99 -               list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
100 +       if (!NEXT_ORPHAN(inode) || NEXT_ORPHAN(inode) >
101 +           (le32_to_cpu(sbi->s_es->s_inodes_count))) {
102 +               /* Insert this inode at the head of the on-disk orphan list */
103 +               NEXT_ORPHAN(inode) = le32_to_cpu(sbi->s_es->s_last_orphan);
104 +               sbi->s_es->s_last_orphan = cpu_to_le32(inode->i_ino);
105 +               dirty = true;
106 +       }
107 +       list_add(&EXT4_I(inode)->i_orphan, &sbi->s_orphan);
108 +       mutex_unlock(&sbi->s_orphan_lock);
110 +       if (dirty) {
111 +               err = ext4_handle_dirty_super(handle, sb);
112 +               rc = ext4_mark_iloc_dirty(handle, inode, &iloc);
113 +               if (!err)
114 +                       err = rc;
115 +               if (err) {
116 +                       /*
117 +                        * We have to remove inode from in-memory list if
118 +                        * addition to on disk orphan list failed. Stray orphan
119 +                        * list entries can cause panics at unmount time.
120 +                        */
121 +                       mutex_lock(&sbi->s_orphan_lock);
122 +                       list_del(&EXT4_I(inode)->i_orphan);
123 +                       mutex_unlock(&sbi->s_orphan_lock);
124 +               }
125 +       }
126         jbd_debug(4, "superblock will point to %lu\n", inode->i_ino);
127         jbd_debug(4, "orphan inode %lu will point to %d\n",
128                         inode->i_ino, NEXT_ORPHAN(inode));
129 -out_unlock:
130 -       mutex_unlock(&sbi->s_orphan_lock);
131 +out:
132         ext4_std_error(sb, err);
133         return err;
135 @@ -2632,35 +2645,43 @@ int ext4_orphan_del(handle_t *handle, struct inode *inode)
136         if (!sbi->s_journal && !(sbi->s_mount_state & EXT4_ORPHAN_FS))
137                 return 0;
139 -       mutex_lock(&sbi->s_orphan_lock);
140 +       WARN_ON_ONCE(!(inode->i_state & (I_NEW | I_FREEING)) &&
141 +                    !mutex_is_locked(&inode->i_mutex));
142 +       /* Do this quick check before taking global s_orphan_lock. */
143         if (list_empty(&ei->i_orphan))
144 -               goto out;
145 +               return 0;
147 -       ino_next = NEXT_ORPHAN(inode);
148 -       prev = ei->i_orphan.prev;
149 +       if (handle) {
150 +               /* Grab inode buffer early before taking global s_orphan_lock */
151 +               err = ext4_reserve_inode_write(handle, inode, &iloc);
152 +       }
154 +       mutex_lock(&sbi->s_orphan_lock);
155         jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino);
157 +       prev = ei->i_orphan.prev;
158         list_del_init(&ei->i_orphan);
160         /* If we're on an error path, we may not have a valid
161          * transaction handle with which to update the orphan list on
162          * disk, but we still need to remove the inode from the linked
163          * list in memory. */
164 -       if (!handle)
165 -               goto out;
167 -       err = ext4_reserve_inode_write(handle, inode, &iloc);
168 -       if (err)
169 +       if (!handle || err) {
170 +               mutex_unlock(&sbi->s_orphan_lock);
171                 goto out_err;
172 +       }
174 +       ino_next = NEXT_ORPHAN(inode);
175         if (prev == &sbi->s_orphan) {
176                 jbd_debug(4, "superblock will point to %u\n", ino_next);
177                 BUFFER_TRACE(sbi->s_sbh, "get_write_access");
178                 err = ext4_journal_get_write_access(handle, sbi->s_sbh);
179 -               if (err)
180 +               if (err) {
181 +                       mutex_unlock(&sbi->s_orphan_lock);
182                         goto out_brelse;
183 +               }
184                 sbi->s_es->s_last_orphan = cpu_to_le32(ino_next);
185 +               mutex_unlock(&sbi->s_orphan_lock);
186                 err = ext4_handle_dirty_super(handle, inode->i_sb);
187         } else {
188                 struct ext4_iloc iloc2;
189 @@ -2670,20 +2691,20 @@ int ext4_orphan_del(handle_t *handle, struct inode *inode)
190                 jbd_debug(4, "orphan inode %lu will point to %u\n",
191                           i_prev->i_ino, ino_next);
192                 err = ext4_reserve_inode_write(handle, i_prev, &iloc2);
193 -               if (err)
194 +               if (err) {
195 +                       mutex_unlock(&sbi->s_orphan_lock);
196                         goto out_brelse;
197 +               }
198                 NEXT_ORPHAN(i_prev) = ino_next;
199                 err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2);
200 +               mutex_unlock(&sbi->s_orphan_lock);
201         }
202         if (err)
203                 goto out_brelse;
204         NEXT_ORPHAN(inode) = 0;
205         err = ext4_mark_iloc_dirty(handle, inode, &iloc);
207  out_err:
208         ext4_std_error(inode->i_sb, err);
209 -out:
210 -       mutex_unlock(&sbi->s_orphan_lock);
211         return err;
213  out_brelse: