Fix gcc 4.5.1 miscompiling drivers/char/i8k.c (again)
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / autofs4 / expire.c
blob74bc9aa6df310d4ccdf0d312dd43976c02f93d1e
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-2006 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 * ------------------------------------------------------------------------- */
15 #include "autofs_i.h"
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 */
26 if (ino == NULL)
27 return 0;
29 /* No point expiring a pending mount */
30 if (ino->flags & AUTOFS_INF_PENDING)
31 return 0;
33 if (!do_now) {
34 /* Too young to die */
35 if (!timeout || time_after(ino->last_used + timeout, now))
36 return 0;
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;
44 return 1;
47 /* Check a mount point for busyness */
48 static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
50 struct dentry *top = dentry;
51 struct path path = {.mnt = mnt, .dentry = dentry};
52 int status = 1;
54 DPRINTK("dentry %p %.*s",
55 dentry, (int)dentry->d_name.len, dentry->d_name.name);
57 path_get(&path);
59 if (!follow_down(&path))
60 goto done;
62 if (is_autofs4_dentry(path.dentry)) {
63 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
65 /* This is an autofs submount, we can't expire it */
66 if (autofs_type_indirect(sbi->type))
67 goto done;
70 * Otherwise it's an offset mount and we need to check
71 * if we can umount its mount, if there is one.
73 if (!d_mountpoint(path.dentry)) {
74 status = 0;
75 goto done;
79 /* Update the expiry counter if fs is busy */
80 if (!may_umount_tree(path.mnt)) {
81 struct autofs_info *ino = autofs4_dentry_ino(top);
82 ino->last_used = jiffies;
83 goto done;
86 status = 0;
87 done:
88 DPRINTK("returning = %d", status);
89 path_put(&path);
90 return status;
94 * Calculate next entry in top down tree traversal.
95 * From next_mnt in namespace.c - elegant.
97 static struct dentry *next_dentry(struct dentry *p, struct dentry *root)
99 struct list_head *next = p->d_subdirs.next;
101 if (next == &p->d_subdirs) {
102 while (1) {
103 if (p == root)
104 return NULL;
105 next = p->d_u.d_child.next;
106 if (next != &p->d_parent->d_subdirs)
107 break;
108 p = p->d_parent;
111 return list_entry(next, struct dentry, d_u.d_child);
115 * Check a direct mount point for busyness.
116 * Direct mounts have similar expiry semantics to tree mounts.
117 * The tree is not busy iff no mountpoints are busy and there are no
118 * autofs submounts.
120 static int autofs4_direct_busy(struct vfsmount *mnt,
121 struct dentry *top,
122 unsigned long timeout,
123 int do_now)
125 DPRINTK("top %p %.*s",
126 top, (int) top->d_name.len, top->d_name.name);
128 /* If it's busy update the expiry counters */
129 if (!may_umount_tree(mnt)) {
130 struct autofs_info *ino = autofs4_dentry_ino(top);
131 if (ino)
132 ino->last_used = jiffies;
133 return 1;
136 /* Timeout of a direct mount is determined by its top dentry */
137 if (!autofs4_can_expire(top, timeout, do_now))
138 return 1;
140 return 0;
143 /* Check a directory tree of mount points for busyness
144 * The tree is not busy iff no mountpoints are busy
146 static int autofs4_tree_busy(struct vfsmount *mnt,
147 struct dentry *top,
148 unsigned long timeout,
149 int do_now)
151 struct autofs_info *top_ino = autofs4_dentry_ino(top);
152 struct dentry *p;
154 DPRINTK("top %p %.*s",
155 top, (int)top->d_name.len, top->d_name.name);
157 /* Negative dentry - give up */
158 if (!simple_positive(top))
159 return 1;
161 spin_lock(&dcache_lock);
162 for (p = top; p; p = next_dentry(p, top)) {
163 /* Negative dentry - give up */
164 if (!simple_positive(p))
165 continue;
167 DPRINTK("dentry %p %.*s",
168 p, (int) p->d_name.len, p->d_name.name);
170 p = dget(p);
171 spin_unlock(&dcache_lock);
174 * Is someone visiting anywhere in the subtree ?
175 * If there's no mount we need to check the usage
176 * count for the autofs dentry.
177 * If the fs is busy update the expiry counter.
179 if (d_mountpoint(p)) {
180 if (autofs4_mount_busy(mnt, p)) {
181 top_ino->last_used = jiffies;
182 dput(p);
183 return 1;
185 } else {
186 struct autofs_info *ino = autofs4_dentry_ino(p);
187 unsigned int ino_count = atomic_read(&ino->count);
190 * Clean stale dentries below that have not been
191 * invalidated after a mount fail during lookup
193 d_invalidate(p);
195 /* allow for dget above and top is already dgot */
196 if (p == top)
197 ino_count += 2;
198 else
199 ino_count++;
201 if (atomic_read(&p->d_count) > ino_count) {
202 top_ino->last_used = jiffies;
203 dput(p);
204 return 1;
207 dput(p);
208 spin_lock(&dcache_lock);
210 spin_unlock(&dcache_lock);
212 /* Timeout of a tree mount is ultimately determined by its top dentry */
213 if (!autofs4_can_expire(top, timeout, do_now))
214 return 1;
216 return 0;
219 static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
220 struct dentry *parent,
221 unsigned long timeout,
222 int do_now)
224 struct dentry *p;
226 DPRINTK("parent %p %.*s",
227 parent, (int)parent->d_name.len, parent->d_name.name);
229 spin_lock(&dcache_lock);
230 for (p = parent; p; p = next_dentry(p, parent)) {
231 /* Negative dentry - give up */
232 if (!simple_positive(p))
233 continue;
235 DPRINTK("dentry %p %.*s",
236 p, (int) p->d_name.len, p->d_name.name);
238 p = dget(p);
239 spin_unlock(&dcache_lock);
241 if (d_mountpoint(p)) {
242 /* Can we umount this guy */
243 if (autofs4_mount_busy(mnt, p))
244 goto cont;
246 /* Can we expire this guy */
247 if (autofs4_can_expire(p, timeout, do_now))
248 return p;
250 cont:
251 dput(p);
252 spin_lock(&dcache_lock);
254 spin_unlock(&dcache_lock);
255 return NULL;
258 /* Check if we can expire a direct mount (possibly a tree) */
259 struct dentry *autofs4_expire_direct(struct super_block *sb,
260 struct vfsmount *mnt,
261 struct autofs_sb_info *sbi,
262 int how)
264 unsigned long timeout;
265 struct dentry *root = dget(sb->s_root);
266 int do_now = how & AUTOFS_EXP_IMMEDIATE;
268 if (!root)
269 return NULL;
271 now = jiffies;
272 timeout = sbi->exp_timeout;
274 spin_lock(&sbi->fs_lock);
275 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
276 struct autofs_info *ino = autofs4_dentry_ino(root);
277 if (d_mountpoint(root)) {
278 ino->flags |= AUTOFS_INF_MOUNTPOINT;
279 root->d_mounted--;
281 ino->flags |= AUTOFS_INF_EXPIRING;
282 autofs4_add_expiring(root);
283 init_completion(&ino->expire_complete);
284 spin_unlock(&sbi->fs_lock);
285 return root;
287 spin_unlock(&sbi->fs_lock);
288 dput(root);
290 return NULL;
294 * Find an eligible tree to time-out
295 * A tree is eligible if :-
296 * - it is unused by any user process
297 * - it has been unused for exp_timeout time
299 struct dentry *autofs4_expire_indirect(struct super_block *sb,
300 struct vfsmount *mnt,
301 struct autofs_sb_info *sbi,
302 int how)
304 unsigned long timeout;
305 struct dentry *root = sb->s_root;
306 struct dentry *expired = NULL;
307 struct list_head *next;
308 int do_now = how & AUTOFS_EXP_IMMEDIATE;
309 int exp_leaves = how & AUTOFS_EXP_LEAVES;
310 struct autofs_info *ino;
311 unsigned int ino_count;
313 if (!root)
314 return NULL;
316 now = jiffies;
317 timeout = sbi->exp_timeout;
319 spin_lock(&dcache_lock);
320 next = root->d_subdirs.next;
322 /* On exit from the loop expire is set to a dgot dentry
323 * to expire or it's NULL */
324 while ( next != &root->d_subdirs ) {
325 struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child);
327 /* Negative dentry - give up */
328 if (!simple_positive(dentry)) {
329 next = next->next;
330 continue;
333 dentry = dget(dentry);
334 spin_unlock(&dcache_lock);
336 spin_lock(&sbi->fs_lock);
337 ino = autofs4_dentry_ino(dentry);
340 * Case 1: (i) indirect mount or top level pseudo direct mount
341 * (autofs-4.1).
342 * (ii) indirect mount with offset mount, check the "/"
343 * offset (autofs-5.0+).
345 if (d_mountpoint(dentry)) {
346 DPRINTK("checking mountpoint %p %.*s",
347 dentry, (int)dentry->d_name.len, dentry->d_name.name);
349 /* Path walk currently on this dentry? */
350 ino_count = atomic_read(&ino->count) + 2;
351 if (atomic_read(&dentry->d_count) > ino_count)
352 goto next;
354 /* Can we umount this guy */
355 if (autofs4_mount_busy(mnt, dentry))
356 goto next;
358 /* Can we expire this guy */
359 if (autofs4_can_expire(dentry, timeout, do_now)) {
360 expired = dentry;
361 goto found;
363 goto next;
366 if (simple_empty(dentry))
367 goto next;
369 /* Case 2: tree mount, expire iff entire tree is not busy */
370 if (!exp_leaves) {
371 /* Path walk currently on this dentry? */
372 ino_count = atomic_read(&ino->count) + 1;
373 if (atomic_read(&dentry->d_count) > ino_count)
374 goto next;
376 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
377 expired = dentry;
378 goto found;
381 * Case 3: pseudo direct mount, expire individual leaves
382 * (autofs-4.1).
384 } else {
385 /* Path walk currently on this dentry? */
386 ino_count = atomic_read(&ino->count) + 1;
387 if (atomic_read(&dentry->d_count) > ino_count)
388 goto next;
390 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
391 if (expired) {
392 dput(dentry);
393 goto found;
396 next:
397 spin_unlock(&sbi->fs_lock);
398 dput(dentry);
399 spin_lock(&dcache_lock);
400 next = next->next;
402 spin_unlock(&dcache_lock);
403 return NULL;
405 found:
406 DPRINTK("returning %p %.*s",
407 expired, (int)expired->d_name.len, expired->d_name.name);
408 ino = autofs4_dentry_ino(expired);
409 ino->flags |= AUTOFS_INF_EXPIRING;
410 autofs4_add_expiring(expired);
411 init_completion(&ino->expire_complete);
412 spin_unlock(&sbi->fs_lock);
413 spin_lock(&dcache_lock);
414 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
415 spin_unlock(&dcache_lock);
416 return expired;
419 int autofs4_expire_wait(struct dentry *dentry)
421 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
422 struct autofs_info *ino = autofs4_dentry_ino(dentry);
423 int status;
425 /* Block on any pending expire */
426 spin_lock(&sbi->fs_lock);
427 if (ino->flags & AUTOFS_INF_EXPIRING) {
428 spin_unlock(&sbi->fs_lock);
430 DPRINTK("waiting for expire %p name=%.*s",
431 dentry, dentry->d_name.len, dentry->d_name.name);
433 status = autofs4_wait(sbi, dentry, NFY_NONE);
434 wait_for_completion(&ino->expire_complete);
436 DPRINTK("expire done status=%d", status);
438 if (d_unhashed(dentry) && IS_DEADDIR(dentry->d_inode))
439 return -EAGAIN;
441 return status;
443 spin_unlock(&sbi->fs_lock);
445 return 0;
448 /* Perform an expiry operation */
449 int autofs4_expire_run(struct super_block *sb,
450 struct vfsmount *mnt,
451 struct autofs_sb_info *sbi,
452 struct autofs_packet_expire __user *pkt_p)
454 struct autofs_packet_expire pkt;
455 struct autofs_info *ino;
456 struct dentry *dentry;
457 int ret = 0;
459 memset(&pkt,0,sizeof pkt);
461 pkt.hdr.proto_version = sbi->version;
462 pkt.hdr.type = autofs_ptype_expire;
464 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
465 return -EAGAIN;
467 pkt.len = dentry->d_name.len;
468 memcpy(pkt.name, dentry->d_name.name, pkt.len);
469 pkt.name[pkt.len] = '\0';
470 dput(dentry);
472 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
473 ret = -EFAULT;
475 spin_lock(&sbi->fs_lock);
476 ino = autofs4_dentry_ino(dentry);
477 ino->flags &= ~AUTOFS_INF_EXPIRING;
478 autofs4_del_expiring(dentry);
479 complete_all(&ino->expire_complete);
480 spin_unlock(&sbi->fs_lock);
482 return ret;
485 int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
486 struct autofs_sb_info *sbi, int when)
488 struct dentry *dentry;
489 int ret = -EAGAIN;
491 if (autofs_type_trigger(sbi->type))
492 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
493 else
494 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
496 if (dentry) {
497 struct autofs_info *ino = autofs4_dentry_ino(dentry);
499 /* This is synchronous because it makes the daemon a
500 little easier */
501 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
503 spin_lock(&sbi->fs_lock);
504 if (ino->flags & AUTOFS_INF_MOUNTPOINT) {
505 sb->s_root->d_mounted++;
506 ino->flags &= ~AUTOFS_INF_MOUNTPOINT;
508 ino->flags &= ~AUTOFS_INF_EXPIRING;
509 autofs4_del_expiring(dentry);
510 complete_all(&ino->expire_complete);
511 spin_unlock(&sbi->fs_lock);
512 dput(dentry);
515 return ret;
518 /* Call repeatedly until it returns -EAGAIN, meaning there's nothing
519 more to be done */
520 int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
521 struct autofs_sb_info *sbi, int __user *arg)
523 int do_now = 0;
525 if (arg && get_user(do_now, arg))
526 return -EFAULT;
528 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);