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