nfsd4: check for negative dentry before use in nfsv4 readdir
[safe/jmp/linux-2.6] / fs / autofs / dirhash.c
1 /* -*- linux-c -*- --------------------------------------------------------- *
2  *
3  * linux/fs/autofs/dirhash.c
4  *
5  *  Copyright 1997-1998 Transmeta Corporation -- All Rights Reserved
6  *
7  * This file is part of the Linux kernel and is made available under
8  * the terms of the GNU General Public License, version 2, or at your
9  * option, any later version, incorporated herein by reference.
10  *
11  * ------------------------------------------------------------------------- */
12
13 #include "autofs_i.h"
14
15 /* Functions for maintenance of expiry queue */
16
17 static void autofs_init_usage(struct autofs_dirhash *dh,
18                               struct autofs_dir_ent *ent)
19 {
20         list_add_tail(&ent->exp, &dh->expiry_head);
21         ent->last_usage = jiffies;
22 }
23
24 static void autofs_delete_usage(struct autofs_dir_ent *ent)
25 {
26         list_del(&ent->exp);
27 }
28
29 void autofs_update_usage(struct autofs_dirhash *dh,
30                          struct autofs_dir_ent *ent)
31 {
32         autofs_delete_usage(ent);   /* Unlink from current position */
33         autofs_init_usage(dh,ent);  /* Relink at queue tail */
34 }
35
36 struct autofs_dir_ent *autofs_expire(struct super_block *sb,
37                                      struct autofs_sb_info *sbi,
38                                      struct vfsmount *mnt)
39 {
40         struct autofs_dirhash *dh = &sbi->dirhash;
41         struct autofs_dir_ent *ent;
42         unsigned long timeout = sbi->exp_timeout;
43
44         while (1) {
45                 struct path path;
46                 int umount_ok;
47
48                 if ( list_empty(&dh->expiry_head) || sbi->catatonic )
49                         return NULL;    /* No entries */
50                 /* We keep the list sorted by last_usage and want old stuff */
51                 ent = list_entry(dh->expiry_head.next, struct autofs_dir_ent, exp);
52                 if (jiffies - ent->last_usage < timeout)
53                         break;
54                 /* Move to end of list in case expiry isn't desirable */
55                 autofs_update_usage(dh, ent);
56
57                 /* Check to see that entry is expirable */
58                 if ( ent->ino < AUTOFS_FIRST_DIR_INO )
59                         return ent; /* Symlinks are always expirable */
60
61                 /* Get the dentry for the autofs subdirectory */
62                 path.dentry = ent->dentry;
63
64                 if (!path.dentry) {
65                         /* Should only happen in catatonic mode */
66                         printk("autofs: dentry == NULL but inode range is directory, entry %s\n", ent->name);
67                         autofs_delete_usage(ent);
68                         continue;
69                 }
70
71                 if (!path.dentry->d_inode) {
72                         dput(path.dentry);
73                         printk("autofs: negative dentry on expiry queue: %s\n",
74                                ent->name);
75                         autofs_delete_usage(ent);
76                         continue;
77                 }
78
79                 /* Make sure entry is mounted and unused; note that dentry will
80                    point to the mounted-on-top root. */
81                 if (!S_ISDIR(path.dentry->d_inode->i_mode) ||
82                     !d_mountpoint(path.dentry)) {
83                         DPRINTK(("autofs: not expirable (not a mounted directory): %s\n", ent->name));
84                         continue;
85                 }
86                 path.mnt = mnt;
87                 path_get(&path);
88                 if (!follow_down(&path.mnt, &path.dentry)) {
89                         path_put(&path);
90                         DPRINTK(("autofs: not expirable (not a mounted directory): %s\n", ent->name));
91                         continue;
92                 }
93                 while (d_mountpoint(path.dentry) &&
94                        follow_down(&path.mnt, &path.dentry))
95                         ;
96                 umount_ok = may_umount(path.mnt);
97                 path_put(&path);
98
99                 if (umount_ok) {
100                         DPRINTK(("autofs: signaling expire on %s\n", ent->name));
101                         return ent; /* Expirable! */
102                 }
103                 DPRINTK(("autofs: didn't expire due to may_umount: %s\n", ent->name));
104         }
105         return NULL;            /* No expirable entries */
106 }
107
108 void autofs_initialize_hash(struct autofs_dirhash *dh) {
109         memset(&dh->h, 0, AUTOFS_HASH_SIZE*sizeof(struct autofs_dir_ent *));
110         INIT_LIST_HEAD(&dh->expiry_head);
111 }
112
113 struct autofs_dir_ent *autofs_hash_lookup(const struct autofs_dirhash *dh, struct qstr *name)
114 {
115         struct autofs_dir_ent *dhn;
116
117         DPRINTK(("autofs_hash_lookup: hash = 0x%08x, name = ", name->hash));
118         autofs_say(name->name,name->len);
119
120         for ( dhn = dh->h[(unsigned) name->hash % AUTOFS_HASH_SIZE] ; dhn ; dhn = dhn->next ) {
121                 if ( name->hash == dhn->hash &&
122                      name->len == dhn->len &&
123                      !memcmp(name->name, dhn->name, name->len) )
124                         break;
125         }
126
127         return dhn;
128 }
129
130 void autofs_hash_insert(struct autofs_dirhash *dh, struct autofs_dir_ent *ent)
131 {
132         struct autofs_dir_ent **dhnp;
133
134         DPRINTK(("autofs_hash_insert: hash = 0x%08x, name = ", ent->hash));
135         autofs_say(ent->name,ent->len);
136
137         autofs_init_usage(dh,ent);
138         if (ent->dentry)
139                 dget(ent->dentry);
140
141         dhnp = &dh->h[(unsigned) ent->hash % AUTOFS_HASH_SIZE];
142         ent->next = *dhnp;
143         ent->back = dhnp;
144         *dhnp = ent;
145         if ( ent->next )
146                 ent->next->back = &(ent->next);
147 }
148
149 void autofs_hash_delete(struct autofs_dir_ent *ent)
150 {
151         *(ent->back) = ent->next;
152         if ( ent->next )
153                 ent->next->back = ent->back;
154
155         autofs_delete_usage(ent);
156
157         if ( ent->dentry )
158                 dput(ent->dentry);
159         kfree(ent->name);
160         kfree(ent);
161 }
162
163 /*
164  * Used by readdir().  We must validate "ptr", so we can't simply make it
165  * a pointer.  Values below 0xffff are reserved; calling with any value
166  * <= 0x10000 will return the first entry found.
167  *
168  * "last" can be NULL or the value returned by the last search *if* we
169  * want the next sequential entry.
170  */
171 struct autofs_dir_ent *autofs_hash_enum(const struct autofs_dirhash *dh,
172                                         off_t *ptr, struct autofs_dir_ent *last)
173 {
174         int bucket, ecount, i;
175         struct autofs_dir_ent *ent;
176
177         bucket = (*ptr >> 16) - 1;
178         ecount = *ptr & 0xffff;
179
180         if ( bucket < 0 ) {
181                 bucket = ecount = 0;
182         } 
183
184         DPRINTK(("autofs_hash_enum: bucket %d, entry %d\n", bucket, ecount));
185
186         ent = last ? last->next : NULL;
187
188         if ( ent ) {
189                 ecount++;
190         } else {
191                 while  ( bucket < AUTOFS_HASH_SIZE ) {
192                         ent = dh->h[bucket];
193                         for ( i = ecount ; ent && i ; i-- )
194                                 ent = ent->next;
195                         
196                         if (ent) {
197                                 ecount++; /* Point to *next* entry */
198                                 break;
199                         }
200                         
201                         bucket++; ecount = 0;
202                 }
203         }
204
205 #ifdef DEBUG
206         if ( !ent )
207                 printk("autofs_hash_enum: nothing found\n");
208         else {
209                 printk("autofs_hash_enum: found hash %08x, name", ent->hash);
210                 autofs_say(ent->name,ent->len);
211         }
212 #endif
213
214         *ptr = ((bucket+1) << 16) + ecount;
215         return ent;
216 }
217
218 /* Iterate over all the ents, and remove all dentry pointers.  Used on
219    entering catatonic mode, in order to make the filesystem unmountable. */
220 void autofs_hash_dputall(struct autofs_dirhash *dh)
221 {
222         int i;
223         struct autofs_dir_ent *ent;
224
225         for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) {
226                 for ( ent = dh->h[i] ; ent ; ent = ent->next ) {
227                         if ( ent->dentry ) {
228                                 dput(ent->dentry);
229                                 ent->dentry = NULL;
230                         }
231                 }
232         }
233 }
234
235 /* Delete everything.  This is used on filesystem destruction, so we
236    make no attempt to keep the pointers valid */
237 void autofs_hash_nuke(struct autofs_sb_info *sbi)
238 {
239         int i;
240         struct autofs_dir_ent *ent, *nent;
241
242         for ( i = 0 ; i < AUTOFS_HASH_SIZE ; i++ ) {
243                 for ( ent = sbi->dirhash.h[i] ; ent ; ent = nent ) {
244                         nent = ent->next;
245                         if ( ent->dentry )
246                                 dput(ent->dentry);
247                         kfree(ent->name);
248                         kfree(ent);
249                 }
250         }
251 }