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