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/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 *);
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 dcache_readdir(file, dirent, filldir);
87 static int autofs4_dir_open(struct inode *inode, struct file *file)
89 struct dentry *dentry = file->f_dentry;
90 struct vfsmount *mnt = file->f_vfsmnt;
91 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
92 struct dentry *cursor;
95 status = dcache_dir_open(inode, file);
99 cursor = file->private_data;
100 cursor->d_fsdata = NULL;
102 DPRINTK("file=%p dentry=%p %.*s",
103 file, dentry, dentry->d_name.len, dentry->d_name.name);
105 if (autofs4_oz_mode(sbi))
108 if (autofs4_ispending(dentry)) {
109 DPRINTK("dentry busy");
110 dcache_dir_close(inode, file);
116 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
120 /* In case there are stale directory dentrys from a failed mount */
121 spin_lock(&dcache_lock);
122 empty = list_empty(&dentry->d_subdirs);
123 spin_unlock(&dcache_lock);
126 d_invalidate(dentry);
128 nd.flags = LOOKUP_DIRECTORY;
129 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
132 dcache_dir_close(inode, file);
137 if (d_mountpoint(dentry)) {
138 struct file *fp = NULL;
139 struct vfsmount *fp_mnt = mntget(mnt);
140 struct dentry *fp_dentry = dget(dentry);
142 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
145 dcache_dir_close(inode, file);
149 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
150 status = PTR_ERR(fp);
152 dcache_dir_close(inode, file);
155 cursor->d_fsdata = fp;
162 static int autofs4_dir_close(struct inode *inode, struct file *file)
164 struct dentry *dentry = file->f_dentry;
165 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
166 struct dentry *cursor = file->private_data;
169 DPRINTK("file=%p dentry=%p %.*s",
170 file, dentry, dentry->d_name.len, dentry->d_name.name);
172 if (autofs4_oz_mode(sbi))
175 if (autofs4_ispending(dentry)) {
176 DPRINTK("dentry busy");
181 if (d_mountpoint(dentry)) {
182 struct file *fp = cursor->d_fsdata;
187 filp_close(fp, current->files);
190 dcache_dir_close(inode, file);
194 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
196 struct dentry *dentry = file->f_dentry;
197 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
198 struct dentry *cursor = file->private_data;
201 DPRINTK("file=%p dentry=%p %.*s",
202 file, dentry, dentry->d_name.len, dentry->d_name.name);
204 if (autofs4_oz_mode(sbi))
207 if (autofs4_ispending(dentry)) {
208 DPRINTK("dentry busy");
212 if (d_mountpoint(dentry)) {
213 struct file *fp = cursor->d_fsdata;
218 if (!fp->f_op || !fp->f_op->readdir)
221 status = vfs_readdir(fp, filldir, dirent);
222 file->f_pos = fp->f_pos;
224 autofs4_copy_atime(file, fp);
228 return dcache_readdir(file, dirent, filldir);
231 static int try_to_fill_dentry(struct dentry *dentry, int flags)
233 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
234 struct autofs_info *ino = autofs4_dentry_ino(dentry);
237 /* Block on any pending expiry here; invalidate the dentry
238 when expiration is done to trigger mount request with a new
240 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
241 DPRINTK("waiting for expire %p name=%.*s",
242 dentry, dentry->d_name.len, dentry->d_name.name);
244 status = autofs4_wait(sbi, dentry, NFY_NONE);
246 DPRINTK("expire done status=%d", status);
249 * If the directory still exists the mount request must
250 * continue otherwise it can't be followed at the right
251 * time during the walk.
253 status = d_invalidate(dentry);
254 if (status != -EBUSY)
258 DPRINTK("dentry=%p %.*s ino=%p",
259 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
262 * Wait for a pending mount, triggering one if there
265 if (dentry->d_inode == NULL) {
266 DPRINTK("waiting for mount name=%.*s",
267 dentry->d_name.len, dentry->d_name.name);
269 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
271 DPRINTK("mount done status=%d", status);
273 if (status && dentry->d_inode)
274 return 0; /* Try to get the kernel to invalidate this dentry */
276 /* Turn this into a real negative dentry? */
277 if (status == -ENOENT) {
278 spin_lock(&dentry->d_lock);
279 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
280 spin_unlock(&dentry->d_lock);
283 /* Return a negative dentry, but leave it "pending" */
286 /* Trigger mount for path component or follow link */
287 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
288 current->link_count) {
289 DPRINTK("waiting for mount name=%.*s",
290 dentry->d_name.len, dentry->d_name.name);
292 spin_lock(&dentry->d_lock);
293 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
294 spin_unlock(&dentry->d_lock);
295 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
297 DPRINTK("mount done status=%d", status);
300 spin_lock(&dentry->d_lock);
301 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
302 spin_unlock(&dentry->d_lock);
307 /* Initialize expiry counter after successful mount */
309 ino->last_used = jiffies;
311 spin_lock(&dentry->d_lock);
312 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
313 spin_unlock(&dentry->d_lock);
318 * Revalidate is called on every cache lookup. Some of those
319 * cache lookups may actually happen while the dentry is not
320 * yet completely filled in, and revalidate has to delay such
323 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
325 struct inode *dir = dentry->d_parent->d_inode;
326 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
327 int oz_mode = autofs4_oz_mode(sbi);
328 int flags = nd ? nd->flags : 0;
332 if (autofs4_ispending(dentry)) {
334 status = try_to_fill_dentry(dentry, flags);
338 /* Negative dentry.. invalidate if "old" */
339 if (dentry->d_inode == NULL)
342 /* Check for a non-mountpoint directory with no contents */
343 spin_lock(&dcache_lock);
344 if (S_ISDIR(dentry->d_inode->i_mode) &&
345 !d_mountpoint(dentry) &&
346 simple_empty_nolock(dentry)) {
347 DPRINTK("dentry=%p %.*s, emptydir",
348 dentry, dentry->d_name.len, dentry->d_name.name);
349 spin_unlock(&dcache_lock);
351 status = try_to_fill_dentry(dentry, flags);
354 spin_unlock(&dcache_lock);
359 static void autofs4_dentry_release(struct dentry *de)
361 struct autofs_info *inf;
363 DPRINTK("releasing %p", de);
365 inf = autofs4_dentry_ino(de);
372 autofs4_free_ino(inf);
376 /* For dentries of directories in the root dir */
377 static struct dentry_operations autofs4_root_dentry_operations = {
378 .d_revalidate = autofs4_revalidate,
379 .d_release = autofs4_dentry_release,
382 /* For other dentries */
383 static struct dentry_operations autofs4_dentry_operations = {
384 .d_revalidate = autofs4_revalidate,
385 .d_release = autofs4_dentry_release,
388 /* Lookups in the root directory */
389 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
391 struct autofs_sb_info *sbi;
394 DPRINTK("name = %.*s",
395 dentry->d_name.len, dentry->d_name.name);
397 /* File name too long to exist */
398 if (dentry->d_name.len > NAME_MAX)
399 return ERR_PTR(-ENAMETOOLONG);
401 sbi = autofs4_sbi(dir->i_sb);
402 oz_mode = autofs4_oz_mode(sbi);
404 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
405 current->pid, process_group(current), sbi->catatonic, oz_mode);
408 * Mark the dentry incomplete, but add it. This is needed so
409 * that the VFS layer knows about the dentry, and we can count
410 * on catching any lookups through the revalidate.
412 * Let all the hard work be done by the revalidate function that
413 * needs to be able to do this anyway..
415 * We need to do this before we release the directory semaphore.
417 dentry->d_op = &autofs4_root_dentry_operations;
420 spin_lock(&dentry->d_lock);
421 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
422 spin_unlock(&dentry->d_lock);
424 dentry->d_fsdata = NULL;
427 if (dentry->d_op && dentry->d_op->d_revalidate) {
428 mutex_unlock(&dir->i_mutex);
429 (dentry->d_op->d_revalidate)(dentry, nd);
430 mutex_lock(&dir->i_mutex);
434 * If we are still pending, check if we had to handle
435 * a signal. If so we can force a restart..
437 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
438 /* See if we were interrupted */
439 if (signal_pending(current)) {
440 sigset_t *sigset = ¤t->pending.signal;
441 if (sigismember (sigset, SIGKILL) ||
442 sigismember (sigset, SIGQUIT) ||
443 sigismember (sigset, SIGINT)) {
444 return ERR_PTR(-ERESTARTNOINTR);
450 * If this dentry is unhashed, then we shouldn't honour this
451 * lookup even if the dentry is positive. Returning ENOENT here
452 * doesn't do the right thing for all system calls, but it should
453 * be OK for the operations we permit from an autofs.
455 if (dentry->d_inode && d_unhashed(dentry))
456 return ERR_PTR(-ENOENT);
461 static int autofs4_dir_symlink(struct inode *dir,
462 struct dentry *dentry,
465 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
466 struct autofs_info *ino = autofs4_dentry_ino(dentry);
467 struct autofs_info *p_ino;
471 DPRINTK("%s <- %.*s", symname,
472 dentry->d_name.len, dentry->d_name.name);
474 if (!autofs4_oz_mode(sbi))
477 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
481 ino->size = strlen(symname);
482 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
491 inode = autofs4_get_inode(dir->i_sb, ino);
492 d_instantiate(dentry, inode);
494 if (dir == dir->i_sb->s_root->d_inode)
495 dentry->d_op = &autofs4_root_dentry_operations;
497 dentry->d_op = &autofs4_dentry_operations;
499 dentry->d_fsdata = ino;
500 ino->dentry = dget(dentry);
501 atomic_inc(&ino->count);
502 p_ino = autofs4_dentry_ino(dentry->d_parent);
503 if (p_ino && dentry->d_parent != dentry)
504 atomic_inc(&p_ino->count);
507 dir->i_mtime = CURRENT_TIME;
515 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
516 * that the file no longer exists. However, doing that means that the
517 * VFS layer can turn the dentry into a negative dentry. We don't want
518 * this, because since the unlink is probably the result of an expire.
519 * We simply d_drop it, which allows the dentry lookup to remount it
522 * If a process is blocked on the dentry waiting for the expire to finish,
523 * it will invalidate the dentry and try to mount with a new one.
525 * Also see autofs4_dir_rmdir()..
527 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
529 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
530 struct autofs_info *ino = autofs4_dentry_ino(dentry);
531 struct autofs_info *p_ino;
533 /* This allows root to remove symlinks */
534 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
537 if (atomic_dec_and_test(&ino->count)) {
538 p_ino = autofs4_dentry_ino(dentry->d_parent);
539 if (p_ino && dentry->d_parent != dentry)
540 atomic_dec(&p_ino->count);
544 dentry->d_inode->i_size = 0;
545 dentry->d_inode->i_nlink = 0;
547 dir->i_mtime = CURRENT_TIME;
554 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
556 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
557 struct autofs_info *ino = autofs4_dentry_ino(dentry);
558 struct autofs_info *p_ino;
560 if (!autofs4_oz_mode(sbi))
563 spin_lock(&dcache_lock);
564 if (!list_empty(&dentry->d_subdirs)) {
565 spin_unlock(&dcache_lock);
568 spin_lock(&dentry->d_lock);
570 spin_unlock(&dentry->d_lock);
571 spin_unlock(&dcache_lock);
573 if (atomic_dec_and_test(&ino->count)) {
574 p_ino = autofs4_dentry_ino(dentry->d_parent);
575 if (p_ino && dentry->d_parent != dentry)
576 atomic_dec(&p_ino->count);
579 dentry->d_inode->i_size = 0;
580 dentry->d_inode->i_nlink = 0;
588 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
590 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
591 struct autofs_info *ino = autofs4_dentry_ino(dentry);
592 struct autofs_info *p_ino;
595 if ( !autofs4_oz_mode(sbi) )
598 DPRINTK("dentry %p, creating %.*s",
599 dentry, dentry->d_name.len, dentry->d_name.name);
601 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
605 inode = autofs4_get_inode(dir->i_sb, ino);
606 d_instantiate(dentry, inode);
608 if (dir == dir->i_sb->s_root->d_inode)
609 dentry->d_op = &autofs4_root_dentry_operations;
611 dentry->d_op = &autofs4_dentry_operations;
613 dentry->d_fsdata = ino;
614 ino->dentry = dget(dentry);
615 atomic_inc(&ino->count);
616 p_ino = autofs4_dentry_ino(dentry->d_parent);
617 if (p_ino && dentry->d_parent != dentry)
618 atomic_inc(&p_ino->count);
621 dir->i_mtime = CURRENT_TIME;
626 /* Get/set timeout ioctl() operation */
627 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
628 unsigned long __user *p)
631 unsigned long ntimeout;
633 if ( (rv = get_user(ntimeout, p)) ||
634 (rv = put_user(sbi->exp_timeout/HZ, p)) )
637 if ( ntimeout > ULONG_MAX/HZ )
638 sbi->exp_timeout = 0;
640 sbi->exp_timeout = ntimeout * HZ;
645 /* Return protocol version */
646 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
648 return put_user(sbi->version, p);
651 /* Return protocol sub version */
652 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
654 return put_user(sbi->sub_version, p);
658 * Tells the daemon whether we need to reghost or not. Also, clears
659 * the reghost_needed flag.
661 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
665 DPRINTK("returning %d", sbi->needs_reghost);
667 status = put_user(sbi->needs_reghost, p);
671 sbi->needs_reghost = 0;
676 * Enable / Disable reghosting ioctl() operation
678 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
683 status = get_user(val, p);
685 DPRINTK("reghost = %d", val);
690 /* turn on/off reghosting, with the val */
691 sbi->reghost_enabled = val;
696 * Tells the daemon whether it can umount the autofs mount.
698 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
702 if (may_umount(mnt) == 0)
705 DPRINTK("returning %d", status);
707 status = put_user(status, p);
712 /* Identify autofs4_dentries - this is so we can tell if there's
713 an extra dentry refcount or not. We only hold a refcount on the
714 dentry if its non-negative (ie, d_inode != NULL)
716 int is_autofs4_dentry(struct dentry *dentry)
718 return dentry && dentry->d_inode &&
719 (dentry->d_op == &autofs4_root_dentry_operations ||
720 dentry->d_op == &autofs4_dentry_operations) &&
721 dentry->d_fsdata != NULL;
725 * ioctl()'s on the root directory is the chief method for the daemon to
726 * generate kernel reactions
728 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
729 unsigned int cmd, unsigned long arg)
731 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
732 void __user *p = (void __user *)arg;
734 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
735 cmd,arg,sbi,process_group(current));
737 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
738 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
741 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
745 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
746 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
747 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
748 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
749 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
750 autofs4_catatonic_mode(sbi);
752 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
753 return autofs4_get_protover(sbi, p);
754 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
755 return autofs4_get_protosubver(sbi, p);
756 case AUTOFS_IOC_SETTIMEOUT:
757 return autofs4_get_set_timeout(sbi, p);
759 case AUTOFS_IOC_TOGGLEREGHOST:
760 return autofs4_toggle_reghost(sbi, p);
761 case AUTOFS_IOC_ASKREGHOST:
762 return autofs4_ask_reghost(sbi, p);
764 case AUTOFS_IOC_ASKUMOUNT:
765 return autofs4_ask_umount(filp->f_vfsmnt, p);
767 /* return a single thing to expire */
768 case AUTOFS_IOC_EXPIRE:
769 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
770 /* same as above, but can send multiple expires through pipe */
771 case AUTOFS_IOC_EXPIRE_MULTI:
772 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);