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-2003 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/errno.h>
16 #include <linux/stat.h>
17 #include <linux/param.h>
18 #include <linux/time.h>
19 #include <linux/smp_lock.h>
22 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static int autofs4_dir_open(struct inode *inode, struct file *file);
28 static int autofs4_dir_close(struct inode *inode, struct file *file);
29 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32 static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
34 struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
42 struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
49 struct inode_operations autofs4_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
57 struct inode_operations autofs4_dir_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .symlink = autofs4_dir_symlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
65 static int autofs4_root_readdir(struct file *file, void *dirent,
68 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
69 int oz_mode = autofs4_oz_mode(sbi);
71 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
74 * Don't set reghost flag if:
75 * 1) f_pos is larger than zero -- we've already been here.
76 * 2) we haven't even enabled reghosting in the 1st place.
77 * 3) this is the daemon doing a readdir
79 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
80 sbi->needs_reghost = 1;
82 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
84 return autofs4_dcache_readdir(file, dirent, filldir);
87 /* Update usage from here to top of tree, so that scan of
88 top-level directories will give a useful result */
89 static void autofs4_update_usage(struct dentry *dentry)
91 struct dentry *top = dentry->d_sb->s_root;
93 spin_lock(&dcache_lock);
94 for(; dentry != top; dentry = dentry->d_parent) {
95 struct autofs_info *ino = autofs4_dentry_ino(dentry);
98 update_atime(dentry->d_inode);
99 ino->last_used = jiffies;
102 spin_unlock(&dcache_lock);
106 * From 2.4 kernel readdir.c
108 static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
111 struct dentry *dentry = filp->f_dentry;
116 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
122 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
128 struct list_head *list;
131 spin_lock(&dcache_lock);
132 list = dentry->d_subdirs.next;
135 if (list == &dentry->d_subdirs) {
136 spin_unlock(&dcache_lock);
146 struct dentry *de = list_entry(list, struct dentry, d_child);
148 if (!d_unhashed(de) && de->d_inode) {
149 spin_unlock(&dcache_lock);
150 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
152 spin_lock(&dcache_lock);
156 if (list != &dentry->d_subdirs)
158 spin_unlock(&dcache_lock);
166 static int autofs4_dir_open(struct inode *inode, struct file *file)
168 struct dentry *dentry = file->f_dentry;
169 struct vfsmount *mnt = file->f_vfsmnt;
170 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
173 DPRINTK("file=%p dentry=%p %.*s",
174 file, dentry, dentry->d_name.len, dentry->d_name.name);
176 if (autofs4_oz_mode(sbi))
179 if (autofs4_ispending(dentry)) {
180 DPRINTK("dentry busy");
184 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
188 /* In case there are stale directory dentrys from a failed mount */
189 spin_lock(&dcache_lock);
190 empty = list_empty(&dentry->d_subdirs);
191 spin_unlock(&dcache_lock);
194 d_invalidate(dentry);
196 nd.flags = LOOKUP_DIRECTORY;
197 status = (dentry->d_op->d_revalidate)(dentry, &nd);
203 if (d_mountpoint(dentry)) {
204 struct file *fp = NULL;
205 struct vfsmount *fp_mnt = mntget(mnt);
206 struct dentry *fp_dentry = dget(dentry);
208 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
214 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
215 status = PTR_ERR(fp);
217 file->private_data = NULL;
220 file->private_data = fp;
226 static int autofs4_dir_close(struct inode *inode, struct file *file)
228 struct dentry *dentry = file->f_dentry;
229 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
231 DPRINTK("file=%p dentry=%p %.*s",
232 file, dentry, dentry->d_name.len, dentry->d_name.name);
234 if (autofs4_oz_mode(sbi))
237 if (autofs4_ispending(dentry)) {
238 DPRINTK("dentry busy");
242 if (d_mountpoint(dentry)) {
243 struct file *fp = file->private_data;
248 filp_close(fp, current->files);
249 file->private_data = NULL;
255 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
257 struct dentry *dentry = file->f_dentry;
258 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
261 DPRINTK("file=%p dentry=%p %.*s",
262 file, dentry, dentry->d_name.len, dentry->d_name.name);
264 if (autofs4_oz_mode(sbi))
267 if (autofs4_ispending(dentry)) {
268 DPRINTK("dentry busy");
272 if (d_mountpoint(dentry)) {
273 struct file *fp = file->private_data;
278 if (!fp->f_op || !fp->f_op->readdir)
281 status = vfs_readdir(fp, filldir, dirent);
282 file->f_pos = fp->f_pos;
284 autofs4_copy_atime(file, fp);
288 return autofs4_dcache_readdir(file, dirent, filldir);
291 static int try_to_fill_dentry(struct dentry *dentry,
292 struct super_block *sb,
293 struct autofs_sb_info *sbi, int flags)
295 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
298 /* Block on any pending expiry here; invalidate the dentry
299 when expiration is done to trigger mount request with a new
301 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
302 DPRINTK("waiting for expire %p name=%.*s",
303 dentry, dentry->d_name.len, dentry->d_name.name);
305 status = autofs4_wait(sbi, dentry, NFY_NONE);
307 DPRINTK("expire done status=%d", status);
312 DPRINTK("dentry=%p %.*s ino=%p",
313 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
315 /* Wait for a pending mount, triggering one if there isn't one already */
316 if (dentry->d_inode == NULL) {
317 DPRINTK("waiting for mount name=%.*s",
318 dentry->d_name.len, dentry->d_name.name);
320 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
322 DPRINTK("mount done status=%d", status);
324 if (status && dentry->d_inode)
325 return 0; /* Try to get the kernel to invalidate this dentry */
327 /* Turn this into a real negative dentry? */
328 if (status == -ENOENT) {
329 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
330 spin_lock(&dentry->d_lock);
331 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
332 spin_unlock(&dentry->d_lock);
335 /* Return a negative dentry, but leave it "pending" */
338 /* Trigger mount for path component or follow link */
339 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
340 current->link_count) {
341 DPRINTK("waiting for mount name=%.*s",
342 dentry->d_name.len, dentry->d_name.name);
344 spin_lock(&dentry->d_lock);
345 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
346 spin_unlock(&dentry->d_lock);
347 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
349 DPRINTK("mount done status=%d", status);
352 spin_lock(&dentry->d_lock);
353 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
354 spin_unlock(&dentry->d_lock);
359 /* We don't update the usages for the autofs daemon itself, this
360 is necessary for recursive autofs mounts */
361 if (!autofs4_oz_mode(sbi))
362 autofs4_update_usage(dentry);
364 spin_lock(&dentry->d_lock);
365 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
366 spin_unlock(&dentry->d_lock);
371 * Revalidate is called on every cache lookup. Some of those
372 * cache lookups may actually happen while the dentry is not
373 * yet completely filled in, and revalidate has to delay such
376 static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
378 struct inode * dir = dentry->d_parent->d_inode;
379 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
380 int oz_mode = autofs4_oz_mode(sbi);
381 int flags = nd ? nd->flags : 0;
385 if (autofs4_ispending(dentry)) {
387 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
391 /* Negative dentry.. invalidate if "old" */
392 if (dentry->d_inode == NULL)
393 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
395 /* Check for a non-mountpoint directory with no contents */
396 spin_lock(&dcache_lock);
397 if (S_ISDIR(dentry->d_inode->i_mode) &&
398 !d_mountpoint(dentry) &&
399 list_empty(&dentry->d_subdirs)) {
400 DPRINTK("dentry=%p %.*s, emptydir",
401 dentry, dentry->d_name.len, dentry->d_name.name);
402 spin_unlock(&dcache_lock);
404 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
407 spin_unlock(&dcache_lock);
409 /* Update the usage list */
411 autofs4_update_usage(dentry);
416 static void autofs4_dentry_release(struct dentry *de)
418 struct autofs_info *inf;
420 DPRINTK("releasing %p", de);
422 inf = autofs4_dentry_ino(de);
429 autofs4_free_ino(inf);
433 /* For dentries of directories in the root dir */
434 static struct dentry_operations autofs4_root_dentry_operations = {
435 .d_revalidate = autofs4_revalidate,
436 .d_release = autofs4_dentry_release,
439 /* For other dentries */
440 static struct dentry_operations autofs4_dentry_operations = {
441 .d_revalidate = autofs4_revalidate,
442 .d_release = autofs4_dentry_release,
445 /* Lookups in the root directory */
446 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
448 struct autofs_sb_info *sbi;
451 DPRINTK("name = %.*s",
452 dentry->d_name.len, dentry->d_name.name);
454 if (dentry->d_name.len > NAME_MAX)
455 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
457 sbi = autofs4_sbi(dir->i_sb);
459 oz_mode = autofs4_oz_mode(sbi);
460 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
461 current->pid, process_group(current), sbi->catatonic, oz_mode);
464 * Mark the dentry incomplete, but add it. This is needed so
465 * that the VFS layer knows about the dentry, and we can count
466 * on catching any lookups through the revalidate.
468 * Let all the hard work be done by the revalidate function that
469 * needs to be able to do this anyway..
471 * We need to do this before we release the directory semaphore.
473 dentry->d_op = &autofs4_root_dentry_operations;
476 spin_lock(&dentry->d_lock);
477 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
478 spin_unlock(&dentry->d_lock);
480 dentry->d_fsdata = NULL;
483 if (dentry->d_op && dentry->d_op->d_revalidate) {
485 (dentry->d_op->d_revalidate)(dentry, nd);
490 * If we are still pending, check if we had to handle
491 * a signal. If so we can force a restart..
493 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
494 /* See if we were interrupted */
495 if (signal_pending(current)) {
496 sigset_t *sigset = ¤t->pending.signal;
497 if (sigismember (sigset, SIGKILL) ||
498 sigismember (sigset, SIGQUIT) ||
499 sigismember (sigset, SIGINT)) {
500 return ERR_PTR(-ERESTARTNOINTR);
506 * If this dentry is unhashed, then we shouldn't honour this
507 * lookup even if the dentry is positive. Returning ENOENT here
508 * doesn't do the right thing for all system calls, but it should
509 * be OK for the operations we permit from an autofs.
511 if ( dentry->d_inode && d_unhashed(dentry) )
512 return ERR_PTR(-ENOENT);
517 static int autofs4_dir_symlink(struct inode *dir,
518 struct dentry *dentry,
521 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
522 struct autofs_info *ino = autofs4_dentry_ino(dentry);
526 DPRINTK("%s <- %.*s", symname,
527 dentry->d_name.len, dentry->d_name.name);
529 if (!autofs4_oz_mode(sbi))
532 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
536 ino->size = strlen(symname);
537 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
546 inode = autofs4_get_inode(dir->i_sb, ino);
547 d_instantiate(dentry, inode);
549 if (dir == dir->i_sb->s_root->d_inode)
550 dentry->d_op = &autofs4_root_dentry_operations;
552 dentry->d_op = &autofs4_dentry_operations;
554 dentry->d_fsdata = ino;
555 ino->dentry = dget(dentry);
558 dir->i_mtime = CURRENT_TIME;
566 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
567 * that the file no longer exists. However, doing that means that the
568 * VFS layer can turn the dentry into a negative dentry. We don't want
569 * this, because since the unlink is probably the result of an expire.
570 * We simply d_drop it, which allows the dentry lookup to remount it
573 * If a process is blocked on the dentry waiting for the expire to finish,
574 * it will invalidate the dentry and try to mount with a new one.
576 * Also see autofs4_dir_rmdir()..
578 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
580 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
581 struct autofs_info *ino = autofs4_dentry_ino(dentry);
583 /* This allows root to remove symlinks */
584 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
589 dentry->d_inode->i_size = 0;
590 dentry->d_inode->i_nlink = 0;
592 dir->i_mtime = CURRENT_TIME;
599 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
601 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
602 struct autofs_info *ino = autofs4_dentry_ino(dentry);
604 if (!autofs4_oz_mode(sbi))
607 spin_lock(&dcache_lock);
608 if (!list_empty(&dentry->d_subdirs)) {
609 spin_unlock(&dcache_lock);
612 spin_lock(&dentry->d_lock);
614 spin_unlock(&dentry->d_lock);
615 spin_unlock(&dcache_lock);
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);
634 if ( !autofs4_oz_mode(sbi) )
637 DPRINTK("dentry %p, creating %.*s",
638 dentry, dentry->d_name.len, dentry->d_name.name);
640 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
644 inode = autofs4_get_inode(dir->i_sb, ino);
645 d_instantiate(dentry, inode);
647 if (dir == dir->i_sb->s_root->d_inode)
648 dentry->d_op = &autofs4_root_dentry_operations;
650 dentry->d_op = &autofs4_dentry_operations;
652 dentry->d_fsdata = ino;
653 ino->dentry = dget(dentry);
656 dir->i_mtime = CURRENT_TIME;
661 /* Get/set timeout ioctl() operation */
662 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
663 unsigned long __user *p)
666 unsigned long ntimeout;
668 if ( (rv = get_user(ntimeout, p)) ||
669 (rv = put_user(sbi->exp_timeout/HZ, p)) )
672 if ( ntimeout > ULONG_MAX/HZ )
673 sbi->exp_timeout = 0;
675 sbi->exp_timeout = ntimeout * HZ;
680 /* Return protocol version */
681 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
683 return put_user(sbi->version, p);
686 /* Return protocol sub version */
687 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
689 return put_user(sbi->sub_version, p);
693 * Tells the daemon whether we need to reghost or not. Also, clears
694 * the reghost_needed flag.
696 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
700 DPRINTK("returning %d", sbi->needs_reghost);
702 status = put_user(sbi->needs_reghost, p);
706 sbi->needs_reghost = 0;
711 * Enable / Disable reghosting ioctl() operation
713 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
718 status = get_user(val, p);
720 DPRINTK("reghost = %d", val);
725 /* turn on/off reghosting, with the val */
726 sbi->reghost_enabled = val;
731 * Tells the daemon whether it can umount the autofs mount.
733 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
737 if (may_umount(mnt) == 0)
740 DPRINTK("returning %d", status);
742 status = put_user(status, p);
747 /* Identify autofs4_dentries - this is so we can tell if there's
748 an extra dentry refcount or not. We only hold a refcount on the
749 dentry if its non-negative (ie, d_inode != NULL)
751 int is_autofs4_dentry(struct dentry *dentry)
753 return dentry && dentry->d_inode &&
754 (dentry->d_op == &autofs4_root_dentry_operations ||
755 dentry->d_op == &autofs4_dentry_operations) &&
756 dentry->d_fsdata != NULL;
760 * ioctl()'s on the root directory is the chief method for the daemon to
761 * generate kernel reactions
763 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
764 unsigned int cmd, unsigned long arg)
766 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
767 void __user *p = (void __user *)arg;
769 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
770 cmd,arg,sbi,process_group(current));
772 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
773 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
776 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
780 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
781 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
782 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
783 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
784 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
785 autofs4_catatonic_mode(sbi);
787 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
788 return autofs4_get_protover(sbi, p);
789 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
790 return autofs4_get_protosubver(sbi, p);
791 case AUTOFS_IOC_SETTIMEOUT:
792 return autofs4_get_set_timeout(sbi, p);
794 case AUTOFS_IOC_TOGGLEREGHOST:
795 return autofs4_toggle_reghost(sbi, p);
796 case AUTOFS_IOC_ASKREGHOST:
797 return autofs4_ask_reghost(sbi, p);
799 case AUTOFS_IOC_ASKUMOUNT:
800 return autofs4_ask_umount(filp->f_vfsmnt, p);
802 /* return a single thing to expire */
803 case AUTOFS_IOC_EXPIRE:
804 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
805 /* same as above, but can send multiple expires through pipe */
806 case AUTOFS_IOC_EXPIRE_MULTI:
807 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);