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/errno.h>
14 #include <linux/stat.h>
15 #include <linux/param.h>
18 static int autofs_root_readdir(struct file
*,void *,filldir_t
);
19 static struct dentry
*autofs_root_lookup(struct inode
*,struct dentry
*);
20 static int autofs_root_symlink(struct inode
*,struct dentry
*,const char *);
21 static int autofs_root_unlink(struct inode
*,struct dentry
*);
22 static int autofs_root_rmdir(struct inode
*,struct dentry
*);
23 static int autofs_root_mkdir(struct inode
*,struct dentry
*,int);
24 static int autofs_root_ioctl(struct inode
*, struct file
*,unsigned int,unsigned long);
26 static struct file_operations autofs_root_operations
= {
30 autofs_root_readdir
, /* readdir */
32 autofs_root_ioctl
, /* ioctl */
39 NULL
, /* check_media_change */
40 NULL
, /* revalidate */
44 struct inode_operations autofs_root_inode_operations
= {
45 &autofs_root_operations
, /* file operations */
47 autofs_root_lookup
, /* lookup */
49 autofs_root_unlink
, /* unlink */
50 autofs_root_symlink
, /* symlink */
51 autofs_root_mkdir
, /* mkdir */
52 autofs_root_rmdir
, /* rmdir */
56 NULL
, /* follow_link */
61 NULL
, /* permission */
65 static int autofs_root_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
67 struct autofs_dir_ent
*ent
= NULL
;
68 struct autofs_dirhash
*dirhash
;
69 struct autofs_sb_info
*sbi
;
70 struct inode
* inode
= filp
->f_dentry
->d_inode
;
73 sbi
= autofs_sbi(inode
->i_sb
);
74 dirhash
= &sbi
->dirhash
;
80 if (filldir(dirent
, ".", 1, nr
, inode
->i_ino
) < 0)
85 if (filldir(dirent
, "..", 2, nr
, inode
->i_ino
) < 0)
90 while ( onr
= nr
, ent
= autofs_hash_enum(dirhash
,&nr
,ent
) ) {
91 if ( !ent
->dentry
|| ent
->dentry
->d_mounts
!= ent
->dentry
) {
92 if (filldir(dirent
,ent
->name
,ent
->len
,onr
,ent
->ino
) < 0)
103 static int try_to_fill_dentry(struct dentry
*dentry
, struct super_block
*sb
, struct autofs_sb_info
*sbi
)
105 struct inode
* inode
;
106 struct autofs_dir_ent
*ent
;
109 if ( !(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)) ) {
111 if ( status
&& dentry
->d_inode
) {
112 if ( status
!= -ENOENT
)
113 printk("autofs warning: lookup failure on positive dentry, status = %d, name = %s\n", status
, dentry
->d_name
.name
);
114 return 0; /* Try to get the kernel to invalidate this dentry */
117 /* Turn this into a real negative dentry? */
118 if (status
== -ENOENT
) {
119 dentry
->d_time
= jiffies
+ AUTOFS_NEGATIVE_TIMEOUT
;
120 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
123 /* Return a negative dentry, but leave it "pending" */
126 status
= autofs_wait(sbi
, &dentry
->d_name
);
127 } while (!(ent
= autofs_hash_lookup(&sbi
->dirhash
, &dentry
->d_name
)) );
130 /* Abuse this field as a pointer to the directory entry, used to
131 find the expire list pointers */
132 dentry
->d_time
= (unsigned long) ent
;
134 if (!dentry
->d_inode
) {
135 inode
= iget(sb
, ent
->ino
);
137 /* Failed, but leave pending for next time */
140 dentry
->d_inode
= inode
;
143 /* If this is a directory that isn't a mount point, bitch at the
144 daemon and fix it in user space */
145 if ( S_ISDIR(dentry
->d_inode
->i_mode
) && dentry
->d_mounts
== dentry
) {
146 return !autofs_wait(sbi
, &dentry
->d_name
);
149 /* We don't update the usages for the autofs daemon itself, this
150 is necessary for recursive autofs mounts */
151 if ( !autofs_oz_mode(sbi
) ) {
152 autofs_update_usage(&sbi
->dirhash
,ent
);
155 dentry
->d_flags
&= ~DCACHE_AUTOFS_PENDING
;
161 * Revalidate is called on every cache lookup. Some of those
162 * cache lookups may actually happen while the dentry is not
163 * yet completely filled in, and revalidate has to delay such
166 static int autofs_revalidate(struct dentry
* dentry
, int flags
)
168 struct inode
* dir
= dentry
->d_parent
->d_inode
;
169 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
170 struct autofs_dir_ent
*ent
;
173 if ( dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
174 if (autofs_oz_mode(sbi
))
177 return try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
180 /* Negative dentry.. invalidate if "old" */
181 if (!dentry
->d_inode
)
182 return (dentry
->d_time
- jiffies
<= AUTOFS_NEGATIVE_TIMEOUT
);
184 /* Check for a non-mountpoint directory */
185 if ( S_ISDIR(dentry
->d_inode
->i_mode
) && dentry
->d_mounts
== dentry
) {
186 if (autofs_oz_mode(sbi
))
189 return try_to_fill_dentry(dentry
, dir
->i_sb
, sbi
);
192 /* Update the usage list */
193 if ( !autofs_oz_mode(sbi
) ) {
194 ent
= (struct autofs_dir_ent
*) dentry
->d_time
;
196 autofs_update_usage(&sbi
->dirhash
,ent
);
201 static struct dentry_operations autofs_dentry_operations
= {
202 autofs_revalidate
, /* d_revalidate */
204 NULL
, /* d_compare */
207 static struct dentry
*autofs_root_lookup(struct inode
*dir
, struct dentry
*dentry
)
209 struct autofs_sb_info
*sbi
;
212 DPRINTK(("autofs_root_lookup: name = "));
213 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
215 if (dentry
->d_name
.len
> NAME_MAX
)
216 return ERR_PTR(-ENOENT
);/* File name too long to exist */
218 sbi
= autofs_sbi(dir
->i_sb
);
220 oz_mode
= autofs_oz_mode(sbi
);
221 DPRINTK(("autofs_lookup: pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d\n",
222 current
->pid
, current
->pgrp
, sbi
->catatonic
, oz_mode
));
225 * Mark the dentry incomplete, but add it. This is needed so
226 * that the VFS layer knows about the dentry, and we can count
227 * on catching any lookups through the revalidate.
229 * Let all the hard work be done by the revalidate function that
230 * needs to be able to do this anyway..
232 * We need to do this before we release the directory semaphore.
234 dentry
->d_op
= &autofs_dentry_operations
;
235 dentry
->d_flags
|= DCACHE_AUTOFS_PENDING
;
239 autofs_revalidate(dentry
, 0);
243 * If we are still pending, check if we had to handle
244 * a signal. If so we can force a restart..
246 if (dentry
->d_flags
& DCACHE_AUTOFS_PENDING
) {
247 if (signal_pending(current
))
248 return ERR_PTR(-ERESTARTNOINTR
);
252 * If this dentry is unhashed, then we shouldn't honour this
253 * lookup even if the dentry is positive. Returning ENOENT here
254 * doesn't do the right thing for all system calls, but it should
255 * be OK for the operations we permit from an autofs.
257 if ( dentry
->d_inode
&& list_empty(&dentry
->d_hash
) )
258 return ERR_PTR(-ENOENT
);
263 static int autofs_root_symlink(struct inode
*dir
, struct dentry
*dentry
, const char *symname
)
265 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
266 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
267 struct autofs_dir_ent
*ent
;
270 struct autofs_symlink
*sl
;
272 DPRINTK(("autofs_root_symlink: %s <- ", symname
));
273 autofs_say(dentry
->d_name
.name
,dentry
->d_name
.len
);
275 if ( !autofs_oz_mode(sbi
) )
278 if ( dentry
->d_name
.len
> NAME_MAX
)
279 return -ENAMETOOLONG
;
281 if ( autofs_hash_lookup(dh
, &dentry
->d_name
) )
284 n
= find_first_zero_bit(sbi
->symlink_bitmap
,AUTOFS_MAX_SYMLINKS
);
285 if ( n
>= AUTOFS_MAX_SYMLINKS
)
288 set_bit(n
,sbi
->symlink_bitmap
);
289 sl
= &sbi
->symlink
[n
];
290 sl
->len
= strlen(symname
);
291 sl
->data
= kmalloc(slsize
= sl
->len
+1, GFP_KERNEL
);
293 clear_bit(n
,sbi
->symlink_bitmap
);
297 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
300 clear_bit(n
,sbi
->symlink_bitmap
);
304 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
308 clear_bit(n
,sbi
->symlink_bitmap
);
312 memcpy(sl
->data
,symname
,slsize
);
313 sl
->mtime
= CURRENT_TIME
;
315 ent
->ino
= AUTOFS_FIRST_SYMLINK
+ n
;
316 ent
->hash
= dentry
->d_name
.hash
;
317 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
318 ent
->dentry
= NULL
; /* We don't keep the dentry for symlinks */
320 autofs_hash_insert(dh
,ent
);
321 d_instantiate(dentry
, iget(dir
->i_sb
,ent
->ino
));
329 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
330 * that the file no longer exists. However, doing that means that the
331 * VFS layer can turn the dentry into a negative dentry, which we
332 * obviously do not want (we're dropping the entry not because it
333 * doesn't exist, but because it has timed out).
335 * Also see autofs_root_rmdir()..
337 static int autofs_root_unlink(struct inode
*dir
, struct dentry
*dentry
)
339 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
340 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
341 struct autofs_dir_ent
*ent
;
344 /* This allows root to remove symlinks */
345 if ( !autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) )
348 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
352 n
= ent
->ino
- AUTOFS_FIRST_SYMLINK
;
353 if ( n
>= AUTOFS_MAX_SYMLINKS
)
354 return -EISDIR
; /* It's a directory, dummy */
355 if ( !test_bit(n
,sbi
->symlink_bitmap
) )
356 return -EINVAL
; /* Nonexistent symlink? Shouldn't happen */
358 dentry
->d_time
= (unsigned long)(struct autofs_dirhash
*)NULL
;
359 autofs_hash_delete(ent
);
360 clear_bit(n
,sbi
->symlink_bitmap
);
361 kfree(sbi
->symlink
[n
].data
);
367 static int autofs_root_rmdir(struct inode
*dir
, struct dentry
*dentry
)
369 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
370 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
371 struct autofs_dir_ent
*ent
;
373 if ( !autofs_oz_mode(sbi
) )
376 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
380 if ( (unsigned int)ent
->ino
< AUTOFS_FIRST_DIR_INO
)
381 return -ENOTDIR
; /* Not a directory */
383 if ( ent
->dentry
!= dentry
) {
384 printk("autofs_rmdir: odentry != dentry for entry %s\n", dentry
->d_name
.name
);
387 dentry
->d_time
= (unsigned long)(struct autofs_dir_ent
*)NULL
;
388 autofs_hash_delete(ent
);
395 static int autofs_root_mkdir(struct inode
*dir
, struct dentry
*dentry
, int mode
)
397 struct autofs_sb_info
*sbi
= autofs_sbi(dir
->i_sb
);
398 struct autofs_dirhash
*dh
= &sbi
->dirhash
;
399 struct autofs_dir_ent
*ent
;
402 if ( !autofs_oz_mode(sbi
) )
405 if ( dentry
->d_name
.len
> NAME_MAX
)
406 return -ENAMETOOLONG
;
408 ent
= autofs_hash_lookup(dh
, &dentry
->d_name
);
412 if ( sbi
->next_dir_ino
< AUTOFS_FIRST_DIR_INO
) {
413 printk("autofs: Out of inode numbers -- what the heck did you do??\n");
416 ino
= sbi
->next_dir_ino
++;
418 ent
= kmalloc(sizeof(struct autofs_dir_ent
), GFP_KERNEL
);
422 ent
->name
= kmalloc(dentry
->d_name
.len
+1, GFP_KERNEL
);
428 ent
->hash
= dentry
->d_name
.hash
;
429 memcpy(ent
->name
, dentry
->d_name
.name
, 1+(ent
->len
= dentry
->d_name
.len
));
431 ent
->dentry
= dentry
;
432 autofs_hash_insert(dh
,ent
);
435 d_instantiate(dentry
, iget(dir
->i_sb
,ino
));
440 /* Get/set timeout ioctl() operation */
441 static inline int autofs_get_set_timeout(struct autofs_sb_info
*sbi
,
445 unsigned long ntimeout
;
447 if ( (rv
= get_user(ntimeout
, p
)) ||
448 (rv
= put_user(sbi
->exp_timeout
/HZ
, p
)) )
451 if ( ntimeout
> ULONG_MAX
/HZ
)
452 sbi
->exp_timeout
= 0;
454 sbi
->exp_timeout
= ntimeout
* HZ
;
459 /* Return protocol version */
460 static inline int autofs_get_protover(int *p
)
462 return put_user(AUTOFS_PROTO_VERSION
, p
);
465 /* Perform an expiry operation */
466 static inline int autofs_expire_run(struct super_block
*sb
,
467 struct autofs_sb_info
*sbi
,
468 struct autofs_packet_expire
*pkt_p
)
470 struct autofs_dir_ent
*ent
;
471 struct autofs_packet_expire pkt
;
473 memset(&pkt
,0,sizeof pkt
);
475 pkt
.hdr
.proto_version
= AUTOFS_PROTO_VERSION
;
476 pkt
.hdr
.type
= autofs_ptype_expire
;
478 if ( !sbi
->exp_timeout
||
479 !(ent
= autofs_expire(sb
,sbi
)) )
483 memcpy(pkt
.name
, ent
->name
, pkt
.len
);
484 pkt
.name
[pkt
.len
] = '\0';
486 if ( copy_to_user(pkt_p
, &pkt
, sizeof(struct autofs_packet_expire
)) )
493 * ioctl()'s on the root directory is the chief method for the daemon to
494 * generate kernel reactions
496 static int autofs_root_ioctl(struct inode
*inode
, struct file
*filp
,
497 unsigned int cmd
, unsigned long arg
)
499 struct autofs_sb_info
*sbi
= autofs_sbi(inode
->i_sb
);
501 DPRINTK(("autofs_ioctl: cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u\n",cmd
,arg
,sbi
,current
->pgrp
));
503 if ( _IOC_TYPE(cmd
) != _IOC_TYPE(AUTOFS_IOC_FIRST
) ||
504 _IOC_NR(cmd
) - _IOC_NR(AUTOFS_IOC_FIRST
) >= AUTOFS_IOC_COUNT
)
507 if ( !autofs_oz_mode(sbi
) && !capable(CAP_SYS_ADMIN
) )
511 case AUTOFS_IOC_READY
: /* Wait queue: go ahead and retry */
512 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,0);
513 case AUTOFS_IOC_FAIL
: /* Wait queue: fail with ENOENT */
514 return autofs_wait_release(sbi
,(autofs_wqt_t
)arg
,-ENOENT
);
515 case AUTOFS_IOC_CATATONIC
: /* Enter catatonic mode (daemon shutdown) */
516 autofs_catatonic_mode(sbi
);
518 case AUTOFS_IOC_PROTOVER
: /* Get protocol version */
519 return autofs_get_protover((int *)arg
);
520 case AUTOFS_IOC_SETTIMEOUT
:
521 return autofs_get_set_timeout(sbi
,(unsigned long *)arg
);
522 case AUTOFS_IOC_EXPIRE
:
523 return autofs_expire_run(inode
->i_sb
,sbi
,
524 (struct autofs_packet_expire
*)arg
);