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