ALSA: oxygen: add self-documenting functions
[firewire-audio.git] / fs / autofs / root.c
blob8aacade56956ec650daaa537e76370724277b765
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/param.h>
17 #include <linux/time.h>
18 #include <linux/smp_lock.h>
19 #include "autofs_i.h"
21 static int autofs_root_readdir(struct file *,void *,filldir_t);
22 static struct dentry *autofs_root_lookup(struct inode *,struct dentry *, struct nameidata *);
23 static int autofs_root_symlink(struct inode *,struct dentry *,const char *);
24 static int autofs_root_unlink(struct inode *,struct dentry *);
25 static int autofs_root_rmdir(struct inode *,struct dentry *);
26 static int autofs_root_mkdir(struct inode *,struct dentry *,int);
27 static int autofs_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
29 const struct file_operations autofs_root_operations = {
30 .read = generic_read_dir,
31 .readdir = autofs_root_readdir,
32 .ioctl = autofs_root_ioctl,
35 const struct inode_operations autofs_root_inode_operations = {
36 .lookup = autofs_root_lookup,
37 .unlink = autofs_root_unlink,
38 .symlink = autofs_root_symlink,
39 .mkdir = autofs_root_mkdir,
40 .rmdir = autofs_root_rmdir,
43 static int autofs_root_readdir(struct file *filp, void *dirent, filldir_t filldir)
45 struct autofs_dir_ent *ent = NULL;
46 struct autofs_dirhash *dirhash;
47 struct autofs_sb_info *sbi;
48 struct inode * inode = filp->f_path.dentry->d_inode;
49 off_t onr, nr;
51 lock_kernel();
53 sbi = autofs_sbi(inode->i_sb);
54 dirhash = &sbi->dirhash;
55 nr = filp->f_pos;
57 switch(nr)
59 case 0:
60 if (filldir(dirent, ".", 1, nr, inode->i_ino, DT_DIR) < 0)
61 goto out;
62 filp->f_pos = ++nr;
63 /* fall through */
64 case 1:
65 if (filldir(dirent, "..", 2, nr, inode->i_ino, DT_DIR) < 0)
66 goto out;
67 filp->f_pos = ++nr;
68 /* fall through */
69 default:
70 while (onr = nr, ent = autofs_hash_enum(dirhash,&nr,ent)) {
71 if (!ent->dentry || d_mountpoint(ent->dentry)) {
72 if (filldir(dirent,ent->name,ent->len,onr,ent->ino,DT_UNKNOWN) < 0)
73 goto out;
74 filp->f_pos = nr;
77 break;
80 out:
81 unlock_kernel();
82 return 0;
85 static int try_to_fill_dentry(struct dentry *dentry, struct super_block *sb, struct autofs_sb_info *sbi)
87 struct inode * inode;
88 struct autofs_dir_ent *ent;
89 int status = 0;
91 if (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name))) {
92 do {
93 if (status && dentry->d_inode) {
94 if (status != -ENOENT)
95 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status, dentry->d_name.name);
96 return 0; /* Try to get the kernel to invalidate this dentry */
99 /* Turn this into a real negative dentry? */
100 if (status == -ENOENT) {
101 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
102 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
103 return 1;
104 } else if (status) {
105 /* Return a negative dentry, but leave it "pending" */
106 return 1;
108 status = autofs_wait(sbi, &dentry->d_name);
109 } while (!(ent = autofs_hash_lookup(&sbi->dirhash, &dentry->d_name)));
112 /* Abuse this field as a pointer to the directory entry, used to
113 find the expire list pointers */
114 dentry->d_time = (unsigned long) ent;
116 if (!dentry->d_inode) {
117 inode = autofs_iget(sb, ent->ino);
118 if (IS_ERR(inode)) {
119 /* Failed, but leave pending for next time */
120 return 1;
122 dentry->d_inode = inode;
125 /* If this is a directory that isn't a mount point, bitch at the
126 daemon and fix it in user space */
127 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
128 return !autofs_wait(sbi, &dentry->d_name);
131 /* We don't update the usages for the autofs daemon itself, this
132 is necessary for recursive autofs mounts */
133 if (!autofs_oz_mode(sbi)) {
134 autofs_update_usage(&sbi->dirhash,ent);
137 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
138 return 1;
143 * Revalidate is called on every cache lookup. Some of those
144 * cache lookups may actually happen while the dentry is not
145 * yet completely filled in, and revalidate has to delay such
146 * lookups..
148 static int autofs_revalidate(struct dentry * dentry, struct nameidata *nd)
150 struct inode * dir;
151 struct autofs_sb_info *sbi;
152 struct autofs_dir_ent *ent;
153 int res;
155 lock_kernel();
156 dir = dentry->d_parent->d_inode;
157 sbi = autofs_sbi(dir->i_sb);
159 /* Pending dentry */
160 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
161 if (autofs_oz_mode(sbi))
162 res = 1;
163 else
164 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
165 unlock_kernel();
166 return res;
169 /* Negative dentry.. invalidate if "old" */
170 if (!dentry->d_inode) {
171 unlock_kernel();
172 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
175 /* Check for a non-mountpoint directory */
176 if (S_ISDIR(dentry->d_inode->i_mode) && !d_mountpoint(dentry)) {
177 if (autofs_oz_mode(sbi))
178 res = 1;
179 else
180 res = try_to_fill_dentry(dentry, dir->i_sb, sbi);
181 unlock_kernel();
182 return res;
185 /* Update the usage list */
186 if (!autofs_oz_mode(sbi)) {
187 ent = (struct autofs_dir_ent *) dentry->d_time;
188 if (ent)
189 autofs_update_usage(&sbi->dirhash,ent);
191 unlock_kernel();
192 return 1;
195 static struct dentry_operations autofs_dentry_operations = {
196 .d_revalidate = autofs_revalidate,
199 static struct dentry *autofs_root_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
201 struct autofs_sb_info *sbi;
202 int oz_mode;
204 DPRINTK(("autofs_root_lookup: name = "));
205 lock_kernel();
206 autofs_say(dentry->d_name.name,dentry->d_name.len);
208 if (dentry->d_name.len > NAME_MAX) {
209 unlock_kernel();
210 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
213 sbi = autofs_sbi(dir->i_sb);
215 oz_mode = autofs_oz_mode(sbi);
216 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
217 "oz_mode = %d\n", task_pid_nr(current),
218 task_pgrp_nr(current), sbi->catatonic,
219 oz_mode));
222 * Mark the dentry incomplete, but add it. This is needed so
223 * that the VFS layer knows about the dentry, and we can count
224 * on catching any lookups through the revalidate.
226 * Let all the hard work be done by the revalidate function that
227 * needs to be able to do this anyway..
229 * We need to do this before we release the directory semaphore.
231 dentry->d_op = &autofs_dentry_operations;
232 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
233 d_add(dentry, NULL);
235 mutex_unlock(&dir->i_mutex);
236 autofs_revalidate(dentry, nd);
237 mutex_lock(&dir->i_mutex);
240 * If we are still pending, check if we had to handle
241 * a signal. If so we can force a restart..
243 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
244 /* See if we were interrupted */
245 if (signal_pending(current)) {
246 sigset_t *sigset = &current->pending.signal;
247 if (sigismember (sigset, SIGKILL) ||
248 sigismember (sigset, SIGQUIT) ||
249 sigismember (sigset, SIGINT)) {
250 unlock_kernel();
251 return ERR_PTR(-ERESTARTNOINTR);
255 unlock_kernel();
258 * If this dentry is unhashed, then we shouldn't honour this
259 * lookup even if the dentry is positive. Returning ENOENT here
260 * doesn't do the right thing for all system calls, but it should
261 * be OK for the operations we permit from an autofs.
263 if (dentry->d_inode && d_unhashed(dentry))
264 return ERR_PTR(-ENOENT);
266 return NULL;
269 static int autofs_root_symlink(struct inode *dir, struct dentry *dentry, const char *symname)
271 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
272 struct autofs_dirhash *dh = &sbi->dirhash;
273 struct autofs_dir_ent *ent;
274 unsigned int n;
275 int slsize;
276 struct autofs_symlink *sl;
277 struct inode *inode;
279 DPRINTK(("autofs_root_symlink: %s <- ", symname));
280 autofs_say(dentry->d_name.name,dentry->d_name.len);
282 lock_kernel();
283 if (!autofs_oz_mode(sbi)) {
284 unlock_kernel();
285 return -EACCES;
288 if (autofs_hash_lookup(dh, &dentry->d_name)) {
289 unlock_kernel();
290 return -EEXIST;
293 n = find_first_zero_bit(sbi->symlink_bitmap,AUTOFS_MAX_SYMLINKS);
294 if (n >= AUTOFS_MAX_SYMLINKS) {
295 unlock_kernel();
296 return -ENOSPC;
299 set_bit(n,sbi->symlink_bitmap);
300 sl = &sbi->symlink[n];
301 sl->len = strlen(symname);
302 sl->data = kmalloc(slsize = sl->len+1, GFP_KERNEL);
303 if (!sl->data) {
304 clear_bit(n,sbi->symlink_bitmap);
305 unlock_kernel();
306 return -ENOSPC;
309 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
310 if (!ent) {
311 kfree(sl->data);
312 clear_bit(n,sbi->symlink_bitmap);
313 unlock_kernel();
314 return -ENOSPC;
317 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
318 if (!ent->name) {
319 kfree(sl->data);
320 kfree(ent);
321 clear_bit(n,sbi->symlink_bitmap);
322 unlock_kernel();
323 return -ENOSPC;
326 memcpy(sl->data,symname,slsize);
327 sl->mtime = get_seconds();
329 ent->ino = AUTOFS_FIRST_SYMLINK + n;
330 ent->hash = dentry->d_name.hash;
331 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
332 ent->dentry = NULL; /* We don't keep the dentry for symlinks */
334 autofs_hash_insert(dh,ent);
336 inode = autofs_iget(dir->i_sb, ent->ino);
337 if (IS_ERR(inode))
338 return PTR_ERR(inode);
340 d_instantiate(dentry, inode);
341 unlock_kernel();
342 return 0;
346 * NOTE!
348 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
349 * that the file no longer exists. However, doing that means that the
350 * VFS layer can turn the dentry into a negative dentry, which we
351 * obviously do not want (we're dropping the entry not because it
352 * doesn't exist, but because it has timed out).
354 * Also see autofs_root_rmdir()..
356 static int autofs_root_unlink(struct inode *dir, struct dentry *dentry)
358 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
359 struct autofs_dirhash *dh = &sbi->dirhash;
360 struct autofs_dir_ent *ent;
361 unsigned int n;
363 /* This allows root to remove symlinks */
364 lock_kernel();
365 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN)) {
366 unlock_kernel();
367 return -EACCES;
370 ent = autofs_hash_lookup(dh, &dentry->d_name);
371 if (!ent) {
372 unlock_kernel();
373 return -ENOENT;
376 n = ent->ino - AUTOFS_FIRST_SYMLINK;
377 if (n >= AUTOFS_MAX_SYMLINKS) {
378 unlock_kernel();
379 return -EISDIR; /* It's a directory, dummy */
381 if (!test_bit(n,sbi->symlink_bitmap)) {
382 unlock_kernel();
383 return -EINVAL; /* Nonexistent symlink? Shouldn't happen */
386 dentry->d_time = (unsigned long)(struct autofs_dirhash *)NULL;
387 autofs_hash_delete(ent);
388 clear_bit(n,sbi->symlink_bitmap);
389 kfree(sbi->symlink[n].data);
390 d_drop(dentry);
392 unlock_kernel();
393 return 0;
396 static int autofs_root_rmdir(struct inode *dir, struct dentry *dentry)
398 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
399 struct autofs_dirhash *dh = &sbi->dirhash;
400 struct autofs_dir_ent *ent;
402 lock_kernel();
403 if (!autofs_oz_mode(sbi)) {
404 unlock_kernel();
405 return -EACCES;
408 ent = autofs_hash_lookup(dh, &dentry->d_name);
409 if (!ent) {
410 unlock_kernel();
411 return -ENOENT;
414 if ((unsigned int)ent->ino < AUTOFS_FIRST_DIR_INO) {
415 unlock_kernel();
416 return -ENOTDIR; /* Not a directory */
419 if (ent->dentry != dentry) {
420 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry->d_name.name);
423 dentry->d_time = (unsigned long)(struct autofs_dir_ent *)NULL;
424 autofs_hash_delete(ent);
425 drop_nlink(dir);
426 d_drop(dentry);
427 unlock_kernel();
429 return 0;
432 static int autofs_root_mkdir(struct inode *dir, struct dentry *dentry, int mode)
434 struct autofs_sb_info *sbi = autofs_sbi(dir->i_sb);
435 struct autofs_dirhash *dh = &sbi->dirhash;
436 struct autofs_dir_ent *ent;
437 struct inode *inode;
438 ino_t ino;
440 lock_kernel();
441 if (!autofs_oz_mode(sbi)) {
442 unlock_kernel();
443 return -EACCES;
446 ent = autofs_hash_lookup(dh, &dentry->d_name);
447 if (ent) {
448 unlock_kernel();
449 return -EEXIST;
452 if (sbi->next_dir_ino < AUTOFS_FIRST_DIR_INO) {
453 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
454 unlock_kernel();
455 return -ENOSPC;
457 ino = sbi->next_dir_ino++;
459 ent = kmalloc(sizeof(struct autofs_dir_ent), GFP_KERNEL);
460 if (!ent) {
461 unlock_kernel();
462 return -ENOSPC;
465 ent->name = kmalloc(dentry->d_name.len+1, GFP_KERNEL);
466 if (!ent->name) {
467 kfree(ent);
468 unlock_kernel();
469 return -ENOSPC;
472 ent->hash = dentry->d_name.hash;
473 memcpy(ent->name, dentry->d_name.name, 1+(ent->len = dentry->d_name.len));
474 ent->ino = ino;
475 ent->dentry = dentry;
476 autofs_hash_insert(dh,ent);
478 inc_nlink(dir);
480 inode = autofs_iget(dir->i_sb, ino);
481 if (IS_ERR(inode)) {
482 drop_nlink(dir);
483 return PTR_ERR(inode);
486 d_instantiate(dentry, inode);
487 unlock_kernel();
489 return 0;
492 /* Get/set timeout ioctl() operation */
493 static inline int autofs_get_set_timeout(struct autofs_sb_info *sbi,
494 unsigned long __user *p)
496 unsigned long ntimeout;
498 if (get_user(ntimeout, p) ||
499 put_user(sbi->exp_timeout / HZ, p))
500 return -EFAULT;
502 if (ntimeout > ULONG_MAX/HZ)
503 sbi->exp_timeout = 0;
504 else
505 sbi->exp_timeout = ntimeout * HZ;
507 return 0;
510 /* Return protocol version */
511 static inline int autofs_get_protover(int __user *p)
513 return put_user(AUTOFS_PROTO_VERSION, p);
516 /* Perform an expiry operation */
517 static inline int autofs_expire_run(struct super_block *sb,
518 struct autofs_sb_info *sbi,
519 struct vfsmount *mnt,
520 struct autofs_packet_expire __user *pkt_p)
522 struct autofs_dir_ent *ent;
523 struct autofs_packet_expire pkt;
525 memset(&pkt,0,sizeof pkt);
527 pkt.hdr.proto_version = AUTOFS_PROTO_VERSION;
528 pkt.hdr.type = autofs_ptype_expire;
530 if (!sbi->exp_timeout || !(ent = autofs_expire(sb,sbi,mnt)))
531 return -EAGAIN;
533 pkt.len = ent->len;
534 memcpy(pkt.name, ent->name, pkt.len);
535 pkt.name[pkt.len] = '\0';
537 if (copy_to_user(pkt_p, &pkt, sizeof(struct autofs_packet_expire)))
538 return -EFAULT;
540 return 0;
544 * ioctl()'s on the root directory is the chief method for the daemon to
545 * generate kernel reactions
547 static int autofs_root_ioctl(struct inode *inode, struct file *filp,
548 unsigned int cmd, unsigned long arg)
550 struct autofs_sb_info *sbi = autofs_sbi(inode->i_sb);
551 void __user *argp = (void __user *)arg;
553 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd,arg,sbi,task_pgrp_nr(current)));
555 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
556 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
557 return -ENOTTY;
559 if (!autofs_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
560 return -EPERM;
562 switch(cmd) {
563 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
564 return autofs_wait_release(sbi,(autofs_wqt_t)arg,0);
565 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
566 return autofs_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
567 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
568 autofs_catatonic_mode(sbi);
569 return 0;
570 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
571 return autofs_get_protover(argp);
572 case AUTOFS_IOC_SETTIMEOUT:
573 return autofs_get_set_timeout(sbi, argp);
574 case AUTOFS_IOC_EXPIRE:
575 return autofs_expire_run(inode->i_sb, sbi, filp->f_path.mnt,
576 argp);
577 default:
578 return -ENOSYS;