@@ -91,24 +91,64 @@ static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
9191}
9292
9393/*
94- * Calculate next entry in top down tree traversal.
95- * From next_mnt in namespace.c - elegant.
94+ * Calculate and dget next entry in top down tree traversal.
9695 */
97- static struct dentry * next_dentry (struct dentry * p , struct dentry * root )
96+ static struct dentry * get_next_positive_dentry (struct dentry * prev ,
97+ struct dentry * root )
9898{
99- struct list_head * next = p -> d_subdirs .next ;
99+ struct list_head * next ;
100+ struct dentry * p , * ret ;
101+
102+ if (prev == NULL )
103+ return dget (prev );
100104
105+ spin_lock (& dcache_lock );
106+ relock :
107+ p = prev ;
108+ spin_lock (& p -> d_lock );
109+ again :
110+ next = p -> d_subdirs .next ;
101111 if (next == & p -> d_subdirs ) {
102112 while (1 ) {
103- if (p == root )
113+ struct dentry * parent ;
114+
115+ if (p == root ) {
116+ spin_unlock (& p -> d_lock );
117+ spin_unlock (& dcache_lock );
118+ dput (prev );
104119 return NULL ;
120+ }
121+
122+ parent = p -> d_parent ;
123+ if (!spin_trylock (& parent -> d_lock )) {
124+ spin_unlock (& p -> d_lock );
125+ cpu_relax ();
126+ goto relock ;
127+ }
128+ spin_unlock (& p -> d_lock );
105129 next = p -> d_u .d_child .next ;
106- if (next != & p -> d_parent -> d_subdirs )
130+ p = parent ;
131+ if (next != & parent -> d_subdirs )
107132 break ;
108- p = p -> d_parent ;
109133 }
110134 }
111- return list_entry (next , struct dentry , d_u .d_child );
135+ ret = list_entry (next , struct dentry , d_u .d_child );
136+
137+ spin_lock_nested (& ret -> d_lock , DENTRY_D_LOCK_NESTED );
138+ /* Negative dentry - try next */
139+ if (!simple_positive (ret )) {
140+ spin_unlock (& ret -> d_lock );
141+ p = ret ;
142+ goto again ;
143+ }
144+ dget_dlock (ret );
145+ spin_unlock (& ret -> d_lock );
146+ spin_unlock (& p -> d_lock );
147+ spin_unlock (& dcache_lock );
148+
149+ dput (prev );
150+
151+ return ret ;
112152}
113153
114154/*
@@ -158,22 +198,11 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
158198 if (!simple_positive (top ))
159199 return 1 ;
160200
161- spin_lock (& dcache_lock );
162- for (p = top ; p ; p = next_dentry (p , top )) {
163- spin_lock (& p -> d_lock );
164- /* Negative dentry - give up */
165- if (!simple_positive (p )) {
166- spin_unlock (& p -> d_lock );
167- continue ;
168- }
169-
201+ p = NULL ;
202+ while ((p = get_next_positive_dentry (p , top ))) {
170203 DPRINTK ("dentry %p %.*s" ,
171204 p , (int ) p -> d_name .len , p -> d_name .name );
172205
173- p = dget_dlock (p );
174- spin_unlock (& p -> d_lock );
175- spin_unlock (& dcache_lock );
176-
177206 /*
178207 * Is someone visiting anywhere in the subtree ?
179208 * If there's no mount we need to check the usage
@@ -208,10 +237,7 @@ static int autofs4_tree_busy(struct vfsmount *mnt,
208237 return 1 ;
209238 }
210239 }
211- dput (p );
212- spin_lock (& dcache_lock );
213240 }
214- spin_unlock (& dcache_lock );
215241
216242 /* Timeout of a tree mount is ultimately determined by its top dentry */
217243 if (!autofs4_can_expire (top , timeout , do_now ))
@@ -230,36 +256,21 @@ static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
230256 DPRINTK ("parent %p %.*s" ,
231257 parent , (int )parent -> d_name .len , parent -> d_name .name );
232258
233- spin_lock (& dcache_lock );
234- for (p = parent ; p ; p = next_dentry (p , parent )) {
235- spin_lock (& p -> d_lock );
236- /* Negative dentry - give up */
237- if (!simple_positive (p )) {
238- spin_unlock (& p -> d_lock );
239- continue ;
240- }
241-
259+ p = NULL ;
260+ while ((p = get_next_positive_dentry (p , parent ))) {
242261 DPRINTK ("dentry %p %.*s" ,
243262 p , (int ) p -> d_name .len , p -> d_name .name );
244263
245- p = dget_dlock (p );
246- spin_unlock (& p -> d_lock );
247- spin_unlock (& dcache_lock );
248-
249264 if (d_mountpoint (p )) {
250265 /* Can we umount this guy */
251266 if (autofs4_mount_busy (mnt , p ))
252- goto cont ;
267+ continue ;
253268
254269 /* Can we expire this guy */
255270 if (autofs4_can_expire (p , timeout , do_now ))
256271 return p ;
257272 }
258- cont :
259- dput (p );
260- spin_lock (& dcache_lock );
261273 }
262- spin_unlock (& dcache_lock );
263274 return NULL ;
264275}
265276
@@ -310,8 +321,8 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
310321{
311322 unsigned long timeout ;
312323 struct dentry * root = sb -> s_root ;
324+ struct dentry * dentry ;
313325 struct dentry * expired = NULL ;
314- struct list_head * next ;
315326 int do_now = how & AUTOFS_EXP_IMMEDIATE ;
316327 int exp_leaves = how & AUTOFS_EXP_LEAVES ;
317328 struct autofs_info * ino ;
@@ -323,26 +334,8 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
323334 now = jiffies ;
324335 timeout = sbi -> exp_timeout ;
325336
326- spin_lock (& dcache_lock );
327- next = root -> d_subdirs .next ;
328-
329- /* On exit from the loop expire is set to a dgot dentry
330- * to expire or it's NULL */
331- while ( next != & root -> d_subdirs ) {
332- struct dentry * dentry = list_entry (next , struct dentry , d_u .d_child );
333-
334- /* Negative dentry - give up */
335- spin_lock (& dentry -> d_lock );
336- if (!simple_positive (dentry )) {
337- next = next -> next ;
338- spin_unlock (& dentry -> d_lock );
339- continue ;
340- }
341-
342- dentry = dget_dlock (dentry );
343- spin_unlock (& dentry -> d_lock );
344- spin_unlock (& dcache_lock );
345-
337+ dentry = NULL ;
338+ while ((dentry = get_next_positive_dentry (dentry , root ))) {
346339 spin_lock (& sbi -> fs_lock );
347340 ino = autofs4_dentry_ino (dentry );
348341
@@ -405,11 +398,7 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
405398 }
406399next :
407400 spin_unlock (& sbi -> fs_lock );
408- dput (dentry );
409- spin_lock (& dcache_lock );
410- next = next -> next ;
411401 }
412- spin_unlock (& dcache_lock );
413402 return NULL ;
414403
415404found :
@@ -420,7 +409,11 @@ struct dentry *autofs4_expire_indirect(struct super_block *sb,
420409 init_completion (& ino -> expire_complete );
421410 spin_unlock (& sbi -> fs_lock );
422411 spin_lock (& dcache_lock );
412+ spin_lock (& expired -> d_parent -> d_lock );
413+ spin_lock_nested (& expired -> d_lock , DENTRY_D_LOCK_NESTED );
423414 list_move (& expired -> d_parent -> d_subdirs , & expired -> d_u .d_child );
415+ spin_unlock (& expired -> d_lock );
416+ spin_unlock (& expired -> d_parent -> d_lock );
424417 spin_unlock (& dcache_lock );
425418 return expired ;
426419}
0 commit comments