1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2000-2001 Christoph Hellwig.
4 * Copyright (c) 2016 Krzysztof Blaszkowski
8 * Veritas filesystem driver - lookup and other directory related code.
11 #include <linux/time.h>
13 #include <linux/highmem.h>
14 #include <linux/kernel.h>
15 #include <linux/pagemap.h>
19 #include "vxfs_inode.h"
20 #include "vxfs_extern.h"
23 * Number of VxFS blocks per page.
25 #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize))
28 static struct dentry * vxfs_lookup(struct inode *, struct dentry *, unsigned int);
29 static int vxfs_readdir(struct file *, struct dir_context *);
31 const struct inode_operations vxfs_dir_inode_ops = {
32 .lookup = vxfs_lookup,
35 const struct file_operations vxfs_dir_operations = {
36 .llseek = generic_file_llseek,
37 .read = generic_read_dir,
38 .iterate_shared = vxfs_readdir,
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
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
54 * The wanted direct on success, else a NULL pointer.
56 static struct vxfs_direct *
57 vxfs_find_entry(struct inode *ip, struct dentry *dp, struct page **ppp)
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;
65 struct vxfs_sb_info *sbi = VXFS_SBI(ip->i_sb);
70 int pg_ofs = pos & ~PAGE_MASK;
72 pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
75 kaddr = (char *)page_address(pp);
77 while (pg_ofs < PAGE_SIZE && pos < limit) {
78 struct vxfs_direct *de;
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);
89 de = (struct vxfs_direct *)(kaddr + pg_ofs);
97 pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
98 pos += fs16_to_cpu(sbi, de->d_reclen);
102 if (namelen != fs16_to_cpu(sbi, de->d_namelen))
104 if (!memcmp(name, de->d_name, namelen)) {
120 * vxfs_inode_by_name - find inode number for dentry
121 * @dip: directory to search in
122 * @dp: dentry we search for
125 * vxfs_inode_by_name finds out the inode number of
126 * the path component described by @dp in @dip.
129 * The wanted inode number on success, else Zero.
132 vxfs_inode_by_name(struct inode *dip, struct dentry *dp)
134 struct vxfs_direct *de;
138 de = vxfs_find_entry(dip, dp, &pp);
140 ino = fs32_to_cpu(VXFS_SBI(dip->i_sb), de->d_ino);
149 * vxfs_lookup - lookup pathname component
150 * @dip: dir in which we lookup
151 * @dp: dentry we lookup
152 * @flags: lookup flags
155 * vxfs_lookup tries to lookup the pathname component described
159 * A NULL-pointer on success, else a negative error code encoded
160 * in the return pointer.
162 static struct dentry *
163 vxfs_lookup(struct inode *dip, struct dentry *dp, unsigned int flags)
165 struct inode *ip = NULL;
168 if (dp->d_name.len > VXFS_NAMELEN)
169 return ERR_PTR(-ENAMETOOLONG);
171 ino = vxfs_inode_by_name(dip, dp);
173 ip = vxfs_iget(dip->i_sb, ino);
174 return d_splice_alias(ip, dp);
178 * vxfs_readdir - read a directory
179 * @fp: the directory to read
180 * @retp: return buffer
181 * @filler: filldir callback
184 * vxfs_readdir fills @retp with directory entries from @fp
185 * using the VFS supplied callback @filler.
191 vxfs_readdir(struct file *fp, struct dir_context *ctx)
193 struct inode *ip = file_inode(fp);
194 struct super_block *sbp = ip->i_sb;
195 u_long bsize = sbp->s_blocksize;
197 struct vxfs_sb_info *sbi = VXFS_SBI(sbp);
200 if (!dir_emit_dot(fp, ctx))
205 if (!dir_emit(ctx, "..", 2, VXFS_INO(ip)->vii_dotdot, DT_DIR))
210 limit = VXFS_DIRROUND(ip->i_size);
211 if (ctx->pos > limit)
214 pos = ctx->pos & ~3L;
216 while (pos < limit) {
219 int pg_ofs = pos & ~PAGE_MASK;
222 pp = vxfs_get_page(ip->i_mapping, pos >> PAGE_SHIFT);
226 kaddr = (char *)page_address(pp);
228 while (pg_ofs < PAGE_SIZE && pos < limit) {
229 struct vxfs_direct *de;
231 if ((pos & (bsize - 1)) < 4) {
232 struct vxfs_dirblk *dbp =
233 (struct vxfs_dirblk *)
234 (kaddr + (pos & ~PAGE_MASK));
235 int overhead = VXFS_DIRBLKOV(sbi, dbp);
240 de = (struct vxfs_direct *)(kaddr + pg_ofs);
248 pg_ofs += fs16_to_cpu(sbi, de->d_reclen);
249 pos += fs16_to_cpu(sbi, de->d_reclen);
253 rc = dir_emit(ctx, de->d_name,
254 fs16_to_cpu(sbi, de->d_namelen),
255 fs32_to_cpu(sbi, de->d_ino),
258 /* the dir entry was not read, fix pos. */
259 pos -= fs16_to_cpu(sbi, de->d_reclen);