]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - fs/autofs4/root.c
e137acf0543dffb931c83d04d4cac1ee5f83461b
[linux-2.6-omap-h63xx.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 int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
23 static int autofs4_dir_unlink(struct inode *,struct dentry *);
24 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
25 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
26 static int autofs4_root_ioctl(struct inode *, struct file *,unsigned int,unsigned long);
27 static int autofs4_dir_open(struct inode *inode, struct file *file);
28 static int autofs4_dir_close(struct inode *inode, struct file *file);
29 static int autofs4_dir_readdir(struct file * filp, void * dirent, filldir_t filldir);
30 static int autofs4_root_readdir(struct file * filp, void * dirent, filldir_t filldir);
31 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
32 static int autofs4_dcache_readdir(struct file *, void *, filldir_t);
33
34 struct 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
42 struct 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
49 struct 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
57 struct 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
65 static 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 */
89 static 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  */
108 static 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
166 static 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
208                 if (!autofs4_follow_mount(&fp_mnt, &fp_dentry)) {
209                         dput(fp_dentry);
210                         mntput(fp_mnt);
211                         return -ENOENT;
212                 }
213
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         }
222 out:
223         return 0;
224 }
225
226 static 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         }
251 out:
252         return 0;
253 }
254
255 static 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         }
287 out:
288         return autofs4_dcache_readdir(file, dirent, filldir);
289 }
290
291 static 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                 
309                 return 0;
310         }
311
312         DPRINTK("dentry=%p %.*s ino=%p",
313                  dentry, dentry->d_name.len, dentry->d_name.name, dentry->d_inode);
314
315         /* Wait for a pending mount, triggering one if there isn't one already */
316         if (dentry->d_inode == NULL) {
317                 DPRINTK("waiting for mount name=%.*s",
318                          dentry->d_name.len, dentry->d_name.name);
319
320                 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
321                  
322                 DPRINTK("mount done status=%d", status);
323
324                 if (status && dentry->d_inode)
325                         return 0; /* Try to get the kernel to invalidate this dentry */
326                 
327                 /* Turn this into a real negative dentry? */
328                 if (status == -ENOENT) {
329                         dentry->d_time = jiffies + AUTOFS_NEGATIVE_TIMEOUT;
330                         spin_lock(&dentry->d_lock);
331                         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
332                         spin_unlock(&dentry->d_lock);
333                         return 1;
334                 } else if (status) {
335                         /* Return a negative dentry, but leave it "pending" */
336                         return 1;
337                 }
338         /* Trigger mount for path component or follow link */
339         } else if (flags & (LOOKUP_CONTINUE | LOOKUP_DIRECTORY) ||
340                         current->link_count) {
341                 DPRINTK("waiting for mount name=%.*s",
342                         dentry->d_name.len, dentry->d_name.name);
343
344                 spin_lock(&dentry->d_lock);
345                 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
346                 spin_unlock(&dentry->d_lock);
347                 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
348
349                 DPRINTK("mount done status=%d", status);
350
351                 if (status) {
352                         spin_lock(&dentry->d_lock);
353                         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
354                         spin_unlock(&dentry->d_lock);
355                         return 0;
356                 }
357         }
358
359         /* We don't update the usages for the autofs daemon itself, this
360            is necessary for recursive autofs mounts */
361         if (!autofs4_oz_mode(sbi))
362                 autofs4_update_usage(dentry);
363
364         spin_lock(&dentry->d_lock);
365         dentry->d_flags &= ~DCACHE_AUTOFS_PENDING;
366         spin_unlock(&dentry->d_lock);
367         return 1;
368 }
369
370 /*
371  * Revalidate is called on every cache lookup.  Some of those
372  * cache lookups may actually happen while the dentry is not
373  * yet completely filled in, and revalidate has to delay such
374  * lookups..
375  */
376 static int autofs4_revalidate(struct dentry * dentry, struct nameidata *nd)
377 {
378         struct inode * dir = dentry->d_parent->d_inode;
379         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
380         int oz_mode = autofs4_oz_mode(sbi);
381         int flags = nd ? nd->flags : 0;
382         int status = 1;
383
384         /* Pending dentry */
385         if (autofs4_ispending(dentry)) {
386                 if (!oz_mode)
387                         status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
388                 return status;
389         }
390
391         /* Negative dentry.. invalidate if "old" */
392         if (dentry->d_inode == NULL)
393                 return (dentry->d_time - jiffies <= AUTOFS_NEGATIVE_TIMEOUT);
394
395         /* Check for a non-mountpoint directory with no contents */
396         spin_lock(&dcache_lock);
397         if (S_ISDIR(dentry->d_inode->i_mode) &&
398             !d_mountpoint(dentry) && 
399             list_empty(&dentry->d_subdirs)) {
400                 DPRINTK("dentry=%p %.*s, emptydir",
401                          dentry, dentry->d_name.len, dentry->d_name.name);
402                 spin_unlock(&dcache_lock);
403                 if (!oz_mode)
404                         status = try_to_fill_dentry(dentry, dir->i_sb, sbi, flags);
405                 return status;
406         }
407         spin_unlock(&dcache_lock);
408
409         /* Update the usage list */
410         if (!oz_mode)
411                 autofs4_update_usage(dentry);
412
413         return 1;
414 }
415
416 static void autofs4_dentry_release(struct dentry *de)
417 {
418         struct autofs_info *inf;
419
420         DPRINTK("releasing %p", de);
421
422         inf = autofs4_dentry_ino(de);
423         de->d_fsdata = NULL;
424
425         if (inf) {
426                 inf->dentry = NULL;
427                 inf->inode = NULL;
428
429                 autofs4_free_ino(inf);
430         }
431 }
432
433 /* For dentries of directories in the root dir */
434 static struct dentry_operations autofs4_root_dentry_operations = {
435         .d_revalidate   = autofs4_revalidate,
436         .d_release      = autofs4_dentry_release,
437 };
438
439 /* For other dentries */
440 static struct dentry_operations autofs4_dentry_operations = {
441         .d_revalidate   = autofs4_revalidate,
442         .d_release      = autofs4_dentry_release,
443 };
444
445 /* Lookups in the root directory */
446 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
447 {
448         struct autofs_sb_info *sbi;
449         int oz_mode;
450
451         DPRINTK("name = %.*s",
452                 dentry->d_name.len, dentry->d_name.name);
453
454         if (dentry->d_name.len > NAME_MAX)
455                 return ERR_PTR(-ENAMETOOLONG);/* File name too long to exist */
456
457         sbi = autofs4_sbi(dir->i_sb);
458
459         oz_mode = autofs4_oz_mode(sbi);
460         DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
461                  current->pid, process_group(current), sbi->catatonic, oz_mode);
462
463         /*
464          * Mark the dentry incomplete, but add it. This is needed so
465          * that the VFS layer knows about the dentry, and we can count
466          * on catching any lookups through the revalidate.
467          *
468          * Let all the hard work be done by the revalidate function that
469          * needs to be able to do this anyway..
470          *
471          * We need to do this before we release the directory semaphore.
472          */
473         dentry->d_op = &autofs4_root_dentry_operations;
474
475         if (!oz_mode) {
476                 spin_lock(&dentry->d_lock);
477                 dentry->d_flags |= DCACHE_AUTOFS_PENDING;
478                 spin_unlock(&dentry->d_lock);
479         }
480         dentry->d_fsdata = NULL;
481         d_add(dentry, NULL);
482
483         if (dentry->d_op && dentry->d_op->d_revalidate) {
484                 up(&dir->i_sem);
485                 (dentry->d_op->d_revalidate)(dentry, nd);
486                 down(&dir->i_sem);
487         }
488
489         /*
490          * If we are still pending, check if we had to handle
491          * a signal. If so we can force a restart..
492          */
493         if (dentry->d_flags & DCACHE_AUTOFS_PENDING) {
494                 /* See if we were interrupted */
495                 if (signal_pending(current)) {
496                         sigset_t *sigset = &current->pending.signal;
497                         if (sigismember (sigset, SIGKILL) ||
498                             sigismember (sigset, SIGQUIT) ||
499                             sigismember (sigset, SIGINT)) {
500                             return ERR_PTR(-ERESTARTNOINTR);
501                         }
502                 }
503         }
504
505         /*
506          * If this dentry is unhashed, then we shouldn't honour this
507          * lookup even if the dentry is positive.  Returning ENOENT here
508          * doesn't do the right thing for all system calls, but it should
509          * be OK for the operations we permit from an autofs.
510          */
511         if ( dentry->d_inode && d_unhashed(dentry) )
512                 return ERR_PTR(-ENOENT);
513
514         return NULL;
515 }
516
517 static int autofs4_dir_symlink(struct inode *dir, 
518                                struct dentry *dentry,
519                                const char *symname)
520 {
521         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
522         struct autofs_info *ino = autofs4_dentry_ino(dentry);
523         struct inode *inode;
524         char *cp;
525
526         DPRINTK("%s <- %.*s", symname,
527                 dentry->d_name.len, dentry->d_name.name);
528
529         if (!autofs4_oz_mode(sbi))
530                 return -EACCES;
531
532         ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
533         if (ino == NULL)
534                 return -ENOSPC;
535
536         ino->size = strlen(symname);
537         ino->u.symlink = cp = kmalloc(ino->size + 1, GFP_KERNEL);
538
539         if (cp == NULL) {
540                 kfree(ino);
541                 return -ENOSPC;
542         }
543
544         strcpy(cp, symname);
545
546         inode = autofs4_get_inode(dir->i_sb, ino);
547         d_instantiate(dentry, inode);
548
549         if (dir == dir->i_sb->s_root->d_inode)
550                 dentry->d_op = &autofs4_root_dentry_operations;
551         else
552                 dentry->d_op = &autofs4_dentry_operations;
553
554         dentry->d_fsdata = ino;
555         ino->dentry = dget(dentry);
556         ino->inode = inode;
557
558         dir->i_mtime = CURRENT_TIME;
559
560         return 0;
561 }
562
563 /*
564  * NOTE!
565  *
566  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
567  * that the file no longer exists. However, doing that means that the
568  * VFS layer can turn the dentry into a negative dentry.  We don't want
569  * this, because since the unlink is probably the result of an expire.
570  * We simply d_drop it, which allows the dentry lookup to remount it
571  * if necessary.
572  *
573  * If a process is blocked on the dentry waiting for the expire to finish,
574  * it will invalidate the dentry and try to mount with a new one.
575  *
576  * Also see autofs4_dir_rmdir()..
577  */
578 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
579 {
580         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
581         struct autofs_info *ino = autofs4_dentry_ino(dentry);
582         
583         /* This allows root to remove symlinks */
584         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
585                 return -EACCES;
586
587         dput(ino->dentry);
588
589         dentry->d_inode->i_size = 0;
590         dentry->d_inode->i_nlink = 0;
591
592         dir->i_mtime = CURRENT_TIME;
593
594         d_drop(dentry);
595
596         return 0;
597 }
598
599 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
600 {
601         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
602         struct autofs_info *ino = autofs4_dentry_ino(dentry);
603         
604         if (!autofs4_oz_mode(sbi))
605                 return -EACCES;
606
607         spin_lock(&dcache_lock);
608         if (!list_empty(&dentry->d_subdirs)) {
609                 spin_unlock(&dcache_lock);
610                 return -ENOTEMPTY;
611         }
612         spin_lock(&dentry->d_lock);
613         __d_drop(dentry);
614         spin_unlock(&dentry->d_lock);
615         spin_unlock(&dcache_lock);
616
617         dput(ino->dentry);
618
619         dentry->d_inode->i_size = 0;
620         dentry->d_inode->i_nlink = 0;
621
622         if (dir->i_nlink)
623                 dir->i_nlink--;
624
625         return 0;
626 }
627
628 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
629 {
630         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
631         struct autofs_info *ino = autofs4_dentry_ino(dentry);
632         struct inode *inode;
633
634         if ( !autofs4_oz_mode(sbi) )
635                 return -EACCES;
636
637         DPRINTK("dentry %p, creating %.*s",
638                 dentry, dentry->d_name.len, dentry->d_name.name);
639
640         ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
641         if (ino == NULL)
642                 return -ENOSPC;
643
644         inode = autofs4_get_inode(dir->i_sb, ino);
645         d_instantiate(dentry, inode);
646
647         if (dir == dir->i_sb->s_root->d_inode)
648                 dentry->d_op = &autofs4_root_dentry_operations;
649         else
650                 dentry->d_op = &autofs4_dentry_operations;
651
652         dentry->d_fsdata = ino;
653         ino->dentry = dget(dentry);
654         ino->inode = inode;
655         dir->i_nlink++;
656         dir->i_mtime = CURRENT_TIME;
657
658         return 0;
659 }
660
661 /* Get/set timeout ioctl() operation */
662 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
663                                          unsigned long __user *p)
664 {
665         int rv;
666         unsigned long ntimeout;
667
668         if ( (rv = get_user(ntimeout, p)) ||
669              (rv = put_user(sbi->exp_timeout/HZ, p)) )
670                 return rv;
671
672         if ( ntimeout > ULONG_MAX/HZ )
673                 sbi->exp_timeout = 0;
674         else
675                 sbi->exp_timeout = ntimeout * HZ;
676
677         return 0;
678 }
679
680 /* Return protocol version */
681 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
682 {
683         return put_user(sbi->version, p);
684 }
685
686 /* Return protocol sub version */
687 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
688 {
689         return put_user(sbi->sub_version, p);
690 }
691
692 /*
693  * Tells the daemon whether we need to reghost or not. Also, clears
694  * the reghost_needed flag.
695  */
696 static inline int autofs4_ask_reghost(struct autofs_sb_info *sbi, int __user *p)
697 {
698         int status;
699
700         DPRINTK("returning %d", sbi->needs_reghost);
701
702         status = put_user(sbi->needs_reghost, p);
703         if ( status )
704                 return status;
705
706         sbi->needs_reghost = 0;
707         return 0;
708 }
709
710 /*
711  * Enable / Disable reghosting ioctl() operation
712  */
713 static inline int autofs4_toggle_reghost(struct autofs_sb_info *sbi, int __user *p)
714 {
715         int status;
716         int val;
717
718         status = get_user(val, p);
719
720         DPRINTK("reghost = %d", val);
721
722         if (status)
723                 return status;
724
725         /* turn on/off reghosting, with the val */
726         sbi->reghost_enabled = val;
727         return 0;
728 }
729
730 /*
731 * Tells the daemon whether it can umount the autofs mount.
732 */
733 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
734 {
735         int status = 0;
736
737         if (may_umount(mnt) == 0)
738                 status = 1;
739
740         DPRINTK("returning %d", status);
741
742         status = put_user(status, p);
743
744         return status;
745 }
746
747 /* Identify autofs4_dentries - this is so we can tell if there's
748    an extra dentry refcount or not.  We only hold a refcount on the
749    dentry if its non-negative (ie, d_inode != NULL)
750 */
751 int is_autofs4_dentry(struct dentry *dentry)
752 {
753         return dentry && dentry->d_inode &&
754                 (dentry->d_op == &autofs4_root_dentry_operations ||
755                  dentry->d_op == &autofs4_dentry_operations) &&
756                 dentry->d_fsdata != NULL;
757 }
758
759 /*
760  * ioctl()'s on the root directory is the chief method for the daemon to
761  * generate kernel reactions
762  */
763 static int autofs4_root_ioctl(struct inode *inode, struct file *filp,
764                              unsigned int cmd, unsigned long arg)
765 {
766         struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
767         void __user *p = (void __user *)arg;
768
769         DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
770                 cmd,arg,sbi,process_group(current));
771
772         if ( _IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
773              _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT )
774                 return -ENOTTY;
775         
776         if ( !autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN) )
777                 return -EPERM;
778         
779         switch(cmd) {
780         case AUTOFS_IOC_READY:  /* Wait queue: go ahead and retry */
781                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
782         case AUTOFS_IOC_FAIL:   /* Wait queue: fail with ENOENT */
783                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
784         case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
785                 autofs4_catatonic_mode(sbi);
786                 return 0;
787         case AUTOFS_IOC_PROTOVER: /* Get protocol version */
788                 return autofs4_get_protover(sbi, p);
789         case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
790                 return autofs4_get_protosubver(sbi, p);
791         case AUTOFS_IOC_SETTIMEOUT:
792                 return autofs4_get_set_timeout(sbi, p);
793
794         case AUTOFS_IOC_TOGGLEREGHOST:
795                 return autofs4_toggle_reghost(sbi, p);
796         case AUTOFS_IOC_ASKREGHOST:
797                 return autofs4_ask_reghost(sbi, p);
798
799         case AUTOFS_IOC_ASKUMOUNT:
800                 return autofs4_ask_umount(filp->f_vfsmnt, p);
801
802         /* return a single thing to expire */
803         case AUTOFS_IOC_EXPIRE:
804                 return autofs4_expire_run(inode->i_sb,filp->f_vfsmnt,sbi, p);
805         /* same as above, but can send multiple expires through pipe */
806         case AUTOFS_IOC_EXPIRE_MULTI:
807                 return autofs4_expire_multi(inode->i_sb,filp->f_vfsmnt,sbi, p);
808
809         default:
810                 return -ENOSYS;
811         }
812 }