autofs4 - fix d_manage() return on rcu-walk
[deliverable/linux.git] / fs / autofs4 / expire.c
CommitLineData
1da177e4
LT
1/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/expire.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
3a15e2ab 7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
1da177e4
LT
8 *
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
12 *
13 * ------------------------------------------------------------------------- */
14
15#include "autofs_i.h"
16
17static unsigned long now;
18
1f5f2c30 19/* Check if a dentry can be expired */
1da177e4
LT
20static inline int autofs4_can_expire(struct dentry *dentry,
21 unsigned long timeout, int do_now)
22{
23 struct autofs_info *ino = autofs4_dentry_ino(dentry);
24
25 /* dentry in the process of being deleted */
26 if (ino == NULL)
27 return 0;
28
1da177e4
LT
29 if (!do_now) {
30 /* Too young to die */
c0ba7e51 31 if (!timeout || time_after(ino->last_used + timeout, now))
1da177e4
LT
32 return 0;
33
34 /* update last_used here :-
35 - obviously makes sense if it is in use now
36 - less obviously, prevents rapid-fire expire
37 attempts if expire fails the first time */
38 ino->last_used = now;
39 }
1da177e4
LT
40 return 1;
41}
42
1f5f2c30
IK
43/* Check a mount point for busyness */
44static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 45{
e0a7aae9 46 struct dentry *top = dentry;
9393bd07 47 struct path path = {.mnt = mnt, .dentry = dentry};
1f5f2c30 48 int status = 1;
1da177e4
LT
49
50 DPRINTK("dentry %p %.*s",
51 dentry, (int)dentry->d_name.len, dentry->d_name.name);
52
9393bd07 53 path_get(&path);
1da177e4 54
cc53ce53 55 if (!follow_down_one(&path))
1da177e4
LT
56 goto done;
57
9393bd07
AV
58 if (is_autofs4_dentry(path.dentry)) {
59 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
bc9c4068
IK
60
61 /* This is an autofs submount, we can't expire it */
a92daf6b 62 if (autofs_type_indirect(sbi->type))
bc9c4068
IK
63 goto done;
64
65 /*
66 * Otherwise it's an offset mount and we need to check
67 * if we can umount its mount, if there is one.
68 */
9393bd07 69 if (!d_mountpoint(path.dentry)) {
a8985f3a 70 status = 0;
bc9c4068 71 goto done;
a8985f3a 72 }
bc9c4068 73 }
1da177e4 74
e0a7aae9 75 /* Update the expiry counter if fs is busy */
37d0892c 76 if (!may_umount_tree(path.mnt)) {
e0a7aae9
IK
77 struct autofs_info *ino = autofs4_dentry_ino(top);
78 ino->last_used = jiffies;
79 goto done;
80 }
81
82 status = 0;
1da177e4
LT
83done:
84 DPRINTK("returning = %d", status);
9393bd07 85 path_put(&path);
1da177e4
LT
86 return status;
87}
88
d4a85e35
IK
89/*
90 * Calculate and dget next entry in the subdirs list under root.
91 */
92static struct dentry *get_next_positive_subdir(struct dentry *prev,
93 struct dentry *root)
94{
95 struct list_head *next;
96 struct dentry *p, *q;
97
98 spin_lock(&autofs4_lock);
99
100 if (prev == NULL) {
101 spin_lock(&root->d_lock);
102 prev = dget_dlock(root);
103 next = prev->d_subdirs.next;
104 p = prev;
105 goto start;
106 }
107
108 p = prev;
109 spin_lock(&p->d_lock);
110again:
111 next = p->d_u.d_child.next;
112start:
113 if (next == &root->d_subdirs) {
114 spin_unlock(&p->d_lock);
115 spin_unlock(&autofs4_lock);
116 dput(prev);
117 return NULL;
118 }
119
120 q = list_entry(next, struct dentry, d_u.d_child);
121
122 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
123 /* Negative dentry - try next */
124 if (!simple_positive(q)) {
125 spin_unlock(&p->d_lock);
126 p = q;
127 goto again;
128 }
129 dget_dlock(q);
130 spin_unlock(&q->d_lock);
131 spin_unlock(&p->d_lock);
132 spin_unlock(&autofs4_lock);
133
134 dput(prev);
135
136 return q;
137}
138
1ce12bad 139/*
2fd6b7f5 140 * Calculate and dget next entry in top down tree traversal.
1ce12bad 141 */
2fd6b7f5
NP
142static struct dentry *get_next_positive_dentry(struct dentry *prev,
143 struct dentry *root)
1ce12bad 144{
2fd6b7f5
NP
145 struct list_head *next;
146 struct dentry *p, *ret;
147
148 if (prev == NULL)
c14cc63a 149 return dget(root);
1ce12bad 150
b5c84bf6 151 spin_lock(&autofs4_lock);
2fd6b7f5
NP
152relock:
153 p = prev;
154 spin_lock(&p->d_lock);
155again:
156 next = p->d_subdirs.next;
1ce12bad
IK
157 if (next == &p->d_subdirs) {
158 while (1) {
2fd6b7f5
NP
159 struct dentry *parent;
160
161 if (p == root) {
162 spin_unlock(&p->d_lock);
b5c84bf6 163 spin_unlock(&autofs4_lock);
2fd6b7f5 164 dput(prev);
1ce12bad 165 return NULL;
2fd6b7f5
NP
166 }
167
168 parent = p->d_parent;
169 if (!spin_trylock(&parent->d_lock)) {
170 spin_unlock(&p->d_lock);
171 cpu_relax();
172 goto relock;
173 }
174 spin_unlock(&p->d_lock);
1ce12bad 175 next = p->d_u.d_child.next;
2fd6b7f5
NP
176 p = parent;
177 if (next != &parent->d_subdirs)
1ce12bad 178 break;
1ce12bad
IK
179 }
180 }
2fd6b7f5
NP
181 ret = list_entry(next, struct dentry, d_u.d_child);
182
183 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
184 /* Negative dentry - try next */
185 if (!simple_positive(ret)) {
c14cc63a 186 spin_unlock(&p->d_lock);
2fd6b7f5
NP
187 p = ret;
188 goto again;
189 }
190 dget_dlock(ret);
191 spin_unlock(&ret->d_lock);
192 spin_unlock(&p->d_lock);
b5c84bf6 193 spin_unlock(&autofs4_lock);
2fd6b7f5
NP
194
195 dput(prev);
196
197 return ret;
1ce12bad
IK
198}
199
3a15e2ab
IK
200/*
201 * Check a direct mount point for busyness.
202 * Direct mounts have similar expiry semantics to tree mounts.
203 * The tree is not busy iff no mountpoints are busy and there are no
204 * autofs submounts.
205 */
206static int autofs4_direct_busy(struct vfsmount *mnt,
207 struct dentry *top,
208 unsigned long timeout,
209 int do_now)
210{
211 DPRINTK("top %p %.*s",
212 top, (int) top->d_name.len, top->d_name.name);
213
3a15e2ab
IK
214 /* If it's busy update the expiry counters */
215 if (!may_umount_tree(mnt)) {
216 struct autofs_info *ino = autofs4_dentry_ino(top);
217 if (ino)
218 ino->last_used = jiffies;
219 return 1;
220 }
221
222 /* Timeout of a direct mount is determined by its top dentry */
223 if (!autofs4_can_expire(top, timeout, do_now))
224 return 1;
225
226 return 0;
227}
228
1da177e4
LT
229/* Check a directory tree of mount points for busyness
230 * The tree is not busy iff no mountpoints are busy
1da177e4 231 */
1f5f2c30
IK
232static int autofs4_tree_busy(struct vfsmount *mnt,
233 struct dentry *top,
234 unsigned long timeout,
235 int do_now)
1da177e4 236{
e0a7aae9 237 struct autofs_info *top_ino = autofs4_dentry_ino(top);
1ce12bad 238 struct dentry *p;
1da177e4 239
1f5f2c30 240 DPRINTK("top %p %.*s",
1da177e4
LT
241 top, (int)top->d_name.len, top->d_name.name);
242
243 /* Negative dentry - give up */
244 if (!simple_positive(top))
1f5f2c30 245 return 1;
1da177e4 246
2fd6b7f5
NP
247 p = NULL;
248 while ((p = get_next_positive_dentry(p, top))) {
1da177e4 249 DPRINTK("dentry %p %.*s",
1ce12bad 250 p, (int) p->d_name.len, p->d_name.name);
1da177e4 251
1aff3c8b
IK
252 /*
253 * Is someone visiting anywhere in the subtree ?
254 * If there's no mount we need to check the usage
255 * count for the autofs dentry.
e0a7aae9 256 * If the fs is busy update the expiry counter.
1aff3c8b 257 */
1ce12bad 258 if (d_mountpoint(p)) {
1ce12bad 259 if (autofs4_mount_busy(mnt, p)) {
e0a7aae9 260 top_ino->last_used = jiffies;
1ce12bad 261 dput(p);
1f5f2c30 262 return 1;
1da177e4 263 }
1aff3c8b 264 } else {
e0a7aae9 265 struct autofs_info *ino = autofs4_dentry_ino(p);
1aff3c8b
IK
266 unsigned int ino_count = atomic_read(&ino->count);
267
f9022f66
IK
268 /*
269 * Clean stale dentries below that have not been
270 * invalidated after a mount fail during lookup
271 */
272 d_invalidate(p);
273
1aff3c8b
IK
274 /* allow for dget above and top is already dgot */
275 if (p == top)
276 ino_count += 2;
277 else
278 ino_count++;
279
b7ab39f6 280 if (p->d_count > ino_count) {
e0a7aae9 281 top_ino->last_used = jiffies;
1aff3c8b
IK
282 dput(p);
283 return 1;
284 }
1da177e4 285 }
1da177e4 286 }
1aff3c8b
IK
287
288 /* Timeout of a tree mount is ultimately determined by its top dentry */
289 if (!autofs4_can_expire(top, timeout, do_now))
290 return 1;
291
1f5f2c30 292 return 0;
1da177e4
LT
293}
294
295static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
296 struct dentry *parent,
297 unsigned long timeout,
298 int do_now)
299{
1ce12bad 300 struct dentry *p;
1da177e4
LT
301
302 DPRINTK("parent %p %.*s",
303 parent, (int)parent->d_name.len, parent->d_name.name);
304
2fd6b7f5
NP
305 p = NULL;
306 while ((p = get_next_positive_dentry(p, parent))) {
1da177e4 307 DPRINTK("dentry %p %.*s",
1ce12bad 308 p, (int) p->d_name.len, p->d_name.name);
1da177e4 309
1ce12bad 310 if (d_mountpoint(p)) {
e0a7aae9
IK
311 /* Can we umount this guy */
312 if (autofs4_mount_busy(mnt, p))
2fd6b7f5 313 continue;
1da177e4 314
e0a7aae9
IK
315 /* Can we expire this guy */
316 if (autofs4_can_expire(p, timeout, do_now))
1ce12bad 317 return p;
1da177e4 318 }
1da177e4 319 }
1da177e4
LT
320 return NULL;
321}
322
3a15e2ab 323/* Check if we can expire a direct mount (possibly a tree) */
8d7b48e0
IK
324struct dentry *autofs4_expire_direct(struct super_block *sb,
325 struct vfsmount *mnt,
326 struct autofs_sb_info *sbi,
327 int how)
3a15e2ab
IK
328{
329 unsigned long timeout;
330 struct dentry *root = dget(sb->s_root);
331 int do_now = how & AUTOFS_EXP_IMMEDIATE;
b5b80177 332 struct autofs_info *ino;
3a15e2ab 333
c0ba7e51 334 if (!root)
3a15e2ab
IK
335 return NULL;
336
337 now = jiffies;
338 timeout = sbi->exp_timeout;
339
3a15e2ab 340 spin_lock(&sbi->fs_lock);
b5b80177
IK
341 ino = autofs4_dentry_ino(root);
342 /* No point expiring a pending mount */
f9398c23
IK
343 if (ino->flags & AUTOFS_INF_PENDING)
344 goto out;
3a15e2ab
IK
345 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
346 struct autofs_info *ino = autofs4_dentry_ino(root);
3a15e2ab 347 ino->flags |= AUTOFS_INF_EXPIRING;
6e60a9ab 348 init_completion(&ino->expire_complete);
3a15e2ab
IK
349 spin_unlock(&sbi->fs_lock);
350 return root;
351 }
f9398c23 352out:
3a15e2ab
IK
353 spin_unlock(&sbi->fs_lock);
354 dput(root);
355
356 return NULL;
357}
358
1da177e4
LT
359/*
360 * Find an eligible tree to time-out
361 * A tree is eligible if :-
362 * - it is unused by any user process
363 * - it has been unused for exp_timeout time
364 */
8d7b48e0
IK
365struct dentry *autofs4_expire_indirect(struct super_block *sb,
366 struct vfsmount *mnt,
367 struct autofs_sb_info *sbi,
368 int how)
1da177e4
LT
369{
370 unsigned long timeout;
371 struct dentry *root = sb->s_root;
2fd6b7f5 372 struct dentry *dentry;
1da177e4 373 struct dentry *expired = NULL;
1da177e4
LT
374 int do_now = how & AUTOFS_EXP_IMMEDIATE;
375 int exp_leaves = how & AUTOFS_EXP_LEAVES;
97e7449a
IK
376 struct autofs_info *ino;
377 unsigned int ino_count;
1da177e4 378
c0ba7e51 379 if (!root)
1da177e4
LT
380 return NULL;
381
382 now = jiffies;
383 timeout = sbi->exp_timeout;
384
2fd6b7f5 385 dentry = NULL;
d4a85e35 386 while ((dentry = get_next_positive_subdir(dentry, root))) {
97e7449a
IK
387 spin_lock(&sbi->fs_lock);
388 ino = autofs4_dentry_ino(dentry);
b5b80177
IK
389 /* No point expiring a pending mount */
390 if (ino->flags & AUTOFS_INF_PENDING)
3c319985 391 goto next;
97e7449a 392
3a15e2ab
IK
393 /*
394 * Case 1: (i) indirect mount or top level pseudo direct mount
395 * (autofs-4.1).
396 * (ii) indirect mount with offset mount, check the "/"
397 * offset (autofs-5.0+).
398 */
1da177e4
LT
399 if (d_mountpoint(dentry)) {
400 DPRINTK("checking mountpoint %p %.*s",
401 dentry, (int)dentry->d_name.len, dentry->d_name.name);
402
97e7449a
IK
403 /* Path walk currently on this dentry? */
404 ino_count = atomic_read(&ino->count) + 2;
b7ab39f6 405 if (dentry->d_count > ino_count)
97e7449a
IK
406 goto next;
407
e0a7aae9
IK
408 /* Can we umount this guy */
409 if (autofs4_mount_busy(mnt, dentry))
1da177e4
LT
410 goto next;
411
e0a7aae9
IK
412 /* Can we expire this guy */
413 if (autofs4_can_expire(dentry, timeout, do_now)) {
1da177e4 414 expired = dentry;
afec570c 415 goto found;
1da177e4
LT
416 }
417 goto next;
418 }
419
1f5f2c30 420 if (simple_empty(dentry))
1da177e4
LT
421 goto next;
422
423 /* Case 2: tree mount, expire iff entire tree is not busy */
424 if (!exp_leaves) {
97e7449a
IK
425 /* Path walk currently on this dentry? */
426 ino_count = atomic_read(&ino->count) + 1;
b7ab39f6 427 if (dentry->d_count > ino_count)
97e7449a 428 goto next;
3a9720ce 429
97e7449a 430 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
3a9720ce 431 expired = dentry;
afec570c 432 goto found;
1da177e4 433 }
3a15e2ab
IK
434 /*
435 * Case 3: pseudo direct mount, expire individual leaves
436 * (autofs-4.1).
437 */
1da177e4 438 } else {
97e7449a
IK
439 /* Path walk currently on this dentry? */
440 ino_count = atomic_read(&ino->count) + 1;
b7ab39f6 441 if (dentry->d_count > ino_count)
97e7449a
IK
442 goto next;
443
1da177e4
LT
444 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
445 if (expired) {
446 dput(dentry);
afec570c 447 goto found;
1da177e4
LT
448 }
449 }
450next:
97e7449a 451 spin_unlock(&sbi->fs_lock);
1da177e4 452 }
1da177e4 453 return NULL;
afec570c
IK
454
455found:
456 DPRINTK("returning %p %.*s",
457 expired, (int)expired->d_name.len, expired->d_name.name);
97e7449a
IK
458 ino = autofs4_dentry_ino(expired);
459 ino->flags |= AUTOFS_INF_EXPIRING;
6e60a9ab 460 init_completion(&ino->expire_complete);
97e7449a 461 spin_unlock(&sbi->fs_lock);
b5c84bf6 462 spin_lock(&autofs4_lock);
2fd6b7f5
NP
463 spin_lock(&expired->d_parent->d_lock);
464 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
afec570c 465 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
2fd6b7f5
NP
466 spin_unlock(&expired->d_lock);
467 spin_unlock(&expired->d_parent->d_lock);
b5c84bf6 468 spin_unlock(&autofs4_lock);
afec570c 469 return expired;
1da177e4
LT
470}
471
06a35985
IK
472int autofs4_expire_wait(struct dentry *dentry)
473{
474 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
475 struct autofs_info *ino = autofs4_dentry_ino(dentry);
476 int status;
477
478 /* Block on any pending expire */
479 spin_lock(&sbi->fs_lock);
480 if (ino->flags & AUTOFS_INF_EXPIRING) {
481 spin_unlock(&sbi->fs_lock);
482
483 DPRINTK("waiting for expire %p name=%.*s",
484 dentry, dentry->d_name.len, dentry->d_name.name);
485
486 status = autofs4_wait(sbi, dentry, NFY_NONE);
487 wait_for_completion(&ino->expire_complete);
488
489 DPRINTK("expire done status=%d", status);
490
4b1ae27a 491 if (d_unhashed(dentry))
06a35985
IK
492 return -EAGAIN;
493
494 return status;
495 }
496 spin_unlock(&sbi->fs_lock);
497
498 return 0;
499}
500
1da177e4
LT
501/* Perform an expiry operation */
502int autofs4_expire_run(struct super_block *sb,
503 struct vfsmount *mnt,
504 struct autofs_sb_info *sbi,
505 struct autofs_packet_expire __user *pkt_p)
506{
507 struct autofs_packet_expire pkt;
97e7449a 508 struct autofs_info *ino;
1da177e4 509 struct dentry *dentry;
97e7449a 510 int ret = 0;
1da177e4
LT
511
512 memset(&pkt,0,sizeof pkt);
513
514 pkt.hdr.proto_version = sbi->version;
515 pkt.hdr.type = autofs_ptype_expire;
516
3a15e2ab 517 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
1da177e4
LT
518 return -EAGAIN;
519
520 pkt.len = dentry->d_name.len;
521 memcpy(pkt.name, dentry->d_name.name, pkt.len);
522 pkt.name[pkt.len] = '\0';
523 dput(dentry);
524
525 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
97e7449a 526 ret = -EFAULT;
1da177e4 527
97e7449a
IK
528 spin_lock(&sbi->fs_lock);
529 ino = autofs4_dentry_ino(dentry);
530 ino->flags &= ~AUTOFS_INF_EXPIRING;
6e60a9ab 531 complete_all(&ino->expire_complete);
97e7449a
IK
532 spin_unlock(&sbi->fs_lock);
533
534 return ret;
1da177e4
LT
535}
536
56fcef75
IK
537int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
538 struct autofs_sb_info *sbi, int when)
1da177e4
LT
539{
540 struct dentry *dentry;
541 int ret = -EAGAIN;
1da177e4 542
a92daf6b 543 if (autofs_type_trigger(sbi->type))
56fcef75 544 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
3a15e2ab 545 else
56fcef75 546 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
3a15e2ab
IK
547
548 if (dentry) {
1f5f2c30 549 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1da177e4
LT
550
551 /* This is synchronous because it makes the daemon a
552 little easier */
1da177e4 553 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
6e60a9ab 554
97e7449a 555 spin_lock(&sbi->fs_lock);
1f5f2c30 556 ino->flags &= ~AUTOFS_INF_EXPIRING;
b5b80177 557 spin_lock(&dentry->d_lock);
3c319985 558 if (!ret) {
b5b80177
IK
559 if ((IS_ROOT(dentry) ||
560 (autofs_type_indirect(sbi->type) &&
561 IS_ROOT(dentry->d_parent))) &&
562 !(dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
563 __managed_dentry_set_automount(dentry);
564 }
565 spin_unlock(&dentry->d_lock);
6e60a9ab 566 complete_all(&ino->expire_complete);
97e7449a 567 spin_unlock(&sbi->fs_lock);
1da177e4
LT
568 dput(dentry);
569 }
1f5f2c30 570
1da177e4
LT
571 return ret;
572}
573
56fcef75
IK
574/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
575 more to be done */
576int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
577 struct autofs_sb_info *sbi, int __user *arg)
578{
579 int do_now = 0;
580
581 if (arg && get_user(do_now, arg))
582 return -EFAULT;
583
584 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
585}
586
This page took 0.567477 seconds and 5 git commands to generate.