]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) 2000-2001 Christoph Hellwig. | |
1cce1701 | 3 | * Copyright (c) 2016 Krzysztof Blaszkowski |
1da177e4 LT |
4 | * All rights reserved. |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions, and the following disclaimer, | |
11 | * without modification. | |
12 | * 2. The name of the author may not be used to endorse or promote products | |
13 | * derived from this software without specific prior written permission. | |
14 | * | |
15 | * Alternatively, this software may be distributed under the terms of the | |
16 | * GNU General Public License ("GPL"). | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND | |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR | |
22 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 | * SUCH DAMAGE. | |
29 | */ | |
30 | ||
31 | /* | |
32 | * Veritas filesystem driver - lookup and other directory related code. | |
33 | */ | |
34 | #include <linux/fs.h> | |
35 | #include <linux/time.h> | |
36 | #include <linux/mm.h> | |
37 | #include <linux/highmem.h> | |
38 | #include <linux/kernel.h> | |
39 | #include <linux/pagemap.h> | |
1da177e4 LT |
40 | |
41 | #include "vxfs.h" | |
42 | #include "vxfs_dir.h" | |
43 | #include "vxfs_inode.h" | |
44 | #include "vxfs_extern.h" | |
45 | ||
46 | /* | |
47 | * Number of VxFS blocks per page. | |
48 | */ | |
09cbfeaf | 49 | #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize)) |
1da177e4 LT |
50 | |
51 | ||
00cd8dd3 | 52 | static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int); |
9b5d5a17 | 53 | static int vxfs_readdir(struct file *, struct dir_context *); |
1da177e4 | 54 | |
754661f1 | 55 | const struct inode_operations vxfs_dir_inode_ops = { |
1da177e4 LT |
56 | .lookup = vxfs_lookup, |
57 | }; | |
58 | ||
4b6f5d20 | 59 | const struct file_operations vxfs_dir_operations = { |
ca572727 B |
60 | .llseek = generic_file_llseek, |
61 | .read = generic_read_dir, | |
c51da20c | 62 | .iterate_shared = vxfs_readdir, |
1da177e4 LT |
63 | }; |
64 | ||
8cb681b9 | 65 | static inline u_long |
1da177e4 LT |
66 | dir_blocks(struct inode *ip) |
67 | { | |
68 | u_long bsize = ip->i_sb->s_blocksize; | |
69 | return (ip->i_size + bsize - 1) & ~(bsize - 1); | |
70 | } | |
71 | ||
72 | /* | |
73 | * NOTE! unlike strncmp, vxfs_match returns 1 for success, 0 for failure. | |
74 | * | |
75 | * len <= VXFS_NAMELEN and de != NULL are guaranteed by caller. | |
76 | */ | |
8cb681b9 | 77 | static inline int |
0d83f7fc KB |
78 | vxfs_match(struct vxfs_sb_info *sbi, int len, const char *const name, |
79 | struct vxfs_direct *de) | |
1da177e4 | 80 | { |
0d83f7fc | 81 | if (len != fs16_to_cpu(sbi, de->d_namelen)) |
1da177e4 LT |
82 | return 0; |
83 | if (!de->d_ino) | |
84 | return 0; | |
85 | return !memcmp(name, de->d_name, len); | |
86 | } | |
87 | ||
8cb681b9 | 88 | static inline struct vxfs_direct * |
0d83f7fc | 89 | vxfs_next_entry(struct vxfs_sb_info *sbi, struct vxfs_direct *de) |
1da177e4 | 90 | { |
0d83f7fc KB |
91 | return ((struct vxfs_direct *) |
92 | ((char *)de + fs16_to_cpu(sbi, de->d_reclen))); | |
1da177e4 LT |
93 | } |
94 | ||
95 | /** | |
96 | * vxfs_find_entry - find a mathing directory entry for a dentry | |
97 | * @ip: directory inode | |
98 | * @dp: dentry for which we want to find a direct | |
99 | * @ppp: gets filled with the page the return value sits in | |
100 | * | |
101 | * Description: | |
102 | * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory | |
103 | * cache entry @dp. @ppp will be filled with the page the return | |
104 | * value resides in. | |
105 | * | |
106 | * Returns: | |
107 | * The wanted direct on success, else a NULL pointer. | |
108 | */ | |
109 | static struct vxfs_direct * | |
110 | vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp) | |
111 | { | |
0d83f7fc | 112 | struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb); |
1da177e4 LT |
113 | u_long npages, page, nblocks, pblocks, block; |
114 | u_long bsize = ip->i_sb->s_blocksize; | |
115 | const char *name = dp->d_name.name; | |
116 | int namelen = dp->d_name.len; | |
117 | ||
118 | npages = dir_pages(ip); | |
119 | nblocks = dir_blocks(ip); | |
120 | pblocks = VXFS_BLOCK_PER_PAGE(ip->i_sb); | |
121 | ||
122 | for (page = 0; page < npages; page++) { | |
123 | caddr_t kaddr; | |
124 | struct page *pp; | |
125 | ||
126 | pp = vxfs_get_page(ip->i_mapping, page); | |
127 | if (IS_ERR(pp)) | |
128 | continue; | |
129 | kaddr = (caddr_t)page_address(pp); | |
130 | ||
131 | for (block = 0; block <= nblocks && block <= pblocks; block++) { | |
132 | caddr_t baddr, limit; | |
133 | struct vxfs_dirblk *dbp; | |
134 | struct vxfs_direct *de; | |
135 | ||
136 | baddr = kaddr + (block * bsize); | |
137 | limit = baddr + bsize - VXFS_DIRLEN(1); | |
138 | ||
139 | dbp = (struct vxfs_dirblk *)baddr; | |
0d83f7fc KB |
140 | de = (struct vxfs_direct *) |
141 | (baddr + VXFS_DIRBLKOV(sbi, dbp)); | |
1da177e4 | 142 | |
0d83f7fc KB |
143 | for (; (caddr_t)de <= limit; |
144 | de = vxfs_next_entry(sbi, de)) { | |
1da177e4 LT |
145 | if (!de->d_reclen) |
146 | break; | |
147 | if (!de->d_ino) | |
148 | continue; | |
0d83f7fc | 149 | if (vxfs_match(sbi, namelen, name, de)) { |
1da177e4 LT |
150 | *ppp = pp; |
151 | return (de); | |
152 | } | |
153 | } | |
154 | } | |
155 | vxfs_put_page(pp); | |
156 | } | |
157 | ||
158 | return NULL; | |
159 | } | |
160 | ||
161 | /** | |
162 | * vxfs_inode_by_name - find inode number for dentry | |
163 | * @dip: directory to search in | |
25985edc | 164 | * @dp: dentry we search for |
1da177e4 LT |
165 | * |
166 | * Description: | |
167 | * vxfs_inode_by_name finds out the inode number of | |
168 | * the path component described by @dp in @dip. | |
169 | * | |
170 | * Returns: | |
171 | * The wanted inode number on success, else Zero. | |
172 | */ | |
173 | static ino_t | |
174 | vxfs_inode_by_name(struct inode *dip, struct dentry *dp) | |
175 | { | |
176 | struct vxfs_direct *de; | |
177 | struct page *pp; | |
178 | ino_t ino = 0; | |
179 | ||
180 | de = vxfs_find_entry(dip, dp, &pp); | |
181 | if (de) { | |
0d83f7fc | 182 | ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino); |
1da177e4 | 183 | kunmap(pp); |
09cbfeaf | 184 | put_page(pp); |
1da177e4 LT |
185 | } |
186 | ||
187 | return (ino); | |
188 | } | |
189 | ||
190 | /** | |
191 | * vxfs_lookup - lookup pathname component | |
192 | * @dip: dir in which we lookup | |
193 | * @dp: dentry we lookup | |
ddae82d8 | 194 | * @flags: lookup flags |
1da177e4 LT |
195 | * |
196 | * Description: | |
197 | * vxfs_lookup tries to lookup the pathname component described | |
198 | * by @dp in @dip. | |
199 | * | |
200 | * Returns: | |
eaf593c3 | 201 | * A NULL-pointer on success, else a negative error code encoded |
1da177e4 LT |
202 | * in the return pointer. |
203 | */ | |
204 | static struct dentry * | |
00cd8dd3 | 205 | vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags) |
1da177e4 LT |
206 | { |
207 | struct inode *ip = NULL; | |
208 | ino_t ino; | |
209 | ||
210 | if (dp->d_name.len > VXFS_NAMELEN) | |
211 | return ERR_PTR(-ENAMETOOLONG); | |
212 | ||
1da177e4 LT |
213 | ino = vxfs_inode_by_name(dip, dp); |
214 | if (ino) { | |
d0b07948 | 215 | ip = vxfs_iget(dip->i_sb, ino); |
6d7bccc2 | 216 | if (IS_ERR(ip)) |
d0b07948 | 217 | return ERR_CAST(ip); |
1da177e4 | 218 | } |
1da177e4 LT |
219 | d_add(dp, ip); |
220 | return NULL; | |
221 | } | |
222 | ||
223 | /** | |
224 | * vxfs_readdir - read a directory | |
225 | * @fp: the directory to read | |
226 | * @retp: return buffer | |
227 | * @filler: filldir callback | |
228 | * | |
229 | * Description: | |
230 | * vxfs_readdir fills @retp with directory entries from @fp | |
231 | * using the VFS supplied callback @filler. | |
232 | * | |
233 | * Returns: | |
234 | * Zero. | |
235 | */ | |
236 | static int | |
9b5d5a17 | 237 | vxfs_readdir(struct file *fp, struct dir_context *ctx) |
1da177e4 | 238 | { |
496ad9aa | 239 | struct inode *ip = file_inode(fp); |
1da177e4 | 240 | struct super_block *sbp = ip->i_sb; |
0d83f7fc | 241 | struct vxfs_sb_info *sbi = VXFS_SBI(sbp); |
1da177e4 LT |
242 | u_long bsize = sbp->s_blocksize; |
243 | u_long page, npages, block, pblocks, nblocks, offset; | |
244 | loff_t pos; | |
245 | ||
0d83f7fc | 246 | |
9b5d5a17 AV |
247 | if (ctx->pos == 0) { |
248 | if (!dir_emit_dot(fp, ctx)) | |
249 | return 0; | |
250 | ctx->pos = 1; | |
1da177e4 | 251 | } |
9b5d5a17 AV |
252 | if (ctx->pos == 1) { |
253 | if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR)) | |
254 | return 0; | |
255 | ctx->pos = 2; | |
256 | } | |
257 | pos = ctx->pos - 2; | |
1da177e4 | 258 | |
6d7bccc2 | 259 | if (pos > VXFS_DIRROUND(ip->i_size)) |
1da177e4 | 260 | return 0; |
1da177e4 LT |
261 | |
262 | npages = dir_pages(ip); | |
263 | nblocks = dir_blocks(ip); | |
264 | pblocks = VXFS_BLOCK_PER_PAGE(sbp); | |
265 | ||
09cbfeaf KS |
266 | page = pos >> PAGE_SHIFT; |
267 | offset = pos & ~PAGE_MASK; | |
1da177e4 LT |
268 | block = (u_long)(pos >> sbp->s_blocksize_bits) % pblocks; |
269 | ||
270 | for (; page < npages; page++, block = 0) { | |
9b5d5a17 | 271 | char *kaddr; |
1da177e4 LT |
272 | struct page *pp; |
273 | ||
274 | pp = vxfs_get_page(ip->i_mapping, page); | |
275 | if (IS_ERR(pp)) | |
276 | continue; | |
9b5d5a17 | 277 | kaddr = (char *)page_address(pp); |
1da177e4 LT |
278 | |
279 | for (; block <= nblocks && block <= pblocks; block++) { | |
9b5d5a17 | 280 | char *baddr, *limit; |
1da177e4 LT |
281 | struct vxfs_dirblk *dbp; |
282 | struct vxfs_direct *de; | |
283 | ||
284 | baddr = kaddr + (block * bsize); | |
285 | limit = baddr + bsize - VXFS_DIRLEN(1); | |
286 | ||
287 | dbp = (struct vxfs_dirblk *)baddr; | |
288 | de = (struct vxfs_direct *) | |
289 | (offset ? | |
290 | (kaddr + offset) : | |
0d83f7fc | 291 | (baddr + VXFS_DIRBLKOV(sbi, dbp))); |
1da177e4 | 292 | |
0d83f7fc KB |
293 | for (; (char *)de <= limit; |
294 | de = vxfs_next_entry(sbi, de)) { | |
1da177e4 LT |
295 | if (!de->d_reclen) |
296 | break; | |
297 | if (!de->d_ino) | |
298 | continue; | |
299 | ||
9b5d5a17 | 300 | offset = (char *)de - kaddr; |
09cbfeaf | 301 | ctx->pos = ((page << PAGE_SHIFT) | offset) + 2; |
0d83f7fc KB |
302 | if (!dir_emit(ctx, de->d_name, |
303 | fs16_to_cpu(sbi, de->d_namelen), | |
304 | fs32_to_cpu(sbi, de->d_ino), | |
305 | DT_UNKNOWN)) { | |
1da177e4 | 306 | vxfs_put_page(pp); |
9b5d5a17 | 307 | return 0; |
1da177e4 LT |
308 | } |
309 | } | |
310 | offset = 0; | |
311 | } | |
312 | vxfs_put_page(pp); | |
313 | offset = 0; | |
314 | } | |
09cbfeaf | 315 | ctx->pos = ((page << PAGE_SHIFT) | offset) + 2; |
1da177e4 LT |
316 | return 0; |
317 | } |