1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/expire.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
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.
13 * ------------------------------------------------------------------------- */
17 static unsigned long now;
19 /* Check if a dentry can be expired */
20 static inline int autofs4_can_expire(struct dentry *dentry,
21 unsigned long timeout, int do_now)
23 struct autofs_info *ino = autofs4_dentry_ino(dentry);
25 /* dentry in the process of being deleted */
29 /* No point expiring a pending mount */
30 if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
34 /* Too young to die */
35 if (time_after(ino->last_used + timeout, now))
38 /* update last_used here :-
39 - obviously makes sense if it is in use now
40 - less obviously, prevents rapid-fire expire
41 attempts if expire fails the first time */
47 /* Check a mount point for busyness */
48 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
52 DPRINTK("dentry %p %.*s",
53 dentry, (int)dentry->d_name.len, dentry->d_name.name);
58 if (!autofs4_follow_mount(&mnt, &dentry))
61 /* This is an autofs submount, we can't expire it */
62 if (is_autofs4_dentry(dentry))
65 /* The big question */
66 if (may_umount_tree(mnt) == 0)
69 DPRINTK("returning = %d", status);
76 * Calculate next entry in top down tree traversal.
77 * From next_mnt in namespace.c - elegant.
79 static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
81 struct list_head *next = p->d_subdirs.next;
83 if (next == &p->d_subdirs) {
87 next = p->d_u.d_child.next;
88 if (next != &p->d_parent->d_subdirs)
93 return list_entry(next, struct dentry, d_u.d_child);
96 /* Check a directory tree of mount points for busyness
97 * The tree is not busy iff no mountpoints are busy
99 static int autofs4_tree_busy(struct vfsmount *mnt,
101 unsigned long timeout,
104 struct autofs_info *ino;
107 DPRINTK("top %p %.*s",
108 top, (int)top->d_name.len, top->d_name.name);
110 /* Negative dentry - give up */
111 if (!simple_positive(top))
114 spin_lock(&dcache_lock);
115 for (p = top; p; p = next_dentry(p, top)) {
116 /* Negative dentry - give up */
117 if (!simple_positive(p))
120 DPRINTK("dentry %p %.*s",
121 p, (int) p->d_name.len, p->d_name.name);
124 spin_unlock(&dcache_lock);
127 * Is someone visiting anywhere in the subtree ?
128 * If there's no mount we need to check the usage
129 * count for the autofs dentry.
131 ino = autofs4_dentry_ino(p);
132 if (d_mountpoint(p)) {
133 if (autofs4_mount_busy(mnt, p)) {
138 unsigned int ino_count = atomic_read(&ino->count);
140 /* allow for dget above and top is already dgot */
146 if (atomic_read(&p->d_count) > ino_count) {
152 spin_lock(&dcache_lock);
154 spin_unlock(&dcache_lock);
156 /* Timeout of a tree mount is ultimately determined by its top dentry */
157 if (!autofs4_can_expire(top, timeout, do_now))
163 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
164 struct dentry *parent,
165 unsigned long timeout,
170 DPRINTK("parent %p %.*s",
171 parent, (int)parent->d_name.len, parent->d_name.name);
173 spin_lock(&dcache_lock);
174 for (p = parent; p; p = next_dentry(p, parent)) {
175 /* Negative dentry - give up */
176 if (!simple_positive(p))
179 DPRINTK("dentry %p %.*s",
180 p, (int) p->d_name.len, p->d_name.name);
183 spin_unlock(&dcache_lock);
185 if (d_mountpoint(p)) {
186 /* Can we expire this guy */
187 if (!autofs4_can_expire(p, timeout, do_now))
190 /* Can we umount this guy */
191 if (!autofs4_mount_busy(mnt, p))
197 spin_lock(&dcache_lock);
199 spin_unlock(&dcache_lock);
204 * Find an eligible tree to time-out
205 * A tree is eligible if :-
206 * - it is unused by any user process
207 * - it has been unused for exp_timeout time
209 static struct dentry *autofs4_expire(struct super_block *sb,
210 struct vfsmount *mnt,
211 struct autofs_sb_info *sbi,
214 unsigned long timeout;
215 struct dentry *root = sb->s_root;
216 struct dentry *expired = NULL;
217 struct list_head *next;
218 int do_now = how & AUTOFS_EXP_IMMEDIATE;
219 int exp_leaves = how & AUTOFS_EXP_LEAVES;
221 if ( !sbi->exp_timeout || !root )
225 timeout = sbi->exp_timeout;
227 spin_lock(&dcache_lock);
228 next = root->d_subdirs.next;
230 /* On exit from the loop expire is set to a dgot dentry
231 * to expire or it's NULL */
232 while ( next != &root->d_subdirs ) {
233 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
235 /* Negative dentry - give up */
236 if (!simple_positive(dentry)) {
241 dentry = dget(dentry);
242 spin_unlock(&dcache_lock);
244 /* Case 1: indirect mount or top level direct mount */
245 if (d_mountpoint(dentry)) {
246 DPRINTK("checking mountpoint %p %.*s",
247 dentry, (int)dentry->d_name.len, dentry->d_name.name);
249 /* Can we expire this guy */
250 if (!autofs4_can_expire(dentry, timeout, do_now))
253 /* Can we umount this guy */
254 if (!autofs4_mount_busy(mnt, dentry)) {
261 if (simple_empty(dentry))
264 /* Case 2: tree mount, expire iff entire tree is not busy */
266 /* Lock the tree as we must expire as a whole */
267 spin_lock(&sbi->fs_lock);
268 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
269 struct autofs_info *inf = autofs4_dentry_ino(dentry);
271 /* Set this flag early to catch sys_chdir and the like */
272 inf->flags |= AUTOFS_INF_EXPIRING;
273 spin_unlock(&sbi->fs_lock);
277 spin_unlock(&sbi->fs_lock);
278 /* Case 3: direct mount, expire individual leaves */
280 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
288 spin_lock(&dcache_lock);
293 DPRINTK("returning %p %.*s",
294 expired, (int)expired->d_name.len, expired->d_name.name);
295 spin_lock(&dcache_lock);
296 list_del(&expired->d_parent->d_subdirs);
297 list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
298 spin_unlock(&dcache_lock);
301 spin_unlock(&dcache_lock);
306 /* Perform an expiry operation */
307 int autofs4_expire_run(struct super_block *sb,
308 struct vfsmount *mnt,
309 struct autofs_sb_info *sbi,
310 struct autofs_packet_expire __user *pkt_p)
312 struct autofs_packet_expire pkt;
313 struct dentry *dentry;
315 memset(&pkt,0,sizeof pkt);
317 pkt.hdr.proto_version = sbi->version;
318 pkt.hdr.type = autofs_ptype_expire;
320 if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL)
323 pkt.len = dentry->d_name.len;
324 memcpy(pkt.name, dentry->d_name.name, pkt.len);
325 pkt.name[pkt.len] = '\0';
328 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
334 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
336 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
337 struct autofs_sb_info *sbi, int __user *arg)
339 struct dentry *dentry;
343 if (arg && get_user(do_now, arg))
346 if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) {
347 struct autofs_info *ino = autofs4_dentry_ino(dentry);
349 /* This is synchronous because it makes the daemon a
351 ino->flags |= AUTOFS_INF_EXPIRING;
352 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
353 ino->flags &= ~AUTOFS_INF_EXPIRING;