Do not disable interrupts when reading min_free_kbytes
[linux-2.6/mini2440.git] / fs / autofs4 / root.c
blobd0e9b3a3905d6e125a8d2fa2025251fbeab496ee
1 /* -*- c -*- --------------------------------------------------------------- *
3 * linux/fs/autofs/root.c
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2006 Ian Kent <raven@themaw.net>
9 * This file is part of the Linux kernel and is made available under
10 * the terms of the GNU General Public License, version 2, or at your
11 * option, any later version, incorporated herein by reference.
13 * ------------------------------------------------------------------------- */
15 #include <linux/capability.h>
16 #include <linux/errno.h>
17 #include <linux/stat.h>
18 #include <linux/param.h>
19 #include <linux/time.h>
20 #include <linux/smp_lock.h>
21 #include "autofs_i.h"
23 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
24 static int autofs4_dir_unlink(struct inode *,struct dentry *);
25 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
26 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
27 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
28 static int autofs4_dir_open(struct inode *inode, struct file *file);
29 static int autofs4_dir_close(struct inode *inode, struct file *file);
30 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
32 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
33 static void *autofs4_follow_link(struct dentry *, struct nameidata *);
35 const struct file_operations autofs4_root_operations = {
36 .open = dcache_dir_open,
37 .release = dcache_dir_close,
38 .read = generic_read_dir,
39 .readdir = autofs4_root_readdir,
40 .ioctl = autofs4_root_ioctl,
43 const struct file_operations autofs4_dir_operations = {
44 .open = autofs4_dir_open,
45 .release = autofs4_dir_close,
46 .read = generic_read_dir,
47 .readdir = autofs4_dir_readdir,
50 const struct inode_operations autofs4_indirect_root_inode_operations = {
51 .lookup = autofs4_lookup,
52 .unlink = autofs4_dir_unlink,
53 .symlink = autofs4_dir_symlink,
54 .mkdir = autofs4_dir_mkdir,
55 .rmdir = autofs4_dir_rmdir,
58 const struct inode_operations autofs4_direct_root_inode_operations = {
59 .lookup = autofs4_lookup,
60 .unlink = autofs4_dir_unlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
63 .follow_link = autofs4_follow_link,
66 const struct inode_operations autofs4_dir_inode_operations = {
67 .lookup = autofs4_lookup,
68 .unlink = autofs4_dir_unlink,
69 .symlink = autofs4_dir_symlink,
70 .mkdir = autofs4_dir_mkdir,
71 .rmdir = autofs4_dir_rmdir,
74 static int autofs4_root_readdir(struct file *file, void *dirent,
75 filldir_t filldir)
77 struct autofs_sb_info *sbi = autofs4_sbi(file->f_path.dentry->d_sb);
78 int oz_mode = autofs4_oz_mode(sbi);
80 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
83 * Don't set reghost flag if:
84 * 1) f_pos is larger than zero -- we've already been here.
85 * 2) we haven't even enabled reghosting in the 1st place.
86 * 3) this is the daemon doing a readdir
88 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
89 sbi->needs_reghost = 1;
91 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
93 return dcache_readdir(file, dirent, filldir);
96 static int autofs4_dir_open(struct inode *inode, struct file *file)
98 struct dentry *dentry = file->f_path.dentry;
99 struct vfsmount *mnt = file->f_path.mnt;
100 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
101 struct dentry *cursor;
102 int status;
104 status = dcache_dir_open(inode, file);
105 if (status)
106 goto out;
108 cursor = file->private_data;
109 cursor->d_fsdata = NULL;
111 DPRINTK("file=%p dentry=%p %.*s",
112 file, dentry, dentry->d_name.len, dentry->d_name.name);
114 if (autofs4_oz_mode(sbi))
115 goto out;
117 if (autofs4_ispending(dentry)) {
118 DPRINTK("dentry busy");
119 dcache_dir_close(inode, file);
120 status = -EBUSY;
121 goto out;
124 status = -ENOENT;
125 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
126 struct nameidata nd;
127 int empty, ret;
129 /* In case there are stale directory dentrys from a failed mount */
130 spin_lock(&dcache_lock);
131 empty = list_empty(&dentry->d_subdirs);
132 spin_unlock(&dcache_lock);
134 if (!empty)
135 d_invalidate(dentry);
137 nd.flags = LOOKUP_DIRECTORY;
138 ret = (dentry->d_op->d_revalidate)(dentry, &nd);
140 if (ret <= 0) {
141 if (ret < 0)
142 status = ret;
143 dcache_dir_close(inode, file);
144 goto out;
148 if (d_mountpoint(dentry)) {
149 struct file *fp = NULL;
150 struct vfsmount *fp_mnt = mntget(mnt);
151 struct dentry *fp_dentry = dget(dentry);
153 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
154 dput(fp_dentry);
155 mntput(fp_mnt);
156 dcache_dir_close(inode, file);
157 goto out;
160 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
161 status = PTR_ERR(fp);
162 if (IS_ERR(fp)) {
163 dcache_dir_close(inode, file);
164 goto out;
166 cursor->d_fsdata = fp;
168 return 0;
169 out:
170 return status;
173 static int autofs4_dir_close(struct inode *inode, struct file *file)
175 struct dentry *dentry = file->f_path.dentry;
176 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
177 struct dentry *cursor = file->private_data;
178 int status = 0;
180 DPRINTK("file=%p dentry=%p %.*s",
181 file, dentry, dentry->d_name.len, dentry->d_name.name);
183 if (autofs4_oz_mode(sbi))
184 goto out;
186 if (autofs4_ispending(dentry)) {
187 DPRINTK("dentry busy");
188 status = -EBUSY;
189 goto out;
192 if (d_mountpoint(dentry)) {
193 struct file *fp = cursor->d_fsdata;
194 if (!fp) {
195 status = -ENOENT;
196 goto out;
198 filp_close(fp, current->files);
200 out:
201 dcache_dir_close(inode, file);
202 return status;
205 static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
207 struct dentry *dentry = file->f_path.dentry;
208 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
209 struct dentry *cursor = file->private_data;
210 int status;
212 DPRINTK("file=%p dentry=%p %.*s",
213 file, dentry, dentry->d_name.len, dentry->d_name.name);
215 if (autofs4_oz_mode(sbi))
216 goto out;
218 if (autofs4_ispending(dentry)) {
219 DPRINTK("dentry busy");
220 return -EBUSY;
223 if (d_mountpoint(dentry)) {
224 struct file *fp = cursor->d_fsdata;
226 if (!fp)
227 return -ENOENT;
229 if (!fp->f_op || !fp->f_op->readdir)
230 goto out;
232 status = vfs_readdir(fp, filldir, dirent);
233 file->f_pos = fp->f_pos;
234 if (status)
235 autofs4_copy_atime(file, fp);
236 return status;
238 out:
239 return dcache_readdir(file, dirent, filldir);
242 static int try_to_fill_dentry(struct dentry *dentry, int flags)
244 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
245 struct autofs_info *ino = autofs4_dentry_ino(dentry);
246 int status = 0;
248 /* Block on any pending expiry here; invalidate the dentry
249 when expiration is done to trigger mount request with a new
250 dentry */
251 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
252 DPRINTK("waiting for expire %p name=%.*s",
253 dentry, dentry->d_name.len, dentry->d_name.name);
255 status = autofs4_wait(sbi, dentry, NFY_NONE);
257 DPRINTK("expire done status=%d", status);
260 * If the directory still exists the mount request must
261 * continue otherwise it can't be followed at the right
262 * time during the walk.
264 status = d_invalidate(dentry);
265 if (status != -EBUSY)
266 return -EAGAIN;
269 DPRINTK("dentry=%p %.*s ino=%p",
270 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
273 * Wait for a pending mount, triggering one if there
274 * isn't one already
276 if (dentry->d_inode == NULL) {
277 DPRINTK("waiting for mount name=%.*s",
278 dentry->d_name.len, dentry->d_name.name);
280 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
282 DPRINTK("mount done status=%d", status);
284 /* Turn this into a real negative dentry? */
285 if (status == -ENOENT) {
286 spin_lock(&dentry->d_lock);
287 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
288 spin_unlock(&dentry->d_lock);
289 return status;
290 } else if (status) {
291 /* Return a negative dentry, but leave it "pending" */
292 return status;
294 /* Trigger mount for path component or follow link */
295 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
296 current->link_count) {
297 DPRINTK("waiting for mount name=%.*s",
298 dentry->d_name.len, dentry->d_name.name);
300 spin_lock(&dentry->d_lock);
301 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
302 spin_unlock(&dentry->d_lock);
303 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
305 DPRINTK("mount done status=%d", status);
307 if (status) {
308 spin_lock(&dentry->d_lock);
309 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
310 spin_unlock(&dentry->d_lock);
311 return status;
315 /* Initialize expiry counter after successful mount */
316 if (ino)
317 ino->last_used = jiffies;
319 spin_lock(&dentry->d_lock);
320 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
321 spin_unlock(&dentry->d_lock);
322 return status;
325 /* For autofs direct mounts the follow link triggers the mount */
326 static void *autofs4_follow_link(struct dentry *dentry, struct nameidata *nd)
328 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
329 struct autofs_info *ino = autofs4_dentry_ino(dentry);
330 int oz_mode = autofs4_oz_mode(sbi);
331 unsigned int lookup_type;
332 int status;
334 DPRINTK("dentry=%p %.*s oz_mode=%d nd->flags=%d",
335 dentry, dentry->d_name.len, dentry->d_name.name, oz_mode,
336 nd->flags);
338 /* If it's our master or we shouldn't trigger a mount we're done */
339 lookup_type = nd->flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY);
340 if (oz_mode || !lookup_type)
341 goto done;
343 /* If an expire request is pending wait for it. */
344 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
345 DPRINTK("waiting for active request %p name=%.*s",
346 dentry, dentry->d_name.len, dentry->d_name.name);
348 status = autofs4_wait(sbi, dentry, NFY_NONE);
350 DPRINTK("request done status=%d", status);
354 * If the dentry contains directories then it is an
355 * autofs multi-mount with no root mount offset. So
356 * don't try to mount it again.
358 spin_lock(&dcache_lock);
359 if (!d_mountpoint(dentry) && __simple_empty(dentry)) {
360 spin_unlock(&dcache_lock);
362 status = try_to_fill_dentry(dentry, 0);
363 if (status)
364 goto out_error;
367 * The mount succeeded but if there is no root mount
368 * it must be an autofs multi-mount with no root offset
369 * so we don't need to follow the mount.
371 if (d_mountpoint(dentry)) {
372 if (!autofs4_follow_mount(&nd->mnt, &nd->dentry)) {
373 status = -ENOENT;
374 goto out_error;
378 goto done;
380 spin_unlock(&dcache_lock);
382 done:
383 return NULL;
385 out_error:
386 path_release(nd);
387 return ERR_PTR(status);
391 * Revalidate is called on every cache lookup. Some of those
392 * cache lookups may actually happen while the dentry is not
393 * yet completely filled in, and revalidate has to delay such
394 * lookups..
396 static int autofs4_revalidate(struct dentry *dentry, struct nameidata *nd)
398 struct inode *dir = dentry->d_parent->d_inode;
399 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
400 int oz_mode = autofs4_oz_mode(sbi);
401 int flags = nd ? nd->flags : 0;
402 int status = 1;
404 /* Pending dentry */
405 if (autofs4_ispending(dentry)) {
406 /* The daemon never causes a mount to trigger */
407 if (oz_mode)
408 return 1;
411 * A zero status is success otherwise we have a
412 * negative error code.
414 status = try_to_fill_dentry(dentry, flags);
415 if (status == 0)
416 return 1;
419 * A status of EAGAIN here means that the dentry has gone
420 * away while waiting for an expire to complete. If we are
421 * racing with expire lookup will wait for it so this must
422 * be a revalidate and we need to send it to lookup.
424 if (status == -EAGAIN)
425 return 0;
427 return status;
430 /* Negative dentry.. invalidate if "old" */
431 if (dentry->d_inode == NULL)
432 return 0;
434 /* Check for a non-mountpoint directory with no contents */
435 spin_lock(&dcache_lock);
436 if (S_ISDIR(dentry->d_inode->i_mode) &&
437 !d_mountpoint(dentry) &&
438 __simple_empty(dentry)) {
439 DPRINTK("dentry=%p %.*s, emptydir",
440 dentry, dentry->d_name.len, dentry->d_name.name);
441 spin_unlock(&dcache_lock);
442 /* The daemon never causes a mount to trigger */
443 if (oz_mode)
444 return 1;
447 * A zero status is success otherwise we have a
448 * negative error code.
450 status = try_to_fill_dentry(dentry, flags);
451 if (status == 0)
452 return 1;
454 return status;
456 spin_unlock(&dcache_lock);
458 return 1;
461 void autofs4_dentry_release(struct dentry *de)
463 struct autofs_info *inf;
465 DPRINTK("releasing %p", de);
467 inf = autofs4_dentry_ino(de);
468 de->d_fsdata = NULL;
470 if (inf) {
471 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
473 if (sbi) {
474 spin_lock(&sbi->rehash_lock);
475 if (!list_empty(&inf->rehash))
476 list_del(&inf->rehash);
477 spin_unlock(&sbi->rehash_lock);
480 inf->dentry = NULL;
481 inf->inode = NULL;
483 autofs4_free_ino(inf);
487 /* For dentries of directories in the root dir */
488 static struct dentry_operations autofs4_root_dentry_operations = {
489 .d_revalidate = autofs4_revalidate,
490 .d_release = autofs4_dentry_release,
493 /* For other dentries */
494 static struct dentry_operations autofs4_dentry_operations = {
495 .d_revalidate = autofs4_revalidate,
496 .d_release = autofs4_dentry_release,
499 static struct dentry *autofs4_lookup_unhashed(struct autofs_sb_info *sbi, struct dentry *parent, struct qstr *name)
501 unsigned int len = name->len;
502 unsigned int hash = name->hash;
503 const unsigned char *str = name->name;
504 struct list_head *p, *head;
506 spin_lock(&dcache_lock);
507 spin_lock(&sbi->rehash_lock);
508 head = &sbi->rehash_list;
509 list_for_each(p, head) {
510 struct autofs_info *ino;
511 struct dentry *dentry;
512 struct qstr *qstr;
514 ino = list_entry(p, struct autofs_info, rehash);
515 dentry = ino->dentry;
517 spin_lock(&dentry->d_lock);
519 /* Bad luck, we've already been dentry_iput */
520 if (!dentry->d_inode)
521 goto next;
523 qstr = &dentry->d_name;
525 if (dentry->d_name.hash != hash)
526 goto next;
527 if (dentry->d_parent != parent)
528 goto next;
530 if (qstr->len != len)
531 goto next;
532 if (memcmp(qstr->name, str, len))
533 goto next;
535 if (d_unhashed(dentry)) {
536 struct autofs_info *ino = autofs4_dentry_ino(dentry);
537 struct inode *inode = dentry->d_inode;
539 list_del_init(&ino->rehash);
540 dget(dentry);
542 * Make the rehashed dentry negative so the VFS
543 * behaves as it should.
545 if (inode) {
546 dentry->d_inode = NULL;
547 list_del_init(&dentry->d_alias);
548 spin_unlock(&dentry->d_lock);
549 spin_unlock(&sbi->rehash_lock);
550 spin_unlock(&dcache_lock);
551 iput(inode);
552 return dentry;
554 spin_unlock(&dentry->d_lock);
555 spin_unlock(&sbi->rehash_lock);
556 spin_unlock(&dcache_lock);
557 return dentry;
559 next:
560 spin_unlock(&dentry->d_lock);
562 spin_unlock(&sbi->rehash_lock);
563 spin_unlock(&dcache_lock);
565 return NULL;
568 /* Lookups in the root directory */
569 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
571 struct autofs_sb_info *sbi;
572 struct dentry *unhashed;
573 int oz_mode;
575 DPRINTK("name = %.*s",
576 dentry->d_name.len, dentry->d_name.name);
578 /* File name too long to exist */
579 if (dentry->d_name.len > NAME_MAX)
580 return ERR_PTR(-ENAMETOOLONG);
582 sbi = autofs4_sbi(dir->i_sb);
583 oz_mode = autofs4_oz_mode(sbi);
585 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
586 current->pid, process_group(current), sbi->catatonic, oz_mode);
588 unhashed = autofs4_lookup_unhashed(sbi, dentry->d_parent, &dentry->d_name);
589 if (!unhashed) {
591 * Mark the dentry incomplete, but add it. This is needed so
592 * that the VFS layer knows about the dentry, and we can count
593 * on catching any lookups through the revalidate.
595 * Let all the hard work be done by the revalidate function that
596 * needs to be able to do this anyway..
598 * We need to do this before we release the directory semaphore.
600 dentry->d_op = &autofs4_root_dentry_operations;
602 dentry->d_fsdata = NULL;
603 d_add(dentry, NULL);
604 } else {
605 struct autofs_info *ino = autofs4_dentry_ino(unhashed);
606 DPRINTK("rehash %p with %p", dentry, unhashed);
608 * If we are racing with expire the request might not
609 * be quite complete but the directory has been removed
610 * so it must have been successful, so just wait for it.
612 if (ino && (ino->flags & AUTOFS_INF_EXPIRING)) {
613 DPRINTK("wait for incomplete expire %p name=%.*s",
614 unhashed, unhashed->d_name.len,
615 unhashed->d_name.name);
616 autofs4_wait(sbi, unhashed, NFY_NONE);
617 DPRINTK("request completed");
619 d_rehash(unhashed);
620 dentry = unhashed;
623 if (!oz_mode) {
624 spin_lock(&dentry->d_lock);
625 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
626 spin_unlock(&dentry->d_lock);
629 if (dentry->d_op && dentry->d_op->d_revalidate) {
630 mutex_unlock(&dir->i_mutex);
631 (dentry->d_op->d_revalidate)(dentry, nd);
632 mutex_lock(&dir->i_mutex);
636 * If we are still pending, check if we had to handle
637 * a signal. If so we can force a restart..
639 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
640 /* See if we were interrupted */
641 if (signal_pending(current)) {
642 sigset_t *sigset = &current->pending.signal;
643 if (sigismember (sigset, SIGKILL) ||
644 sigismember (sigset, SIGQUIT) ||
645 sigismember (sigset, SIGINT)) {
646 if (unhashed)
647 dput(unhashed);
648 return ERR_PTR(-ERESTARTNOINTR);
651 spin_lock(&dentry->d_lock);
652 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
653 spin_unlock(&dentry->d_lock);
657 * If this dentry is unhashed, then we shouldn't honour this
658 * lookup. Returning ENOENT here doesn't do the right thing
659 * for all system calls, but it should be OK for the operations
660 * we permit from an autofs.
662 if (dentry->d_inode && d_unhashed(dentry)) {
664 * A user space application can (and has done in the past)
665 * remove and re-create this directory during the callback.
666 * This can leave us with an unhashed dentry, but a
667 * successful mount! So we need to perform another
668 * cached lookup in case the dentry now exists.
670 struct dentry *parent = dentry->d_parent;
671 struct dentry *new = d_lookup(parent, &dentry->d_name);
672 if (new != NULL)
673 dentry = new;
674 else
675 dentry = ERR_PTR(-ENOENT);
677 if (unhashed)
678 dput(unhashed);
680 return dentry;
683 if (unhashed)
684 return dentry;
686 return NULL;
689 static int autofs4_dir_symlink(struct inode *dir,
690 struct dentry *dentry,
691 const char *symname)
693 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
694 struct autofs_info *ino = autofs4_dentry_ino(dentry);
695 struct autofs_info *p_ino;
696 struct inode *inode;
697 char *cp;
699 DPRINTK("%s <- %.*s", symname,
700 dentry->d_name.len, dentry->d_name.name);
702 if (!autofs4_oz_mode(sbi))
703 return -EACCES;
705 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
706 if (ino == NULL)
707 return -ENOSPC;
709 ino->size = strlen(symname);
710 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
712 if (cp == NULL) {
713 kfree(ino);
714 return -ENOSPC;
717 strcpy(cp, symname);
719 inode = autofs4_get_inode(dir->i_sb, ino);
720 d_instantiate(dentry, inode);
722 if (dir == dir->i_sb->s_root->d_inode)
723 dentry->d_op = &autofs4_root_dentry_operations;
724 else
725 dentry->d_op = &autofs4_dentry_operations;
727 dentry->d_fsdata = ino;
728 ino->dentry = dget(dentry);
729 atomic_inc(&ino->count);
730 p_ino = autofs4_dentry_ino(dentry->d_parent);
731 if (p_ino && dentry->d_parent != dentry)
732 atomic_inc(&p_ino->count);
733 ino->inode = inode;
735 dir->i_mtime = CURRENT_TIME;
737 return 0;
741 * NOTE!
743 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
744 * that the file no longer exists. However, doing that means that the
745 * VFS layer can turn the dentry into a negative dentry. We don't want
746 * this, because the unlink is probably the result of an expire.
747 * We simply d_drop it and add it to a rehash candidates list in the
748 * super block, which allows the dentry lookup to reuse it retaining
749 * the flags, such as expire in progress, in case we're racing with expire.
751 * If a process is blocked on the dentry waiting for the expire to finish,
752 * it will invalidate the dentry and try to mount with a new one.
754 * Also see autofs4_dir_rmdir()..
756 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
758 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
759 struct autofs_info *ino = autofs4_dentry_ino(dentry);
760 struct autofs_info *p_ino;
762 /* This allows root to remove symlinks */
763 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
764 return -EACCES;
766 if (atomic_dec_and_test(&ino->count)) {
767 p_ino = autofs4_dentry_ino(dentry->d_parent);
768 if (p_ino && dentry->d_parent != dentry)
769 atomic_dec(&p_ino->count);
771 dput(ino->dentry);
773 dentry->d_inode->i_size = 0;
774 clear_nlink(dentry->d_inode);
776 dir->i_mtime = CURRENT_TIME;
778 spin_lock(&dcache_lock);
779 spin_lock(&sbi->rehash_lock);
780 list_add(&ino->rehash, &sbi->rehash_list);
781 spin_unlock(&sbi->rehash_lock);
782 spin_lock(&dentry->d_lock);
783 __d_drop(dentry);
784 spin_unlock(&dentry->d_lock);
785 spin_unlock(&dcache_lock);
787 return 0;
790 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
792 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
793 struct autofs_info *ino = autofs4_dentry_ino(dentry);
794 struct autofs_info *p_ino;
796 DPRINTK("dentry %p, removing %.*s",
797 dentry, dentry->d_name.len, dentry->d_name.name);
799 if (!autofs4_oz_mode(sbi))
800 return -EACCES;
802 spin_lock(&dcache_lock);
803 if (!list_empty(&dentry->d_subdirs)) {
804 spin_unlock(&dcache_lock);
805 return -ENOTEMPTY;
807 spin_lock(&sbi->rehash_lock);
808 list_add(&ino->rehash, &sbi->rehash_list);
809 spin_unlock(&sbi->rehash_lock);
810 spin_lock(&dentry->d_lock);
811 __d_drop(dentry);
812 spin_unlock(&dentry->d_lock);
813 spin_unlock(&dcache_lock);
815 if (atomic_dec_and_test(&ino->count)) {
816 p_ino = autofs4_dentry_ino(dentry->d_parent);
817 if (p_ino && dentry->d_parent != dentry)
818 atomic_dec(&p_ino->count);
820 dput(ino->dentry);
821 dentry->d_inode->i_size = 0;
822 clear_nlink(dentry->d_inode);
824 if (dir->i_nlink)
825 drop_nlink(dir);
827 return 0;
830 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
832 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
833 struct autofs_info *ino = autofs4_dentry_ino(dentry);
834 struct autofs_info *p_ino;
835 struct inode *inode;
837 if ( !autofs4_oz_mode(sbi) )
838 return -EACCES;
840 DPRINTK("dentry %p, creating %.*s",
841 dentry, dentry->d_name.len, dentry->d_name.name);
843 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
844 if (ino == NULL)
845 return -ENOSPC;
847 inode = autofs4_get_inode(dir->i_sb, ino);
848 d_instantiate(dentry, inode);
850 if (dir == dir->i_sb->s_root->d_inode)
851 dentry->d_op = &autofs4_root_dentry_operations;
852 else
853 dentry->d_op = &autofs4_dentry_operations;
855 dentry->d_fsdata = ino;
856 ino->dentry = dget(dentry);
857 atomic_inc(&ino->count);
858 p_ino = autofs4_dentry_ino(dentry->d_parent);
859 if (p_ino && dentry->d_parent != dentry)
860 atomic_inc(&p_ino->count);
861 ino->inode = inode;
862 inc_nlink(dir);
863 dir->i_mtime = CURRENT_TIME;
865 return 0;
868 /* Get/set timeout ioctl() operation */
869 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
870 unsigned long __user *p)
872 int rv;
873 unsigned long ntimeout;
875 if ( (rv = get_user(ntimeout, p)) ||
876 (rv = put_user(sbi->exp_timeout/HZ, p)) )
877 return rv;
879 if ( ntimeout > ULONG_MAX/HZ )
880 sbi->exp_timeout = 0;
881 else
882 sbi->exp_timeout = ntimeout * HZ;
884 return 0;
887 /* Return protocol version */
888 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
890 return put_user(sbi->version, p);
893 /* Return protocol sub version */
894 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
896 return put_user(sbi->sub_version, p);
900 * Tells the daemon whether we need to reghost or not. Also, clears
901 * the reghost_needed flag.
903 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
905 int status;
907 DPRINTK("returning %d", sbi->needs_reghost);
909 status = put_user(sbi->needs_reghost, p);
910 if ( status )
911 return status;
913 sbi->needs_reghost = 0;
914 return 0;
918 * Enable / Disable reghosting ioctl() operation
920 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
922 int status;
923 int val;
925 status = get_user(val, p);
927 DPRINTK("reghost = %d", val);
929 if (status)
930 return status;
932 /* turn on/off reghosting, with the val */
933 sbi->reghost_enabled = val;
934 return 0;
938 * Tells the daemon whether it can umount the autofs mount.
940 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
942 int status = 0;
944 if (may_umount(mnt))
945 status = 1;
947 DPRINTK("returning %d", status);
949 status = put_user(status, p);
951 return status;
954 /* Identify autofs4_dentries - this is so we can tell if there's
955 an extra dentry refcount or not. We only hold a refcount on the
956 dentry if its non-negative (ie, d_inode != NULL)
958 int is_autofs4_dentry(struct dentry *dentry)
960 return dentry && dentry->d_inode &&
961 (dentry->d_op == &autofs4_root_dentry_operations ||
962 dentry->d_op == &autofs4_dentry_operations) &&
963 dentry->d_fsdata != NULL;
967 * ioctl()'s on the root directory is the chief method for the daemon to
968 * generate kernel reactions
970 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
971 unsigned int cmd, unsigned long arg)
973 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
974 void __user *p = (void __user *)arg;
976 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
977 cmd,arg,sbi,process_group(current));
979 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
980 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
981 return -ENOTTY;
983 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
984 return -EPERM;
986 switch(cmd) {
987 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
988 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
989 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
990 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
991 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
992 autofs4_catatonic_mode(sbi);
993 return 0;
994 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
995 return autofs4_get_protover(sbi, p);
996 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
997 return autofs4_get_protosubver(sbi, p);
998 case AUTOFS_IOC_SETTIMEOUT:
999 return autofs4_get_set_timeout(sbi, p);
1001 case AUTOFS_IOC_TOGGLEREGHOST:
1002 return autofs4_toggle_reghost(sbi, p);
1003 case AUTOFS_IOC_ASKREGHOST:
1004 return autofs4_ask_reghost(sbi, p);
1006 case AUTOFS_IOC_ASKUMOUNT:
1007 return autofs4_ask_umount(filp->f_path.mnt, p);
1009 /* return a single thing to expire */
1010 case AUTOFS_IOC_EXPIRE:
1011 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
1012 /* same as above, but can send multiple expires through pipe */
1013 case AUTOFS_IOC_EXPIRE_MULTI:
1014 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
1016 default:
1017 return -ENOSYS;