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