1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
13 * ------------------------------------------------------------------------- */
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include <linux/smp_lock.h>
23 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24 static int autofs4_dir_unlink(struct inode *,struct dentry *);
25 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28 static int autofs4_dir_open(struct inode *inode, struct file *file);
29 static int autofs4_dir_close(struct inode *inode, struct file *file);
30 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
33 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
35 struct file_operations autofs4_root_operations = {
36 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
43 struct file_operations autofs4_dir_operations = {
44 .open = autofs4_dir_open,
45 .release = autofs4_dir_close,
46 .read = generic_read_dir,
47 .readdir = autofs4_dir_readdir,
50 struct inode_operations autofs4_indirect_root_inode_operations = {
51 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
58 struct inode_operations autofs4_direct_root_inode_operations = {
59 .lookup = autofs4_lookup,
60 .follow_link = autofs4_follow_link,
63 struct inode_operations autofs4_dir_inode_operations = {
64 .lookup = autofs4_lookup,
65 .unlink = autofs4_dir_unlink,
66 .symlink = autofs4_dir_symlink,
67 .mkdir = autofs4_dir_mkdir,
68 .rmdir = autofs4_dir_rmdir,
71 static int autofs4_root_readdir(struct file *file, void *dirent,
74 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
75 int oz_mode = autofs4_oz_mode(sbi);
77 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
80 * Don't set reghost flag if:
81 * 1) f_pos is larger than zero -- we've already been here.
82 * 2) we haven't even enabled reghosting in the 1st place.
83 * 3) this is the daemon doing a readdir
85 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
86 sbi->needs_reghost = 1;
88 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
90 return dcache_readdir(file, dirent, filldir);
93 static int autofs4_dir_open(struct inode *inode, struct file *file)
95 struct dentry *dentry = file->f_dentry;
96 struct vfsmount *mnt = file->f_vfsmnt;
97 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
98 struct dentry *cursor;
101 status = dcache_dir_open(inode, file);
105 cursor = file->private_data;
106 cursor->d_fsdata = NULL;
108 DPRINTK("file=%p dentry=%p %.*s",
109 file, dentry, dentry->d_name.len, dentry->d_name.name);
111 if (autofs4_oz_mode(sbi))
114 if (autofs4_ispending(dentry)) {
115 DPRINTK("dentry busy");
116 dcache_dir_close(inode, file);
122 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
126 /* In case there are stale directory dentrys from a failed mount */
127 spin_lock(&dcache_lock);
128 empty = list_empty(&dentry->d_subdirs);
129 spin_unlock(&dcache_lock);
132 d_invalidate(dentry);
134 nd.flags = LOOKUP_DIRECTORY;
135 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
138 dcache_dir_close(inode, file);
143 if (d_mountpoint(dentry)) {
144 struct file *fp = NULL;
145 struct vfsmount *fp_mnt = mntget(mnt);
146 struct dentry *fp_dentry = dget(dentry);
148 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
151 dcache_dir_close(inode, file);
155 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
156 status = PTR_ERR(fp);
158 dcache_dir_close(inode, file);
161 cursor->d_fsdata = fp;
168 static int autofs4_dir_close(struct inode *inode, struct file *file)
170 struct dentry *dentry = file->f_dentry;
171 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
172 struct dentry *cursor = file->private_data;
175 DPRINTK("file=%p dentry=%p %.*s",
176 file, dentry, dentry->d_name.len, dentry->d_name.name);
178 if (autofs4_oz_mode(sbi))
181 if (autofs4_ispending(dentry)) {
182 DPRINTK("dentry busy");
187 if (d_mountpoint(dentry)) {
188 struct file *fp = cursor->d_fsdata;
193 filp_close(fp, current->files);
196 dcache_dir_close(inode, file);
200 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
202 struct dentry *dentry = file->f_dentry;
203 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
204 struct dentry *cursor = file->private_data;
207 DPRINTK("file=%p dentry=%p %.*s",
208 file, dentry, dentry->d_name.len, dentry->d_name.name);
210 if (autofs4_oz_mode(sbi))
213 if (autofs4_ispending(dentry)) {
214 DPRINTK("dentry busy");
218 if (d_mountpoint(dentry)) {
219 struct file *fp = cursor->d_fsdata;
224 if (!fp->f_op || !fp->f_op->readdir)
227 status = vfs_readdir(fp, filldir, dirent);
228 file->f_pos = fp->f_pos;
230 autofs4_copy_atime(file, fp);
234 return dcache_readdir(file, dirent, filldir);
237 static int try_to_fill_dentry(struct dentry *dentry, int flags)
239 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
240 struct autofs_info *ino = autofs4_dentry_ino(dentry);
243 /* Block on any pending expiry here; invalidate the dentry
244 when expiration is done to trigger mount request with a new
246 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
247 DPRINTK("waiting for expire %p name=%.*s",
248 dentry, dentry->d_name.len, dentry->d_name.name);
250 status = autofs4_wait(sbi, dentry, NFY_NONE);
252 DPRINTK("expire done status=%d", status);
255 * If the directory still exists the mount request must
256 * continue otherwise it can't be followed at the right
257 * time during the walk.
259 status = d_invalidate(dentry);
260 if (status != -EBUSY)
264 DPRINTK("dentry=%p %.*s ino=%p",
265 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
268 * Wait for a pending mount, triggering one if there
271 if (dentry->d_inode == NULL) {
272 DPRINTK("waiting for mount name=%.*s",
273 dentry->d_name.len, dentry->d_name.name);
275 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
277 DPRINTK("mount done status=%d", status);
279 if (status && dentry->d_inode)
280 return status; /* Try to get the kernel to invalidate this dentry */
282 /* Turn this into a real negative dentry? */
283 if (status == -ENOENT) {
284 spin_lock(&dentry->d_lock);
285 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
286 spin_unlock(&dentry->d_lock);
289 /* Return a negative dentry, but leave it "pending" */
292 /* Trigger mount for path component or follow link */
293 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
294 current->link_count) {
295 DPRINTK("waiting for mount name=%.*s",
296 dentry->d_name.len, dentry->d_name.name);
298 spin_lock(&dentry->d_lock);
299 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
300 spin_unlock(&dentry->d_lock);
301 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
303 DPRINTK("mount done status=%d", status);
306 spin_lock(&dentry->d_lock);
307 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
308 spin_unlock(&dentry->d_lock);
313 /* Initialize expiry counter after successful mount */
315 ino->last_used = jiffies;
317 spin_lock(&dentry->d_lock);
318 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
319 spin_unlock(&dentry->d_lock);
323 /* For autofs direct mounts the follow link triggers the mount */
324 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
326 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
327 int oz_mode = autofs4_oz_mode(sbi);
328 unsigned int lookup_type;
331 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
332 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
335 /* If it's our master or we shouldn't trigger a mount we're done */
336 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
337 if (oz_mode || !lookup_type)
340 status = try_to_fill_dentry(dentry, 0);
344 if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
354 return ERR_PTR(status);
358 * Revalidate is called on every cache lookup. Some of those
359 * cache lookups may actually happen while the dentry is not
360 * yet completely filled in, and revalidate has to delay such
363 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
365 struct inode *dir = dentry->d_parent->d_inode;
366 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
367 int oz_mode = autofs4_oz_mode(sbi);
368 int flags = nd ? nd->flags : 0;
372 if (autofs4_ispending(dentry)) {
374 status = try_to_fill_dentry(dentry, flags);
378 /* Negative dentry.. invalidate if "old" */
379 if (dentry->d_inode == NULL)
382 /* Check for a non-mountpoint directory with no contents */
383 spin_lock(&dcache_lock);
384 if (S_ISDIR(dentry->d_inode->i_mode) &&
385 !d_mountpoint(dentry) &&
386 __simple_empty(dentry)) {
387 DPRINTK("dentry=%p %.*s, emptydir",
388 dentry, dentry->d_name.len, dentry->d_name.name);
389 spin_unlock(&dcache_lock);
391 status = try_to_fill_dentry(dentry, flags);
394 spin_unlock(&dcache_lock);
399 void autofs4_dentry_release(struct dentry *de)
401 struct autofs_info *inf;
403 DPRINTK("releasing %p", de);
405 inf = autofs4_dentry_ino(de);
412 autofs4_free_ino(inf);
416 /* For dentries of directories in the root dir */
417 static struct dentry_operations autofs4_root_dentry_operations = {
418 .d_revalidate = autofs4_revalidate,
419 .d_release = autofs4_dentry_release,
422 /* For other dentries */
423 static struct dentry_operations autofs4_dentry_operations = {
424 .d_revalidate = autofs4_revalidate,
425 .d_release = autofs4_dentry_release,
428 /* Lookups in the root directory */
429 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
431 struct autofs_sb_info *sbi;
434 DPRINTK("name = %.*s",
435 dentry->d_name.len, dentry->d_name.name);
437 /* File name too long to exist */
438 if (dentry->d_name.len > NAME_MAX)
439 return ERR_PTR(-ENAMETOOLONG);
441 sbi = autofs4_sbi(dir->i_sb);
442 oz_mode = autofs4_oz_mode(sbi);
444 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
445 current->pid, process_group(current), sbi->catatonic, oz_mode);
448 * Mark the dentry incomplete, but add it. This is needed so
449 * that the VFS layer knows about the dentry, and we can count
450 * on catching any lookups through the revalidate.
452 * Let all the hard work be done by the revalidate function that
453 * needs to be able to do this anyway..
455 * We need to do this before we release the directory semaphore.
457 dentry->d_op = &autofs4_root_dentry_operations;
460 spin_lock(&dentry->d_lock);
461 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
462 spin_unlock(&dentry->d_lock);
464 dentry->d_fsdata = NULL;
467 if (dentry->d_op && dentry->d_op->d_revalidate) {
468 mutex_unlock(&dir->i_mutex);
469 (dentry->d_op->d_revalidate)(dentry, nd);
470 mutex_lock(&dir->i_mutex);
474 * If we are still pending, check if we had to handle
475 * a signal. If so we can force a restart..
477 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
478 /* See if we were interrupted */
479 if (signal_pending(current)) {
480 sigset_t *sigset = ¤t->pending.signal;
481 if (sigismember (sigset, SIGKILL) ||
482 sigismember (sigset, SIGQUIT) ||
483 sigismember (sigset, SIGINT)) {
484 return ERR_PTR(-ERESTARTNOINTR);
490 * If this dentry is unhashed, then we shouldn't honour this
491 * lookup even if the dentry is positive. Returning ENOENT here
492 * doesn't do the right thing for all system calls, but it should
493 * be OK for the operations we permit from an autofs.
495 if (dentry->d_inode && d_unhashed(dentry))
496 return ERR_PTR(-ENOENT);
501 static int autofs4_dir_symlink(struct inode *dir,
502 struct dentry *dentry,
505 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
506 struct autofs_info *ino = autofs4_dentry_ino(dentry);
507 struct autofs_info *p_ino;
511 DPRINTK("%s <- %.*s", symname,
512 dentry->d_name.len, dentry->d_name.name);
514 if (!autofs4_oz_mode(sbi))
517 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
521 ino->size = strlen(symname);
522 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
531 inode = autofs4_get_inode(dir->i_sb, ino);
532 d_instantiate(dentry, inode);
534 if (dir == dir->i_sb->s_root->d_inode)
535 dentry->d_op = &autofs4_root_dentry_operations;
537 dentry->d_op = &autofs4_dentry_operations;
539 dentry->d_fsdata = ino;
540 ino->dentry = dget(dentry);
541 atomic_inc(&ino->count);
542 p_ino = autofs4_dentry_ino(dentry->d_parent);
543 if (p_ino && dentry->d_parent != dentry)
544 atomic_inc(&p_ino->count);
547 dir->i_mtime = CURRENT_TIME;
555 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
556 * that the file no longer exists. However, doing that means that the
557 * VFS layer can turn the dentry into a negative dentry. We don't want
558 * this, because since the unlink is probably the result of an expire.
559 * We simply d_drop it, which allows the dentry lookup to remount it
562 * If a process is blocked on the dentry waiting for the expire to finish,
563 * it will invalidate the dentry and try to mount with a new one.
565 * Also see autofs4_dir_rmdir()..
567 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
569 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
570 struct autofs_info *ino = autofs4_dentry_ino(dentry);
571 struct autofs_info *p_ino;
573 /* This allows root to remove symlinks */
574 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
577 if (atomic_dec_and_test(&ino->count)) {
578 p_ino = autofs4_dentry_ino(dentry->d_parent);
579 if (p_ino && dentry->d_parent != dentry)
580 atomic_dec(&p_ino->count);
584 dentry->d_inode->i_size = 0;
585 dentry->d_inode->i_nlink = 0;
587 dir->i_mtime = CURRENT_TIME;
594 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
596 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
597 struct autofs_info *ino = autofs4_dentry_ino(dentry);
598 struct autofs_info *p_ino;
600 if (!autofs4_oz_mode(sbi))
603 spin_lock(&dcache_lock);
604 if (!list_empty(&dentry->d_subdirs)) {
605 spin_unlock(&dcache_lock);
608 spin_lock(&dentry->d_lock);
610 spin_unlock(&dentry->d_lock);
611 spin_unlock(&dcache_lock);
613 if (atomic_dec_and_test(&ino->count)) {
614 p_ino = autofs4_dentry_ino(dentry->d_parent);
615 if (p_ino && dentry->d_parent != dentry)
616 atomic_dec(&p_ino->count);
619 dentry->d_inode->i_size = 0;
620 dentry->d_inode->i_nlink = 0;
628 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
630 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
631 struct autofs_info *ino = autofs4_dentry_ino(dentry);
632 struct autofs_info *p_ino;
635 if ( !autofs4_oz_mode(sbi) )
638 DPRINTK("dentry %p, creating %.*s",
639 dentry, dentry->d_name.len, dentry->d_name.name);
641 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
645 inode = autofs4_get_inode(dir->i_sb, ino);
646 d_instantiate(dentry, inode);
648 if (dir == dir->i_sb->s_root->d_inode)
649 dentry->d_op = &autofs4_root_dentry_operations;
651 dentry->d_op = &autofs4_dentry_operations;
653 dentry->d_fsdata = ino;
654 ino->dentry = dget(dentry);
655 atomic_inc(&ino->count);
656 p_ino = autofs4_dentry_ino(dentry->d_parent);
657 if (p_ino && dentry->d_parent != dentry)
658 atomic_inc(&p_ino->count);
661 dir->i_mtime = CURRENT_TIME;
666 /* Get/set timeout ioctl() operation */
667 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
668 unsigned long __user *p)
671 unsigned long ntimeout;
673 if ( (rv = get_user(ntimeout, p)) ||
674 (rv = put_user(sbi->exp_timeout/HZ, p)) )
677 if ( ntimeout > ULONG_MAX/HZ )
678 sbi->exp_timeout = 0;
680 sbi->exp_timeout = ntimeout * HZ;
685 /* Return protocol version */
686 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
688 return put_user(sbi->version, p);
691 /* Return protocol sub version */
692 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
694 return put_user(sbi->sub_version, p);
698 * Tells the daemon whether we need to reghost or not. Also, clears
699 * the reghost_needed flag.
701 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
705 DPRINTK("returning %d", sbi->needs_reghost);
707 status = put_user(sbi->needs_reghost, p);
711 sbi->needs_reghost = 0;
716 * Enable / Disable reghosting ioctl() operation
718 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
723 status = get_user(val, p);
725 DPRINTK("reghost = %d", val);
730 /* turn on/off reghosting, with the val */
731 sbi->reghost_enabled = val;
736 * Tells the daemon whether it can umount the autofs mount.
738 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
745 DPRINTK("returning %d", status);
747 status = put_user(status, p);
752 /* Identify autofs4_dentries - this is so we can tell if there's
753 an extra dentry refcount or not. We only hold a refcount on the
754 dentry if its non-negative (ie, d_inode != NULL)
756 int is_autofs4_dentry(struct dentry *dentry)
758 return dentry && dentry->d_inode &&
759 (dentry->d_op == &autofs4_root_dentry_operations ||
760 dentry->d_op == &autofs4_dentry_operations) &&
761 dentry->d_fsdata != NULL;
765 * ioctl()'s on the root directory is the chief method for the daemon to
766 * generate kernel reactions
768 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
769 unsigned int cmd, unsigned long arg)
771 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
772 void __user *p = (void __user *)arg;
774 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
775 cmd,arg,sbi,process_group(current));
777 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
778 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
781 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
785 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
786 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
787 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
788 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
789 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
790 autofs4_catatonic_mode(sbi);
792 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
793 return autofs4_get_protover(sbi, p);
794 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
795 return autofs4_get_protosubver(sbi, p);
796 case AUTOFS_IOC_SETTIMEOUT:
797 return autofs4_get_set_timeout(sbi, p);
799 case AUTOFS_IOC_TOGGLEREGHOST:
800 return autofs4_toggle_reghost(sbi, p);
801 case AUTOFS_IOC_ASKREGHOST:
802 return autofs4_ask_reghost(sbi, p);
804 case AUTOFS_IOC_ASKUMOUNT:
805 return autofs4_ask_umount(filp->f_vfsmnt, p);
807 /* return a single thing to expire */
808 case AUTOFS_IOC_EXPIRE:
809 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
810 /* same as above, but can send multiple expires through pipe */
811 case AUTOFS_IOC_EXPIRE_MULTI:
812 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);