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/slab.h>
17 #include <linux/param.h>
18 #include <linux/time.h>
19 #include <linux/smp_lock.h>
22 static int autofs_root_readdir(struct file
*,void *,filldir_t
);
23 static struct dentry
*autofs_root_lookup(struct inode
*,struct dentry
*, struct nameidata
*);
24 static int autofs_root_symlink(struct inode
*,struct dentry
*,const char *);
25 static int autofs_root_unlink(struct inode
*,struct dentry
*);
26 static int autofs_root_rmdir(struct inode
*,struct dentry
*);
27 static int autofs_root_mkdir(struct inode
*,struct dentry
*,int);
28 static int autofs_root_ioctl(struct inode
*, struct file
*,unsigned int,unsigned long);
30 const struct file_operations autofs_root_operations
= {
31 .llseek
= generic_file_llseek
,
32 .read
= generic_read_dir
,
33 .readdir
= autofs_root_readdir
,
34 .ioctl
= autofs_root_ioctl
,
37 const struct inode_operations autofs_root_inode_operations
= {
38 .lookup
= autofs_root_lookup
,
39 .unlink
= autofs_root_unlink
,
40 .symlink
= autofs_root_symlink
,
41 .mkdir
= autofs_root_mkdir
,
42 .rmdir
= autofs_root_rmdir
,
45 static int autofs_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
47 struct autofs_dir_ent
*ent
= NULL
;
48 struct autofs_dirhash
*dirhash
;
49 struct autofs_sb_info
*sbi
;
50 struct inode
* inode
= filp
->f_path
.dentry
->d_inode
;
55 sbi
= autofs_sbi(inode
->i_sb
);
56 dirhash
= &sbi
->dirhash
;
62 if (filldir(dirent
, ".", 1, nr
, inode
->i_ino
, DT_DIR
) < 0)
67 if (filldir(dirent
, "..", 2, nr
, inode
->i_ino
, DT_DIR
) < 0)
72 while (onr
= nr
, ent
= autofs_hash_enum(dirhash
,&nr
,ent
)) {
73 if (!ent
->dentry
|| d_mountpoint(ent
->dentry
)) {
74 if (filldir(dirent
,ent
->name
,ent
->len
,onr
,ent
->ino
,DT_UNKNOWN
) < 0)
87 static int try_to_fill_dentry(struct dentry
*dentry
, struct super_block
*sb
, struct autofs_sb_info
*sbi
)
90 struct autofs_dir_ent
*ent
;
93 if (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
))) {
95 if (status
&& dentry
->d_inode
) {
96 if (status
!= -ENOENT
)
97 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status
, dentry
->d_name
.name
);
98 return 0; /* Try to get the kernel to invalidate this dentry */
101 /* Turn this into a real negative dentry? */
102 if (status
== -ENOENT
) {
103 dentry
->d_time
= jiffies
+ AUTOFS_NEGATIVE_TIMEOUT
;
104 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
107 /* Return a negative dentry, but leave it "pending" */
110 status
= autofs_wait(sbi
, &dentry
->d_name
);
111 } while (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)));
114 /* Abuse this field as a pointer to the directory entry, used to
115 find the expire list pointers */
116 dentry
->d_time
= (unsigned long) ent
;
118 if (!dentry
->d_inode
) {
119 inode
= autofs_iget(sb
, ent
->ino
);
121 /* Failed, but leave pending for next time */
124 dentry
->d_inode
= inode
;
127 /* If this is a directory that isn't a mount point, bitch at the
128 daemon and fix it in user space */
129 if (S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
)) {
130 return !autofs_wait(sbi
, &dentry
->d_name
);
133 /* We don't update the usages for the autofs daemon itself, this
134 is necessary for recursive autofs mounts */
135 if (!autofs_oz_mode(sbi
)) {
136 autofs_update_usage(&sbi
->dirhash
,ent
);
139 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
145 * Revalidate is called on every cache lookup. Some of those
146 * cache lookups may actually happen while the dentry is not
147 * yet completely filled in, and revalidate has to delay such
150 static int autofs_revalidate(struct dentry
* dentry
, struct nameidata
*nd
)
153 struct autofs_sb_info
*sbi
;
154 struct autofs_dir_ent
*ent
;
158 dir
= dentry
->d_parent
->d_inode
;
159 sbi
= autofs_sbi(dir
->i_sb
);
162 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
163 if (autofs_oz_mode(sbi
))
166 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
171 /* Negative dentry.. invalidate if "old" */
172 if (!dentry
->d_inode
) {
174 return (dentry
->d_time
- jiffies
<= AUTOFS_NEGATIVE_TIMEOUT
);
177 /* Check for a non-mountpoint directory */
178 if (S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
)) {
179 if (autofs_oz_mode(sbi
))
182 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
187 /* Update the usage list */
188 if (!autofs_oz_mode(sbi
)) {
189 ent
= (struct autofs_dir_ent
*) dentry
->d_time
;
191 autofs_update_usage(&sbi
->dirhash
,ent
);
197 static const struct dentry_operations autofs_dentry_operations
= {
198 .d_revalidate
= autofs_revalidate
,
201 static struct dentry
*autofs_root_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
203 struct autofs_sb_info
*sbi
;
206 DPRINTK(("autofs_root_lookup: name = "));
208 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
210 if (dentry
->d_name
.len
> NAME_MAX
) {
212 return ERR_PTR(-ENAMETOOLONG
);/* File name too long to exist */
215 sbi
= autofs_sbi(dir
->i_sb
);
217 oz_mode
= autofs_oz_mode(sbi
);
218 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
219 "oz_mode = %d\n", task_pid_nr(current
),
220 task_pgrp_nr(current
), sbi
->catatonic
,
224 * Mark the dentry incomplete, but add it. This is needed so
225 * that the VFS layer knows about the dentry, and we can count
226 * on catching any lookups through the revalidate.
228 * Let all the hard work be done by the revalidate function that
229 * needs to be able to do this anyway..
231 * We need to do this before we release the directory semaphore.
233 dentry
->d_op
= &autofs_dentry_operations
;
234 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
237 mutex_unlock(&dir
->i_mutex
);
238 autofs_revalidate(dentry
, nd
);
239 mutex_lock(&dir
->i_mutex
);
242 * If we are still pending, check if we had to handle
243 * a signal. If so we can force a restart..
245 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
246 /* See if we were interrupted */
247 if (signal_pending(current
)) {
248 sigset_t
*sigset
= ¤t
->pending
.signal
;
249 if (sigismember (sigset
, SIGKILL
) ||
250 sigismember (sigset
, SIGQUIT
) ||
251 sigismember (sigset
, SIGINT
)) {
253 return ERR_PTR(-ERESTARTNOINTR
);
260 * If this dentry is unhashed, then we shouldn't honour this
261 * lookup even if the dentry is positive. Returning ENOENT here
262 * doesn't do the right thing for all system calls, but it should
263 * be OK for the operations we permit from an autofs.
265 if (dentry
->d_inode
&& d_unhashed(dentry
))
266 return ERR_PTR(-ENOENT
);
271 static int autofs_root_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
273 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
274 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
275 struct autofs_dir_ent
*ent
;
278 struct autofs_symlink
*sl
;
281 DPRINTK(("autofs_root_symlink: %s <- ", symname
));
282 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
285 if (!autofs_oz_mode(sbi
)) {
290 if (autofs_hash_lookup(dh
, &dentry
->d_name
)) {
295 n
= find_first_zero_bit(sbi
->symlink_bitmap
,AUTOFS_MAX_SYMLINKS
);
296 if (n
>= AUTOFS_MAX_SYMLINKS
) {
301 set_bit(n
,sbi
->symlink_bitmap
);
302 sl
= &sbi
->symlink
[n
];
303 sl
->len
= strlen(symname
);
304 sl
->data
= kmalloc(slsize
= sl
->len
+1, GFP_KERNEL
);
306 clear_bit(n
,sbi
->symlink_bitmap
);
311 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
314 clear_bit(n
,sbi
->symlink_bitmap
);
319 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
323 clear_bit(n
,sbi
->symlink_bitmap
);
328 memcpy(sl
->data
,symname
,slsize
);
329 sl
->mtime
= get_seconds();
331 ent
->ino
= AUTOFS_FIRST_SYMLINK
+ n
;
332 ent
->hash
= dentry
->d_name
.hash
;
333 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
334 ent
->dentry
= NULL
; /* We don't keep the dentry for symlinks */
336 autofs_hash_insert(dh
,ent
);
338 inode
= autofs_iget(dir
->i_sb
, ent
->ino
);
340 return PTR_ERR(inode
);
342 d_instantiate(dentry
, inode
);
350 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
351 * that the file no longer exists. However, doing that means that the
352 * VFS layer can turn the dentry into a negative dentry, which we
353 * obviously do not want (we're dropping the entry not because it
354 * doesn't exist, but because it has timed out).
356 * Also see autofs_root_rmdir()..
358 static int autofs_root_unlink(struct inode
*dir
, struct dentry
*dentry
)
360 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
361 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
362 struct autofs_dir_ent
*ent
;
365 /* This allows root to remove symlinks */
367 if (!autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
)) {
372 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
378 n
= ent
->ino
- AUTOFS_FIRST_SYMLINK
;
379 if (n
>= AUTOFS_MAX_SYMLINKS
) {
381 return -EISDIR
; /* It's a directory, dummy */
383 if (!test_bit(n
,sbi
->symlink_bitmap
)) {
385 return -EINVAL
; /* Nonexistent symlink? Shouldn't happen */
388 dentry
->d_time
= (unsigned long)(struct autofs_dirhash
*)NULL
;
389 autofs_hash_delete(ent
);
390 clear_bit(n
,sbi
->symlink_bitmap
);
391 kfree(sbi
->symlink
[n
].data
);
398 static int autofs_root_rmdir(struct inode
*dir
, struct dentry
*dentry
)
400 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
401 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
402 struct autofs_dir_ent
*ent
;
405 if (!autofs_oz_mode(sbi
)) {
410 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
416 if ((unsigned int)ent
->ino
< AUTOFS_FIRST_DIR_INO
) {
418 return -ENOTDIR
; /* Not a directory */
421 if (ent
->dentry
!= dentry
) {
422 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry
->d_name
.name
);
425 dentry
->d_time
= (unsigned long)(struct autofs_dir_ent
*)NULL
;
426 autofs_hash_delete(ent
);
434 static int autofs_root_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
436 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
437 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
438 struct autofs_dir_ent
*ent
;
443 if (!autofs_oz_mode(sbi
)) {
448 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
454 if (sbi
->next_dir_ino
< AUTOFS_FIRST_DIR_INO
) {
455 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
459 ino
= sbi
->next_dir_ino
++;
461 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
467 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
474 ent
->hash
= dentry
->d_name
.hash
;
475 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
477 ent
->dentry
= dentry
;
478 autofs_hash_insert(dh
,ent
);
482 inode
= autofs_iget(dir
->i_sb
, ino
);
485 return PTR_ERR(inode
);
488 d_instantiate(dentry
, inode
);
494 /* Get/set timeout ioctl() operation */
495 static inline int autofs_get_set_timeout(struct autofs_sb_info
*sbi
,
496 unsigned long __user
*p
)
498 unsigned long ntimeout
;
500 if (get_user(ntimeout
, p
) ||
501 put_user(sbi
->exp_timeout
/ HZ
, p
))
504 if (ntimeout
> ULONG_MAX
/HZ
)
505 sbi
->exp_timeout
= 0;
507 sbi
->exp_timeout
= ntimeout
* HZ
;
512 /* Return protocol version */
513 static inline int autofs_get_protover(int __user
*p
)
515 return put_user(AUTOFS_PROTO_VERSION
, p
);
518 /* Perform an expiry operation */
519 static inline int autofs_expire_run(struct super_block
*sb
,
520 struct autofs_sb_info
*sbi
,
521 struct vfsmount
*mnt
,
522 struct autofs_packet_expire __user
*pkt_p
)
524 struct autofs_dir_ent
*ent
;
525 struct autofs_packet_expire pkt
;
527 memset(&pkt
,0,sizeof pkt
);
529 pkt
.hdr
.proto_version
= AUTOFS_PROTO_VERSION
;
530 pkt
.hdr
.type
= autofs_ptype_expire
;
532 if (!sbi
->exp_timeout
|| !(ent
= autofs_expire(sb
,sbi
,mnt
)))
536 memcpy(pkt
.name
, ent
->name
, pkt
.len
);
537 pkt
.name
[pkt
.len
] = '\0';
539 if (copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)))
546 * ioctl()'s on the root directory is the chief method for the daemon to
547 * generate kernel reactions
549 static int autofs_root_ioctl(struct inode
*inode
, struct file
*filp
,
550 unsigned int cmd
, unsigned long arg
)
552 struct autofs_sb_info
*sbi
= autofs_sbi(inode
->i_sb
);
553 void __user
*argp
= (void __user
*)arg
;
555 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd
,arg
,sbi
,task_pgrp_nr(current
)));
557 if (_IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
558 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
561 if (!autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
565 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
566 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
567 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
568 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
569 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
570 autofs_catatonic_mode(sbi
);
572 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
573 return autofs_get_protover(argp
);
574 case AUTOFS_IOC_SETTIMEOUT
:
575 return autofs_get_set_timeout(sbi
, argp
);
576 case AUTOFS_IOC_EXPIRE
:
577 return autofs_expire_run(inode
->i_sb
, sbi
, filp
->f_path
.mnt
,