1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33#include <linux/fs.h>
34#include <linux/time.h>
35#include <linux/mm.h>
36#include <linux/highmem.h>
37#include <linux/kernel.h>
38#include <linux/pagemap.h>
39
40#include "vxfs.h"
41#include "vxfs_dir.h"
42#include "vxfs_inode.h"
43#include "vxfs_extern.h"
44
45
46
47
48#define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_CACHE_SIZE / (sbp)->s_blocksize))
49
50
51static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int);
52static int vxfs_readdir(struct file *, void *, filldir_t);
53
54const struct inode_operations vxfs_dir_inode_ops = {
55 .lookup = vxfs_lookup,
56};
57
58const struct file_operations vxfs_dir_operations = {
59 .llseek = generic_file_llseek,
60 .read = generic_read_dir,
61 .readdir = vxfs_readdir,
62};
63
64
65static inline u_long
66dir_pages(struct inode *inode)
67{
68 return (inode->i_size + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
69}
70
71static inline u_long
72dir_blocks(struct inode *ip)
73{
74 u_long bsize = ip->i_sb->s_blocksize;
75 return (ip->i_size + bsize - 1) & ~(bsize - 1);
76}
77
78
79
80
81
82
83static inline int
84vxfs_match(int len, const char * const name, struct vxfs_direct *de)
85{
86 if (len != de->d_namelen)
87 return 0;
88 if (!de->d_ino)
89 return 0;
90 return !memcmp(name, de->d_name, len);
91}
92
93static inline struct vxfs_direct *
94vxfs_next_entry(struct vxfs_direct *de)
95{
96 return ((struct vxfs_direct *)((char*)de + de->d_reclen));
97}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113static struct vxfs_direct *
114vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp)
115{
116 u_long npages, page, nblocks, pblocks, block;
117 u_long bsize = ip->i_sb->s_blocksize;
118 const char *name = dp->d_name.name;
119 int namelen = dp->d_name.len;
120
121 npages = dir_pages(ip);
122 nblocks = dir_blocks(ip);
123 pblocks = VXFS_BLOCK_PER_PAGE(ip->i_sb);
124
125 for (page = 0; page < npages; page++) {
126 caddr_t kaddr;
127 struct page *pp;
128
129 pp = vxfs_get_page(ip->i_mapping, page);
130 if (IS_ERR(pp))
131 continue;
132 kaddr = (caddr_t)page_address(pp);
133
134 for (block = 0; block <= nblocks && block <= pblocks; block++) {
135 caddr_t baddr, limit;
136 struct vxfs_dirblk *dbp;
137 struct vxfs_direct *de;
138
139 baddr = kaddr + (block * bsize);
140 limit = baddr + bsize - VXFS_DIRLEN(1);
141
142 dbp = (struct vxfs_dirblk *)baddr;
143 de = (struct vxfs_direct *)(baddr + VXFS_DIRBLKOV(dbp));
144
145 for (; (caddr_t)de <= limit; de = vxfs_next_entry(de)) {
146 if (!de->d_reclen)
147 break;
148 if (!de->d_ino)
149 continue;
150 if (vxfs_match(namelen, name, de)) {
151 *ppp = pp;
152 return (de);
153 }
154 }
155 }
156 vxfs_put_page(pp);
157 }
158
159 return NULL;
160}
161
162
163
164
165
166
167
168
169
170
171
172
173
174static ino_t
175vxfs_inode_by_name(struct inode *dip, struct dentry *dp)
176{
177 struct vxfs_direct *de;
178 struct page *pp;
179 ino_t ino = 0;
180
181 de = vxfs_find_entry(dip, dp, &pp);
182 if (de) {
183 ino = de->d_ino;
184 kunmap(pp);
185 page_cache_release(pp);
186 }
187
188 return (ino);
189}
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205static struct dentry *
206vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags)
207{
208 struct inode *ip = NULL;
209 ino_t ino;
210
211 if (dp->d_name.len > VXFS_NAMELEN)
212 return ERR_PTR(-ENAMETOOLONG);
213
214 ino = vxfs_inode_by_name(dip, dp);
215 if (ino) {
216 ip = vxfs_iget(dip->i_sb, ino);
217 if (IS_ERR(ip))
218 return ERR_CAST(ip);
219 }
220 d_add(dp, ip);
221 return NULL;
222}
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237static int
238vxfs_readdir(struct file *fp, void *retp, filldir_t filler)
239{
240 struct inode *ip = file_inode(fp);
241 struct super_block *sbp = ip->i_sb;
242 u_long bsize = sbp->s_blocksize;
243 u_long page, npages, block, pblocks, nblocks, offset;
244 loff_t pos;
245
246 switch ((long)fp->f_pos) {
247 case 0:
248 if (filler(retp, ".", 1, fp->f_pos, ip->i_ino, DT_DIR) < 0)
249 goto out;
250 fp->f_pos++;
251
252 case 1:
253 if (filler(retp, "..", 2, fp->f_pos, VXFS_INO(ip)->vii_dotdot, DT_DIR) < 0)
254 goto out;
255 fp->f_pos++;
256
257 }
258
259 pos = fp->f_pos - 2;
260
261 if (pos > VXFS_DIRROUND(ip->i_size))
262 return 0;
263
264 npages = dir_pages(ip);
265 nblocks = dir_blocks(ip);
266 pblocks = VXFS_BLOCK_PER_PAGE(sbp);
267
268 page = pos >> PAGE_CACHE_SHIFT;
269 offset = pos & ~PAGE_CACHE_MASK;
270 block = (u_long)(pos >> sbp->s_blocksize_bits) % pblocks;
271
272 for (; page < npages; page++, block = 0) {
273 caddr_t kaddr;
274 struct page *pp;
275
276 pp = vxfs_get_page(ip->i_mapping, page);
277 if (IS_ERR(pp))
278 continue;
279 kaddr = (caddr_t)page_address(pp);
280
281 for (; block <= nblocks && block <= pblocks; block++) {
282 caddr_t baddr, limit;
283 struct vxfs_dirblk *dbp;
284 struct vxfs_direct *de;
285
286 baddr = kaddr + (block * bsize);
287 limit = baddr + bsize - VXFS_DIRLEN(1);
288
289 dbp = (struct vxfs_dirblk *)baddr;
290 de = (struct vxfs_direct *)
291 (offset ?
292 (kaddr + offset) :
293 (baddr + VXFS_DIRBLKOV(dbp)));
294
295 for (; (caddr_t)de <= limit; de = vxfs_next_entry(de)) {
296 int over;
297
298 if (!de->d_reclen)
299 break;
300 if (!de->d_ino)
301 continue;
302
303 offset = (caddr_t)de - kaddr;
304 over = filler(retp, de->d_name, de->d_namelen,
305 ((page << PAGE_CACHE_SHIFT) | offset) + 2,
306 de->d_ino, DT_UNKNOWN);
307 if (over) {
308 vxfs_put_page(pp);
309 goto done;
310 }
311 }
312 offset = 0;
313 }
314 vxfs_put_page(pp);
315 offset = 0;
316 }
317
318done:
319 fp->f_pos = ((page << PAGE_CACHE_SHIFT) | offset) + 2;
320out:
321 return 0;
322}
323