Blame view

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