dentry: switch the lists of children to hlist
[linux-2.6-block.git] / fs / autofs / expire.c
CommitLineData
d6910058 1// SPDX-License-Identifier: GPL-2.0-or-later
ebc921ca
IK
2/*
3 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
4 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
5 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
ebc921ca
IK
6 */
7
8#include "autofs_i.h"
9
ebc921ca
IK
10/* Check if a dentry can be expired */
11static inline int autofs_can_expire(struct dentry *dentry,
e5c85e1f 12 unsigned long timeout, unsigned int how)
ebc921ca
IK
13{
14 struct autofs_info *ino = autofs_dentry_ino(dentry);
15
16 /* dentry in the process of being deleted */
17 if (ino == NULL)
18 return 0;
19
e5c85e1f 20 if (!(how & AUTOFS_EXP_IMMEDIATE)) {
ebc921ca 21 /* Too young to die */
2fd9944f 22 if (!timeout || time_after(ino->last_used + timeout, jiffies))
ebc921ca
IK
23 return 0;
24 }
25 return 1;
26}
27
28/* Check a mount point for busyness */
cbf6898f
IK
29static int autofs_mount_busy(struct vfsmount *mnt,
30 struct dentry *dentry, unsigned int how)
ebc921ca
IK
31{
32 struct dentry *top = dentry;
33 struct path path = {.mnt = mnt, .dentry = dentry};
34 int status = 1;
35
36 pr_debug("dentry %p %pd\n", dentry, dentry);
37
38 path_get(&path);
39
40 if (!follow_down_one(&path))
41 goto done;
42
43 if (is_autofs_dentry(path.dentry)) {
44 struct autofs_sb_info *sbi = autofs_sbi(path.dentry->d_sb);
45
46 /* This is an autofs submount, we can't expire it */
47 if (autofs_type_indirect(sbi->type))
48 goto done;
49 }
50
cbf6898f
IK
51 /* Not a submount, has a forced expire been requested */
52 if (how & AUTOFS_EXP_FORCED) {
53 status = 0;
54 goto done;
55 }
56
ebc921ca
IK
57 /* Update the expiry counter if fs is busy */
58 if (!may_umount_tree(path.mnt)) {
59 struct autofs_info *ino;
60
61 ino = autofs_dentry_ino(top);
62 ino->last_used = jiffies;
63 goto done;
64 }
65
66 status = 0;
67done:
68 pr_debug("returning = %d\n", status);
69 path_put(&path);
70 return status;
71}
72
ff09297e
AV
73/* p->d_lock held */
74static struct dentry *positive_after(struct dentry *p, struct dentry *child)
75{
da549bdd 76 child = child ? d_next_sibling(child) : d_first_child(p);
ff09297e 77
da549bdd 78 hlist_for_each_entry_from(child, d_sib) {
ff09297e
AV
79 spin_lock_nested(&child->d_lock, DENTRY_D_LOCK_NESTED);
80 if (simple_positive(child)) {
81 dget_dlock(child);
82 spin_unlock(&child->d_lock);
83 return child;
84 }
85 spin_unlock(&child->d_lock);
86 }
87
88 return NULL;
89}
90
ebc921ca
IK
91/*
92 * Calculate and dget next entry in the subdirs list under root.
93 */
94static struct dentry *get_next_positive_subdir(struct dentry *prev,
95 struct dentry *root)
96{
97 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
ebc921ca
IK
98 struct dentry *q;
99
100 spin_lock(&sbi->lookup_lock);
101 spin_lock(&root->d_lock);
ff09297e 102 q = positive_after(root, prev);
ebc921ca
IK
103 spin_unlock(&root->d_lock);
104 spin_unlock(&sbi->lookup_lock);
ebc921ca 105 dput(prev);
ebc921ca
IK
106 return q;
107}
108
109/*
110 * Calculate and dget next entry in top down tree traversal.
111 */
112static struct dentry *get_next_positive_dentry(struct dentry *prev,
113 struct dentry *root)
114{
115 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
ff09297e 116 struct dentry *p = prev, *ret = NULL, *d = NULL;
ebc921ca
IK
117
118 if (prev == NULL)
119 return dget(root);
120
121 spin_lock(&sbi->lookup_lock);
ebc921ca 122 spin_lock(&p->d_lock);
ff09297e
AV
123 while (1) {
124 struct dentry *parent;
ebc921ca 125
ff09297e
AV
126 ret = positive_after(p, d);
127 if (ret || p == root)
128 break;
129 parent = p->d_parent;
ebc921ca 130 spin_unlock(&p->d_lock);
ff09297e
AV
131 spin_lock(&parent->d_lock);
132 d = p;
133 p = parent;
ebc921ca 134 }
ebc921ca
IK
135 spin_unlock(&p->d_lock);
136 spin_unlock(&sbi->lookup_lock);
ebc921ca 137 dput(prev);
ebc921ca
IK
138 return ret;
139}
140
141/*
142 * Check a direct mount point for busyness.
143 * Direct mounts have similar expiry semantics to tree mounts.
144 * The tree is not busy iff no mountpoints are busy and there are no
145 * autofs submounts.
146 */
147static int autofs_direct_busy(struct vfsmount *mnt,
148 struct dentry *top,
149 unsigned long timeout,
e5c85e1f 150 unsigned int how)
ebc921ca
IK
151{
152 pr_debug("top %p %pd\n", top, top);
153
cbf6898f
IK
154 /* Forced expire, user space handles busy mounts */
155 if (how & AUTOFS_EXP_FORCED)
156 return 0;
157
ebc921ca
IK
158 /* If it's busy update the expiry counters */
159 if (!may_umount_tree(mnt)) {
160 struct autofs_info *ino;
161
162 ino = autofs_dentry_ino(top);
163 if (ino)
164 ino->last_used = jiffies;
165 return 1;
166 }
167
168 /* Timeout of a direct mount is determined by its top dentry */
e5c85e1f 169 if (!autofs_can_expire(top, timeout, how))
ebc921ca
IK
170 return 1;
171
172 return 0;
173}
174
175/*
176 * Check a directory tree of mount points for busyness
177 * The tree is not busy iff no mountpoints are busy
178 */
179static int autofs_tree_busy(struct vfsmount *mnt,
180 struct dentry *top,
181 unsigned long timeout,
e5c85e1f 182 unsigned int how)
ebc921ca
IK
183{
184 struct autofs_info *top_ino = autofs_dentry_ino(top);
185 struct dentry *p;
186
187 pr_debug("top %p %pd\n", top, top);
188
189 /* Negative dentry - give up */
190 if (!simple_positive(top))
191 return 1;
192
193 p = NULL;
194 while ((p = get_next_positive_dentry(p, top))) {
195 pr_debug("dentry %p %pd\n", p, p);
196
197 /*
198 * Is someone visiting anywhere in the subtree ?
199 * If there's no mount we need to check the usage
200 * count for the autofs dentry.
201 * If the fs is busy update the expiry counter.
202 */
203 if (d_mountpoint(p)) {
cbf6898f 204 if (autofs_mount_busy(mnt, p, how)) {
ebc921ca
IK
205 top_ino->last_used = jiffies;
206 dput(p);
207 return 1;
208 }
209 } else {
210 struct autofs_info *ino = autofs_dentry_ino(p);
850d71ac 211 unsigned int ino_count = READ_ONCE(ino->count);
ebc921ca
IK
212
213 /* allow for dget above and top is already dgot */
214 if (p == top)
215 ino_count += 2;
216 else
217 ino_count++;
218
219 if (d_count(p) > ino_count) {
220 top_ino->last_used = jiffies;
221 dput(p);
222 return 1;
223 }
224 }
225 }
226
cbf6898f
IK
227 /* Forced expire, user space handles busy mounts */
228 if (how & AUTOFS_EXP_FORCED)
229 return 0;
230
ebc921ca 231 /* Timeout of a tree mount is ultimately determined by its top dentry */
e5c85e1f 232 if (!autofs_can_expire(top, timeout, how))
ebc921ca
IK
233 return 1;
234
235 return 0;
236}
237
238static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
239 struct dentry *parent,
240 unsigned long timeout,
e5c85e1f 241 unsigned int how)
ebc921ca
IK
242{
243 struct dentry *p;
244
245 pr_debug("parent %p %pd\n", parent, parent);
246
247 p = NULL;
248 while ((p = get_next_positive_dentry(p, parent))) {
249 pr_debug("dentry %p %pd\n", p, p);
250
251 if (d_mountpoint(p)) {
252 /* Can we umount this guy */
cbf6898f 253 if (autofs_mount_busy(mnt, p, how))
ebc921ca
IK
254 continue;
255
cbf6898f
IK
256 /* This isn't a submount so if a forced expire
257 * has been requested, user space handles busy
258 * mounts */
259 if (how & AUTOFS_EXP_FORCED)
260 return p;
261
ebc921ca 262 /* Can we expire this guy */
e5c85e1f 263 if (autofs_can_expire(p, timeout, how))
ebc921ca
IK
264 return p;
265 }
266 }
267 return NULL;
268}
269
270/* Check if we can expire a direct mount (possibly a tree) */
5d30517d
IK
271static struct dentry *autofs_expire_direct(struct super_block *sb,
272 struct vfsmount *mnt,
273 struct autofs_sb_info *sbi,
e5c85e1f 274 unsigned int how)
ebc921ca 275{
ebc921ca 276 struct dentry *root = dget(sb->s_root);
ebc921ca 277 struct autofs_info *ino;
e5c85e1f 278 unsigned long timeout;
ebc921ca
IK
279
280 if (!root)
281 return NULL;
282
ebc921ca
IK
283 timeout = sbi->exp_timeout;
284
e5c85e1f 285 if (!autofs_direct_busy(mnt, root, timeout, how)) {
ebc921ca
IK
286 spin_lock(&sbi->fs_lock);
287 ino = autofs_dentry_ino(root);
288 /* No point expiring a pending mount */
289 if (ino->flags & AUTOFS_INF_PENDING) {
290 spin_unlock(&sbi->fs_lock);
291 goto out;
292 }
293 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
294 spin_unlock(&sbi->fs_lock);
295 synchronize_rcu();
e5c85e1f 296 if (!autofs_direct_busy(mnt, root, timeout, how)) {
ebc921ca
IK
297 spin_lock(&sbi->fs_lock);
298 ino->flags |= AUTOFS_INF_EXPIRING;
299 init_completion(&ino->expire_complete);
300 spin_unlock(&sbi->fs_lock);
301 return root;
302 }
303 spin_lock(&sbi->fs_lock);
304 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
305 spin_unlock(&sbi->fs_lock);
306 }
307out:
308 dput(root);
309
310 return NULL;
311}
312
313/* Check if 'dentry' should expire, or return a nearby
314 * dentry that is suitable.
315 * If returned dentry is different from arg dentry,
316 * then a dget() reference was taken, else not.
317 */
318static struct dentry *should_expire(struct dentry *dentry,
319 struct vfsmount *mnt,
320 unsigned long timeout,
e5c85e1f 321 unsigned int how)
ebc921ca 322{
ebc921ca
IK
323 struct autofs_info *ino = autofs_dentry_ino(dentry);
324 unsigned int ino_count;
325
326 /* No point expiring a pending mount */
327 if (ino->flags & AUTOFS_INF_PENDING)
328 return NULL;
329
330 /*
331 * Case 1: (i) indirect mount or top level pseudo direct mount
332 * (autofs-4.1).
333 * (ii) indirect mount with offset mount, check the "/"
334 * offset (autofs-5.0+).
335 */
336 if (d_mountpoint(dentry)) {
337 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
338
339 /* Can we umount this guy */
cbf6898f 340 if (autofs_mount_busy(mnt, dentry, how))
ebc921ca
IK
341 return NULL;
342
cbf6898f
IK
343 /* This isn't a submount so if a forced expire
344 * has been requested, user space handles busy
345 * mounts */
346 if (how & AUTOFS_EXP_FORCED)
347 return dentry;
348
ebc921ca 349 /* Can we expire this guy */
e5c85e1f 350 if (autofs_can_expire(dentry, timeout, how))
ebc921ca
IK
351 return dentry;
352 return NULL;
353 }
354
eecf77e0 355 if (d_is_symlink(dentry)) {
ebc921ca 356 pr_debug("checking symlink %p %pd\n", dentry, dentry);
cbf6898f
IK
357
358 /* Forced expire, user space handles busy mounts */
359 if (how & AUTOFS_EXP_FORCED)
360 return dentry;
361
ebc921ca
IK
362 /*
363 * A symlink can't be "busy" in the usual sense so
364 * just check last used for expire timeout.
365 */
e5c85e1f 366 if (autofs_can_expire(dentry, timeout, how))
ebc921ca
IK
367 return dentry;
368 return NULL;
369 }
370
a4a87303 371 if (autofs_empty(ino))
ebc921ca
IK
372 return NULL;
373
374 /* Case 2: tree mount, expire iff entire tree is not busy */
e5c85e1f 375 if (!(how & AUTOFS_EXP_LEAVES)) {
cbf6898f
IK
376 /* Not a forced expire? */
377 if (!(how & AUTOFS_EXP_FORCED)) {
378 /* ref-walk currently on this dentry? */
850d71ac 379 ino_count = READ_ONCE(ino->count) + 1;
cbf6898f
IK
380 if (d_count(dentry) > ino_count)
381 return NULL;
382 }
ebc921ca 383
e5c85e1f 384 if (!autofs_tree_busy(mnt, dentry, timeout, how))
ebc921ca
IK
385 return dentry;
386 /*
387 * Case 3: pseudo direct mount, expire individual leaves
388 * (autofs-4.1).
389 */
390 } else {
ebc921ca
IK
391 struct dentry *expired;
392
cbf6898f
IK
393 /* Not a forced expire? */
394 if (!(how & AUTOFS_EXP_FORCED)) {
395 /* ref-walk currently on this dentry? */
850d71ac 396 ino_count = READ_ONCE(ino->count) + 1;
cbf6898f
IK
397 if (d_count(dentry) > ino_count)
398 return NULL;
399 }
ebc921ca 400
e5c85e1f 401 expired = autofs_check_leaves(mnt, dentry, timeout, how);
ebc921ca
IK
402 if (expired) {
403 if (expired == dentry)
404 dput(dentry);
405 return expired;
406 }
407 }
408 return NULL;
409}
410
411/*
412 * Find an eligible tree to time-out
413 * A tree is eligible if :-
414 * - it is unused by any user process
415 * - it has been unused for exp_timeout time
416 */
571bc35c
IK
417static struct dentry *autofs_expire_indirect(struct super_block *sb,
418 struct vfsmount *mnt,
419 struct autofs_sb_info *sbi,
e5c85e1f 420 unsigned int how)
ebc921ca
IK
421{
422 unsigned long timeout;
423 struct dentry *root = sb->s_root;
424 struct dentry *dentry;
425 struct dentry *expired;
426 struct dentry *found;
427 struct autofs_info *ino;
428
429 if (!root)
430 return NULL;
431
ebc921ca
IK
432 timeout = sbi->exp_timeout;
433
434 dentry = NULL;
435 while ((dentry = get_next_positive_subdir(dentry, root))) {
ebc921ca
IK
436 spin_lock(&sbi->fs_lock);
437 ino = autofs_dentry_ino(dentry);
438 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
439 spin_unlock(&sbi->fs_lock);
440 continue;
441 }
442 spin_unlock(&sbi->fs_lock);
443
e5c85e1f 444 expired = should_expire(dentry, mnt, timeout, how);
ebc921ca
IK
445 if (!expired)
446 continue;
447
448 spin_lock(&sbi->fs_lock);
449 ino = autofs_dentry_ino(expired);
450 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
451 spin_unlock(&sbi->fs_lock);
452 synchronize_rcu();
453
454 /* Make sure a reference is not taken on found if
455 * things have changed.
456 */
e5c85e1f
IK
457 how &= ~AUTOFS_EXP_LEAVES;
458 found = should_expire(expired, mnt, timeout, how);
03ad0d70
AV
459 if (found != expired) { // something has changed, continue
460 dput(found);
ebc921ca 461 goto next;
03ad0d70 462 }
ebc921ca
IK
463
464 if (expired != dentry)
465 dput(dentry);
466
467 spin_lock(&sbi->fs_lock);
468 goto found;
469next:
470 spin_lock(&sbi->fs_lock);
471 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
472 spin_unlock(&sbi->fs_lock);
473 if (expired != dentry)
474 dput(expired);
475 }
476 return NULL;
477
478found:
479 pr_debug("returning %p %pd\n", expired, expired);
480 ino->flags |= AUTOFS_INF_EXPIRING;
481 init_completion(&ino->expire_complete);
482 spin_unlock(&sbi->fs_lock);
483 return expired;
484}
485
486int autofs_expire_wait(const struct path *path, int rcu_walk)
487{
488 struct dentry *dentry = path->dentry;
489 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
490 struct autofs_info *ino = autofs_dentry_ino(dentry);
491 int status;
492 int state;
493
494 /* Block on any pending expire */
495 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
496 return 0;
497 if (rcu_walk)
498 return -ECHILD;
499
500retry:
501 spin_lock(&sbi->fs_lock);
502 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
503 if (state == AUTOFS_INF_WANT_EXPIRE) {
504 spin_unlock(&sbi->fs_lock);
505 /*
506 * Possibly being selected for expire, wait until
507 * it's selected or not.
508 */
509 schedule_timeout_uninterruptible(HZ/10);
510 goto retry;
511 }
512 if (state & AUTOFS_INF_EXPIRING) {
513 spin_unlock(&sbi->fs_lock);
514
515 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
516
517 status = autofs_wait(sbi, path, NFY_NONE);
518 wait_for_completion(&ino->expire_complete);
519
520 pr_debug("expire done status=%d\n", status);
521
522 if (d_unhashed(dentry))
523 return -EAGAIN;
524
525 return status;
526 }
527 spin_unlock(&sbi->fs_lock);
528
529 return 0;
530}
531
532/* Perform an expiry operation */
533int autofs_expire_run(struct super_block *sb,
534 struct vfsmount *mnt,
535 struct autofs_sb_info *sbi,
536 struct autofs_packet_expire __user *pkt_p)
537{
538 struct autofs_packet_expire pkt;
539 struct autofs_info *ino;
540 struct dentry *dentry;
541 int ret = 0;
542
543 memset(&pkt, 0, sizeof(pkt));
544
545 pkt.hdr.proto_version = sbi->version;
546 pkt.hdr.type = autofs_ptype_expire;
547
548 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
549 if (!dentry)
550 return -EAGAIN;
551
552 pkt.len = dentry->d_name.len;
553 memcpy(pkt.name, dentry->d_name.name, pkt.len);
554 pkt.name[pkt.len] = '\0';
ebc921ca
IK
555
556 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
557 ret = -EFAULT;
558
559 spin_lock(&sbi->fs_lock);
560 ino = autofs_dentry_ino(dentry);
561 /* avoid rapid-fire expire attempts if expiry fails */
2fd9944f 562 ino->last_used = jiffies;
ebc921ca
IK
563 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
564 complete_all(&ino->expire_complete);
565 spin_unlock(&sbi->fs_lock);
566
63ce5f55
PB
567 dput(dentry);
568
ebc921ca
IK
569 return ret;
570}
571
572int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
e5c85e1f 573 struct autofs_sb_info *sbi, unsigned int how)
ebc921ca
IK
574{
575 struct dentry *dentry;
576 int ret = -EAGAIN;
577
578 if (autofs_type_trigger(sbi->type))
e5c85e1f 579 dentry = autofs_expire_direct(sb, mnt, sbi, how);
ebc921ca 580 else
e5c85e1f 581 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
ebc921ca
IK
582
583 if (dentry) {
584 struct autofs_info *ino = autofs_dentry_ino(dentry);
585 const struct path path = { .mnt = mnt, .dentry = dentry };
586
587 /* This is synchronous because it makes the daemon a
588 * little easier
589 */
590 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
591
592 spin_lock(&sbi->fs_lock);
593 /* avoid rapid-fire expire attempts if expiry fails */
2fd9944f 594 ino->last_used = jiffies;
ebc921ca
IK
595 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
596 complete_all(&ino->expire_complete);
597 spin_unlock(&sbi->fs_lock);
598 dput(dentry);
599 }
600
601 return ret;
602}
603
604/*
605 * Call repeatedly until it returns -EAGAIN, meaning there's nothing
606 * more to be done.
607 */
608int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
609 struct autofs_sb_info *sbi, int __user *arg)
610{
e5c85e1f 611 unsigned int how = 0;
ebc921ca 612
e5c85e1f 613 if (arg && get_user(how, arg))
ebc921ca
IK
614 return -EFAULT;
615
e5c85e1f 616 return autofs_do_expire_multi(sb, mnt, sbi, how);
ebc921ca 617}