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