Driver-core: Always create class directories for classses that support namespaces.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / autofs / root.c
blob9a0520b50663b52f9508eb962bd3fbd92f55baa4
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/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/smp_lock.h>
20 #include "autofs_i.h"
22 static int autofs_root_readdir(struct file *,void *,filldir_t);
23 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
24 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
25 static int autofs_root_unlink(struct inode *,struct dentry *);
26 static int autofs_root_rmdir(struct inode *,struct dentry *);
27 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
28 static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
30 const struct file_operations autofs_root_operations = {
31 .llseek = generic_file_llseek,
32 .read = generic_read_dir,
33 .readdir = autofs_root_readdir,
34 .ioctl = autofs_root_ioctl,
37 const struct inode_operations autofs_root_inode_operations = {
38 .lookup = autofs_root_lookup,
39 .unlink = autofs_root_unlink,
40 .symlink = autofs_root_symlink,
41 .mkdir = autofs_root_mkdir,
42 .rmdir = autofs_root_rmdir,
45 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
47 struct autofs_dir_ent *ent = NULL;
48 struct autofs_dirhash *dirhash;
49 struct autofs_sb_info *sbi;
50 struct inode * inode = filp->f_path.dentry->d_inode;
51 off_t onr, nr;
53 lock_kernel();
55 sbi = autofs_sbi(inode->i_sb);
56 dirhash = &sbi->dirhash;
57 nr = filp->f_pos;
59 switch(nr)
61 case 0:
62 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
63 goto out;
64 filp->f_pos = ++nr;
65 /* fall through */
66 case 1:
67 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
68 goto out;
69 filp->f_pos = ++nr;
70 /* fall through */
71 default:
72 while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
73 if (!ent->dentry || d_mountpoint(ent->dentry)) {
74 if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
75 goto out;
76 filp->f_pos = nr;
79 break;
82 out:
83 unlock_kernel();
84 return 0;
87 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
89 struct inode * inode;
90 struct autofs_dir_ent *ent;
91 int status = 0;
93 if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
94 do {
95 if (status && dentry->d_inode) {
96 if (status != -ENOENT)
97 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
98 return 0; /* Try to get the kernel to invalidate this dentry */
101 /* Turn this into a real negative dentry? */
102 if (status == -ENOENT) {
103 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
104 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
105 return 1;
106 } else if (status) {
107 /* Return a negative dentry, but leave it "pending" */
108 return 1;
110 status = autofs_wait(sbi, &dentry->d_name);
111 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
114 /* Abuse this field as a pointer to the directory entry, used to
115 find the expire list pointers */
116 dentry->d_time = (unsigned long) ent;
118 if (!dentry->d_inode) {
119 inode = autofs_iget(sb, ent->ino);
120 if (IS_ERR(inode)) {
121 /* Failed, but leave pending for next time */
122 return 1;
124 dentry->d_inode = inode;
127 /* If this is a directory that isn't a mount point, bitch at the
128 daemon and fix it in user space */
129 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
130 return !autofs_wait(sbi, &dentry->d_name);
133 /* We don't update the usages for the autofs daemon itself, this
134 is necessary for recursive autofs mounts */
135 if (!autofs_oz_mode(sbi)) {
136 autofs_update_usage(&sbi->dirhash,ent);
139 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
140 return 1;
145 * Revalidate is called on every cache lookup. Some of those
146 * cache lookups may actually happen while the dentry is not
147 * yet completely filled in, and revalidate has to delay such
148 * lookups..
150 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
152 struct inode * dir;
153 struct autofs_sb_info *sbi;
154 struct autofs_dir_ent *ent;
155 int res;
157 lock_kernel();
158 dir = dentry->d_parent->d_inode;
159 sbi = autofs_sbi(dir->i_sb);
161 /* Pending dentry */
162 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
163 if (autofs_oz_mode(sbi))
164 res = 1;
165 else
166 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
167 unlock_kernel();
168 return res;
171 /* Negative dentry.. invalidate if "old" */
172 if (!dentry->d_inode) {
173 unlock_kernel();
174 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
177 /* Check for a non-mountpoint directory */
178 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
179 if (autofs_oz_mode(sbi))
180 res = 1;
181 else
182 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
183 unlock_kernel();
184 return res;
187 /* Update the usage list */
188 if (!autofs_oz_mode(sbi)) {
189 ent = (struct autofs_dir_ent *) dentry->d_time;
190 if (ent)
191 autofs_update_usage(&sbi->dirhash,ent);
193 unlock_kernel();
194 return 1;
197 static const struct dentry_operations autofs_dentry_operations = {
198 .d_revalidate = autofs_revalidate,
201 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
203 struct autofs_sb_info *sbi;
204 int oz_mode;
206 DPRINTK(("autofs_root_lookup: name = "));
207 lock_kernel();
208 autofs_say(dentry->d_name.name,dentry->d_name.len);
210 if (dentry->d_name.len > NAME_MAX) {
211 unlock_kernel();
212 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
215 sbi = autofs_sbi(dir->i_sb);
217 oz_mode = autofs_oz_mode(sbi);
218 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
219 "oz_mode = %d\n", task_pid_nr(current),
220 task_pgrp_nr(current), sbi->catatonic,
221 oz_mode));
224 * Mark the dentry incomplete, but add it. This is needed so
225 * that the VFS layer knows about the dentry, and we can count
226 * on catching any lookups through the revalidate.
228 * Let all the hard work be done by the revalidate function that
229 * needs to be able to do this anyway..
231 * We need to do this before we release the directory semaphore.
233 dentry->d_op = &autofs_dentry_operations;
234 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
235 d_add(dentry, NULL);
237 mutex_unlock(&dir->i_mutex);
238 autofs_revalidate(dentry, nd);
239 mutex_lock(&dir->i_mutex);
242 * If we are still pending, check if we had to handle
243 * a signal. If so we can force a restart..
245 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
246 /* See if we were interrupted */
247 if (signal_pending(current)) {
248 sigset_t *sigset = &current->pending.signal;
249 if (sigismember (sigset, SIGKILL) ||
250 sigismember (sigset, SIGQUIT) ||
251 sigismember (sigset, SIGINT)) {
252 unlock_kernel();
253 return ERR_PTR(-ERESTARTNOINTR);
257 unlock_kernel();
260 * If this dentry is unhashed, then we shouldn't honour this
261 * lookup even if the dentry is positive. Returning ENOENT here
262 * doesn't do the right thing for all system calls, but it should
263 * be OK for the operations we permit from an autofs.
265 if (dentry->d_inode && d_unhashed(dentry))
266 return ERR_PTR(-ENOENT);
268 return NULL;
271 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
273 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
274 struct autofs_dirhash *dh = &sbi->dirhash;
275 struct autofs_dir_ent *ent;
276 unsigned int n;
277 int slsize;
278 struct autofs_symlink *sl;
279 struct inode *inode;
281 DPRINTK(("autofs_root_symlink: %s <- ", symname));
282 autofs_say(dentry->d_name.name,dentry->d_name.len);
284 lock_kernel();
285 if (!autofs_oz_mode(sbi)) {
286 unlock_kernel();
287 return -EACCES;
290 if (autofs_hash_lookup(dh, &dentry->d_name)) {
291 unlock_kernel();
292 return -EEXIST;
295 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
296 if (n >= AUTOFS_MAX_SYMLINKS) {
297 unlock_kernel();
298 return -ENOSPC;
301 set_bit(n,sbi->symlink_bitmap);
302 sl = &sbi->symlink[n];
303 sl->len = strlen(symname);
304 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
305 if (!sl->data) {
306 clear_bit(n,sbi->symlink_bitmap);
307 unlock_kernel();
308 return -ENOSPC;
311 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
312 if (!ent) {
313 kfree(sl->data);
314 clear_bit(n,sbi->symlink_bitmap);
315 unlock_kernel();
316 return -ENOSPC;
319 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
320 if (!ent->name) {
321 kfree(sl->data);
322 kfree(ent);
323 clear_bit(n,sbi->symlink_bitmap);
324 unlock_kernel();
325 return -ENOSPC;
328 memcpy(sl->data,symname,slsize);
329 sl->mtime = get_seconds();
331 ent->ino = AUTOFS_FIRST_SYMLINK + n;
332 ent->hash = dentry->d_name.hash;
333 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
334 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
336 autofs_hash_insert(dh,ent);
338 inode = autofs_iget(dir->i_sb, ent->ino);
339 if (IS_ERR(inode))
340 return PTR_ERR(inode);
342 d_instantiate(dentry, inode);
343 unlock_kernel();
344 return 0;
348 * NOTE!
350 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
351 * that the file no longer exists. However, doing that means that the
352 * VFS layer can turn the dentry into a negative dentry, which we
353 * obviously do not want (we're dropping the entry not because it
354 * doesn't exist, but because it has timed out).
356 * Also see autofs_root_rmdir()..
358 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
360 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
361 struct autofs_dirhash *dh = &sbi->dirhash;
362 struct autofs_dir_ent *ent;
363 unsigned int n;
365 /* This allows root to remove symlinks */
366 lock_kernel();
367 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
368 unlock_kernel();
369 return -EACCES;
372 ent = autofs_hash_lookup(dh, &dentry->d_name);
373 if (!ent) {
374 unlock_kernel();
375 return -ENOENT;
378 n = ent->ino - AUTOFS_FIRST_SYMLINK;
379 if (n >= AUTOFS_MAX_SYMLINKS) {
380 unlock_kernel();
381 return -EISDIR; /* It's a directory, dummy */
383 if (!test_bit(n,sbi->symlink_bitmap)) {
384 unlock_kernel();
385 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
388 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
389 autofs_hash_delete(ent);
390 clear_bit(n,sbi->symlink_bitmap);
391 kfree(sbi->symlink[n].data);
392 d_drop(dentry);
394 unlock_kernel();
395 return 0;
398 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
400 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
401 struct autofs_dirhash *dh = &sbi->dirhash;
402 struct autofs_dir_ent *ent;
404 lock_kernel();
405 if (!autofs_oz_mode(sbi)) {
406 unlock_kernel();
407 return -EACCES;
410 ent = autofs_hash_lookup(dh, &dentry->d_name);
411 if (!ent) {
412 unlock_kernel();
413 return -ENOENT;
416 if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
417 unlock_kernel();
418 return -ENOTDIR; /* Not a directory */
421 if (ent->dentry != dentry) {
422 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
425 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
426 autofs_hash_delete(ent);
427 drop_nlink(dir);
428 d_drop(dentry);
429 unlock_kernel();
431 return 0;
434 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
436 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
437 struct autofs_dirhash *dh = &sbi->dirhash;
438 struct autofs_dir_ent *ent;
439 struct inode *inode;
440 ino_t ino;
442 lock_kernel();
443 if (!autofs_oz_mode(sbi)) {
444 unlock_kernel();
445 return -EACCES;
448 ent = autofs_hash_lookup(dh, &dentry->d_name);
449 if (ent) {
450 unlock_kernel();
451 return -EEXIST;
454 if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
455 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
456 unlock_kernel();
457 return -ENOSPC;
459 ino = sbi->next_dir_ino++;
461 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
462 if (!ent) {
463 unlock_kernel();
464 return -ENOSPC;
467 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
468 if (!ent->name) {
469 kfree(ent);
470 unlock_kernel();
471 return -ENOSPC;
474 ent->hash = dentry->d_name.hash;
475 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
476 ent->ino = ino;
477 ent->dentry = dentry;
478 autofs_hash_insert(dh,ent);
480 inc_nlink(dir);
482 inode = autofs_iget(dir->i_sb, ino);
483 if (IS_ERR(inode)) {
484 drop_nlink(dir);
485 return PTR_ERR(inode);
488 d_instantiate(dentry, inode);
489 unlock_kernel();
491 return 0;
494 /* Get/set timeout ioctl() operation */
495 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
496 unsigned long __user *p)
498 unsigned long ntimeout;
500 if (get_user(ntimeout, p) ||
501 put_user(sbi->exp_timeout / HZ, p))
502 return -EFAULT;
504 if (ntimeout > ULONG_MAX/HZ)
505 sbi->exp_timeout = 0;
506 else
507 sbi->exp_timeout = ntimeout * HZ;
509 return 0;
512 /* Return protocol version */
513 static inline int autofs_get_protover(int __user *p)
515 return put_user(AUTOFS_PROTO_VERSION, p);
518 /* Perform an expiry operation */
519 static inline int autofs_expire_run(struct super_block *sb,
520 struct autofs_sb_info *sbi,
521 struct vfsmount *mnt,
522 struct autofs_packet_expire __user *pkt_p)
524 struct autofs_dir_ent *ent;
525 struct autofs_packet_expire pkt;
527 memset(&pkt,0,sizeof pkt);
529 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
530 pkt.hdr.type = autofs_ptype_expire;
532 if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
533 return -EAGAIN;
535 pkt.len = ent->len;
536 memcpy(pkt.name, ent->name, pkt.len);
537 pkt.name[pkt.len] = '\0';
539 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
540 return -EFAULT;
542 return 0;
546 * ioctl()'s on the root directory is the chief method for the daemon to
547 * generate kernel reactions
549 static int autofs_root_ioctl(struct inode *inode, struct file *filp,
550 unsigned int cmd, unsigned long arg)
552 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
553 void __user *argp = (void __user *)arg;
555 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
557 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
558 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
559 return -ENOTTY;
561 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
562 return -EPERM;
564 switch(cmd) {
565 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
566 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
567 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
568 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
569 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
570 autofs_catatonic_mode(sbi);
571 return 0;
572 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
573 return autofs_get_protover(argp);
574 case AUTOFS_IOC_SETTIMEOUT:
575 return autofs_get_set_timeout(sbi, argp);
576 case AUTOFS_IOC_EXPIRE:
577 return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
578 argp);
579 default:
580 return -ENOSYS;