Commit | Line | Data |
---|---|---|
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 | ||
17 | static unsigned long now; | |
18 | ||
1f5f2c30 | 19 | /* Check if a dentry can be expired */ |
1da177e4 LT |
20 | static 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 */ |
48 | static 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 | ||
bc9c4068 | 59 | if (!follow_down(&mnt, &dentry)) |
1da177e4 LT |
60 | goto done; |
61 | ||
bc9c4068 IK |
62 | if (is_autofs4_dentry(dentry)) { |
63 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
64 | ||
65 | /* This is an autofs submount, we can't expire it */ | |
a92daf6b | 66 | if (autofs_type_indirect(sbi->type)) |
bc9c4068 IK |
67 | goto done; |
68 | ||
69 | /* | |
70 | * Otherwise it's an offset mount and we need to check | |
71 | * if we can umount its mount, if there is one. | |
72 | */ | |
a8985f3a IK |
73 | if (!d_mountpoint(dentry)) { |
74 | status = 0; | |
bc9c4068 | 75 | goto done; |
a8985f3a | 76 | } |
bc9c4068 | 77 | } |
1da177e4 | 78 | |
e0a7aae9 | 79 | /* Update the expiry counter if fs is busy */ |
e3474a8e | 80 | if (!may_umount_tree(mnt)) { |
e0a7aae9 IK |
81 | struct autofs_info *ino = autofs4_dentry_ino(top); |
82 | ino->last_used = jiffies; | |
83 | goto done; | |
84 | } | |
85 | ||
86 | status = 0; | |
1da177e4 LT |
87 | done: |
88 | DPRINTK("returning = %d", status); | |
1da177e4 | 89 | dput(dentry); |
868eb7a8 | 90 | mntput(mnt); |
1da177e4 LT |
91 | return status; |
92 | } | |
93 | ||
1ce12bad IK |
94 | /* |
95 | * Calculate next entry in top down tree traversal. | |
96 | * From next_mnt in namespace.c - elegant. | |
97 | */ | |
98 | static struct dentry *next_dentry(struct dentry *p, struct dentry *root) | |
99 | { | |
100 | struct list_head *next = p->d_subdirs.next; | |
101 | ||
102 | if (next == &p->d_subdirs) { | |
103 | while (1) { | |
104 | if (p == root) | |
105 | return NULL; | |
106 | next = p->d_u.d_child.next; | |
107 | if (next != &p->d_parent->d_subdirs) | |
108 | break; | |
109 | p = p->d_parent; | |
110 | } | |
111 | } | |
112 | return list_entry(next, struct dentry, d_u.d_child); | |
113 | } | |
114 | ||
3a15e2ab IK |
115 | /* |
116 | * Check a direct mount point for busyness. | |
117 | * Direct mounts have similar expiry semantics to tree mounts. | |
118 | * The tree is not busy iff no mountpoints are busy and there are no | |
119 | * autofs submounts. | |
120 | */ | |
121 | static int autofs4_direct_busy(struct vfsmount *mnt, | |
122 | struct dentry *top, | |
123 | unsigned long timeout, | |
124 | int do_now) | |
125 | { | |
126 | DPRINTK("top %p %.*s", | |
127 | top, (int) top->d_name.len, top->d_name.name); | |
128 | ||
3a15e2ab IK |
129 | /* If it's busy update the expiry counters */ |
130 | if (!may_umount_tree(mnt)) { | |
131 | struct autofs_info *ino = autofs4_dentry_ino(top); | |
132 | if (ino) | |
133 | ino->last_used = jiffies; | |
134 | return 1; | |
135 | } | |
136 | ||
137 | /* Timeout of a direct mount is determined by its top dentry */ | |
138 | if (!autofs4_can_expire(top, timeout, do_now)) | |
139 | return 1; | |
140 | ||
141 | return 0; | |
142 | } | |
143 | ||
1da177e4 LT |
144 | /* Check a directory tree of mount points for busyness |
145 | * The tree is not busy iff no mountpoints are busy | |
1da177e4 | 146 | */ |
1f5f2c30 IK |
147 | static int autofs4_tree_busy(struct vfsmount *mnt, |
148 | struct dentry *top, | |
149 | unsigned long timeout, | |
150 | int do_now) | |
1da177e4 | 151 | { |
e0a7aae9 | 152 | struct autofs_info *top_ino = autofs4_dentry_ino(top); |
1ce12bad | 153 | struct dentry *p; |
1da177e4 | 154 | |
1f5f2c30 | 155 | DPRINTK("top %p %.*s", |
1da177e4 LT |
156 | top, (int)top->d_name.len, top->d_name.name); |
157 | ||
158 | /* Negative dentry - give up */ | |
159 | if (!simple_positive(top)) | |
1f5f2c30 | 160 | return 1; |
1da177e4 | 161 | |
1da177e4 | 162 | spin_lock(&dcache_lock); |
1ce12bad | 163 | for (p = top; p; p = next_dentry(p, top)) { |
1da177e4 | 164 | /* Negative dentry - give up */ |
1ce12bad | 165 | if (!simple_positive(p)) |
1da177e4 | 166 | continue; |
1da177e4 LT |
167 | |
168 | DPRINTK("dentry %p %.*s", | |
1ce12bad | 169 | p, (int) p->d_name.len, p->d_name.name); |
1da177e4 | 170 | |
1ce12bad | 171 | p = dget(p); |
1da177e4 LT |
172 | spin_unlock(&dcache_lock); |
173 | ||
1aff3c8b IK |
174 | /* |
175 | * Is someone visiting anywhere in the subtree ? | |
176 | * If there's no mount we need to check the usage | |
177 | * count for the autofs dentry. | |
e0a7aae9 | 178 | * If the fs is busy update the expiry counter. |
1aff3c8b | 179 | */ |
1ce12bad | 180 | if (d_mountpoint(p)) { |
1ce12bad | 181 | if (autofs4_mount_busy(mnt, p)) { |
e0a7aae9 | 182 | top_ino->last_used = jiffies; |
1ce12bad | 183 | dput(p); |
1f5f2c30 | 184 | return 1; |
1da177e4 | 185 | } |
1aff3c8b | 186 | } else { |
e0a7aae9 | 187 | struct autofs_info *ino = autofs4_dentry_ino(p); |
1aff3c8b IK |
188 | unsigned int ino_count = atomic_read(&ino->count); |
189 | ||
f9022f66 IK |
190 | /* |
191 | * Clean stale dentries below that have not been | |
192 | * invalidated after a mount fail during lookup | |
193 | */ | |
194 | d_invalidate(p); | |
195 | ||
1aff3c8b IK |
196 | /* allow for dget above and top is already dgot */ |
197 | if (p == top) | |
198 | ino_count += 2; | |
199 | else | |
200 | ino_count++; | |
201 | ||
202 | if (atomic_read(&p->d_count) > ino_count) { | |
e0a7aae9 | 203 | top_ino->last_used = jiffies; |
1aff3c8b IK |
204 | dput(p); |
205 | return 1; | |
206 | } | |
1da177e4 | 207 | } |
1ce12bad | 208 | dput(p); |
1da177e4 | 209 | spin_lock(&dcache_lock); |
1da177e4 LT |
210 | } |
211 | spin_unlock(&dcache_lock); | |
1aff3c8b IK |
212 | |
213 | /* Timeout of a tree mount is ultimately determined by its top dentry */ | |
214 | if (!autofs4_can_expire(top, timeout, do_now)) | |
215 | return 1; | |
216 | ||
1f5f2c30 | 217 | return 0; |
1da177e4 LT |
218 | } |
219 | ||
220 | static struct dentry *autofs4_check_leaves(struct vfsmount *mnt, | |
221 | struct dentry *parent, | |
222 | unsigned long timeout, | |
223 | int do_now) | |
224 | { | |
1ce12bad | 225 | struct dentry *p; |
1da177e4 LT |
226 | |
227 | DPRINTK("parent %p %.*s", | |
228 | parent, (int)parent->d_name.len, parent->d_name.name); | |
229 | ||
230 | spin_lock(&dcache_lock); | |
1ce12bad | 231 | for (p = parent; p; p = next_dentry(p, parent)) { |
1da177e4 | 232 | /* Negative dentry - give up */ |
1ce12bad | 233 | if (!simple_positive(p)) |
1da177e4 | 234 | continue; |
1da177e4 LT |
235 | |
236 | DPRINTK("dentry %p %.*s", | |
1ce12bad | 237 | p, (int) p->d_name.len, p->d_name.name); |
1da177e4 | 238 | |
1ce12bad | 239 | p = dget(p); |
1da177e4 LT |
240 | spin_unlock(&dcache_lock); |
241 | ||
1ce12bad | 242 | if (d_mountpoint(p)) { |
e0a7aae9 IK |
243 | /* Can we umount this guy */ |
244 | if (autofs4_mount_busy(mnt, p)) | |
1da177e4 LT |
245 | goto cont; |
246 | ||
e0a7aae9 IK |
247 | /* Can we expire this guy */ |
248 | if (autofs4_can_expire(p, timeout, do_now)) | |
1ce12bad | 249 | return p; |
1da177e4 LT |
250 | } |
251 | cont: | |
1ce12bad | 252 | dput(p); |
1da177e4 | 253 | spin_lock(&dcache_lock); |
1da177e4 LT |
254 | } |
255 | spin_unlock(&dcache_lock); | |
1da177e4 LT |
256 | return NULL; |
257 | } | |
258 | ||
3a15e2ab | 259 | /* Check if we can expire a direct mount (possibly a tree) */ |
8d7b48e0 IK |
260 | struct dentry *autofs4_expire_direct(struct super_block *sb, |
261 | struct vfsmount *mnt, | |
262 | struct autofs_sb_info *sbi, | |
263 | int how) | |
3a15e2ab IK |
264 | { |
265 | unsigned long timeout; | |
266 | struct dentry *root = dget(sb->s_root); | |
267 | int do_now = how & AUTOFS_EXP_IMMEDIATE; | |
268 | ||
c0ba7e51 | 269 | if (!root) |
3a15e2ab IK |
270 | return NULL; |
271 | ||
272 | now = jiffies; | |
273 | timeout = sbi->exp_timeout; | |
274 | ||
3a15e2ab IK |
275 | spin_lock(&sbi->fs_lock); |
276 | if (!autofs4_direct_busy(mnt, root, timeout, do_now)) { | |
277 | struct autofs_info *ino = autofs4_dentry_ino(root); | |
6e60a9ab IK |
278 | if (d_mountpoint(root)) { |
279 | ino->flags |= AUTOFS_INF_MOUNTPOINT; | |
280 | root->d_mounted--; | |
281 | } | |
3a15e2ab | 282 | ino->flags |= AUTOFS_INF_EXPIRING; |
6e60a9ab | 283 | init_completion(&ino->expire_complete); |
3a15e2ab IK |
284 | spin_unlock(&sbi->fs_lock); |
285 | return root; | |
286 | } | |
287 | spin_unlock(&sbi->fs_lock); | |
288 | dput(root); | |
289 | ||
290 | return NULL; | |
291 | } | |
292 | ||
1da177e4 LT |
293 | /* |
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 | |
298 | */ | |
8d7b48e0 IK |
299 | struct dentry *autofs4_expire_indirect(struct super_block *sb, |
300 | struct vfsmount *mnt, | |
301 | struct autofs_sb_info *sbi, | |
302 | int how) | |
1da177e4 LT |
303 | { |
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; | |
97e7449a IK |
310 | struct autofs_info *ino; |
311 | unsigned int ino_count; | |
1da177e4 | 312 | |
c0ba7e51 | 313 | if (!root) |
1da177e4 LT |
314 | return NULL; |
315 | ||
316 | now = jiffies; | |
317 | timeout = sbi->exp_timeout; | |
318 | ||
319 | spin_lock(&dcache_lock); | |
320 | next = root->d_subdirs.next; | |
321 | ||
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 ) { | |
5160ee6f | 325 | struct dentry *dentry = list_entry(next, struct dentry, d_u.d_child); |
1da177e4 LT |
326 | |
327 | /* Negative dentry - give up */ | |
1f5f2c30 | 328 | if (!simple_positive(dentry)) { |
1da177e4 LT |
329 | next = next->next; |
330 | continue; | |
331 | } | |
332 | ||
333 | dentry = dget(dentry); | |
334 | spin_unlock(&dcache_lock); | |
335 | ||
97e7449a IK |
336 | spin_lock(&sbi->fs_lock); |
337 | ino = autofs4_dentry_ino(dentry); | |
338 | ||
3a15e2ab IK |
339 | /* |
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+). | |
344 | */ | |
1da177e4 LT |
345 | if (d_mountpoint(dentry)) { |
346 | DPRINTK("checking mountpoint %p %.*s", | |
347 | dentry, (int)dentry->d_name.len, dentry->d_name.name); | |
348 | ||
97e7449a IK |
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; | |
353 | ||
e0a7aae9 IK |
354 | /* Can we umount this guy */ |
355 | if (autofs4_mount_busy(mnt, dentry)) | |
1da177e4 LT |
356 | goto next; |
357 | ||
e0a7aae9 IK |
358 | /* Can we expire this guy */ |
359 | if (autofs4_can_expire(dentry, timeout, do_now)) { | |
1da177e4 | 360 | expired = dentry; |
afec570c | 361 | goto found; |
1da177e4 LT |
362 | } |
363 | goto next; | |
364 | } | |
365 | ||
1f5f2c30 | 366 | if (simple_empty(dentry)) |
1da177e4 LT |
367 | goto next; |
368 | ||
369 | /* Case 2: tree mount, expire iff entire tree is not busy */ | |
370 | if (!exp_leaves) { | |
97e7449a IK |
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; | |
3a9720ce | 375 | |
97e7449a | 376 | if (!autofs4_tree_busy(mnt, dentry, timeout, do_now)) { |
3a9720ce | 377 | expired = dentry; |
afec570c | 378 | goto found; |
1da177e4 | 379 | } |
3a15e2ab IK |
380 | /* |
381 | * Case 3: pseudo direct mount, expire individual leaves | |
382 | * (autofs-4.1). | |
383 | */ | |
1da177e4 | 384 | } else { |
97e7449a IK |
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; | |
389 | ||
1da177e4 LT |
390 | expired = autofs4_check_leaves(mnt, dentry, timeout, do_now); |
391 | if (expired) { | |
392 | dput(dentry); | |
afec570c | 393 | goto found; |
1da177e4 LT |
394 | } |
395 | } | |
396 | next: | |
97e7449a | 397 | spin_unlock(&sbi->fs_lock); |
1da177e4 LT |
398 | dput(dentry); |
399 | spin_lock(&dcache_lock); | |
400 | next = next->next; | |
401 | } | |
1da177e4 | 402 | spin_unlock(&dcache_lock); |
1da177e4 | 403 | return NULL; |
afec570c IK |
404 | |
405 | found: | |
406 | DPRINTK("returning %p %.*s", | |
407 | expired, (int)expired->d_name.len, expired->d_name.name); | |
97e7449a IK |
408 | ino = autofs4_dentry_ino(expired); |
409 | ino->flags |= AUTOFS_INF_EXPIRING; | |
6e60a9ab | 410 | init_completion(&ino->expire_complete); |
97e7449a | 411 | spin_unlock(&sbi->fs_lock); |
afec570c IK |
412 | spin_lock(&dcache_lock); |
413 | list_move(&expired->d_parent->d_subdirs, &expired->d_u.d_child); | |
414 | spin_unlock(&dcache_lock); | |
415 | return expired; | |
1da177e4 LT |
416 | } |
417 | ||
06a35985 IK |
418 | int autofs4_expire_wait(struct dentry *dentry) |
419 | { | |
420 | struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb); | |
421 | struct autofs_info *ino = autofs4_dentry_ino(dentry); | |
422 | int status; | |
423 | ||
424 | /* Block on any pending expire */ | |
425 | spin_lock(&sbi->fs_lock); | |
426 | if (ino->flags & AUTOFS_INF_EXPIRING) { | |
427 | spin_unlock(&sbi->fs_lock); | |
428 | ||
429 | DPRINTK("waiting for expire %p name=%.*s", | |
430 | dentry, dentry->d_name.len, dentry->d_name.name); | |
431 | ||
432 | status = autofs4_wait(sbi, dentry, NFY_NONE); | |
433 | wait_for_completion(&ino->expire_complete); | |
434 | ||
435 | DPRINTK("expire done status=%d", status); | |
436 | ||
437 | if (d_unhashed(dentry)) | |
438 | return -EAGAIN; | |
439 | ||
440 | return status; | |
441 | } | |
442 | spin_unlock(&sbi->fs_lock); | |
443 | ||
444 | return 0; | |
445 | } | |
446 | ||
1da177e4 LT |
447 | /* Perform an expiry operation */ |
448 | int autofs4_expire_run(struct super_block *sb, | |
449 | struct vfsmount *mnt, | |
450 | struct autofs_sb_info *sbi, | |
451 | struct autofs_packet_expire __user *pkt_p) | |
452 | { | |
453 | struct autofs_packet_expire pkt; | |
97e7449a | 454 | struct autofs_info *ino; |
1da177e4 | 455 | struct dentry *dentry; |
97e7449a | 456 | int ret = 0; |
1da177e4 LT |
457 | |
458 | memset(&pkt,0,sizeof pkt); | |
459 | ||
460 | pkt.hdr.proto_version = sbi->version; | |
461 | pkt.hdr.type = autofs_ptype_expire; | |
462 | ||
3a15e2ab | 463 | if ((dentry = autofs4_expire_indirect(sb, mnt, sbi, 0)) == NULL) |
1da177e4 LT |
464 | return -EAGAIN; |
465 | ||
466 | pkt.len = dentry->d_name.len; | |
467 | memcpy(pkt.name, dentry->d_name.name, pkt.len); | |
468 | pkt.name[pkt.len] = '\0'; | |
469 | dput(dentry); | |
470 | ||
471 | if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) ) | |
97e7449a | 472 | ret = -EFAULT; |
1da177e4 | 473 | |
97e7449a IK |
474 | spin_lock(&sbi->fs_lock); |
475 | ino = autofs4_dentry_ino(dentry); | |
476 | ino->flags &= ~AUTOFS_INF_EXPIRING; | |
6e60a9ab | 477 | complete_all(&ino->expire_complete); |
97e7449a IK |
478 | spin_unlock(&sbi->fs_lock); |
479 | ||
480 | return ret; | |
1da177e4 LT |
481 | } |
482 | ||
56fcef75 IK |
483 | int autofs4_do_expire_multi(struct super_block *sb, struct vfsmount *mnt, |
484 | struct autofs_sb_info *sbi, int when) | |
1da177e4 LT |
485 | { |
486 | struct dentry *dentry; | |
487 | int ret = -EAGAIN; | |
1da177e4 | 488 | |
a92daf6b | 489 | if (autofs_type_trigger(sbi->type)) |
56fcef75 | 490 | dentry = autofs4_expire_direct(sb, mnt, sbi, when); |
3a15e2ab | 491 | else |
56fcef75 | 492 | dentry = autofs4_expire_indirect(sb, mnt, sbi, when); |
3a15e2ab IK |
493 | |
494 | if (dentry) { | |
1f5f2c30 | 495 | struct autofs_info *ino = autofs4_dentry_ino(dentry); |
1da177e4 LT |
496 | |
497 | /* This is synchronous because it makes the daemon a | |
498 | little easier */ | |
1da177e4 | 499 | ret = autofs4_wait(sbi, dentry, NFY_EXPIRE); |
6e60a9ab | 500 | |
97e7449a | 501 | spin_lock(&sbi->fs_lock); |
6e60a9ab IK |
502 | if (ino->flags & AUTOFS_INF_MOUNTPOINT) { |
503 | sb->s_root->d_mounted++; | |
504 | ino->flags &= ~AUTOFS_INF_MOUNTPOINT; | |
505 | } | |
1f5f2c30 | 506 | ino->flags &= ~AUTOFS_INF_EXPIRING; |
6e60a9ab | 507 | complete_all(&ino->expire_complete); |
97e7449a | 508 | spin_unlock(&sbi->fs_lock); |
1da177e4 LT |
509 | dput(dentry); |
510 | } | |
1f5f2c30 | 511 | |
1da177e4 LT |
512 | return ret; |
513 | } | |
514 | ||
56fcef75 IK |
515 | /* Call repeatedly until it returns -EAGAIN, meaning there's nothing |
516 | more to be done */ | |
517 | int autofs4_expire_multi(struct super_block *sb, struct vfsmount *mnt, | |
518 | struct autofs_sb_info *sbi, int __user *arg) | |
519 | { | |
520 | int do_now = 0; | |
521 | ||
522 | if (arg && get_user(do_now, arg)) | |
523 | return -EFAULT; | |
524 | ||
525 | return autofs4_do_expire_multi(sb, mnt, sbi, do_now); | |
526 | } | |
527 |