xref: /openbmc/linux/fs/autofs/root.c (revision 36aa5eae)
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 <linux/capability.h>
9ebc921caSIan Kent #include <linux/compat.h>
10ebc921caSIan Kent 
11ebc921caSIan Kent #include "autofs_i.h"
12ebc921caSIan Kent 
134609e1f1SChristian Brauner static int autofs_dir_permission(struct mnt_idmap *, struct inode *, int);
147a77db95SChristian Brauner static int autofs_dir_symlink(struct mnt_idmap *, struct inode *,
15549c7297SChristian Brauner 			      struct dentry *, const char *);
16ebc921caSIan Kent static int autofs_dir_unlink(struct inode *, struct dentry *);
17ebc921caSIan Kent static int autofs_dir_rmdir(struct inode *, struct dentry *);
18c54bd91eSChristian Brauner static int autofs_dir_mkdir(struct mnt_idmap *, struct inode *,
19549c7297SChristian Brauner 			    struct dentry *, umode_t);
20ebc921caSIan Kent static long autofs_root_ioctl(struct file *, unsigned int, unsigned long);
21ebc921caSIan Kent #ifdef CONFIG_COMPAT
22ebc921caSIan Kent static long autofs_root_compat_ioctl(struct file *,
23ebc921caSIan Kent 				     unsigned int, unsigned long);
24ebc921caSIan Kent #endif
25ebc921caSIan Kent static int autofs_dir_open(struct inode *inode, struct file *file);
26ebc921caSIan Kent static struct dentry *autofs_lookup(struct inode *,
27ebc921caSIan Kent 				    struct dentry *, unsigned int);
28ebc921caSIan Kent static struct vfsmount *autofs_d_automount(struct path *);
29ebc921caSIan Kent static int autofs_d_manage(const struct path *, bool);
30ebc921caSIan Kent static void autofs_dentry_release(struct dentry *);
31ebc921caSIan Kent 
32ebc921caSIan Kent const struct file_operations autofs_root_operations = {
33ebc921caSIan Kent 	.open		= dcache_dir_open,
34ebc921caSIan Kent 	.release	= dcache_dir_close,
35ebc921caSIan Kent 	.read		= generic_read_dir,
36ebc921caSIan Kent 	.iterate_shared	= dcache_readdir,
37ebc921caSIan Kent 	.llseek		= dcache_dir_lseek,
38ebc921caSIan Kent 	.unlocked_ioctl	= autofs_root_ioctl,
39ebc921caSIan Kent #ifdef CONFIG_COMPAT
40ebc921caSIan Kent 	.compat_ioctl	= autofs_root_compat_ioctl,
41ebc921caSIan Kent #endif
42ebc921caSIan Kent };
43ebc921caSIan Kent 
44ebc921caSIan Kent const struct file_operations autofs_dir_operations = {
45ebc921caSIan Kent 	.open		= autofs_dir_open,
46ebc921caSIan Kent 	.release	= dcache_dir_close,
47ebc921caSIan Kent 	.read		= generic_read_dir,
48ebc921caSIan Kent 	.iterate_shared	= dcache_readdir,
49ebc921caSIan Kent 	.llseek		= dcache_dir_lseek,
50ebc921caSIan Kent };
51ebc921caSIan Kent 
52ebc921caSIan Kent const struct inode_operations autofs_dir_inode_operations = {
53ebc921caSIan Kent 	.lookup		= autofs_lookup,
54f71381fcSIan Kent 	.permission	= autofs_dir_permission,
55ebc921caSIan Kent 	.unlink		= autofs_dir_unlink,
56ebc921caSIan Kent 	.symlink	= autofs_dir_symlink,
57ebc921caSIan Kent 	.mkdir		= autofs_dir_mkdir,
58ebc921caSIan Kent 	.rmdir		= autofs_dir_rmdir,
59ebc921caSIan Kent };
60ebc921caSIan Kent 
61ebc921caSIan Kent const struct dentry_operations autofs_dentry_operations = {
62ebc921caSIan Kent 	.d_automount	= autofs_d_automount,
63ebc921caSIan Kent 	.d_manage	= autofs_d_manage,
64ebc921caSIan Kent 	.d_release	= autofs_dentry_release,
65ebc921caSIan Kent };
66ebc921caSIan Kent 
autofs_del_active(struct dentry * dentry)67ebc921caSIan Kent static void autofs_del_active(struct dentry *dentry)
68ebc921caSIan Kent {
69ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
70ebc921caSIan Kent 	struct autofs_info *ino;
71ebc921caSIan Kent 
72ebc921caSIan Kent 	ino = autofs_dentry_ino(dentry);
73ebc921caSIan Kent 	spin_lock(&sbi->lookup_lock);
74ebc921caSIan Kent 	list_del_init(&ino->active);
75ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
76ebc921caSIan Kent }
77ebc921caSIan Kent 
autofs_dir_open(struct inode * inode,struct file * file)78ebc921caSIan Kent static int autofs_dir_open(struct inode *inode, struct file *file)
79ebc921caSIan Kent {
80ebc921caSIan Kent 	struct dentry *dentry = file->f_path.dentry;
81ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
82a4a87303SIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
83ebc921caSIan Kent 
84ebc921caSIan Kent 	pr_debug("file=%p dentry=%p %pd\n", file, dentry, dentry);
85ebc921caSIan Kent 
86ebc921caSIan Kent 	if (autofs_oz_mode(sbi))
87ebc921caSIan Kent 		goto out;
88ebc921caSIan Kent 
89ebc921caSIan Kent 	/*
90ebc921caSIan Kent 	 * An empty directory in an autofs file system is always a
91ebc921caSIan Kent 	 * mount point. The daemon must have failed to mount this
92ebc921caSIan Kent 	 * during lookup so it doesn't exist. This can happen, for
93ebc921caSIan Kent 	 * example, if user space returns an incorrect status for a
94ebc921caSIan Kent 	 * mount request. Otherwise we're doing a readdir on the
95ebc921caSIan Kent 	 * autofs file system so just let the libfs routines handle
96ebc921caSIan Kent 	 * it.
97ebc921caSIan Kent 	 */
98ebc921caSIan Kent 	spin_lock(&sbi->lookup_lock);
99a4a87303SIan Kent 	if (!path_is_mountpoint(&file->f_path) && autofs_empty(ino)) {
100ebc921caSIan Kent 		spin_unlock(&sbi->lookup_lock);
101ebc921caSIan Kent 		return -ENOENT;
102ebc921caSIan Kent 	}
103ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
104ebc921caSIan Kent 
105ebc921caSIan Kent out:
106ebc921caSIan Kent 	return dcache_dir_open(inode, file);
107ebc921caSIan Kent }
108ebc921caSIan Kent 
autofs_dentry_release(struct dentry * de)109ebc921caSIan Kent static void autofs_dentry_release(struct dentry *de)
110ebc921caSIan Kent {
111ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(de);
112ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(de->d_sb);
113ebc921caSIan Kent 
114ebc921caSIan Kent 	pr_debug("releasing %p\n", de);
115ebc921caSIan Kent 
116ebc921caSIan Kent 	if (!ino)
117ebc921caSIan Kent 		return;
118ebc921caSIan Kent 
119ebc921caSIan Kent 	if (sbi) {
120ebc921caSIan Kent 		spin_lock(&sbi->lookup_lock);
121ebc921caSIan Kent 		if (!list_empty(&ino->active))
122ebc921caSIan Kent 			list_del(&ino->active);
123ebc921caSIan Kent 		if (!list_empty(&ino->expiring))
124ebc921caSIan Kent 			list_del(&ino->expiring);
125ebc921caSIan Kent 		spin_unlock(&sbi->lookup_lock);
126ebc921caSIan Kent 	}
127ebc921caSIan Kent 
128ebc921caSIan Kent 	autofs_free_ino(ino);
129ebc921caSIan Kent }
130ebc921caSIan Kent 
autofs_lookup_active(struct dentry * dentry)131ebc921caSIan Kent static struct dentry *autofs_lookup_active(struct dentry *dentry)
132ebc921caSIan Kent {
133ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
134ebc921caSIan Kent 	struct dentry *parent = dentry->d_parent;
135ebc921caSIan Kent 	const struct qstr *name = &dentry->d_name;
136ebc921caSIan Kent 	unsigned int len = name->len;
137ebc921caSIan Kent 	unsigned int hash = name->hash;
138ebc921caSIan Kent 	const unsigned char *str = name->name;
139ebc921caSIan Kent 	struct list_head *p, *head;
140ebc921caSIan Kent 
141ebc921caSIan Kent 	head = &sbi->active_list;
142ebc921caSIan Kent 	if (list_empty(head))
143ebc921caSIan Kent 		return NULL;
144ebc921caSIan Kent 	spin_lock(&sbi->lookup_lock);
145ebc921caSIan Kent 	list_for_each(p, head) {
146ebc921caSIan Kent 		struct autofs_info *ino;
147ebc921caSIan Kent 		struct dentry *active;
148ebc921caSIan Kent 		const struct qstr *qstr;
149ebc921caSIan Kent 
150ebc921caSIan Kent 		ino = list_entry(p, struct autofs_info, active);
151ebc921caSIan Kent 		active = ino->dentry;
152ebc921caSIan Kent 
153ebc921caSIan Kent 		spin_lock(&active->d_lock);
154ebc921caSIan Kent 
155ebc921caSIan Kent 		/* Already gone? */
156ebc921caSIan Kent 		if ((int) d_count(active) <= 0)
157ebc921caSIan Kent 			goto next;
158ebc921caSIan Kent 
159ebc921caSIan Kent 		qstr = &active->d_name;
160ebc921caSIan Kent 
161ebc921caSIan Kent 		if (active->d_name.hash != hash)
162ebc921caSIan Kent 			goto next;
163ebc921caSIan Kent 		if (active->d_parent != parent)
164ebc921caSIan Kent 			goto next;
165ebc921caSIan Kent 
166ebc921caSIan Kent 		if (qstr->len != len)
167ebc921caSIan Kent 			goto next;
168ebc921caSIan Kent 		if (memcmp(qstr->name, str, len))
169ebc921caSIan Kent 			goto next;
170ebc921caSIan Kent 
171ebc921caSIan Kent 		if (d_unhashed(active)) {
172ebc921caSIan Kent 			dget_dlock(active);
173ebc921caSIan Kent 			spin_unlock(&active->d_lock);
174ebc921caSIan Kent 			spin_unlock(&sbi->lookup_lock);
175ebc921caSIan Kent 			return active;
176ebc921caSIan Kent 		}
177ebc921caSIan Kent next:
178ebc921caSIan Kent 		spin_unlock(&active->d_lock);
179ebc921caSIan Kent 	}
180ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
181ebc921caSIan Kent 
182ebc921caSIan Kent 	return NULL;
183ebc921caSIan Kent }
184ebc921caSIan Kent 
autofs_lookup_expiring(struct dentry * dentry,bool rcu_walk)185ebc921caSIan Kent static struct dentry *autofs_lookup_expiring(struct dentry *dentry,
186ebc921caSIan Kent 					     bool rcu_walk)
187ebc921caSIan Kent {
188ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
189ebc921caSIan Kent 	struct dentry *parent = dentry->d_parent;
190ebc921caSIan Kent 	const struct qstr *name = &dentry->d_name;
191ebc921caSIan Kent 	unsigned int len = name->len;
192ebc921caSIan Kent 	unsigned int hash = name->hash;
193ebc921caSIan Kent 	const unsigned char *str = name->name;
194ebc921caSIan Kent 	struct list_head *p, *head;
195ebc921caSIan Kent 
196ebc921caSIan Kent 	head = &sbi->expiring_list;
197ebc921caSIan Kent 	if (list_empty(head))
198ebc921caSIan Kent 		return NULL;
199ebc921caSIan Kent 	spin_lock(&sbi->lookup_lock);
200ebc921caSIan Kent 	list_for_each(p, head) {
201ebc921caSIan Kent 		struct autofs_info *ino;
202ebc921caSIan Kent 		struct dentry *expiring;
203ebc921caSIan Kent 		const struct qstr *qstr;
204ebc921caSIan Kent 
205ebc921caSIan Kent 		if (rcu_walk) {
206ebc921caSIan Kent 			spin_unlock(&sbi->lookup_lock);
207ebc921caSIan Kent 			return ERR_PTR(-ECHILD);
208ebc921caSIan Kent 		}
209ebc921caSIan Kent 
210ebc921caSIan Kent 		ino = list_entry(p, struct autofs_info, expiring);
211ebc921caSIan Kent 		expiring = ino->dentry;
212ebc921caSIan Kent 
213ebc921caSIan Kent 		spin_lock(&expiring->d_lock);
214ebc921caSIan Kent 
215ebc921caSIan Kent 		/* We've already been dentry_iput or unlinked */
216ebc921caSIan Kent 		if (d_really_is_negative(expiring))
217ebc921caSIan Kent 			goto next;
218ebc921caSIan Kent 
219ebc921caSIan Kent 		qstr = &expiring->d_name;
220ebc921caSIan Kent 
221ebc921caSIan Kent 		if (expiring->d_name.hash != hash)
222ebc921caSIan Kent 			goto next;
223ebc921caSIan Kent 		if (expiring->d_parent != parent)
224ebc921caSIan Kent 			goto next;
225ebc921caSIan Kent 
226ebc921caSIan Kent 		if (qstr->len != len)
227ebc921caSIan Kent 			goto next;
228ebc921caSIan Kent 		if (memcmp(qstr->name, str, len))
229ebc921caSIan Kent 			goto next;
230ebc921caSIan Kent 
231ebc921caSIan Kent 		if (d_unhashed(expiring)) {
232ebc921caSIan Kent 			dget_dlock(expiring);
233ebc921caSIan Kent 			spin_unlock(&expiring->d_lock);
234ebc921caSIan Kent 			spin_unlock(&sbi->lookup_lock);
235ebc921caSIan Kent 			return expiring;
236ebc921caSIan Kent 		}
237ebc921caSIan Kent next:
238ebc921caSIan Kent 		spin_unlock(&expiring->d_lock);
239ebc921caSIan Kent 	}
240ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
241ebc921caSIan Kent 
242ebc921caSIan Kent 	return NULL;
243ebc921caSIan Kent }
244ebc921caSIan Kent 
autofs_mount_wait(const struct path * path,bool rcu_walk)245ebc921caSIan Kent static int autofs_mount_wait(const struct path *path, bool rcu_walk)
246ebc921caSIan Kent {
247ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(path->dentry->d_sb);
248ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(path->dentry);
249ebc921caSIan Kent 	int status = 0;
250ebc921caSIan Kent 
251ebc921caSIan Kent 	if (ino->flags & AUTOFS_INF_PENDING) {
252ebc921caSIan Kent 		if (rcu_walk)
253ebc921caSIan Kent 			return -ECHILD;
254ebc921caSIan Kent 		pr_debug("waiting for mount name=%pd\n", path->dentry);
255ebc921caSIan Kent 		status = autofs_wait(sbi, path, NFY_MOUNT);
256ebc921caSIan Kent 		pr_debug("mount wait done status=%d\n", status);
257f5162216SIan Kent 		ino->last_used = jiffies;
258f5162216SIan Kent 		return status;
259ebc921caSIan Kent 	}
260f5162216SIan Kent 	if (!(sbi->flags & AUTOFS_SBI_STRICTEXPIRE))
261ebc921caSIan Kent 		ino->last_used = jiffies;
262ebc921caSIan Kent 	return status;
263ebc921caSIan Kent }
264ebc921caSIan Kent 
do_expire_wait(const struct path * path,bool rcu_walk)265ebc921caSIan Kent static int do_expire_wait(const struct path *path, bool rcu_walk)
266ebc921caSIan Kent {
267ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
268ebc921caSIan Kent 	struct dentry *expiring;
269ebc921caSIan Kent 
270ebc921caSIan Kent 	expiring = autofs_lookup_expiring(dentry, rcu_walk);
271ebc921caSIan Kent 	if (IS_ERR(expiring))
272ebc921caSIan Kent 		return PTR_ERR(expiring);
273ebc921caSIan Kent 	if (!expiring)
274ebc921caSIan Kent 		return autofs_expire_wait(path, rcu_walk);
275ebc921caSIan Kent 	else {
276ebc921caSIan Kent 		const struct path this = { .mnt = path->mnt, .dentry = expiring };
277ebc921caSIan Kent 		/*
278ebc921caSIan Kent 		 * If we are racing with expire the request might not
279ebc921caSIan Kent 		 * be quite complete, but the directory has been removed
280ebc921caSIan Kent 		 * so it must have been successful, just wait for it.
281ebc921caSIan Kent 		 */
282ebc921caSIan Kent 		autofs_expire_wait(&this, 0);
283ebc921caSIan Kent 		autofs_del_expiring(expiring);
284ebc921caSIan Kent 		dput(expiring);
285ebc921caSIan Kent 	}
286ebc921caSIan Kent 	return 0;
287ebc921caSIan Kent }
288ebc921caSIan Kent 
autofs_mountpoint_changed(struct path * path)289ebc921caSIan Kent static struct dentry *autofs_mountpoint_changed(struct path *path)
290ebc921caSIan Kent {
291ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
292ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
293ebc921caSIan Kent 
294ba97a0a3SIan Kent 	/* If this is an indirect mount the dentry could have gone away
295ba97a0a3SIan Kent 	 * and a new one created.
296ba97a0a3SIan Kent 	 *
297ba97a0a3SIan Kent 	 * This is unusual and I can't remember the case for which it
298ba97a0a3SIan Kent 	 * was originally added now. But an example of how this can
299ba97a0a3SIan Kent 	 * happen is an autofs indirect mount that has the "browse"
300ba97a0a3SIan Kent 	 * option set and also has the "symlink" option in the autofs
301ba97a0a3SIan Kent 	 * map entry. In this case the daemon will remove the browse
302ba97a0a3SIan Kent 	 * directory and create a symlink as the mount leaving the
303ba97a0a3SIan Kent 	 * struct path stale.
304ba97a0a3SIan Kent 	 *
305ba97a0a3SIan Kent 	 * Another not so obvious case is when a mount in an autofs
306ba97a0a3SIan Kent 	 * indirect mount that uses the "nobrowse" option is being
307ba97a0a3SIan Kent 	 * expired at the same time as a path walk. If the mount has
308ba97a0a3SIan Kent 	 * been umounted but the mount point directory seen before
309ba97a0a3SIan Kent 	 * becoming unhashed (during a lockless path walk) when a stat
310ba97a0a3SIan Kent 	 * family system call is made the mount won't be re-mounted as
311ba97a0a3SIan Kent 	 * it should. In this case the mount point that's been removed
312ba97a0a3SIan Kent 	 * (by the daemon) will be stale and the a new mount point
313ba97a0a3SIan Kent 	 * dentry created.
314ebc921caSIan Kent 	 */
315ebc921caSIan Kent 	if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
316ebc921caSIan Kent 		struct dentry *parent = dentry->d_parent;
317ebc921caSIan Kent 		struct autofs_info *ino;
318ebc921caSIan Kent 		struct dentry *new;
319ebc921caSIan Kent 
320ebc921caSIan Kent 		new = d_lookup(parent, &dentry->d_name);
321ebc921caSIan Kent 		if (!new)
322ebc921caSIan Kent 			return NULL;
323ebc921caSIan Kent 		ino = autofs_dentry_ino(new);
324ebc921caSIan Kent 		ino->last_used = jiffies;
325ebc921caSIan Kent 		dput(path->dentry);
326ebc921caSIan Kent 		path->dentry = new;
327ebc921caSIan Kent 	}
328ebc921caSIan Kent 	return path->dentry;
329ebc921caSIan Kent }
330ebc921caSIan Kent 
autofs_d_automount(struct path * path)331ebc921caSIan Kent static struct vfsmount *autofs_d_automount(struct path *path)
332ebc921caSIan Kent {
333ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
334ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
335ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
336ebc921caSIan Kent 	int status;
337ebc921caSIan Kent 
338ebc921caSIan Kent 	pr_debug("dentry=%p %pd\n", dentry, dentry);
339ebc921caSIan Kent 
340ebc921caSIan Kent 	/* The daemon never triggers a mount. */
341ebc921caSIan Kent 	if (autofs_oz_mode(sbi))
342ebc921caSIan Kent 		return NULL;
343ebc921caSIan Kent 
344ebc921caSIan Kent 	/*
345ebc921caSIan Kent 	 * If an expire request is pending everyone must wait.
346ebc921caSIan Kent 	 * If the expire fails we're still mounted so continue
347ebc921caSIan Kent 	 * the follow and return. A return of -EAGAIN (which only
348ebc921caSIan Kent 	 * happens with indirect mounts) means the expire completed
349ebc921caSIan Kent 	 * and the directory was removed, so just go ahead and try
350ebc921caSIan Kent 	 * the mount.
351ebc921caSIan Kent 	 */
352ebc921caSIan Kent 	status = do_expire_wait(path, 0);
353ebc921caSIan Kent 	if (status && status != -EAGAIN)
354ebc921caSIan Kent 		return NULL;
355ebc921caSIan Kent 
356ebc921caSIan Kent 	/* Callback to the daemon to perform the mount or wait */
357ebc921caSIan Kent 	spin_lock(&sbi->fs_lock);
358ebc921caSIan Kent 	if (ino->flags & AUTOFS_INF_PENDING) {
359ebc921caSIan Kent 		spin_unlock(&sbi->fs_lock);
360ebc921caSIan Kent 		status = autofs_mount_wait(path, 0);
361ebc921caSIan Kent 		if (status)
362ebc921caSIan Kent 			return ERR_PTR(status);
363ebc921caSIan Kent 		goto done;
364ebc921caSIan Kent 	}
365ebc921caSIan Kent 
366ebc921caSIan Kent 	/*
367ebc921caSIan Kent 	 * If the dentry is a symlink it's equivalent to a directory
368ebc921caSIan Kent 	 * having path_is_mountpoint() true, so there's no need to call
369ebc921caSIan Kent 	 * back to the daemon.
370ebc921caSIan Kent 	 */
371ebc921caSIan Kent 	if (d_really_is_positive(dentry) && d_is_symlink(dentry)) {
372ebc921caSIan Kent 		spin_unlock(&sbi->fs_lock);
373ebc921caSIan Kent 		goto done;
374ebc921caSIan Kent 	}
375ebc921caSIan Kent 
376ebc921caSIan Kent 	if (!path_is_mountpoint(path)) {
377ebc921caSIan Kent 		/*
378ebc921caSIan Kent 		 * It's possible that user space hasn't removed directories
379ebc921caSIan Kent 		 * after umounting a rootless multi-mount, although it
380ebc921caSIan Kent 		 * should. For v5 path_has_submounts() is sufficient to
381ebc921caSIan Kent 		 * handle this because the leaves of the directory tree under
382ebc921caSIan Kent 		 * the mount never trigger mounts themselves (they have an
383ebc921caSIan Kent 		 * autofs trigger mount mounted on them). But v4 pseudo direct
384ebc921caSIan Kent 		 * mounts do need the leaves to trigger mounts. In this case
385a4a87303SIan Kent 		 * we have no choice but to use the autofs_empty() check and
386ebc921caSIan Kent 		 * require user space behave.
387ebc921caSIan Kent 		 */
388ebc921caSIan Kent 		if (sbi->version > 4) {
389ebc921caSIan Kent 			if (path_has_submounts(path)) {
390ebc921caSIan Kent 				spin_unlock(&sbi->fs_lock);
391ebc921caSIan Kent 				goto done;
392ebc921caSIan Kent 			}
393ebc921caSIan Kent 		} else {
394a4a87303SIan Kent 			if (!autofs_empty(ino)) {
395ebc921caSIan Kent 				spin_unlock(&sbi->fs_lock);
396ebc921caSIan Kent 				goto done;
397ebc921caSIan Kent 			}
398ebc921caSIan Kent 		}
399ebc921caSIan Kent 		ino->flags |= AUTOFS_INF_PENDING;
400ebc921caSIan Kent 		spin_unlock(&sbi->fs_lock);
401ebc921caSIan Kent 		status = autofs_mount_wait(path, 0);
402ebc921caSIan Kent 		spin_lock(&sbi->fs_lock);
403ebc921caSIan Kent 		ino->flags &= ~AUTOFS_INF_PENDING;
404ebc921caSIan Kent 		if (status) {
405ebc921caSIan Kent 			spin_unlock(&sbi->fs_lock);
406ebc921caSIan Kent 			return ERR_PTR(status);
407ebc921caSIan Kent 		}
408ebc921caSIan Kent 	}
409ebc921caSIan Kent 	spin_unlock(&sbi->fs_lock);
410ebc921caSIan Kent done:
411ebc921caSIan Kent 	/* Mount succeeded, check if we ended up with a new dentry */
412ebc921caSIan Kent 	dentry = autofs_mountpoint_changed(path);
413ebc921caSIan Kent 	if (!dentry)
414ebc921caSIan Kent 		return ERR_PTR(-ENOENT);
415ebc921caSIan Kent 
416ebc921caSIan Kent 	return NULL;
417ebc921caSIan Kent }
418ebc921caSIan Kent 
autofs_d_manage(const struct path * path,bool rcu_walk)419ebc921caSIan Kent static int autofs_d_manage(const struct path *path, bool rcu_walk)
420ebc921caSIan Kent {
421ebc921caSIan Kent 	struct dentry *dentry = path->dentry;
422ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dentry->d_sb);
423ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
424ebc921caSIan Kent 	int status;
425ebc921caSIan Kent 
426ebc921caSIan Kent 	pr_debug("dentry=%p %pd\n", dentry, dentry);
427ebc921caSIan Kent 
428ebc921caSIan Kent 	/* The daemon never waits. */
429ebc921caSIan Kent 	if (autofs_oz_mode(sbi)) {
430ebc921caSIan Kent 		if (!path_is_mountpoint(path))
431ebc921caSIan Kent 			return -EISDIR;
432ebc921caSIan Kent 		return 0;
433ebc921caSIan Kent 	}
434ebc921caSIan Kent 
435ebc921caSIan Kent 	/* Wait for pending expires */
436ebc921caSIan Kent 	if (do_expire_wait(path, rcu_walk) == -ECHILD)
437ebc921caSIan Kent 		return -ECHILD;
438ebc921caSIan Kent 
439ebc921caSIan Kent 	/*
440ebc921caSIan Kent 	 * This dentry may be under construction so wait on mount
441ebc921caSIan Kent 	 * completion.
442ebc921caSIan Kent 	 */
443ebc921caSIan Kent 	status = autofs_mount_wait(path, rcu_walk);
444ebc921caSIan Kent 	if (status)
445ebc921caSIan Kent 		return status;
446ebc921caSIan Kent 
447ebc921caSIan Kent 	if (rcu_walk) {
448ebc921caSIan Kent 		/* We don't need fs_lock in rcu_walk mode,
449a4a87303SIan Kent 		 * just testing 'AUTOFS_INF_WANT_EXPIRE' is enough.
450a4a87303SIan Kent 		 *
451ebc921caSIan Kent 		 * We only return -EISDIR when certain this isn't
452ebc921caSIan Kent 		 * a mount-trap.
453ebc921caSIan Kent 		 */
454ebc921caSIan Kent 		struct inode *inode;
455ebc921caSIan Kent 
456ebc921caSIan Kent 		if (ino->flags & AUTOFS_INF_WANT_EXPIRE)
457ebc921caSIan Kent 			return 0;
458ebc921caSIan Kent 		if (path_is_mountpoint(path))
459ebc921caSIan Kent 			return 0;
460ebc921caSIan Kent 		inode = d_inode_rcu(dentry);
461ebc921caSIan Kent 		if (inode && S_ISLNK(inode->i_mode))
462ebc921caSIan Kent 			return -EISDIR;
463a4a87303SIan Kent 		if (!autofs_empty(ino))
464ebc921caSIan Kent 			return -EISDIR;
465ebc921caSIan Kent 		return 0;
466ebc921caSIan Kent 	}
467ebc921caSIan Kent 
468ebc921caSIan Kent 	spin_lock(&sbi->fs_lock);
469ebc921caSIan Kent 	/*
470ebc921caSIan Kent 	 * If the dentry has been selected for expire while we slept
471ebc921caSIan Kent 	 * on the lock then it might go away. We'll deal with that in
472ebc921caSIan Kent 	 * ->d_automount() and wait on a new mount if the expire
473ebc921caSIan Kent 	 * succeeds or return here if it doesn't (since there's no
474ebc921caSIan Kent 	 * mount to follow with a rootless multi-mount).
475ebc921caSIan Kent 	 */
476ebc921caSIan Kent 	if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
477ebc921caSIan Kent 		/*
478ebc921caSIan Kent 		 * Any needed mounting has been completed and the path
479ebc921caSIan Kent 		 * updated so check if this is a rootless multi-mount so
480ebc921caSIan Kent 		 * we can avoid needless calls ->d_automount() and avoid
481ebc921caSIan Kent 		 * an incorrect ELOOP error return.
482ebc921caSIan Kent 		 */
483a4a87303SIan Kent 		if ((!path_is_mountpoint(path) && !autofs_empty(ino)) ||
484ebc921caSIan Kent 		    (d_really_is_positive(dentry) && d_is_symlink(dentry)))
485ebc921caSIan Kent 			status = -EISDIR;
486ebc921caSIan Kent 	}
487ebc921caSIan Kent 	spin_unlock(&sbi->fs_lock);
488ebc921caSIan Kent 
489ebc921caSIan Kent 	return status;
490ebc921caSIan Kent }
491ebc921caSIan Kent 
492ebc921caSIan Kent /* Lookups in the root directory */
autofs_lookup(struct inode * dir,struct dentry * dentry,unsigned int flags)493ebc921caSIan Kent static struct dentry *autofs_lookup(struct inode *dir,
494ebc921caSIan Kent 				    struct dentry *dentry, unsigned int flags)
495ebc921caSIan Kent {
496ebc921caSIan Kent 	struct autofs_sb_info *sbi;
497ebc921caSIan Kent 	struct autofs_info *ino;
498ebc921caSIan Kent 	struct dentry *active;
499ebc921caSIan Kent 
500ebc921caSIan Kent 	pr_debug("name = %pd\n", dentry);
501ebc921caSIan Kent 
502ebc921caSIan Kent 	/* File name too long to exist */
503ebc921caSIan Kent 	if (dentry->d_name.len > NAME_MAX)
504ebc921caSIan Kent 		return ERR_PTR(-ENAMETOOLONG);
505ebc921caSIan Kent 
506ebc921caSIan Kent 	sbi = autofs_sbi(dir->i_sb);
507ebc921caSIan Kent 
508ebc921caSIan Kent 	pr_debug("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
5099d8719a4SIan Kent 		 current->pid, task_pgrp_nr(current),
5109d8719a4SIan Kent 		 sbi->flags & AUTOFS_SBI_CATATONIC,
511ebc921caSIan Kent 		 autofs_oz_mode(sbi));
512ebc921caSIan Kent 
513ebc921caSIan Kent 	active = autofs_lookup_active(dentry);
514ebc921caSIan Kent 	if (active)
515ebc921caSIan Kent 		return active;
516ebc921caSIan Kent 	else {
517ebc921caSIan Kent 		/*
518ebc921caSIan Kent 		 * A dentry that is not within the root can never trigger a
519ebc921caSIan Kent 		 * mount operation, unless the directory already exists, so we
520ebc921caSIan Kent 		 * can return fail immediately.  The daemon however does need
521ebc921caSIan Kent 		 * to create directories within the file system.
522ebc921caSIan Kent 		 */
523ebc921caSIan Kent 		if (!autofs_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
524ebc921caSIan Kent 			return ERR_PTR(-ENOENT);
525ebc921caSIan Kent 
526ebc921caSIan Kent 		ino = autofs_new_ino(sbi);
527ebc921caSIan Kent 		if (!ino)
528ebc921caSIan Kent 			return ERR_PTR(-ENOMEM);
529ebc921caSIan Kent 
5305f68056cSAl Viro 		spin_lock(&sbi->lookup_lock);
5315f68056cSAl Viro 		spin_lock(&dentry->d_lock);
5325f68056cSAl Viro 		/* Mark entries in the root as mount triggers */
5335f68056cSAl Viro 		if (IS_ROOT(dentry->d_parent) &&
5345f68056cSAl Viro 		    autofs_type_indirect(sbi->type))
5355f68056cSAl Viro 			__managed_dentry_set_managed(dentry);
536ebc921caSIan Kent 		dentry->d_fsdata = ino;
537ebc921caSIan Kent 		ino->dentry = dentry;
538ebc921caSIan Kent 
539c4931db9SAl Viro 		list_add(&ino->active, &sbi->active_list);
540c4931db9SAl Viro 		spin_unlock(&sbi->lookup_lock);
5415f68056cSAl Viro 		spin_unlock(&dentry->d_lock);
542ebc921caSIan Kent 	}
543ebc921caSIan Kent 	return NULL;
544ebc921caSIan Kent }
545ebc921caSIan Kent 
autofs_dir_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)5464609e1f1SChristian Brauner static int autofs_dir_permission(struct mnt_idmap *idmap,
547f71381fcSIan Kent 				 struct inode *inode, int mask)
548ebc921caSIan Kent {
549f71381fcSIan Kent 	if (mask & MAY_WRITE) {
550f71381fcSIan Kent 		struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
551ebc921caSIan Kent 
552ebc921caSIan Kent 		if (!autofs_oz_mode(sbi))
553ebc921caSIan Kent 			return -EACCES;
554ebc921caSIan Kent 
555d4d79b81SIan Kent 		/* autofs_oz_mode() needs to allow path walks when the
556d4d79b81SIan Kent 		 * autofs mount is catatonic but the state of an autofs
557d4d79b81SIan Kent 		 * file system needs to be preserved over restarts.
558d4d79b81SIan Kent 		 */
5599d8719a4SIan Kent 		if (sbi->flags & AUTOFS_SBI_CATATONIC)
560d4d79b81SIan Kent 			return -EACCES;
561f71381fcSIan Kent 	}
562f71381fcSIan Kent 
5634609e1f1SChristian Brauner 	return generic_permission(idmap, inode, mask);
564f71381fcSIan Kent }
565f71381fcSIan Kent 
autofs_dir_symlink(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,const char * symname)5667a77db95SChristian Brauner static int autofs_dir_symlink(struct mnt_idmap *idmap,
567f71381fcSIan Kent 			      struct inode *dir, struct dentry *dentry,
568f71381fcSIan Kent 			      const char *symname)
569f71381fcSIan Kent {
570f71381fcSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
571f71381fcSIan Kent 	struct autofs_info *p_ino;
572f71381fcSIan Kent 	struct inode *inode;
573f71381fcSIan Kent 	size_t size = strlen(symname);
574f71381fcSIan Kent 	char *cp;
575f71381fcSIan Kent 
576f71381fcSIan Kent 	pr_debug("%s <- %pd\n", symname, dentry);
577d4d79b81SIan Kent 
578ebc921caSIan Kent 	BUG_ON(!ino);
579ebc921caSIan Kent 
580ebc921caSIan Kent 	autofs_clean_ino(ino);
581ebc921caSIan Kent 
582ebc921caSIan Kent 	autofs_del_active(dentry);
583ebc921caSIan Kent 
584ebc921caSIan Kent 	cp = kmalloc(size + 1, GFP_KERNEL);
585ebc921caSIan Kent 	if (!cp)
586ebc921caSIan Kent 		return -ENOMEM;
587ebc921caSIan Kent 
588ebc921caSIan Kent 	strcpy(cp, symname);
589ebc921caSIan Kent 
590ebc921caSIan Kent 	inode = autofs_get_inode(dir->i_sb, S_IFLNK | 0555);
591ebc921caSIan Kent 	if (!inode) {
592ebc921caSIan Kent 		kfree(cp);
593ebc921caSIan Kent 		return -ENOMEM;
594ebc921caSIan Kent 	}
595ebc921caSIan Kent 	inode->i_private = cp;
596ebc921caSIan Kent 	inode->i_size = size;
597ebc921caSIan Kent 	d_add(dentry, inode);
598ebc921caSIan Kent 
599ebc921caSIan Kent 	dget(dentry);
600ebc921caSIan Kent 	p_ino = autofs_dentry_ino(dentry->d_parent);
601850d71acSAl Viro 	p_ino->count++;
602ebc921caSIan Kent 
603*36aa5eaeSJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
604ebc921caSIan Kent 
605ebc921caSIan Kent 	return 0;
606ebc921caSIan Kent }
607ebc921caSIan Kent 
608ebc921caSIan Kent /*
609ebc921caSIan Kent  * NOTE!
610ebc921caSIan Kent  *
611ebc921caSIan Kent  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
612ebc921caSIan Kent  * that the file no longer exists. However, doing that means that the
613ebc921caSIan Kent  * VFS layer can turn the dentry into a negative dentry.  We don't want
614ebc921caSIan Kent  * this, because the unlink is probably the result of an expire.
615ebc921caSIan Kent  * We simply d_drop it and add it to a expiring list in the super block,
616ebc921caSIan Kent  * which allows the dentry lookup to check for an incomplete expire.
617ebc921caSIan Kent  *
618ebc921caSIan Kent  * If a process is blocked on the dentry waiting for the expire to finish,
619ebc921caSIan Kent  * it will invalidate the dentry and try to mount with a new one.
620ebc921caSIan Kent  *
621ebc921caSIan Kent  * Also see autofs_dir_rmdir()..
622ebc921caSIan Kent  */
autofs_dir_unlink(struct inode * dir,struct dentry * dentry)623ebc921caSIan Kent static int autofs_dir_unlink(struct inode *dir, struct dentry *dentry)
624ebc921caSIan Kent {
625ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
626ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
627ebc921caSIan Kent 	struct autofs_info *p_ino;
628ebc921caSIan Kent 
629ebc921caSIan Kent 	p_ino = autofs_dentry_ino(dentry->d_parent);
630850d71acSAl Viro 	p_ino->count--;
631ebc921caSIan Kent 	dput(ino->dentry);
632ebc921caSIan Kent 
633ebc921caSIan Kent 	d_inode(dentry)->i_size = 0;
634ebc921caSIan Kent 	clear_nlink(d_inode(dentry));
635ebc921caSIan Kent 
636*36aa5eaeSJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
637ebc921caSIan Kent 
638ebc921caSIan Kent 	spin_lock(&sbi->lookup_lock);
639ebc921caSIan Kent 	__autofs_add_expiring(dentry);
640ebc921caSIan Kent 	d_drop(dentry);
641ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
642ebc921caSIan Kent 
643ebc921caSIan Kent 	return 0;
644ebc921caSIan Kent }
645ebc921caSIan Kent 
646ebc921caSIan Kent /*
647ebc921caSIan Kent  * Version 4 of autofs provides a pseudo direct mount implementation
648ebc921caSIan Kent  * that relies on directories at the leaves of a directory tree under
649ebc921caSIan Kent  * an indirect mount to trigger mounts. To allow for this we need to
650ebc921caSIan Kent  * set the DMANAGED_AUTOMOUNT and DMANAGED_TRANSIT flags on the leaves
651ebc921caSIan Kent  * of the directory tree. There is no need to clear the automount flag
652ebc921caSIan Kent  * following a mount or restore it after an expire because these mounts
653ebc921caSIan Kent  * are always covered. However, it is necessary to ensure that these
654ebc921caSIan Kent  * flags are clear on non-empty directories to avoid unnecessary calls
655ebc921caSIan Kent  * during path walks.
656ebc921caSIan Kent  */
autofs_set_leaf_automount_flags(struct dentry * dentry)657ebc921caSIan Kent static void autofs_set_leaf_automount_flags(struct dentry *dentry)
658ebc921caSIan Kent {
659ebc921caSIan Kent 	struct dentry *parent;
660ebc921caSIan Kent 
661ebc921caSIan Kent 	/* root and dentrys in the root are already handled */
662ebc921caSIan Kent 	if (IS_ROOT(dentry->d_parent))
663ebc921caSIan Kent 		return;
664ebc921caSIan Kent 
665ebc921caSIan Kent 	managed_dentry_set_managed(dentry);
666ebc921caSIan Kent 
667ebc921caSIan Kent 	parent = dentry->d_parent;
668ebc921caSIan Kent 	/* only consider parents below dentrys in the root */
669ebc921caSIan Kent 	if (IS_ROOT(parent->d_parent))
670ebc921caSIan Kent 		return;
671ebc921caSIan Kent 	managed_dentry_clear_managed(parent);
672ebc921caSIan Kent }
673ebc921caSIan Kent 
autofs_clear_leaf_automount_flags(struct dentry * dentry)674ebc921caSIan Kent static void autofs_clear_leaf_automount_flags(struct dentry *dentry)
675ebc921caSIan Kent {
676ebc921caSIan Kent 	struct dentry *parent;
677ebc921caSIan Kent 
678ebc921caSIan Kent 	/* flags for dentrys in the root are handled elsewhere */
679ebc921caSIan Kent 	if (IS_ROOT(dentry->d_parent))
680ebc921caSIan Kent 		return;
681ebc921caSIan Kent 
682ebc921caSIan Kent 	managed_dentry_clear_managed(dentry);
683ebc921caSIan Kent 
684ebc921caSIan Kent 	parent = dentry->d_parent;
685ebc921caSIan Kent 	/* only consider parents below dentrys in the root */
686ebc921caSIan Kent 	if (IS_ROOT(parent->d_parent))
687ebc921caSIan Kent 		return;
688850d71acSAl Viro 	if (autofs_dentry_ino(parent)->count == 2)
689ebc921caSIan Kent 		managed_dentry_set_managed(parent);
690ebc921caSIan Kent }
691ebc921caSIan Kent 
autofs_dir_rmdir(struct inode * dir,struct dentry * dentry)692ebc921caSIan Kent static int autofs_dir_rmdir(struct inode *dir, struct dentry *dentry)
693ebc921caSIan Kent {
694ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
695ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
696ebc921caSIan Kent 	struct autofs_info *p_ino;
697ebc921caSIan Kent 
698ebc921caSIan Kent 	pr_debug("dentry %p, removing %pd\n", dentry, dentry);
699ebc921caSIan Kent 
700850d71acSAl Viro 	if (ino->count != 1)
701ebc921caSIan Kent 		return -ENOTEMPTY;
702c3aed166SAl Viro 
703c3aed166SAl Viro 	spin_lock(&sbi->lookup_lock);
704ebc921caSIan Kent 	__autofs_add_expiring(dentry);
705ebc921caSIan Kent 	d_drop(dentry);
706ebc921caSIan Kent 	spin_unlock(&sbi->lookup_lock);
707ebc921caSIan Kent 
708ebc921caSIan Kent 	if (sbi->version < 5)
709ebc921caSIan Kent 		autofs_clear_leaf_automount_flags(dentry);
710ebc921caSIan Kent 
711ebc921caSIan Kent 	p_ino = autofs_dentry_ino(dentry->d_parent);
712850d71acSAl Viro 	p_ino->count--;
713ebc921caSIan Kent 	dput(ino->dentry);
714ebc921caSIan Kent 	d_inode(dentry)->i_size = 0;
715ebc921caSIan Kent 	clear_nlink(d_inode(dentry));
716ebc921caSIan Kent 
717ebc921caSIan Kent 	if (dir->i_nlink)
718ebc921caSIan Kent 		drop_nlink(dir);
719ebc921caSIan Kent 
720ebc921caSIan Kent 	return 0;
721ebc921caSIan Kent }
722ebc921caSIan Kent 
autofs_dir_mkdir(struct mnt_idmap * idmap,struct inode * dir,struct dentry * dentry,umode_t mode)723c54bd91eSChristian Brauner static int autofs_dir_mkdir(struct mnt_idmap *idmap,
724549c7297SChristian Brauner 			    struct inode *dir, struct dentry *dentry,
725549c7297SChristian Brauner 			    umode_t mode)
726ebc921caSIan Kent {
727ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
728ebc921caSIan Kent 	struct autofs_info *ino = autofs_dentry_ino(dentry);
729ebc921caSIan Kent 	struct autofs_info *p_ino;
730ebc921caSIan Kent 	struct inode *inode;
731ebc921caSIan Kent 
732ebc921caSIan Kent 	pr_debug("dentry %p, creating %pd\n", dentry, dentry);
733ebc921caSIan Kent 
734ebc921caSIan Kent 	BUG_ON(!ino);
735ebc921caSIan Kent 
736ebc921caSIan Kent 	autofs_clean_ino(ino);
737ebc921caSIan Kent 
738ebc921caSIan Kent 	autofs_del_active(dentry);
739ebc921caSIan Kent 
740ebc921caSIan Kent 	inode = autofs_get_inode(dir->i_sb, S_IFDIR | mode);
741ebc921caSIan Kent 	if (!inode)
742ebc921caSIan Kent 		return -ENOMEM;
743ebc921caSIan Kent 	d_add(dentry, inode);
744ebc921caSIan Kent 
745ebc921caSIan Kent 	if (sbi->version < 5)
746ebc921caSIan Kent 		autofs_set_leaf_automount_flags(dentry);
747ebc921caSIan Kent 
748ebc921caSIan Kent 	dget(dentry);
749ebc921caSIan Kent 	p_ino = autofs_dentry_ino(dentry->d_parent);
750850d71acSAl Viro 	p_ino->count++;
751ebc921caSIan Kent 	inc_nlink(dir);
752*36aa5eaeSJeff Layton 	dir->i_mtime = inode_set_ctime_current(dir);
753ebc921caSIan Kent 
754ebc921caSIan Kent 	return 0;
755ebc921caSIan Kent }
756ebc921caSIan Kent 
757ebc921caSIan Kent /* Get/set timeout ioctl() operation */
758ebc921caSIan Kent #ifdef CONFIG_COMPAT
autofs_compat_get_set_timeout(struct autofs_sb_info * sbi,compat_ulong_t __user * p)759ebc921caSIan Kent static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
760ebc921caSIan Kent 						 compat_ulong_t __user *p)
761ebc921caSIan Kent {
762ebc921caSIan Kent 	unsigned long ntimeout;
763ebc921caSIan Kent 	int rv;
764ebc921caSIan Kent 
765ebc921caSIan Kent 	rv = get_user(ntimeout, p);
766ebc921caSIan Kent 	if (rv)
767ebc921caSIan Kent 		goto error;
768ebc921caSIan Kent 
769ebc921caSIan Kent 	rv = put_user(sbi->exp_timeout/HZ, p);
770ebc921caSIan Kent 	if (rv)
771ebc921caSIan Kent 		goto error;
772ebc921caSIan Kent 
773ebc921caSIan Kent 	if (ntimeout > UINT_MAX/HZ)
774ebc921caSIan Kent 		sbi->exp_timeout = 0;
775ebc921caSIan Kent 	else
776ebc921caSIan Kent 		sbi->exp_timeout = ntimeout * HZ;
777ebc921caSIan Kent 
778ebc921caSIan Kent 	return 0;
779ebc921caSIan Kent error:
780ebc921caSIan Kent 	return rv;
781ebc921caSIan Kent }
782ebc921caSIan Kent #endif
783ebc921caSIan Kent 
autofs_get_set_timeout(struct autofs_sb_info * sbi,unsigned long __user * p)784ebc921caSIan Kent static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
785ebc921caSIan Kent 					  unsigned long __user *p)
786ebc921caSIan Kent {
787ebc921caSIan Kent 	unsigned long ntimeout;
788ebc921caSIan Kent 	int rv;
789ebc921caSIan Kent 
790ebc921caSIan Kent 	rv = get_user(ntimeout, p);
791ebc921caSIan Kent 	if (rv)
792ebc921caSIan Kent 		goto error;
793ebc921caSIan Kent 
794ebc921caSIan Kent 	rv = put_user(sbi->exp_timeout/HZ, p);
795ebc921caSIan Kent 	if (rv)
796ebc921caSIan Kent 		goto error;
797ebc921caSIan Kent 
798ebc921caSIan Kent 	if (ntimeout > ULONG_MAX/HZ)
799ebc921caSIan Kent 		sbi->exp_timeout = 0;
800ebc921caSIan Kent 	else
801ebc921caSIan Kent 		sbi->exp_timeout = ntimeout * HZ;
802ebc921caSIan Kent 
803ebc921caSIan Kent 	return 0;
804ebc921caSIan Kent error:
805ebc921caSIan Kent 	return rv;
806ebc921caSIan Kent }
807ebc921caSIan Kent 
808ebc921caSIan Kent /* Return protocol version */
autofs_get_protover(struct autofs_sb_info * sbi,int __user * p)809ebc921caSIan Kent static inline int autofs_get_protover(struct autofs_sb_info *sbi,
810ebc921caSIan Kent 				       int __user *p)
811ebc921caSIan Kent {
812ebc921caSIan Kent 	return put_user(sbi->version, p);
813ebc921caSIan Kent }
814ebc921caSIan Kent 
815ebc921caSIan Kent /* Return protocol sub version */
autofs_get_protosubver(struct autofs_sb_info * sbi,int __user * p)816ebc921caSIan Kent static inline int autofs_get_protosubver(struct autofs_sb_info *sbi,
817ebc921caSIan Kent 					  int __user *p)
818ebc921caSIan Kent {
819ebc921caSIan Kent 	return put_user(sbi->sub_version, p);
820ebc921caSIan Kent }
821ebc921caSIan Kent 
822ebc921caSIan Kent /*
823ebc921caSIan Kent * Tells the daemon whether it can umount the autofs mount.
824ebc921caSIan Kent */
autofs_ask_umount(struct vfsmount * mnt,int __user * p)825ebc921caSIan Kent static inline int autofs_ask_umount(struct vfsmount *mnt, int __user *p)
826ebc921caSIan Kent {
827ebc921caSIan Kent 	int status = 0;
828ebc921caSIan Kent 
829ebc921caSIan Kent 	if (may_umount(mnt))
830ebc921caSIan Kent 		status = 1;
831ebc921caSIan Kent 
832ebc921caSIan Kent 	pr_debug("may umount %d\n", status);
833ebc921caSIan Kent 
834ebc921caSIan Kent 	status = put_user(status, p);
835ebc921caSIan Kent 
836ebc921caSIan Kent 	return status;
837ebc921caSIan Kent }
838ebc921caSIan Kent 
839ebc921caSIan Kent /* Identify autofs_dentries - this is so we can tell if there's
840ebc921caSIan Kent  * an extra dentry refcount or not.  We only hold a refcount on the
841ebc921caSIan Kent  * dentry if its non-negative (ie, d_inode != NULL)
842ebc921caSIan Kent  */
is_autofs_dentry(struct dentry * dentry)843ebc921caSIan Kent int is_autofs_dentry(struct dentry *dentry)
844ebc921caSIan Kent {
845ebc921caSIan Kent 	return dentry && d_really_is_positive(dentry) &&
846ebc921caSIan Kent 		dentry->d_op == &autofs_dentry_operations &&
847ebc921caSIan Kent 		dentry->d_fsdata != NULL;
848ebc921caSIan Kent }
849ebc921caSIan Kent 
850ebc921caSIan Kent /*
851ebc921caSIan Kent  * ioctl()'s on the root directory is the chief method for the daemon to
852ebc921caSIan Kent  * generate kernel reactions
853ebc921caSIan Kent  */
autofs_root_ioctl_unlocked(struct inode * inode,struct file * filp,unsigned int cmd,unsigned long arg)854ebc921caSIan Kent static int autofs_root_ioctl_unlocked(struct inode *inode, struct file *filp,
855ebc921caSIan Kent 				       unsigned int cmd, unsigned long arg)
856ebc921caSIan Kent {
857ebc921caSIan Kent 	struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
858ebc921caSIan Kent 	void __user *p = (void __user *)arg;
859ebc921caSIan Kent 
860ebc921caSIan Kent 	pr_debug("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
861ebc921caSIan Kent 		 cmd, arg, sbi, task_pgrp_nr(current));
862ebc921caSIan Kent 
863ebc921caSIan Kent 	if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
864ebc921caSIan Kent 	     _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
865ebc921caSIan Kent 		return -ENOTTY;
866ebc921caSIan Kent 
867ebc921caSIan Kent 	if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
868ebc921caSIan Kent 		return -EPERM;
869ebc921caSIan Kent 
870ebc921caSIan Kent 	switch (cmd) {
871ebc921caSIan Kent 	case AUTOFS_IOC_READY:	/* Wait queue: go ahead and retry */
872ebc921caSIan Kent 		return autofs_wait_release(sbi, (autofs_wqt_t) arg, 0);
873ebc921caSIan Kent 	case AUTOFS_IOC_FAIL:	/* Wait queue: fail with ENOENT */
874ebc921caSIan Kent 		return autofs_wait_release(sbi, (autofs_wqt_t) arg, -ENOENT);
875ebc921caSIan Kent 	case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
876ebc921caSIan Kent 		autofs_catatonic_mode(sbi);
877ebc921caSIan Kent 		return 0;
878ebc921caSIan Kent 	case AUTOFS_IOC_PROTOVER: /* Get protocol version */
879ebc921caSIan Kent 		return autofs_get_protover(sbi, p);
880ebc921caSIan Kent 	case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
881ebc921caSIan Kent 		return autofs_get_protosubver(sbi, p);
882ebc921caSIan Kent 	case AUTOFS_IOC_SETTIMEOUT:
883ebc921caSIan Kent 		return autofs_get_set_timeout(sbi, p);
884ebc921caSIan Kent #ifdef CONFIG_COMPAT
885ebc921caSIan Kent 	case AUTOFS_IOC_SETTIMEOUT32:
886ebc921caSIan Kent 		return autofs_compat_get_set_timeout(sbi, p);
887ebc921caSIan Kent #endif
888ebc921caSIan Kent 
889ebc921caSIan Kent 	case AUTOFS_IOC_ASKUMOUNT:
890ebc921caSIan Kent 		return autofs_ask_umount(filp->f_path.mnt, p);
891ebc921caSIan Kent 
892ebc921caSIan Kent 	/* return a single thing to expire */
893ebc921caSIan Kent 	case AUTOFS_IOC_EXPIRE:
894ebc921caSIan Kent 		return autofs_expire_run(inode->i_sb, filp->f_path.mnt, sbi, p);
895ebc921caSIan Kent 	/* same as above, but can send multiple expires through pipe */
896ebc921caSIan Kent 	case AUTOFS_IOC_EXPIRE_MULTI:
897ebc921caSIan Kent 		return autofs_expire_multi(inode->i_sb,
898ebc921caSIan Kent 					   filp->f_path.mnt, sbi, p);
899ebc921caSIan Kent 
900ebc921caSIan Kent 	default:
901ebc921caSIan Kent 		return -EINVAL;
902ebc921caSIan Kent 	}
903ebc921caSIan Kent }
904ebc921caSIan Kent 
autofs_root_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)905ebc921caSIan Kent static long autofs_root_ioctl(struct file *filp,
906ebc921caSIan Kent 			       unsigned int cmd, unsigned long arg)
907ebc921caSIan Kent {
908ebc921caSIan Kent 	struct inode *inode = file_inode(filp);
909ebc921caSIan Kent 
910ebc921caSIan Kent 	return autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
911ebc921caSIan Kent }
912ebc921caSIan Kent 
913ebc921caSIan Kent #ifdef CONFIG_COMPAT
autofs_root_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)914ebc921caSIan Kent static long autofs_root_compat_ioctl(struct file *filp,
915ebc921caSIan Kent 				      unsigned int cmd, unsigned long arg)
916ebc921caSIan Kent {
917ebc921caSIan Kent 	struct inode *inode = file_inode(filp);
918ebc921caSIan Kent 	int ret;
919ebc921caSIan Kent 
920ebc921caSIan Kent 	if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
921ebc921caSIan Kent 		ret = autofs_root_ioctl_unlocked(inode, filp, cmd, arg);
922ebc921caSIan Kent 	else
923ebc921caSIan Kent 		ret = autofs_root_ioctl_unlocked(inode, filp, cmd,
924ebc921caSIan Kent 					      (unsigned long) compat_ptr(arg));
925ebc921caSIan Kent 
926ebc921caSIan Kent 	return ret;
927ebc921caSIan Kent }
928ebc921caSIan Kent #endif
929