5 * Directory related functions
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.
18 #include <linux/string.h>
19 #include <linux/buffer_head.h>
23 udf_filead_read(struct inode *dir, uint8_t *tmpad, uint8_t ad_size,
24 kernel_lb_addr fe_loc, int *pos, int *offset,
25 struct buffer_head **bh, int *error)
27 int loffset = *offset;
34 ad = (uint8_t *)(*bh)->b_data + *offset;
44 if (*offset == dir->i_sb->s_blocksize)
47 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
50 if (!(*bh = udf_tread(dir->i_sb, block)))
53 else if (*offset > dir->i_sb->s_blocksize)
57 remainder = dir->i_sb->s_blocksize - loffset;
58 memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder);
61 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
64 if (!((*bh) = udf_tread(dir->i_sb, block)))
67 memcpy((uint8_t *)ad + remainder, (*bh)->b_data, ad_size - remainder);
68 *offset = ad_size - remainder;
74 struct fileIdentDesc *
75 udf_fileident_read(struct inode *dir, loff_t *nf_pos,
76 struct udf_fileident_bh *fibh,
77 struct fileIdentDesc *cfi,
78 struct extent_position *epos,
79 kernel_lb_addr *eloc, uint32_t *elen,
82 struct fileIdentDesc *fi;
84 struct buffer_head * tmp, * bha[16];
86 fibh->soffset = fibh->eoffset;
88 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB)
90 fi = udf_get_fileident(UDF_I_DATA(dir) -
92 sizeof(struct extendedFileEntry) :
93 sizeof(struct fileEntry)),
94 dir->i_sb->s_blocksize, &(fibh->eoffset));
99 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
101 memcpy((uint8_t *)cfi, (uint8_t *)fi, sizeof(struct fileIdentDesc));
106 if (fibh->eoffset == dir->i_sb->s_blocksize)
108 int lextoffset = epos->offset;
110 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
111 (EXT_RECORDED_ALLOCATED >> 30))
114 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
118 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
121 epos->offset = lextoffset;
124 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
126 fibh->soffset = fibh->eoffset = 0;
128 if (!(*offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9))-1)))
130 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
131 if (i+*offset > (*elen >> dir->i_sb->s_blocksize_bits))
132 i = (*elen >> dir->i_sb->s_blocksize_bits)-*offset;
133 for (num=0; i>0; i--)
135 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset+i);
136 tmp = udf_tgetblk(dir->i_sb, block);
137 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
144 ll_rw_block(READA, num, bha);
145 for (i=0; i<num; i++)
150 else if (fibh->sbh != fibh->ebh)
153 fibh->sbh = fibh->ebh;
156 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
162 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
164 if (fibh->eoffset <= dir->i_sb->s_blocksize)
166 memcpy((uint8_t *)cfi, (uint8_t *)fi, sizeof(struct fileIdentDesc));
168 else if (fibh->eoffset > dir->i_sb->s_blocksize)
170 int lextoffset = epos->offset;
172 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
173 (EXT_RECORDED_ALLOCATED >> 30))
176 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
180 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
183 epos->offset = lextoffset;
185 fibh->soffset -= dir->i_sb->s_blocksize;
186 fibh->eoffset -= dir->i_sb->s_blocksize;
188 if (!(fibh->ebh = udf_tread(dir->i_sb, block)))
191 if (sizeof(struct fileIdentDesc) > - fibh->soffset)
195 memcpy((uint8_t *)cfi, (uint8_t *)fi, - fibh->soffset);
196 memcpy((uint8_t *)cfi - fibh->soffset, fibh->ebh->b_data,
197 sizeof(struct fileIdentDesc) + fibh->soffset);
199 fi_len = (sizeof(struct fileIdentDesc) + cfi->lengthFileIdent +
200 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
202 *nf_pos += ((fi_len - (fibh->eoffset - fibh->soffset)) >> 2);
203 fibh->eoffset = fibh->soffset + fi_len;
207 memcpy((uint8_t *)cfi, (uint8_t *)fi, sizeof(struct fileIdentDesc));
213 struct fileIdentDesc *
214 udf_get_fileident(void * buffer, int bufsize, int * offset)
216 struct fileIdentDesc *fi;
221 if ( (!buffer) || (!offset) ) {
222 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer, offset);
228 if ( (*offset > 0) && (*offset < bufsize) ) {
231 fi=(struct fileIdentDesc *)ptr;
232 if (le16_to_cpu(fi->descTag.tagIdent) != TAG_IDENT_FID)
234 udf_debug("0x%x != TAG_IDENT_FID\n",
235 le16_to_cpu(fi->descTag.tagIdent));
236 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
237 *offset, (unsigned long)sizeof(struct fileIdentDesc), bufsize);
240 if ( (*offset + sizeof(struct fileIdentDesc)) > bufsize )
242 lengthThisIdent = sizeof(struct fileIdentDesc);
245 lengthThisIdent = sizeof(struct fileIdentDesc) +
246 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
248 /* we need to figure padding, too! */
249 padlen = lengthThisIdent % UDF_NAME_PAD;
251 lengthThisIdent += (UDF_NAME_PAD - padlen);
252 *offset = *offset + lengthThisIdent;
259 udf_get_fileextent(void * buffer, int bufsize, int * offset)
262 struct fileEntry *fe;
265 if ( (!buffer) || (!offset) )
267 printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
271 fe = (struct fileEntry *)buffer;
273 if ( le16_to_cpu(fe->descTag.tagIdent) != TAG_IDENT_FE )
275 udf_debug("0x%x != TAG_IDENT_FE\n",
276 le16_to_cpu(fe->descTag.tagIdent));
280 ptr=(uint8_t *)(fe->extendedAttr) + le32_to_cpu(fe->lengthExtendedAttr);
282 if ( (*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs)) )
287 ext = (extent_ad *)ptr;
289 *offset = *offset + sizeof(extent_ad);
295 udf_get_fileshortad(uint8_t *ptr, int maxoffset, int *offset, int inc)
299 if ( (!ptr) || (!offset) )
301 printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
305 if ( (*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset) )
307 else if ((sa = (short_ad *)ptr)->extLength == 0)
311 *offset += sizeof(short_ad);
316 udf_get_filelongad(uint8_t *ptr, int maxoffset, int * offset, int inc)
320 if ( (!ptr) || (!offset) )
322 printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
326 if ( (*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset) )
328 else if ((la = (long_ad *)ptr)->extLength == 0)
332 *offset += sizeof(long_ad);