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)
50 struct dentry *top = dentry;
53 DPRINTK("dentry %p %.*s",
54 dentry, (int)dentry->d_name.len, dentry->d_name.name);
59 if (!autofs4_follow_mount(&mnt, &dentry))
62 /* This is an autofs submount, we can't expire it */
63 if (is_autofs4_dentry(dentry))
66 /* Update the expiry counter if fs is busy */
67 if (may_umount_tree(mnt)) {
68 struct autofs_info *ino = autofs4_dentry_ino(top);
69 ino->last_used = jiffies;
75 DPRINTK("returning = %d", status);
82 * Calculate next entry in top down tree traversal.
83 * From next_mnt in namespace.c - elegant.
85 static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
87 struct list_head *next = p->d_subdirs.next;
89 if (next == &p->d_subdirs) {
93 next = p->d_u.d_child.next;
94 if (next != &p->d_parent->d_subdirs)
99 return list_entry(next, struct dentry, d_u.d_child);
102 /* Check a directory tree of mount points for busyness
103 * The tree is not busy iff no mountpoints are busy
105 static int autofs4_tree_busy(struct vfsmount *mnt,
107 unsigned long timeout,
110 struct autofs_info *top_ino = autofs4_dentry_ino(top);
113 DPRINTK("top %p %.*s",
114 top, (int)top->d_name.len, top->d_name.name);
116 /* Negative dentry - give up */
117 if (!simple_positive(top))
120 spin_lock(&dcache_lock);
121 for (p = top; p; p = next_dentry(p, top)) {
122 /* Negative dentry - give up */
123 if (!simple_positive(p))
126 DPRINTK("dentry %p %.*s",
127 p, (int) p->d_name.len, p->d_name.name);
130 spin_unlock(&dcache_lock);
133 * Is someone visiting anywhere in the subtree ?
134 * If there's no mount we need to check the usage
135 * count for the autofs dentry.
136 * If the fs is busy update the expiry counter.
138 if (d_mountpoint(p)) {
139 if (autofs4_mount_busy(mnt, p)) {
140 top_ino->last_used = jiffies;
145 struct autofs_info *ino = autofs4_dentry_ino(p);
146 unsigned int ino_count = atomic_read(&ino->count);
148 /* allow for dget above and top is already dgot */
154 if (atomic_read(&p->d_count) > ino_count) {
155 top_ino->last_used = jiffies;
161 spin_lock(&dcache_lock);
163 spin_unlock(&dcache_lock);
165 /* Timeout of a tree mount is ultimately determined by its top dentry */
166 if (!autofs4_can_expire(top, timeout, do_now))
172 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
173 struct dentry *parent,
174 unsigned long timeout,
179 DPRINTK("parent %p %.*s",
180 parent, (int)parent->d_name.len, parent->d_name.name);
182 spin_lock(&dcache_lock);
183 for (p = parent; p; p = next_dentry(p, parent)) {
184 /* Negative dentry - give up */
185 if (!simple_positive(p))
188 DPRINTK("dentry %p %.*s",
189 p, (int) p->d_name.len, p->d_name.name);
192 spin_unlock(&dcache_lock);
194 if (d_mountpoint(p)) {
195 /* Can we umount this guy */
196 if (autofs4_mount_busy(mnt, p))
199 /* Can we expire this guy */
200 if (autofs4_can_expire(p, timeout, do_now))
205 spin_lock(&dcache_lock);
207 spin_unlock(&dcache_lock);
212 * Find an eligible tree to time-out
213 * A tree is eligible if :-
214 * - it is unused by any user process
215 * - it has been unused for exp_timeout time
217 static struct dentry *autofs4_expire(struct super_block *sb,
218 struct vfsmount *mnt,
219 struct autofs_sb_info *sbi,
222 unsigned long timeout;
223 struct dentry *root = sb->s_root;
224 struct dentry *expired = NULL;
225 struct list_head *next;
226 int do_now = how & AUTOFS_EXP_IMMEDIATE;
227 int exp_leaves = how & AUTOFS_EXP_LEAVES;
229 if ( !sbi->exp_timeout || !root )
233 timeout = sbi->exp_timeout;
235 spin_lock(&dcache_lock);
236 next = root->d_subdirs.next;
238 /* On exit from the loop expire is set to a dgot dentry
239 * to expire or it's NULL */
240 while ( next != &root->d_subdirs ) {
241 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
243 /* Negative dentry - give up */
244 if (!simple_positive(dentry)) {
249 dentry = dget(dentry);
250 spin_unlock(&dcache_lock);
252 /* Case 1: indirect mount or top level direct mount */
253 if (d_mountpoint(dentry)) {
254 DPRINTK("checking mountpoint %p %.*s",
255 dentry, (int)dentry->d_name.len, dentry->d_name.name);
257 /* Can we umount this guy */
258 if (autofs4_mount_busy(mnt, dentry))
261 /* Can we expire this guy */
262 if (autofs4_can_expire(dentry, timeout, do_now)) {
269 if (simple_empty(dentry))
272 /* Case 2: tree mount, expire iff entire tree is not busy */
274 /* Lock the tree as we must expire as a whole */
275 spin_lock(&sbi->fs_lock);
276 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
277 struct autofs_info *inf = autofs4_dentry_ino(dentry);
279 /* Set this flag early to catch sys_chdir and the like */
280 inf->flags |= AUTOFS_INF_EXPIRING;
281 spin_unlock(&sbi->fs_lock);
285 spin_unlock(&sbi->fs_lock);
286 /* Case 3: direct mount, expire individual leaves */
288 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
296 spin_lock(&dcache_lock);
301 DPRINTK("returning %p %.*s",
302 expired, (int)expired->d_name.len, expired->d_name.name);
303 spin_lock(&dcache_lock);
304 list_del(&expired->d_parent->d_subdirs);
305 list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
306 spin_unlock(&dcache_lock);
309 spin_unlock(&dcache_lock);
314 /* Perform an expiry operation */
315 int autofs4_expire_run(struct super_block *sb,
316 struct vfsmount *mnt,
317 struct autofs_sb_info *sbi,
318 struct autofs_packet_expire __user *pkt_p)
320 struct autofs_packet_expire pkt;
321 struct dentry *dentry;
323 memset(&pkt,0,sizeof pkt);
325 pkt.hdr.proto_version = sbi->version;
326 pkt.hdr.type = autofs_ptype_expire;
328 if ((dentry = autofs4_expire(sb, mnt, sbi, 0)) == NULL)
331 pkt.len = dentry->d_name.len;
332 memcpy(pkt.name, dentry->d_name.name, pkt.len);
333 pkt.name[pkt.len] = '\0';
336 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
342 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
344 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
345 struct autofs_sb_info *sbi, int __user *arg)
347 struct dentry *dentry;
351 if (arg && get_user(do_now, arg))
354 if ((dentry = autofs4_expire(sb, mnt, sbi, do_now)) != NULL) {
355 struct autofs_info *ino = autofs4_dentry_ino(dentry);
357 /* This is synchronous because it makes the daemon a
359 ino->flags |= AUTOFS_INF_EXPIRING;
360 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
361 ino->flags &= ~AUTOFS_INF_EXPIRING;