autofs4: Clean up inode operations
[firefly-linux-kernel-4.4.55.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/slab.h>
19 #include <linux/param.h>
20 #include <linux/time.h>
21 #include <linux/compat.h>
22 #include <linux/mutex.h>
23
24 #include "autofs_i.h"
25
26 DEFINE_SPINLOCK(autofs4_lock);
27
28 static int autofs4_dir_symlink(struct inode *,struct dentry *,const char *);
29 static int autofs4_dir_unlink(struct inode *,struct dentry *);
30 static int autofs4_dir_rmdir(struct inode *,struct dentry *);
31 static int autofs4_dir_mkdir(struct inode *,struct dentry *,int);
32 static long autofs4_root_ioctl(struct file *,unsigned int,unsigned long);
33 #ifdef CONFIG_COMPAT
34 static long autofs4_root_compat_ioctl(struct file *,unsigned int,unsigned long);
35 #endif
36 static int autofs4_dir_open(struct inode *inode, struct file *file);
37 static struct dentry *autofs4_lookup(struct inode *,struct dentry *, struct nameidata *);
38
39 const struct file_operations autofs4_root_operations = {
40         .open           = dcache_dir_open,
41         .release        = dcache_dir_close,
42         .read           = generic_read_dir,
43         .readdir        = dcache_readdir,
44         .llseek         = dcache_dir_lseek,
45         .unlocked_ioctl = autofs4_root_ioctl,
46 #ifdef CONFIG_COMPAT
47         .compat_ioctl   = autofs4_root_compat_ioctl,
48 #endif
49 };
50
51 const struct file_operations autofs4_dir_operations = {
52         .open           = autofs4_dir_open,
53         .release        = dcache_dir_close,
54         .read           = generic_read_dir,
55         .readdir        = dcache_readdir,
56         .llseek         = dcache_dir_lseek,
57 };
58
59 const struct inode_operations autofs4_dir_inode_operations = {
60         .lookup         = autofs4_lookup,
61         .unlink         = autofs4_dir_unlink,
62         .symlink        = autofs4_dir_symlink,
63         .mkdir          = autofs4_dir_mkdir,
64         .rmdir          = autofs4_dir_rmdir,
65 };
66
67 static void autofs4_add_active(struct dentry *dentry)
68 {
69         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
70         struct autofs_info *ino = autofs4_dentry_ino(dentry);
71         if (ino) {
72                 spin_lock(&sbi->lookup_lock);
73                 if (!ino->active_count) {
74                         if (list_empty(&ino->active))
75                                 list_add(&ino->active, &sbi->active_list);
76                 }
77                 ino->active_count++;
78                 spin_unlock(&sbi->lookup_lock);
79         }
80         return;
81 }
82
83 static void autofs4_del_active(struct dentry *dentry)
84 {
85         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
86         struct autofs_info *ino = autofs4_dentry_ino(dentry);
87         if (ino) {
88                 spin_lock(&sbi->lookup_lock);
89                 ino->active_count--;
90                 if (!ino->active_count) {
91                         if (!list_empty(&ino->active))
92                                 list_del_init(&ino->active);
93                 }
94                 spin_unlock(&sbi->lookup_lock);
95         }
96         return;
97 }
98
99 static int autofs4_dir_open(struct inode *inode, struct file *file)
100 {
101         struct dentry *dentry = file->f_path.dentry;
102         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
103
104         DPRINTK("file=%p dentry=%p %.*s",
105                 file, dentry, dentry->d_name.len, dentry->d_name.name);
106
107         if (autofs4_oz_mode(sbi))
108                 goto out;
109
110         /*
111          * An empty directory in an autofs file system is always a
112          * mount point. The daemon must have failed to mount this
113          * during lookup so it doesn't exist. This can happen, for
114          * example, if user space returns an incorrect status for a
115          * mount request. Otherwise we're doing a readdir on the
116          * autofs file system so just let the libfs routines handle
117          * it.
118          */
119         spin_lock(&autofs4_lock);
120         spin_lock(&dentry->d_lock);
121         if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
122                 spin_unlock(&dentry->d_lock);
123                 spin_unlock(&autofs4_lock);
124                 return -ENOENT;
125         }
126         spin_unlock(&dentry->d_lock);
127         spin_unlock(&autofs4_lock);
128
129 out:
130         return dcache_dir_open(inode, file);
131 }
132
133 void autofs4_dentry_release(struct dentry *de)
134 {
135         struct autofs_info *inf;
136
137         DPRINTK("releasing %p", de);
138
139         inf = autofs4_dentry_ino(de);
140         de->d_fsdata = NULL;
141
142         if (inf) {
143                 struct autofs_sb_info *sbi = autofs4_sbi(de->d_sb);
144
145                 if (sbi) {
146                         spin_lock(&sbi->lookup_lock);
147                         if (!list_empty(&inf->active))
148                                 list_del(&inf->active);
149                         if (!list_empty(&inf->expiring))
150                                 list_del(&inf->expiring);
151                         spin_unlock(&sbi->lookup_lock);
152                 }
153
154                 inf->dentry = NULL;
155                 inf->inode = NULL;
156
157                 autofs4_free_ino(inf);
158         }
159 }
160
161 /* For dentries of directories in the root dir */
162 static const struct dentry_operations autofs4_root_dentry_operations = {
163         .d_release      = autofs4_dentry_release,
164 };
165
166 /* For other dentries */
167 static const struct dentry_operations autofs4_dentry_operations = {
168         .d_automount    = autofs4_d_automount,
169         .d_manage       = autofs4_d_manage,
170         .d_release      = autofs4_dentry_release,
171 };
172
173 static struct dentry *autofs4_lookup_active(struct dentry *dentry)
174 {
175         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
176         struct dentry *parent = dentry->d_parent;
177         struct qstr *name = &dentry->d_name;
178         unsigned int len = name->len;
179         unsigned int hash = name->hash;
180         const unsigned char *str = name->name;
181         struct list_head *p, *head;
182
183         spin_lock(&autofs4_lock);
184         spin_lock(&sbi->lookup_lock);
185         head = &sbi->active_list;
186         list_for_each(p, head) {
187                 struct autofs_info *ino;
188                 struct dentry *active;
189                 struct qstr *qstr;
190
191                 ino = list_entry(p, struct autofs_info, active);
192                 active = ino->dentry;
193
194                 spin_lock(&active->d_lock);
195
196                 /* Already gone? */
197                 if (active->d_count == 0)
198                         goto next;
199
200                 qstr = &active->d_name;
201
202                 if (active->d_name.hash != hash)
203                         goto next;
204                 if (active->d_parent != parent)
205                         goto next;
206
207                 if (qstr->len != len)
208                         goto next;
209                 if (memcmp(qstr->name, str, len))
210                         goto next;
211
212                 if (d_unhashed(active)) {
213                         dget_dlock(active);
214                         spin_unlock(&active->d_lock);
215                         spin_unlock(&sbi->lookup_lock);
216                         spin_unlock(&autofs4_lock);
217                         return active;
218                 }
219 next:
220                 spin_unlock(&active->d_lock);
221         }
222         spin_unlock(&sbi->lookup_lock);
223         spin_unlock(&autofs4_lock);
224
225         return NULL;
226 }
227
228 static struct dentry *autofs4_lookup_expiring(struct dentry *dentry)
229 {
230         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
231         struct dentry *parent = dentry->d_parent;
232         struct qstr *name = &dentry->d_name;
233         unsigned int len = name->len;
234         unsigned int hash = name->hash;
235         const unsigned char *str = name->name;
236         struct list_head *p, *head;
237
238         spin_lock(&autofs4_lock);
239         spin_lock(&sbi->lookup_lock);
240         head = &sbi->expiring_list;
241         list_for_each(p, head) {
242                 struct autofs_info *ino;
243                 struct dentry *expiring;
244                 struct qstr *qstr;
245
246                 ino = list_entry(p, struct autofs_info, expiring);
247                 expiring = ino->dentry;
248
249                 spin_lock(&expiring->d_lock);
250
251                 /* Bad luck, we've already been dentry_iput */
252                 if (!expiring->d_inode)
253                         goto next;
254
255                 qstr = &expiring->d_name;
256
257                 if (expiring->d_name.hash != hash)
258                         goto next;
259                 if (expiring->d_parent != parent)
260                         goto next;
261
262                 if (qstr->len != len)
263                         goto next;
264                 if (memcmp(qstr->name, str, len))
265                         goto next;
266
267                 if (d_unhashed(expiring)) {
268                         dget_dlock(expiring);
269                         spin_unlock(&expiring->d_lock);
270                         spin_unlock(&sbi->lookup_lock);
271                         spin_unlock(&autofs4_lock);
272                         return expiring;
273                 }
274 next:
275                 spin_unlock(&expiring->d_lock);
276         }
277         spin_unlock(&sbi->lookup_lock);
278         spin_unlock(&autofs4_lock);
279
280         return NULL;
281 }
282
283 static int autofs4_mount_wait(struct dentry *dentry)
284 {
285         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
286         struct autofs_info *ino = autofs4_dentry_ino(dentry);
287         int status;
288
289         if (ino->flags & AUTOFS_INF_PENDING) {
290                 DPRINTK("waiting for mount name=%.*s",
291                         dentry->d_name.len, dentry->d_name.name);
292                 status = autofs4_wait(sbi, dentry, NFY_MOUNT);
293                 DPRINTK("mount wait done status=%d", status);
294                 ino->last_used = jiffies;
295                 return status;
296         }
297         return 0;
298 }
299
300 static int do_expire_wait(struct dentry *dentry)
301 {
302         struct dentry *expiring;
303
304         expiring = autofs4_lookup_expiring(dentry);
305         if (!expiring)
306                 return autofs4_expire_wait(dentry);
307         else {
308                 /*
309                  * If we are racing with expire the request might not
310                  * be quite complete, but the directory has been removed
311                  * so it must have been successful, just wait for it.
312                  */
313                 autofs4_expire_wait(expiring);
314                 autofs4_del_expiring(expiring);
315                 dput(expiring);
316         }
317         return 0;
318 }
319
320 static struct dentry *autofs4_mountpoint_changed(struct path *path)
321 {
322         struct dentry *dentry = path->dentry;
323         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
324
325         /*
326          * If this is an indirect mount the dentry could have gone away
327          * as a result of an expire and a new one created.
328          */
329         if (autofs_type_indirect(sbi->type) && d_unhashed(dentry)) {
330                 struct dentry *parent = dentry->d_parent;
331                 struct dentry *new = d_lookup(parent, &dentry->d_name);
332                 if (!new)
333                         return NULL;
334                 dput(path->dentry);
335                 path->dentry = new;
336         }
337         return path->dentry;
338 }
339
340 struct vfsmount *autofs4_d_automount(struct path *path)
341 {
342         struct dentry *dentry = path->dentry;
343         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
344         struct autofs_info *ino = autofs4_dentry_ino(dentry);
345         int status;
346
347         DPRINTK("dentry=%p %.*s",
348                 dentry, dentry->d_name.len, dentry->d_name.name);
349
350         /*
351          * Someone may have manually umounted this or it was a submount
352          * that has gone away.
353          */
354         spin_lock(&dentry->d_lock);
355         if (!d_mountpoint(dentry) && list_empty(&dentry->d_subdirs)) {
356                 if (!(dentry->d_flags & DCACHE_MANAGE_TRANSIT) &&
357                      (dentry->d_flags & DCACHE_NEED_AUTOMOUNT))
358                         __managed_dentry_set_transit(path->dentry);
359         }
360         spin_unlock(&dentry->d_lock);
361
362         /* The daemon never triggers a mount. */
363         if (autofs4_oz_mode(sbi))
364                 return NULL;
365
366         /*
367          * If an expire request is pending everyone must wait.
368          * If the expire fails we're still mounted so continue
369          * the follow and return. A return of -EAGAIN (which only
370          * happens with indirect mounts) means the expire completed
371          * and the directory was removed, so just go ahead and try
372          * the mount.
373          */
374         status = do_expire_wait(dentry);
375         if (status && status != -EAGAIN)
376                 return NULL;
377
378         /* Callback to the daemon to perform the mount or wait */
379         spin_lock(&sbi->fs_lock);
380         if (ino->flags & AUTOFS_INF_PENDING) {
381                 spin_unlock(&sbi->fs_lock);
382                 status = autofs4_mount_wait(dentry);
383                 if (status)
384                         return ERR_PTR(status);
385                 spin_lock(&sbi->fs_lock);
386                 goto done;
387         }
388
389         /*
390          * If the dentry is a symlink it's equivalent to a directory
391          * having d_mountpoint() true, so there's no need to call back
392          * to the daemon.
393          */
394         if (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode))
395                 goto done;
396         if (!d_mountpoint(dentry)) {
397                 /*
398                  * It's possible that user space hasn't removed directories
399                  * after umounting a rootless multi-mount, although it
400                  * should. For v5 have_submounts() is sufficient to handle
401                  * this because the leaves of the directory tree under the
402                  * mount never trigger mounts themselves (they have an autofs
403                  * trigger mount mounted on them). But v4 pseudo direct mounts
404                  * do need the leaves to to trigger mounts. In this case we
405                  * have no choice but to use the list_empty() check and
406                  * require user space behave.
407                  */
408                 if (sbi->version > 4) {
409                         if (have_submounts(dentry))
410                                 goto done;
411                 } else {
412                         spin_lock(&dentry->d_lock);
413                         if (!list_empty(&dentry->d_subdirs)) {
414                                 spin_unlock(&dentry->d_lock);
415                                 goto done;
416                         }
417                         spin_unlock(&dentry->d_lock);
418                 }
419                 ino->flags |= AUTOFS_INF_PENDING;
420                 spin_unlock(&sbi->fs_lock);
421                 status = autofs4_mount_wait(dentry);
422                 if (status)
423                         return ERR_PTR(status);
424                 spin_lock(&sbi->fs_lock);
425                 ino->flags &= ~AUTOFS_INF_PENDING;
426         }
427 done:
428         if (!(ino->flags & AUTOFS_INF_EXPIRING)) {
429                 /*
430                  * Any needed mounting has been completed and the path updated
431                  * so turn this into a normal dentry so we don't continually
432                  * call ->d_automount() and ->d_manage().
433                  */
434                 spin_lock(&dentry->d_lock);
435                 __managed_dentry_clear_transit(dentry);
436                 /*
437                  * Only clear DMANAGED_AUTOMOUNT for rootless multi-mounts and
438                  * symlinks as in all other cases the dentry will be covered by
439                  * an actual mount so ->d_automount() won't be called during
440                  * the follow.
441                  */
442                 if ((!d_mountpoint(dentry) &&
443                     !list_empty(&dentry->d_subdirs)) ||
444                     (dentry->d_inode && S_ISLNK(dentry->d_inode->i_mode)))
445                         __managed_dentry_clear_automount(dentry);
446                 spin_unlock(&dentry->d_lock);
447         }
448         spin_unlock(&sbi->fs_lock);
449
450         /* Mount succeeded, check if we ended up with a new dentry */
451         dentry = autofs4_mountpoint_changed(path);
452         if (!dentry)
453                 return ERR_PTR(-ENOENT);
454
455         return NULL;
456 }
457
458 int autofs4_d_manage(struct dentry *dentry, bool mounting_here)
459 {
460         struct autofs_sb_info *sbi = autofs4_sbi(dentry->d_sb);
461
462         DPRINTK("dentry=%p %.*s",
463                 dentry, dentry->d_name.len, dentry->d_name.name);
464
465         /* The daemon never waits. */
466         if (autofs4_oz_mode(sbi) || mounting_here) {
467                 if (!d_mountpoint(dentry))
468                         return -EISDIR;
469                 return 0;
470         }
471
472         /* Wait for pending expires */
473         do_expire_wait(dentry);
474
475         /*
476          * This dentry may be under construction so wait on mount
477          * completion.
478          */
479         return autofs4_mount_wait(dentry);
480 }
481
482 /* Lookups in the root directory */
483 static struct dentry *autofs4_lookup(struct inode *dir, struct dentry *dentry, struct nameidata *nd)
484 {
485         struct autofs_sb_info *sbi;
486         struct autofs_info *ino;
487         struct dentry *active;
488
489         DPRINTK("name = %.*s", dentry->d_name.len, dentry->d_name.name);
490
491         /* File name too long to exist */
492         if (dentry->d_name.len > NAME_MAX)
493                 return ERR_PTR(-ENAMETOOLONG);
494
495         sbi = autofs4_sbi(dir->i_sb);
496
497         DPRINTK("pid = %u, pgrp = %u, catatonic = %d, oz_mode = %d",
498                 current->pid, task_pgrp_nr(current), sbi->catatonic, oz_mode);
499
500         active = autofs4_lookup_active(dentry);
501         if (active) {
502                 return active;
503         } else {
504                 d_set_d_op(dentry, &autofs4_root_dentry_operations);
505
506                 /*
507                  * A dentry that is not within the root can never trigger a
508                  * mount operation, unless the directory already exists, so we
509                  * can return fail immediately.  The daemon however does need
510                  * to create directories within the file system.
511                  */
512                 if (!autofs4_oz_mode(sbi) && !IS_ROOT(dentry->d_parent))
513                         return ERR_PTR(-ENOENT);
514
515                 /* Mark entries in the root as mount triggers */
516                 if (autofs_type_indirect(sbi->type) && IS_ROOT(dentry->d_parent)) {
517                         d_set_d_op(dentry, &autofs4_dentry_operations);
518                         __managed_dentry_set_managed(dentry);
519                 }
520
521                 ino = autofs4_init_ino(NULL, sbi, 0555);
522                 if (!ino)
523                         return ERR_PTR(-ENOMEM);
524
525                 dentry->d_fsdata = ino;
526                 ino->dentry = dentry;
527
528                 autofs4_add_active(dentry);
529
530                 d_instantiate(dentry, NULL);
531         }
532         return NULL;
533 }
534
535 static int autofs4_dir_symlink(struct inode *dir, 
536                                struct dentry *dentry,
537                                const char *symname)
538 {
539         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
540         struct autofs_info *ino = autofs4_dentry_ino(dentry);
541         struct autofs_info *p_ino;
542         struct inode *inode;
543         char *cp;
544
545         DPRINTK("%s <- %.*s", symname,
546                 dentry->d_name.len, dentry->d_name.name);
547
548         if (!autofs4_oz_mode(sbi))
549                 return -EACCES;
550
551         ino = autofs4_init_ino(ino, sbi, S_IFLNK | 0555);
552         if (!ino)
553                 return -ENOMEM;
554
555         autofs4_del_active(dentry);
556
557         ino->size = strlen(symname);
558         cp = kmalloc(ino->size + 1, GFP_KERNEL);
559         if (!cp) {
560                 if (!dentry->d_fsdata)
561                         kfree(ino);
562                 return -ENOMEM;
563         }
564
565         strcpy(cp, symname);
566
567         inode = autofs4_get_inode(dir->i_sb, ino);
568         if (!inode) {
569                 kfree(cp);
570                 if (!dentry->d_fsdata)
571                         kfree(ino);
572                 return -ENOMEM;
573         }
574         d_add(dentry, inode);
575
576         dentry->d_fsdata = ino;
577         ino->dentry = dget(dentry);
578         atomic_inc(&ino->count);
579         p_ino = autofs4_dentry_ino(dentry->d_parent);
580         if (p_ino && dentry->d_parent != dentry)
581                 atomic_inc(&p_ino->count);
582         ino->inode = inode;
583
584         ino->u.symlink = cp;
585         dir->i_mtime = CURRENT_TIME;
586
587         return 0;
588 }
589
590 /*
591  * NOTE!
592  *
593  * Normal filesystems would do a "d_delete()" to tell the VFS dcache
594  * that the file no longer exists. However, doing that means that the
595  * VFS layer can turn the dentry into a negative dentry.  We don't want
596  * this, because the unlink is probably the result of an expire.
597  * We simply d_drop it and add it to a expiring list in the super block,
598  * which allows the dentry lookup to check for an incomplete expire.
599  *
600  * If a process is blocked on the dentry waiting for the expire to finish,
601  * it will invalidate the dentry and try to mount with a new one.
602  *
603  * Also see autofs4_dir_rmdir()..
604  */
605 static int autofs4_dir_unlink(struct inode *dir, struct dentry *dentry)
606 {
607         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
608         struct autofs_info *ino = autofs4_dentry_ino(dentry);
609         struct autofs_info *p_ino;
610         
611         /* This allows root to remove symlinks */
612         if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
613                 return -EACCES;
614
615         if (atomic_dec_and_test(&ino->count)) {
616                 p_ino = autofs4_dentry_ino(dentry->d_parent);
617                 if (p_ino && dentry->d_parent != dentry)
618                         atomic_dec(&p_ino->count);
619         }
620         dput(ino->dentry);
621
622         dentry->d_inode->i_size = 0;
623         clear_nlink(dentry->d_inode);
624
625         dir->i_mtime = CURRENT_TIME;
626
627         spin_lock(&autofs4_lock);
628         autofs4_add_expiring(dentry);
629         spin_lock(&dentry->d_lock);
630         __d_drop(dentry);
631         spin_unlock(&dentry->d_lock);
632         spin_unlock(&autofs4_lock);
633
634         return 0;
635 }
636
637 static int autofs4_dir_rmdir(struct inode *dir, struct dentry *dentry)
638 {
639         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
640         struct autofs_info *ino = autofs4_dentry_ino(dentry);
641         struct autofs_info *p_ino;
642         
643         DPRINTK("dentry %p, removing %.*s",
644                 dentry, dentry->d_name.len, dentry->d_name.name);
645
646         if (!autofs4_oz_mode(sbi))
647                 return -EACCES;
648
649         spin_lock(&autofs4_lock);
650         spin_lock(&sbi->lookup_lock);
651         spin_lock(&dentry->d_lock);
652         if (!list_empty(&dentry->d_subdirs)) {
653                 spin_unlock(&dentry->d_lock);
654                 spin_unlock(&sbi->lookup_lock);
655                 spin_unlock(&autofs4_lock);
656                 return -ENOTEMPTY;
657         }
658         __autofs4_add_expiring(dentry);
659         spin_unlock(&sbi->lookup_lock);
660         __d_drop(dentry);
661         spin_unlock(&dentry->d_lock);
662         spin_unlock(&autofs4_lock);
663
664         if (atomic_dec_and_test(&ino->count)) {
665                 p_ino = autofs4_dentry_ino(dentry->d_parent);
666                 if (p_ino && dentry->d_parent != dentry)
667                         atomic_dec(&p_ino->count);
668         }
669         dput(ino->dentry);
670         dentry->d_inode->i_size = 0;
671         clear_nlink(dentry->d_inode);
672
673         if (dir->i_nlink)
674                 drop_nlink(dir);
675
676         return 0;
677 }
678
679 static int autofs4_dir_mkdir(struct inode *dir, struct dentry *dentry, int mode)
680 {
681         struct autofs_sb_info *sbi = autofs4_sbi(dir->i_sb);
682         struct autofs_info *ino = autofs4_dentry_ino(dentry);
683         struct autofs_info *p_ino;
684         struct inode *inode;
685
686         if (!autofs4_oz_mode(sbi))
687                 return -EACCES;
688
689         DPRINTK("dentry %p, creating %.*s",
690                 dentry, dentry->d_name.len, dentry->d_name.name);
691
692         ino = autofs4_init_ino(ino, sbi, S_IFDIR | 0555);
693         if (!ino)
694                 return -ENOMEM;
695
696         autofs4_del_active(dentry);
697
698         inode = autofs4_get_inode(dir->i_sb, ino);
699         if (!inode) {
700                 if (!dentry->d_fsdata)
701                         kfree(ino);
702                 return -ENOMEM;
703         }
704         d_add(dentry, inode);
705
706         dentry->d_fsdata = ino;
707         ino->dentry = dget(dentry);
708         atomic_inc(&ino->count);
709         p_ino = autofs4_dentry_ino(dentry->d_parent);
710         if (p_ino && dentry->d_parent != dentry)
711                 atomic_inc(&p_ino->count);
712         ino->inode = inode;
713         inc_nlink(dir);
714         dir->i_mtime = CURRENT_TIME;
715
716         return 0;
717 }
718
719 /* Get/set timeout ioctl() operation */
720 #ifdef CONFIG_COMPAT
721 static inline int autofs4_compat_get_set_timeout(struct autofs_sb_info *sbi,
722                                          compat_ulong_t __user *p)
723 {
724         int rv;
725         unsigned long ntimeout;
726
727         if ((rv = get_user(ntimeout, p)) ||
728              (rv = put_user(sbi->exp_timeout/HZ, p)))
729                 return rv;
730
731         if (ntimeout > UINT_MAX/HZ)
732                 sbi->exp_timeout = 0;
733         else
734                 sbi->exp_timeout = ntimeout * HZ;
735
736         return 0;
737 }
738 #endif
739
740 static inline int autofs4_get_set_timeout(struct autofs_sb_info *sbi,
741                                          unsigned long __user *p)
742 {
743         int rv;
744         unsigned long ntimeout;
745
746         if ((rv = get_user(ntimeout, p)) ||
747              (rv = put_user(sbi->exp_timeout/HZ, p)))
748                 return rv;
749
750         if (ntimeout > ULONG_MAX/HZ)
751                 sbi->exp_timeout = 0;
752         else
753                 sbi->exp_timeout = ntimeout * HZ;
754
755         return 0;
756 }
757
758 /* Return protocol version */
759 static inline int autofs4_get_protover(struct autofs_sb_info *sbi, int __user *p)
760 {
761         return put_user(sbi->version, p);
762 }
763
764 /* Return protocol sub version */
765 static inline int autofs4_get_protosubver(struct autofs_sb_info *sbi, int __user *p)
766 {
767         return put_user(sbi->sub_version, p);
768 }
769
770 /*
771 * Tells the daemon whether it can umount the autofs mount.
772 */
773 static inline int autofs4_ask_umount(struct vfsmount *mnt, int __user *p)
774 {
775         int status = 0;
776
777         if (may_umount(mnt))
778                 status = 1;
779
780         DPRINTK("returning %d", status);
781
782         status = put_user(status, p);
783
784         return status;
785 }
786
787 /* Identify autofs4_dentries - this is so we can tell if there's
788    an extra dentry refcount or not.  We only hold a refcount on the
789    dentry if its non-negative (ie, d_inode != NULL)
790 */
791 int is_autofs4_dentry(struct dentry *dentry)
792 {
793         return dentry && dentry->d_inode &&
794                 (dentry->d_op == &autofs4_root_dentry_operations ||
795                  dentry->d_op == &autofs4_dentry_operations) &&
796                 dentry->d_fsdata != NULL;
797 }
798
799 /*
800  * ioctl()'s on the root directory is the chief method for the daemon to
801  * generate kernel reactions
802  */
803 static int autofs4_root_ioctl_unlocked(struct inode *inode, struct file *filp,
804                                        unsigned int cmd, unsigned long arg)
805 {
806         struct autofs_sb_info *sbi = autofs4_sbi(inode->i_sb);
807         void __user *p = (void __user *)arg;
808
809         DPRINTK("cmd = 0x%08x, arg = 0x%08lx, sbi = %p, pgrp = %u",
810                 cmd,arg,sbi,task_pgrp_nr(current));
811
812         if (_IOC_TYPE(cmd) != _IOC_TYPE(AUTOFS_IOC_FIRST) ||
813              _IOC_NR(cmd) - _IOC_NR(AUTOFS_IOC_FIRST) >= AUTOFS_IOC_COUNT)
814                 return -ENOTTY;
815         
816         if (!autofs4_oz_mode(sbi) && !capable(CAP_SYS_ADMIN))
817                 return -EPERM;
818         
819         switch(cmd) {
820         case AUTOFS_IOC_READY:  /* Wait queue: go ahead and retry */
821                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,0);
822         case AUTOFS_IOC_FAIL:   /* Wait queue: fail with ENOENT */
823                 return autofs4_wait_release(sbi,(autofs_wqt_t)arg,-ENOENT);
824         case AUTOFS_IOC_CATATONIC: /* Enter catatonic mode (daemon shutdown) */
825                 autofs4_catatonic_mode(sbi);
826                 return 0;
827         case AUTOFS_IOC_PROTOVER: /* Get protocol version */
828                 return autofs4_get_protover(sbi, p);
829         case AUTOFS_IOC_PROTOSUBVER: /* Get protocol sub version */
830                 return autofs4_get_protosubver(sbi, p);
831         case AUTOFS_IOC_SETTIMEOUT:
832                 return autofs4_get_set_timeout(sbi, p);
833 #ifdef CONFIG_COMPAT
834         case AUTOFS_IOC_SETTIMEOUT32:
835                 return autofs4_compat_get_set_timeout(sbi, p);
836 #endif
837
838         case AUTOFS_IOC_ASKUMOUNT:
839                 return autofs4_ask_umount(filp->f_path.mnt, p);
840
841         /* return a single thing to expire */
842         case AUTOFS_IOC_EXPIRE:
843                 return autofs4_expire_run(inode->i_sb,filp->f_path.mnt,sbi, p);
844         /* same as above, but can send multiple expires through pipe */
845         case AUTOFS_IOC_EXPIRE_MULTI:
846                 return autofs4_expire_multi(inode->i_sb,filp->f_path.mnt,sbi, p);
847
848         default:
849                 return -ENOSYS;
850         }
851 }
852
853 static long autofs4_root_ioctl(struct file *filp,
854                                unsigned int cmd, unsigned long arg)
855 {
856         struct inode *inode = filp->f_dentry->d_inode;
857         return autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
858 }
859
860 #ifdef CONFIG_COMPAT
861 static long autofs4_root_compat_ioctl(struct file *filp,
862                              unsigned int cmd, unsigned long arg)
863 {
864         struct inode *inode = filp->f_path.dentry->d_inode;
865         int ret;
866
867         if (cmd == AUTOFS_IOC_READY || cmd == AUTOFS_IOC_FAIL)
868                 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd, arg);
869         else
870                 ret = autofs4_root_ioctl_unlocked(inode, filp, cmd,
871                         (unsigned long)compat_ptr(arg));
872
873         return ret;
874 }
875 #endif