Import 2.3.30pre7
[davej-history.git] / fs / autofs / root.c
blob4f0569ca736e8d7fd64709e6dc006a6053621504
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 "autofs_i.h"
18 static int autofs_root_readdir(struct file *,void *,filldir_t);
19 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *);
20 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
21 static int autofs_root_unlink(struct inode *,struct dentry *);
22 static int autofs_root_rmdir(struct inode *,struct dentry *);
23 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
24 static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
26 static struct file_operations autofs_root_operations = {
27 NULL, /* llseek */
28 NULL, /* read */
29 NULL, /* write */
30 autofs_root_readdir, /* readdir */
31 NULL, /* poll */
32 autofs_root_ioctl, /* ioctl */
33 NULL, /* mmap */
34 NULL, /* open */
35 NULL, /* flush */
36 NULL, /* release */
37 NULL, /* fsync */
38 NULL, /* fasync */
39 NULL, /* check_media_change */
40 NULL, /* revalidate */
41 NULL /* lock */
44 struct inode_operations autofs_root_inode_operations = {
45 &autofs_root_operations, /* file operations */
46 NULL, /* create */
47 autofs_root_lookup, /* lookup */
48 NULL, /* link */
49 autofs_root_unlink, /* unlink */
50 autofs_root_symlink, /* symlink */
51 autofs_root_mkdir, /* mkdir */
52 autofs_root_rmdir, /* rmdir */
53 NULL, /* mknod */
54 NULL, /* rename */
55 NULL, /* readlink */
56 NULL, /* follow_link */
57 NULL, /* get_block */
58 NULL, /* readpage */
59 NULL, /* writepage */
60 NULL, /* truncate */
61 NULL, /* permission */
62 NULL /* revalidate */
65 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
67 struct autofs_dir_ent *ent = NULL;
68 struct autofs_dirhash *dirhash;
69 struct autofs_sb_info *sbi;
70 struct inode * inode = filp->f_dentry->d_inode;
71 off_t onr, nr;
73 sbi = autofs_sbi(inode->i_sb);
74 dirhash = &sbi->dirhash;
75 nr = filp->f_pos;
77 switch(nr)
79 case 0:
80 if (filldir(dirent, ".", 1, nr, inode->i_ino) < 0)
81 return 0;
82 filp->f_pos = ++nr;
83 /* fall through */
84 case 1:
85 if (filldir(dirent, "..", 2, nr, inode->i_ino) < 0)
86 return 0;
87 filp->f_pos = ++nr;
88 /* fall through */
89 default:
90 while ( onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent) ) {
91 if ( !ent->dentry || ent->dentry->d_mounts != ent->dentry ) {
92 if (filldir(dirent,ent->name,ent->len,onr,ent->ino) < 0)
93 return 0;
94 filp->f_pos = nr;
97 break;
100 return 0;
103 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
105 struct inode * inode;
106 struct autofs_dir_ent *ent;
107 int status = 0;
109 if ( !(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) ) {
110 do {
111 if ( status && dentry->d_inode ) {
112 if ( status != -ENOENT )
113 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
114 return 0; /* Try to get the kernel to invalidate this dentry */
117 /* Turn this into a real negative dentry? */
118 if (status == -ENOENT) {
119 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
120 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
121 return 1;
122 } else if (status) {
123 /* Return a negative dentry, but leave it "pending" */
124 return 1;
126 status = autofs_wait(sbi, &dentry->d_name);
127 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)) );
130 /* Abuse this field as a pointer to the directory entry, used to
131 find the expire list pointers */
132 dentry->d_time = (unsigned long) ent;
134 if (!dentry->d_inode) {
135 inode = iget(sb, ent->ino);
136 if (!inode) {
137 /* Failed, but leave pending for next time */
138 return 1;
140 dentry->d_inode = inode;
143 /* If this is a directory that isn't a mount point, bitch at the
144 daemon and fix it in user space */
145 if ( S_ISDIR(dentry->d_inode->i_mode) && dentry->d_mounts == dentry ) {
146 return !autofs_wait(sbi, &dentry->d_name);
149 /* We don't update the usages for the autofs daemon itself, this
150 is necessary for recursive autofs mounts */
151 if ( !autofs_oz_mode(sbi) ) {
152 autofs_update_usage(&sbi->dirhash,ent);
155 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
156 return 1;
161 * Revalidate is called on every cache lookup. Some of those
162 * cache lookups may actually happen while the dentry is not
163 * yet completely filled in, and revalidate has to delay such
164 * lookups..
166 static int autofs_revalidate(struct dentry * dentry, int flags)
168 struct inode * dir = dentry->d_parent->d_inode;
169 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
170 struct autofs_dir_ent *ent;
172 /* Pending dentry */
173 if ( dentry->d_flags & DCACHE_AUTOFS_PENDING ) {
174 if (autofs_oz_mode(sbi))
175 return 1;
176 else
177 return try_to_fill_dentry(dentry, dir->i_sb, sbi);
180 /* Negative dentry.. invalidate if "old" */
181 if (!dentry->d_inode)
182 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
184 /* Check for a non-mountpoint directory */
185 if ( S_ISDIR(dentry->d_inode->i_mode) && dentry->d_mounts == dentry ) {
186 if (autofs_oz_mode(sbi))
187 return 1;
188 else
189 return try_to_fill_dentry(dentry, dir->i_sb, sbi);
192 /* Update the usage list */
193 if ( !autofs_oz_mode(sbi) ) {
194 ent = (struct autofs_dir_ent *) dentry->d_time;
195 if ( ent )
196 autofs_update_usage(&sbi->dirhash,ent);
198 return 1;
201 static struct dentry_operations autofs_dentry_operations = {
202 autofs_revalidate, /* d_revalidate */
203 NULL, /* d_hash */
204 NULL, /* d_compare */
207 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry)
209 struct autofs_sb_info *sbi;
210 int oz_mode;
212 DPRINTK(("autofs_root_lookup: name = "));
213 autofs_say(dentry->d_name.name,dentry->d_name.len);
215 if (dentry->d_name.len > NAME_MAX)
216 return ERR_PTR(-ENOENT);/* File name too long to exist */
218 sbi = autofs_sbi(dir->i_sb);
220 oz_mode = autofs_oz_mode(sbi);
221 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
222 current->pid, current->pgrp, sbi->catatonic, oz_mode));
225 * Mark the dentry incomplete, but add it. This is needed so
226 * that the VFS layer knows about the dentry, and we can count
227 * on catching any lookups through the revalidate.
229 * Let all the hard work be done by the revalidate function that
230 * needs to be able to do this anyway..
232 * We need to do this before we release the directory semaphore.
234 dentry->d_op = &autofs_dentry_operations;
235 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
236 d_add(dentry, NULL);
238 up(&dir->i_sem);
239 autofs_revalidate(dentry, 0);
240 down(&dir->i_sem);
243 * If we are still pending, check if we had to handle
244 * a signal. If so we can force a restart..
246 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
247 if (signal_pending(current))
248 return ERR_PTR(-ERESTARTNOINTR);
252 * If this dentry is unhashed, then we shouldn't honour this
253 * lookup even if the dentry is positive. Returning ENOENT here
254 * doesn't do the right thing for all system calls, but it should
255 * be OK for the operations we permit from an autofs.
257 if ( dentry->d_inode && list_empty(&dentry->d_hash) )
258 return ERR_PTR(-ENOENT);
260 return NULL;
263 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
265 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
266 struct autofs_dirhash *dh = &sbi->dirhash;
267 struct autofs_dir_ent *ent;
268 unsigned int n;
269 int slsize;
270 struct autofs_symlink *sl;
272 DPRINTK(("autofs_root_symlink: %s <- ", symname));
273 autofs_say(dentry->d_name.name,dentry->d_name.len);
275 if ( !autofs_oz_mode(sbi) )
276 return -EACCES;
278 if ( dentry->d_name.len > NAME_MAX )
279 return -ENAMETOOLONG;
281 if ( autofs_hash_lookup(dh, &dentry->d_name) )
282 return -EEXIST;
284 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
285 if ( n >= AUTOFS_MAX_SYMLINKS )
286 return -ENOSPC;
288 set_bit(n,sbi->symlink_bitmap);
289 sl = &sbi->symlink[n];
290 sl->len = strlen(symname);
291 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
292 if ( !sl->data ) {
293 clear_bit(n,sbi->symlink_bitmap);
294 return -ENOSPC;
297 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
298 if ( !ent ) {
299 kfree(sl->data);
300 clear_bit(n,sbi->symlink_bitmap);
301 return -ENOSPC;
304 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
305 if ( !ent->name ) {
306 kfree(sl->data);
307 kfree(ent);
308 clear_bit(n,sbi->symlink_bitmap);
309 return -ENOSPC;
312 memcpy(sl->data,symname,slsize);
313 sl->mtime = CURRENT_TIME;
315 ent->ino = AUTOFS_FIRST_SYMLINK + n;
316 ent->hash = dentry->d_name.hash;
317 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
318 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
320 autofs_hash_insert(dh,ent);
321 d_instantiate(dentry, iget(dir->i_sb,ent->ino));
323 return 0;
327 * NOTE!
329 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
330 * that the file no longer exists. However, doing that means that the
331 * VFS layer can turn the dentry into a negative dentry, which we
332 * obviously do not want (we're dropping the entry not because it
333 * doesn't exist, but because it has timed out).
335 * Also see autofs_root_rmdir()..
337 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
339 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
340 struct autofs_dirhash *dh = &sbi->dirhash;
341 struct autofs_dir_ent *ent;
342 unsigned int n;
344 /* This allows root to remove symlinks */
345 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
346 return -EACCES;
348 ent = autofs_hash_lookup(dh, &dentry->d_name);
349 if ( !ent )
350 return -ENOENT;
352 n = ent->ino - AUTOFS_FIRST_SYMLINK;
353 if ( n >= AUTOFS_MAX_SYMLINKS )
354 return -EISDIR; /* It's a directory, dummy */
355 if ( !test_bit(n,sbi->symlink_bitmap) )
356 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
358 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
359 autofs_hash_delete(ent);
360 clear_bit(n,sbi->symlink_bitmap);
361 kfree(sbi->symlink[n].data);
362 d_drop(dentry);
364 return 0;
367 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
369 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
370 struct autofs_dirhash *dh = &sbi->dirhash;
371 struct autofs_dir_ent *ent;
373 if ( !autofs_oz_mode(sbi) )
374 return -EACCES;
376 ent = autofs_hash_lookup(dh, &dentry->d_name);
377 if ( !ent )
378 return -ENOENT;
380 if ( (unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO )
381 return -ENOTDIR; /* Not a directory */
383 if ( ent->dentry != dentry ) {
384 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
387 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
388 autofs_hash_delete(ent);
389 dir->i_nlink--;
390 d_drop(dentry);
392 return 0;
395 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
397 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
398 struct autofs_dirhash *dh = &sbi->dirhash;
399 struct autofs_dir_ent *ent;
400 ino_t ino;
402 if ( !autofs_oz_mode(sbi) )
403 return -EACCES;
405 if ( dentry->d_name.len > NAME_MAX )
406 return -ENAMETOOLONG;
408 ent = autofs_hash_lookup(dh, &dentry->d_name);
409 if ( ent )
410 return -EEXIST;
412 if ( sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO ) {
413 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
414 return -ENOSPC;
416 ino = sbi->next_dir_ino++;
418 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
419 if ( !ent )
420 return -ENOSPC;
422 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
423 if ( !ent->name ) {
424 kfree(ent);
425 return -ENOSPC;
428 ent->hash = dentry->d_name.hash;
429 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
430 ent->ino = ino;
431 ent->dentry = dentry;
432 autofs_hash_insert(dh,ent);
434 dir->i_nlink++;
435 d_instantiate(dentry, iget(dir->i_sb,ino));
437 return 0;
440 /* Get/set timeout ioctl() operation */
441 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
442 unsigned long *p)
444 int rv;
445 unsigned long ntimeout;
447 if ( (rv = get_user(ntimeout, p)) ||
448 (rv = put_user(sbi->exp_timeout/HZ, p)) )
449 return rv;
451 if ( ntimeout > ULONG_MAX/HZ )
452 sbi->exp_timeout = 0;
453 else
454 sbi->exp_timeout = ntimeout * HZ;
456 return 0;
459 /* Return protocol version */
460 static inline int autofs_get_protover(int *p)
462 return put_user(AUTOFS_PROTO_VERSION, p);
465 /* Perform an expiry operation */
466 static inline int autofs_expire_run(struct super_block *sb,
467 struct autofs_sb_info *sbi,
468 struct autofs_packet_expire *pkt_p)
470 struct autofs_dir_ent *ent;
471 struct autofs_packet_expire pkt;
473 memset(&pkt,0,sizeof pkt);
475 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
476 pkt.hdr.type = autofs_ptype_expire;
478 if ( !sbi->exp_timeout ||
479 !(ent = autofs_expire(sb,sbi)) )
480 return -EAGAIN;
482 pkt.len = ent->len;
483 memcpy(pkt.name, ent->name, pkt.len);
484 pkt.name[pkt.len] = '\0';
486 if ( copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)) )
487 return -EFAULT;
489 return 0;
493 * ioctl()'s on the root directory is the chief method for the daemon to
494 * generate kernel reactions
496 static int autofs_root_ioctl(struct inode *inode, struct file *filp,
497 unsigned int cmd, unsigned long arg)
499 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
501 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,current->pgrp));
503 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
504 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
505 return -ENOTTY;
507 if ( !autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
508 return -EPERM;
510 switch(cmd) {
511 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
512 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
513 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
514 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
515 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
516 autofs_catatonic_mode(sbi);
517 return 0;
518 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
519 return autofs_get_protover((int *)arg);
520 case AUTOFS_IOC_SETTIMEOUT:
521 return autofs_get_set_timeout(sbi,(unsigned long *)arg);
522 case AUTOFS_IOC_EXPIRE:
523 return autofs_expire_run(inode->i_sb,sbi,
524 (struct autofs_packet_expire *)arg);
525 default:
526 return -ENOSYS;