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