iwlwifi: fix strict_strtoul error checking
[linux-2.6/libata-dev.git] / fs / autofs4 / root.c
blobbcfb2dc0a61bf56d0e1c4be07612f24454834c08
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/param.h>
19 #include <linux/time.h>
20 #include "autofs_i.h"
22 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static int autofs4_dir_open(struct inode *inode, struct file *file);
28 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
29 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
31 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
32 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
34 const struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = dcache_readdir,
39 .ioctl = autofs4_root_ioctl,
42 const struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = dcache_dir_close,
45 .read = generic_read_dir,
46 .readdir = dcache_readdir,
49 const struct inode_operations autofs4_indirect_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
57 const struct inode_operations autofs4_direct_root_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .mkdir = autofs4_dir_mkdir,
61 .rmdir = autofs4_dir_rmdir,
62 .follow_link = autofs4_follow_link,
65 const struct inode_operations autofs4_dir_inode_operations = {
66 .lookup = autofs4_lookup,
67 .unlink = autofs4_dir_unlink,
68 .symlink = autofs4_dir_symlink,
69 .mkdir = autofs4_dir_mkdir,
70 .rmdir = autofs4_dir_rmdir,
73 static int autofs4_dir_open(struct inode *inode, struct file *file)
75 struct dentry *dentry = file->f_path.dentry;
76 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
78 DPRINTK("file=%p dentry=%p %.*s",
79 file, dentry, dentry->d_name.len, dentry->d_name.name);
81 if (autofs4_oz_mode(sbi))
82 goto out;
85 * An empty directory in an autofs file system is always a
86 * mount point. The daemon must have failed to mount this
87 * during lookup so it doesn't exist. This can happen, for
88 * example, if user space returns an incorrect status for a
89 * mount request. Otherwise we're doing a readdir on the
90 * autofs file system so just let the libfs routines handle
91 * it.
93 spin_lock(&dcache_lock);
94 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
95 spin_unlock(&dcache_lock);
96 return -ENOENT;
98 spin_unlock(&dcache_lock);
100 out:
101 return dcache_dir_open(inode, file);
104 static int try_to_fill_dentry(struct dentry *dentry, int flags)
106 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
107 struct autofs_info *ino = autofs4_dentry_ino(dentry);
108 int status;
110 DPRINTK("dentry=%p %.*s ino=%p",
111 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
114 * Wait for a pending mount, triggering one if there
115 * isn't one already
117 if (dentry->d_inode == NULL) {
118 DPRINTK("waiting for mount name=%.*s",
119 dentry->d_name.len, dentry->d_name.name);
121 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
123 DPRINTK("mount done status=%d", status);
125 /* Turn this into a real negative dentry? */
126 if (status == -ENOENT) {
127 spin_lock(&dentry->d_lock);
128 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
129 spin_unlock(&dentry->d_lock);
130 return status;
131 } else if (status) {
132 /* Return a negative dentry, but leave it "pending" */
133 return status;
135 /* Trigger mount for path component or follow link */
136 } else if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
137 flags & (TRIGGER_FLAGS | TRIGGER_INTENTS) ||
138 current->link_count) {
139 DPRINTK("waiting for mount name=%.*s",
140 dentry->d_name.len, dentry->d_name.name);
142 spin_lock(&dentry->d_lock);
143 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
144 spin_unlock(&dentry->d_lock);
145 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
147 DPRINTK("mount done status=%d", status);
149 if (status) {
150 spin_lock(&dentry->d_lock);
151 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
152 spin_unlock(&dentry->d_lock);
153 return status;
157 /* Initialize expiry counter after successful mount */
158 if (ino)
159 ino->last_used = jiffies;
161 spin_lock(&dentry->d_lock);
162 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
163 spin_unlock(&dentry->d_lock);
165 return 0;
168 /* For autofs direct mounts the follow link triggers the mount */
169 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
171 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
172 struct autofs_info *ino = autofs4_dentry_ino(dentry);
173 int oz_mode = autofs4_oz_mode(sbi);
174 unsigned int lookup_type;
175 int status;
177 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
178 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
179 nd->flags);
181 * For an expire of a covered direct or offset mount we need
182 * to beeak out of follow_down() at the autofs mount trigger
183 * (d_mounted--), so we can see the expiring flag, and manage
184 * the blocking and following here until the expire is completed.
186 if (oz_mode) {
187 spin_lock(&sbi->fs_lock);
188 if (ino->flags & AUTOFS_INF_EXPIRING) {
189 spin_unlock(&sbi->fs_lock);
190 /* Follow down to our covering mount. */
191 if (!follow_down(&nd->path.mnt, &nd->path.dentry))
192 goto done;
193 goto follow;
195 spin_unlock(&sbi->fs_lock);
196 goto done;
199 /* If an expire request is pending everyone must wait. */
200 autofs4_expire_wait(dentry);
202 /* We trigger a mount for almost all flags */
203 lookup_type = nd->flags & (TRIGGER_FLAGS | TRIGGER_INTENTS);
204 if (!(lookup_type || dentry->d_flags & DCACHE_AUTOFS_PENDING))
205 goto follow;
208 * If the dentry contains directories then it is an autofs
209 * multi-mount with no root mount offset. So don't try to
210 * mount it again.
212 spin_lock(&dcache_lock);
213 if (dentry->d_flags & DCACHE_AUTOFS_PENDING ||
214 (!d_mountpoint(dentry) && __simple_empty(dentry))) {
215 spin_unlock(&dcache_lock);
217 status = try_to_fill_dentry(dentry, 0);
218 if (status)
219 goto out_error;
221 goto follow;
223 spin_unlock(&dcache_lock);
224 follow:
226 * If there is no root mount it must be an autofs
227 * multi-mount with no root offset so we don't need
228 * to follow it.
230 if (d_mountpoint(dentry)) {
231 if (!autofs4_follow_mount(&nd->path.mnt,
232 &nd->path.dentry)) {
233 status = -ENOENT;
234 goto out_error;
238 done:
239 return NULL;
241 out_error:
242 path_put(&nd->path);
243 return ERR_PTR(status);
247 * Revalidate is called on every cache lookup. Some of those
248 * cache lookups may actually happen while the dentry is not
249 * yet completely filled in, and revalidate has to delay such
250 * lookups..
252 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
254 struct inode *dir = dentry->d_parent->d_inode;
255 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
256 int oz_mode = autofs4_oz_mode(sbi);
257 int flags = nd ? nd->flags : 0;
258 int status = 1;
260 /* Pending dentry */
261 spin_lock(&sbi->fs_lock);
262 if (autofs4_ispending(dentry)) {
263 /* The daemon never causes a mount to trigger */
264 spin_unlock(&sbi->fs_lock);
266 if (oz_mode)
267 return 1;
270 * If the directory has gone away due to an expire
271 * we have been called as ->d_revalidate() and so
272 * we need to return false and proceed to ->lookup().
274 if (autofs4_expire_wait(dentry) == -EAGAIN)
275 return 0;
278 * A zero status is success otherwise we have a
279 * negative error code.
281 status = try_to_fill_dentry(dentry, flags);
282 if (status == 0)
283 return 1;
285 return status;
287 spin_unlock(&sbi->fs_lock);
289 /* Negative dentry.. invalidate if "old" */
290 if (dentry->d_inode == NULL)
291 return 0;
293 /* Check for a non-mountpoint directory with no contents */
294 spin_lock(&dcache_lock);
295 if (S_ISDIR(dentry->d_inode->i_mode) &&
296 !d_mountpoint(dentry) &&
297 __simple_empty(dentry)) {
298 DPRINTK("dentry=%p %.*s, emptydir",
299 dentry, dentry->d_name.len, dentry->d_name.name);
300 spin_unlock(&dcache_lock);
302 /* The daemon never causes a mount to trigger */
303 if (oz_mode)
304 return 1;
307 * A zero status is success otherwise we have a
308 * negative error code.
310 status = try_to_fill_dentry(dentry, flags);
311 if (status == 0)
312 return 1;
314 return status;
316 spin_unlock(&dcache_lock);
318 return 1;
321 void autofs4_dentry_release(struct dentry *de)
323 struct autofs_info *inf;
325 DPRINTK("releasing %p", de);
327 inf = autofs4_dentry_ino(de);
328 de->d_fsdata = NULL;
330 if (inf) {
331 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
333 if (sbi) {
334 spin_lock(&sbi->lookup_lock);
335 if (!list_empty(&inf->active))
336 list_del(&inf->active);
337 if (!list_empty(&inf->expiring))
338 list_del(&inf->expiring);
339 spin_unlock(&sbi->lookup_lock);
342 inf->dentry = NULL;
343 inf->inode = NULL;
345 autofs4_free_ino(inf);
349 /* For dentries of directories in the root dir */
350 static struct dentry_operations autofs4_root_dentry_operations = {
351 .d_revalidate = autofs4_revalidate,
352 .d_release = autofs4_dentry_release,
355 /* For other dentries */
356 static struct dentry_operations autofs4_dentry_operations = {
357 .d_revalidate = autofs4_revalidate,
358 .d_release = autofs4_dentry_release,
361 static struct dentry *autofs4_lookup_active(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
363 unsigned int len = name->len;
364 unsigned int hash = name->hash;
365 const unsigned char *str = name->name;
366 struct list_head *p, *head;
368 spin_lock(&dcache_lock);
369 spin_lock(&sbi->lookup_lock);
370 head = &sbi->active_list;
371 list_for_each(p, head) {
372 struct autofs_info *ino;
373 struct dentry *dentry;
374 struct qstr *qstr;
376 ino = list_entry(p, struct autofs_info, active);
377 dentry = ino->dentry;
379 spin_lock(&dentry->d_lock);
381 /* Already gone? */
382 if (atomic_read(&dentry->d_count) == 0)
383 goto next;
385 qstr = &dentry->d_name;
387 if (dentry->d_name.hash != hash)
388 goto next;
389 if (dentry->d_parent != parent)
390 goto next;
392 if (qstr->len != len)
393 goto next;
394 if (memcmp(qstr->name, str, len))
395 goto next;
397 if (d_unhashed(dentry)) {
398 dget(dentry);
399 spin_unlock(&dentry->d_lock);
400 spin_unlock(&sbi->lookup_lock);
401 spin_unlock(&dcache_lock);
402 return dentry;
404 next:
405 spin_unlock(&dentry->d_lock);
407 spin_unlock(&sbi->lookup_lock);
408 spin_unlock(&dcache_lock);
410 return NULL;
413 static struct dentry *autofs4_lookup_expiring(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
415 unsigned int len = name->len;
416 unsigned int hash = name->hash;
417 const unsigned char *str = name->name;
418 struct list_head *p, *head;
420 spin_lock(&dcache_lock);
421 spin_lock(&sbi->lookup_lock);
422 head = &sbi->expiring_list;
423 list_for_each(p, head) {
424 struct autofs_info *ino;
425 struct dentry *dentry;
426 struct qstr *qstr;
428 ino = list_entry(p, struct autofs_info, expiring);
429 dentry = ino->dentry;
431 spin_lock(&dentry->d_lock);
433 /* Bad luck, we've already been dentry_iput */
434 if (!dentry->d_inode)
435 goto next;
437 qstr = &dentry->d_name;
439 if (dentry->d_name.hash != hash)
440 goto next;
441 if (dentry->d_parent != parent)
442 goto next;
444 if (qstr->len != len)
445 goto next;
446 if (memcmp(qstr->name, str, len))
447 goto next;
449 if (d_unhashed(dentry)) {
450 dget(dentry);
451 spin_unlock(&dentry->d_lock);
452 spin_unlock(&sbi->lookup_lock);
453 spin_unlock(&dcache_lock);
454 return dentry;
456 next:
457 spin_unlock(&dentry->d_lock);
459 spin_unlock(&sbi->lookup_lock);
460 spin_unlock(&dcache_lock);
462 return NULL;
465 /* Lookups in the root directory */
466 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
468 struct autofs_sb_info *sbi;
469 struct autofs_info *ino;
470 struct dentry *expiring, *unhashed;
471 int oz_mode;
473 DPRINTK("name = %.*s",
474 dentry->d_name.len, dentry->d_name.name);
476 /* File name too long to exist */
477 if (dentry->d_name.len > NAME_MAX)
478 return ERR_PTR(-ENAMETOOLONG);
480 sbi = autofs4_sbi(dir->i_sb);
481 oz_mode = autofs4_oz_mode(sbi);
483 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
484 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
486 expiring = autofs4_lookup_expiring(sbi, dentry->d_parent, &dentry->d_name);
487 if (expiring) {
489 * If we are racing with expire the request might not
490 * be quite complete but the directory has been removed
491 * so it must have been successful, so just wait for it.
493 ino = autofs4_dentry_ino(expiring);
494 autofs4_expire_wait(expiring);
495 spin_lock(&sbi->lookup_lock);
496 if (!list_empty(&ino->expiring))
497 list_del_init(&ino->expiring);
498 spin_unlock(&sbi->lookup_lock);
499 dput(expiring);
502 unhashed = autofs4_lookup_active(sbi, dentry->d_parent, &dentry->d_name);
503 if (unhashed)
504 dentry = unhashed;
505 else {
507 * Mark the dentry incomplete but don't hash it. We do this
508 * to serialize our inode creation operations (symlink and
509 * mkdir) which prevents deadlock during the callback to
510 * the daemon. Subsequent user space lookups for the same
511 * dentry are placed on the wait queue while the daemon
512 * itself is allowed passage unresticted so the create
513 * operation itself can then hash the dentry. Finally,
514 * we check for the hashed dentry and return the newly
515 * hashed dentry.
517 dentry->d_op = &autofs4_root_dentry_operations;
520 * And we need to ensure that the same dentry is used for
521 * all following lookup calls until it is hashed so that
522 * the dentry flags are persistent throughout the request.
524 ino = autofs4_init_ino(NULL, sbi, 0555);
525 if (!ino)
526 return ERR_PTR(-ENOMEM);
528 dentry->d_fsdata = ino;
529 ino->dentry = dentry;
531 spin_lock(&sbi->lookup_lock);
532 list_add(&ino->active, &sbi->active_list);
533 spin_unlock(&sbi->lookup_lock);
535 d_instantiate(dentry, NULL);
538 if (!oz_mode) {
539 spin_lock(&dentry->d_lock);
540 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
541 spin_unlock(&dentry->d_lock);
542 if (dentry->d_op && dentry->d_op->d_revalidate) {
543 mutex_unlock(&dir->i_mutex);
544 (dentry->d_op->d_revalidate)(dentry, nd);
545 mutex_lock(&dir->i_mutex);
550 * If we are still pending, check if we had to handle
551 * a signal. If so we can force a restart..
553 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
554 /* See if we were interrupted */
555 if (signal_pending(current)) {
556 sigset_t *sigset = &current->pending.signal;
557 if (sigismember (sigset, SIGKILL) ||
558 sigismember (sigset, SIGQUIT) ||
559 sigismember (sigset, SIGINT)) {
560 if (unhashed)
561 dput(unhashed);
562 return ERR_PTR(-ERESTARTNOINTR);
565 if (!oz_mode) {
566 spin_lock(&dentry->d_lock);
567 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
568 spin_unlock(&dentry->d_lock);
573 * If this dentry is unhashed, then we shouldn't honour this
574 * lookup. Returning ENOENT here doesn't do the right thing
575 * for all system calls, but it should be OK for the operations
576 * we permit from an autofs.
578 if (!oz_mode && d_unhashed(dentry)) {
580 * A user space application can (and has done in the past)
581 * remove and re-create this directory during the callback.
582 * This can leave us with an unhashed dentry, but a
583 * successful mount! So we need to perform another
584 * cached lookup in case the dentry now exists.
586 struct dentry *parent = dentry->d_parent;
587 struct dentry *new = d_lookup(parent, &dentry->d_name);
588 if (new != NULL)
589 dentry = new;
590 else
591 dentry = ERR_PTR(-ENOENT);
593 if (unhashed)
594 dput(unhashed);
596 return dentry;
599 if (unhashed)
600 return unhashed;
602 return NULL;
605 static int autofs4_dir_symlink(struct inode *dir,
606 struct dentry *dentry,
607 const char *symname)
609 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
610 struct autofs_info *ino = autofs4_dentry_ino(dentry);
611 struct autofs_info *p_ino;
612 struct inode *inode;
613 char *cp;
615 DPRINTK("%s <- %.*s", symname,
616 dentry->d_name.len, dentry->d_name.name);
618 if (!autofs4_oz_mode(sbi))
619 return -EACCES;
621 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
622 if (!ino)
623 return -ENOMEM;
625 spin_lock(&sbi->lookup_lock);
626 if (!list_empty(&ino->active))
627 list_del_init(&ino->active);
628 spin_unlock(&sbi->lookup_lock);
630 ino->size = strlen(symname);
631 cp = kmalloc(ino->size + 1, GFP_KERNEL);
632 if (!cp) {
633 if (!dentry->d_fsdata)
634 kfree(ino);
635 return -ENOMEM;
638 strcpy(cp, symname);
640 inode = autofs4_get_inode(dir->i_sb, ino);
641 if (!inode) {
642 kfree(cp);
643 if (!dentry->d_fsdata)
644 kfree(ino);
645 return -ENOMEM;
647 d_add(dentry, inode);
649 if (dir == dir->i_sb->s_root->d_inode)
650 dentry->d_op = &autofs4_root_dentry_operations;
651 else
652 dentry->d_op = &autofs4_dentry_operations;
654 dentry->d_fsdata = ino;
655 ino->dentry = dget(dentry);
656 atomic_inc(&ino->count);
657 p_ino = autofs4_dentry_ino(dentry->d_parent);
658 if (p_ino && dentry->d_parent != dentry)
659 atomic_inc(&p_ino->count);
660 ino->inode = inode;
662 ino->u.symlink = cp;
663 dir->i_mtime = CURRENT_TIME;
665 return 0;
669 * NOTE!
671 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
672 * that the file no longer exists. However, doing that means that the
673 * VFS layer can turn the dentry into a negative dentry. We don't want
674 * this, because the unlink is probably the result of an expire.
675 * We simply d_drop it and add it to a expiring list in the super block,
676 * which allows the dentry lookup to check for an incomplete expire.
678 * If a process is blocked on the dentry waiting for the expire to finish,
679 * it will invalidate the dentry and try to mount with a new one.
681 * Also see autofs4_dir_rmdir()..
683 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
685 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
686 struct autofs_info *ino = autofs4_dentry_ino(dentry);
687 struct autofs_info *p_ino;
689 /* This allows root to remove symlinks */
690 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
691 return -EACCES;
693 if (atomic_dec_and_test(&ino->count)) {
694 p_ino = autofs4_dentry_ino(dentry->d_parent);
695 if (p_ino && dentry->d_parent != dentry)
696 atomic_dec(&p_ino->count);
698 dput(ino->dentry);
700 dentry->d_inode->i_size = 0;
701 clear_nlink(dentry->d_inode);
703 dir->i_mtime = CURRENT_TIME;
705 spin_lock(&dcache_lock);
706 spin_lock(&sbi->lookup_lock);
707 if (list_empty(&ino->expiring))
708 list_add(&ino->expiring, &sbi->expiring_list);
709 spin_unlock(&sbi->lookup_lock);
710 spin_lock(&dentry->d_lock);
711 __d_drop(dentry);
712 spin_unlock(&dentry->d_lock);
713 spin_unlock(&dcache_lock);
715 return 0;
718 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
720 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
721 struct autofs_info *ino = autofs4_dentry_ino(dentry);
722 struct autofs_info *p_ino;
724 DPRINTK("dentry %p, removing %.*s",
725 dentry, dentry->d_name.len, dentry->d_name.name);
727 if (!autofs4_oz_mode(sbi))
728 return -EACCES;
730 spin_lock(&dcache_lock);
731 if (!list_empty(&dentry->d_subdirs)) {
732 spin_unlock(&dcache_lock);
733 return -ENOTEMPTY;
735 spin_lock(&sbi->lookup_lock);
736 if (list_empty(&ino->expiring))
737 list_add(&ino->expiring, &sbi->expiring_list);
738 spin_unlock(&sbi->lookup_lock);
739 spin_lock(&dentry->d_lock);
740 __d_drop(dentry);
741 spin_unlock(&dentry->d_lock);
742 spin_unlock(&dcache_lock);
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);
750 dentry->d_inode->i_size = 0;
751 clear_nlink(dentry->d_inode);
753 if (dir->i_nlink)
754 drop_nlink(dir);
756 return 0;
759 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
761 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
762 struct autofs_info *ino = autofs4_dentry_ino(dentry);
763 struct autofs_info *p_ino;
764 struct inode *inode;
766 if (!autofs4_oz_mode(sbi))
767 return -EACCES;
769 DPRINTK("dentry %p, creating %.*s",
770 dentry, dentry->d_name.len, dentry->d_name.name);
772 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
773 if (!ino)
774 return -ENOMEM;
776 spin_lock(&sbi->lookup_lock);
777 if (!list_empty(&ino->active))
778 list_del_init(&ino->active);
779 spin_unlock(&sbi->lookup_lock);
781 inode = autofs4_get_inode(dir->i_sb, ino);
782 if (!inode) {
783 if (!dentry->d_fsdata)
784 kfree(ino);
785 return -ENOMEM;
787 d_add(dentry, inode);
789 if (dir == dir->i_sb->s_root->d_inode)
790 dentry->d_op = &autofs4_root_dentry_operations;
791 else
792 dentry->d_op = &autofs4_dentry_operations;
794 dentry->d_fsdata = ino;
795 ino->dentry = dget(dentry);
796 atomic_inc(&ino->count);
797 p_ino = autofs4_dentry_ino(dentry->d_parent);
798 if (p_ino && dentry->d_parent != dentry)
799 atomic_inc(&p_ino->count);
800 ino->inode = inode;
801 inc_nlink(dir);
802 dir->i_mtime = CURRENT_TIME;
804 return 0;
807 /* Get/set timeout ioctl() operation */
808 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
809 unsigned long __user *p)
811 int rv;
812 unsigned long ntimeout;
814 if ((rv = get_user(ntimeout, p)) ||
815 (rv = put_user(sbi->exp_timeout/HZ, p)))
816 return rv;
818 if (ntimeout > ULONG_MAX/HZ)
819 sbi->exp_timeout = 0;
820 else
821 sbi->exp_timeout = ntimeout * HZ;
823 return 0;
826 /* Return protocol version */
827 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
829 return put_user(sbi->version, p);
832 /* Return protocol sub version */
833 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
835 return put_user(sbi->sub_version, p);
839 * Tells the daemon whether it can umount the autofs mount.
841 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
843 int status = 0;
845 if (may_umount(mnt))
846 status = 1;
848 DPRINTK("returning %d", status);
850 status = put_user(status, p);
852 return status;
855 /* Identify autofs4_dentries - this is so we can tell if there's
856 an extra dentry refcount or not. We only hold a refcount on the
857 dentry if its non-negative (ie, d_inode != NULL)
859 int is_autofs4_dentry(struct dentry *dentry)
861 return dentry && dentry->d_inode &&
862 (dentry->d_op == &autofs4_root_dentry_operations ||
863 dentry->d_op == &autofs4_dentry_operations) &&
864 dentry->d_fsdata != NULL;
868 * ioctl()'s on the root directory is the chief method for the daemon to
869 * generate kernel reactions
871 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
872 unsigned int cmd, unsigned long arg)
874 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
875 void __user *p = (void __user *)arg;
877 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
878 cmd,arg,sbi,task_pgrp_nr(current));
880 if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
881 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
882 return -ENOTTY;
884 if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
885 return -EPERM;
887 switch(cmd) {
888 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
889 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
890 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
891 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
892 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
893 autofs4_catatonic_mode(sbi);
894 return 0;
895 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
896 return autofs4_get_protover(sbi, p);
897 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
898 return autofs4_get_protosubver(sbi, p);
899 case AUTOFS_IOC_SETTIMEOUT:
900 return autofs4_get_set_timeout(sbi, p);
902 case AUTOFS_IOC_ASKUMOUNT:
903 return autofs4_ask_umount(filp->f_path.mnt, p);
905 /* return a single thing to expire */
906 case AUTOFS_IOC_EXPIRE:
907 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
908 /* same as above, but can send multiple expires through pipe */
909 case AUTOFS_IOC_EXPIRE_MULTI:
910 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
912 default:
913 return -ENOSYS;