1
2
3
4
5
6
7
8#include "autofs_i.h"
9
10
11static inline int autofs_can_expire(struct dentry *dentry,
12 unsigned long timeout, unsigned int how)
13{
14 struct autofs_info *ino = autofs_dentry_ino(dentry);
15
16
17 if (ino == NULL)
18 return 0;
19
20 if (!(how & AUTOFS_EXP_IMMEDIATE)) {
21
22 if (!timeout || time_after(ino->last_used + timeout, jiffies))
23 return 0;
24 }
25 return 1;
26}
27
28
29static int autofs_mount_busy(struct vfsmount *mnt,
30 struct dentry *dentry, unsigned int how)
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
47 if (autofs_type_indirect(sbi->type))
48 goto done;
49 }
50
51
52 if (how & AUTOFS_EXP_FORCED) {
53 status = 0;
54 goto done;
55 }
56
57
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
73
74
75
76static struct dentry *get_next_positive_subdir(struct dentry *prev,
77 struct dentry *root)
78{
79 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
80 struct list_head *next;
81 struct dentry *q;
82
83 spin_lock(&sbi->lookup_lock);
84 spin_lock(&root->d_lock);
85
86 if (prev)
87 next = prev->d_child.next;
88 else {
89 prev = dget_dlock(root);
90 next = prev->d_subdirs.next;
91 }
92
93cont:
94 if (next == &root->d_subdirs) {
95 spin_unlock(&root->d_lock);
96 spin_unlock(&sbi->lookup_lock);
97 dput(prev);
98 return NULL;
99 }
100
101 q = list_entry(next, struct dentry, d_child);
102
103 spin_lock_nested(&q->d_lock, DENTRY_D_LOCK_NESTED);
104
105 if (!d_count(q) || !simple_positive(q)) {
106 spin_unlock(&q->d_lock);
107 next = q->d_child.next;
108 goto cont;
109 }
110 dget_dlock(q);
111 spin_unlock(&q->d_lock);
112 spin_unlock(&root->d_lock);
113 spin_unlock(&sbi->lookup_lock);
114
115 dput(prev);
116
117 return q;
118}
119
120
121
122
123static struct dentry *get_next_positive_dentry(struct dentry *prev,
124 struct dentry *root)
125{
126 struct autofs_sb_info *sbi = autofs_sbi(root->d_sb);
127 struct list_head *next;
128 struct dentry *p, *ret;
129
130 if (prev == NULL)
131 return dget(root);
132
133 spin_lock(&sbi->lookup_lock);
134relock:
135 p = prev;
136 spin_lock(&p->d_lock);
137again:
138 next = p->d_subdirs.next;
139 if (next == &p->d_subdirs) {
140 while (1) {
141 struct dentry *parent;
142
143 if (p == root) {
144 spin_unlock(&p->d_lock);
145 spin_unlock(&sbi->lookup_lock);
146 dput(prev);
147 return NULL;
148 }
149
150 parent = p->d_parent;
151 if (!spin_trylock(&parent->d_lock)) {
152 spin_unlock(&p->d_lock);
153 cpu_relax();
154 goto relock;
155 }
156 spin_unlock(&p->d_lock);
157 next = p->d_child.next;
158 p = parent;
159 if (next != &parent->d_subdirs)
160 break;
161 }
162 }
163 ret = list_entry(next, struct dentry, d_child);
164
165 spin_lock_nested(&ret->d_lock, DENTRY_D_LOCK_NESTED);
166
167 if (!simple_positive(ret)) {
168 spin_unlock(&p->d_lock);
169 lock_set_subclass(&ret->d_lock.dep_map, 0, _RET_IP_);
170 p = ret;
171 goto again;
172 }
173 dget_dlock(ret);
174 spin_unlock(&ret->d_lock);
175 spin_unlock(&p->d_lock);
176 spin_unlock(&sbi->lookup_lock);
177
178 dput(prev);
179
180 return ret;
181}
182
183
184
185
186
187
188
189static int autofs_direct_busy(struct vfsmount *mnt,
190 struct dentry *top,
191 unsigned long timeout,
192 unsigned int how)
193{
194 pr_debug("top %p %pd\n", top, top);
195
196
197 if (how & AUTOFS_EXP_FORCED)
198 return 0;
199
200
201 if (!may_umount_tree(mnt)) {
202 struct autofs_info *ino;
203
204 ino = autofs_dentry_ino(top);
205 if (ino)
206 ino->last_used = jiffies;
207 return 1;
208 }
209
210
211 if (!autofs_can_expire(top, timeout, how))
212 return 1;
213
214 return 0;
215}
216
217
218
219
220
221static int autofs_tree_busy(struct vfsmount *mnt,
222 struct dentry *top,
223 unsigned long timeout,
224 unsigned int how)
225{
226 struct autofs_info *top_ino = autofs_dentry_ino(top);
227 struct dentry *p;
228
229 pr_debug("top %p %pd\n", top, top);
230
231
232 if (!simple_positive(top))
233 return 1;
234
235 p = NULL;
236 while ((p = get_next_positive_dentry(p, top))) {
237 pr_debug("dentry %p %pd\n", p, p);
238
239
240
241
242
243
244
245 if (d_mountpoint(p)) {
246 if (autofs_mount_busy(mnt, p, how)) {
247 top_ino->last_used = jiffies;
248 dput(p);
249 return 1;
250 }
251 } else {
252 struct autofs_info *ino = autofs_dentry_ino(p);
253 unsigned int ino_count = atomic_read(&ino->count);
254
255
256 if (p == top)
257 ino_count += 2;
258 else
259 ino_count++;
260
261 if (d_count(p) > ino_count) {
262 top_ino->last_used = jiffies;
263 dput(p);
264 return 1;
265 }
266 }
267 }
268
269
270 if (how & AUTOFS_EXP_FORCED)
271 return 0;
272
273
274 if (!autofs_can_expire(top, timeout, how))
275 return 1;
276
277 return 0;
278}
279
280static struct dentry *autofs_check_leaves(struct vfsmount *mnt,
281 struct dentry *parent,
282 unsigned long timeout,
283 unsigned int how)
284{
285 struct dentry *p;
286
287 pr_debug("parent %p %pd\n", parent, parent);
288
289 p = NULL;
290 while ((p = get_next_positive_dentry(p, parent))) {
291 pr_debug("dentry %p %pd\n", p, p);
292
293 if (d_mountpoint(p)) {
294
295 if (autofs_mount_busy(mnt, p, how))
296 continue;
297
298
299
300
301 if (how & AUTOFS_EXP_FORCED)
302 return p;
303
304
305 if (autofs_can_expire(p, timeout, how))
306 return p;
307 }
308 }
309 return NULL;
310}
311
312
313static struct dentry *autofs_expire_direct(struct super_block *sb,
314 struct vfsmount *mnt,
315 struct autofs_sb_info *sbi,
316 unsigned int how)
317{
318 struct dentry *root = dget(sb->s_root);
319 struct autofs_info *ino;
320 unsigned long timeout;
321
322 if (!root)
323 return NULL;
324
325 timeout = sbi->exp_timeout;
326
327 if (!autofs_direct_busy(mnt, root, timeout, how)) {
328 spin_lock(&sbi->fs_lock);
329 ino = autofs_dentry_ino(root);
330
331 if (ino->flags & AUTOFS_INF_PENDING) {
332 spin_unlock(&sbi->fs_lock);
333 goto out;
334 }
335 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
336 spin_unlock(&sbi->fs_lock);
337 synchronize_rcu();
338 if (!autofs_direct_busy(mnt, root, timeout, how)) {
339 spin_lock(&sbi->fs_lock);
340 ino->flags |= AUTOFS_INF_EXPIRING;
341 init_completion(&ino->expire_complete);
342 spin_unlock(&sbi->fs_lock);
343 return root;
344 }
345 spin_lock(&sbi->fs_lock);
346 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
347 spin_unlock(&sbi->fs_lock);
348 }
349out:
350 dput(root);
351
352 return NULL;
353}
354
355
356
357
358
359
360static struct dentry *should_expire(struct dentry *dentry,
361 struct vfsmount *mnt,
362 unsigned long timeout,
363 unsigned int how)
364{
365 struct autofs_info *ino = autofs_dentry_ino(dentry);
366 unsigned int ino_count;
367
368
369 if (ino->flags & AUTOFS_INF_PENDING)
370 return NULL;
371
372
373
374
375
376
377
378 if (d_mountpoint(dentry)) {
379 pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
380
381
382 if (autofs_mount_busy(mnt, dentry, how))
383 return NULL;
384
385
386
387
388 if (how & AUTOFS_EXP_FORCED)
389 return dentry;
390
391
392 if (autofs_can_expire(dentry, timeout, how))
393 return dentry;
394 return NULL;
395 }
396
397 if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
398 pr_debug("checking symlink %p %pd\n", dentry, dentry);
399
400
401 if (how & AUTOFS_EXP_FORCED)
402 return dentry;
403
404
405
406
407
408 if (autofs_can_expire(dentry, timeout, how))
409 return dentry;
410 return NULL;
411 }
412
413 if (simple_empty(dentry))
414 return NULL;
415
416
417 if (!(how & AUTOFS_EXP_LEAVES)) {
418
419 if (!(how & AUTOFS_EXP_FORCED)) {
420
421 ino_count = atomic_read(&ino->count) + 1;
422 if (d_count(dentry) > ino_count)
423 return NULL;
424 }
425
426 if (!autofs_tree_busy(mnt, dentry, timeout, how))
427 return dentry;
428
429
430
431
432 } else {
433 struct dentry *expired;
434
435
436 if (!(how & AUTOFS_EXP_FORCED)) {
437
438 ino_count = atomic_read(&ino->count) + 1;
439 if (d_count(dentry) > ino_count)
440 return NULL;
441 }
442
443 expired = autofs_check_leaves(mnt, dentry, timeout, how);
444 if (expired) {
445 if (expired == dentry)
446 dput(dentry);
447 return expired;
448 }
449 }
450 return NULL;
451}
452
453
454
455
456
457
458
459static struct dentry *autofs_expire_indirect(struct super_block *sb,
460 struct vfsmount *mnt,
461 struct autofs_sb_info *sbi,
462 unsigned int how)
463{
464 unsigned long timeout;
465 struct dentry *root = sb->s_root;
466 struct dentry *dentry;
467 struct dentry *expired;
468 struct dentry *found;
469 struct autofs_info *ino;
470
471 if (!root)
472 return NULL;
473
474 timeout = sbi->exp_timeout;
475
476 dentry = NULL;
477 while ((dentry = get_next_positive_subdir(dentry, root))) {
478 spin_lock(&sbi->fs_lock);
479 ino = autofs_dentry_ino(dentry);
480 if (ino->flags & AUTOFS_INF_WANT_EXPIRE) {
481 spin_unlock(&sbi->fs_lock);
482 continue;
483 }
484 spin_unlock(&sbi->fs_lock);
485
486 expired = should_expire(dentry, mnt, timeout, how);
487 if (!expired)
488 continue;
489
490 spin_lock(&sbi->fs_lock);
491 ino = autofs_dentry_ino(expired);
492 ino->flags |= AUTOFS_INF_WANT_EXPIRE;
493 spin_unlock(&sbi->fs_lock);
494 synchronize_rcu();
495
496
497
498
499 how &= ~AUTOFS_EXP_LEAVES;
500 found = should_expire(expired, mnt, timeout, how);
501 if (!found || found != expired)
502
503 goto next;
504
505 if (expired != dentry)
506 dput(dentry);
507
508 spin_lock(&sbi->fs_lock);
509 goto found;
510next:
511 spin_lock(&sbi->fs_lock);
512 ino->flags &= ~AUTOFS_INF_WANT_EXPIRE;
513 spin_unlock(&sbi->fs_lock);
514 if (expired != dentry)
515 dput(expired);
516 }
517 return NULL;
518
519found:
520 pr_debug("returning %p %pd\n", expired, expired);
521 ino->flags |= AUTOFS_INF_EXPIRING;
522 init_completion(&ino->expire_complete);
523 spin_unlock(&sbi->fs_lock);
524 return expired;
525}
526
527int autofs_expire_wait(const struct path *path, int rcu_walk)
528{
529 struct dentry *dentry = path->dentry;
530 struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
531 struct autofs_info *ino = autofs_dentry_ino(dentry);
532 int status;
533 int state;
534
535
536 if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
537 return 0;
538 if (rcu_walk)
539 return -ECHILD;
540
541retry:
542 spin_lock(&sbi->fs_lock);
543 state = ino->flags & (AUTOFS_INF_WANT_EXPIRE | AUTOFS_INF_EXPIRING);
544 if (state == AUTOFS_INF_WANT_EXPIRE) {
545 spin_unlock(&sbi->fs_lock);
546
547
548
549
550 schedule_timeout_uninterruptible(HZ/10);
551 goto retry;
552 }
553 if (state & AUTOFS_INF_EXPIRING) {
554 spin_unlock(&sbi->fs_lock);
555
556 pr_debug("waiting for expire %p name=%pd\n", dentry, dentry);
557
558 status = autofs_wait(sbi, path, NFY_NONE);
559 wait_for_completion(&ino->expire_complete);
560
561 pr_debug("expire done status=%d\n", status);
562
563 if (d_unhashed(dentry))
564 return -EAGAIN;
565
566 return status;
567 }
568 spin_unlock(&sbi->fs_lock);
569
570 return 0;
571}
572
573
574int autofs_expire_run(struct super_block *sb,
575 struct vfsmount *mnt,
576 struct autofs_sb_info *sbi,
577 struct autofs_packet_expire __user *pkt_p)
578{
579 struct autofs_packet_expire pkt;
580 struct autofs_info *ino;
581 struct dentry *dentry;
582 int ret = 0;
583
584 memset(&pkt, 0, sizeof(pkt));
585
586 pkt.hdr.proto_version = sbi->version;
587 pkt.hdr.type = autofs_ptype_expire;
588
589 dentry = autofs_expire_indirect(sb, mnt, sbi, 0);
590 if (!dentry)
591 return -EAGAIN;
592
593 pkt.len = dentry->d_name.len;
594 memcpy(pkt.name, dentry->d_name.name, pkt.len);
595 pkt.name[pkt.len] = '\0';
596
597 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
598 ret = -EFAULT;
599
600 spin_lock(&sbi->fs_lock);
601 ino = autofs_dentry_ino(dentry);
602
603 ino->last_used = jiffies;
604 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
605 complete_all(&ino->expire_complete);
606 spin_unlock(&sbi->fs_lock);
607
608 dput(dentry);
609
610 return ret;
611}
612
613int autofs_do_expire_multi(struct super_block *sb, struct vfsmount *mnt,
614 struct autofs_sb_info *sbi, unsigned int how)
615{
616 struct dentry *dentry;
617 int ret = -EAGAIN;
618
619 if (autofs_type_trigger(sbi->type))
620 dentry = autofs_expire_direct(sb, mnt, sbi, how);
621 else
622 dentry = autofs_expire_indirect(sb, mnt, sbi, how);
623
624 if (dentry) {
625 struct autofs_info *ino = autofs_dentry_ino(dentry);
626 const struct path path = { .mnt = mnt, .dentry = dentry };
627
628
629
630
631 ret = autofs_wait(sbi, &path, NFY_EXPIRE);
632
633 spin_lock(&sbi->fs_lock);
634
635 ino->last_used = jiffies;
636 ino->flags &= ~(AUTOFS_INF_EXPIRING|AUTOFS_INF_WANT_EXPIRE);
637 complete_all(&ino->expire_complete);
638 spin_unlock(&sbi->fs_lock);
639 dput(dentry);
640 }
641
642 return ret;
643}
644
645
646
647
648
649int autofs_expire_multi(struct super_block *sb, struct vfsmount *mnt,
650 struct autofs_sb_info *sbi, int __user *arg)
651{
652 unsigned int how = 0;
653
654 if (arg && get_user(how, arg))
655 return -EFAULT;
656
657 return autofs_do_expire_multi(sb, mnt, sbi, how);
658}
659