blob: b8ce02607d66bacd03d60acad9b99ffd9e48d7d2 [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
Ian Kent3a15e2a2006-03-27 01:14:55 -0800116 /* If it's busy update the expiry counters */
117 if (!may_umount_tree(mnt)) {
118 struct autofs_info *ino = autofs4_dentry_ino(top);
119 if (ino)
120 ino->last_used = jiffies;
121 return 1;
122 }
123
124 /* Timeout of a direct mount is determined by its top dentry */
125 if (!autofs4_can_expire(top, timeout, do_now))
126 return 1;
127
128 return 0;
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/* Check a directory tree of mount points for busyness
132 * The tree is not busy iff no mountpoints are busy
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800134static int autofs4_tree_busy(struct vfsmount *mnt,
135 struct dentry *top,
136 unsigned long timeout,
137 int do_now)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Ian Kente0a7aae2006-03-27 01:14:47 -0800139 struct autofs_info *top_ino = autofs4_dentry_ino(top);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800140 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Ian Kent1f5f2c32006-03-27 01:14:44 -0800142 DPRINTK("top %p %.*s",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 top, (int)top->d_name.len, top->d_name.name);
144
145 /* Negative dentry - give up */
146 if (!simple_positive(top))
Ian Kent1f5f2c32006-03-27 01:14:44 -0800147 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800150 for (p = top; p; p = next_dentry(p, top)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800152 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800156 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157
Ian Kent1ce12ba2006-03-27 01:14:45 -0800158 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 spin_unlock(&dcache_lock);
160
Ian Kent1aff3c82006-03-27 01:14:46 -0800161 /*
162 * Is someone visiting anywhere in the subtree ?
163 * If there's no mount we need to check the usage
164 * count for the autofs dentry.
Ian Kente0a7aae2006-03-27 01:14:47 -0800165 * If the fs is busy update the expiry counter.
Ian Kent1aff3c82006-03-27 01:14:46 -0800166 */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800167 if (d_mountpoint(p)) {
Ian Kent1ce12ba2006-03-27 01:14:45 -0800168 if (autofs4_mount_busy(mnt, p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800169 top_ino->last_used = jiffies;
Ian Kent1ce12ba2006-03-27 01:14:45 -0800170 dput(p);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800171 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
Ian Kent1aff3c82006-03-27 01:14:46 -0800173 } else {
Ian Kente0a7aae2006-03-27 01:14:47 -0800174 struct autofs_info *ino = autofs4_dentry_ino(p);
Ian Kent1aff3c82006-03-27 01:14:46 -0800175 unsigned int ino_count = atomic_read(&ino->count);
176
177 /* allow for dget above and top is already dgot */
178 if (p == top)
179 ino_count += 2;
180 else
181 ino_count++;
182
183 if (atomic_read(&p->d_count) > ino_count) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800184 top_ino->last_used = jiffies;
Ian Kent1aff3c82006-03-27 01:14:46 -0800185 dput(p);
186 return 1;
187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
Ian Kent1ce12ba2006-03-27 01:14:45 -0800189 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192 spin_unlock(&dcache_lock);
Ian Kent1aff3c82006-03-27 01:14:46 -0800193
194 /* Timeout of a tree mount is ultimately determined by its top dentry */
195 if (!autofs4_can_expire(top, timeout, do_now))
196 return 1;
197
Ian Kent1f5f2c32006-03-27 01:14:44 -0800198 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
201static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
202 struct dentry *parent,
203 unsigned long timeout,
204 int do_now)
205{
Ian Kent1ce12ba2006-03-27 01:14:45 -0800206 struct dentry *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 DPRINTK("parent %p %.*s",
209 parent, (int)parent->d_name.len, parent->d_name.name);
210
211 spin_lock(&dcache_lock);
Ian Kent1ce12ba2006-03-27 01:14:45 -0800212 for (p = parent; p; p = next_dentry(p, parent)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Negative dentry - give up */
Ian Kent1ce12ba2006-03-27 01:14:45 -0800214 if (!simple_positive(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 DPRINTK("dentry %p %.*s",
Ian Kent1ce12ba2006-03-27 01:14:45 -0800218 p, (int) p->d_name.len, p->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Ian Kent1ce12ba2006-03-27 01:14:45 -0800220 p = dget(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 spin_unlock(&dcache_lock);
222
Ian Kent1ce12ba2006-03-27 01:14:45 -0800223 if (d_mountpoint(p)) {
Ian Kente0a7aae2006-03-27 01:14:47 -0800224 /* Can we umount this guy */
225 if (autofs4_mount_busy(mnt, p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 goto cont;
227
Ian Kente0a7aae2006-03-27 01:14:47 -0800228 /* Can we expire this guy */
229 if (autofs4_can_expire(p, timeout, do_now))
Ian Kent1ce12ba2006-03-27 01:14:45 -0800230 return p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232cont:
Ian Kent1ce12ba2006-03-27 01:14:45 -0800233 dput(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 spin_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return NULL;
238}
239
Ian Kent3a15e2a2006-03-27 01:14:55 -0800240/* Check if we can expire a direct mount (possibly a tree) */
241static struct dentry *autofs4_expire_direct(struct super_block *sb,
242 struct vfsmount *mnt,
243 struct autofs_sb_info *sbi,
244 int how)
245{
246 unsigned long timeout;
247 struct dentry *root = dget(sb->s_root);
248 int do_now = how & AUTOFS_EXP_IMMEDIATE;
249
250 if (!sbi->exp_timeout || !root)
251 return NULL;
252
253 now = jiffies;
254 timeout = sbi->exp_timeout;
255
256 /* Lock the tree as we must expire as a whole */
257 spin_lock(&sbi->fs_lock);
258 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
259 struct autofs_info *ino = autofs4_dentry_ino(root);
260
261 /* Set this flag early to catch sys_chdir and the like */
262 ino->flags |= AUTOFS_INF_EXPIRING;
263 spin_unlock(&sbi->fs_lock);
264 return root;
265 }
266 spin_unlock(&sbi->fs_lock);
267 dput(root);
268
269 return NULL;
270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*
273 * Find an eligible tree to time-out
274 * A tree is eligible if :-
275 * - it is unused by any user process
276 * - it has been unused for exp_timeout time
277 */
Ian Kent3a15e2a2006-03-27 01:14:55 -0800278static struct dentry *autofs4_expire_indirect(struct super_block *sb,
279 struct vfsmount *mnt,
280 struct autofs_sb_info *sbi,
281 int how)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 unsigned long timeout;
284 struct dentry *root = sb->s_root;
285 struct dentry *expired = NULL;
286 struct list_head *next;
287 int do_now = how & AUTOFS_EXP_IMMEDIATE;
288 int exp_leaves = how & AUTOFS_EXP_LEAVES;
289
290 if ( !sbi->exp_timeout || !root )
291 return NULL;
292
293 now = jiffies;
294 timeout = sbi->exp_timeout;
295
296 spin_lock(&dcache_lock);
297 next = root->d_subdirs.next;
298
299 /* On exit from the loop expire is set to a dgot dentry
300 * to expire or it's NULL */
301 while ( next != &root->d_subdirs ) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800302 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 /* Negative dentry - give up */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800305 if (!simple_positive(dentry)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 next = next->next;
307 continue;
308 }
309
310 dentry = dget(dentry);
311 spin_unlock(&dcache_lock);
312
Ian Kent3a15e2a2006-03-27 01:14:55 -0800313 /*
314 * Case 1: (i) indirect mount or top level pseudo direct mount
315 * (autofs-4.1).
316 * (ii) indirect mount with offset mount, check the "/"
317 * offset (autofs-5.0+).
318 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 if (d_mountpoint(dentry)) {
320 DPRINTK("checking mountpoint %p %.*s",
321 dentry, (int)dentry->d_name.len, dentry->d_name.name);
322
Ian Kente0a7aae2006-03-27 01:14:47 -0800323 /* Can we umount this guy */
324 if (autofs4_mount_busy(mnt, dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 goto next;
326
Ian Kente0a7aae2006-03-27 01:14:47 -0800327 /* Can we expire this guy */
328 if (autofs4_can_expire(dentry, timeout, do_now)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 expired = dentry;
330 break;
331 }
332 goto next;
333 }
334
Ian Kent1f5f2c32006-03-27 01:14:44 -0800335 if (simple_empty(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 goto next;
337
338 /* Case 2: tree mount, expire iff entire tree is not busy */
339 if (!exp_leaves) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700340 /* Lock the tree as we must expire as a whole */
341 spin_lock(&sbi->fs_lock);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800342 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
Ian Kent3a9720c2005-05-01 08:59:17 -0700343 struct autofs_info *inf = autofs4_dentry_ino(dentry);
344
345 /* Set this flag early to catch sys_chdir and the like */
346 inf->flags |= AUTOFS_INF_EXPIRING;
347 spin_unlock(&sbi->fs_lock);
348 expired = dentry;
349 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
Ian Kent3a9720c2005-05-01 08:59:17 -0700351 spin_unlock(&sbi->fs_lock);
Ian Kent3a15e2a2006-03-27 01:14:55 -0800352 /*
353 * Case 3: pseudo direct mount, expire individual leaves
354 * (autofs-4.1).
355 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 } else {
357 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
358 if (expired) {
359 dput(dentry);
360 break;
361 }
362 }
363next:
364 dput(dentry);
365 spin_lock(&dcache_lock);
366 next = next->next;
367 }
368
Ian Kent1f5f2c32006-03-27 01:14:44 -0800369 if (expired) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 DPRINTK("returning %p %.*s",
371 expired, (int)expired->d_name.len, expired->d_name.name);
372 spin_lock(&dcache_lock);
373 list_del(&expired->d_parent->d_subdirs);
Eric Dumazet5160ee62006-01-08 01:03:32 -0800374 list_add(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 spin_unlock(&dcache_lock);
376 return expired;
377 }
378 spin_unlock(&dcache_lock);
379
380 return NULL;
381}
382
383/* Perform an expiry operation */
384int autofs4_expire_run(struct super_block *sb,
385 struct vfsmount *mnt,
386 struct autofs_sb_info *sbi,
387 struct autofs_packet_expire __user *pkt_p)
388{
389 struct autofs_packet_expire pkt;
390 struct dentry *dentry;
391
392 memset(&pkt,0,sizeof pkt);
393
394 pkt.hdr.proto_version = sbi->version;
395 pkt.hdr.type = autofs_ptype_expire;
396
Ian Kent3a15e2a2006-03-27 01:14:55 -0800397 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return -EAGAIN;
399
400 pkt.len = dentry->d_name.len;
401 memcpy(pkt.name, dentry->d_name.name, pkt.len);
402 pkt.name[pkt.len] = '\0';
403 dput(dentry);
404
405 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
406 return -EFAULT;
407
408 return 0;
409}
410
411/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
412 more to be done */
413int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
414 struct autofs_sb_info *sbi, int __user *arg)
415{
416 struct dentry *dentry;
417 int ret = -EAGAIN;
418 int do_now = 0;
419
420 if (arg && get_user(do_now, arg))
421 return -EFAULT;
422
Ian Kent44d53eb2006-03-27 01:14:56 -0800423 if (sbi->type & AUTOFS_TYPE_DIRECT)
Ian Kent3a15e2a2006-03-27 01:14:55 -0800424 dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
425 else
426 dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
427
428 if (dentry) {
Ian Kent1f5f2c32006-03-27 01:14:44 -0800429 struct autofs_info *ino = autofs4_dentry_ino(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* This is synchronous because it makes the daemon a
432 little easier */
Ian Kent1f5f2c32006-03-27 01:14:44 -0800433 ino->flags |= AUTOFS_INF_EXPIRING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
Ian Kent1f5f2c32006-03-27 01:14:44 -0800435 ino->flags &= ~AUTOFS_INF_EXPIRING;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 dput(dentry);
437 }
Ian Kent1f5f2c32006-03-27 01:14:44 -0800438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret;
440}
441