[PATCH] delete from_swap_cache BUG_ONs
[linux-ginger.git] / fs / autofs / root.c
bloba1ab1c0ed21584ba5a6a0cb5195f2e89c505e7bd
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>
18 #include "autofs_i.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;
48 off_t onr, nr;
50 lock_kernel();
52 sbi = autofs_sbi(inode->i_sb);
53 dirhash = &sbi->dirhash;
54 nr = filp->f_pos;
56 switch(nr)
58 case 0:
59 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
60 goto out;
61 filp->f_pos = ++nr;
62 /* fall through */
63 case 1:
64 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
65 goto out;
66 filp->f_pos = ++nr;
67 /* fall through */
68 default:
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)
72 goto out;
73 filp->f_pos = nr;
76 break;
79 out:
80 unlock_kernel();
81 return 0;
84 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
86 struct inode * inode;
87 struct autofs_dir_ent *ent;
88 int status = 0;
90 if ( !(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) ) {
91 do {
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;
102 return 1;
103 } else if (status) {
104 /* Return a negative dentry, but leave it "pending" */
105 return 1;
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);
117 if (!inode) {
118 /* Failed, but leave pending for next time */
119 return 1;
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;
137 return 1;
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
145 * lookups..
147 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
149 struct inode * dir;
150 struct autofs_sb_info *sbi;
151 struct autofs_dir_ent *ent;
152 int res;
154 lock_kernel();
155 dir = dentry->d_parent->d_inode;
156 sbi = autofs_sbi(dir->i_sb);
158 /* Pending dentry */
159 if ( dentry->d_flags & DCACHE_AUTOFS_PENDING ) {
160 if (autofs_oz_mode(sbi))
161 res = 1;
162 else
163 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
164 unlock_kernel();
165 return res;
168 /* Negative dentry.. invalidate if "old" */
169 if (!dentry->d_inode) {
170 unlock_kernel();
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))
177 res = 1;
178 else
179 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
180 unlock_kernel();
181 return res;
184 /* Update the usage list */
185 if ( !autofs_oz_mode(sbi) ) {
186 ent = (struct autofs_dir_ent *) dentry->d_time;
187 if ( ent )
188 autofs_update_usage(&sbi->dirhash,ent);
190 unlock_kernel();
191 return 1;
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;
201 int oz_mode;
203 DPRINTK(("autofs_root_lookup: name = "));
204 lock_kernel();
205 autofs_say(dentry->d_name.name,dentry->d_name.len);
207 if (dentry->d_name.len > NAME_MAX) {
208 unlock_kernel();
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;
230 d_add(dentry, NULL);
232 up(&dir->i_sem);
233 autofs_revalidate(dentry, nd);
234 down(&dir->i_sem);
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 = &current->pending.signal;
244 if (sigismember (sigset, SIGKILL) ||
245 sigismember (sigset, SIGQUIT) ||
246 sigismember (sigset, SIGINT)) {
247 unlock_kernel();
248 return ERR_PTR(-ERESTARTNOINTR);
252 unlock_kernel();
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);
263 return NULL;
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;
271 unsigned int n;
272 int slsize;
273 struct autofs_symlink *sl;
275 DPRINTK(("autofs_root_symlink: %s <- ", symname));
276 autofs_say(dentry->d_name.name,dentry->d_name.len);
278 lock_kernel();
279 if ( !autofs_oz_mode(sbi) ) {
280 unlock_kernel();
281 return -EACCES;
284 if ( autofs_hash_lookup(dh, &dentry->d_name) ) {
285 unlock_kernel();
286 return -EEXIST;
289 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
290 if ( n >= AUTOFS_MAX_SYMLINKS ) {
291 unlock_kernel();
292 return -ENOSPC;
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);
299 if ( !sl->data ) {
300 clear_bit(n,sbi->symlink_bitmap);
301 unlock_kernel();
302 return -ENOSPC;
305 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
306 if ( !ent ) {
307 kfree(sl->data);
308 clear_bit(n,sbi->symlink_bitmap);
309 unlock_kernel();
310 return -ENOSPC;
313 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
314 if ( !ent->name ) {
315 kfree(sl->data);
316 kfree(ent);
317 clear_bit(n,sbi->symlink_bitmap);
318 unlock_kernel();
319 return -ENOSPC;
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));
332 unlock_kernel();
333 return 0;
337 * NOTE!
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;
352 unsigned int n;
354 /* This allows root to remove symlinks */
355 lock_kernel();
356 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) ) {
357 unlock_kernel();
358 return -EACCES;
361 ent = autofs_hash_lookup(dh, &dentry->d_name);
362 if ( !ent ) {
363 unlock_kernel();
364 return -ENOENT;
367 n = ent->ino - AUTOFS_FIRST_SYMLINK;
368 if ( n >= AUTOFS_MAX_SYMLINKS ) {
369 unlock_kernel();
370 return -EISDIR; /* It's a directory, dummy */
372 if ( !test_bit(n,sbi->symlink_bitmap) ) {
373 unlock_kernel();
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);
381 d_drop(dentry);
383 unlock_kernel();
384 return 0;
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;
393 lock_kernel();
394 if ( !autofs_oz_mode(sbi) ) {
395 unlock_kernel();
396 return -EACCES;
399 ent = autofs_hash_lookup(dh, &dentry->d_name);
400 if ( !ent ) {
401 unlock_kernel();
402 return -ENOENT;
405 if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO ) {
406 unlock_kernel();
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);
416 dir->i_nlink--;
417 d_drop(dentry);
418 unlock_kernel();
420 return 0;
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;
428 ino_t ino;
430 lock_kernel();
431 if ( !autofs_oz_mode(sbi) ) {
432 unlock_kernel();
433 return -EACCES;
436 ent = autofs_hash_lookup(dh, &dentry->d_name);
437 if ( ent ) {
438 unlock_kernel();
439 return -EEXIST;
442 if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) {
443 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
444 unlock_kernel();
445 return -ENOSPC;
447 ino = sbi->next_dir_ino++;
449 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
450 if ( !ent ) {
451 unlock_kernel();
452 return -ENOSPC;
455 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
456 if ( !ent->name ) {
457 kfree(ent);
458 unlock_kernel();
459 return -ENOSPC;
462 ent->hash = dentry->d_name.hash;
463 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
464 ent->ino = ino;
465 ent->dentry = dentry;
466 autofs_hash_insert(dh,ent);
468 dir->i_nlink++;
469 d_instantiate(dentry, iget(dir->i_sb,ino));
470 unlock_kernel();
472 return 0;
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))
483 return -EFAULT;
485 if ( ntimeout > ULONG_MAX/HZ )
486 sbi->exp_timeout = 0;
487 else
488 sbi->exp_timeout = ntimeout * HZ;
490 return 0;
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)) )
515 return -EAGAIN;
517 pkt.len = ent->len;
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)) )
522 return -EFAULT;
524 return 0;
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 )
541 return -ENOTTY;
543 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
544 return -EPERM;
546 switch(cmd) {
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);
553 return 0;
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,
560 argp);
561 default:
562 return -ENOSYS;