1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999 Jeremy Fitzhardinge <jeremy@goop.org>
8 * This file is part of the Linux kernel and is made available under
9 * the terms of the GNU General Public License, version 2, or at your
10 * option, any later version, incorporated herein by reference.
12 * ------------------------------------------------------------------------- */
14 #include <linux/errno.h>
15 #include <linux/stat.h>
16 #include <linux/param.h>
19 static struct dentry
*autofs4_dir_lookup(struct inode
*,struct dentry
*);
20 static int autofs4_dir_symlink(struct inode
*,struct dentry
*,const char *);
21 static int autofs4_dir_unlink(struct inode
*,struct dentry
*);
22 static int autofs4_dir_rmdir(struct inode
*,struct dentry
*);
23 static int autofs4_dir_mkdir(struct inode
*,struct dentry
*,int);
24 static int autofs4_root_ioctl(struct inode
*, struct file
*,unsigned int,unsigned long);
25 static struct dentry
*autofs4_root_lookup(struct inode
*,struct dentry
*);
27 struct file_operations autofs4_root_operations
= {
28 read
: generic_read_dir
,
29 readdir
: dcache_readdir
,
30 ioctl
: autofs4_root_ioctl
,
33 struct file_operations autofs4_dir_operations
= {
34 read
: generic_read_dir
,
35 readdir
: dcache_readdir
,
38 struct inode_operations autofs4_root_inode_operations
= {
39 lookup
: autofs4_root_lookup
,
40 unlink
: autofs4_dir_unlink
,
41 symlink
: autofs4_dir_symlink
,
42 mkdir
: autofs4_dir_mkdir
,
43 rmdir
: autofs4_dir_rmdir
,
46 struct inode_operations autofs4_dir_inode_operations
= {
47 lookup
: autofs4_dir_lookup
,
48 unlink
: autofs4_dir_unlink
,
49 symlink
: autofs4_dir_symlink
,
50 mkdir
: autofs4_dir_mkdir
,
51 rmdir
: autofs4_dir_rmdir
,
54 /* Update usage from here to top of tree, so that scan of
55 top-level directories will give a useful result */
56 static void autofs4_update_usage(struct dentry
*dentry
)
58 struct dentry
*top
= dentry
->d_sb
->s_root
;
60 for(; dentry
!= top
; dentry
= dentry
->d_parent
) {
61 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
64 update_atime(dentry
->d_inode
);
65 ino
->last_used
= jiffies
;
70 static int try_to_fill_dentry(struct dentry
*dentry
,
71 struct super_block
*sb
,
72 struct autofs_sb_info
*sbi
)
74 struct autofs_info
*de_info
= autofs4_dentry_ino(dentry
);
77 /* Block on any pending expiry here; invalidate the dentry
78 when expiration is done to trigger mount request with a new
80 if (de_info
&& (de_info
->flags
& AUTOFS_INF_EXPIRING
)) {
81 DPRINTK(("try_to_fill_entry: waiting for expire %p name=%.*s, flags&PENDING=%s de_info=%p de_info->flags=%x\n",
82 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
,
83 dentry
->d_flags
& DCACHE_AUTOFS_PENDING
?"t":"f",
84 de_info
, de_info
?de_info
->flags
:0));
85 status
= autofs4_wait(sbi
, &dentry
->d_name
, NFY_NONE
);
87 DPRINTK(("try_to_fill_entry: expire done status=%d\n", status
));
92 DPRINTK(("try_to_fill_entry: dentry=%p %.*s ino=%p\n",
93 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, dentry
->d_inode
));
95 /* Wait for a pending mount, triggering one if there isn't one already */
96 while(dentry
->d_inode
== NULL
) {
97 DPRINTK(("try_to_fill_entry: waiting for mount name=%.*s, de_info=%p de_info->flags=%x\n",
98 dentry
->d_name
.len
, dentry
->d_name
.name
,
99 de_info
, de_info
?de_info
->flags
:0));
100 status
= autofs4_wait(sbi
, &dentry
->d_name
, NFY_MOUNT
);
102 DPRINTK(("try_to_fill_entry: mount done status=%d\n", status
));
104 if (status
&& dentry
->d_inode
)
105 return 0; /* Try to get the kernel to invalidate this dentry */
107 /* Turn this into a real negative dentry? */
108 if (status
== -ENOENT
) {
109 dentry
->d_time
= jiffies
+ AUTOFS_NEGATIVE_TIMEOUT
;
110 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
113 /* Return a negative dentry, but leave it "pending" */
116 /* status = autofs4_wait(sbi, &dentry->d_name, NFY_MOUNT); */
119 /* If this is an unused directory that isn't a mount point,
120 bitch at the daemon and fix it in user space */
121 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
122 !d_mountpoint(dentry
) &&
123 list_empty(&dentry
->d_subdirs
)) {
124 DPRINTK(("try_to_fill_entry: mounting existing dir\n"));
125 return autofs4_wait(sbi
, &dentry
->d_name
, NFY_MOUNT
) == 0;
128 /* We don't update the usages for the autofs daemon itself, this
129 is necessary for recursive autofs mounts */
130 if (!autofs4_oz_mode(sbi
))
131 autofs4_update_usage(dentry
);
133 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
139 * Revalidate is called on every cache lookup. Some of those
140 * cache lookups may actually happen while the dentry is not
141 * yet completely filled in, and revalidate has to delay such
144 static int autofs4_root_revalidate(struct dentry
* dentry
, int flags
)
146 struct inode
* dir
= dentry
->d_parent
->d_inode
;
147 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
148 struct autofs_info
*ino
;
149 int oz_mode
= autofs4_oz_mode(sbi
);
152 if (autofs4_ispending(dentry
)) {
153 if (autofs4_oz_mode(sbi
))
156 return try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
159 /* Negative dentry.. invalidate if "old" */
160 if (dentry
->d_inode
== NULL
)
161 return (dentry
->d_time
- jiffies
<= AUTOFS_NEGATIVE_TIMEOUT
);
163 ino
= autofs4_dentry_ino(dentry
);
165 /* Check for a non-mountpoint directory with no contents */
166 if (S_ISDIR(dentry
->d_inode
->i_mode
) &&
167 !d_mountpoint(dentry
) &&
168 list_empty(&dentry
->d_subdirs
)) {
169 DPRINTK(("autofs_root_revalidate: dentry=%p %.*s, emptydir\n",
170 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
));
174 return try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
177 /* Update the usage list */
179 autofs4_update_usage(dentry
);
184 static int autofs4_revalidate(struct dentry
*dentry
, int flags
)
186 struct autofs_sb_info
*sbi
= autofs4_sbi(dentry
->d_sb
);
188 if (!autofs4_oz_mode(sbi
))
189 autofs4_update_usage(dentry
);
194 static void autofs4_dentry_release(struct dentry
*de
)
196 struct autofs_info
*inf
= autofs4_dentry_ino(de
);
198 DPRINTK(("autofs4_dentry_release: releasing %p\n", de
));
205 autofs4_free_ino(inf
);
209 /* For dentries of directories in the root dir */
210 static struct dentry_operations autofs4_root_dentry_operations
= {
211 d_revalidate
: autofs4_root_revalidate
, /* d_revalidate */
212 d_release
: autofs4_dentry_release
,
215 /* For other dentries */
216 static struct dentry_operations autofs4_dentry_operations
= {
217 d_revalidate
: autofs4_revalidate
, /* d_revalidate */
218 d_release
: autofs4_dentry_release
,
221 /* Lookups in non-root dirs never find anything - if it's there, it's
222 already in the dcache */
223 static struct dentry
*autofs4_dir_lookup(struct inode
*dir
, struct dentry
*dentry
)
226 DPRINTK(("autofs_dir_lookup: ignoring lookup of %.*s/%.*s\n",
227 dentry
->d_parent
->d_name
.len
, dentry
->d_parent
->d_name
.name
,
228 dentry
->d_name
.len
, dentry
->d_name
.name
));
231 dentry
->d_fsdata
= NULL
;
236 /* Lookups in the root directory */
237 static struct dentry
*autofs4_root_lookup(struct inode
*dir
, struct dentry
*dentry
)
239 struct autofs_sb_info
*sbi
;
242 DPRINTK(("autofs_root_lookup: name = %.*s\n",
243 dentry
->d_name
.len
, dentry
->d_name
.name
));
245 if (dentry
->d_name
.len
> NAME_MAX
)
246 return ERR_PTR(-ENAMETOOLONG
);/* File name too long to exist */
248 sbi
= autofs4_sbi(dir
->i_sb
);
250 oz_mode
= autofs4_oz_mode(sbi
);
251 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
252 current
->pid
, current
->pgrp
, sbi
->catatonic
, oz_mode
));
255 * Mark the dentry incomplete, but add it. This is needed so
256 * that the VFS layer knows about the dentry, and we can count
257 * on catching any lookups through the revalidate.
259 * Let all the hard work be done by the revalidate function that
260 * needs to be able to do this anyway..
262 * We need to do this before we release the directory semaphore.
264 dentry
->d_op
= &autofs4_root_dentry_operations
;
267 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
268 dentry
->d_fsdata
= NULL
;
271 if (dentry
->d_op
&& dentry
->d_op
->d_revalidate
) {
273 (dentry
->d_op
->d_revalidate
)(dentry
, 0);
278 * If we are still pending, check if we had to handle
279 * a signal. If so we can force a restart..
281 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
282 if (signal_pending(current
))
283 return ERR_PTR(-ERESTARTNOINTR
);
287 * If this dentry is unhashed, then we shouldn't honour this
288 * lookup even if the dentry is positive. Returning ENOENT here
289 * doesn't do the right thing for all system calls, but it should
290 * be OK for the operations we permit from an autofs.
292 if ( dentry
->d_inode
&& d_unhashed(dentry
) )
293 return ERR_PTR(-ENOENT
);
298 static int autofs4_dir_symlink(struct inode
*dir
,
299 struct dentry
*dentry
,
302 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
303 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
307 DPRINTK(("autofs_dir_symlink: %s <- %.*s\n", symname
,
308 dentry
->d_name
.len
, dentry
->d_name
.name
));
310 if (!autofs4_oz_mode(sbi
))
313 ino
= autofs4_init_ino(ino
, sbi
, S_IFLNK
| 0555);
317 ino
->size
= strlen(symname
);
318 ino
->u
.symlink
= cp
= kmalloc(ino
->size
+ 1, GFP_KERNEL
);
327 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
328 d_instantiate(dentry
, inode
);
330 if (dir
== dir
->i_sb
->s_root
->d_inode
)
331 dentry
->d_op
= &autofs4_root_dentry_operations
;
333 dentry
->d_op
= &autofs4_dentry_operations
;
335 dentry
->d_fsdata
= ino
;
336 ino
->dentry
= dget(dentry
);
339 dir
->i_mtime
= CURRENT_TIME
;
347 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
348 * that the file no longer exists. However, doing that means that the
349 * VFS layer can turn the dentry into a negative dentry. We don't want
350 * this, because since the unlink is probably the result of an expire.
351 * We simply d_drop it, which allows the dentry lookup to remount it
354 * If a process is blocked on the dentry waiting for the expire to finish,
355 * it will invalidate the dentry and try to mount with a new one.
357 * Also see autofs_dir_rmdir()..
359 static int autofs4_dir_unlink(struct inode
*dir
, struct dentry
*dentry
)
361 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
362 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
364 /* This allows root to remove symlinks */
365 if ( !autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) )
370 dentry
->d_inode
->i_size
= 0;
371 dentry
->d_inode
->i_nlink
= 0;
373 dir
->i_mtime
= CURRENT_TIME
;
375 DPRINTK(("autofs_dir_unlink: unlinking %p %.*s, count=%d\n",
376 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, dentry
->d_count
));
383 static int autofs4_dir_rmdir(struct inode
*dir
, struct dentry
*dentry
)
385 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
386 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
388 if (!autofs4_oz_mode(sbi
))
391 if (!list_empty(&dentry
->d_subdirs
))
396 dentry
->d_inode
->i_size
= 0;
397 dentry
->d_inode
->i_nlink
= 0;
402 DPRINTK(("autofs_dir_rmdir: rmdir %p %.*s, count=%d\n",
403 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
, dentry
->d_count
));
412 static int autofs4_dir_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
414 struct autofs_sb_info
*sbi
= autofs4_sbi(dir
->i_sb
);
415 struct autofs_info
*ino
= autofs4_dentry_ino(dentry
);
418 if ( !autofs4_oz_mode(sbi
) )
421 DPRINTK(("autofs_dir_mkdir: dentry %p, creating %.*s\n",
422 dentry
, dentry
->d_name
.len
, dentry
->d_name
.name
));
424 ino
= autofs4_init_ino(ino
, sbi
, S_IFDIR
| 0555);
428 inode
= autofs4_get_inode(dir
->i_sb
, ino
);
429 d_instantiate(dentry
, inode
);
431 if (dir
== dir
->i_sb
->s_root
->d_inode
)
432 dentry
->d_op
= &autofs4_root_dentry_operations
;
434 dentry
->d_op
= &autofs4_dentry_operations
;
436 dentry
->d_fsdata
= ino
;
437 ino
->dentry
= dget(dentry
);
440 dir
->i_mtime
= CURRENT_TIME
;
445 /* Get/set timeout ioctl() operation */
446 static inline int autofs4_get_set_timeout(struct autofs_sb_info
*sbi
,
450 unsigned long ntimeout
;
452 if ( (rv
= get_user(ntimeout
, p
)) ||
453 (rv
= put_user(sbi
->exp_timeout
/HZ
, p
)) )
456 if ( ntimeout
> ULONG_MAX
/HZ
)
457 sbi
->exp_timeout
= 0;
459 sbi
->exp_timeout
= ntimeout
* HZ
;
464 /* Return protocol version */
465 static inline int autofs4_get_protover(struct autofs_sb_info
*sbi
, int *p
)
467 return put_user(sbi
->version
, p
);
470 /* Identify autofs_dentries - this is so we can tell if there's
471 an extra dentry refcount or not. We only hold a refcount on the
472 dentry if its non-negative (ie, d_inode != NULL)
474 int is_autofs4_dentry(struct dentry
*dentry
)
476 return dentry
&& dentry
->d_inode
&&
477 (dentry
->d_op
== &autofs4_root_dentry_operations
||
478 dentry
->d_op
== &autofs4_dentry_operations
) &&
479 dentry
->d_fsdata
!= NULL
;
483 * ioctl()'s on the root directory is the chief method for the daemon to
484 * generate kernel reactions
486 static int autofs4_root_ioctl(struct inode
*inode
, struct file
*filp
,
487 unsigned int cmd
, unsigned long arg
)
489 struct autofs_sb_info
*sbi
= autofs4_sbi(inode
->i_sb
);
491 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",
492 cmd
,arg
,sbi
,current
->pgrp
));
494 if ( _IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
495 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
498 if ( !autofs4_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) )
502 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
503 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
504 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
505 return autofs4_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
506 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
507 autofs4_catatonic_mode(sbi
);
509 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
510 return autofs4_get_protover(sbi
, (int *)arg
);
511 case AUTOFS_IOC_SETTIMEOUT
:
512 return autofs4_get_set_timeout(sbi
,(unsigned long *)arg
);
514 /* return a single thing to expire */
515 case AUTOFS_IOC_EXPIRE
:
516 return autofs4_expire_run(inode
->i_sb
,filp
->f_vfsmnt
,sbi
,
517 (struct autofs_packet_expire
*)arg
);
518 /* same as above, but can send multiple expires through pipe */
519 case AUTOFS_IOC_EXPIRE_MULTI
:
520 return autofs4_expire_multi(inode
->i_sb
,filp
->f_vfsmnt
,sbi
,