Commit | Line | Data |
---|---|---|
0b1e987c | 1 | // SPDX-License-Identifier: GPL-2.0-only |
1da177e4 LT |
2 | /* |
3 | * Copyright (c) 2000-2001 Christoph Hellwig. | |
1cce1701 | 4 | * Copyright (c) 2016 Krzysztof Blaszkowski |
1da177e4 LT |
5 | */ |
6 | ||
7 | /* | |
8 | * Veritas filesystem driver - lookup and other directory related code. | |
9 | */ | |
10 | #include <linux/fs.h> | |
11 | #include <linux/time.h> | |
12 | #include <linux/mm.h> | |
13 | #include <linux/highmem.h> | |
14 | #include <linux/kernel.h> | |
15 | #include <linux/pagemap.h> | |
1da177e4 LT |
16 | |
17 | #include "vxfs.h" | |
18 | #include "vxfs_dir.h" | |
19 | #include "vxfs_inode.h" | |
20 | #include "vxfs_extern.h" | |
21 | ||
22 | /* | |
23 | * Number of VxFS blocks per page. | |
24 | */ | |
09cbfeaf | 25 | #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize)) |
1da177e4 LT |
26 | |
27 | ||
00cd8dd3 | 28 | static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int); |
9b5d5a17 | 29 | static int vxfs_readdir(struct file *, struct dir_context *); |
1da177e4 | 30 | |
754661f1 | 31 | const struct inode_operations vxfs_dir_inode_ops = { |
1da177e4 LT |
32 | .lookup = vxfs_lookup, |
33 | }; | |
34 | ||
4b6f5d20 | 35 | const struct file_operations vxfs_dir_operations = { |
ca572727 B |
36 | .llseek = generic_file_llseek, |
37 | .read = generic_read_dir, | |
c51da20c | 38 | .iterate_shared = vxfs_readdir, |
1da177e4 LT |
39 | }; |
40 | ||
1da177e4 LT |
41 | |
42 | /** | |
43 | * vxfs_find_entry - find a mathing directory entry for a dentry | |
44 | * @ip: directory inode | |
45 | * @dp: dentry for which we want to find a direct | |
46 | * @ppp: gets filled with the page the return value sits in | |
47 | * | |
48 | * Description: | |
49 | * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory | |
50 | * cache entry @dp. @ppp will be filled with the page the return | |
51 | * value resides in. | |
52 | * | |
53 | * Returns: | |
54 | * The wanted direct on success, else a NULL pointer. | |
55 | */ | |
56 | static struct vxfs_direct * | |
57 | vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp) | |
58 | { | |
12495ea3 KB |
59 | u_long bsize = ip->i_sb->s_blocksize; |
60 | const char *name = dp->d_name.name; | |
61 | int namelen = dp->d_name.len; | |
62 | loff_t limit = VXFS_DIRROUND(ip->i_size); | |
63 | struct vxfs_direct *de_exit = NULL; | |
64 | loff_t pos = 0; | |
65 | struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb); | |
66 | ||
67 | while (pos < limit) { | |
68 | struct page *pp; | |
69 | char *kaddr; | |
70 | int pg_ofs = pos & ~PAGE_MASK; | |
1da177e4 | 71 | |
12495ea3 | 72 | pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT); |
1da177e4 | 73 | if (IS_ERR(pp)) |
12495ea3 KB |
74 | return NULL; |
75 | kaddr = (char *)page_address(pp); | |
76 | ||
77 | while (pg_ofs < PAGE_SIZE && pos < limit) { | |
78 | struct vxfs_direct *de; | |
79 | ||
80 | if ((pos & (bsize - 1)) < 4) { | |
81 | struct vxfs_dirblk *dbp = | |
82 | (struct vxfs_dirblk *) | |
83 | (kaddr + (pos & ~PAGE_MASK)); | |
84 | int overhead = VXFS_DIRBLKOV(sbi, dbp); | |
85 | ||
86 | pos += overhead; | |
87 | pg_ofs += overhead; | |
88 | } | |
89 | de = (struct vxfs_direct *)(kaddr + pg_ofs); | |
90 | ||
91 | if (!de->d_reclen) { | |
92 | pos += bsize - 1; | |
93 | pos &= ~(bsize - 1); | |
94 | break; | |
95 | } | |
96 | ||
97 | pg_ofs += fs16_to_cpu(sbi, de->d_reclen); | |
98 | pos += fs16_to_cpu(sbi, de->d_reclen); | |
99 | if (!de->d_ino) | |
100 | continue; | |
101 | ||
102 | if (namelen != fs16_to_cpu(sbi, de->d_namelen)) | |
103 | continue; | |
104 | if (!memcmp(name, de->d_name, namelen)) { | |
105 | *ppp = pp; | |
106 | de_exit = de; | |
107 | break; | |
1da177e4 LT |
108 | } |
109 | } | |
12495ea3 KB |
110 | if (!de_exit) |
111 | vxfs_put_page(pp); | |
112 | else | |
113 | break; | |
1da177e4 LT |
114 | } |
115 | ||
12495ea3 | 116 | return de_exit; |
1da177e4 LT |
117 | } |
118 | ||
119 | /** | |
120 | * vxfs_inode_by_name - find inode number for dentry | |
121 | * @dip: directory to search in | |
25985edc | 122 | * @dp: dentry we search for |
1da177e4 LT |
123 | * |
124 | * Description: | |
125 | * vxfs_inode_by_name finds out the inode number of | |
126 | * the path component described by @dp in @dip. | |
127 | * | |
128 | * Returns: | |
129 | * The wanted inode number on success, else Zero. | |
130 | */ | |
131 | static ino_t | |
132 | vxfs_inode_by_name(struct inode *dip, struct dentry *dp) | |
133 | { | |
134 | struct vxfs_direct *de; | |
135 | struct page *pp; | |
136 | ino_t ino = 0; | |
137 | ||
138 | de = vxfs_find_entry(dip, dp, &pp); | |
139 | if (de) { | |
0d83f7fc | 140 | ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino); |
1da177e4 | 141 | kunmap(pp); |
09cbfeaf | 142 | put_page(pp); |
1da177e4 LT |
143 | } |
144 | ||
145 | return (ino); | |
146 | } | |
147 | ||
148 | /** | |
149 | * vxfs_lookup - lookup pathname component | |
150 | * @dip: dir in which we lookup | |
151 | * @dp: dentry we lookup | |
ddae82d8 | 152 | * @flags: lookup flags |
1da177e4 LT |
153 | * |
154 | * Description: | |
155 | * vxfs_lookup tries to lookup the pathname component described | |
156 | * by @dp in @dip. | |
157 | * | |
158 | * Returns: | |
eaf593c3 | 159 | * A NULL-pointer on success, else a negative error code encoded |
1da177e4 LT |
160 | * in the return pointer. |
161 | */ | |
162 | static struct dentry * | |
00cd8dd3 | 163 | vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags) |
1da177e4 LT |
164 | { |
165 | struct inode *ip = NULL; | |
166 | ino_t ino; | |
167 | ||
168 | if (dp->d_name.len > VXFS_NAMELEN) | |
169 | return ERR_PTR(-ENAMETOOLONG); | |
170 | ||
1da177e4 | 171 | ino = vxfs_inode_by_name(dip, dp); |
72ff0b03 | 172 | if (ino) |
d0b07948 | 173 | ip = vxfs_iget(dip->i_sb, ino); |
72ff0b03 | 174 | return d_splice_alias(ip, dp); |
1da177e4 LT |
175 | } |
176 | ||
177 | /** | |
178 | * vxfs_readdir - read a directory | |
179 | * @fp: the directory to read | |
6915f40c | 180 | * @ctx: dir_context for filldir/readdir |
1da177e4 LT |
181 | * |
182 | * Description: | |
183 | * vxfs_readdir fills @retp with directory entries from @fp | |
184 | * using the VFS supplied callback @filler. | |
185 | * | |
186 | * Returns: | |
187 | * Zero. | |
188 | */ | |
189 | static int | |
9b5d5a17 | 190 | vxfs_readdir(struct file *fp, struct dir_context *ctx) |
1da177e4 | 191 | { |
496ad9aa | 192 | struct inode *ip = file_inode(fp); |
1da177e4 LT |
193 | struct super_block *sbp = ip->i_sb; |
194 | u_long bsize = sbp->s_blocksize; | |
12495ea3 KB |
195 | loff_t pos, limit; |
196 | struct vxfs_sb_info *sbi = VXFS_SBI(sbp); | |
0d83f7fc | 197 | |
9b5d5a17 AV |
198 | if (ctx->pos == 0) { |
199 | if (!dir_emit_dot(fp, ctx)) | |
12495ea3 KB |
200 | goto out; |
201 | ctx->pos++; | |
1da177e4 | 202 | } |
9b5d5a17 AV |
203 | if (ctx->pos == 1) { |
204 | if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR)) | |
12495ea3 KB |
205 | goto out; |
206 | ctx->pos++; | |
9b5d5a17 | 207 | } |
1da177e4 | 208 | |
12495ea3 KB |
209 | limit = VXFS_DIRROUND(ip->i_size); |
210 | if (ctx->pos > limit) | |
211 | goto out; | |
1da177e4 | 212 | |
12495ea3 | 213 | pos = ctx->pos & ~3L; |
1da177e4 | 214 | |
12495ea3 KB |
215 | while (pos < limit) { |
216 | struct page *pp; | |
217 | char *kaddr; | |
218 | int pg_ofs = pos & ~PAGE_MASK; | |
219 | int rc = 0; | |
1da177e4 | 220 | |
12495ea3 | 221 | pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT); |
1da177e4 | 222 | if (IS_ERR(pp)) |
12495ea3 KB |
223 | return -ENOMEM; |
224 | ||
9b5d5a17 | 225 | kaddr = (char *)page_address(pp); |
1da177e4 | 226 | |
12495ea3 KB |
227 | while (pg_ofs < PAGE_SIZE && pos < limit) { |
228 | struct vxfs_direct *de; | |
1da177e4 | 229 | |
12495ea3 KB |
230 | if ((pos & (bsize - 1)) < 4) { |
231 | struct vxfs_dirblk *dbp = | |
232 | (struct vxfs_dirblk *) | |
233 | (kaddr + (pos & ~PAGE_MASK)); | |
234 | int overhead = VXFS_DIRBLKOV(sbi, dbp); | |
235 | ||
236 | pos += overhead; | |
237 | pg_ofs += overhead; | |
238 | } | |
239 | de = (struct vxfs_direct *)(kaddr + pg_ofs); | |
240 | ||
241 | if (!de->d_reclen) { | |
242 | pos += bsize - 1; | |
243 | pos &= ~(bsize - 1); | |
244 | break; | |
245 | } | |
246 | ||
247 | pg_ofs += fs16_to_cpu(sbi, de->d_reclen); | |
248 | pos += fs16_to_cpu(sbi, de->d_reclen); | |
249 | if (!de->d_ino) | |
250 | continue; | |
251 | ||
252 | rc = dir_emit(ctx, de->d_name, | |
253 | fs16_to_cpu(sbi, de->d_namelen), | |
254 | fs32_to_cpu(sbi, de->d_ino), | |
255 | DT_UNKNOWN); | |
256 | if (!rc) { | |
257 | /* the dir entry was not read, fix pos. */ | |
258 | pos -= fs16_to_cpu(sbi, de->d_reclen); | |
259 | break; | |
1da177e4 | 260 | } |
1da177e4 LT |
261 | } |
262 | vxfs_put_page(pp); | |
12495ea3 KB |
263 | if (!rc) |
264 | break; | |
1da177e4 | 265 | } |
12495ea3 KB |
266 | |
267 | ctx->pos = pos | 2; | |
268 | ||
269 | out: | |
1da177e4 LT |
270 | return 0; |
271 | } |