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>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
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
17 static unsigned long now;
18
19 /* Check if a dentry can be expired */
autofs4_can_expire(struct dentry * dentry,unsigned long timeout,int do_now)20 static 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
29 if (!do_now) {
30 /* Too young to die */
31 if (!timeout || time_after(ino->last_used + timeout, now))
32 return 0;
33 }
34 return 1;
35 }
36
37 /* Check a mount point for busyness */
autofs4_mount_busy(struct vfsmount * mnt,struct dentry * dentry)38 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
39 {
40 struct dentry *top = dentry;
41 struct path path = {.mnt = mnt, .dentry = dentry};
42 int status = 1;
43
44 DPRINTK("dentry %p %.*s",
45 dentry, (int)dentry->d_name.len, dentry->d_name.name);
46
47 path_get(&path);
48
49 if (!follow_down_one(&path))
50 goto done;
51
52 if (is_autofs4_dentry(path.dentry)) {
53 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
54
55 /* This is an autofs submount, we can't expire it */
56 if (autofs_type_indirect(sbi->type))
57 goto done;
58 }
59
60 /* Update the expiry counter if fs is busy */
61 if (!may_umount_tree(path.mnt)) {
62 struct autofs_info *ino = autofs4_dentry_ino(top);
63 ino->last_used = jiffies;
64 goto done;
65 }
66
67 status = 0;
68 done:
69 DPRINTK("returning = %d", status);
70 path_put(&path);
71 return status;
72 }
73
74 /*
75 * Calculate and dget next entry in the subdirs list under root.
76 */
get_next_positive_subdir(struct dentry * prev,struct dentry * root)77 static struct dentry *get_next_positive_subdir(struct dentry *prev,
78 struct dentry *root)
79 {
80 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
81 struct list_head *next;
82 struct dentry *q;
83
84 spin_lock(&sbi->lookup_lock);
85 spin_lock(&root->d_lock);
86
87 if (prev)
88 next = prev->d_child.next;
89 else {
90 prev = dget_dlock(root);
91 next = prev->d_subdirs.next;
92 }
93
94 cont:
95 if (next == &root->d_subdirs) {
96 spin_unlock(&root->d_lock);
97 spin_unlock(&sbi->lookup_lock);
98 dput(prev);
99 return NULL;
100 }
101
102 q = list_entry(next, struct dentry, d_child);
103
104 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
105 /* Already gone or negative dentry (under construction) - try next */
106 if (!d_count(q) || !simple_positive(q)) {
107 spin_unlock(&q->d_lock);
108 next = q->d_child.next;
109 goto cont;
110 }
111 dget_dlock(q);
112 spin_unlock(&q->d_lock);
113 spin_unlock(&root->d_lock);
114 spin_unlock(&sbi->lookup_lock);
115
116 dput(prev);
117
118 return q;
119 }
120
121 /*
122 * Calculate and dget next entry in top down tree traversal.
123 */
get_next_positive_dentry(struct dentry * prev,struct dentry * root)124 static struct dentry *get_next_positive_dentry(struct dentry *prev,
125 struct dentry *root)
126 {
127 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
128 struct list_head *next;
129 struct dentry *p, *ret;
130
131 if (prev == NULL)
132 return dget(root);
133
134 spin_lock(&sbi->lookup_lock);
135 relock:
136 p = prev;
137 spin_lock(&p->d_lock);
138 again:
139 next = p->d_subdirs.next;
140 if (next == &p->d_subdirs) {
141 while (1) {
142 struct dentry *parent;
143
144 if (p == root) {
145 spin_unlock(&p->d_lock);
146 spin_unlock(&sbi->lookup_lock);
147 dput(prev);
148 return NULL;
149 }
150
151 parent = p->d_parent;
152 if (!spin_trylock(&parent->d_lock)) {
153 spin_unlock(&p->d_lock);
154 cpu_relax();
155 goto relock;
156 }
157 spin_unlock(&p->d_lock);
158 next = p->d_child.next;
159 p = parent;
160 if (next != &parent->d_subdirs)
161 break;
162 }
163 }
164 ret = list_entry(next, struct dentry, d_child);
165
166 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
167 /* Negative dentry - try next */
168 if (!simple_positive(ret)) {
169 spin_unlock(&p->d_lock);
170 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
171 p = ret;
172 goto again;
173 }
174 dget_dlock(ret);
175 spin_unlock(&ret->d_lock);
176 spin_unlock(&p->d_lock);
177 spin_unlock(&sbi->lookup_lock);
178
179 dput(prev);
180
181 return ret;
182 }
183
184 /*
185 * Check a direct mount point for busyness.
186 * Direct mounts have similar expiry semantics to tree mounts.
187 * The tree is not busy iff no mountpoints are busy and there are no
188 * autofs submounts.
189 */
autofs4_direct_busy(struct vfsmount * mnt,struct dentry * top,unsigned long timeout,int do_now)190 static int autofs4_direct_busy(struct vfsmount *mnt,
191 struct dentry *top,
192 unsigned long timeout,
193 int do_now)
194 {
195 DPRINTK("top %p %.*s",
196 top, (int) top->d_name.len, top->d_name.name);
197
198 /* If it's busy update the expiry counters */
199 if (!may_umount_tree(mnt)) {
200 struct autofs_info *ino = autofs4_dentry_ino(top);
201 if (ino)
202 ino->last_used = jiffies;
203 return 1;
204 }
205
206 /* Timeout of a direct mount is determined by its top dentry */
207 if (!autofs4_can_expire(top, timeout, do_now))
208 return 1;
209
210 return 0;
211 }
212
213 /* Check a directory tree of mount points for busyness
214 * The tree is not busy iff no mountpoints are busy
215 */
autofs4_tree_busy(struct vfsmount * mnt,struct dentry * top,unsigned long timeout,int do_now)216 static int autofs4_tree_busy(struct vfsmount *mnt,
217 struct dentry *top,
218 unsigned long timeout,
219 int do_now)
220 {
221 struct autofs_info *top_ino = autofs4_dentry_ino(top);
222 struct dentry *p;
223
224 DPRINTK("top %p %.*s",
225 top, (int)top->d_name.len, top->d_name.name);
226
227 /* Negative dentry - give up */
228 if (!simple_positive(top))
229 return 1;
230
231 p = NULL;
232 while ((p = get_next_positive_dentry(p, top))) {
233 DPRINTK("dentry %p %.*s",
234 p, (int) p->d_name.len, p->d_name.name);
235
236 /*
237 * Is someone visiting anywhere in the subtree ?
238 * If there's no mount we need to check the usage
239 * count for the autofs dentry.
240 * If the fs is busy update the expiry counter.
241 */
242 if (d_mountpoint(p)) {
243 if (autofs4_mount_busy(mnt, p)) {
244 top_ino->last_used = jiffies;
245 dput(p);
246 return 1;
247 }
248 } else {
249 struct autofs_info *ino = autofs4_dentry_ino(p);
250 unsigned int ino_count = atomic_read(&ino->count);
251
252 /* allow for dget above and top is already dgot */
253 if (p == top)
254 ino_count += 2;
255 else
256 ino_count++;
257
258 if (d_count(p) > ino_count) {
259 top_ino->last_used = jiffies;
260 dput(p);
261 return 1;
262 }
263 }
264 }
265
266 /* Timeout of a tree mount is ultimately determined by its top dentry */
267 if (!autofs4_can_expire(top, timeout, do_now))
268 return 1;
269
270 return 0;
271 }
272
autofs4_check_leaves(struct vfsmount * mnt,struct dentry * parent,unsigned long timeout,int do_now)273 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
274 struct dentry *parent,
275 unsigned long timeout,
276 int do_now)
277 {
278 struct dentry *p;
279
280 DPRINTK("parent %p %.*s",
281 parent, (int)parent->d_name.len, parent->d_name.name);
282
283 p = NULL;
284 while ((p = get_next_positive_dentry(p, parent))) {
285 DPRINTK("dentry %p %.*s",
286 p, (int) p->d_name.len, p->d_name.name);
287
288 if (d_mountpoint(p)) {
289 /* Can we umount this guy */
290 if (autofs4_mount_busy(mnt, p))
291 continue;
292
293 /* Can we expire this guy */
294 if (autofs4_can_expire(p, timeout, do_now))
295 return p;
296 }
297 }
298 return NULL;
299 }
300
301 /* Check if we can expire a direct mount (possibly a tree) */
autofs4_expire_direct(struct super_block * sb,struct vfsmount * mnt,struct autofs_sb_info * sbi,int how)302 struct dentry *autofs4_expire_direct(struct super_block *sb,
303 struct vfsmount *mnt,
304 struct autofs_sb_info *sbi,
305 int how)
306 {
307 unsigned long timeout;
308 struct dentry *root = dget(sb->s_root);
309 int do_now = how & AUTOFS_EXP_IMMEDIATE;
310 struct autofs_info *ino;
311
312 if (!root)
313 return NULL;
314
315 now = jiffies;
316 timeout = sbi->exp_timeout;
317
318 spin_lock(&sbi->fs_lock);
319 ino = autofs4_dentry_ino(root);
320 /* No point expiring a pending mount */
321 if (ino->flags & AUTOFS_INF_PENDING)
322 goto out;
323 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
324 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
325 spin_unlock(&sbi->fs_lock);
326 synchronize_rcu();
327 spin_lock(&sbi->fs_lock);
328 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
329 ino->flags |= AUTOFS_INF_EXPIRING;
330 init_completion(&ino->expire_complete);
331 spin_unlock(&sbi->fs_lock);
332 return root;
333 }
334 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
335 }
336 out:
337 spin_unlock(&sbi->fs_lock);
338 dput(root);
339
340 return NULL;
341 }
342
343 /* Check if 'dentry' should expire, or return a nearby
344 * dentry that is suitable.
345 * If returned dentry is different from arg dentry,
346 * then a dget() reference was taken, else not.
347 */
should_expire(struct dentry * dentry,struct vfsmount * mnt,unsigned long timeout,int how)348 static struct dentry *should_expire(struct dentry *dentry,
349 struct vfsmount *mnt,
350 unsigned long timeout,
351 int how)
352 {
353 int do_now = how & AUTOFS_EXP_IMMEDIATE;
354 int exp_leaves = how & AUTOFS_EXP_LEAVES;
355 struct autofs_info *ino = autofs4_dentry_ino(dentry);
356 unsigned int ino_count;
357
358 /* No point expiring a pending mount */
359 if (ino->flags & AUTOFS_INF_PENDING)
360 return NULL;
361
362 /*
363 * Case 1: (i) indirect mount or top level pseudo direct mount
364 * (autofs-4.1).
365 * (ii) indirect mount with offset mount, check the "/"
366 * offset (autofs-5.0+).
367 */
368 if (d_mountpoint(dentry)) {
369 DPRINTK("checking mountpoint %p %.*s",
370 dentry, (int)dentry->d_name.len, dentry->d_name.name);
371
372 /* Can we umount this guy */
373 if (autofs4_mount_busy(mnt, dentry))
374 return NULL;
375
376 /* Can we expire this guy */
377 if (autofs4_can_expire(dentry, timeout, do_now))
378 return dentry;
379 return NULL;
380 }
381
382 if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)) {
383 DPRINTK("checking symlink %p %.*s",
384 dentry, (int)dentry->d_name.len, dentry->d_name.name);
385 /*
386 * A symlink can't be "busy" in the usual sense so
387 * just check last used for expire timeout.
388 */
389 if (autofs4_can_expire(dentry, timeout, do_now))
390 return dentry;
391 return NULL;
392 }
393
394 if (simple_empty(dentry))
395 return NULL;
396
397 /* Case 2: tree mount, expire iff entire tree is not busy */
398 if (!exp_leaves) {
399 /* Path walk currently on this dentry? */
400 ino_count = atomic_read(&ino->count) + 1;
401 if (d_count(dentry) > ino_count)
402 return NULL;
403
404 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now))
405 return dentry;
406 /*
407 * Case 3: pseudo direct mount, expire individual leaves
408 * (autofs-4.1).
409 */
410 } else {
411 /* Path walk currently on this dentry? */
412 struct dentry *expired;
413 ino_count = atomic_read(&ino->count) + 1;
414 if (d_count(dentry) > ino_count)
415 return NULL;
416
417 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
418 if (expired) {
419 if (expired == dentry)
420 dput(dentry);
421 return expired;
422 }
423 }
424 return NULL;
425 }
426
427 /*
428 * Find an eligible tree to time-out
429 * A tree is eligible if :-
430 * - it is unused by any user process
431 * - it has been unused for exp_timeout time
432 */
autofs4_expire_indirect(struct super_block * sb,struct vfsmount * mnt,struct autofs_sb_info * sbi,int how)433 struct dentry *autofs4_expire_indirect(struct super_block *sb,
434 struct vfsmount *mnt,
435 struct autofs_sb_info *sbi,
436 int how)
437 {
438 unsigned long timeout;
439 struct dentry *root = sb->s_root;
440 struct dentry *dentry;
441 struct dentry *expired;
442 struct dentry *found;
443 struct autofs_info *ino;
444
445 if (!root)
446 return NULL;
447
448 now = jiffies;
449 timeout = sbi->exp_timeout;
450
451 dentry = NULL;
452 while ((dentry = get_next_positive_subdir(dentry, root))) {
453 int flags = how;
454
455 spin_lock(&sbi->fs_lock);
456 ino = autofs4_dentry_ino(dentry);
457 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
458 spin_unlock(&sbi->fs_lock);
459 continue;
460 }
461 spin_unlock(&sbi->fs_lock);
462
463 expired = should_expire(dentry, mnt, timeout, flags);
464 if (!expired)
465 continue;
466
467 spin_lock(&sbi->fs_lock);
468 ino = autofs4_dentry_ino(expired);
469 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
470 spin_unlock(&sbi->fs_lock);
471 synchronize_rcu();
472
473 /* Make sure a reference is not taken on found if
474 * things have changed.
475 */
476 flags &= ~AUTOFS_EXP_LEAVES;
477 found = should_expire(expired, mnt, timeout, how);
478 if (!found || found != expired)
479 /* Something has changed, continue */
480 goto next;
481
482 if (expired != dentry)
483 dput(dentry);
484
485 spin_lock(&sbi->fs_lock);
486 goto found;
487 next:
488 spin_lock(&sbi->fs_lock);
489 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
490 spin_unlock(&sbi->fs_lock);
491 if (expired != dentry)
492 dput(expired);
493 }
494 return NULL;
495
496 found:
497 DPRINTK("returning %p %.*s",
498 expired, (int)expired->d_name.len, expired->d_name.name);
499 ino->flags |= AUTOFS_INF_EXPIRING;
500 init_completion(&ino->expire_complete);
501 spin_unlock(&sbi->fs_lock);
502 return expired;
503 }
504
autofs4_expire_wait(struct dentry * dentry,int rcu_walk)505 int autofs4_expire_wait(struct dentry *dentry, int rcu_walk)
506 {
507 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
508 struct autofs_info *ino = autofs4_dentry_ino(dentry);
509 int status;
510 int state;
511
512 /* Block on any pending expire */
513 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
514 return 0;
515 if (rcu_walk)
516 return -ECHILD;
517
518 retry:
519 spin_lock(&sbi->fs_lock);
520 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
521 if (state == AUTOFS_INF_WANT_EXPIRE) {
522 spin_unlock(&sbi->fs_lock);
523 /*
524 * Possibly being selected for expire, wait until
525 * it's selected or not.
526 */
527 schedule_timeout_uninterruptible(HZ/10);
528 goto retry;
529 }
530 if (state & AUTOFS_INF_EXPIRING) {
531 spin_unlock(&sbi->fs_lock);
532
533 DPRINTK("waiting for expire %p name=%.*s",
534 dentry, dentry->d_name.len, dentry->d_name.name);
535
536 status = autofs4_wait(sbi, dentry, NFY_NONE);
537 wait_for_completion(&ino->expire_complete);
538
539 DPRINTK("expire done status=%d", status);
540
541 if (d_unhashed(dentry))
542 return -EAGAIN;
543
544 return status;
545 }
546 spin_unlock(&sbi->fs_lock);
547
548 return 0;
549 }
550
551 /* Perform an expiry operation */
autofs4_expire_run(struct super_block * sb,struct vfsmount * mnt,struct autofs_sb_info * sbi,struct autofs_packet_expire __user * pkt_p)552 int autofs4_expire_run(struct super_block *sb,
553 struct vfsmount *mnt,
554 struct autofs_sb_info *sbi,
555 struct autofs_packet_expire __user *pkt_p)
556 {
557 struct autofs_packet_expire pkt;
558 struct autofs_info *ino;
559 struct dentry *dentry;
560 int ret = 0;
561
562 memset(&pkt,0,sizeof pkt);
563
564 pkt.hdr.proto_version = sbi->version;
565 pkt.hdr.type = autofs_ptype_expire;
566
567 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
568 return -EAGAIN;
569
570 pkt.len = dentry->d_name.len;
571 memcpy(pkt.name, dentry->d_name.name, pkt.len);
572 pkt.name[pkt.len] = '\0';
573 dput(dentry);
574
575 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
576 ret = -EFAULT;
577
578 spin_lock(&sbi->fs_lock);
579 ino = autofs4_dentry_ino(dentry);
580 /* avoid rapid-fire expire attempts if expiry fails */
581 ino->last_used = now;
582 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
583 complete_all(&ino->expire_complete);
584 spin_unlock(&sbi->fs_lock);
585
586 return ret;
587 }
588
autofs4_do_expire_multi(struct super_block * sb,struct vfsmount * mnt,struct autofs_sb_info * sbi,int when)589 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
590 struct autofs_sb_info *sbi, int when)
591 {
592 struct dentry *dentry;
593 int ret = -EAGAIN;
594
595 if (autofs_type_trigger(sbi->type))
596 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
597 else
598 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
599
600 if (dentry) {
601 struct autofs_info *ino = autofs4_dentry_ino(dentry);
602
603 /* This is synchronous because it makes the daemon a
604 little easier */
605 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
606
607 spin_lock(&sbi->fs_lock);
608 /* avoid rapid-fire expire attempts if expiry fails */
609 ino->last_used = now;
610 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
611 complete_all(&ino->expire_complete);
612 spin_unlock(&sbi->fs_lock);
613 dput(dentry);
614 }
615
616 return ret;
617 }
618
619 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
620 more to be done */
autofs4_expire_multi(struct super_block * sb,struct vfsmount * mnt,struct autofs_sb_info * sbi,int __user * arg)621 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
622 struct autofs_sb_info *sbi, int __user *arg)
623 {
624 int do_now = 0;
625
626 if (arg && get_user(do_now, arg))
627 return -EFAULT;
628
629 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
630 }
631
632