2 * Copyright (c) 2000-2001 Christoph Hellwig.
3 * Copyright (c) 2016 Krzysztof Blaszkowski
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
15 * Alternatively, this software may be distributed under the terms of the
16 * GNU General Public License ("GPL").
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
32 * Veritas filesystem driver - lookup and other directory related code.
35 #include <linux/time.h>
37 #include <linux/highmem.h>
38 #include <linux/kernel.h>
39 #include <linux/pagemap.h>
43 #include "vxfs_inode.h"
44 #include "vxfs_extern.h"
47 * Number of VxFS blocks per page.
49 #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_SIZE / (sbp)->s_blocksize))
52 static struct dentry
* vxfs_lookup(struct inode
*, struct dentry
*, unsigned int);
53 static int vxfs_readdir(struct file
*, struct dir_context
*);
55 const struct inode_operations vxfs_dir_inode_ops
= {
56 .lookup
= vxfs_lookup
,
59 const struct file_operations vxfs_dir_operations
= {
60 .llseek
= generic_file_llseek
,
61 .read
= generic_read_dir
,
62 .iterate_shared
= vxfs_readdir
,
67 * vxfs_find_entry - find a mathing directory entry for a dentry
68 * @ip: directory inode
69 * @dp: dentry for which we want to find a direct
70 * @ppp: gets filled with the page the return value sits in
73 * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory
74 * cache entry @dp. @ppp will be filled with the page the return
78 * The wanted direct on success, else a NULL pointer.
80 static struct vxfs_direct
*
81 vxfs_find_entry(struct inode
*ip
, struct dentry
*dp
, struct page
**ppp
)
83 u_long bsize
= ip
->i_sb
->s_blocksize
;
84 const char *name
= dp
->d_name
.name
;
85 int namelen
= dp
->d_name
.len
;
86 loff_t limit
= VXFS_DIRROUND(ip
->i_size
);
87 struct vxfs_direct
*de_exit
= NULL
;
89 struct vxfs_sb_info
*sbi
= VXFS_SBI(ip
->i_sb
);
94 int pg_ofs
= pos
& ~PAGE_MASK
;
96 pp
= vxfs_get_page(ip
->i_mapping
, pos
>> PAGE_SHIFT
);
99 kaddr
= (char *)page_address(pp
);
101 while (pg_ofs
< PAGE_SIZE
&& pos
< limit
) {
102 struct vxfs_direct
*de
;
104 if ((pos
& (bsize
- 1)) < 4) {
105 struct vxfs_dirblk
*dbp
=
106 (struct vxfs_dirblk
*)
107 (kaddr
+ (pos
& ~PAGE_MASK
));
108 int overhead
= VXFS_DIRBLKOV(sbi
, dbp
);
113 de
= (struct vxfs_direct
*)(kaddr
+ pg_ofs
);
121 pg_ofs
+= fs16_to_cpu(sbi
, de
->d_reclen
);
122 pos
+= fs16_to_cpu(sbi
, de
->d_reclen
);
126 if (namelen
!= fs16_to_cpu(sbi
, de
->d_namelen
))
128 if (!memcmp(name
, de
->d_name
, namelen
)) {
144 * vxfs_inode_by_name - find inode number for dentry
145 * @dip: directory to search in
146 * @dp: dentry we search for
149 * vxfs_inode_by_name finds out the inode number of
150 * the path component described by @dp in @dip.
153 * The wanted inode number on success, else Zero.
156 vxfs_inode_by_name(struct inode
*dip
, struct dentry
*dp
)
158 struct vxfs_direct
*de
;
162 de
= vxfs_find_entry(dip
, dp
, &pp
);
164 ino
= fs32_to_cpu(VXFS_SBI(dip
->i_sb
), de
->d_ino
);
173 * vxfs_lookup - lookup pathname component
174 * @dip: dir in which we lookup
175 * @dp: dentry we lookup
176 * @flags: lookup flags
179 * vxfs_lookup tries to lookup the pathname component described
183 * A NULL-pointer on success, else a negative error code encoded
184 * in the return pointer.
186 static struct dentry
*
187 vxfs_lookup(struct inode
*dip
, struct dentry
*dp
, unsigned int flags
)
189 struct inode
*ip
= NULL
;
192 if (dp
->d_name
.len
> VXFS_NAMELEN
)
193 return ERR_PTR(-ENAMETOOLONG
);
195 ino
= vxfs_inode_by_name(dip
, dp
);
197 ip
= vxfs_iget(dip
->i_sb
, ino
);
206 * vxfs_readdir - read a directory
207 * @fp: the directory to read
208 * @retp: return buffer
209 * @filler: filldir callback
212 * vxfs_readdir fills @retp with directory entries from @fp
213 * using the VFS supplied callback @filler.
219 vxfs_readdir(struct file
*fp
, struct dir_context
*ctx
)
221 struct inode
*ip
= file_inode(fp
);
222 struct super_block
*sbp
= ip
->i_sb
;
223 u_long bsize
= sbp
->s_blocksize
;
225 struct vxfs_sb_info
*sbi
= VXFS_SBI(sbp
);
228 if (!dir_emit_dot(fp
, ctx
))
233 if (!dir_emit(ctx
, "..", 2, VXFS_INO(ip
)->vii_dotdot
, DT_DIR
))
238 limit
= VXFS_DIRROUND(ip
->i_size
);
239 if (ctx
->pos
> limit
)
242 pos
= ctx
->pos
& ~3L;
244 while (pos
< limit
) {
247 int pg_ofs
= pos
& ~PAGE_MASK
;
250 pp
= vxfs_get_page(ip
->i_mapping
, pos
>> PAGE_SHIFT
);
254 kaddr
= (char *)page_address(pp
);
256 while (pg_ofs
< PAGE_SIZE
&& pos
< limit
) {
257 struct vxfs_direct
*de
;
259 if ((pos
& (bsize
- 1)) < 4) {
260 struct vxfs_dirblk
*dbp
=
261 (struct vxfs_dirblk
*)
262 (kaddr
+ (pos
& ~PAGE_MASK
));
263 int overhead
= VXFS_DIRBLKOV(sbi
, dbp
);
268 de
= (struct vxfs_direct
*)(kaddr
+ pg_ofs
);
276 pg_ofs
+= fs16_to_cpu(sbi
, de
->d_reclen
);
277 pos
+= fs16_to_cpu(sbi
, de
->d_reclen
);
281 rc
= dir_emit(ctx
, de
->d_name
,
282 fs16_to_cpu(sbi
, de
->d_namelen
),
283 fs32_to_cpu(sbi
, de
->d_ino
),
286 /* the dir entry was not read, fix pos. */
287 pos
-= fs16_to_cpu(sbi
, de
->d_reclen
);