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