[S390] irq: have detailed statistics for interrupt types
[linux-2.6/x86.git] / fs / autofs4 / root.c
blobd34896cfb19f1512855d907b9d4c8c6b4cac2e09
1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
13 * ------------------------------------------------------------------------- */
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/slab.h>
19 #include <linux/param.h>
20 #include <linux/time.h>
21 #include <linux/compat.h>
22 #include <linux/mutex.h>
24 #include "autofs_i.h"
26 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
27 static int autofs4_dir_unlink(struct inode *,struct dentry *);
28 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
29 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
30 static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
31 #ifdef CONFIG_COMPAT
32 static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
33 #endif
34 static int autofs4_dir_open(struct inode *inode, struct file *file);
35 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
36 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
38 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
39 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
41 const struct file_operations autofs4_root_operations = {
42 .open = dcache_dir_open,
43 .release = dcache_dir_close,
44 .read = generic_read_dir,
45 .readdir = dcache_readdir,
46 .llseek = dcache_dir_lseek,
47 .unlocked_ioctl = autofs4_root_ioctl,
48 #ifdef CONFIG_COMPAT
49 .compat_ioctl = autofs4_root_compat_ioctl,
50 #endif
53 const struct file_operations autofs4_dir_operations = {
54 .open = autofs4_dir_open,
55 .release = dcache_dir_close,
56 .read = generic_read_dir,
57 .readdir = dcache_readdir,
58 .llseek = dcache_dir_lseek,
61 const struct inode_operations autofs4_indirect_root_inode_operations = {
62 .lookup = autofs4_lookup,
63 .unlink = autofs4_dir_unlink,
64 .symlink = autofs4_dir_symlink,
65 .mkdir = autofs4_dir_mkdir,
66 .rmdir = autofs4_dir_rmdir,
69 const struct inode_operations autofs4_direct_root_inode_operations = {
70 .lookup = autofs4_lookup,
71 .unlink = autofs4_dir_unlink,
72 .mkdir = autofs4_dir_mkdir,
73 .rmdir = autofs4_dir_rmdir,
74 .follow_link = autofs4_follow_link,
77 const struct inode_operations autofs4_dir_inode_operations = {
78 .lookup = autofs4_lookup,
79 .unlink = autofs4_dir_unlink,
80 .symlink = autofs4_dir_symlink,
81 .mkdir = autofs4_dir_mkdir,
82 .rmdir = autofs4_dir_rmdir,
85 static void autofs4_add_active(struct dentry *dentry)
87 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
88 struct autofs_info *ino = autofs4_dentry_ino(dentry);
89 if (ino) {
90 spin_lock(&sbi->lookup_lock);
91 if (!ino->active_count) {
92 if (list_empty(&ino->active))
93 list_add(&ino->active, &sbi->active_list);
95 ino->active_count++;
96 spin_unlock(&sbi->lookup_lock);
98 return;
101 static void autofs4_del_active(struct dentry *dentry)
103 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
104 struct autofs_info *ino = autofs4_dentry_ino(dentry);
105 if (ino) {
106 spin_lock(&sbi->lookup_lock);
107 ino->active_count--;
108 if (!ino->active_count) {
109 if (!list_empty(&ino->active))
110 list_del_init(&ino->active);
112 spin_unlock(&sbi->lookup_lock);
114 return;
117 static unsigned int autofs4_need_mount(unsigned int flags)
119 unsigned int res = 0;
120 if (flags & (TRIGGER_FLAGS | TRIGGER_INTENTS))
121 res = 1;
122 return res;
125 static int autofs4_dir_open(struct inode *inode, struct file *file)
127 struct dentry *dentry = file->f_path.dentry;
128 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
130 DPRINTK("file=%p dentry=%p %.*s",
131 file, dentry, dentry->d_name.len, dentry->d_name.name);
133 if (autofs4_oz_mode(sbi))
134 goto out;
137 * An empty directory in an autofs file system is always a
138 * mount point. The daemon must have failed to mount this
139 * during lookup so it doesn't exist. This can happen, for
140 * example, if user space returns an incorrect status for a
141 * mount request. Otherwise we're doing a readdir on the
142 * autofs file system so just let the libfs routines handle
143 * it.
145 spin_lock(&dcache_lock);
146 if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
147 spin_unlock(&dcache_lock);
148 return -ENOENT;
150 spin_unlock(&dcache_lock);
152 out:
153 return dcache_dir_open(inode, file);
156 static int try_to_fill_dentry(struct dentry *dentry, int flags)
158 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
159 struct autofs_info *ino = autofs4_dentry_ino(dentry);
160 int status;
162 DPRINTK("dentry=%p %.*s ino=%p",
163 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
166 * Wait for a pending mount, triggering one if there
167 * isn't one already
169 if (dentry->d_inode == NULL) {
170 DPRINTK("waiting for mount name=%.*s",
171 dentry->d_name.len, dentry->d_name.name);
173 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
175 DPRINTK("mount done status=%d", status);
177 /* Turn this into a real negative dentry? */
178 if (status == -ENOENT) {
179 spin_lock(&sbi->fs_lock);
180 ino->flags &= ~AUTOFS_INF_PENDING;
181 spin_unlock(&sbi->fs_lock);
182 return status;
183 } else if (status) {
184 /* Return a negative dentry, but leave it "pending" */
185 return status;
187 /* Trigger mount for path component or follow link */
188 } else if (ino->flags & AUTOFS_INF_PENDING ||
189 autofs4_need_mount(flags)) {
190 DPRINTK("waiting for mount name=%.*s",
191 dentry->d_name.len, dentry->d_name.name);
193 spin_lock(&sbi->fs_lock);
194 ino->flags |= AUTOFS_INF_PENDING;
195 spin_unlock(&sbi->fs_lock);
196 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
198 DPRINTK("mount done status=%d", status);
200 if (status) {
201 spin_lock(&sbi->fs_lock);
202 ino->flags &= ~AUTOFS_INF_PENDING;
203 spin_unlock(&sbi->fs_lock);
204 return status;
208 /* Initialize expiry counter after successful mount */
209 ino->last_used = jiffies;
211 spin_lock(&sbi->fs_lock);
212 ino->flags &= ~AUTOFS_INF_PENDING;
213 spin_unlock(&sbi->fs_lock);
215 return 0;
218 /* For autofs direct mounts the follow link triggers the mount */
219 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
221 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
222 struct autofs_info *ino = autofs4_dentry_ino(dentry);
223 int oz_mode = autofs4_oz_mode(sbi);
224 unsigned int lookup_type;
225 int status;
227 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
228 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
229 nd->flags);
231 * For an expire of a covered direct or offset mount we need
232 * to break out of follow_down() at the autofs mount trigger
233 * (d_mounted--), so we can see the expiring flag, and manage
234 * the blocking and following here until the expire is completed.
236 if (oz_mode) {
237 spin_lock(&sbi->fs_lock);
238 if (ino->flags & AUTOFS_INF_EXPIRING) {
239 spin_unlock(&sbi->fs_lock);
240 /* Follow down to our covering mount. */
241 if (!follow_down(&nd->path))
242 goto done;
243 goto follow;
245 spin_unlock(&sbi->fs_lock);
246 goto done;
249 /* If an expire request is pending everyone must wait. */
250 autofs4_expire_wait(dentry);
252 /* We trigger a mount for almost all flags */
253 lookup_type = autofs4_need_mount(nd->flags);
254 spin_lock(&sbi->fs_lock);
255 spin_lock(&dcache_lock);
256 if (!(lookup_type || ino->flags & AUTOFS_INF_PENDING)) {
257 spin_unlock(&dcache_lock);
258 spin_unlock(&sbi->fs_lock);
259 goto follow;
263 * If the dentry contains directories then it is an autofs
264 * multi-mount with no root mount offset. So don't try to
265 * mount it again.
267 if (ino->flags & AUTOFS_INF_PENDING ||
268 (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs))) {
269 spin_unlock(&dcache_lock);
270 spin_unlock(&sbi->fs_lock);
272 status = try_to_fill_dentry(dentry, nd->flags);
273 if (status)
274 goto out_error;
276 goto follow;
278 spin_unlock(&dcache_lock);
279 spin_unlock(&sbi->fs_lock);
280 follow:
282 * If there is no root mount it must be an autofs
283 * multi-mount with no root offset so we don't need
284 * to follow it.
286 if (d_mountpoint(dentry)) {
287 if (!autofs4_follow_mount(&nd->path)) {
288 status = -ENOENT;
289 goto out_error;
293 done:
294 return NULL;
296 out_error:
297 path_put(&nd->path);
298 return ERR_PTR(status);
302 * Revalidate is called on every cache lookup. Some of those
303 * cache lookups may actually happen while the dentry is not
304 * yet completely filled in, and revalidate has to delay such
305 * lookups..
307 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
309 struct inode *dir = dentry->d_parent->d_inode;
310 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
311 int oz_mode = autofs4_oz_mode(sbi);
312 int flags = nd ? nd->flags : 0;
313 int status = 1;
315 /* Pending dentry */
316 spin_lock(&sbi->fs_lock);
317 if (autofs4_ispending(dentry)) {
318 /* The daemon never causes a mount to trigger */
319 spin_unlock(&sbi->fs_lock);
321 if (oz_mode)
322 return 1;
325 * If the directory has gone away due to an expire
326 * we have been called as ->d_revalidate() and so
327 * we need to return false and proceed to ->lookup().
329 if (autofs4_expire_wait(dentry) == -EAGAIN)
330 return 0;
333 * A zero status is success otherwise we have a
334 * negative error code.
336 status = try_to_fill_dentry(dentry, flags);
337 if (status == 0)
338 return 1;
340 return status;
342 spin_unlock(&sbi->fs_lock);
344 /* Negative dentry.. invalidate if "old" */
345 if (dentry->d_inode == NULL)
346 return 0;
348 /* Check for a non-mountpoint directory with no contents */
349 spin_lock(&dcache_lock);
350 if (S_ISDIR(dentry->d_inode->i_mode) &&
351 !d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
352 DPRINTK("dentry=%p %.*s, emptydir",
353 dentry, dentry->d_name.len, dentry->d_name.name);
354 spin_unlock(&dcache_lock);
356 /* The daemon never causes a mount to trigger */
357 if (oz_mode)
358 return 1;
361 * A zero status is success otherwise we have a
362 * negative error code.
364 status = try_to_fill_dentry(dentry, flags);
365 if (status == 0)
366 return 1;
368 return status;
370 spin_unlock(&dcache_lock);
372 return 1;
375 void autofs4_dentry_release(struct dentry *de)
377 struct autofs_info *inf;
379 DPRINTK("releasing %p", de);
381 inf = autofs4_dentry_ino(de);
382 de->d_fsdata = NULL;
384 if (inf) {
385 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
387 if (sbi) {
388 spin_lock(&sbi->lookup_lock);
389 if (!list_empty(&inf->active))
390 list_del(&inf->active);
391 if (!list_empty(&inf->expiring))
392 list_del(&inf->expiring);
393 spin_unlock(&sbi->lookup_lock);
396 inf->dentry = NULL;
397 inf->inode = NULL;
399 autofs4_free_ino(inf);
403 /* For dentries of directories in the root dir */
404 static const struct dentry_operations autofs4_root_dentry_operations = {
405 .d_revalidate = autofs4_revalidate,
406 .d_release = autofs4_dentry_release,
409 /* For other dentries */
410 static const struct dentry_operations autofs4_dentry_operations = {
411 .d_revalidate = autofs4_revalidate,
412 .d_release = autofs4_dentry_release,
415 static struct dentry *autofs4_lookup_active(struct dentry *dentry)
417 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
418 struct dentry *parent = dentry->d_parent;
419 struct qstr *name = &dentry->d_name;
420 unsigned int len = name->len;
421 unsigned int hash = name->hash;
422 const unsigned char *str = name->name;
423 struct list_head *p, *head;
425 spin_lock(&dcache_lock);
426 spin_lock(&sbi->lookup_lock);
427 head = &sbi->active_list;
428 list_for_each(p, head) {
429 struct autofs_info *ino;
430 struct dentry *active;
431 struct qstr *qstr;
433 ino = list_entry(p, struct autofs_info, active);
434 active = ino->dentry;
436 spin_lock(&active->d_lock);
438 /* Already gone? */
439 if (atomic_read(&active->d_count) == 0)
440 goto next;
442 qstr = &active->d_name;
444 if (active->d_name.hash != hash)
445 goto next;
446 if (active->d_parent != parent)
447 goto next;
449 if (qstr->len != len)
450 goto next;
451 if (memcmp(qstr->name, str, len))
452 goto next;
454 if (d_unhashed(active)) {
455 dget(active);
456 spin_unlock(&active->d_lock);
457 spin_unlock(&sbi->lookup_lock);
458 spin_unlock(&dcache_lock);
459 return active;
461 next:
462 spin_unlock(&active->d_lock);
464 spin_unlock(&sbi->lookup_lock);
465 spin_unlock(&dcache_lock);
467 return NULL;
470 static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
472 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
473 struct dentry *parent = dentry->d_parent;
474 struct qstr *name = &dentry->d_name;
475 unsigned int len = name->len;
476 unsigned int hash = name->hash;
477 const unsigned char *str = name->name;
478 struct list_head *p, *head;
480 spin_lock(&dcache_lock);
481 spin_lock(&sbi->lookup_lock);
482 head = &sbi->expiring_list;
483 list_for_each(p, head) {
484 struct autofs_info *ino;
485 struct dentry *expiring;
486 struct qstr *qstr;
488 ino = list_entry(p, struct autofs_info, expiring);
489 expiring = ino->dentry;
491 spin_lock(&expiring->d_lock);
493 /* Bad luck, we've already been dentry_iput */
494 if (!expiring->d_inode)
495 goto next;
497 qstr = &expiring->d_name;
499 if (expiring->d_name.hash != hash)
500 goto next;
501 if (expiring->d_parent != parent)
502 goto next;
504 if (qstr->len != len)
505 goto next;
506 if (memcmp(qstr->name, str, len))
507 goto next;
509 if (d_unhashed(expiring)) {
510 dget(expiring);
511 spin_unlock(&expiring->d_lock);
512 spin_unlock(&sbi->lookup_lock);
513 spin_unlock(&dcache_lock);
514 return expiring;
516 next:
517 spin_unlock(&expiring->d_lock);
519 spin_unlock(&sbi->lookup_lock);
520 spin_unlock(&dcache_lock);
522 return NULL;
525 /* Lookups in the root directory */
526 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
528 struct autofs_sb_info *sbi;
529 struct autofs_info *ino;
530 struct dentry *expiring, *active;
531 int oz_mode;
533 DPRINTK("name = %.*s",
534 dentry->d_name.len, dentry->d_name.name);
536 /* File name too long to exist */
537 if (dentry->d_name.len > NAME_MAX)
538 return ERR_PTR(-ENAMETOOLONG);
540 sbi = autofs4_sbi(dir->i_sb);
541 oz_mode = autofs4_oz_mode(sbi);
543 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
544 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
546 active = autofs4_lookup_active(dentry);
547 if (active) {
548 dentry = active;
549 ino = autofs4_dentry_ino(dentry);
550 } else {
552 * Mark the dentry incomplete but don't hash it. We do this
553 * to serialize our inode creation operations (symlink and
554 * mkdir) which prevents deadlock during the callback to
555 * the daemon. Subsequent user space lookups for the same
556 * dentry are placed on the wait queue while the daemon
557 * itself is allowed passage unresticted so the create
558 * operation itself can then hash the dentry. Finally,
559 * we check for the hashed dentry and return the newly
560 * hashed dentry.
562 dentry->d_op = &autofs4_root_dentry_operations;
565 * And we need to ensure that the same dentry is used for
566 * all following lookup calls until it is hashed so that
567 * the dentry flags are persistent throughout the request.
569 ino = autofs4_init_ino(NULL, sbi, 0555);
570 if (!ino)
571 return ERR_PTR(-ENOMEM);
573 dentry->d_fsdata = ino;
574 ino->dentry = dentry;
576 autofs4_add_active(dentry);
578 d_instantiate(dentry, NULL);
581 if (!oz_mode) {
582 mutex_unlock(&dir->i_mutex);
583 expiring = autofs4_lookup_expiring(dentry);
584 if (expiring) {
586 * If we are racing with expire the request might not
587 * be quite complete but the directory has been removed
588 * so it must have been successful, so just wait for it.
590 autofs4_expire_wait(expiring);
591 autofs4_del_expiring(expiring);
592 dput(expiring);
595 spin_lock(&sbi->fs_lock);
596 ino->flags |= AUTOFS_INF_PENDING;
597 spin_unlock(&sbi->fs_lock);
598 if (dentry->d_op && dentry->d_op->d_revalidate)
599 (dentry->d_op->d_revalidate)(dentry, nd);
600 mutex_lock(&dir->i_mutex);
604 * If we are still pending, check if we had to handle
605 * a signal. If so we can force a restart..
607 if (ino->flags & AUTOFS_INF_PENDING) {
608 /* See if we were interrupted */
609 if (signal_pending(current)) {
610 sigset_t *sigset = &current->pending.signal;
611 if (sigismember (sigset, SIGKILL) ||
612 sigismember (sigset, SIGQUIT) ||
613 sigismember (sigset, SIGINT)) {
614 if (active)
615 dput(active);
616 return ERR_PTR(-ERESTARTNOINTR);
619 if (!oz_mode) {
620 spin_lock(&sbi->fs_lock);
621 ino->flags &= ~AUTOFS_INF_PENDING;
622 spin_unlock(&sbi->fs_lock);
627 * If this dentry is unhashed, then we shouldn't honour this
628 * lookup. Returning ENOENT here doesn't do the right thing
629 * for all system calls, but it should be OK for the operations
630 * we permit from an autofs.
632 if (!oz_mode && d_unhashed(dentry)) {
634 * A user space application can (and has done in the past)
635 * remove and re-create this directory during the callback.
636 * This can leave us with an unhashed dentry, but a
637 * successful mount! So we need to perform another
638 * cached lookup in case the dentry now exists.
640 struct dentry *parent = dentry->d_parent;
641 struct dentry *new = d_lookup(parent, &dentry->d_name);
642 if (new != NULL)
643 dentry = new;
644 else
645 dentry = ERR_PTR(-ENOENT);
647 if (active)
648 dput(active);
650 return dentry;
653 if (active)
654 return active;
656 return NULL;
659 static int autofs4_dir_symlink(struct inode *dir,
660 struct dentry *dentry,
661 const char *symname)
663 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
664 struct autofs_info *ino = autofs4_dentry_ino(dentry);
665 struct autofs_info *p_ino;
666 struct inode *inode;
667 char *cp;
669 DPRINTK("%s <- %.*s", symname,
670 dentry->d_name.len, dentry->d_name.name);
672 if (!autofs4_oz_mode(sbi))
673 return -EACCES;
675 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
676 if (!ino)
677 return -ENOMEM;
679 autofs4_del_active(dentry);
681 ino->size = strlen(symname);
682 cp = kmalloc(ino->size + 1, GFP_KERNEL);
683 if (!cp) {
684 if (!dentry->d_fsdata)
685 kfree(ino);
686 return -ENOMEM;
689 strcpy(cp, symname);
691 inode = autofs4_get_inode(dir->i_sb, ino);
692 if (!inode) {
693 kfree(cp);
694 if (!dentry->d_fsdata)
695 kfree(ino);
696 return -ENOMEM;
698 d_add(dentry, inode);
700 if (dir == dir->i_sb->s_root->d_inode)
701 dentry->d_op = &autofs4_root_dentry_operations;
702 else
703 dentry->d_op = &autofs4_dentry_operations;
705 dentry->d_fsdata = ino;
706 ino->dentry = dget(dentry);
707 atomic_inc(&ino->count);
708 p_ino = autofs4_dentry_ino(dentry->d_parent);
709 if (p_ino && dentry->d_parent != dentry)
710 atomic_inc(&p_ino->count);
711 ino->inode = inode;
713 ino->u.symlink = cp;
714 dir->i_mtime = CURRENT_TIME;
716 return 0;
720 * NOTE!
722 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
723 * that the file no longer exists. However, doing that means that the
724 * VFS layer can turn the dentry into a negative dentry. We don't want
725 * this, because the unlink is probably the result of an expire.
726 * We simply d_drop it and add it to a expiring list in the super block,
727 * which allows the dentry lookup to check for an incomplete expire.
729 * If a process is blocked on the dentry waiting for the expire to finish,
730 * it will invalidate the dentry and try to mount with a new one.
732 * Also see autofs4_dir_rmdir()..
734 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
736 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
737 struct autofs_info *ino = autofs4_dentry_ino(dentry);
738 struct autofs_info *p_ino;
740 /* This allows root to remove symlinks */
741 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
742 return -EACCES;
744 if (atomic_dec_and_test(&ino->count)) {
745 p_ino = autofs4_dentry_ino(dentry->d_parent);
746 if (p_ino && dentry->d_parent != dentry)
747 atomic_dec(&p_ino->count);
749 dput(ino->dentry);
751 dentry->d_inode->i_size = 0;
752 clear_nlink(dentry->d_inode);
754 dir->i_mtime = CURRENT_TIME;
756 spin_lock(&dcache_lock);
757 autofs4_add_expiring(dentry);
758 spin_lock(&dentry->d_lock);
759 __d_drop(dentry);
760 spin_unlock(&dentry->d_lock);
761 spin_unlock(&dcache_lock);
763 return 0;
766 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
768 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
769 struct autofs_info *ino = autofs4_dentry_ino(dentry);
770 struct autofs_info *p_ino;
772 DPRINTK("dentry %p, removing %.*s",
773 dentry, dentry->d_name.len, dentry->d_name.name);
775 if (!autofs4_oz_mode(sbi))
776 return -EACCES;
778 spin_lock(&dcache_lock);
779 if (!list_empty(&dentry->d_subdirs)) {
780 spin_unlock(&dcache_lock);
781 return -ENOTEMPTY;
783 autofs4_add_expiring(dentry);
784 spin_lock(&dentry->d_lock);
785 __d_drop(dentry);
786 spin_unlock(&dentry->d_lock);
787 spin_unlock(&dcache_lock);
789 if (atomic_dec_and_test(&ino->count)) {
790 p_ino = autofs4_dentry_ino(dentry->d_parent);
791 if (p_ino && dentry->d_parent != dentry)
792 atomic_dec(&p_ino->count);
794 dput(ino->dentry);
795 dentry->d_inode->i_size = 0;
796 clear_nlink(dentry->d_inode);
798 if (dir->i_nlink)
799 drop_nlink(dir);
801 return 0;
804 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
806 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
807 struct autofs_info *ino = autofs4_dentry_ino(dentry);
808 struct autofs_info *p_ino;
809 struct inode *inode;
811 if (!autofs4_oz_mode(sbi))
812 return -EACCES;
814 DPRINTK("dentry %p, creating %.*s",
815 dentry, dentry->d_name.len, dentry->d_name.name);
817 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
818 if (!ino)
819 return -ENOMEM;
821 autofs4_del_active(dentry);
823 inode = autofs4_get_inode(dir->i_sb, ino);
824 if (!inode) {
825 if (!dentry->d_fsdata)
826 kfree(ino);
827 return -ENOMEM;
829 d_add(dentry, inode);
831 if (dir == dir->i_sb->s_root->d_inode)
832 dentry->d_op = &autofs4_root_dentry_operations;
833 else
834 dentry->d_op = &autofs4_dentry_operations;
836 dentry->d_fsdata = ino;
837 ino->dentry = dget(dentry);
838 atomic_inc(&ino->count);
839 p_ino = autofs4_dentry_ino(dentry->d_parent);
840 if (p_ino && dentry->d_parent != dentry)
841 atomic_inc(&p_ino->count);
842 ino->inode = inode;
843 inc_nlink(dir);
844 dir->i_mtime = CURRENT_TIME;
846 return 0;
849 /* Get/set timeout ioctl() operation */
850 #ifdef CONFIG_COMPAT
851 static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
852 compat_ulong_t __user *p)
854 int rv;
855 unsigned long ntimeout;
857 if ((rv = get_user(ntimeout, p)) ||
858 (rv = put_user(sbi->exp_timeout/HZ, p)))
859 return rv;
861 if (ntimeout > UINT_MAX/HZ)
862 sbi->exp_timeout = 0;
863 else
864 sbi->exp_timeout = ntimeout * HZ;
866 return 0;
868 #endif
870 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
871 unsigned long __user *p)
873 int rv;
874 unsigned long ntimeout;
876 if ((rv = get_user(ntimeout, p)) ||
877 (rv = put_user(sbi->exp_timeout/HZ, p)))
878 return rv;
880 if (ntimeout > ULONG_MAX/HZ)
881 sbi->exp_timeout = 0;
882 else
883 sbi->exp_timeout = ntimeout * HZ;
885 return 0;
888 /* Return protocol version */
889 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
891 return put_user(sbi->version, p);
894 /* Return protocol sub version */
895 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
897 return put_user(sbi->sub_version, p);
901 * Tells the daemon whether it can umount the autofs mount.
903 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
905 int status = 0;
907 if (may_umount(mnt))
908 status = 1;
910 DPRINTK("returning %d", status);
912 status = put_user(status, p);
914 return status;
917 /* Identify autofs4_dentries - this is so we can tell if there's
918 an extra dentry refcount or not. We only hold a refcount on the
919 dentry if its non-negative (ie, d_inode != NULL)
921 int is_autofs4_dentry(struct dentry *dentry)
923 return dentry && dentry->d_inode &&
924 (dentry->d_op == &autofs4_root_dentry_operations ||
925 dentry->d_op == &autofs4_dentry_operations) &&
926 dentry->d_fsdata != NULL;
930 * ioctl()'s on the root directory is the chief method for the daemon to
931 * generate kernel reactions
933 static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
934 unsigned int cmd, unsigned long arg)
936 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
937 void __user *p = (void __user *)arg;
939 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
940 cmd,arg,sbi,task_pgrp_nr(current));
942 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
943 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
944 return -ENOTTY;
946 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
947 return -EPERM;
949 switch(cmd) {
950 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
951 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
952 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
953 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
954 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
955 autofs4_catatonic_mode(sbi);
956 return 0;
957 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
958 return autofs4_get_protover(sbi, p);
959 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
960 return autofs4_get_protosubver(sbi, p);
961 case AUTOFS_IOC_SETTIMEOUT:
962 return autofs4_get_set_timeout(sbi, p);
963 #ifdef CONFIG_COMPAT
964 case AUTOFS_IOC_SETTIMEOUT32:
965 return autofs4_compat_get_set_timeout(sbi, p);
966 #endif
968 case AUTOFS_IOC_ASKUMOUNT:
969 return autofs4_ask_umount(filp->f_path.mnt, p);
971 /* return a single thing to expire */
972 case AUTOFS_IOC_EXPIRE:
973 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
974 /* same as above, but can send multiple expires through pipe */
975 case AUTOFS_IOC_EXPIRE_MULTI:
976 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
978 default:
979 return -ENOSYS;
983 static long autofs4_root_ioctl(struct file *filp,
984 unsigned int cmd, unsigned long arg)
986 struct inode *inode = filp->f_dentry->d_inode;
987 return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
990 #ifdef CONFIG_COMPAT
991 static long autofs4_root_compat_ioctl(struct file *filp,
992 unsigned int cmd, unsigned long arg)
994 struct inode *inode = filp->f_path.dentry->d_inode;
995 int ret;
997 if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
998 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
999 else
1000 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
1001 (unsigned long)compat_ptr(arg));
1003 return ret;
1005 #endif