Btrfs: don't use global block reservation for inode cache truncation
[deliverable/linux.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
1da177e4
LT
29 if (!do_now) {
30 /* Too young to die */
c0ba7e51 31 if (!timeout || time_after(ino->last_used + timeout, now))
1da177e4
LT
32 return 0;
33
34 /* update last_used here :-
35 - obviously makes sense if it is in use now
36 - less obviously, prevents rapid-fire expire
37 attempts if expire fails the first time */
38 ino->last_used = now;
39 }
1da177e4
LT
40 return 1;
41}
42
1f5f2c30
IK
43/* Check a mount point for busyness */
44static int autofs4_mount_busy(struct vfsmount *mnt, struct dentry *dentry)
1da177e4 45{
e0a7aae9 46 struct dentry *top = dentry;
9393bd07 47 struct path path = {.mnt = mnt, .dentry = dentry};
1f5f2c30 48 int status = 1;
1da177e4
LT
49
50 DPRINTK("dentry %p %.*s",
51 dentry, (int)dentry->d_name.len, dentry->d_name.name);
52
9393bd07 53 path_get(&path);
1da177e4 54
cc53ce53 55 if (!follow_down_one(&path))
1da177e4
LT
56 goto done;
57
9393bd07
AV
58 if (is_autofs4_dentry(path.dentry)) {
59 struct autofs_sb_info *sbi = autofs4_sbi(path.dentry->d_sb);
bc9c4068
IK
60
61 /* This is an autofs submount, we can't expire it */
a92daf6b 62 if (autofs_type_indirect(sbi->type))
bc9c4068
IK
63 goto done;
64
65 /*
66 * Otherwise it's an offset mount and we need to check
67 * if we can umount its mount, if there is one.
68 */
9393bd07 69 if (!d_mountpoint(path.dentry)) {
a8985f3a 70 status = 0;
bc9c4068 71 goto done;
a8985f3a 72 }
bc9c4068 73 }
1da177e4 74
e0a7aae9 75 /* Update the expiry counter if fs is busy */
37d0892c 76 if (!may_umount_tree(path.mnt)) {
e0a7aae9
IK
77 struct autofs_info *ino = autofs4_dentry_ino(top);
78 ino->last_used = jiffies;
79 goto done;
80 }
81
82 status = 0;
1da177e4
LT
83done:
84 DPRINTK("returning = %d", status);
9393bd07 85 path_put(&path);
1da177e4
LT
86 return status;
87}
88
d4a85e35
IK
89/*
90 * Calculate and dget next entry in the subdirs list under root.
91 */
92static struct dentry *get_next_positive_subdir(struct dentry *prev,
93 struct dentry *root)
94{
e7854723 95 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
d4a85e35 96 struct list_head *next;
a45440f0 97 struct dentry *q;
d4a85e35 98
e7854723 99 spin_lock(&sbi->lookup_lock);
a45440f0 100 spin_lock(&root->d_lock);
d4a85e35 101
a45440f0
IK
102 if (prev)
103 next = prev->d_u.d_child.next;
104 else {
d4a85e35
IK
105 prev = dget_dlock(root);
106 next = prev->d_subdirs.next;
d4a85e35
IK
107 }
108
a45440f0 109cont:
d4a85e35 110 if (next == &root->d_subdirs) {
a45440f0 111 spin_unlock(&root->d_lock);
e7854723 112 spin_unlock(&sbi->lookup_lock);
d4a85e35
IK
113 dput(prev);
114 return NULL;
115 }
116
117 q = list_entry(next, struct dentry, d_u.d_child);
118
119 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
a45440f0
IK
120 /* Already gone or negative dentry (under construction) - try next */
121 if (q->d_count == 0 || !simple_positive(q)) {
122 spin_unlock(&q->d_lock);
123 next = q->d_u.d_child.next;
124 goto cont;
d4a85e35
IK
125 }
126 dget_dlock(q);
127 spin_unlock(&q->d_lock);
a45440f0 128 spin_unlock(&root->d_lock);
e7854723 129 spin_unlock(&sbi->lookup_lock);
d4a85e35
IK
130
131 dput(prev);
132
133 return q;
134}
135
1ce12bad 136/*
2fd6b7f5 137 * Calculate and dget next entry in top down tree traversal.
1ce12bad 138 */
2fd6b7f5
NP
139static struct dentry *get_next_positive_dentry(struct dentry *prev,
140 struct dentry *root)
1ce12bad 141{
e7854723 142 struct autofs_sb_info *sbi = autofs4_sbi(root->d_sb);
2fd6b7f5
NP
143 struct list_head *next;
144 struct dentry *p, *ret;
145
146 if (prev == NULL)
c14cc63a 147 return dget(root);
1ce12bad 148
e7854723 149 spin_lock(&sbi->lookup_lock);
2fd6b7f5
NP
150relock:
151 p = prev;
152 spin_lock(&p->d_lock);
153again:
154 next = p->d_subdirs.next;
1ce12bad
IK
155 if (next == &p->d_subdirs) {
156 while (1) {
2fd6b7f5
NP
157 struct dentry *parent;
158
159 if (p == root) {
160 spin_unlock(&p->d_lock);
e7854723 161 spin_unlock(&sbi->lookup_lock);
2fd6b7f5 162 dput(prev);
1ce12bad 163 return NULL;
2fd6b7f5
NP
164 }
165
166 parent = p->d_parent;
167 if (!spin_trylock(&parent->d_lock)) {
168 spin_unlock(&p->d_lock);
169 cpu_relax();
170 goto relock;
171 }
172 spin_unlock(&p->d_lock);
1ce12bad 173 next = p->d_u.d_child.next;
2fd6b7f5
NP
174 p = parent;
175 if (next != &parent->d_subdirs)
1ce12bad 176 break;
1ce12bad
IK
177 }
178 }
2fd6b7f5
NP
179 ret = list_entry(next, struct dentry, d_u.d_child);
180
181 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
182 /* Negative dentry - try next */
183 if (!simple_positive(ret)) {
c14cc63a 184 spin_unlock(&p->d_lock);
1d6f2097 185 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
2fd6b7f5
NP
186 p = ret;
187 goto again;
188 }
189 dget_dlock(ret);
190 spin_unlock(&ret->d_lock);
191 spin_unlock(&p->d_lock);
e7854723 192 spin_unlock(&sbi->lookup_lock);
2fd6b7f5
NP
193
194 dput(prev);
195
196 return ret;
1ce12bad
IK
197}
198
3a15e2ab
IK
199/*
200 * Check a direct mount point for busyness.
201 * Direct mounts have similar expiry semantics to tree mounts.
202 * The tree is not busy iff no mountpoints are busy and there are no
203 * autofs submounts.
204 */
205static int autofs4_direct_busy(struct vfsmount *mnt,
206 struct dentry *top,
207 unsigned long timeout,
208 int do_now)
209{
210 DPRINTK("top %p %.*s",
211 top, (int) top->d_name.len, top->d_name.name);
212
3a15e2ab
IK
213 /* If it's busy update the expiry counters */
214 if (!may_umount_tree(mnt)) {
215 struct autofs_info *ino = autofs4_dentry_ino(top);
216 if (ino)
217 ino->last_used = jiffies;
218 return 1;
219 }
220
221 /* Timeout of a direct mount is determined by its top dentry */
222 if (!autofs4_can_expire(top, timeout, do_now))
223 return 1;
224
225 return 0;
226}
227
1da177e4
LT
228/* Check a directory tree of mount points for busyness
229 * The tree is not busy iff no mountpoints are busy
1da177e4 230 */
1f5f2c30
IK
231static int autofs4_tree_busy(struct vfsmount *mnt,
232 struct dentry *top,
233 unsigned long timeout,
234 int do_now)
1da177e4 235{
e0a7aae9 236 struct autofs_info *top_ino = autofs4_dentry_ino(top);
1ce12bad 237 struct dentry *p;
1da177e4 238
1f5f2c30 239 DPRINTK("top %p %.*s",
1da177e4
LT
240 top, (int)top->d_name.len, top->d_name.name);
241
242 /* Negative dentry - give up */
243 if (!simple_positive(top))
1f5f2c30 244 return 1;
1da177e4 245
2fd6b7f5
NP
246 p = NULL;
247 while ((p = get_next_positive_dentry(p, top))) {
1da177e4 248 DPRINTK("dentry %p %.*s",
1ce12bad 249 p, (int) p->d_name.len, p->d_name.name);
1da177e4 250
1aff3c8b
IK
251 /*
252 * Is someone visiting anywhere in the subtree ?
253 * If there's no mount we need to check the usage
254 * count for the autofs dentry.
e0a7aae9 255 * If the fs is busy update the expiry counter.
1aff3c8b 256 */
1ce12bad 257 if (d_mountpoint(p)) {
1ce12bad 258 if (autofs4_mount_busy(mnt, p)) {
e0a7aae9 259 top_ino->last_used = jiffies;
1ce12bad 260 dput(p);
1f5f2c30 261 return 1;
1da177e4 262 }
1aff3c8b 263 } else {
e0a7aae9 264 struct autofs_info *ino = autofs4_dentry_ino(p);
1aff3c8b
IK
265 unsigned int ino_count = atomic_read(&ino->count);
266
f9022f66
IK
267 /*
268 * Clean stale dentries below that have not been
269 * invalidated after a mount fail during lookup
270 */
271 d_invalidate(p);
272
1aff3c8b
IK
273 /* allow for dget above and top is already dgot */
274 if (p == top)
275 ino_count += 2;
276 else
277 ino_count++;
278
b7ab39f6 279 if (p->d_count > ino_count) {
e0a7aae9 280 top_ino->last_used = jiffies;
1aff3c8b
IK
281 dput(p);
282 return 1;
283 }
1da177e4 284 }
1da177e4 285 }
1aff3c8b
IK
286
287 /* Timeout of a tree mount is ultimately determined by its top dentry */
288 if (!autofs4_can_expire(top, timeout, do_now))
289 return 1;
290
1f5f2c30 291 return 0;
1da177e4
LT
292}
293
294static struct dentry *autofs4_check_leaves(struct vfsmount *mnt,
295 struct dentry *parent,
296 unsigned long timeout,
297 int do_now)
298{
1ce12bad 299 struct dentry *p;
1da177e4
LT
300
301 DPRINTK("parent %p %.*s",
302 parent, (int)parent->d_name.len, parent->d_name.name);
303
2fd6b7f5
NP
304 p = NULL;
305 while ((p = get_next_positive_dentry(p, parent))) {
1da177e4 306 DPRINTK("dentry %p %.*s",
1ce12bad 307 p, (int) p->d_name.len, p->d_name.name);
1da177e4 308
1ce12bad 309 if (d_mountpoint(p)) {
e0a7aae9
IK
310 /* Can we umount this guy */
311 if (autofs4_mount_busy(mnt, p))
2fd6b7f5 312 continue;
1da177e4 313
e0a7aae9
IK
314 /* Can we expire this guy */
315 if (autofs4_can_expire(p, timeout, do_now))
1ce12bad 316 return p;
1da177e4 317 }
1da177e4 318 }
1da177e4
LT
319 return NULL;
320}
321
3a15e2ab 322/* Check if we can expire a direct mount (possibly a tree) */
8d7b48e0
IK
323struct dentry *autofs4_expire_direct(struct super_block *sb,
324 struct vfsmount *mnt,
325 struct autofs_sb_info *sbi,
326 int how)
3a15e2ab
IK
327{
328 unsigned long timeout;
329 struct dentry *root = dget(sb->s_root);
330 int do_now = how & AUTOFS_EXP_IMMEDIATE;
b5b80177 331 struct autofs_info *ino;
3a15e2ab 332
c0ba7e51 333 if (!root)
3a15e2ab
IK
334 return NULL;
335
336 now = jiffies;
337 timeout = sbi->exp_timeout;
338
3a15e2ab 339 spin_lock(&sbi->fs_lock);
b5b80177
IK
340 ino = autofs4_dentry_ino(root);
341 /* No point expiring a pending mount */
f9398c23
IK
342 if (ino->flags & AUTOFS_INF_PENDING)
343 goto out;
3a15e2ab
IK
344 if (!autofs4_direct_busy(mnt, root, timeout, do_now)) {
345 struct autofs_info *ino = autofs4_dentry_ino(root);
3a15e2ab 346 ino->flags |= AUTOFS_INF_EXPIRING;
6e60a9ab 347 init_completion(&ino->expire_complete);
3a15e2ab
IK
348 spin_unlock(&sbi->fs_lock);
349 return root;
350 }
f9398c23 351out:
3a15e2ab
IK
352 spin_unlock(&sbi->fs_lock);
353 dput(root);
354
355 return NULL;
356}
357
1da177e4
LT
358/*
359 * Find an eligible tree to time-out
360 * A tree is eligible if :-
361 * - it is unused by any user process
362 * - it has been unused for exp_timeout time
363 */
8d7b48e0
IK
364struct dentry *autofs4_expire_indirect(struct super_block *sb,
365 struct vfsmount *mnt,
366 struct autofs_sb_info *sbi,
367 int how)
1da177e4
LT
368{
369 unsigned long timeout;
370 struct dentry *root = sb->s_root;
2fd6b7f5 371 struct dentry *dentry;
1da177e4 372 struct dentry *expired = NULL;
1da177e4
LT
373 int do_now = how & AUTOFS_EXP_IMMEDIATE;
374 int exp_leaves = how & AUTOFS_EXP_LEAVES;
97e7449a
IK
375 struct autofs_info *ino;
376 unsigned int ino_count;
1da177e4 377
c0ba7e51 378 if (!root)
1da177e4
LT
379 return NULL;
380
381 now = jiffies;
382 timeout = sbi->exp_timeout;
383
2fd6b7f5 384 dentry = NULL;
d4a85e35 385 while ((dentry = get_next_positive_subdir(dentry, root))) {
97e7449a
IK
386 spin_lock(&sbi->fs_lock);
387 ino = autofs4_dentry_ino(dentry);
b5b80177
IK
388 /* No point expiring a pending mount */
389 if (ino->flags & AUTOFS_INF_PENDING)
3c319985 390 goto next;
97e7449a 391
3a15e2ab
IK
392 /*
393 * Case 1: (i) indirect mount or top level pseudo direct mount
394 * (autofs-4.1).
395 * (ii) indirect mount with offset mount, check the "/"
396 * offset (autofs-5.0+).
397 */
1da177e4
LT
398 if (d_mountpoint(dentry)) {
399 DPRINTK("checking mountpoint %p %.*s",
400 dentry, (int)dentry->d_name.len, dentry->d_name.name);
401
e0a7aae9
IK
402 /* Can we umount this guy */
403 if (autofs4_mount_busy(mnt, dentry))
1da177e4
LT
404 goto next;
405
e0a7aae9
IK
406 /* Can we expire this guy */
407 if (autofs4_can_expire(dentry, timeout, do_now)) {
1da177e4 408 expired = dentry;
afec570c 409 goto found;
1da177e4
LT
410 }
411 goto next;
412 }
413
1f5f2c30 414 if (simple_empty(dentry))
1da177e4
LT
415 goto next;
416
417 /* Case 2: tree mount, expire iff entire tree is not busy */
418 if (!exp_leaves) {
97e7449a
IK
419 /* Path walk currently on this dentry? */
420 ino_count = atomic_read(&ino->count) + 1;
b7ab39f6 421 if (dentry->d_count > ino_count)
97e7449a 422 goto next;
3a9720ce 423
97e7449a 424 if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) {
3a9720ce 425 expired = dentry;
afec570c 426 goto found;
1da177e4 427 }
3a15e2ab
IK
428 /*
429 * Case 3: pseudo direct mount, expire individual leaves
430 * (autofs-4.1).
431 */
1da177e4 432 } else {
97e7449a
IK
433 /* Path walk currently on this dentry? */
434 ino_count = atomic_read(&ino->count) + 1;
b7ab39f6 435 if (dentry->d_count > ino_count)
97e7449a
IK
436 goto next;
437
1da177e4
LT
438 expired = autofs4_check_leaves(mnt, dentry, timeout, do_now);
439 if (expired) {
440 dput(dentry);
afec570c 441 goto found;
1da177e4
LT
442 }
443 }
444next:
97e7449a 445 spin_unlock(&sbi->fs_lock);
1da177e4 446 }
1da177e4 447 return NULL;
afec570c
IK
448
449found:
450 DPRINTK("returning %p %.*s",
451 expired, (int)expired->d_name.len, expired->d_name.name);
97e7449a
IK
452 ino = autofs4_dentry_ino(expired);
453 ino->flags |= AUTOFS_INF_EXPIRING;
6e60a9ab 454 init_completion(&ino->expire_complete);
97e7449a 455 spin_unlock(&sbi->fs_lock);
e7854723 456 spin_lock(&sbi->lookup_lock);
2fd6b7f5
NP
457 spin_lock(&expired->d_parent->d_lock);
458 spin_lock_nested(&expired->d_lock, DENTRY_D_LOCK_NESTED);
afec570c 459 list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child);
2fd6b7f5
NP
460 spin_unlock(&expired->d_lock);
461 spin_unlock(&expired->d_parent->d_lock);
e7854723 462 spin_unlock(&sbi->lookup_lock);
afec570c 463 return expired;
1da177e4
LT
464}
465
06a35985
IK
466int autofs4_expire_wait(struct dentry *dentry)
467{
468 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
469 struct autofs_info *ino = autofs4_dentry_ino(dentry);
470 int status;
471
472 /* Block on any pending expire */
473 spin_lock(&sbi->fs_lock);
474 if (ino->flags & AUTOFS_INF_EXPIRING) {
475 spin_unlock(&sbi->fs_lock);
476
477 DPRINTK("waiting for expire %p name=%.*s",
478 dentry, dentry->d_name.len, dentry->d_name.name);
479
480 status = autofs4_wait(sbi, dentry, NFY_NONE);
481 wait_for_completion(&ino->expire_complete);
482
483 DPRINTK("expire done status=%d", status);
484
4b1ae27a 485 if (d_unhashed(dentry))
06a35985
IK
486 return -EAGAIN;
487
488 return status;
489 }
490 spin_unlock(&sbi->fs_lock);
491
492 return 0;
493}
494
1da177e4
LT
495/* Perform an expiry operation */
496int autofs4_expire_run(struct super_block *sb,
497 struct vfsmount *mnt,
498 struct autofs_sb_info *sbi,
499 struct autofs_packet_expire __user *pkt_p)
500{
501 struct autofs_packet_expire pkt;
97e7449a 502 struct autofs_info *ino;
1da177e4 503 struct dentry *dentry;
97e7449a 504 int ret = 0;
1da177e4
LT
505
506 memset(&pkt,0,sizeof pkt);
507
508 pkt.hdr.proto_version = sbi->version;
509 pkt.hdr.type = autofs_ptype_expire;
510
3a15e2ab 511 if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL)
1da177e4
LT
512 return -EAGAIN;
513
514 pkt.len = dentry->d_name.len;
515 memcpy(pkt.name, dentry->d_name.name, pkt.len);
516 pkt.name[pkt.len] = '\0';
517 dput(dentry);
518
519 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
97e7449a 520 ret = -EFAULT;
1da177e4 521
97e7449a
IK
522 spin_lock(&sbi->fs_lock);
523 ino = autofs4_dentry_ino(dentry);
524 ino->flags &= ~AUTOFS_INF_EXPIRING;
6e60a9ab 525 complete_all(&ino->expire_complete);
97e7449a
IK
526 spin_unlock(&sbi->fs_lock);
527
528 return ret;
1da177e4
LT
529}
530
56fcef75
IK
531int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
532 struct autofs_sb_info *sbi, int when)
1da177e4
LT
533{
534 struct dentry *dentry;
535 int ret = -EAGAIN;
1da177e4 536
a92daf6b 537 if (autofs_type_trigger(sbi->type))
56fcef75 538 dentry = autofs4_expire_direct(sb, mnt, sbi, when);
3a15e2ab 539 else
56fcef75 540 dentry = autofs4_expire_indirect(sb, mnt, sbi, when);
3a15e2ab
IK
541
542 if (dentry) {
1f5f2c30 543 struct autofs_info *ino = autofs4_dentry_ino(dentry);
1da177e4
LT
544
545 /* This is synchronous because it makes the daemon a
546 little easier */
1da177e4 547 ret = autofs4_wait(sbi, dentry, NFY_EXPIRE);
6e60a9ab 548
97e7449a 549 spin_lock(&sbi->fs_lock);
1f5f2c30 550 ino->flags &= ~AUTOFS_INF_EXPIRING;
6e60a9ab 551 complete_all(&ino->expire_complete);
97e7449a 552 spin_unlock(&sbi->fs_lock);
1da177e4
LT
553 dput(dentry);
554 }
1f5f2c30 555
1da177e4
LT
556 return ret;
557}
558
56fcef75
IK
559/* Call repeatedly until it returns -EAGAIN, meaning there's nothing
560 more to be done */
561int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt,
562 struct autofs_sb_info *sbi, int __user *arg)
563{
564 int do_now = 0;
565
566 if (arg && get_user(do_now, arg))
567 return -EFAULT;
568
569 return autofs4_do_expire_multi(sb, mnt, sbi, do_now);
570}
571
This page took 0.662096 seconds and 5 git commands to generate.