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