1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * drivers/staging/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
7 * This file is part of the Linux kernel and is made available under
8 * the terms of the GNU General Public License, version 2, or at your
9 * option, any later version, incorporated herein by reference.
11 * ------------------------------------------------------------------------- */
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/stat.h>
16 #include <linux/slab.h>
17 #include <linux/param.h>
18 #include <linux/time.h>
19 #include <linux/compat.h>
20 #include <linux/smp_lock.h>
23 static int autofs_root_readdir(struct file *,void *,filldir_t);
24 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
25 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
26 static int autofs_root_unlink(struct inode *,struct dentry *);
27 static int autofs_root_rmdir(struct inode *,struct dentry *);
28 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
29 static long autofs_root_ioctl(struct file *,unsigned int,unsigned long);
31 static long autofs_root_compat_ioctl(struct file *,unsigned int,unsigned long);
34 const struct file_operations autofs_root_operations = {
35 .llseek = generic_file_llseek,
36 .read = generic_read_dir,
37 .readdir = autofs_root_readdir,
38 .unlocked_ioctl = autofs_root_ioctl,
40 .compat_ioctl = autofs_root_compat_ioctl,
44 const struct inode_operations autofs_root_inode_operations = {
45 .lookup = autofs_root_lookup,
46 .unlink = autofs_root_unlink,
47 .symlink = autofs_root_symlink,
48 .mkdir = autofs_root_mkdir,
49 .rmdir = autofs_root_rmdir,
52 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
54 struct autofs_dir_ent *ent = NULL;
55 struct autofs_dirhash *dirhash;
56 struct autofs_sb_info *sbi;
57 struct inode * inode = filp->f_path.dentry->d_inode;
62 sbi = autofs_sbi(inode->i_sb);
63 dirhash = &sbi->dirhash;
69 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
74 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
79 while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
80 if (!ent->dentry || d_mountpoint(ent->dentry)) {
81 if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
94 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
97 struct autofs_dir_ent *ent;
100 if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
102 if (status && dentry->d_inode) {
103 if (status != -ENOENT)
104 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
105 return 0; /* Try to get the kernel to invalidate this dentry */
108 /* Turn this into a real negative dentry? */
109 if (status == -ENOENT) {
110 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
111 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
114 /* Return a negative dentry, but leave it "pending" */
117 status = autofs_wait(sbi, &dentry->d_name);
118 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
121 /* Abuse this field as a pointer to the directory entry, used to
122 find the expire list pointers */
123 dentry->d_time = (unsigned long) ent;
125 if (!dentry->d_inode) {
126 inode = autofs_iget(sb, ent->ino);
128 /* Failed, but leave pending for next time */
131 dentry->d_inode = inode;
134 /* If this is a directory that isn't a mount point, bitch at the
135 daemon and fix it in user space */
136 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
137 return !autofs_wait(sbi, &dentry->d_name);
140 /* We don't update the usages for the autofs daemon itself, this
141 is necessary for recursive autofs mounts */
142 if (!autofs_oz_mode(sbi)) {
143 autofs_update_usage(&sbi->dirhash,ent);
146 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
152 * Revalidate is called on every cache lookup. Some of those
153 * cache lookups may actually happen while the dentry is not
154 * yet completely filled in, and revalidate has to delay such
157 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
160 struct autofs_sb_info *sbi;
161 struct autofs_dir_ent *ent;
165 dir = dentry->d_parent->d_inode;
166 sbi = autofs_sbi(dir->i_sb);
169 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
170 if (autofs_oz_mode(sbi))
173 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
178 /* Negative dentry.. invalidate if "old" */
179 if (!dentry->d_inode) {
181 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
184 /* Check for a non-mountpoint directory */
185 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
186 if (autofs_oz_mode(sbi))
189 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
194 /* Update the usage list */
195 if (!autofs_oz_mode(sbi)) {
196 ent = (struct autofs_dir_ent *) dentry->d_time;
198 autofs_update_usage(&sbi->dirhash,ent);
204 static const struct dentry_operations autofs_dentry_operations = {
205 .d_revalidate = autofs_revalidate,
208 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
210 struct autofs_sb_info *sbi;
213 DPRINTK(("autofs_root_lookup: name = "));
215 autofs_say(dentry->d_name.name,dentry->d_name.len);
217 if (dentry->d_name.len > NAME_MAX) {
219 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
222 sbi = autofs_sbi(dir->i_sb);
224 oz_mode = autofs_oz_mode(sbi);
225 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
226 "oz_mode = %d\n", task_pid_nr(current),
227 task_pgrp_nr(current), sbi->catatonic,
231 * Mark the dentry incomplete, but add it. This is needed so
232 * that the VFS layer knows about the dentry, and we can count
233 * on catching any lookups through the revalidate.
235 * Let all the hard work be done by the revalidate function that
236 * needs to be able to do this anyway..
238 * We need to do this before we release the directory semaphore.
240 d_set_d_op(dentry, &autofs_dentry_operations);
241 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
244 mutex_unlock(&dir->i_mutex);
245 autofs_revalidate(dentry, nd);
246 mutex_lock(&dir->i_mutex);
249 * If we are still pending, check if we had to handle
250 * a signal. If so we can force a restart..
252 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
253 /* See if we were interrupted */
254 if (signal_pending(current)) {
255 sigset_t *sigset = ¤t->pending.signal;
256 if (sigismember (sigset, SIGKILL) ||
257 sigismember (sigset, SIGQUIT) ||
258 sigismember (sigset, SIGINT)) {
260 return ERR_PTR(-ERESTARTNOINTR);
267 * If this dentry is unhashed, then we shouldn't honour this
268 * lookup even if the dentry is positive. Returning ENOENT here
269 * doesn't do the right thing for all system calls, but it should
270 * be OK for the operations we permit from an autofs.
272 if (dentry->d_inode && d_unhashed(dentry))
273 return ERR_PTR(-ENOENT);
278 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
280 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
281 struct autofs_dirhash *dh = &sbi->dirhash;
282 struct autofs_dir_ent *ent;
285 struct autofs_symlink *sl;
288 DPRINTK(("autofs_root_symlink: %s <- ", symname));
289 autofs_say(dentry->d_name.name,dentry->d_name.len);
292 if (!autofs_oz_mode(sbi)) {
297 if (autofs_hash_lookup(dh, &dentry->d_name)) {
302 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
303 if (n >= AUTOFS_MAX_SYMLINKS) {
308 set_bit(n,sbi->symlink_bitmap);
309 sl = &sbi->symlink[n];
310 sl->len = strlen(symname);
311 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
313 clear_bit(n,sbi->symlink_bitmap);
318 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
321 clear_bit(n,sbi->symlink_bitmap);
326 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
330 clear_bit(n,sbi->symlink_bitmap);
335 memcpy(sl->data,symname,slsize);
336 sl->mtime = get_seconds();
338 ent->ino = AUTOFS_FIRST_SYMLINK + n;
339 ent->hash = dentry->d_name.hash;
340 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
341 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
343 autofs_hash_insert(dh,ent);
345 inode = autofs_iget(dir->i_sb, ent->ino);
347 return PTR_ERR(inode);
349 d_instantiate(dentry, inode);
357 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
358 * that the file no longer exists. However, doing that means that the
359 * VFS layer can turn the dentry into a negative dentry, which we
360 * obviously do not want (we're dropping the entry not because it
361 * doesn't exist, but because it has timed out).
363 * Also see autofs_root_rmdir()..
365 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
367 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
368 struct autofs_dirhash *dh = &sbi->dirhash;
369 struct autofs_dir_ent *ent;
372 /* This allows root to remove symlinks */
374 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
379 ent = autofs_hash_lookup(dh, &dentry->d_name);
385 n = ent->ino - AUTOFS_FIRST_SYMLINK;
386 if (n >= AUTOFS_MAX_SYMLINKS) {
388 return -EISDIR; /* It's a directory, dummy */
390 if (!test_bit(n,sbi->symlink_bitmap)) {
392 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
395 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
396 autofs_hash_delete(ent);
397 clear_bit(n,sbi->symlink_bitmap);
398 kfree(sbi->symlink[n].data);
405 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
407 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
408 struct autofs_dirhash *dh = &sbi->dirhash;
409 struct autofs_dir_ent *ent;
412 if (!autofs_oz_mode(sbi)) {
417 ent = autofs_hash_lookup(dh, &dentry->d_name);
423 if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
425 return -ENOTDIR; /* Not a directory */
428 if (ent->dentry != dentry) {
429 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
432 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
433 autofs_hash_delete(ent);
441 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
443 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
444 struct autofs_dirhash *dh = &sbi->dirhash;
445 struct autofs_dir_ent *ent;
450 if (!autofs_oz_mode(sbi)) {
455 ent = autofs_hash_lookup(dh, &dentry->d_name);
461 if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
462 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
466 ino = sbi->next_dir_ino++;
468 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
474 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
481 ent->hash = dentry->d_name.hash;
482 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
484 ent->dentry = dentry;
485 autofs_hash_insert(dh,ent);
489 inode = autofs_iget(dir->i_sb, ino);
492 return PTR_ERR(inode);
495 d_instantiate(dentry, inode);
501 /* Get/set timeout ioctl() operation */
503 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info *sbi,
504 unsigned int __user *p)
506 unsigned long ntimeout;
508 if (get_user(ntimeout, p) ||
509 put_user(sbi->exp_timeout / HZ, p))
512 if (ntimeout > UINT_MAX/HZ)
513 sbi->exp_timeout = 0;
515 sbi->exp_timeout = ntimeout * HZ;
521 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
522 unsigned long __user *p)
524 unsigned long ntimeout;
526 if (get_user(ntimeout, p) ||
527 put_user(sbi->exp_timeout / HZ, p))
530 if (ntimeout > ULONG_MAX/HZ)
531 sbi->exp_timeout = 0;
533 sbi->exp_timeout = ntimeout * HZ;
538 /* Return protocol version */
539 static inline int autofs_get_protover(int __user *p)
541 return put_user(AUTOFS_PROTO_VERSION, p);
544 /* Perform an expiry operation */
545 static inline int autofs_expire_run(struct super_block *sb,
546 struct autofs_sb_info *sbi,
547 struct vfsmount *mnt,
548 struct autofs_packet_expire __user *pkt_p)
550 struct autofs_dir_ent *ent;
551 struct autofs_packet_expire pkt;
553 memset(&pkt,0,sizeof pkt);
555 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
556 pkt.hdr.type = autofs_ptype_expire;
558 if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
562 memcpy(pkt.name, ent->name, pkt.len);
563 pkt.name[pkt.len] = '\0';
565 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
572 * ioctl()'s on the root directory is the chief method for the daemon to
573 * generate kernel reactions
575 static int autofs_do_root_ioctl(struct inode *inode, struct file *filp,
576 unsigned int cmd, unsigned long arg)
578 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
579 void __user *argp = (void __user *)arg;
581 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
583 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
584 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
587 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
591 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
592 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
593 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
594 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
595 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
596 autofs_catatonic_mode(sbi);
598 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
599 return autofs_get_protover(argp);
601 case AUTOFS_IOC_SETTIMEOUT32:
602 return autofs_compat_get_set_timeout(sbi, argp);
604 case AUTOFS_IOC_SETTIMEOUT:
605 return autofs_get_set_timeout(sbi, argp);
606 case AUTOFS_IOC_EXPIRE:
607 return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
615 static long autofs_root_ioctl(struct file *filp,
616 unsigned int cmd, unsigned long arg)
621 ret = autofs_do_root_ioctl(filp->f_path.dentry->d_inode,
629 static long autofs_root_compat_ioctl(struct file *filp,
630 unsigned int cmd, unsigned long arg)
632 struct inode *inode = filp->f_path.dentry->d_inode;
636 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
637 ret = autofs_do_root_ioctl(inode, filp, cmd, arg);
639 ret = autofs_do_root_ioctl(inode, filp, cmd,
640 (unsigned long)compat_ptr(arg));