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