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