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/smp_lock.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 static long autofs4_root_compat_ioctl(struct file
*,unsigned int,unsigned long);
32 static int autofs4_dir_open(struct inode
*inode
, struct file
*file
);
33 static struct dentry
*autofs4_lookup(struct inode
*,struct dentry
*, struct nameidata
*);
34 static void *autofs4_follow_link(struct dentry
*, struct nameidata
*);
36 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
37 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
39 const struct file_operations autofs4_root_operations
= {
40 .open
= dcache_dir_open
,
41 .release
= dcache_dir_close
,
42 .read
= generic_read_dir
,
43 .readdir
= dcache_readdir
,
44 .llseek
= dcache_dir_lseek
,
45 .unlocked_ioctl
= autofs4_root_ioctl
,
47 .compat_ioctl
= autofs4_root_compat_ioctl
,
51 const struct file_operations autofs4_dir_operations
= {
52 .open
= autofs4_dir_open
,
53 .release
= dcache_dir_close
,
54 .read
= generic_read_dir
,
55 .readdir
= dcache_readdir
,
56 .llseek
= dcache_dir_lseek
,
59 const struct inode_operations autofs4_indirect_root_inode_operations
= {
60 .lookup
= autofs4_lookup
,
61 .unlink
= autofs4_dir_unlink
,
62 .symlink
= autofs4_dir_symlink
,
63 .mkdir
= autofs4_dir_mkdir
,
64 .rmdir
= autofs4_dir_rmdir
,
67 const struct inode_operations autofs4_direct_root_inode_operations
= {
68 .lookup
= autofs4_lookup
,
69 .unlink
= autofs4_dir_unlink
,
70 .mkdir
= autofs4_dir_mkdir
,
71 .rmdir
= autofs4_dir_rmdir
,
72 .follow_link
= autofs4_follow_link
,
75 const struct inode_operations autofs4_dir_inode_operations
= {
76 .lookup
= autofs4_lookup
,
77 .unlink
= autofs4_dir_unlink
,
78 .symlink
= autofs4_dir_symlink
,
79 .mkdir
= autofs4_dir_mkdir
,
80 .rmdir
= autofs4_dir_rmdir
,
83 static void autofs4_add_active(struct dentry
*dentry
)
85 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
86 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
88 spin_lock(&sbi
->lookup_lock
);
89 if (!ino
->active_count
) {
90 if (list_empty(&ino
->active
))
91 list_add(&ino
->active
, &sbi
->active_list
);
94 spin_unlock(&sbi
->lookup_lock
);
99 static void autofs4_del_active(struct dentry
*dentry
)
101 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
102 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
104 spin_lock(&sbi
->lookup_lock
);
106 if (!ino
->active_count
) {
107 if (!list_empty(&ino
->active
))
108 list_del_init(&ino
->active
);
110 spin_unlock(&sbi
->lookup_lock
);
115 static unsigned int autofs4_need_mount(unsigned int flags
)
117 unsigned int res
= 0;
118 if (flags
& (TRIGGER_FLAGS
| TRIGGER_INTENTS
))
123 static int autofs4_dir_open(struct inode
*inode
, struct file
*file
)
125 struct dentry
*dentry
= file
->f_path
.dentry
;
126 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
128 DPRINTK("file=%p dentry=%p %.*s",
129 file
, dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
131 if (autofs4_oz_mode(sbi
))
135 * An empty directory in an autofs file system is always a
136 * mount point. The daemon must have failed to mount this
137 * during lookup so it doesn't exist. This can happen, for
138 * example, if user space returns an incorrect status for a
139 * mount request. Otherwise we're doing a readdir on the
140 * autofs file system so just let the libfs routines handle
143 spin_lock(&dcache_lock
);
144 if (!d_mountpoint(dentry
) && list_empty(&dentry
->d_subdirs
)) {
145 spin_unlock(&dcache_lock
);
148 spin_unlock(&dcache_lock
);
151 return dcache_dir_open(inode
, file
);
154 static int try_to_fill_dentry(struct dentry
*dentry
, int flags
)
156 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
157 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
160 DPRINTK("dentry=%p %.*s ino=%p",
161 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, dentry
->d_inode
);
164 * Wait for a pending mount, triggering one if there
167 if (dentry
->d_inode
== NULL
) {
168 DPRINTK("waiting for mount name=%.*s",
169 dentry
->d_name
.len
, dentry
->d_name
.name
);
171 status
= autofs4_wait(sbi
, dentry
, NFY_MOUNT
);
173 DPRINTK("mount done status=%d", status
);
175 /* Turn this into a real negative dentry? */
176 if (status
== -ENOENT
) {
177 spin_lock(&sbi
->fs_lock
);
178 ino
->flags
&= ~AUTOFS_INF_PENDING
;
179 spin_unlock(&sbi
->fs_lock
);
182 /* Return a negative dentry, but leave it "pending" */
185 /* Trigger mount for path component or follow link */
186 } else if (ino
->flags
& AUTOFS_INF_PENDING
||
187 autofs4_need_mount(flags
)) {
188 DPRINTK("waiting for mount name=%.*s",
189 dentry
->d_name
.len
, dentry
->d_name
.name
);
191 spin_lock(&sbi
->fs_lock
);
192 ino
->flags
|= AUTOFS_INF_PENDING
;
193 spin_unlock(&sbi
->fs_lock
);
194 status
= autofs4_wait(sbi
, dentry
, NFY_MOUNT
);
196 DPRINTK("mount done status=%d", status
);
199 spin_lock(&sbi
->fs_lock
);
200 ino
->flags
&= ~AUTOFS_INF_PENDING
;
201 spin_unlock(&sbi
->fs_lock
);
206 /* Initialize expiry counter after successful mount */
207 ino
->last_used
= jiffies
;
209 spin_lock(&sbi
->fs_lock
);
210 ino
->flags
&= ~AUTOFS_INF_PENDING
;
211 spin_unlock(&sbi
->fs_lock
);
216 /* For autofs direct mounts the follow link triggers the mount */
217 static void *autofs4_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
219 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
220 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
221 int oz_mode
= autofs4_oz_mode(sbi
);
222 unsigned int lookup_type
;
225 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
226 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, oz_mode
,
229 * For an expire of a covered direct or offset mount we need
230 * to break out of follow_down() at the autofs mount trigger
231 * (d_mounted--), so we can see the expiring flag, and manage
232 * the blocking and following here until the expire is completed.
235 spin_lock(&sbi
->fs_lock
);
236 if (ino
->flags
& AUTOFS_INF_EXPIRING
) {
237 spin_unlock(&sbi
->fs_lock
);
238 /* Follow down to our covering mount. */
239 if (!follow_down(&nd
->path
))
243 spin_unlock(&sbi
->fs_lock
);
247 /* If an expire request is pending everyone must wait. */
248 autofs4_expire_wait(dentry
);
250 /* We trigger a mount for almost all flags */
251 lookup_type
= autofs4_need_mount(nd
->flags
);
252 spin_lock(&sbi
->fs_lock
);
253 spin_lock(&dcache_lock
);
254 if (!(lookup_type
|| ino
->flags
& AUTOFS_INF_PENDING
)) {
255 spin_unlock(&dcache_lock
);
256 spin_unlock(&sbi
->fs_lock
);
261 * If the dentry contains directories then it is an autofs
262 * multi-mount with no root mount offset. So don't try to
265 if (ino
->flags
& AUTOFS_INF_PENDING
||
266 (!d_mountpoint(dentry
) && list_empty(&dentry
->d_subdirs
))) {
267 spin_unlock(&dcache_lock
);
268 spin_unlock(&sbi
->fs_lock
);
270 status
= try_to_fill_dentry(dentry
, nd
->flags
);
276 spin_unlock(&dcache_lock
);
277 spin_unlock(&sbi
->fs_lock
);
280 * If there is no root mount it must be an autofs
281 * multi-mount with no root offset so we don't need
284 if (d_mountpoint(dentry
)) {
285 if (!autofs4_follow_mount(&nd
->path
)) {
296 return ERR_PTR(status
);
300 * Revalidate is called on every cache lookup. Some of those
301 * cache lookups may actually happen while the dentry is not
302 * yet completely filled in, and revalidate has to delay such
305 static int autofs4_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
307 struct inode
*dir
= dentry
->d_parent
->d_inode
;
308 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
309 int oz_mode
= autofs4_oz_mode(sbi
);
310 int flags
= nd
? nd
->flags
: 0;
314 spin_lock(&sbi
->fs_lock
);
315 if (autofs4_ispending(dentry
)) {
316 /* The daemon never causes a mount to trigger */
317 spin_unlock(&sbi
->fs_lock
);
323 * If the directory has gone away due to an expire
324 * we have been called as ->d_revalidate() and so
325 * we need to return false and proceed to ->lookup().
327 if (autofs4_expire_wait(dentry
) == -EAGAIN
)
331 * A zero status is success otherwise we have a
332 * negative error code.
334 status
= try_to_fill_dentry(dentry
, flags
);
340 spin_unlock(&sbi
->fs_lock
);
342 /* Negative dentry.. invalidate if "old" */
343 if (dentry
->d_inode
== NULL
)
346 /* Check for a non-mountpoint directory with no contents */
347 spin_lock(&dcache_lock
);
348 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
349 !d_mountpoint(dentry
) && list_empty(&dentry
->d_subdirs
)) {
350 DPRINTK("dentry=%p %.*s, emptydir",
351 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
352 spin_unlock(&dcache_lock
);
354 /* The daemon never causes a mount to trigger */
359 * A zero status is success otherwise we have a
360 * negative error code.
362 status
= try_to_fill_dentry(dentry
, flags
);
368 spin_unlock(&dcache_lock
);
373 void autofs4_dentry_release(struct dentry
*de
)
375 struct autofs_info
*inf
;
377 DPRINTK("releasing %p", de
);
379 inf
= autofs4_dentry_ino(de
);
383 struct autofs_sb_info
*sbi
= autofs4_sbi(de
->d_sb
);
386 spin_lock(&sbi
->lookup_lock
);
387 if (!list_empty(&inf
->active
))
388 list_del(&inf
->active
);
389 if (!list_empty(&inf
->expiring
))
390 list_del(&inf
->expiring
);
391 spin_unlock(&sbi
->lookup_lock
);
397 autofs4_free_ino(inf
);
401 /* For dentries of directories in the root dir */
402 static const struct dentry_operations autofs4_root_dentry_operations
= {
403 .d_revalidate
= autofs4_revalidate
,
404 .d_release
= autofs4_dentry_release
,
407 /* For other dentries */
408 static const struct dentry_operations autofs4_dentry_operations
= {
409 .d_revalidate
= autofs4_revalidate
,
410 .d_release
= autofs4_dentry_release
,
413 static struct dentry
*autofs4_lookup_active(struct dentry
*dentry
)
415 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
416 struct dentry
*parent
= dentry
->d_parent
;
417 struct qstr
*name
= &dentry
->d_name
;
418 unsigned int len
= name
->len
;
419 unsigned int hash
= name
->hash
;
420 const unsigned char *str
= name
->name
;
421 struct list_head
*p
, *head
;
423 spin_lock(&dcache_lock
);
424 spin_lock(&sbi
->lookup_lock
);
425 head
= &sbi
->active_list
;
426 list_for_each(p
, head
) {
427 struct autofs_info
*ino
;
428 struct dentry
*active
;
431 ino
= list_entry(p
, struct autofs_info
, active
);
432 active
= ino
->dentry
;
434 spin_lock(&active
->d_lock
);
437 if (atomic_read(&active
->d_count
) == 0)
440 qstr
= &active
->d_name
;
442 if (active
->d_name
.hash
!= hash
)
444 if (active
->d_parent
!= parent
)
447 if (qstr
->len
!= len
)
449 if (memcmp(qstr
->name
, str
, len
))
452 if (d_unhashed(active
)) {
454 spin_unlock(&active
->d_lock
);
455 spin_unlock(&sbi
->lookup_lock
);
456 spin_unlock(&dcache_lock
);
460 spin_unlock(&active
->d_lock
);
462 spin_unlock(&sbi
->lookup_lock
);
463 spin_unlock(&dcache_lock
);
468 static struct dentry
*autofs4_lookup_expiring(struct dentry
*dentry
)
470 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
471 struct dentry
*parent
= dentry
->d_parent
;
472 struct qstr
*name
= &dentry
->d_name
;
473 unsigned int len
= name
->len
;
474 unsigned int hash
= name
->hash
;
475 const unsigned char *str
= name
->name
;
476 struct list_head
*p
, *head
;
478 spin_lock(&dcache_lock
);
479 spin_lock(&sbi
->lookup_lock
);
480 head
= &sbi
->expiring_list
;
481 list_for_each(p
, head
) {
482 struct autofs_info
*ino
;
483 struct dentry
*expiring
;
486 ino
= list_entry(p
, struct autofs_info
, expiring
);
487 expiring
= ino
->dentry
;
489 spin_lock(&expiring
->d_lock
);
491 /* Bad luck, we've already been dentry_iput */
492 if (!expiring
->d_inode
)
495 qstr
= &expiring
->d_name
;
497 if (expiring
->d_name
.hash
!= hash
)
499 if (expiring
->d_parent
!= parent
)
502 if (qstr
->len
!= len
)
504 if (memcmp(qstr
->name
, str
, len
))
507 if (d_unhashed(expiring
)) {
509 spin_unlock(&expiring
->d_lock
);
510 spin_unlock(&sbi
->lookup_lock
);
511 spin_unlock(&dcache_lock
);
515 spin_unlock(&expiring
->d_lock
);
517 spin_unlock(&sbi
->lookup_lock
);
518 spin_unlock(&dcache_lock
);
523 /* Lookups in the root directory */
524 static struct dentry
*autofs4_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
526 struct autofs_sb_info
*sbi
;
527 struct autofs_info
*ino
;
528 struct dentry
*expiring
, *active
;
531 DPRINTK("name = %.*s",
532 dentry
->d_name
.len
, dentry
->d_name
.name
);
534 /* File name too long to exist */
535 if (dentry
->d_name
.len
> NAME_MAX
)
536 return ERR_PTR(-ENAMETOOLONG
);
538 sbi
= autofs4_sbi(dir
->i_sb
);
539 oz_mode
= autofs4_oz_mode(sbi
);
541 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
542 current
->pid
, task_pgrp_nr(current
), sbi
->catatonic
, oz_mode
);
544 active
= autofs4_lookup_active(dentry
);
547 ino
= autofs4_dentry_ino(dentry
);
550 * Mark the dentry incomplete but don't hash it. We do this
551 * to serialize our inode creation operations (symlink and
552 * mkdir) which prevents deadlock during the callback to
553 * the daemon. Subsequent user space lookups for the same
554 * dentry are placed on the wait queue while the daemon
555 * itself is allowed passage unresticted so the create
556 * operation itself can then hash the dentry. Finally,
557 * we check for the hashed dentry and return the newly
560 dentry
->d_op
= &autofs4_root_dentry_operations
;
563 * And we need to ensure that the same dentry is used for
564 * all following lookup calls until it is hashed so that
565 * the dentry flags are persistent throughout the request.
567 ino
= autofs4_init_ino(NULL
, sbi
, 0555);
569 return ERR_PTR(-ENOMEM
);
571 dentry
->d_fsdata
= ino
;
572 ino
->dentry
= dentry
;
574 autofs4_add_active(dentry
);
576 d_instantiate(dentry
, NULL
);
580 mutex_unlock(&dir
->i_mutex
);
581 expiring
= autofs4_lookup_expiring(dentry
);
584 * If we are racing with expire the request might not
585 * be quite complete but the directory has been removed
586 * so it must have been successful, so just wait for it.
588 autofs4_expire_wait(expiring
);
589 autofs4_del_expiring(expiring
);
593 spin_lock(&sbi
->fs_lock
);
594 ino
->flags
|= AUTOFS_INF_PENDING
;
595 spin_unlock(&sbi
->fs_lock
);
596 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
)
597 (dentry
->d_op
->d_revalidate
)(dentry
, nd
);
598 mutex_lock(&dir
->i_mutex
);
602 * If we are still pending, check if we had to handle
603 * a signal. If so we can force a restart..
605 if (ino
->flags
& AUTOFS_INF_PENDING
) {
606 /* See if we were interrupted */
607 if (signal_pending(current
)) {
608 sigset_t
*sigset
= ¤t
->pending
.signal
;
609 if (sigismember (sigset
, SIGKILL
) ||
610 sigismember (sigset
, SIGQUIT
) ||
611 sigismember (sigset
, SIGINT
)) {
614 return ERR_PTR(-ERESTARTNOINTR
);
618 spin_lock(&sbi
->fs_lock
);
619 ino
->flags
&= ~AUTOFS_INF_PENDING
;
620 spin_unlock(&sbi
->fs_lock
);
625 * If this dentry is unhashed, then we shouldn't honour this
626 * lookup. Returning ENOENT here doesn't do the right thing
627 * for all system calls, but it should be OK for the operations
628 * we permit from an autofs.
630 if (!oz_mode
&& d_unhashed(dentry
)) {
632 * A user space application can (and has done in the past)
633 * remove and re-create this directory during the callback.
634 * This can leave us with an unhashed dentry, but a
635 * successful mount! So we need to perform another
636 * cached lookup in case the dentry now exists.
638 struct dentry
*parent
= dentry
->d_parent
;
639 struct dentry
*new = d_lookup(parent
, &dentry
->d_name
);
643 dentry
= ERR_PTR(-ENOENT
);
657 static int autofs4_dir_symlink(struct inode
*dir
,
658 struct dentry
*dentry
,
661 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
662 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
663 struct autofs_info
*p_ino
;
667 DPRINTK("%s <- %.*s", symname
,
668 dentry
->d_name
.len
, dentry
->d_name
.name
);
670 if (!autofs4_oz_mode(sbi
))
673 ino
= autofs4_init_ino(ino
, sbi
, S_IFLNK
| 0555);
677 autofs4_del_active(dentry
);
679 ino
->size
= strlen(symname
);
680 cp
= kmalloc(ino
->size
+ 1, GFP_KERNEL
);
682 if (!dentry
->d_fsdata
)
689 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
692 if (!dentry
->d_fsdata
)
696 d_add(dentry
, inode
);
698 if (dir
== dir
->i_sb
->s_root
->d_inode
)
699 dentry
->d_op
= &autofs4_root_dentry_operations
;
701 dentry
->d_op
= &autofs4_dentry_operations
;
703 dentry
->d_fsdata
= ino
;
704 ino
->dentry
= dget(dentry
);
705 atomic_inc(&ino
->count
);
706 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
707 if (p_ino
&& dentry
->d_parent
!= dentry
)
708 atomic_inc(&p_ino
->count
);
712 dir
->i_mtime
= CURRENT_TIME
;
720 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
721 * that the file no longer exists. However, doing that means that the
722 * VFS layer can turn the dentry into a negative dentry. We don't want
723 * this, because the unlink is probably the result of an expire.
724 * We simply d_drop it and add it to a expiring list in the super block,
725 * which allows the dentry lookup to check for an incomplete expire.
727 * If a process is blocked on the dentry waiting for the expire to finish,
728 * it will invalidate the dentry and try to mount with a new one.
730 * Also see autofs4_dir_rmdir()..
732 static int autofs4_dir_unlink(struct inode
*dir
, struct dentry
*dentry
)
734 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
735 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
736 struct autofs_info
*p_ino
;
738 /* This allows root to remove symlinks */
739 if (!autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
742 if (atomic_dec_and_test(&ino
->count
)) {
743 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
744 if (p_ino
&& dentry
->d_parent
!= dentry
)
745 atomic_dec(&p_ino
->count
);
749 dentry
->d_inode
->i_size
= 0;
750 clear_nlink(dentry
->d_inode
);
752 dir
->i_mtime
= CURRENT_TIME
;
754 spin_lock(&dcache_lock
);
755 autofs4_add_expiring(dentry
);
756 spin_lock(&dentry
->d_lock
);
758 spin_unlock(&dentry
->d_lock
);
759 spin_unlock(&dcache_lock
);
764 static int autofs4_dir_rmdir(struct inode
*dir
, struct dentry
*dentry
)
766 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
767 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
768 struct autofs_info
*p_ino
;
770 DPRINTK("dentry %p, removing %.*s",
771 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
773 if (!autofs4_oz_mode(sbi
))
776 spin_lock(&dcache_lock
);
777 if (!list_empty(&dentry
->d_subdirs
)) {
778 spin_unlock(&dcache_lock
);
781 autofs4_add_expiring(dentry
);
782 spin_lock(&dentry
->d_lock
);
784 spin_unlock(&dentry
->d_lock
);
785 spin_unlock(&dcache_lock
);
787 if (atomic_dec_and_test(&ino
->count
)) {
788 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
789 if (p_ino
&& dentry
->d_parent
!= dentry
)
790 atomic_dec(&p_ino
->count
);
793 dentry
->d_inode
->i_size
= 0;
794 clear_nlink(dentry
->d_inode
);
802 static int autofs4_dir_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
804 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
805 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
806 struct autofs_info
*p_ino
;
809 if (!autofs4_oz_mode(sbi
))
812 DPRINTK("dentry %p, creating %.*s",
813 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
815 ino
= autofs4_init_ino(ino
, sbi
, S_IFDIR
| 0555);
819 autofs4_del_active(dentry
);
821 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
823 if (!dentry
->d_fsdata
)
827 d_add(dentry
, inode
);
829 if (dir
== dir
->i_sb
->s_root
->d_inode
)
830 dentry
->d_op
= &autofs4_root_dentry_operations
;
832 dentry
->d_op
= &autofs4_dentry_operations
;
834 dentry
->d_fsdata
= ino
;
835 ino
->dentry
= dget(dentry
);
836 atomic_inc(&ino
->count
);
837 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
838 if (p_ino
&& dentry
->d_parent
!= dentry
)
839 atomic_inc(&p_ino
->count
);
842 dir
->i_mtime
= CURRENT_TIME
;
847 /* Get/set timeout ioctl() operation */
849 static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info
*sbi
,
850 compat_ulong_t __user
*p
)
853 unsigned long ntimeout
;
855 if ((rv
= get_user(ntimeout
, p
)) ||
856 (rv
= put_user(sbi
->exp_timeout
/HZ
, p
)))
859 if (ntimeout
> UINT_MAX
/HZ
)
860 sbi
->exp_timeout
= 0;
862 sbi
->exp_timeout
= ntimeout
* HZ
;
868 static inline int autofs4_get_set_timeout(struct autofs_sb_info
*sbi
,
869 unsigned long __user
*p
)
872 unsigned long ntimeout
;
874 if ((rv
= get_user(ntimeout
, p
)) ||
875 (rv
= put_user(sbi
->exp_timeout
/HZ
, p
)))
878 if (ntimeout
> ULONG_MAX
/HZ
)
879 sbi
->exp_timeout
= 0;
881 sbi
->exp_timeout
= ntimeout
* HZ
;
886 /* Return protocol version */
887 static inline int autofs4_get_protover(struct autofs_sb_info
*sbi
, int __user
*p
)
889 return put_user(sbi
->version
, p
);
892 /* Return protocol sub version */
893 static inline int autofs4_get_protosubver(struct autofs_sb_info
*sbi
, int __user
*p
)
895 return put_user(sbi
->sub_version
, p
);
899 * Tells the daemon whether it can umount the autofs mount.
901 static inline int autofs4_ask_umount(struct vfsmount
*mnt
, int __user
*p
)
908 DPRINTK("returning %d", status
);
910 status
= put_user(status
, p
);
915 /* Identify autofs4_dentries - this is so we can tell if there's
916 an extra dentry refcount or not. We only hold a refcount on the
917 dentry if its non-negative (ie, d_inode != NULL)
919 int is_autofs4_dentry(struct dentry
*dentry
)
921 return dentry
&& dentry
->d_inode
&&
922 (dentry
->d_op
== &autofs4_root_dentry_operations
||
923 dentry
->d_op
== &autofs4_dentry_operations
) &&
924 dentry
->d_fsdata
!= NULL
;
928 * ioctl()'s on the root directory is the chief method for the daemon to
929 * generate kernel reactions
931 static int autofs4_root_ioctl_unlocked(struct inode
*inode
, struct file
*filp
,
932 unsigned int cmd
, unsigned long arg
)
934 struct autofs_sb_info
*sbi
= autofs4_sbi(inode
->i_sb
);
935 void __user
*p
= (void __user
*)arg
;
937 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
938 cmd
,arg
,sbi
,task_pgrp_nr(current
));
940 if (_IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
941 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
944 if (!autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
948 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
949 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
950 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
951 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
952 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
953 autofs4_catatonic_mode(sbi
);
955 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
956 return autofs4_get_protover(sbi
, p
);
957 case AUTOFS_IOC_PROTOSUBVER
: /* Get protocol sub version */
958 return autofs4_get_protosubver(sbi
, p
);
959 case AUTOFS_IOC_SETTIMEOUT
:
960 return autofs4_get_set_timeout(sbi
, p
);
962 case AUTOFS_IOC_SETTIMEOUT32
:
963 return autofs4_compat_get_set_timeout(sbi
, p
);
966 case AUTOFS_IOC_ASKUMOUNT
:
967 return autofs4_ask_umount(filp
->f_path
.mnt
, p
);
969 /* return a single thing to expire */
970 case AUTOFS_IOC_EXPIRE
:
971 return autofs4_expire_run(inode
->i_sb
,filp
->f_path
.mnt
,sbi
, p
);
972 /* same as above, but can send multiple expires through pipe */
973 case AUTOFS_IOC_EXPIRE_MULTI
:
974 return autofs4_expire_multi(inode
->i_sb
,filp
->f_path
.mnt
,sbi
, p
);
981 static long autofs4_root_ioctl(struct file
*filp
,
982 unsigned int cmd
, unsigned long arg
)
985 struct inode
*inode
= filp
->f_dentry
->d_inode
;
988 ret
= autofs4_root_ioctl_unlocked(inode
, filp
, cmd
, arg
);
995 static long autofs4_root_compat_ioctl(struct file
*filp
,
996 unsigned int cmd
, unsigned long arg
)
998 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
1002 if (cmd
== AUTOFS_IOC_READY
|| cmd
== AUTOFS_IOC_FAIL
)
1003 ret
= autofs4_root_ioctl_unlocked(inode
, filp
, cmd
, arg
);
1005 ret
= autofs4_root_ioctl_unlocked(inode
, filp
, cmd
,
1006 (unsigned long)compat_ptr(arg
));