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