quota: add a convenience macro for filesystems
[linux-2.6-block.git] / fs / autofs4 / expire.c
CommitLineData
1da177e4
LT
1/* -*- 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>
3a15e2ab 7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
1da177e4
LT
8 *
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
1f5f2c30 19/* Check if a dentry can be expired */
1da177e4
LT
20static 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 */
c0ba7e51 35 if (!timeout || time_after(ino->last_used + timeout, now))
1da177e4
LT
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 }
1da177e4
LT
44 return 1;
45}
46
1f5f2c30
IK
47/* Check a mount point for busyness */
48static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 49{
e0a7aae9 50 struct dentry *top = dentry;
1f5f2c30 51 int status = 1;
1da177e4
LT
52
53 DPRINTK("dentry %p %.*s",
54 dentry, (int)dentry->d_name.len, dentry->d_name.name);
55
56 mntget(mnt);
57 dget(dentry);
58
9b1e3afd 59 if (!autofs4_follow_mount(&mnt, &dentry))
1da177e4
LT
60 goto done;
61
1da177e4
LT
62 /* This is an autofs submount, we can't expire it */
63 if (is_autofs4_dentry(dentry))
64 goto done;
65
e0a7aae9 66 /* Update the expiry counter if fs is busy */
e3474a8e 67 if (!may_umount_tree(mnt)) {
e0a7aae9
IK
68 struct autofs_info *ino = autofs4_dentry_ino(top);
69 ino->last_used = jiffies;
70 goto done;
71 }
72
73 status = 0;
1da177e4
LT
74done:
75 DPRINTK("returning = %d", status);
76 mntput(mnt);
77 dput(dentry);
78 return status;
79}
80
1ce12bad
IK
81/*
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
3a15e2ab
IK
102/*
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
3a15e2ab
IK
116 /* 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
1da177e4
LT
131/* Check a directory tree of mount points for busyness
132 * The tree is not busy iff no mountpoints are busy
1da177e4 133 */
1f5f2c30
IK
134static int autofs4_tree_busy(struct vfsmount *mnt,
135 struct dentry *top,
136 unsigned long timeout,
137 int do_now)
1da177e4 138{
e0a7aae9 139 struct autofs_info *top_ino = autofs4_dentry_ino(top);
1ce12bad 140 struct dentry *p;
1da177e4 141
1f5f2c30 142 DPRINTK("top %p %.*s",
1da177e4
LT
143 top, (int)top->d_name.len, top->d_name.name);
144
145 /* Negative dentry - give up */
146 if (!simple_positive(top))
1f5f2c30 147 return 1;
1da177e4 148
1da177e4 149 spin_lock(&dcache_lock);
1ce12bad 150 for (p = top; p; p = next_dentry(p, top)) {
1da177e4 151 /* Negative dentry - give up */
1ce12bad 152 if (!simple_positive(p))
1da177e4 153 continue;
1da177e4
LT
154
155 DPRINTK("dentry %p %.*s",
1ce12bad 156 p, (int) p->d_name.len, p->d_name.name);
1da177e4 157
1ce12bad 158 p = dget(p);
1da177e4
LT
159 spin_unlock(&dcache_lock);
160
1aff3c8b
IK
161 /*
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.
e0a7aae9 165 * If the fs is busy update the expiry counter.
1aff3c8b 166 */
1ce12bad 167 if (d_mountpoint(p)) {
1ce12bad 168 if (autofs4_mount_busy(mnt, p)) {
e0a7aae9 169 top_ino->last_used = jiffies;
1ce12bad 170 dput(p);
1f5f2c30 171 return 1;
1da177e4 172 }
1aff3c8b 173 } else {
e0a7aae9 174 struct autofs_info *ino = autofs4_dentry_ino(p);
1aff3c8b
IK
175 unsigned int ino_count = atomic_read(&ino->count);
176
f9022f66
IK
177 /*
178 * Clean stale dentries below that have not been
179 * invalidated after a mount fail during lookup
180 */
181 d_invalidate(p);
182
1aff3c8b
IK
183 /* allow for dget above and top is already dgot */
184 if (p == top)
185 ino_count += 2;
186 else
187 ino_count++;
188
189 if (atomic_read(&p->d_count) > ino_count) {
e0a7aae9 190 top_ino->last_used = jiffies;
1aff3c8b
IK
191 dput(p);
192 return 1;
193 }
1da177e4 194 }
1ce12bad 195 dput(p);
1da177e4 196 spin_lock(&dcache_lock);
1da177e4
LT
197 }
198 spin_unlock(&dcache_lock);
1aff3c8b
IK
199
200 /* Timeout of a tree mount is ultimately determined by its top dentry */
201 if (!autofs4_can_expire(top, timeout, do_now))
202 return 1;
203
1f5f2c30 204 return 0;
1da177e4
LT
205}
206
207static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
208 struct dentry *parent,
209 unsigned long timeout,
210 int do_now)
211{
1ce12bad 212 struct dentry *p;
1da177e4
LT
213
214 DPRINTK("parent %p %.*s",
215 parent, (int)parent->d_name.len, parent->d_name.name);
216
217 spin_lock(&dcache_lock);
1ce12bad 218 for (p = parent; p; p = next_dentry(p, parent)) {
1da177e4 219 /* Negative dentry - give up */
1ce12bad 220 if (!simple_positive(p))
1da177e4 221 continue;
1da177e4
LT
222
223 DPRINTK("dentry %p %.*s",
1ce12bad 224 p, (int) p->d_name.len, p->d_name.name);
1da177e4 225
1ce12bad 226 p = dget(p);
1da177e4
LT
227 spin_unlock(&dcache_lock);
228
1ce12bad 229 if (d_mountpoint(p)) {
e0a7aae9
IK
230 /* Can we umount this guy */
231 if (autofs4_mount_busy(mnt, p))
1da177e4
LT
232 goto cont;
233
e0a7aae9
IK
234 /* Can we expire this guy */
235 if (autofs4_can_expire(p, timeout, do_now))
1ce12bad 236 return p;
1da177e4
LT
237 }
238cont:
1ce12bad 239 dput(p);
1da177e4 240 spin_lock(&dcache_lock);
1da177e4
LT
241 }
242 spin_unlock(&dcache_lock);
1da177e4
LT
243 return NULL;
244}
245
3a15e2ab
IK
246/* Check if we can expire a direct mount (possibly a tree) */
247static struct dentry *autofs4_expire_direct(struct super_block *sb,
248 struct vfsmount *mnt,
249 struct autofs_sb_info *sbi,
250 int how)
251{
252 unsigned long timeout;
253 struct dentry *root = dget(sb->s_root);
254 int do_now = how & AUTOFS_EXP_IMMEDIATE;
255
c0ba7e51 256 if (!root)
3a15e2ab
IK
257 return NULL;
258
259 now = jiffies;
260 timeout = sbi->exp_timeout;
261
262 /* Lock the tree as we must expire as a whole */
263 spin_lock(&sbi->fs_lock);
264 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
265 struct autofs_info *ino = autofs4_dentry_ino(root);
266
267 /* Set this flag early to catch sys_chdir and the like */
268 ino->flags |= AUTOFS_INF_EXPIRING;
269 spin_unlock(&sbi->fs_lock);
270 return root;
271 }
272 spin_unlock(&sbi->fs_lock);
273 dput(root);
274
275 return NULL;
276}
277
1da177e4
LT
278/*
279 * Find an eligible tree to time-out
280 * A tree is eligible if :-
281 * - it is unused by any user process
282 * - it has been unused for exp_timeout time
283 */
3a15e2ab
IK
284static struct dentry *autofs4_expire_indirect(struct super_block *sb,
285 struct vfsmount *mnt,
286 struct autofs_sb_info *sbi,
287 int how)
1da177e4
LT
288{
289 unsigned long timeout;
290 struct dentry *root = sb->s_root;
291 struct dentry *expired = NULL;
292 struct list_head *next;
293 int do_now = how & AUTOFS_EXP_IMMEDIATE;
294 int exp_leaves = how & AUTOFS_EXP_LEAVES;
295
c0ba7e51 296 if (!root)
1da177e4
LT
297 return NULL;
298
299 now = jiffies;
300 timeout = sbi->exp_timeout;
301
302 spin_lock(&dcache_lock);
303 next = root->d_subdirs.next;
304
305 /* On exit from the loop expire is set to a dgot dentry
306 * to expire or it's NULL */
307 while ( next != &root->d_subdirs ) {
5160ee6f 308 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
1da177e4
LT
309
310 /* Negative dentry - give up */
1f5f2c30 311 if (!simple_positive(dentry)) {
1da177e4
LT
312 next = next->next;
313 continue;
314 }
315
316 dentry = dget(dentry);
317 spin_unlock(&dcache_lock);
318
3a15e2ab
IK
319 /*
320 * Case 1: (i) indirect mount or top level pseudo direct mount
321 * (autofs-4.1).
322 * (ii) indirect mount with offset mount, check the "/"
323 * offset (autofs-5.0+).
324 */
1da177e4
LT
325 if (d_mountpoint(dentry)) {
326 DPRINTK("checking mountpoint %p %.*s",
327 dentry, (int)dentry->d_name.len, dentry->d_name.name);
328
e0a7aae9
IK
329 /* Can we umount this guy */
330 if (autofs4_mount_busy(mnt, dentry))
1da177e4
LT
331 goto next;
332
e0a7aae9
IK
333 /* Can we expire this guy */
334 if (autofs4_can_expire(dentry, timeout, do_now)) {
1da177e4
LT
335 expired = dentry;
336 break;
337 }
338 goto next;
339 }
340
1f5f2c30 341 if (simple_empty(dentry))
1da177e4
LT
342 goto next;
343
344 /* Case 2: tree mount, expire iff entire tree is not busy */
345 if (!exp_leaves) {
3a9720ce
IK
346 /* Lock the tree as we must expire as a whole */
347 spin_lock(&sbi->fs_lock);
1f5f2c30 348 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
3a9720ce
IK
349 struct autofs_info *inf = autofs4_dentry_ino(dentry);
350
351 /* Set this flag early to catch sys_chdir and the like */
352 inf->flags |= AUTOFS_INF_EXPIRING;
353 spin_unlock(&sbi->fs_lock);
354 expired = dentry;
355 break;
1da177e4 356 }
3a9720ce 357 spin_unlock(&sbi->fs_lock);
3a15e2ab
IK
358 /*
359 * Case 3: pseudo direct mount, expire individual leaves
360 * (autofs-4.1).
361 */
1da177e4
LT
362 } else {
363 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
364 if (expired) {
365 dput(dentry);
366 break;
367 }
368 }
369next:
370 dput(dentry);
371 spin_lock(&dcache_lock);
372 next = next->next;
373 }
374
1f5f2c30 375 if (expired) {
1da177e4
LT
376 DPRINTK("returning %p %.*s",
377 expired, (int)expired->d_name.len, expired->d_name.name);
378 spin_lock(&dcache_lock);
f116629d 379 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
1da177e4
LT
380 spin_unlock(&dcache_lock);
381 return expired;
382 }
383 spin_unlock(&dcache_lock);
384
385 return NULL;
386}
387
388/* Perform an expiry operation */
389int autofs4_expire_run(struct super_block *sb,
390 struct vfsmount *mnt,
391 struct autofs_sb_info *sbi,
392 struct autofs_packet_expire __user *pkt_p)
393{
394 struct autofs_packet_expire pkt;
395 struct dentry *dentry;
396
397 memset(&pkt,0,sizeof pkt);
398
399 pkt.hdr.proto_version = sbi->version;
400 pkt.hdr.type = autofs_ptype_expire;
401
3a15e2ab 402 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
1da177e4
LT
403 return -EAGAIN;
404
405 pkt.len = dentry->d_name.len;
406 memcpy(pkt.name, dentry->d_name.name, pkt.len);
407 pkt.name[pkt.len] = '\0';
408 dput(dentry);
409
410 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
411 return -EFAULT;
412
413 return 0;
414}
415
416/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
417 more to be done */
418int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
419 struct autofs_sb_info *sbi, int __user *arg)
420{
421 struct dentry *dentry;
422 int ret = -EAGAIN;
423 int do_now = 0;
424
425 if (arg && get_user(do_now, arg))
426 return -EFAULT;
427
44d53eb0 428 if (sbi->type & AUTOFS_TYPE_DIRECT)
3a15e2ab
IK
429 dentry = autofs4_expire_direct(sb, mnt, sbi, do_now);
430 else
431 dentry = autofs4_expire_indirect(sb, mnt, sbi, do_now);
432
433 if (dentry) {
1f5f2c30 434 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1da177e4
LT
435
436 /* This is synchronous because it makes the daemon a
437 little easier */
1f5f2c30 438 ino->flags |= AUTOFS_INF_EXPIRING;
1da177e4 439 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
1f5f2c30 440 ino->flags &= ~AUTOFS_INF_EXPIRING;
1da177e4
LT
441 dput(dentry);
442 }
1f5f2c30 443
1da177e4
LT
444 return ret;
445}
446