1 /* -*- linux-c -*- --------------------------------------------------------- *
3 * drivers/staging/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/compat.h>
20 #include <linux/smp_lock.h>
23 static int autofs_root_readdir(struct file
*,void *,filldir_t
);
24 static struct dentry
*autofs_root_lookup(struct inode
*,struct dentry
*, struct nameidata
*);
25 static int autofs_root_symlink(struct inode
*,struct dentry
*,const char *);
26 static int autofs_root_unlink(struct inode
*,struct dentry
*);
27 static int autofs_root_rmdir(struct inode
*,struct dentry
*);
28 static int autofs_root_mkdir(struct inode
*,struct dentry
*,int);
29 static long autofs_root_ioctl(struct file
*,unsigned int,unsigned long);
31 static long autofs_root_compat_ioctl(struct file
*,unsigned int,unsigned long);
34 const struct file_operations autofs_root_operations
= {
35 .llseek
= generic_file_llseek
,
36 .read
= generic_read_dir
,
37 .readdir
= autofs_root_readdir
,
38 .unlocked_ioctl
= autofs_root_ioctl
,
40 .compat_ioctl
= autofs_root_compat_ioctl
,
44 const struct inode_operations autofs_root_inode_operations
= {
45 .lookup
= autofs_root_lookup
,
46 .unlink
= autofs_root_unlink
,
47 .symlink
= autofs_root_symlink
,
48 .mkdir
= autofs_root_mkdir
,
49 .rmdir
= autofs_root_rmdir
,
52 static int autofs_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
54 struct autofs_dir_ent
*ent
= NULL
;
55 struct autofs_dirhash
*dirhash
;
56 struct autofs_sb_info
*sbi
;
57 struct inode
* inode
= filp
->f_path
.dentry
->d_inode
;
62 sbi
= autofs_sbi(inode
->i_sb
);
63 dirhash
= &sbi
->dirhash
;
69 if (filldir(dirent
, ".", 1, nr
, inode
->i_ino
, DT_DIR
) < 0)
74 if (filldir(dirent
, "..", 2, nr
, inode
->i_ino
, DT_DIR
) < 0)
79 while (onr
= nr
, ent
= autofs_hash_enum(dirhash
,&nr
,ent
)) {
80 if (!ent
->dentry
|| d_mountpoint(ent
->dentry
)) {
81 if (filldir(dirent
,ent
->name
,ent
->len
,onr
,ent
->ino
,DT_UNKNOWN
) < 0)
94 static int try_to_fill_dentry(struct dentry
*dentry
, struct super_block
*sb
, struct autofs_sb_info
*sbi
)
97 struct autofs_dir_ent
*ent
;
100 if (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
))) {
102 if (status
&& dentry
->d_inode
) {
103 if (status
!= -ENOENT
)
104 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status
, dentry
->d_name
.name
);
105 return 0; /* Try to get the kernel to invalidate this dentry */
108 /* Turn this into a real negative dentry? */
109 if (status
== -ENOENT
) {
110 dentry
->d_time
= jiffies
+ AUTOFS_NEGATIVE_TIMEOUT
;
111 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
114 /* Return a negative dentry, but leave it "pending" */
117 status
= autofs_wait(sbi
, &dentry
->d_name
);
118 } while (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)));
121 /* Abuse this field as a pointer to the directory entry, used to
122 find the expire list pointers */
123 dentry
->d_time
= (unsigned long) ent
;
125 if (!dentry
->d_inode
) {
126 inode
= autofs_iget(sb
, ent
->ino
);
128 /* Failed, but leave pending for next time */
131 dentry
->d_inode
= inode
;
134 /* If this is a directory that isn't a mount point, bitch at the
135 daemon and fix it in user space */
136 if (S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
)) {
137 return !autofs_wait(sbi
, &dentry
->d_name
);
140 /* We don't update the usages for the autofs daemon itself, this
141 is necessary for recursive autofs mounts */
142 if (!autofs_oz_mode(sbi
)) {
143 autofs_update_usage(&sbi
->dirhash
,ent
);
146 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
152 * Revalidate is called on every cache lookup. Some of those
153 * cache lookups may actually happen while the dentry is not
154 * yet completely filled in, and revalidate has to delay such
157 static int autofs_revalidate(struct dentry
*dentry
, struct nameidata
*nd
)
160 struct autofs_sb_info
*sbi
;
161 struct autofs_dir_ent
*ent
;
164 if (nd
->flags
& LOOKUP_RCU
)
168 dir
= dentry
->d_parent
->d_inode
;
169 sbi
= autofs_sbi(dir
->i_sb
);
172 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
173 if (autofs_oz_mode(sbi
))
176 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
181 /* Negative dentry.. invalidate if "old" */
182 if (!dentry
->d_inode
) {
184 return (dentry
->d_time
- jiffies
<= AUTOFS_NEGATIVE_TIMEOUT
);
187 /* Check for a non-mountpoint directory */
188 if (S_ISDIR(dentry
->d_inode
->i_mode
) && !d_mountpoint(dentry
)) {
189 if (autofs_oz_mode(sbi
))
192 res
= try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
197 /* Update the usage list */
198 if (!autofs_oz_mode(sbi
)) {
199 ent
= (struct autofs_dir_ent
*) dentry
->d_time
;
201 autofs_update_usage(&sbi
->dirhash
,ent
);
207 static const struct dentry_operations autofs_dentry_operations
= {
208 .d_revalidate
= autofs_revalidate
,
211 static struct dentry
*autofs_root_lookup(struct inode
*dir
, struct dentry
*dentry
, struct nameidata
*nd
)
213 struct autofs_sb_info
*sbi
;
216 DPRINTK(("autofs_root_lookup: name = "));
218 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
220 if (dentry
->d_name
.len
> NAME_MAX
) {
222 return ERR_PTR(-ENAMETOOLONG
);/* File name too long to exist */
225 sbi
= autofs_sbi(dir
->i_sb
);
227 oz_mode
= autofs_oz_mode(sbi
);
228 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, "
229 "oz_mode = %d\n", task_pid_nr(current
),
230 task_pgrp_nr(current
), sbi
->catatonic
,
234 * Mark the dentry incomplete, but add it. This is needed so
235 * that the VFS layer knows about the dentry, and we can count
236 * on catching any lookups through the revalidate.
238 * Let all the hard work be done by the revalidate function that
239 * needs to be able to do this anyway..
241 * We need to do this before we release the directory semaphore.
243 d_set_d_op(dentry
, &autofs_dentry_operations
);
244 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
247 mutex_unlock(&dir
->i_mutex
);
248 autofs_revalidate(dentry
, nd
);
249 mutex_lock(&dir
->i_mutex
);
252 * If we are still pending, check if we had to handle
253 * a signal. If so we can force a restart..
255 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
256 /* See if we were interrupted */
257 if (signal_pending(current
)) {
258 sigset_t
*sigset
= ¤t
->pending
.signal
;
259 if (sigismember (sigset
, SIGKILL
) ||
260 sigismember (sigset
, SIGQUIT
) ||
261 sigismember (sigset
, SIGINT
)) {
263 return ERR_PTR(-ERESTARTNOINTR
);
270 * If this dentry is unhashed, then we shouldn't honour this
271 * lookup even if the dentry is positive. Returning ENOENT here
272 * doesn't do the right thing for all system calls, but it should
273 * be OK for the operations we permit from an autofs.
275 if (dentry
->d_inode
&& d_unhashed(dentry
))
276 return ERR_PTR(-ENOENT
);
281 static int autofs_root_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
283 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
284 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
285 struct autofs_dir_ent
*ent
;
288 struct autofs_symlink
*sl
;
291 DPRINTK(("autofs_root_symlink: %s <- ", symname
));
292 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
295 if (!autofs_oz_mode(sbi
)) {
300 if (autofs_hash_lookup(dh
, &dentry
->d_name
)) {
305 n
= find_first_zero_bit(sbi
->symlink_bitmap
,AUTOFS_MAX_SYMLINKS
);
306 if (n
>= AUTOFS_MAX_SYMLINKS
) {
311 set_bit(n
,sbi
->symlink_bitmap
);
312 sl
= &sbi
->symlink
[n
];
313 sl
->len
= strlen(symname
);
314 sl
->data
= kmalloc(slsize
= sl
->len
+1, GFP_KERNEL
);
316 clear_bit(n
,sbi
->symlink_bitmap
);
321 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
324 clear_bit(n
,sbi
->symlink_bitmap
);
329 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
333 clear_bit(n
,sbi
->symlink_bitmap
);
338 memcpy(sl
->data
,symname
,slsize
);
339 sl
->mtime
= get_seconds();
341 ent
->ino
= AUTOFS_FIRST_SYMLINK
+ n
;
342 ent
->hash
= dentry
->d_name
.hash
;
343 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
344 ent
->dentry
= NULL
; /* We don't keep the dentry for symlinks */
346 autofs_hash_insert(dh
,ent
);
348 inode
= autofs_iget(dir
->i_sb
, ent
->ino
);
350 return PTR_ERR(inode
);
352 d_instantiate(dentry
, inode
);
360 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
361 * that the file no longer exists. However, doing that means that the
362 * VFS layer can turn the dentry into a negative dentry, which we
363 * obviously do not want (we're dropping the entry not because it
364 * doesn't exist, but because it has timed out).
366 * Also see autofs_root_rmdir()..
368 static int autofs_root_unlink(struct inode
*dir
, struct dentry
*dentry
)
370 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
371 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
372 struct autofs_dir_ent
*ent
;
375 /* This allows root to remove symlinks */
377 if (!autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
)) {
382 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
388 n
= ent
->ino
- AUTOFS_FIRST_SYMLINK
;
389 if (n
>= AUTOFS_MAX_SYMLINKS
) {
391 return -EISDIR
; /* It's a directory, dummy */
393 if (!test_bit(n
,sbi
->symlink_bitmap
)) {
395 return -EINVAL
; /* Nonexistent symlink? Shouldn't happen */
398 dentry
->d_time
= (unsigned long)(struct autofs_dirhash
*)NULL
;
399 autofs_hash_delete(ent
);
400 clear_bit(n
,sbi
->symlink_bitmap
);
401 kfree(sbi
->symlink
[n
].data
);
408 static int autofs_root_rmdir(struct inode
*dir
, struct dentry
*dentry
)
410 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
411 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
412 struct autofs_dir_ent
*ent
;
415 if (!autofs_oz_mode(sbi
)) {
420 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
426 if ((unsigned int)ent
->ino
< AUTOFS_FIRST_DIR_INO
) {
428 return -ENOTDIR
; /* Not a directory */
431 if (ent
->dentry
!= dentry
) {
432 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry
->d_name
.name
);
435 dentry
->d_time
= (unsigned long)(struct autofs_dir_ent
*)NULL
;
436 autofs_hash_delete(ent
);
444 static int autofs_root_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
446 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
447 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
448 struct autofs_dir_ent
*ent
;
453 if (!autofs_oz_mode(sbi
)) {
458 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
464 if (sbi
->next_dir_ino
< AUTOFS_FIRST_DIR_INO
) {
465 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
469 ino
= sbi
->next_dir_ino
++;
471 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
477 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
484 ent
->hash
= dentry
->d_name
.hash
;
485 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
487 ent
->dentry
= dentry
;
488 autofs_hash_insert(dh
,ent
);
492 inode
= autofs_iget(dir
->i_sb
, ino
);
495 return PTR_ERR(inode
);
498 d_instantiate(dentry
, inode
);
504 /* Get/set timeout ioctl() operation */
506 static inline int autofs_compat_get_set_timeout(struct autofs_sb_info
*sbi
,
507 unsigned int __user
*p
)
509 unsigned long ntimeout
;
511 if (get_user(ntimeout
, p
) ||
512 put_user(sbi
->exp_timeout
/ HZ
, p
))
515 if (ntimeout
> UINT_MAX
/HZ
)
516 sbi
->exp_timeout
= 0;
518 sbi
->exp_timeout
= ntimeout
* HZ
;
524 static inline int autofs_get_set_timeout(struct autofs_sb_info
*sbi
,
525 unsigned long __user
*p
)
527 unsigned long ntimeout
;
529 if (get_user(ntimeout
, p
) ||
530 put_user(sbi
->exp_timeout
/ HZ
, p
))
533 if (ntimeout
> ULONG_MAX
/HZ
)
534 sbi
->exp_timeout
= 0;
536 sbi
->exp_timeout
= ntimeout
* HZ
;
541 /* Return protocol version */
542 static inline int autofs_get_protover(int __user
*p
)
544 return put_user(AUTOFS_PROTO_VERSION
, p
);
547 /* Perform an expiry operation */
548 static inline int autofs_expire_run(struct super_block
*sb
,
549 struct autofs_sb_info
*sbi
,
550 struct vfsmount
*mnt
,
551 struct autofs_packet_expire __user
*pkt_p
)
553 struct autofs_dir_ent
*ent
;
554 struct autofs_packet_expire pkt
;
556 memset(&pkt
,0,sizeof pkt
);
558 pkt
.hdr
.proto_version
= AUTOFS_PROTO_VERSION
;
559 pkt
.hdr
.type
= autofs_ptype_expire
;
561 if (!sbi
->exp_timeout
|| !(ent
= autofs_expire(sb
,sbi
,mnt
)))
565 memcpy(pkt
.name
, ent
->name
, pkt
.len
);
566 pkt
.name
[pkt
.len
] = '\0';
568 if (copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)))
575 * ioctl()'s on the root directory is the chief method for the daemon to
576 * generate kernel reactions
578 static int autofs_do_root_ioctl(struct inode
*inode
, struct file
*filp
,
579 unsigned int cmd
, unsigned long arg
)
581 struct autofs_sb_info
*sbi
= autofs_sbi(inode
->i_sb
);
582 void __user
*argp
= (void __user
*)arg
;
584 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd
,arg
,sbi
,task_pgrp_nr(current
)));
586 if (_IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
587 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
590 if (!autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
))
594 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
595 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
596 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
597 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
598 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
599 autofs_catatonic_mode(sbi
);
601 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
602 return autofs_get_protover(argp
);
604 case AUTOFS_IOC_SETTIMEOUT32
:
605 return autofs_compat_get_set_timeout(sbi
, argp
);
607 case AUTOFS_IOC_SETTIMEOUT
:
608 return autofs_get_set_timeout(sbi
, argp
);
609 case AUTOFS_IOC_EXPIRE
:
610 return autofs_expire_run(inode
->i_sb
, sbi
, filp
->f_path
.mnt
,
618 static long autofs_root_ioctl(struct file
*filp
,
619 unsigned int cmd
, unsigned long arg
)
624 ret
= autofs_do_root_ioctl(filp
->f_path
.dentry
->d_inode
,
632 static long autofs_root_compat_ioctl(struct file
*filp
,
633 unsigned int cmd
, unsigned long arg
)
635 struct inode
*inode
= filp
->f_path
.dentry
->d_inode
;
639 if (cmd
== AUTOFS_IOC_READY
|| cmd
== AUTOFS_IOC_FAIL
)
640 ret
= autofs_do_root_ioctl(inode
, filp
, cmd
, arg
);
642 ret
= autofs_do_root_ioctl(inode
, filp
, cmd
,
643 (unsigned long)compat_ptr(arg
));