2 * Copyright (c) 2000-2001 Christoph Hellwig.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. The name of the author may not be used to endorse or promote products
12 * derived from this software without specific prior written permission.
14 * Alternatively, this software may be distributed under the terms of the
15 * GNU General Public License ("GPL").
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #ident "$Id: vxfs_lookup.c,v 1.21 2002/01/02 22:00:13 hch Exp hch $"
33 * Veritas filesystem driver - lookup and other directory related code.
36 #include <linux/time.h>
38 #include <linux/highmem.h>
39 #include <linux/kernel.h>
40 #include <linux/pagemap.h>
41 #include <linux/smp_lock.h>
45 #include "vxfs_inode.h"
46 #include "vxfs_extern.h"
49 * Number of VxFS blocks per page.
51 #define VXFS_BLOCK_PER_PAGE(sbp) ((PAGE_CACHE_SIZE / (sbp)->s_blocksize))
54 static struct dentry
* vxfs_lookup(struct inode
*, struct dentry
*);
55 static int vxfs_readdir(struct file
*, void *, filldir_t
);
57 struct inode_operations vxfs_dir_inode_ops
= {
58 .lookup
= vxfs_lookup
,
61 struct file_operations vxfs_dir_operations
= {
62 .readdir
= vxfs_readdir
,
66 static __inline__ u_long
67 dir_pages(struct inode
*inode
)
69 return (inode
->i_size
+ PAGE_CACHE_SIZE
- 1) >> PAGE_CACHE_SHIFT
;
72 static __inline__ u_long
73 dir_blocks(struct inode
*ip
)
75 u_long bsize
= ip
->i_sb
->s_blocksize
;
76 return (ip
->i_size
+ bsize
- 1) & ~(bsize
- 1);
80 * NOTE! unlike strncmp, vxfs_match returns 1 for success, 0 for failure.
82 * len <= VXFS_NAMELEN and de != NULL are guaranteed by caller.
85 vxfs_match(int len
, const char * const name
, struct vxfs_direct
*de
)
87 if (len
!= de
->d_namelen
)
91 return !memcmp(name
, de
->d_name
, len
);
94 static __inline__
struct vxfs_direct
*
95 vxfs_next_entry(struct vxfs_direct
*de
)
97 return ((struct vxfs_direct
*)((char*)de
+ de
->d_reclen
));
101 * vxfs_find_entry - find a mathing directory entry for a dentry
102 * @ip: directory inode
103 * @dp: dentry for which we want to find a direct
104 * @ppp: gets filled with the page the return value sits in
107 * vxfs_find_entry finds a &struct vxfs_direct for the VFS directory
108 * cache entry @dp. @ppp will be filled with the page the return
112 * The wanted direct on success, else a NULL pointer.
114 static struct vxfs_direct
*
115 vxfs_find_entry(struct inode
*ip
, struct dentry
*dp
, struct page
**ppp
)
117 u_long npages
, page
, nblocks
, pblocks
, block
;
118 u_long bsize
= ip
->i_sb
->s_blocksize
;
119 const char *name
= dp
->d_name
.name
;
120 int namelen
= dp
->d_name
.len
;
122 npages
= dir_pages(ip
);
123 nblocks
= dir_blocks(ip
);
124 pblocks
= VXFS_BLOCK_PER_PAGE(ip
->i_sb
);
126 for (page
= 0; page
< npages
; page
++) {
130 pp
= vxfs_get_page(ip
->i_mapping
, page
);
133 kaddr
= (caddr_t
)page_address(pp
);
135 for (block
= 0; block
<= nblocks
&& block
<= pblocks
; block
++) {
136 caddr_t baddr
, limit
;
137 struct vxfs_dirblk
*dbp
;
138 struct vxfs_direct
*de
;
140 baddr
= kaddr
+ (block
* bsize
);
141 limit
= baddr
+ bsize
- VXFS_DIRLEN(1);
143 dbp
= (struct vxfs_dirblk
*)baddr
;
144 de
= (struct vxfs_direct
*)(baddr
+ VXFS_DIRBLKOV(dbp
));
146 for (; (caddr_t
)de
<= limit
; de
= vxfs_next_entry(de
)) {
151 if (vxfs_match(namelen
, name
, de
)) {
164 * vxfs_inode_by_name - find inode number for dentry
165 * @dip: directory to search in
166 * @dp: dentry we seach for
169 * vxfs_inode_by_name finds out the inode number of
170 * the path component described by @dp in @dip.
173 * The wanted inode number on success, else Zero.
176 vxfs_inode_by_name(struct inode
*dip
, struct dentry
*dp
)
178 struct vxfs_direct
*de
;
182 de
= vxfs_find_entry(dip
, dp
, &pp
);
186 page_cache_release(pp
);
193 * vxfs_lookup - lookup pathname component
194 * @dip: dir in which we lookup
195 * @dp: dentry we lookup
198 * vxfs_lookup tries to lookup the pathname component described
202 * A NULL-pointer on success, else an negative error code encoded
203 * in the return pointer.
205 static struct dentry
*
206 vxfs_lookup(struct inode
*dip
, struct dentry
*dp
)
208 struct inode
*ip
= NULL
;
211 if (dp
->d_name
.len
> VXFS_NAMELEN
)
212 return ERR_PTR(-ENAMETOOLONG
);
215 ino
= vxfs_inode_by_name(dip
, dp
);
217 ip
= iget(dip
->i_sb
, ino
);
220 return ERR_PTR(-EACCES
);
229 * vxfs_readdir - read a directory
230 * @fp: the directory to read
231 * @retp: return buffer
232 * @filler: filldir callback
235 * vxfs_readdir fills @retp with directory entries from @fp
236 * using the VFS supplied callback @filler.
242 vxfs_readdir(struct file
*fp
, void *retp
, filldir_t filler
)
244 struct inode
*ip
= fp
->f_dentry
->d_inode
;
245 struct super_block
*sbp
= ip
->i_sb
;
246 u_long bsize
= sbp
->s_blocksize
;
247 u_long page
, npages
, block
, pblocks
, nblocks
, offset
;
250 switch ((long)fp
->f_pos
) {
252 if (filler(retp
, ".", 1, fp
->f_pos
, ip
->i_ino
, DT_DIR
) < 0)
257 if (filler(retp
, "..", 2, fp
->f_pos
, VXFS_INO(ip
)->vii_dotdot
, DT_DIR
) < 0)
265 if (pos
> VXFS_DIRROUND(ip
->i_size
)) {
270 npages
= dir_pages(ip
);
271 nblocks
= dir_blocks(ip
);
272 pblocks
= VXFS_BLOCK_PER_PAGE(sbp
);
274 page
= pos
>> PAGE_CACHE_SHIFT
;
275 offset
= pos
& ~PAGE_CACHE_MASK
;
276 block
= (u_long
)(pos
>> sbp
->s_blocksize_bits
) % pblocks
;
278 for (; page
< npages
; page
++, block
= 0) {
282 pp
= vxfs_get_page(ip
->i_mapping
, page
);
285 kaddr
= (caddr_t
)page_address(pp
);
287 for (; block
<= nblocks
&& block
<= pblocks
; block
++) {
288 caddr_t baddr
, limit
;
289 struct vxfs_dirblk
*dbp
;
290 struct vxfs_direct
*de
;
292 baddr
= kaddr
+ (block
* bsize
);
293 limit
= baddr
+ bsize
- VXFS_DIRLEN(1);
295 dbp
= (struct vxfs_dirblk
*)baddr
;
296 de
= (struct vxfs_direct
*)
299 (baddr
+ VXFS_DIRBLKOV(dbp
)));
301 for (; (caddr_t
)de
<= limit
; de
= vxfs_next_entry(de
)) {
309 offset
= (caddr_t
)de
- kaddr
;
310 over
= filler(retp
, de
->d_name
, de
->d_namelen
,
311 ((page
<< PAGE_CACHE_SHIFT
) | offset
) + 2,
312 de
->d_ino
, DT_UNKNOWN
);
325 fp
->f_pos
= ((page
<< PAGE_CACHE_SHIFT
) | offset
) + 2;