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