1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
7 * This file is part of the Linux kernel and is made available under
8 * the terms of the GNU General Public License, version 2, or at your
9 * option, any later version, incorporated herein by reference.
11 * ------------------------------------------------------------------------- */
13 #include <linux/capability.h>
14 #include <linux/errno.h>
15 #include <linux/stat.h>
16 #include <linux/param.h>
17 #include <linux/time.h>
18 #include <linux/smp_lock.h>
21 static int autofs_root_readdir(struct file
*,void *,filldir_t
);
22 static struct dentry
*autofs_root_lookup(struct inode
*,struct dentry
*, struct nameidata
*);
23 static int autofs_root_symlink(struct inode
*,struct dentry
*,const char *);
24 static int autofs_root_unlink(struct inode
*,struct dentry
*);
25 static int autofs_root_rmdir(struct inode
*,struct dentry
*);
26 static int autofs_root_mkdir(struct inode
*,struct dentry
*,int);
27 static int autofs_root_ioctl(struct inode
*, struct file
*,unsigned int,unsigned long);
29 const struct file_operations autofs_root_operations
= {
30 .read
= generic_read_dir
,
31 .readdir
= autofs_root_readdir
,
32 .ioctl
= autofs_root_ioctl
,
35 const struct inode_operations autofs_root_inode_operations
= {
36 .lookup
= autofs_root_lookup
,
37 .unlink
= autofs_root_unlink
,
38 .symlink
= autofs_root_symlink
,
39 .mkdir
= autofs_root_mkdir
,
40 .rmdir
= autofs_root_rmdir
,
43 static int autofs_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
45 struct autofs_dir_ent
*ent
= NULL
;
46 struct autofs_dirhash
*dirhash
;
47 struct autofs_sb_info
*sbi
;
48 struct inode
* inode
= filp
->f_path
.dentry
->d_inode
;
53 sbi
= autofs_sbi(inode
->i_sb
);
54 dirhash
= &sbi
->dirhash
;
60 if (filldir(dirent
, ".", 1, nr
, inode
->i_ino
, DT_DIR
) < 0)
65 if (filldir(dirent
, "..", 2, nr
, inode
->i_ino
, DT_DIR
) < 0)
70 while ( onr
= nr
, ent
= autofs_hash_enum(dirhash
,&nr
,ent
) ) {
71 if ( !ent
->dentry
|| d_mountpoint(ent
->dentry
) ) {
72 if (filldir(dirent
,ent
->name
,ent
->len
,onr
,ent
->ino
,DT_UNKNOWN
) < 0)
85 static int try_to_fill_dentry(struct dentry
*dentry
, struct super_block
*sb
, struct autofs_sb_info
*sbi
)
88 struct autofs_dir_ent
*ent
;
91 if ( !(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)) ) {
93 if ( status
&& dentry
->d_inode
) {
94 if ( status
!= -ENOENT
)
95 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status
, dentry
->d_name
.name
);
96 return 0; /* Try to get the kernel to invalidate this dentry */
99 /* Turn this into a real negative dentry? */
100 if (status
== -ENOENT
) {
101 dentry
->d_time
= jiffies
+ AUTOFS_NEGATIVE_TIMEOUT
;
102 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
105 /* Return a negative dentry, but leave it "pending" */
108 status
= autofs_wait(sbi
, &dentry
->d_name
);
109 } while (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)) );
112 /* Abuse this field as a pointer to the directory entry, used to
113 find the expire list pointers */
114 dentry
->d_time
= (unsigned long) ent
;
116 if (!dentry
->d_inode
) {
117 inode
= iget(sb
, ent
->ino
);
119 /* Failed, but leave pending for next time */
122 dentry
->d_inode
= inode
;
125 /* If this is a directory that isn't a mount point, bitch at the
126 daemon and fix it in user space */
127 if ( S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
) ) {
128 return !autofs_wait(sbi
, &dentry
->d_name
);
131 /* We don't update the usages for the autofs daemon itself, this
132 is necessary for recursive autofs mounts */
133 if ( !autofs_oz_mode(sbi
) ) {
134 autofs_update_usage(&sbi
->dirhash
,ent
);
137 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
143 * Revalidate is called on every cache lookup. Some of those
144 * cache lookups may actually happen while the dentry is not
145 * yet completely filled in, and revalidate has to delay such
148 static int autofs_revalidate(struct dentry
* dentry
, struct nameidata
*nd
)
151 struct autofs_sb_info
*sbi
;
152 struct autofs_dir_ent
*ent
;
156 dir
= dentry
->d_parent
->d_inode
;
157 sbi
= autofs_sbi(dir
->i_sb
);
160 if ( dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
161 if (autofs_oz_mode(sbi
))
164 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
169 /* Negative dentry.. invalidate if "old" */
170 if (!dentry
->d_inode
) {
172 return (dentry
->d_time
- jiffies
<= AUTOFS_NEGATIVE_TIMEOUT
);
175 /* Check for a non-mountpoint directory */
176 if ( S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
) ) {
177 if (autofs_oz_mode(sbi
))
180 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
185 /* Update the usage list */
186 if ( !autofs_oz_mode(sbi
) ) {
187 ent
= (struct autofs_dir_ent
*) dentry
->d_time
;
189 autofs_update_usage(&sbi
->dirhash
,ent
);
195 static struct dentry_operations autofs_dentry_operations
= {
196 .d_revalidate
= autofs_revalidate
,
199 static struct dentry
*autofs_root_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
201 struct autofs_sb_info
*sbi
;
204 DPRINTK(("autofs_root_lookup: name = "));
206 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
208 if (dentry
->d_name
.len
> NAME_MAX
) {
210 return ERR_PTR(-ENAMETOOLONG
);/* File name too long to exist */
213 sbi
= autofs_sbi(dir
->i_sb
);
215 oz_mode
= autofs_oz_mode(sbi
);
216 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
217 current
->pid
, process_group(current
), sbi
->catatonic
, oz_mode
));
220 * Mark the dentry incomplete, but add it. This is needed so
221 * that the VFS layer knows about the dentry, and we can count
222 * on catching any lookups through the revalidate.
224 * Let all the hard work be done by the revalidate function that
225 * needs to be able to do this anyway..
227 * We need to do this before we release the directory semaphore.
229 dentry
->d_op
= &autofs_dentry_operations
;
230 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
233 mutex_unlock(&dir
->i_mutex
);
234 autofs_revalidate(dentry
, nd
);
235 mutex_lock(&dir
->i_mutex
);
238 * If we are still pending, check if we had to handle
239 * a signal. If so we can force a restart..
241 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
242 /* See if we were interrupted */
243 if (signal_pending(current
)) {
244 sigset_t
*sigset
= ¤t
->pending
.signal
;
245 if (sigismember (sigset
, SIGKILL
) ||
246 sigismember (sigset
, SIGQUIT
) ||
247 sigismember (sigset
, SIGINT
)) {
249 return ERR_PTR(-ERESTARTNOINTR
);
256 * If this dentry is unhashed, then we shouldn't honour this
257 * lookup even if the dentry is positive. Returning ENOENT here
258 * doesn't do the right thing for all system calls, but it should
259 * be OK for the operations we permit from an autofs.
261 if ( dentry
->d_inode
&& d_unhashed(dentry
) )
262 return ERR_PTR(-ENOENT
);
267 static int autofs_root_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
269 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
270 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
271 struct autofs_dir_ent
*ent
;
274 struct autofs_symlink
*sl
;
276 DPRINTK(("autofs_root_symlink: %s <- ", symname
));
277 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
280 if ( !autofs_oz_mode(sbi
) ) {
285 if ( autofs_hash_lookup(dh
, &dentry
->d_name
) ) {
290 n
= find_first_zero_bit(sbi
->symlink_bitmap
,AUTOFS_MAX_SYMLINKS
);
291 if ( n
>= AUTOFS_MAX_SYMLINKS
) {
296 set_bit(n
,sbi
->symlink_bitmap
);
297 sl
= &sbi
->symlink
[n
];
298 sl
->len
= strlen(symname
);
299 sl
->data
= kmalloc(slsize
= sl
->len
+1, GFP_KERNEL
);
301 clear_bit(n
,sbi
->symlink_bitmap
);
306 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
309 clear_bit(n
,sbi
->symlink_bitmap
);
314 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
318 clear_bit(n
,sbi
->symlink_bitmap
);
323 memcpy(sl
->data
,symname
,slsize
);
324 sl
->mtime
= get_seconds();
326 ent
->ino
= AUTOFS_FIRST_SYMLINK
+ n
;
327 ent
->hash
= dentry
->d_name
.hash
;
328 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
329 ent
->dentry
= NULL
; /* We don't keep the dentry for symlinks */
331 autofs_hash_insert(dh
,ent
);
332 d_instantiate(dentry
, iget(dir
->i_sb
,ent
->ino
));
340 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
341 * that the file no longer exists. However, doing that means that the
342 * VFS layer can turn the dentry into a negative dentry, which we
343 * obviously do not want (we're dropping the entry not because it
344 * doesn't exist, but because it has timed out).
346 * Also see autofs_root_rmdir()..
348 static int autofs_root_unlink(struct inode
*dir
, struct dentry
*dentry
)
350 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
351 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
352 struct autofs_dir_ent
*ent
;
355 /* This allows root to remove symlinks */
357 if ( !autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) ) {
362 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
368 n
= ent
->ino
- AUTOFS_FIRST_SYMLINK
;
369 if ( n
>= AUTOFS_MAX_SYMLINKS
) {
371 return -EISDIR
; /* It's a directory, dummy */
373 if ( !test_bit(n
,sbi
->symlink_bitmap
) ) {
375 return -EINVAL
; /* Nonexistent symlink? Shouldn't happen */
378 dentry
->d_time
= (unsigned long)(struct autofs_dirhash
*)NULL
;
379 autofs_hash_delete(ent
);
380 clear_bit(n
,sbi
->symlink_bitmap
);
381 kfree(sbi
->symlink
[n
].data
);
388 static int autofs_root_rmdir(struct inode
*dir
, struct dentry
*dentry
)
390 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
391 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
392 struct autofs_dir_ent
*ent
;
395 if ( !autofs_oz_mode(sbi
) ) {
400 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
406 if ( (unsigned int)ent
->ino
< AUTOFS_FIRST_DIR_INO
) {
408 return -ENOTDIR
; /* Not a directory */
411 if ( ent
->dentry
!= dentry
) {
412 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry
->d_name
.name
);
415 dentry
->d_time
= (unsigned long)(struct autofs_dir_ent
*)NULL
;
416 autofs_hash_delete(ent
);
424 static int autofs_root_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
426 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
427 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
428 struct autofs_dir_ent
*ent
;
432 if ( !autofs_oz_mode(sbi
) ) {
437 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
443 if ( sbi
->next_dir_ino
< AUTOFS_FIRST_DIR_INO
) {
444 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
448 ino
= sbi
->next_dir_ino
++;
450 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
456 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
463 ent
->hash
= dentry
->d_name
.hash
;
464 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
466 ent
->dentry
= dentry
;
467 autofs_hash_insert(dh
,ent
);
470 d_instantiate(dentry
, iget(dir
->i_sb
,ino
));
476 /* Get/set timeout ioctl() operation */
477 static inline int autofs_get_set_timeout(struct autofs_sb_info
*sbi
,
478 unsigned long __user
*p
)
480 unsigned long ntimeout
;
482 if (get_user(ntimeout
, p
) ||
483 put_user(sbi
->exp_timeout
/ HZ
, p
))
486 if ( ntimeout
> ULONG_MAX
/HZ
)
487 sbi
->exp_timeout
= 0;
489 sbi
->exp_timeout
= ntimeout
* HZ
;
494 /* Return protocol version */
495 static inline int autofs_get_protover(int __user
*p
)
497 return put_user(AUTOFS_PROTO_VERSION
, p
);
500 /* Perform an expiry operation */
501 static inline int autofs_expire_run(struct super_block
*sb
,
502 struct autofs_sb_info
*sbi
,
503 struct vfsmount
*mnt
,
504 struct autofs_packet_expire __user
*pkt_p
)
506 struct autofs_dir_ent
*ent
;
507 struct autofs_packet_expire pkt
;
509 memset(&pkt
,0,sizeof pkt
);
511 pkt
.hdr
.proto_version
= AUTOFS_PROTO_VERSION
;
512 pkt
.hdr
.type
= autofs_ptype_expire
;
514 if ( !sbi
->exp_timeout
||
515 !(ent
= autofs_expire(sb
,sbi
,mnt
)) )
519 memcpy(pkt
.name
, ent
->name
, pkt
.len
);
520 pkt
.name
[pkt
.len
] = '\0';
522 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
529 * ioctl()'s on the root directory is the chief method for the daemon to
530 * generate kernel reactions
532 static int autofs_root_ioctl(struct inode
*inode
, struct file
*filp
,
533 unsigned int cmd
, unsigned long arg
)
535 struct autofs_sb_info
*sbi
= autofs_sbi(inode
->i_sb
);
536 void __user
*argp
= (void __user
*)arg
;
538 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd
,arg
,sbi
,process_group(current
)));
540 if ( _IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
541 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
544 if ( !autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) )
548 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
549 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
550 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
551 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
552 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
553 autofs_catatonic_mode(sbi
);
555 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
556 return autofs_get_protover(argp
);
557 case AUTOFS_IOC_SETTIMEOUT
:
558 return autofs_get_set_timeout(sbi
, argp
);
559 case AUTOFS_IOC_EXPIRE
:
560 return autofs_expire_run(inode
->i_sb
, sbi
, filp
->f_path
.mnt
,