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