blob: 08e33218a64ac0464e2a88f60577ac7c9f08735c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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>
Ian Kent3a15e2a2006-03-27 01:14:55 -08007 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
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
Ian Kent1f5f2c32006-03-27 01:14:44 -080019/* Check if a dentry can be expired */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static 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 /* No point expiring a pending mount */
30 if (dentry->d_flags & DCACHE_AUTOFS_PENDING)
31 return 0;
32
33 if (!do_now) {
34 /* Too young to die */
35 if (time_after(ino->last_used + timeout, now))
36 return 0;
37
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 */
42 ino->last_used = now;
43 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 return 1;
45}
46
Ian Kent1f5f2c32006-03-27 01:14:44 -080047/* Check a mount point for busyness */
48static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Ian Kente0a7aae2006-03-27 01:14:47 -080050 struct dentry *top = dentry;
Ian Kent1f5f2c32006-03-27 01:14:44 -080051 int status = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 DPRINTK("dentry %p %.*s",
54 dentry, (int)dentry->d_name.len, dentry->d_name.name);
55
56 mntget(mnt);
57 dget(dentry);
58
Ian Kent9b1e3af2005-06-21 17:16:38 -070059 if (!autofs4_follow_mount(&mnt, &dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 goto done;
61
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 /* This is an autofs submount, we can't expire it */
63 if (is_autofs4_dentry(dentry))
64 goto done;
65
Ian Kente0a7aae2006-03-27 01:14:47 -080066 /* Update the expiry counter if fs is busy */
Ian Kente3474a82006-03-27 01:14:51 -080067 if (!may_umount_tree(mnt)) {
Ian Kente0a7aae2006-03-27 01:14:47 -080068 struct autofs_info *ino = autofs4_dentry_ino(top);
69 ino->last_used = jiffies;
70 goto done;
71 }
72
73 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074done:
75 DPRINTK("returning = %d", status);
76 mntput(mnt);
77 dput(dentry);
78 return status;
79}
80
Ian Kent1ce12ba2006-03-27 01:14:45 -080081/*
82 * Calculate next entry in top down tree traversal.
83 * From next_mnt in namespace.c - elegant.
84 */
85static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
86{
87 struct list_head *next = p->d_subdirs.next;
88
89 if (next == &p->d_subdirs) {
90 while (1) {
91 if (p == root)
92 return NULL;
93 next = p->d_u.d_child.next;
94 if (next != &p->d_parent->d_subdirs)
95 break;
96 p = p->d_parent;
97 }
98 }
99 return list_entry(next, struct dentry, d_u.d_child);
100}
101
Ian Kent3a15e2a2006-03-27 01:14:55 -0800102/*
103 * Check a direct mount point for busyness.
104 * Direct mounts have similar expiry semantics to tree mounts.
105 * The tree is not busy iff no mountpoints are busy and there are no
106 * autofs submounts.
107 */
108static int autofs4_direct_busy(struct vfsmount *mnt,
109 struct dentry *top,
110 unsigned long timeout,
111 int do_now)
112{
113 DPRINTK("top %p %.*s",
114 top, (int) top->d_name.len, top->d_name.name);
115
116 /* Not a mountpoint - give up */
117 if (!d_mountpoint(top))
118 return 1;
119
120 /* If it's busy update the expiry counters */
121 if (!may_umount_tree(mnt)) {
122 struct autofs_info *ino = autofs4_dentry_ino(top);
123 if (ino)
124 ino->last_used = jiffies;
125 return 1;
126 }
127
128 /* Timeout of a direct mount is determined by its top dentry */
129 if (!autofs4_can_expire(top, timeout, do_now))
130 return 1;
131
132 return 0;
133}
134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/* Check a directory tree of mount points for busyness
136 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800138static int autofs4_tree_busy(struct vfsmount *mnt,
139 struct dentry *top,
140 unsigned long timeout,
141 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Ian Kente0a7aae2006-03-27 01:14:47 -0800143 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800144 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Ian Kent1f5f2c32006-03-27 01:14:44 -0800146 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 top, (int)top->d_name.len, top->d_name.name);
148
149 /* Negative dentry - give up */
150 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800151 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800154 for (p = top; p; p = next_dentry(p, top)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800156 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800160 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Ian Kent1ce12ba2006-03-27 01:14:45 -0800162 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 spin_unlock(&dcache_lock);
164
Ian Kent1aff3c82006-03-27 01:14:46 -0800165 /*
166 * Is someone visiting anywhere in the subtree ?
167 * If there's no mount we need to check the usage
168 * count for the autofs dentry.
Ian Kente0a7aae2006-03-27 01:14:47 -0800169 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800170 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800171 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800172 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800173 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800174 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800175 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800177 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800178 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800179 unsigned int ino_count = atomic_read(&ino->count);
180
181 /* allow for dget above and top is already dgot */
182 if (p == top)
183 ino_count += 2;
184 else
185 ino_count++;
186
187 if (atomic_read(&p->d_count) > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800188 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800189 dput(p);
190 return 1;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
Ian Kent1ce12ba2006-03-27 01:14:45 -0800193 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 }
196 spin_unlock(&dcache_lock);
Ian Kent1aff3c82006-03-27 01:14:46 -0800197
198 /* Timeout of a tree mount is ultimately determined by its top dentry */
199 if (!autofs4_can_expire(top, timeout, do_now))
200 return 1;
201
Ian Kent1f5f2c32006-03-27 01:14:44 -0800202 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
206 struct dentry *parent,
207 unsigned long timeout,
208 int do_now)
209{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800210 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 DPRINTK("parent %p %.*s",
213 parent, (int)parent->d_name.len, parent->d_name.name);
214
215 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800216 for (p = parent; p; p = next_dentry(p, parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800218 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
221 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800222 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Ian Kent1ce12ba2006-03-27 01:14:45 -0800224 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_unlock(&dcache_lock);
226
Ian Kent1ce12ba2006-03-27 01:14:45 -0800227 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800228 /* Can we umount this guy */
229 if (autofs4_mount_busy(mnt, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 goto cont;
231
Ian Kente0a7aae2006-03-27 01:14:47 -0800232 /* Can we expire this guy */
233 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800234 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236cont:
Ian Kent1ce12ba2006-03-27 01:14:45 -0800237 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 }
240 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return NULL;
242}
243
Ian Kent3a15e2a2006-03-27 01:14:55 -0800244/* Check if we can expire a direct mount (possibly a tree) */
245static struct dentry *autofs4_expire_direct(struct super_block *sb,
246 struct vfsmount *mnt,
247 struct autofs_sb_info *sbi,
248 int how)
249{
250 unsigned long timeout;
251 struct dentry *root = dget(sb->s_root);
252 int do_now = how & AUTOFS_EXP_IMMEDIATE;
253
254 if (!sbi->exp_timeout || !root)
255 return NULL;
256
257 now = jiffies;
258 timeout = sbi->exp_timeout;
259
260 /* Lock the tree as we must expire as a whole */
261 spin_lock(&sbi->fs_lock);
262 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
263 struct autofs_info *ino = autofs4_dentry_ino(root);
264
265 /* Set this flag early to catch sys_chdir and the like */
266 ino->flags |= AUTOFS_INF_EXPIRING;
267 spin_unlock(&sbi->fs_lock);
268 return root;
269 }
270 spin_unlock(&sbi->fs_lock);
271 dput(root);
272
273 return NULL;
274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276/*
277 * Find an eligible tree to time-out
278 * A tree is eligible if :-
279 * - it is unused by any user process
280 * - it has been unused for exp_timeout time
281 */
Ian Kent3a15e2a2006-03-27 01:14:55 -0800282static struct dentry *autofs4_expire_indirect(struct super_block *sb,
283 struct vfsmount *mnt,
284 struct autofs_sb_info *sbi,
285 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 unsigned long timeout;
288 struct dentry *root = sb->s_root;
289 struct dentry *expired = NULL;
290 struct list_head *next;
291 int do_now = how & AUTOFS_EXP_IMMEDIATE;
292 int exp_leaves = how & AUTOFS_EXP_LEAVES;
293
294 if ( !sbi->exp_timeout || !root )
295 return NULL;
296
297 now = jiffies;
298 timeout = sbi->exp_timeout;
299
300 spin_lock(&dcache_lock);
301 next = root->d_subdirs.next;
302
303 /* On exit from the loop expire is set to a dgot dentry
304 * to expire or it's NULL */
305 while ( next != &root->d_subdirs ) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800306 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 /* Negative dentry - give up */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800309 if (!simple_positive(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 next = next->next;
311 continue;
312 }
313
314 dentry = dget(dentry);
315 spin_unlock(&dcache_lock);
316
Ian Kent3a15e2a2006-03-27 01:14:55 -0800317 /*
318 * Case 1: (i) indirect mount or top level pseudo direct mount
319 * (autofs-4.1).
320 * (ii) indirect mount with offset mount, check the "/"
321 * offset (autofs-5.0+).
322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (d_mountpoint(dentry)) {
324 DPRINTK("checking mountpoint %p %.*s",
325 dentry, (int)dentry->d_name.len, dentry->d_name.name);
326
Ian Kente0a7aae2006-03-27 01:14:47 -0800327 /* Can we umount this guy */
328 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto next;
330
Ian Kente0a7aae2006-03-27 01:14:47 -0800331 /* Can we expire this guy */
332 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 expired = dentry;
334 break;
335 }
336 goto next;
337 }
338
Ian Kent1f5f2c32006-03-27 01:14:44 -0800339 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 goto next;
341
342 /* Case 2: tree mount, expire iff entire tree is not busy */
343 if (!exp_leaves) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700344 /* Lock the tree as we must expire as a whole */
345 spin_lock(&sbi->fs_lock);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800346 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700347 struct autofs_info *inf = autofs4_dentry_ino(dentry);
348
349 /* Set this flag early to catch sys_chdir and the like */
350 inf->flags |= AUTOFS_INF_EXPIRING;
351 spin_unlock(&sbi->fs_lock);
352 expired = dentry;
353 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 }
Ian Kent3a9720c2005-05-01 08:59:17 -0700355 spin_unlock(&sbi->fs_lock);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800356 /*
357 * Case 3: pseudo direct mount, expire individual leaves
358 * (autofs-4.1).
359 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 } else {
361 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
362 if (expired) {
363 dput(dentry);
364 break;
365 }
366 }
367next:
368 dput(dentry);
369 spin_lock(&dcache_lock);
370 next = next->next;
371 }
372
Ian Kent1f5f2c32006-03-27 01:14:44 -0800373 if (expired) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 DPRINTK("returning %p %.*s",
375 expired, (int)expired->d_name.len, expired->d_name.name);
376 spin_lock(&dcache_lock);
377 list_del(&expired->d_parent->d_subdirs);
Eric Dumazet5160ee62006-01-08 01:03:32 -0800378 list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock(&dcache_lock);
380 return expired;
381 }
382 spin_unlock(&dcache_lock);
383
384 return NULL;
385}
386
387/* Perform an expiry operation */
388int autofs4_expire_run(struct super_block *sb,
389 struct vfsmount *mnt,
390 struct autofs_sb_info *sbi,
391 struct autofs_packet_expire __user *pkt_p)
392{
393 struct autofs_packet_expire pkt;
394 struct dentry *dentry;
395
396 memset(&pkt,0,sizeof pkt);
397
398 pkt.hdr.proto_version = sbi->version;
399 pkt.hdr.type = autofs_ptype_expire;
400
Ian Kent3a15e2a2006-03-27 01:14:55 -0800401 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 return -EAGAIN;
403
404 pkt.len = dentry->d_name.len;
405 memcpy(pkt.name, dentry->d_name.name, pkt.len);
406 pkt.name[pkt.len] = '\0';
407 dput(dentry);
408
409 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
410 return -EFAULT;
411
412 return 0;
413}
414
415/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
416 more to be done */
417int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
418 struct autofs_sb_info *sbi, int __user *arg)
419{
420 struct dentry *dentry;
421 int ret = -EAGAIN;
422 int do_now = 0;
423
424 if (arg && get_user(do_now, arg))
425 return -EFAULT;
426
Ian Kent44d53eb2006-03-27 01:14:56 -0800427 if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800428 dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
429 else
430 dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
431
432 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800433 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /* This is synchronous because it makes the daemon a
436 little easier */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800437 ino->flags |= AUTOFS_INF_EXPIRING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800439 ino->flags &= ~AUTOFS_INF_EXPIRING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 dput(dentry);
441 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 return ret;
444}
445