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