xref: /linux/fs/autofs/expire.c (revision b7019ac550eb3916f34d79db583e9b7ea2524afa)
1 // SPDX-License-Identifier: GPL-2.0-or-later
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>
6  */
7 
8 #include "autofs_i.h"
9 
10 /* Check if a dentry can be expired */
11 static 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 	/* dentry in the process of being deleted */
17 	if (ino == NULL)
18 		return 0;
19 
20 	if (!(how & AUTOFS_EXP_IMMEDIATE)) {
21 		/* Too young to die */
22 		if (!timeout || time_after(ino->last_used + timeout, jiffies))
23 			return 0;
24 	}
25 	return 1;
26 }
27 
28 /* Check a mount point for busyness */
29 static 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 		/* This is an autofs submount, we can't expire it */
47 		if (autofs_type_indirect(sbi->type))
48 			goto done;
49 	}
50 
51 	/* Not a submount, has a forced expire been requested */
52 	if (how & AUTOFS_EXP_FORCED) {
53 		status = 0;
54 		goto done;
55 	}
56 
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;
67 done:
68 	pr_debug("returning = %d\n", status);
69 	path_put(&path);
70 	return status;
71 }
72 
73 /*
74  * Calculate and dget next entry in the subdirs list under root.
75  */
76 static 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 
93 cont:
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 	/* Already gone or negative dentry (under construction) - try next */
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  * Calculate and dget next entry in top down tree traversal.
122  */
123 static 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);
134 relock:
135 	p = prev;
136 	spin_lock(&p->d_lock);
137 again:
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 	/* Negative dentry - try next */
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  * Check a direct mount point for busyness.
185  * Direct mounts have similar expiry semantics to tree mounts.
186  * The tree is not busy iff no mountpoints are busy and there are no
187  * autofs submounts.
188  */
189 static 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 	/* Forced expire, user space handles busy mounts */
197 	if (how & AUTOFS_EXP_FORCED)
198 		return 0;
199 
200 	/* If it's busy update the expiry counters */
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 	/* Timeout of a direct mount is determined by its top dentry */
211 	if (!autofs_can_expire(top, timeout, how))
212 		return 1;
213 
214 	return 0;
215 }
216 
217 /*
218  * Check a directory tree of mount points for busyness
219  * The tree is not busy iff no mountpoints are busy
220  */
221 static 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 	/* Negative dentry - give up */
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 		 * Is someone visiting anywhere in the subtree ?
241 		 * If there's no mount we need to check the usage
242 		 * count for the autofs dentry.
243 		 * If the fs is busy update the expiry counter.
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 			/* allow for dget above and top is already dgot */
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 	/* Forced expire, user space handles busy mounts */
270 	if (how & AUTOFS_EXP_FORCED)
271 		return 0;
272 
273 	/* Timeout of a tree mount is ultimately determined by its top dentry */
274 	if (!autofs_can_expire(top, timeout, how))
275 		return 1;
276 
277 	return 0;
278 }
279 
280 static 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 			/* Can we umount this guy */
295 			if (autofs_mount_busy(mnt, p, how))
296 				continue;
297 
298 			/* This isn't a submount so if a forced expire
299 			 * has been requested, user space handles busy
300 			 * mounts */
301 			if (how & AUTOFS_EXP_FORCED)
302 				return p;
303 
304 			/* Can we expire this guy */
305 			if (autofs_can_expire(p, timeout, how))
306 				return p;
307 		}
308 	}
309 	return NULL;
310 }
311 
312 /* Check if we can expire a direct mount (possibly a tree) */
313 static 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 		/* No point expiring a pending mount */
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 	}
349 out:
350 	dput(root);
351 
352 	return NULL;
353 }
354 
355 /* Check if 'dentry' should expire, or return a nearby
356  * dentry that is suitable.
357  * If returned dentry is different from arg dentry,
358  * then a dget() reference was taken, else not.
359  */
360 static 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 	/* No point expiring a pending mount */
369 	if (ino->flags & AUTOFS_INF_PENDING)
370 		return NULL;
371 
372 	/*
373 	 * Case 1: (i) indirect mount or top level pseudo direct mount
374 	 *	   (autofs-4.1).
375 	 *	   (ii) indirect mount with offset mount, check the "/"
376 	 *	   offset (autofs-5.0+).
377 	 */
378 	if (d_mountpoint(dentry)) {
379 		pr_debug("checking mountpoint %p %pd\n", dentry, dentry);
380 
381 		/* Can we umount this guy */
382 		if (autofs_mount_busy(mnt, dentry, how))
383 			return NULL;
384 
385 		/* This isn't a submount so if a forced expire
386 		 * has been requested, user space handles busy
387 		 * mounts */
388 		if (how & AUTOFS_EXP_FORCED)
389 			return dentry;
390 
391 		/* Can we expire this guy */
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 		/* Forced expire, user space handles busy mounts */
401 		if (how & AUTOFS_EXP_FORCED)
402 			return dentry;
403 
404 		/*
405 		 * A symlink can't be "busy" in the usual sense so
406 		 * just check last used for expire timeout.
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 	/* Case 2: tree mount, expire iff entire tree is not busy */
417 	if (!(how & AUTOFS_EXP_LEAVES)) {
418 		/* Not a forced expire? */
419 		if (!(how & AUTOFS_EXP_FORCED)) {
420 			/* ref-walk currently on this dentry? */
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 	 * Case 3: pseudo direct mount, expire individual leaves
430 	 *	   (autofs-4.1).
431 	 */
432 	} else {
433 		struct dentry *expired;
434 
435 		/* Not a forced expire? */
436 		if (!(how & AUTOFS_EXP_FORCED)) {
437 			/* ref-walk currently on this dentry? */
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  * Find an eligible tree to time-out
455  * A tree is eligible if :-
456  *  - it is unused by any user process
457  *  - it has been unused for exp_timeout time
458  */
459 static 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 		/* Make sure a reference is not taken on found if
497 		 * things have changed.
498 		 */
499 		how &= ~AUTOFS_EXP_LEAVES;
500 		found = should_expire(expired, mnt, timeout, how);
501 		if (!found || found != expired)
502 			/* Something has changed, continue */
503 			goto next;
504 
505 		if (expired != dentry)
506 			dput(dentry);
507 
508 		spin_lock(&sbi->fs_lock);
509 		goto found;
510 next:
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 
519 found:
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 
527 int 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 	/* Block on any pending expire */
536 	if (!(ino->flags & AUTOFS_INF_WANT_EXPIRE))
537 		return 0;
538 	if (rcu_walk)
539 		return -ECHILD;
540 
541 retry:
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 		 * Possibly being selected for expire, wait until
548 		 * it's selected or not.
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 /* Perform an expiry operation */
574 int 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 	/* avoid rapid-fire expire attempts if expiry fails */
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 
613 int 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 		/* This is synchronous because it makes the daemon a
629 		 * little easier
630 		 */
631 		ret = autofs_wait(sbi, &path, NFY_EXPIRE);
632 
633 		spin_lock(&sbi->fs_lock);
634 		/* avoid rapid-fire expire attempts if expiry fails */
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  * Call repeatedly until it returns -EAGAIN, meaning there's nothing
647  * more to be done.
648  */
649 int 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