1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/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/errno.h>
14 #include <linux/stat.h>
15 #include <linux/param.h>
16 #include <linux/time.h>
17 #include <linux/smp_lock.h>
20 static int autofs_root_readdir(struct file *,void *,filldir_t);
21 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
22 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs_root_unlink(struct inode *,struct dentry *);
24 static int autofs_root_rmdir(struct inode *,struct dentry *);
25 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
26 static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28 struct file_operations autofs_root_operations = {
29 .read = generic_read_dir,
30 .readdir = autofs_root_readdir,
31 .ioctl = autofs_root_ioctl,
34 struct inode_operations autofs_root_inode_operations = {
35 .lookup = autofs_root_lookup,
36 .unlink = autofs_root_unlink,
37 .symlink = autofs_root_symlink,
38 .mkdir = autofs_root_mkdir,
39 .rmdir = autofs_root_rmdir,
42 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
44 struct autofs_dir_ent *ent = NULL;
45 struct autofs_dirhash *dirhash;
46 struct autofs_sb_info *sbi;
47 struct inode * inode = filp->f_dentry->d_inode;
52 sbi = autofs_sbi(inode->i_sb);
53 dirhash = &sbi->dirhash;
59 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
64 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
69 while ( onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent) ) {
70 if ( !ent->dentry || d_mountpoint(ent->dentry) ) {
71 if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
84 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
87 struct autofs_dir_ent *ent;
90 if ( !(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) ) {
92 if ( status && dentry->d_inode ) {
93 if ( status != -ENOENT )
94 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
95 return 0; /* Try to get the kernel to invalidate this dentry */
98 /* Turn this into a real negative dentry? */
99 if (status == -ENOENT) {
100 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
101 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
104 /* Return a negative dentry, but leave it "pending" */
107 status = autofs_wait(sbi, &dentry->d_name);
108 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) );
111 /* Abuse this field as a pointer to the directory entry, used to
112 find the expire list pointers */
113 dentry->d_time = (unsigned long) ent;
115 if (!dentry->d_inode) {
116 inode = iget(sb, ent->ino);
118 /* Failed, but leave pending for next time */
121 dentry->d_inode = inode;
124 /* If this is a directory that isn't a mount point, bitch at the
125 daemon and fix it in user space */
126 if ( S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry) ) {
127 return !autofs_wait(sbi, &dentry->d_name);
130 /* We don't update the usages for the autofs daemon itself, this
131 is necessary for recursive autofs mounts */
132 if ( !autofs_oz_mode(sbi) ) {
133 autofs_update_usage(&sbi->dirhash,ent);
136 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
142 * Revalidate is called on every cache lookup. Some of those
143 * cache lookups may actually happen while the dentry is not
144 * yet completely filled in, and revalidate has to delay such
147 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
150 struct autofs_sb_info *sbi;
151 struct autofs_dir_ent *ent;
155 dir = dentry->d_parent->d_inode;
156 sbi = autofs_sbi(dir->i_sb);
159 if ( dentry->d_flags & DCACHE_AUTOFS_PENDING ) {
160 if (autofs_oz_mode(sbi))
163 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
168 /* Negative dentry.. invalidate if "old" */
169 if (!dentry->d_inode) {
171 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
174 /* Check for a non-mountpoint directory */
175 if ( S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry) ) {
176 if (autofs_oz_mode(sbi))
179 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
184 /* Update the usage list */
185 if ( !autofs_oz_mode(sbi) ) {
186 ent = (struct autofs_dir_ent *) dentry->d_time;
188 autofs_update_usage(&sbi->dirhash,ent);
194 static struct dentry_operations autofs_dentry_operations = {
195 .d_revalidate = autofs_revalidate,
198 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
200 struct autofs_sb_info *sbi;
203 DPRINTK(("autofs_root_lookup: name = "));
205 autofs_say(dentry->d_name.name,dentry->d_name.len);
207 if (dentry->d_name.len > NAME_MAX) {
209 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
212 sbi = autofs_sbi(dir->i_sb);
214 oz_mode = autofs_oz_mode(sbi);
215 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
216 current->pid, process_group(current), sbi->catatonic, oz_mode));
219 * Mark the dentry incomplete, but add it. This is needed so
220 * that the VFS layer knows about the dentry, and we can count
221 * on catching any lookups through the revalidate.
223 * Let all the hard work be done by the revalidate function that
224 * needs to be able to do this anyway..
226 * We need to do this before we release the directory semaphore.
228 dentry->d_op = &autofs_dentry_operations;
229 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
233 autofs_revalidate(dentry, nd);
237 * If we are still pending, check if we had to handle
238 * a signal. If so we can force a restart..
240 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
241 /* See if we were interrupted */
242 if (signal_pending(current)) {
243 sigset_t *sigset = ¤t->pending.signal;
244 if (sigismember (sigset, SIGKILL) ||
245 sigismember (sigset, SIGQUIT) ||
246 sigismember (sigset, SIGINT)) {
248 return ERR_PTR(-ERESTARTNOINTR);
255 * If this dentry is unhashed, then we shouldn't honour this
256 * lookup even if the dentry is positive. Returning ENOENT here
257 * doesn't do the right thing for all system calls, but it should
258 * be OK for the operations we permit from an autofs.
260 if ( dentry->d_inode && d_unhashed(dentry) )
261 return ERR_PTR(-ENOENT);
266 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
268 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
269 struct autofs_dirhash *dh = &sbi->dirhash;
270 struct autofs_dir_ent *ent;
273 struct autofs_symlink *sl;
275 DPRINTK(("autofs_root_symlink: %s <- ", symname));
276 autofs_say(dentry->d_name.name,dentry->d_name.len);
279 if ( !autofs_oz_mode(sbi) ) {
284 if ( autofs_hash_lookup(dh, &dentry->d_name) ) {
289 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
290 if ( n >= AUTOFS_MAX_SYMLINKS ) {
295 set_bit(n,sbi->symlink_bitmap);
296 sl = &sbi->symlink[n];
297 sl->len = strlen(symname);
298 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
300 clear_bit(n,sbi->symlink_bitmap);
305 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
308 clear_bit(n,sbi->symlink_bitmap);
313 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
317 clear_bit(n,sbi->symlink_bitmap);
322 memcpy(sl->data,symname,slsize);
323 sl->mtime = get_seconds();
325 ent->ino = AUTOFS_FIRST_SYMLINK + n;
326 ent->hash = dentry->d_name.hash;
327 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
328 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
330 autofs_hash_insert(dh,ent);
331 d_instantiate(dentry, iget(dir->i_sb,ent->ino));
339 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
340 * that the file no longer exists. However, doing that means that the
341 * VFS layer can turn the dentry into a negative dentry, which we
342 * obviously do not want (we're dropping the entry not because it
343 * doesn't exist, but because it has timed out).
345 * Also see autofs_root_rmdir()..
347 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
349 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
350 struct autofs_dirhash *dh = &sbi->dirhash;
351 struct autofs_dir_ent *ent;
354 /* This allows root to remove symlinks */
356 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) {
361 ent = autofs_hash_lookup(dh, &dentry->d_name);
367 n = ent->ino - AUTOFS_FIRST_SYMLINK;
368 if ( n >= AUTOFS_MAX_SYMLINKS ) {
370 return -EISDIR; /* It's a directory, dummy */
372 if ( !test_bit(n,sbi->symlink_bitmap) ) {
374 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
377 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
378 autofs_hash_delete(ent);
379 clear_bit(n,sbi->symlink_bitmap);
380 kfree(sbi->symlink[n].data);
387 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
389 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
390 struct autofs_dirhash *dh = &sbi->dirhash;
391 struct autofs_dir_ent *ent;
394 if ( !autofs_oz_mode(sbi) ) {
399 ent = autofs_hash_lookup(dh, &dentry->d_name);
405 if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO ) {
407 return -ENOTDIR; /* Not a directory */
410 if ( ent->dentry != dentry ) {
411 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
414 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
415 autofs_hash_delete(ent);
423 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
425 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
426 struct autofs_dirhash *dh = &sbi->dirhash;
427 struct autofs_dir_ent *ent;
431 if ( !autofs_oz_mode(sbi) ) {
436 ent = autofs_hash_lookup(dh, &dentry->d_name);
442 if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) {
443 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
447 ino = sbi->next_dir_ino++;
449 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
455 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
462 ent->hash = dentry->d_name.hash;
463 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
465 ent->dentry = dentry;
466 autofs_hash_insert(dh,ent);
469 d_instantiate(dentry, iget(dir->i_sb,ino));
475 /* Get/set timeout ioctl() operation */
476 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
477 unsigned long __user *p)
479 unsigned long ntimeout;
481 if (get_user(ntimeout, p) ||
482 put_user(sbi->exp_timeout / HZ, p))
485 if ( ntimeout > ULONG_MAX/HZ )
486 sbi->exp_timeout = 0;
488 sbi->exp_timeout = ntimeout * HZ;
493 /* Return protocol version */
494 static inline int autofs_get_protover(int __user *p)
496 return put_user(AUTOFS_PROTO_VERSION, p);
499 /* Perform an expiry operation */
500 static inline int autofs_expire_run(struct super_block *sb,
501 struct autofs_sb_info *sbi,
502 struct vfsmount *mnt,
503 struct autofs_packet_expire __user *pkt_p)
505 struct autofs_dir_ent *ent;
506 struct autofs_packet_expire pkt;
508 memset(&pkt,0,sizeof pkt);
510 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
511 pkt.hdr.type = autofs_ptype_expire;
513 if ( !sbi->exp_timeout ||
514 !(ent = autofs_expire(sb,sbi,mnt)) )
518 memcpy(pkt.name, ent->name, pkt.len);
519 pkt.name[pkt.len] = '\0';
521 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
528 * ioctl()'s on the root directory is the chief method for the daemon to
529 * generate kernel reactions
531 static int autofs_root_ioctl(struct inode *inode, struct file *filp,
532 unsigned int cmd, unsigned long arg)
534 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
535 void __user *argp = (void __user *)arg;
537 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,process_group(current)));
539 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
540 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
543 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
547 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
548 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
549 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
550 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
551 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
552 autofs_catatonic_mode(sbi);
554 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
555 return autofs_get_protover(argp);
556 case AUTOFS_IOC_SETTIMEOUT:
557 return autofs_get_set_timeout(sbi, argp);
558 case AUTOFS_IOC_EXPIRE:
559 return autofs_expire_run(inode->i_sb, sbi, filp->f_vfsmnt,