File Index
Files and directories
Sign in
FILE

root.c

File preview with syntax highlighting.

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