blob: 2a771ec66956f92615de35694dc512f0144aedba [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- c -*- --------------------------------------------------------------- *
2 *
3 * linux/fs/autofs/root.c
4 *
5 * Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6 * Copyright 1999-2000 Jeremy Fitzhardinge <jeremy@goop.org>
7 * Copyright 2001-2003 Ian Kent <raven@themaw.net>
8 *
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.
12 *
13 * ------------------------------------------------------------------------- */
14
15#include <linux/errno.h>
16#include <linux/stat.h>
17#include <linux/param.h>
18#include <linux/time.h>
19#include <linux/smp_lock.h>
20#include "autofs_i.h"
21
22static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23static int autofs4_dir_unlink(struct inode *,struct dentry *);
24static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27static int autofs4_dir_open(struct inode *inode, struct file *file);
28static int autofs4_dir_close(struct inode *inode, struct file *file);
29static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
33
34struct file_operations autofs4_root_operations = {
35 .open = dcache_dir_open,
36 .release = dcache_dir_close,
37 .read = generic_read_dir,
38 .readdir = autofs4_root_readdir,
39 .ioctl = autofs4_root_ioctl,
40};
41
42struct file_operations autofs4_dir_operations = {
43 .open = autofs4_dir_open,
44 .release = autofs4_dir_close,
45 .read = generic_read_dir,
46 .readdir = autofs4_dir_readdir,
47};
48
49struct inode_operations autofs4_root_inode_operations = {
50 .lookup = autofs4_lookup,
51 .unlink = autofs4_dir_unlink,
52 .symlink = autofs4_dir_symlink,
53 .mkdir = autofs4_dir_mkdir,
54 .rmdir = autofs4_dir_rmdir,
55};
56
57struct inode_operations autofs4_dir_inode_operations = {
58 .lookup = autofs4_lookup,
59 .unlink = autofs4_dir_unlink,
60 .symlink = autofs4_dir_symlink,
61 .mkdir = autofs4_dir_mkdir,
62 .rmdir = autofs4_dir_rmdir,
63};
64
65static int autofs4_root_readdir(struct file *file, void *dirent,
66 filldir_t filldir)
67{
68 struct autofs_sb_info *sbi = autofs4_sbi(file->f_dentry->d_sb);
69 int oz_mode = autofs4_oz_mode(sbi);
70
71 DPRINTK("called, filp->f_pos = %lld", file->f_pos);
72
73 /*
74 * Don't set reghost flag if:
75 * 1) f_pos is larger than zero -- we've already been here.
76 * 2) we haven't even enabled reghosting in the 1st place.
77 * 3) this is the daemon doing a readdir
78 */
79 if (oz_mode && file->f_pos == 0 && sbi->reghost_enabled)
80 sbi->needs_reghost = 1;
81
82 DPRINTK("needs_reghost = %d", sbi->needs_reghost);
83
84 return autofs4_dcache_readdir(file, dirent, filldir);
85}
86
87/* Update usage from here to top of tree, so that scan of
88 top-level directories will give a useful result */
89static void autofs4_update_usage(struct dentry *dentry)
90{
91 struct dentry *top = dentry->d_sb->s_root;
92
93 spin_lock(&dcache_lock);
94 for(; dentry != top; dentry = dentry->d_parent) {
95 struct autofs_info *ino = autofs4_dentry_ino(dentry);
96
97 if (ino) {
98 update_atime(dentry->d_inode);
99 ino->last_used = jiffies;
100 }
101 }
102 spin_unlock(&dcache_lock);
103}
104
105/*
106 * From 2.4 kernel readdir.c
107 */
108static int autofs4_dcache_readdir(struct file * filp, void * dirent, filldir_t filldir)
109{
110 int i;
111 struct dentry *dentry = filp->f_dentry;
112
113 i = filp->f_pos;
114 switch (i) {
115 case 0:
116 if (filldir(dirent, ".", 1, i, dentry->d_inode->i_ino, DT_DIR) < 0)
117 break;
118 i++;
119 filp->f_pos++;
120 /* fallthrough */
121 case 1:
122 if (filldir(dirent, "..", 2, i, dentry->d_parent->d_inode->i_ino, DT_DIR) < 0)
123 break;
124 i++;
125 filp->f_pos++;
126 /* fallthrough */
127 default: {
128 struct list_head *list;
129 int j = i-2;
130
131 spin_lock(&dcache_lock);
132 list = dentry->d_subdirs.next;
133
134 for (;;) {
135 if (list == &dentry->d_subdirs) {
136 spin_unlock(&dcache_lock);
137 return 0;
138 }
139 if (!j)
140 break;
141 j--;
142 list = list->next;
143 }
144
145 while(1) {
146 struct dentry *de = list_entry(list, struct dentry, d_child);
147
148 if (!d_unhashed(de) && de->d_inode) {
149 spin_unlock(&dcache_lock);
150 if (filldir(dirent, de->d_name.name, de->d_name.len, filp->f_pos, de->d_inode->i_ino, DT_UNKNOWN) < 0)
151 break;
152 spin_lock(&dcache_lock);
153 }
154 filp->f_pos++;
155 list = list->next;
156 if (list != &dentry->d_subdirs)
157 continue;
158 spin_unlock(&dcache_lock);
159 break;
160 }
161 }
162 }
163 return 0;
164}
165
166static int autofs4_dir_open(struct inode *inode, struct file *file)
167{
168 struct dentry *dentry = file->f_dentry;
169 struct vfsmount *mnt = file->f_vfsmnt;
170 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
171 int status;
172
173 DPRINTK("file=%p dentry=%p %.*s",
174 file, dentry, dentry->d_name.len, dentry->d_name.name);
175
176 if (autofs4_oz_mode(sbi))
177 goto out;
178
179 if (autofs4_ispending(dentry)) {
180 DPRINTK("dentry busy");
181 return -EBUSY;
182 }
183
184 if (!d_mountpoint(dentry) && dentry->d_op && dentry->d_op->d_revalidate) {
185 struct nameidata nd;
186 int empty;
187
188 /* In case there are stale directory dentrys from a failed mount */
189 spin_lock(&dcache_lock);
190 empty = list_empty(&dentry->d_subdirs);
191 spin_unlock(&dcache_lock);
192
193 if (!empty)
194 d_invalidate(dentry);
195
196 nd.flags = LOOKUP_DIRECTORY;
197 status = (dentry->d_op->d_revalidate)(dentry, &nd);
198
199 if (!status)
200 return -ENOENT;
201 }
202
203 if (d_mountpoint(dentry)) {
204 struct file *fp = NULL;
205 struct vfsmount *fp_mnt = mntget(mnt);
206 struct dentry *fp_dentry = dget(dentry);
207
Ian Kent9b1e3af2005-06-21 17:16:38 -0700208 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
209 dput(fp_dentry);
210 mntput(fp_mnt);
211 return -ENOENT;
212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 fp = dentry_open(fp_dentry, fp_mnt, file->f_flags);
215 status = PTR_ERR(fp);
216 if (IS_ERR(fp)) {
217 file->private_data = NULL;
218 return status;
219 }
220 file->private_data = fp;
221 }
222out:
223 return 0;
224}
225
226static int autofs4_dir_close(struct inode *inode, struct file *file)
227{
228 struct dentry *dentry = file->f_dentry;
229 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
230
231 DPRINTK("file=%p dentry=%p %.*s",
232 file, dentry, dentry->d_name.len, dentry->d_name.name);
233
234 if (autofs4_oz_mode(sbi))
235 goto out;
236
237 if (autofs4_ispending(dentry)) {
238 DPRINTK("dentry busy");
239 return -EBUSY;
240 }
241
242 if (d_mountpoint(dentry)) {
243 struct file *fp = file->private_data;
244
245 if (!fp)
246 return -ENOENT;
247
248 filp_close(fp, current->files);
249 file->private_data = NULL;
250 }
251out:
252 return 0;
253}
254
255static int autofs4_dir_readdir(struct file *file, void *dirent, filldir_t filldir)
256{
257 struct dentry *dentry = file->f_dentry;
258 struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
259 int status;
260
261 DPRINTK("file=%p dentry=%p %.*s",
262 file, dentry, dentry->d_name.len, dentry->d_name.name);
263
264 if (autofs4_oz_mode(sbi))
265 goto out;
266
267 if (autofs4_ispending(dentry)) {
268 DPRINTK("dentry busy");
269 return -EBUSY;
270 }
271
272 if (d_mountpoint(dentry)) {
273 struct file *fp = file->private_data;
274
275 if (!fp)
276 return -ENOENT;
277
278 if (!fp->f_op || !fp->f_op->readdir)
279 goto out;
280
281 status = vfs_readdir(fp, filldir, dirent);
282 file->f_pos = fp->f_pos;
283 if (status)
284 autofs4_copy_atime(file, fp);
285 return status;
286 }
287out:
288 return autofs4_dcache_readdir(file, dirent, filldir);
289}
290
291static int try_to_fill_dentry(struct dentry *dentry,
292 struct super_block *sb,
293 struct autofs_sb_info *sbi, int flags)
294{
295 struct autofs_info *de_info = autofs4_dentry_ino(dentry);
296 int status = 0;
297
298 /* Block on any pending expiry here; invalidate the dentry
299 when expiration is done to trigger mount request with a new
300 dentry */
301 if (de_info && (de_info->flags & AUTOFS_INF_EXPIRING)) {
302 DPRINTK("waiting for expire %p name=%.*s",
303 dentry, dentry->d_name.len, dentry->d_name.name);
304
305 status = autofs4_wait(sbi, dentry, NFY_NONE);
306
307 DPRINTK("expire done status=%d", status);
308
Ian Kent1684b2b2005-06-21 17:16:41 -0700309 /*
310 * If the directory still exists the mount request must
311 * continue otherwise it can't be followed at the right
312 * time during the walk.
313 */
314 status = d_invalidate(dentry);
315 if (status != -EBUSY)
316 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 DPRINTK("dentry=%p %.*s ino=%p",
320 dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
321
322 /* Wait for a pending mount, triggering one if there isn't one already */
323 if (dentry->d_inode == NULL) {
324 DPRINTK("waiting for mount name=%.*s",
325 dentry->d_name.len, dentry->d_name.name);
326
327 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
328
329 DPRINTK("mount done status=%d", status);
330
331 if (status && dentry->d_inode)
332 return 0; /* Try to get the kernel to invalidate this dentry */
333
334 /* Turn this into a real negative dentry? */
335 if (status == -ENOENT) {
336 dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
337 spin_lock(&dentry->d_lock);
338 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
339 spin_unlock(&dentry->d_lock);
340 return 1;
341 } else if (status) {
342 /* Return a negative dentry, but leave it "pending" */
343 return 1;
344 }
345 /* Trigger mount for path component or follow link */
346 } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
347 current->link_count) {
348 DPRINTK("waiting for mount name=%.*s",
349 dentry->d_name.len, dentry->d_name.name);
350
351 spin_lock(&dentry->d_lock);
352 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
353 spin_unlock(&dentry->d_lock);
354 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
355
356 DPRINTK("mount done status=%d", status);
357
358 if (status) {
359 spin_lock(&dentry->d_lock);
360 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
361 spin_unlock(&dentry->d_lock);
362 return 0;
363 }
364 }
365
366 /* We don't update the usages for the autofs daemon itself, this
367 is necessary for recursive autofs mounts */
368 if (!autofs4_oz_mode(sbi))
369 autofs4_update_usage(dentry);
370
371 spin_lock(&dentry->d_lock);
372 dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
373 spin_unlock(&dentry->d_lock);
374 return 1;
375}
376
377/*
378 * Revalidate is called on every cache lookup. Some of those
379 * cache lookups may actually happen while the dentry is not
380 * yet completely filled in, and revalidate has to delay such
381 * lookups..
382 */
383static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
384{
385 struct inode * dir = dentry->d_parent->d_inode;
386 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
387 int oz_mode = autofs4_oz_mode(sbi);
388 int flags = nd ? nd->flags : 0;
389 int status = 1;
390
391 /* Pending dentry */
392 if (autofs4_ispending(dentry)) {
393 if (!oz_mode)
394 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
395 return status;
396 }
397
398 /* Negative dentry.. invalidate if "old" */
399 if (dentry->d_inode == NULL)
400 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
401
402 /* Check for a non-mountpoint directory with no contents */
403 spin_lock(&dcache_lock);
404 if (S_ISDIR(dentry->d_inode->i_mode) &&
405 !d_mountpoint(dentry) &&
406 list_empty(&dentry->d_subdirs)) {
407 DPRINTK("dentry=%p %.*s, emptydir",
408 dentry, dentry->d_name.len, dentry->d_name.name);
409 spin_unlock(&dcache_lock);
410 if (!oz_mode)
411 status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
412 return status;
413 }
414 spin_unlock(&dcache_lock);
415
416 /* Update the usage list */
417 if (!oz_mode)
418 autofs4_update_usage(dentry);
419
420 return 1;
421}
422
423static void autofs4_dentry_release(struct dentry *de)
424{
425 struct autofs_info *inf;
426
427 DPRINTK("releasing %p", de);
428
429 inf = autofs4_dentry_ino(de);
430 de->d_fsdata = NULL;
431
432 if (inf) {
433 inf->dentry = NULL;
434 inf->inode = NULL;
435
436 autofs4_free_ino(inf);
437 }
438}
439
440/* For dentries of directories in the root dir */
441static struct dentry_operations autofs4_root_dentry_operations = {
442 .d_revalidate = autofs4_revalidate,
443 .d_release = autofs4_dentry_release,
444};
445
446/* For other dentries */
447static struct dentry_operations autofs4_dentry_operations = {
448 .d_revalidate = autofs4_revalidate,
449 .d_release = autofs4_dentry_release,
450};
451
452/* Lookups in the root directory */
453static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
454{
455 struct autofs_sb_info *sbi;
456 int oz_mode;
457
458 DPRINTK("name = %.*s",
459 dentry->d_name.len, dentry->d_name.name);
460
461 if (dentry->d_name.len > NAME_MAX)
462 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
463
464 sbi = autofs4_sbi(dir->i_sb);
465
466 oz_mode = autofs4_oz_mode(sbi);
467 DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
468 current->pid, process_group(current), sbi->catatonic, oz_mode);
469
470 /*
471 * Mark the dentry incomplete, but add it. This is needed so
472 * that the VFS layer knows about the dentry, and we can count
473 * on catching any lookups through the revalidate.
474 *
475 * Let all the hard work be done by the revalidate function that
476 * needs to be able to do this anyway..
477 *
478 * We need to do this before we release the directory semaphore.
479 */
480 dentry->d_op = &autofs4_root_dentry_operations;
481
482 if (!oz_mode) {
483 spin_lock(&dentry->d_lock);
484 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
485 spin_unlock(&dentry->d_lock);
486 }
487 dentry->d_fsdata = NULL;
488 d_add(dentry, NULL);
489
490 if (dentry->d_op && dentry->d_op->d_revalidate) {
491 up(&dir->i_sem);
492 (dentry->d_op->d_revalidate)(dentry, nd);
493 down(&dir->i_sem);
494 }
495
496 /*
497 * If we are still pending, check if we had to handle
498 * a signal. If so we can force a restart..
499 */
500 if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
501 /* See if we were interrupted */
502 if (signal_pending(current)) {
503 sigset_t *sigset = &current->pending.signal;
504 if (sigismember (sigset, SIGKILL) ||
505 sigismember (sigset, SIGQUIT) ||
506 sigismember (sigset, SIGINT)) {
507 return ERR_PTR(-ERESTARTNOINTR);
508 }
509 }
510 }
511
512 /*
513 * If this dentry is unhashed, then we shouldn't honour this
514 * lookup even if the dentry is positive. Returning ENOENT here
515 * doesn't do the right thing for all system calls, but it should
516 * be OK for the operations we permit from an autofs.
517 */
518 if ( dentry->d_inode && d_unhashed(dentry) )
519 return ERR_PTR(-ENOENT);
520
521 return NULL;
522}
523
524static int autofs4_dir_symlink(struct inode *dir,
525 struct dentry *dentry,
526 const char *symname)
527{
528 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
529 struct autofs_info *ino = autofs4_dentry_ino(dentry);
530 struct inode *inode;
531 char *cp;
532
533 DPRINTK("%s <- %.*s", symname,
534 dentry->d_name.len, dentry->d_name.name);
535
536 if (!autofs4_oz_mode(sbi))
537 return -EACCES;
538
539 ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
540 if (ino == NULL)
541 return -ENOSPC;
542
543 ino->size = strlen(symname);
544 ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
545
546 if (cp == NULL) {
547 kfree(ino);
548 return -ENOSPC;
549 }
550
551 strcpy(cp, symname);
552
553 inode = autofs4_get_inode(dir->i_sb, ino);
554 d_instantiate(dentry, inode);
555
556 if (dir == dir->i_sb->s_root->d_inode)
557 dentry->d_op = &autofs4_root_dentry_operations;
558 else
559 dentry->d_op = &autofs4_dentry_operations;
560
561 dentry->d_fsdata = ino;
562 ino->dentry = dget(dentry);
563 ino->inode = inode;
564
565 dir->i_mtime = CURRENT_TIME;
566
567 return 0;
568}
569
570/*
571 * NOTE!
572 *
573 * Normal filesystems would do a "d_delete()" to tell the VFS dcache
574 * that the file no longer exists. However, doing that means that the
575 * VFS layer can turn the dentry into a negative dentry. We don't want
576 * this, because since the unlink is probably the result of an expire.
577 * We simply d_drop it, which allows the dentry lookup to remount it
578 * if necessary.
579 *
580 * If a process is blocked on the dentry waiting for the expire to finish,
581 * it will invalidate the dentry and try to mount with a new one.
582 *
583 * Also see autofs4_dir_rmdir()..
584 */
585static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
586{
587 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
588 struct autofs_info *ino = autofs4_dentry_ino(dentry);
589
590 /* This allows root to remove symlinks */
591 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
592 return -EACCES;
593
594 dput(ino->dentry);
595
596 dentry->d_inode->i_size = 0;
597 dentry->d_inode->i_nlink = 0;
598
599 dir->i_mtime = CURRENT_TIME;
600
601 d_drop(dentry);
602
603 return 0;
604}
605
606static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
607{
608 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
609 struct autofs_info *ino = autofs4_dentry_ino(dentry);
610
611 if (!autofs4_oz_mode(sbi))
612 return -EACCES;
613
614 spin_lock(&dcache_lock);
615 if (!list_empty(&dentry->d_subdirs)) {
616 spin_unlock(&dcache_lock);
617 return -ENOTEMPTY;
618 }
619 spin_lock(&dentry->d_lock);
620 __d_drop(dentry);
621 spin_unlock(&dentry->d_lock);
622 spin_unlock(&dcache_lock);
623
624 dput(ino->dentry);
625
626 dentry->d_inode->i_size = 0;
627 dentry->d_inode->i_nlink = 0;
628
629 if (dir->i_nlink)
630 dir->i_nlink--;
631
632 return 0;
633}
634
635static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
636{
637 struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
638 struct autofs_info *ino = autofs4_dentry_ino(dentry);
639 struct inode *inode;
640
641 if ( !autofs4_oz_mode(sbi) )
642 return -EACCES;
643
644 DPRINTK("dentry %p, creating %.*s",
645 dentry, dentry->d_name.len, dentry->d_name.name);
646
647 ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
648 if (ino == NULL)
649 return -ENOSPC;
650
651 inode = autofs4_get_inode(dir->i_sb, ino);
652 d_instantiate(dentry, inode);
653
654 if (dir == dir->i_sb->s_root->d_inode)
655 dentry->d_op = &autofs4_root_dentry_operations;
656 else
657 dentry->d_op = &autofs4_dentry_operations;
658
659 dentry->d_fsdata = ino;
660 ino->dentry = dget(dentry);
661 ino->inode = inode;
662 dir->i_nlink++;
663 dir->i_mtime = CURRENT_TIME;
664
665 return 0;
666}
667
668/* Get/set timeout ioctl() operation */
669static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
670 unsigned long __user *p)
671{
672 int rv;
673 unsigned long ntimeout;
674
675 if ( (rv = get_user(ntimeout, p)) ||
676 (rv = put_user(sbi->exp_timeout/HZ, p)) )
677 return rv;
678
679 if ( ntimeout > ULONG_MAX/HZ )
680 sbi->exp_timeout = 0;
681 else
682 sbi->exp_timeout = ntimeout * HZ;
683
684 return 0;
685}
686
687/* Return protocol version */
688static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
689{
690 return put_user(sbi->version, p);
691}
692
693/* Return protocol sub version */
694static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
695{
696 return put_user(sbi->sub_version, p);
697}
698
699/*
700 * Tells the daemon whether we need to reghost or not. Also, clears
701 * the reghost_needed flag.
702 */
703static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
704{
705 int status;
706
707 DPRINTK("returning %d", sbi->needs_reghost);
708
709 status = put_user(sbi->needs_reghost, p);
710 if ( status )
711 return status;
712
713 sbi->needs_reghost = 0;
714 return 0;
715}
716
717/*
718 * Enable / Disable reghosting ioctl() operation
719 */
720static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
721{
722 int status;
723 int val;
724
725 status = get_user(val, p);
726
727 DPRINTK("reghost = %d", val);
728
729 if (status)
730 return status;
731
732 /* turn on/off reghosting, with the val */
733 sbi->reghost_enabled = val;
734 return 0;
735}
736
737/*
738* Tells the daemon whether it can umount the autofs mount.
739*/
740static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
741{
742 int status = 0;
743
744 if (may_umount(mnt) == 0)
745 status = 1;
746
747 DPRINTK("returning %d", status);
748
749 status = put_user(status, p);
750
751 return status;
752}
753
754/* Identify autofs4_dentries - this is so we can tell if there's
755 an extra dentry refcount or not. We only hold a refcount on the
756 dentry if its non-negative (ie, d_inode != NULL)
757*/
758int is_autofs4_dentry(struct dentry *dentry)
759{
760 return dentry && dentry->d_inode &&
761 (dentry->d_op == &autofs4_root_dentry_operations ||
762 dentry->d_op == &autofs4_dentry_operations) &&
763 dentry->d_fsdata != NULL;
764}
765
766/*
767 * ioctl()'s on the root directory is the chief method for the daemon to
768 * generate kernel reactions
769 */
770static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
771 unsigned int cmd, unsigned long arg)
772{
773 struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
774 void __user *p = (void __user *)arg;
775
776 DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
777 cmd,arg,sbi,process_group(current));
778
779 if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
780 _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
781 return -ENOTTY;
782
783 if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
784 return -EPERM;
785
786 switch(cmd) {
787 case AUTOFS_IOC_READY: /* Wait queue: go ahead and retry */
788 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
789 case AUTOFS_IOC_FAIL: /* Wait queue: fail with ENOENT */
790 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
791 case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
792 autofs4_catatonic_mode(sbi);
793 return 0;
794 case AUTOFS_IOC_PROTOVER: /* Get protocol version */
795 return autofs4_get_protover(sbi, p);
796 case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
797 return autofs4_get_protosubver(sbi, p);
798 case AUTOFS_IOC_SETTIMEOUT:
799 return autofs4_get_set_timeout(sbi, p);
800
801 case AUTOFS_IOC_TOGGLEREGHOST:
802 return autofs4_toggle_reghost(sbi, p);
803 case AUTOFS_IOC_ASKREGHOST:
804 return autofs4_ask_reghost(sbi, p);
805
806 case AUTOFS_IOC_ASKUMOUNT:
807 return autofs4_ask_umount(filp->f_vfsmnt, p);
808
809 /* return a single thing to expire */
810 case AUTOFS_IOC_EXPIRE:
811 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
812 /* same as above, but can send multiple expires through pipe */
813 case AUTOFS_IOC_EXPIRE_MULTI:
814 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
815
816 default:
817 return -ENOSYS;
818 }
819}