Linux Audio

Check our new training course

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