mm: introduce vmf_insert_pfn_prot()
[linux/fpc-iii.git] / fs / autofs / expire.c
blobd441244b79df98d23d6e0640466a2db68ffdac6b
1 /*
2 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
3 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
4 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
6 * This file is part of the Linux kernel and is made available under
7 * the terms of the GNU General Public License, version 2, or at your
8 * option, any later version, incorporated herein by reference.
9 */
11 #include "autofs_i.h"
13 /* Check if a dentry can be expired */
14 static inline int autofs_can_expire(struct dentry *dentry,
15 unsigned long timeout, unsigned int how)
17 struct autofs_info *ino = autofs_dentry_ino(dentry);
19 /* dentry in the process of being deleted */
20 if (ino == NULL)
21 return 0;
23 if (!(how & AUTOFS_EXP_IMMEDIATE)) {
24 /* Too young to die */
25 if (!timeout || time_after(ino->last_used + timeout, jiffies))
26 return 0;
28 return 1;
31 /* Check a mount point for busyness */
32 static int autofs_mount_busy(struct vfsmount *mnt,
33 struct dentry *dentry, unsigned int how)
35 struct dentry *top = dentry;
36 struct path path = {.mnt = mnt, .dentry = dentry};
37 int status = 1;
39 pr_debug("dentry %p %pd\n", dentry, dentry);
41 path_get(&path);
43 if (!follow_down_one(&path))
44 goto done;
46 if (is_autofs_dentry(path.dentry)) {
47 struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
49 /* This is an autofs submount, we can't expire it */
50 if (autofs_type_indirect(sbi->type))
51 goto done;
54 /* Not a submount, has a forced expire been requested */
55 if (how & AUTOFS_EXP_FORCED) {
56 status = 0;
57 goto done;
60 /* Update the expiry counter if fs is busy */
61 if (!may_umount_tree(path.mnt)) {
62 struct autofs_info *ino;
64 ino = autofs_dentry_ino(top);
65 ino->last_used = jiffies;
66 goto done;
69 status = 0;
70 done:
71 pr_debug("returning = %d\n", status);
72 path_put(&path);
73 return status;
77 * Calculate and dget next entry in the subdirs list under root.
79 static struct dentry *get_next_positive_subdir(struct dentry *prev,
80 struct dentry *root)
82 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
83 struct list_head *next;
84 struct dentry *q;
86 spin_lock(&sbi->lookup_lock);
87 spin_lock(&root->d_lock);
89 if (prev)
90 next = prev->d_child.next;
91 else {
92 prev = dget_dlock(root);
93 next = prev->d_subdirs.next;
96 cont:
97 if (next == &root->d_subdirs) {
98 spin_unlock(&root->d_lock);
99 spin_unlock(&sbi->lookup_lock);
100 dput(prev);
101 return NULL;
104 q = list_entry(next, struct dentry, d_child);
106 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
107 /* Already gone or negative dentry (under construction) - try next */
108 if (!d_count(q) || !simple_positive(q)) {
109 spin_unlock(&q->d_lock);
110 next = q->d_child.next;
111 goto cont;
113 dget_dlock(q);
114 spin_unlock(&q->d_lock);
115 spin_unlock(&root->d_lock);
116 spin_unlock(&sbi->lookup_lock);
118 dput(prev);
120 return q;
124 * Calculate and dget next entry in top down tree traversal.
126 static struct dentry *get_next_positive_dentry(struct dentry *prev,
127 struct dentry *root)
129 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
130 struct list_head *next;
131 struct dentry *p, *ret;
133 if (prev == NULL)
134 return dget(root);
136 spin_lock(&sbi->lookup_lock);
137 relock:
138 p = prev;
139 spin_lock(&p->d_lock);
140 again:
141 next = p->d_subdirs.next;
142 if (next == &p->d_subdirs) {
143 while (1) {
144 struct dentry *parent;
146 if (p == root) {
147 spin_unlock(&p->d_lock);
148 spin_unlock(&sbi->lookup_lock);
149 dput(prev);
150 return NULL;
153 parent = p->d_parent;
154 if (!spin_trylock(&parent->d_lock)) {
155 spin_unlock(&p->d_lock);
156 cpu_relax();
157 goto relock;
159 spin_unlock(&p->d_lock);
160 next = p->d_child.next;
161 p = parent;
162 if (next != &parent->d_subdirs)
163 break;
166 ret = list_entry(next, struct dentry, d_child);
168 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
169 /* Negative dentry - try next */
170 if (!simple_positive(ret)) {
171 spin_unlock(&p->d_lock);
172 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
173 p = ret;
174 goto again;
176 dget_dlock(ret);
177 spin_unlock(&ret->d_lock);
178 spin_unlock(&p->d_lock);
179 spin_unlock(&sbi->lookup_lock);
181 dput(prev);
183 return ret;
187 * Check a direct mount point for busyness.
188 * Direct mounts have similar expiry semantics to tree mounts.
189 * The tree is not busy iff no mountpoints are busy and there are no
190 * autofs submounts.
192 static int autofs_direct_busy(struct vfsmount *mnt,
193 struct dentry *top,
194 unsigned long timeout,
195 unsigned int how)
197 pr_debug("top %p %pd\n", top, top);
199 /* Forced expire, user space handles busy mounts */
200 if (how & AUTOFS_EXP_FORCED)
201 return 0;
203 /* If it's busy update the expiry counters */
204 if (!may_umount_tree(mnt)) {
205 struct autofs_info *ino;
207 ino = autofs_dentry_ino(top);
208 if (ino)
209 ino->last_used = jiffies;
210 return 1;
213 /* Timeout of a direct mount is determined by its top dentry */
214 if (!autofs_can_expire(top, timeout, how))
215 return 1;
217 return 0;
221 * Check a directory tree of mount points for busyness
222 * The tree is not busy iff no mountpoints are busy
224 static int autofs_tree_busy(struct vfsmount *mnt,
225 struct dentry *top,
226 unsigned long timeout,
227 unsigned int how)
229 struct autofs_info *top_ino = autofs_dentry_ino(top);
230 struct dentry *p;
232 pr_debug("top %p %pd\n", top, top);
234 /* Negative dentry - give up */
235 if (!simple_positive(top))
236 return 1;
238 p = NULL;
239 while ((p = get_next_positive_dentry(p, top))) {
240 pr_debug("dentry %p %pd\n", p, p);
243 * Is someone visiting anywhere in the subtree ?
244 * If there's no mount we need to check the usage
245 * count for the autofs dentry.
246 * If the fs is busy update the expiry counter.
248 if (d_mountpoint(p)) {
249 if (autofs_mount_busy(mnt, p, how)) {
250 top_ino->last_used = jiffies;
251 dput(p);
252 return 1;
254 } else {
255 struct autofs_info *ino = autofs_dentry_ino(p);
256 unsigned int ino_count = atomic_read(&ino->count);
258 /* allow for dget above and top is already dgot */
259 if (p == top)
260 ino_count += 2;
261 else
262 ino_count++;
264 if (d_count(p) > ino_count) {
265 top_ino->last_used = jiffies;
266 dput(p);
267 return 1;
272 /* Forced expire, user space handles busy mounts */
273 if (how & AUTOFS_EXP_FORCED)
274 return 0;
276 /* Timeout of a tree mount is ultimately determined by its top dentry */
277 if (!autofs_can_expire(top, timeout, how))
278 return 1;
280 return 0;
283 static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
284 struct dentry *parent,
285 unsigned long timeout,
286 unsigned int how)
288 struct dentry *p;
290 pr_debug("parent %p %pd\n", parent, parent);
292 p = NULL;
293 while ((p = get_next_positive_dentry(p, parent))) {
294 pr_debug("dentry %p %pd\n", p, p);
296 if (d_mountpoint(p)) {
297 /* Can we umount this guy */
298 if (autofs_mount_busy(mnt, p, how))
299 continue;
301 /* This isn't a submount so if a forced expire
302 * has been requested, user space handles busy
303 * mounts */
304 if (how & AUTOFS_EXP_FORCED)
305 return p;
307 /* Can we expire this guy */
308 if (autofs_can_expire(p, timeout, how))
309 return p;
312 return NULL;
315 /* Check if we can expire a direct mount (possibly a tree) */
316 static struct dentry *autofs_expire_direct(struct super_block *sb,
317 struct vfsmount *mnt,
318 struct autofs_sb_info *sbi,
319 unsigned int how)
321 struct dentry *root = dget(sb->s_root);
322 struct autofs_info *ino;
323 unsigned long timeout;
325 if (!root)
326 return NULL;
328 timeout = sbi->exp_timeout;
330 if (!autofs_direct_busy(mnt, root, timeout, how)) {
331 spin_lock(&sbi->fs_lock);
332 ino = autofs_dentry_ino(root);
333 /* No point expiring a pending mount */
334 if (ino->flags & AUTOFS_INF_PENDING) {
335 spin_unlock(&sbi->fs_lock);
336 goto out;
338 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
339 spin_unlock(&sbi->fs_lock);
340 synchronize_rcu();
341 if (!autofs_direct_busy(mnt, root, timeout, how)) {
342 spin_lock(&sbi->fs_lock);
343 ino->flags |= AUTOFS_INF_EXPIRING;
344 init_completion(&ino->expire_complete);
345 spin_unlock(&sbi->fs_lock);
346 return root;
348 spin_lock(&sbi->fs_lock);
349 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
350 spin_unlock(&sbi->fs_lock);
352 out:
353 dput(root);
355 return NULL;
358 /* Check if 'dentry' should expire, or return a nearby
359 * dentry that is suitable.
360 * If returned dentry is different from arg dentry,
361 * then a dget() reference was taken, else not.
363 static struct dentry *should_expire(struct dentry *dentry,
364 struct vfsmount *mnt,
365 unsigned long timeout,
366 unsigned int how)
368 struct autofs_info *ino = autofs_dentry_ino(dentry);
369 unsigned int ino_count;
371 /* No point expiring a pending mount */
372 if (ino->flags & AUTOFS_INF_PENDING)
373 return NULL;
376 * Case 1: (i) indirect mount or top level pseudo direct mount
377 * (autofs-4.1).
378 * (ii) indirect mount with offset mount, check the "/"
379 * offset (autofs-5.0+).
381 if (d_mountpoint(dentry)) {
382 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
384 /* Can we umount this guy */
385 if (autofs_mount_busy(mnt, dentry, how))
386 return NULL;
388 /* This isn't a submount so if a forced expire
389 * has been requested, user space handles busy
390 * mounts */
391 if (how & AUTOFS_EXP_FORCED)
392 return dentry;
394 /* Can we expire this guy */
395 if (autofs_can_expire(dentry, timeout, how))
396 return dentry;
397 return NULL;
400 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
401 pr_debug("checking symlink %p %pd\n", dentry, dentry);
403 /* Forced expire, user space handles busy mounts */
404 if (how & AUTOFS_EXP_FORCED)
405 return dentry;
408 * A symlink can't be "busy" in the usual sense so
409 * just check last used for expire timeout.
411 if (autofs_can_expire(dentry, timeout, how))
412 return dentry;
413 return NULL;
416 if (simple_empty(dentry))
417 return NULL;
419 /* Case 2: tree mount, expire iff entire tree is not busy */
420 if (!(how & AUTOFS_EXP_LEAVES)) {
421 /* Not a forced expire? */
422 if (!(how & AUTOFS_EXP_FORCED)) {
423 /* ref-walk currently on this dentry? */
424 ino_count = atomic_read(&ino->count) + 1;
425 if (d_count(dentry) > ino_count)
426 return NULL;
429 if (!autofs_tree_busy(mnt, dentry, timeout, how))
430 return dentry;
432 * Case 3: pseudo direct mount, expire individual leaves
433 * (autofs-4.1).
435 } else {
436 struct dentry *expired;
438 /* Not a forced expire? */
439 if (!(how & AUTOFS_EXP_FORCED)) {
440 /* ref-walk currently on this dentry? */
441 ino_count = atomic_read(&ino->count) + 1;
442 if (d_count(dentry) > ino_count)
443 return NULL;
446 expired = autofs_check_leaves(mnt, dentry, timeout, how);
447 if (expired) {
448 if (expired == dentry)
449 dput(dentry);
450 return expired;
453 return NULL;
457 * Find an eligible tree to time-out
458 * A tree is eligible if :-
459 * - it is unused by any user process
460 * - it has been unused for exp_timeout time
462 static struct dentry *autofs_expire_indirect(struct super_block *sb,
463 struct vfsmount *mnt,
464 struct autofs_sb_info *sbi,
465 unsigned int how)
467 unsigned long timeout;
468 struct dentry *root = sb->s_root;
469 struct dentry *dentry;
470 struct dentry *expired;
471 struct dentry *found;
472 struct autofs_info *ino;
474 if (!root)
475 return NULL;
477 timeout = sbi->exp_timeout;
479 dentry = NULL;
480 while ((dentry = get_next_positive_subdir(dentry, root))) {
481 spin_lock(&sbi->fs_lock);
482 ino = autofs_dentry_ino(dentry);
483 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
484 spin_unlock(&sbi->fs_lock);
485 continue;
487 spin_unlock(&sbi->fs_lock);
489 expired = should_expire(dentry, mnt, timeout, how);
490 if (!expired)
491 continue;
493 spin_lock(&sbi->fs_lock);
494 ino = autofs_dentry_ino(expired);
495 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
496 spin_unlock(&sbi->fs_lock);
497 synchronize_rcu();
499 /* Make sure a reference is not taken on found if
500 * things have changed.
502 how &= ~AUTOFS_EXP_LEAVES;
503 found = should_expire(expired, mnt, timeout, how);
504 if (!found || found != expired)
505 /* Something has changed, continue */
506 goto next;
508 if (expired != dentry)
509 dput(dentry);
511 spin_lock(&sbi->fs_lock);
512 goto found;
513 next:
514 spin_lock(&sbi->fs_lock);
515 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
516 spin_unlock(&sbi->fs_lock);
517 if (expired != dentry)
518 dput(expired);
520 return NULL;
522 found:
523 pr_debug("returning %p %pd\n", expired, expired);
524 ino->flags |= AUTOFS_INF_EXPIRING;
525 init_completion(&ino->expire_complete);
526 spin_unlock(&sbi->fs_lock);
527 return expired;
530 int autofs_expire_wait(const struct path *path, int rcu_walk)
532 struct dentry *dentry = path->dentry;
533 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
534 struct autofs_info *ino = autofs_dentry_ino(dentry);
535 int status;
536 int state;
538 /* Block on any pending expire */
539 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
540 return 0;
541 if (rcu_walk)
542 return -ECHILD;
544 retry:
545 spin_lock(&sbi->fs_lock);
546 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
547 if (state == AUTOFS_INF_WANT_EXPIRE) {
548 spin_unlock(&sbi->fs_lock);
550 * Possibly being selected for expire, wait until
551 * it's selected or not.
553 schedule_timeout_uninterruptible(HZ/10);
554 goto retry;
556 if (state & AUTOFS_INF_EXPIRING) {
557 spin_unlock(&sbi->fs_lock);
559 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
561 status = autofs_wait(sbi, path, NFY_NONE);
562 wait_for_completion(&ino->expire_complete);
564 pr_debug("expire done status=%d\n", status);
566 if (d_unhashed(dentry))
567 return -EAGAIN;
569 return status;
571 spin_unlock(&sbi->fs_lock);
573 return 0;
576 /* Perform an expiry operation */
577 int autofs_expire_run(struct super_block *sb,
578 struct vfsmount *mnt,
579 struct autofs_sb_info *sbi,
580 struct autofs_packet_expire __user *pkt_p)
582 struct autofs_packet_expire pkt;
583 struct autofs_info *ino;
584 struct dentry *dentry;
585 int ret = 0;
587 memset(&pkt, 0, sizeof(pkt));
589 pkt.hdr.proto_version = sbi->version;
590 pkt.hdr.type = autofs_ptype_expire;
592 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
593 if (!dentry)
594 return -EAGAIN;
596 pkt.len = dentry->d_name.len;
597 memcpy(pkt.name, dentry->d_name.name, pkt.len);
598 pkt.name[pkt.len] = '\0';
599 dput(dentry);
601 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
602 ret = -EFAULT;
604 spin_lock(&sbi->fs_lock);
605 ino = autofs_dentry_ino(dentry);
606 /* avoid rapid-fire expire attempts if expiry fails */
607 ino->last_used = jiffies;
608 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
609 complete_all(&ino->expire_complete);
610 spin_unlock(&sbi->fs_lock);
612 return ret;
615 int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
616 struct autofs_sb_info *sbi, unsigned int how)
618 struct dentry *dentry;
619 int ret = -EAGAIN;
621 if (autofs_type_trigger(sbi->type))
622 dentry = autofs_expire_direct(sb, mnt, sbi, how);
623 else
624 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
626 if (dentry) {
627 struct autofs_info *ino = autofs_dentry_ino(dentry);
628 const struct path path = { .mnt = mnt, .dentry = dentry };
630 /* This is synchronous because it makes the daemon a
631 * little easier
633 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
635 spin_lock(&sbi->fs_lock);
636 /* avoid rapid-fire expire attempts if expiry fails */
637 ino->last_used = jiffies;
638 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
639 complete_all(&ino->expire_complete);
640 spin_unlock(&sbi->fs_lock);
641 dput(dentry);
644 return ret;
648 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
649 * more to be done.
651 int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
652 struct autofs_sb_info *sbi, int __user *arg)
654 unsigned int how = 0;
656 if (arg && get_user(how, arg))
657 return -EFAULT;
659 return autofs_do_expire_multi(sb, mnt, sbi, how);