fs: use kmem_cache_zalloc instead
[pv_ops_mirror.git] / fs / udf / directory.c
blobff8c08fd7bf533ff53d6e659f284edb9c23e5a1d
1 /*
2 * directory.c
4 * PURPOSE
5 * Directory related functions
7 * COPYRIGHT
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
14 #include "udfdecl.h"
15 #include "udf_i.h"
17 #include <linux/fs.h>
18 #include <linux/string.h>
19 #include <linux/buffer_head.h>
21 #if 0
22 static uint8_t *udf_filead_read(struct inode *dir, uint8_t * tmpad,
23 uint8_t ad_size, kernel_lb_addr fe_loc,
24 int *pos, int *offset, struct buffer_head **bh,
25 int *error)
27 int loffset = *offset;
28 int block;
29 uint8_t *ad;
30 int remainder;
32 *error = 0;
34 ad = (uint8_t *)(*bh)->b_data + *offset;
35 *offset += ad_size;
37 if (!ad) {
38 brelse(*bh);
39 *error = 1;
40 return NULL;
43 if (*offset == dir->i_sb->s_blocksize) {
44 brelse(*bh);
45 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
46 if (!block)
47 return NULL;
48 if (!(*bh = udf_tread(dir->i_sb, block)))
49 return NULL;
50 } else if (*offset > dir->i_sb->s_blocksize) {
51 ad = tmpad;
53 remainder = dir->i_sb->s_blocksize - loffset;
54 memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder);
56 brelse(*bh);
57 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
58 if (!block)
59 return NULL;
60 if (!((*bh) = udf_tread(dir->i_sb, block)))
61 return NULL;
63 memcpy((uint8_t *)ad + remainder, (*bh)->b_data, ad_size - remainder);
64 *offset = ad_size - remainder;
67 return ad;
69 #endif
71 struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t * nf_pos,
72 struct udf_fileident_bh *fibh,
73 struct fileIdentDesc *cfi,
74 struct extent_position *epos,
75 kernel_lb_addr * eloc, uint32_t * elen,
76 sector_t * offset)
78 struct fileIdentDesc *fi;
79 int i, num, block;
80 struct buffer_head *tmp, *bha[16];
82 fibh->soffset = fibh->eoffset;
84 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
85 fi = udf_get_fileident(UDF_I_DATA(dir) -
86 (UDF_I_EFE(dir) ?
87 sizeof(struct extendedFileEntry) :
88 sizeof(struct fileEntry)),
89 dir->i_sb->s_blocksize, &(fibh->eoffset));
90 if (!fi)
91 return NULL;
93 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
95 memcpy((uint8_t *)cfi, (uint8_t *)fi,
96 sizeof(struct fileIdentDesc));
98 return fi;
101 if (fibh->eoffset == dir->i_sb->s_blocksize) {
102 int lextoffset = epos->offset;
104 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
105 (EXT_RECORDED_ALLOCATED >> 30))
106 return NULL;
108 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
110 (*offset)++;
112 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
113 *offset = 0;
114 else
115 epos->offset = lextoffset;
117 brelse(fibh->sbh);
118 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
119 return NULL;
120 fibh->soffset = fibh->eoffset = 0;
122 if (!(*offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
123 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
124 if (i + *offset > (*elen >> dir->i_sb->s_blocksize_bits))
125 i = (*elen >> dir->i_sb->s_blocksize_bits)-*offset;
126 for (num = 0; i > 0; i--) {
127 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset + i);
128 tmp = udf_tgetblk(dir->i_sb, block);
129 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
130 bha[num++] = tmp;
131 else
132 brelse(tmp);
134 if (num) {
135 ll_rw_block(READA, num, bha);
136 for (i = 0; i < num; i++)
137 brelse(bha[i]);
140 } else if (fibh->sbh != fibh->ebh) {
141 brelse(fibh->sbh);
142 fibh->sbh = fibh->ebh;
145 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
146 &(fibh->eoffset));
148 if (!fi)
149 return NULL;
151 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
153 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
154 memcpy((uint8_t *)cfi, (uint8_t *)fi,
155 sizeof(struct fileIdentDesc));
156 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
157 int lextoffset = epos->offset;
159 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
160 (EXT_RECORDED_ALLOCATED >> 30))
161 return NULL;
163 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
165 (*offset)++;
167 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
168 *offset = 0;
169 else
170 epos->offset = lextoffset;
172 fibh->soffset -= dir->i_sb->s_blocksize;
173 fibh->eoffset -= dir->i_sb->s_blocksize;
175 if (!(fibh->ebh = udf_tread(dir->i_sb, block)))
176 return NULL;
178 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
179 int fi_len;
181 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
182 memcpy((uint8_t *)cfi - fibh->soffset, fibh->ebh->b_data,
183 sizeof(struct fileIdentDesc) + fibh->soffset);
185 fi_len = (sizeof(struct fileIdentDesc) + cfi->lengthFileIdent +
186 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
188 *nf_pos += ((fi_len - (fibh->eoffset - fibh->soffset)) >> 2);
189 fibh->eoffset = fibh->soffset + fi_len;
190 } else {
191 memcpy((uint8_t *)cfi, (uint8_t *)fi,
192 sizeof(struct fileIdentDesc));
195 return fi;
198 struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
200 struct fileIdentDesc *fi;
201 int lengthThisIdent;
202 uint8_t *ptr;
203 int padlen;
205 if ((!buffer) || (!offset)) {
206 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
207 offset);
208 return NULL;
211 ptr = buffer;
213 if ((*offset > 0) && (*offset < bufsize)) {
214 ptr += *offset;
216 fi = (struct fileIdentDesc *)ptr;
217 if (le16_to_cpu(fi->descTag.tagIdent) != TAG_IDENT_FID) {
218 udf_debug("0x%x != TAG_IDENT_FID\n",
219 le16_to_cpu(fi->descTag.tagIdent));
220 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
221 *offset, (unsigned long)sizeof(struct fileIdentDesc),
222 bufsize);
223 return NULL;
225 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) {
226 lengthThisIdent = sizeof(struct fileIdentDesc);
227 } else {
228 lengthThisIdent = sizeof(struct fileIdentDesc) +
229 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
232 /* we need to figure padding, too! */
233 padlen = lengthThisIdent % UDF_NAME_PAD;
234 if (padlen)
235 lengthThisIdent += (UDF_NAME_PAD - padlen);
236 *offset = *offset + lengthThisIdent;
238 return fi;
241 #if 0
242 static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
244 extent_ad *ext;
245 struct fileEntry *fe;
246 uint8_t *ptr;
248 if ((!buffer) || (!offset)) {
249 printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
250 return NULL;
253 fe = (struct fileEntry *)buffer;
255 if (le16_to_cpu(fe->descTag.tagIdent) != TAG_IDENT_FE) {
256 udf_debug("0x%x != TAG_IDENT_FE\n",
257 le16_to_cpu(fe->descTag.tagIdent));
258 return NULL;
261 ptr = (uint8_t *)(fe->extendedAttr) + le32_to_cpu(fe->lengthExtendedAttr);
263 if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs))) {
264 ptr += *offset;
267 ext = (extent_ad *)ptr;
269 *offset = *offset + sizeof(extent_ad);
270 return ext;
272 #endif
274 short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, int *offset,
275 int inc)
277 short_ad *sa;
279 if ((!ptr) || (!offset)) {
280 printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
281 return NULL;
284 if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset))
285 return NULL;
286 else if ((sa = (short_ad *)ptr)->extLength == 0)
287 return NULL;
289 if (inc)
290 *offset += sizeof(short_ad);
291 return sa;
294 long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, int *offset, int inc)
296 long_ad *la;
298 if ((!ptr) || (!offset)) {
299 printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
300 return NULL;
303 if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset))
304 return NULL;
305 else if ((la = (long_ad *)ptr)->extLength == 0)
306 return NULL;
308 if (inc)
309 *offset += sizeof(long_ad);
310 return la;