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>
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 int autofs4_dir_close(struct inode
*inode
, struct file
*file
);
29 static int autofs4_dir_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
30 static int autofs4_root_readdir(struct file
* filp
, void * dirent
, filldir_t filldir
);
31 static struct dentry
*autofs4_lookup(struct inode
*,struct dentry
*, struct nameidata
*);
32 static void *autofs4_follow_link(struct dentry
*, struct nameidata
*);
34 #define TRIGGER_FLAGS (LOOKUP_CONTINUE | LOOKUP_DIRECTORY)
35 #define TRIGGER_INTENTS (LOOKUP_OPEN | LOOKUP_CREATE)
37 const struct file_operations autofs4_root_operations
= {
38 .open
= dcache_dir_open
,
39 .release
= dcache_dir_close
,
40 .read
= generic_read_dir
,
41 .readdir
= autofs4_root_readdir
,
42 .ioctl
= autofs4_root_ioctl
,
45 const struct file_operations autofs4_dir_operations
= {
46 .open
= autofs4_dir_open
,
47 .release
= autofs4_dir_close
,
48 .read
= generic_read_dir
,
49 .readdir
= autofs4_dir_readdir
,
52 const struct inode_operations autofs4_indirect_root_inode_operations
= {
53 .lookup
= autofs4_lookup
,
54 .unlink
= autofs4_dir_unlink
,
55 .symlink
= autofs4_dir_symlink
,
56 .mkdir
= autofs4_dir_mkdir
,
57 .rmdir
= autofs4_dir_rmdir
,
60 const struct inode_operations autofs4_direct_root_inode_operations
= {
61 .lookup
= autofs4_lookup
,
62 .unlink
= autofs4_dir_unlink
,
63 .mkdir
= autofs4_dir_mkdir
,
64 .rmdir
= autofs4_dir_rmdir
,
65 .follow_link
= autofs4_follow_link
,
68 const struct inode_operations autofs4_dir_inode_operations
= {
69 .lookup
= autofs4_lookup
,
70 .unlink
= autofs4_dir_unlink
,
71 .symlink
= autofs4_dir_symlink
,
72 .mkdir
= autofs4_dir_mkdir
,
73 .rmdir
= autofs4_dir_rmdir
,
76 static int autofs4_root_readdir(struct file
*file
, void *dirent
,
79 struct autofs_sb_info
*sbi
= autofs4_sbi(file
->f_path
.dentry
->d_sb
);
80 int oz_mode
= autofs4_oz_mode(sbi
);
82 DPRINTK("called, filp->f_pos = %lld", file
->f_pos
);
85 * Don't set reghost flag if:
86 * 1) f_pos is larger than zero -- we've already been here.
87 * 2) we haven't even enabled reghosting in the 1st place.
88 * 3) this is the daemon doing a readdir
90 if (oz_mode
&& file
->f_pos
== 0 && sbi
->reghost_enabled
)
91 sbi
->needs_reghost
= 1;
93 DPRINTK("needs_reghost = %d", sbi
->needs_reghost
);
95 return dcache_readdir(file
, dirent
, filldir
);
98 static int autofs4_dir_open(struct inode
*inode
, struct file
*file
)
100 struct dentry
*dentry
= file
->f_path
.dentry
;
101 struct vfsmount
*mnt
= file
->f_path
.mnt
;
102 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
103 struct dentry
*cursor
;
106 status
= dcache_dir_open(inode
, file
);
110 cursor
= file
->private_data
;
111 cursor
->d_fsdata
= NULL
;
113 DPRINTK("file=%p dentry=%p %.*s",
114 file
, dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
116 if (autofs4_oz_mode(sbi
))
119 if (autofs4_ispending(dentry
)) {
120 DPRINTK("dentry busy");
121 dcache_dir_close(inode
, file
);
127 if (!d_mountpoint(dentry
) && dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
131 /* In case there are stale directory dentrys from a failed mount */
132 spin_lock(&dcache_lock
);
133 empty
= list_empty(&dentry
->d_subdirs
);
134 spin_unlock(&dcache_lock
);
137 d_invalidate(dentry
);
139 nd
.flags
= LOOKUP_DIRECTORY
;
140 ret
= (dentry
->d_op
->d_revalidate
)(dentry
, &nd
);
145 dcache_dir_close(inode
, file
);
150 if (d_mountpoint(dentry
)) {
151 struct file
*fp
= NULL
;
152 struct path fp_path
= { .dentry
= dentry
, .mnt
= mnt
};
156 if (!autofs4_follow_mount(&fp_path
.mnt
, &fp_path
.dentry
)) {
158 dcache_dir_close(inode
, file
);
162 fp
= dentry_open(fp_path
.dentry
, fp_path
.mnt
, file
->f_flags
);
163 status
= PTR_ERR(fp
);
165 dcache_dir_close(inode
, file
);
168 cursor
->d_fsdata
= fp
;
175 static int autofs4_dir_close(struct inode
*inode
, struct file
*file
)
177 struct dentry
*dentry
= file
->f_path
.dentry
;
178 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
179 struct dentry
*cursor
= file
->private_data
;
182 DPRINTK("file=%p dentry=%p %.*s",
183 file
, dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
185 if (autofs4_oz_mode(sbi
))
188 if (autofs4_ispending(dentry
)) {
189 DPRINTK("dentry busy");
194 if (d_mountpoint(dentry
)) {
195 struct file
*fp
= cursor
->d_fsdata
;
200 filp_close(fp
, current
->files
);
203 dcache_dir_close(inode
, file
);
207 static int autofs4_dir_readdir(struct file
*file
, void *dirent
, filldir_t filldir
)
209 struct dentry
*dentry
= file
->f_path
.dentry
;
210 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
211 struct dentry
*cursor
= file
->private_data
;
214 DPRINTK("file=%p dentry=%p %.*s",
215 file
, dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
217 if (autofs4_oz_mode(sbi
))
220 if (autofs4_ispending(dentry
)) {
221 DPRINTK("dentry busy");
225 if (d_mountpoint(dentry
)) {
226 struct file
*fp
= cursor
->d_fsdata
;
231 if (!fp
->f_op
|| !fp
->f_op
->readdir
)
234 status
= vfs_readdir(fp
, filldir
, dirent
);
235 file
->f_pos
= fp
->f_pos
;
237 autofs4_copy_atime(file
, fp
);
241 return dcache_readdir(file
, dirent
, filldir
);
244 static int try_to_fill_dentry(struct dentry
*dentry
, int flags
)
246 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
247 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
250 /* Block on any pending expiry here; invalidate the dentry
251 when expiration is done to trigger mount request with a new
253 if (ino
&& (ino
->flags
& AUTOFS_INF_EXPIRING
)) {
254 DPRINTK("waiting for expire %p name=%.*s",
255 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
257 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
259 DPRINTK("expire done status=%d", status
);
262 * If the directory still exists the mount request must
263 * continue otherwise it can't be followed at the right
264 * time during the walk.
266 status
= d_invalidate(dentry
);
267 if (status
!= -EBUSY
)
271 DPRINTK("dentry=%p %.*s ino=%p",
272 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, dentry
->d_inode
);
275 * Wait for a pending mount, triggering one if there
278 if (dentry
->d_inode
== NULL
) {
279 DPRINTK("waiting for mount name=%.*s",
280 dentry
->d_name
.len
, dentry
->d_name
.name
);
282 status
= autofs4_wait(sbi
, dentry
, NFY_MOUNT
);
284 DPRINTK("mount done status=%d", status
);
286 /* Turn this into a real negative dentry? */
287 if (status
== -ENOENT
) {
288 spin_lock(&dentry
->d_lock
);
289 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
290 spin_unlock(&dentry
->d_lock
);
293 /* Return a negative dentry, but leave it "pending" */
296 /* Trigger mount for path component or follow link */
297 } else if (flags
& (TRIGGER_FLAGS
| TRIGGER_INTENTS
) ||
298 current
->link_count
) {
299 DPRINTK("waiting for mount name=%.*s",
300 dentry
->d_name
.len
, dentry
->d_name
.name
);
302 spin_lock(&dentry
->d_lock
);
303 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
304 spin_unlock(&dentry
->d_lock
);
305 status
= autofs4_wait(sbi
, dentry
, NFY_MOUNT
);
307 DPRINTK("mount done status=%d", status
);
310 spin_lock(&dentry
->d_lock
);
311 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
312 spin_unlock(&dentry
->d_lock
);
317 /* Initialize expiry counter after successful mount */
319 ino
->last_used
= jiffies
;
321 spin_lock(&dentry
->d_lock
);
322 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
323 spin_unlock(&dentry
->d_lock
);
328 /* For autofs direct mounts the follow link triggers the mount */
329 static void *autofs4_follow_link(struct dentry
*dentry
, struct nameidata
*nd
)
331 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
332 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
333 int oz_mode
= autofs4_oz_mode(sbi
);
334 unsigned int lookup_type
;
337 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
338 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, oz_mode
,
341 /* If it's our master or we shouldn't trigger a mount we're done */
342 lookup_type
= nd
->flags
& (TRIGGER_FLAGS
| TRIGGER_INTENTS
);
343 if (oz_mode
|| !lookup_type
)
346 /* If an expire request is pending wait for it. */
347 if (ino
&& (ino
->flags
& AUTOFS_INF_EXPIRING
)) {
348 DPRINTK("waiting for active request %p name=%.*s",
349 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
351 status
= autofs4_wait(sbi
, dentry
, NFY_NONE
);
353 DPRINTK("request done status=%d", status
);
357 * If the dentry contains directories then it is an
358 * autofs multi-mount with no root mount offset. So
359 * don't try to mount it again.
361 spin_lock(&dcache_lock
);
362 if (!d_mountpoint(dentry
) && __simple_empty(dentry
)) {
363 spin_unlock(&dcache_lock
);
365 status
= try_to_fill_dentry(dentry
, 0);
370 * The mount succeeded but if there is no root mount
371 * it must be an autofs multi-mount with no root offset
372 * so we don't need to follow the mount.
374 if (d_mountpoint(dentry
)) {
375 if (!autofs4_follow_mount(&nd
->path
.mnt
,
384 spin_unlock(&dcache_lock
);
391 return ERR_PTR(status
);
395 * Revalidate is called on every cache lookup. Some of those
396 * cache lookups may actually happen while the dentry is not
397 * yet completely filled in, and revalidate has to delay such
400 static int autofs4_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
402 struct inode
*dir
= dentry
->d_parent
->d_inode
;
403 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
404 int oz_mode
= autofs4_oz_mode(sbi
);
405 int flags
= nd
? nd
->flags
: 0;
409 if (autofs4_ispending(dentry
)) {
410 /* The daemon never causes a mount to trigger */
415 * A zero status is success otherwise we have a
416 * negative error code.
418 status
= try_to_fill_dentry(dentry
, flags
);
423 * A status of EAGAIN here means that the dentry has gone
424 * away while waiting for an expire to complete. If we are
425 * racing with expire lookup will wait for it so this must
426 * be a revalidate and we need to send it to lookup.
428 if (status
== -EAGAIN
)
434 /* Negative dentry.. invalidate if "old" */
435 if (dentry
->d_inode
== NULL
)
438 /* Check for a non-mountpoint directory with no contents */
439 spin_lock(&dcache_lock
);
440 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
441 !d_mountpoint(dentry
) &&
442 __simple_empty(dentry
)) {
443 DPRINTK("dentry=%p %.*s, emptydir",
444 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
445 spin_unlock(&dcache_lock
);
446 /* The daemon never causes a mount to trigger */
451 * A zero status is success otherwise we have a
452 * negative error code.
454 status
= try_to_fill_dentry(dentry
, flags
);
460 spin_unlock(&dcache_lock
);
465 void autofs4_dentry_release(struct dentry
*de
)
467 struct autofs_info
*inf
;
469 DPRINTK("releasing %p", de
);
471 inf
= autofs4_dentry_ino(de
);
475 struct autofs_sb_info
*sbi
= autofs4_sbi(de
->d_sb
);
478 spin_lock(&sbi
->lookup_lock
);
479 if (!list_empty(&inf
->active
))
480 list_del(&inf
->active
);
481 if (!list_empty(&inf
->expiring
))
482 list_del(&inf
->expiring
);
483 spin_unlock(&sbi
->lookup_lock
);
489 autofs4_free_ino(inf
);
493 /* For dentries of directories in the root dir */
494 static struct dentry_operations autofs4_root_dentry_operations
= {
495 .d_revalidate
= autofs4_revalidate
,
496 .d_release
= autofs4_dentry_release
,
499 /* For other dentries */
500 static struct dentry_operations autofs4_dentry_operations
= {
501 .d_revalidate
= autofs4_revalidate
,
502 .d_release
= autofs4_dentry_release
,
505 static struct dentry
*autofs4_lookup_active(struct autofs_sb_info
*sbi
, struct dentry
*parent
, struct qstr
*name
)
507 unsigned int len
= name
->len
;
508 unsigned int hash
= name
->hash
;
509 const unsigned char *str
= name
->name
;
510 struct list_head
*p
, *head
;
512 spin_lock(&dcache_lock
);
513 spin_lock(&sbi
->lookup_lock
);
514 head
= &sbi
->active_list
;
515 list_for_each(p
, head
) {
516 struct autofs_info
*ino
;
517 struct dentry
*dentry
;
520 ino
= list_entry(p
, struct autofs_info
, active
);
521 dentry
= ino
->dentry
;
523 spin_lock(&dentry
->d_lock
);
526 if (atomic_read(&dentry
->d_count
) == 0)
529 qstr
= &dentry
->d_name
;
531 if (dentry
->d_name
.hash
!= hash
)
533 if (dentry
->d_parent
!= parent
)
536 if (qstr
->len
!= len
)
538 if (memcmp(qstr
->name
, str
, len
))
541 if (d_unhashed(dentry
)) {
543 spin_unlock(&dentry
->d_lock
);
544 spin_unlock(&sbi
->lookup_lock
);
545 spin_unlock(&dcache_lock
);
549 spin_unlock(&dentry
->d_lock
);
551 spin_unlock(&sbi
->lookup_lock
);
552 spin_unlock(&dcache_lock
);
557 static struct dentry
*autofs4_lookup_expiring(struct autofs_sb_info
*sbi
, struct dentry
*parent
, struct qstr
*name
)
559 unsigned int len
= name
->len
;
560 unsigned int hash
= name
->hash
;
561 const unsigned char *str
= name
->name
;
562 struct list_head
*p
, *head
;
564 spin_lock(&dcache_lock
);
565 spin_lock(&sbi
->lookup_lock
);
566 head
= &sbi
->expiring_list
;
567 list_for_each(p
, head
) {
568 struct autofs_info
*ino
;
569 struct dentry
*dentry
;
572 ino
= list_entry(p
, struct autofs_info
, expiring
);
573 dentry
= ino
->dentry
;
575 spin_lock(&dentry
->d_lock
);
577 /* Bad luck, we've already been dentry_iput */
578 if (!dentry
->d_inode
)
581 qstr
= &dentry
->d_name
;
583 if (dentry
->d_name
.hash
!= hash
)
585 if (dentry
->d_parent
!= parent
)
588 if (qstr
->len
!= len
)
590 if (memcmp(qstr
->name
, str
, len
))
593 if (d_unhashed(dentry
)) {
595 spin_unlock(&dentry
->d_lock
);
596 spin_unlock(&sbi
->lookup_lock
);
597 spin_unlock(&dcache_lock
);
601 spin_unlock(&dentry
->d_lock
);
603 spin_unlock(&sbi
->lookup_lock
);
604 spin_unlock(&dcache_lock
);
609 /* Lookups in the root directory */
610 static struct dentry
*autofs4_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
612 struct autofs_sb_info
*sbi
;
613 struct autofs_info
*ino
;
614 struct dentry
*expiring
, *unhashed
;
617 DPRINTK("name = %.*s",
618 dentry
->d_name
.len
, dentry
->d_name
.name
);
620 /* File name too long to exist */
621 if (dentry
->d_name
.len
> NAME_MAX
)
622 return ERR_PTR(-ENAMETOOLONG
);
624 sbi
= autofs4_sbi(dir
->i_sb
);
625 oz_mode
= autofs4_oz_mode(sbi
);
627 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
628 current
->pid
, task_pgrp_nr(current
), sbi
->catatonic
, oz_mode
);
630 expiring
= autofs4_lookup_expiring(sbi
, dentry
->d_parent
, &dentry
->d_name
);
633 * If we are racing with expire the request might not
634 * be quite complete but the directory has been removed
635 * so it must have been successful, so just wait for it.
637 ino
= autofs4_dentry_ino(expiring
);
638 while (ino
&& (ino
->flags
& AUTOFS_INF_EXPIRING
)) {
639 DPRINTK("wait for incomplete expire %p name=%.*s",
640 expiring
, expiring
->d_name
.len
,
641 expiring
->d_name
.name
);
642 autofs4_wait(sbi
, expiring
, NFY_NONE
);
643 DPRINTK("request completed");
645 spin_lock(&sbi
->lookup_lock
);
646 if (!list_empty(&ino
->expiring
))
647 list_del_init(&ino
->expiring
);
648 spin_unlock(&sbi
->lookup_lock
);
652 unhashed
= autofs4_lookup_active(sbi
, dentry
->d_parent
, &dentry
->d_name
);
657 * Mark the dentry incomplete but don't hash it. We do this
658 * to serialize our inode creation operations (symlink and
659 * mkdir) which prevents deadlock during the callback to
660 * the daemon. Subsequent user space lookups for the same
661 * dentry are placed on the wait queue while the daemon
662 * itself is allowed passage unresticted so the create
663 * operation itself can then hash the dentry. Finally,
664 * we check for the hashed dentry and return the newly
667 dentry
->d_op
= &autofs4_root_dentry_operations
;
670 * And we need to ensure that the same dentry is used for
671 * all following lookup calls until it is hashed so that
672 * the dentry flags are persistent throughout the request.
674 ino
= autofs4_init_ino(NULL
, sbi
, 0555);
676 return ERR_PTR(-ENOMEM
);
678 dentry
->d_fsdata
= ino
;
679 ino
->dentry
= dentry
;
681 spin_lock(&sbi
->lookup_lock
);
682 list_add(&ino
->active
, &sbi
->active_list
);
683 spin_unlock(&sbi
->lookup_lock
);
685 d_instantiate(dentry
, NULL
);
689 spin_lock(&dentry
->d_lock
);
690 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
691 spin_unlock(&dentry
->d_lock
);
692 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
693 mutex_unlock(&dir
->i_mutex
);
694 (dentry
->d_op
->d_revalidate
)(dentry
, nd
);
695 mutex_lock(&dir
->i_mutex
);
700 * If we are still pending, check if we had to handle
701 * a signal. If so we can force a restart..
703 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
704 /* See if we were interrupted */
705 if (signal_pending(current
)) {
706 sigset_t
*sigset
= ¤t
->pending
.signal
;
707 if (sigismember (sigset
, SIGKILL
) ||
708 sigismember (sigset
, SIGQUIT
) ||
709 sigismember (sigset
, SIGINT
)) {
712 return ERR_PTR(-ERESTARTNOINTR
);
716 spin_lock(&dentry
->d_lock
);
717 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
718 spin_unlock(&dentry
->d_lock
);
723 * If this dentry is unhashed, then we shouldn't honour this
724 * lookup. Returning ENOENT here doesn't do the right thing
725 * for all system calls, but it should be OK for the operations
726 * we permit from an autofs.
728 if (!oz_mode
&& d_unhashed(dentry
)) {
730 * A user space application can (and has done in the past)
731 * remove and re-create this directory during the callback.
732 * This can leave us with an unhashed dentry, but a
733 * successful mount! So we need to perform another
734 * cached lookup in case the dentry now exists.
736 struct dentry
*parent
= dentry
->d_parent
;
737 struct dentry
*new = d_lookup(parent
, &dentry
->d_name
);
741 dentry
= ERR_PTR(-ENOENT
);
755 static int autofs4_dir_symlink(struct inode
*dir
,
756 struct dentry
*dentry
,
759 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
760 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
761 struct autofs_info
*p_ino
;
765 DPRINTK("%s <- %.*s", symname
,
766 dentry
->d_name
.len
, dentry
->d_name
.name
);
768 if (!autofs4_oz_mode(sbi
))
771 ino
= autofs4_init_ino(ino
, sbi
, S_IFLNK
| 0555);
775 spin_lock(&sbi
->lookup_lock
);
776 if (!list_empty(&ino
->active
))
777 list_del_init(&ino
->active
);
778 spin_unlock(&sbi
->lookup_lock
);
780 ino
->size
= strlen(symname
);
781 cp
= kmalloc(ino
->size
+ 1, GFP_KERNEL
);
783 if (!dentry
->d_fsdata
)
790 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
793 if (!dentry
->d_fsdata
)
797 d_add(dentry
, inode
);
799 if (dir
== dir
->i_sb
->s_root
->d_inode
)
800 dentry
->d_op
= &autofs4_root_dentry_operations
;
802 dentry
->d_op
= &autofs4_dentry_operations
;
804 dentry
->d_fsdata
= ino
;
805 ino
->dentry
= dget(dentry
);
806 atomic_inc(&ino
->count
);
807 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
808 if (p_ino
&& dentry
->d_parent
!= dentry
)
809 atomic_inc(&p_ino
->count
);
813 dir
->i_mtime
= CURRENT_TIME
;
821 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
822 * that the file no longer exists. However, doing that means that the
823 * VFS layer can turn the dentry into a negative dentry. We don't want
824 * this, because the unlink is probably the result of an expire.
825 * We simply d_drop it and add it to a expiring list in the super block,
826 * which allows the dentry lookup to check for an incomplete expire.
828 * If a process is blocked on the dentry waiting for the expire to finish,
829 * it will invalidate the dentry and try to mount with a new one.
831 * Also see autofs4_dir_rmdir()..
833 static int autofs4_dir_unlink(struct inode
*dir
, struct dentry
*dentry
)
835 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
836 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
837 struct autofs_info
*p_ino
;
839 /* This allows root to remove symlinks */
840 if (!autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
843 if (atomic_dec_and_test(&ino
->count
)) {
844 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
845 if (p_ino
&& dentry
->d_parent
!= dentry
)
846 atomic_dec(&p_ino
->count
);
850 dentry
->d_inode
->i_size
= 0;
851 clear_nlink(dentry
->d_inode
);
853 dir
->i_mtime
= CURRENT_TIME
;
855 spin_lock(&dcache_lock
);
856 spin_lock(&sbi
->lookup_lock
);
857 if (list_empty(&ino
->expiring
))
858 list_add(&ino
->expiring
, &sbi
->expiring_list
);
859 spin_unlock(&sbi
->lookup_lock
);
860 spin_lock(&dentry
->d_lock
);
862 spin_unlock(&dentry
->d_lock
);
863 spin_unlock(&dcache_lock
);
868 static int autofs4_dir_rmdir(struct inode
*dir
, struct dentry
*dentry
)
870 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
871 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
872 struct autofs_info
*p_ino
;
874 DPRINTK("dentry %p, removing %.*s",
875 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
877 if (!autofs4_oz_mode(sbi
))
880 spin_lock(&dcache_lock
);
881 if (!list_empty(&dentry
->d_subdirs
)) {
882 spin_unlock(&dcache_lock
);
885 spin_lock(&sbi
->lookup_lock
);
886 if (list_empty(&ino
->expiring
))
887 list_add(&ino
->expiring
, &sbi
->expiring_list
);
888 spin_unlock(&sbi
->lookup_lock
);
889 spin_lock(&dentry
->d_lock
);
891 spin_unlock(&dentry
->d_lock
);
892 spin_unlock(&dcache_lock
);
894 if (atomic_dec_and_test(&ino
->count
)) {
895 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
896 if (p_ino
&& dentry
->d_parent
!= dentry
)
897 atomic_dec(&p_ino
->count
);
900 dentry
->d_inode
->i_size
= 0;
901 clear_nlink(dentry
->d_inode
);
909 static int autofs4_dir_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
911 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
912 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
913 struct autofs_info
*p_ino
;
916 if (!autofs4_oz_mode(sbi
))
919 DPRINTK("dentry %p, creating %.*s",
920 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
);
922 ino
= autofs4_init_ino(ino
, sbi
, S_IFDIR
| 0555);
926 spin_lock(&sbi
->lookup_lock
);
927 if (!list_empty(&ino
->active
))
928 list_del_init(&ino
->active
);
929 spin_unlock(&sbi
->lookup_lock
);
931 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
933 if (!dentry
->d_fsdata
)
937 d_add(dentry
, inode
);
939 if (dir
== dir
->i_sb
->s_root
->d_inode
)
940 dentry
->d_op
= &autofs4_root_dentry_operations
;
942 dentry
->d_op
= &autofs4_dentry_operations
;
944 dentry
->d_fsdata
= ino
;
945 ino
->dentry
= dget(dentry
);
946 atomic_inc(&ino
->count
);
947 p_ino
= autofs4_dentry_ino(dentry
->d_parent
);
948 if (p_ino
&& dentry
->d_parent
!= dentry
)
949 atomic_inc(&p_ino
->count
);
952 dir
->i_mtime
= CURRENT_TIME
;
957 /* Get/set timeout ioctl() operation */
958 static inline int autofs4_get_set_timeout(struct autofs_sb_info
*sbi
,
959 unsigned long __user
*p
)
962 unsigned long ntimeout
;
964 if ((rv
= get_user(ntimeout
, p
)) ||
965 (rv
= put_user(sbi
->exp_timeout
/HZ
, p
)))
968 if (ntimeout
> ULONG_MAX
/HZ
)
969 sbi
->exp_timeout
= 0;
971 sbi
->exp_timeout
= ntimeout
* HZ
;
976 /* Return protocol version */
977 static inline int autofs4_get_protover(struct autofs_sb_info
*sbi
, int __user
*p
)
979 return put_user(sbi
->version
, p
);
982 /* Return protocol sub version */
983 static inline int autofs4_get_protosubver(struct autofs_sb_info
*sbi
, int __user
*p
)
985 return put_user(sbi
->sub_version
, p
);
989 * Tells the daemon whether we need to reghost or not. Also, clears
990 * the reghost_needed flag.
992 static inline int autofs4_ask_reghost(struct autofs_sb_info
*sbi
, int __user
*p
)
996 DPRINTK("returning %d", sbi
->needs_reghost
);
998 status
= put_user(sbi
->needs_reghost
, p
);
1002 sbi
->needs_reghost
= 0;
1007 * Enable / Disable reghosting ioctl() operation
1009 static inline int autofs4_toggle_reghost(struct autofs_sb_info
*sbi
, int __user
*p
)
1014 status
= get_user(val
, p
);
1016 DPRINTK("reghost = %d", val
);
1021 /* turn on/off reghosting, with the val */
1022 sbi
->reghost_enabled
= val
;
1027 * Tells the daemon whether it can umount the autofs mount.
1029 static inline int autofs4_ask_umount(struct vfsmount
*mnt
, int __user
*p
)
1033 if (may_umount(mnt
))
1036 DPRINTK("returning %d", status
);
1038 status
= put_user(status
, p
);
1043 /* Identify autofs4_dentries - this is so we can tell if there's
1044 an extra dentry refcount or not. We only hold a refcount on the
1045 dentry if its non-negative (ie, d_inode != NULL)
1047 int is_autofs4_dentry(struct dentry
*dentry
)
1049 return dentry
&& dentry
->d_inode
&&
1050 (dentry
->d_op
== &autofs4_root_dentry_operations
||
1051 dentry
->d_op
== &autofs4_dentry_operations
) &&
1052 dentry
->d_fsdata
!= NULL
;
1056 * ioctl()'s on the root directory is the chief method for the daemon to
1057 * generate kernel reactions
1059 static int autofs4_root_ioctl(struct inode
*inode
, struct file
*filp
,
1060 unsigned int cmd
, unsigned long arg
)
1062 struct autofs_sb_info
*sbi
= autofs4_sbi(inode
->i_sb
);
1063 void __user
*p
= (void __user
*)arg
;
1065 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
1066 cmd
,arg
,sbi
,task_pgrp_nr(current
));
1068 if (_IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
1069 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
1072 if (!autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
1076 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
1077 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
1078 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
1079 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
1080 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
1081 autofs4_catatonic_mode(sbi
);
1083 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
1084 return autofs4_get_protover(sbi
, p
);
1085 case AUTOFS_IOC_PROTOSUBVER
: /* Get protocol sub version */
1086 return autofs4_get_protosubver(sbi
, p
);
1087 case AUTOFS_IOC_SETTIMEOUT
:
1088 return autofs4_get_set_timeout(sbi
, p
);
1090 case AUTOFS_IOC_TOGGLEREGHOST
:
1091 return autofs4_toggle_reghost(sbi
, p
);
1092 case AUTOFS_IOC_ASKREGHOST
:
1093 return autofs4_ask_reghost(sbi
, p
);
1095 case AUTOFS_IOC_ASKUMOUNT
:
1096 return autofs4_ask_umount(filp
->f_path
.mnt
, p
);
1098 /* return a single thing to expire */
1099 case AUTOFS_IOC_EXPIRE
:
1100 return autofs4_expire_run(inode
->i_sb
,filp
->f_path
.mnt
,sbi
, p
);
1101 /* same as above, but can send multiple expires through pipe */
1102 case AUTOFS_IOC_EXPIRE_MULTI
:
1103 return autofs4_expire_multi(inode
->i_sb
,filp
->f_path
.mnt
,sbi
, p
);