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>
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
,
27 int loffset
= *offset
;
34 ad
= (uint8_t *)(*bh
)->b_data
+ *offset
;
43 if (*offset
== dir
->i_sb
->s_blocksize
) {
45 block
= udf_get_lb_pblock(dir
->i_sb
, fe_loc
, ++*pos
);
48 *bh
= udf_tread(dir
->i_sb
, block
);
51 } else if (*offset
> dir
->i_sb
->s_blocksize
) {
54 remainder
= dir
->i_sb
->s_blocksize
- loffset
;
55 memcpy((uint8_t *)ad
, (*bh
)->b_data
+ loffset
, remainder
);
58 block
= udf_get_lb_pblock(dir
->i_sb
, fe_loc
, ++*pos
);
61 (*bh
) = udf_tread(dir
->i_sb
, block
);
65 memcpy((uint8_t *)ad
+ remainder
, (*bh
)->b_data
,
67 *offset
= ad_size
- remainder
;
74 struct fileIdentDesc
*udf_fileident_read(struct inode
*dir
, loff_t
*nf_pos
,
75 struct udf_fileident_bh
*fibh
,
76 struct fileIdentDesc
*cfi
,
77 struct extent_position
*epos
,
78 kernel_lb_addr
*eloc
, uint32_t *elen
,
81 struct fileIdentDesc
*fi
;
83 struct buffer_head
*tmp
, *bha
[16];
84 struct udf_inode_info
*iinfo
= UDF_I(dir
);
86 fibh
->soffset
= fibh
->eoffset
;
88 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
89 fi
= udf_get_fileident(iinfo
->i_ext
.i_data
-
91 sizeof(struct extendedFileEntry
) :
92 sizeof(struct fileEntry
)),
93 dir
->i_sb
->s_blocksize
,
98 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
100 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
101 sizeof(struct fileIdentDesc
));
106 if (fibh
->eoffset
== dir
->i_sb
->s_blocksize
) {
107 int lextoffset
= epos
->offset
;
108 unsigned char blocksize_bits
= dir
->i_sb
->s_blocksize_bits
;
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
<< blocksize_bits
) >= *elen
)
121 epos
->offset
= lextoffset
;
124 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
127 fibh
->soffset
= fibh
->eoffset
= 0;
129 if (!(*offset
& ((16 >> (blocksize_bits
- 9)) - 1))) {
130 i
= 16 >> (blocksize_bits
- 9);
131 if (i
+ *offset
> (*elen
>> blocksize_bits
))
132 i
= (*elen
>> blocksize_bits
)-*offset
;
133 for (num
= 0; i
> 0; i
--) {
134 block
= udf_get_lb_pblock(dir
->i_sb
, *eloc
,
136 tmp
= udf_tgetblk(dir
->i_sb
, block
);
137 if (tmp
&& !buffer_uptodate(tmp
) &&
144 ll_rw_block(READA
, num
, bha
);
145 for (i
= 0; i
< num
; i
++)
149 } else if (fibh
->sbh
!= fibh
->ebh
) {
151 fibh
->sbh
= fibh
->ebh
;
154 fi
= udf_get_fileident(fibh
->sbh
->b_data
, dir
->i_sb
->s_blocksize
,
160 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
162 if (fibh
->eoffset
<= dir
->i_sb
->s_blocksize
) {
163 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
164 sizeof(struct fileIdentDesc
));
165 } else if (fibh
->eoffset
> dir
->i_sb
->s_blocksize
) {
166 int lextoffset
= epos
->offset
;
168 if (udf_next_aext(dir
, epos
, eloc
, elen
, 1) !=
169 (EXT_RECORDED_ALLOCATED
>> 30))
172 block
= udf_get_lb_pblock(dir
->i_sb
, *eloc
, *offset
);
176 if ((*offset
<< dir
->i_sb
->s_blocksize_bits
) >= *elen
)
179 epos
->offset
= lextoffset
;
181 fibh
->soffset
-= dir
->i_sb
->s_blocksize
;
182 fibh
->eoffset
-= dir
->i_sb
->s_blocksize
;
184 fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
188 if (sizeof(struct fileIdentDesc
) > -fibh
->soffset
) {
191 memcpy((uint8_t *)cfi
, (uint8_t *)fi
, -fibh
->soffset
);
192 memcpy((uint8_t *)cfi
- fibh
->soffset
,
194 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
196 fi_len
= (sizeof(struct fileIdentDesc
) +
197 cfi
->lengthFileIdent
+
198 le16_to_cpu(cfi
->lengthOfImpUse
) + 3) & ~3;
200 *nf_pos
+= fi_len
- (fibh
->eoffset
- fibh
->soffset
);
201 fibh
->eoffset
= fibh
->soffset
+ fi_len
;
203 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
204 sizeof(struct fileIdentDesc
));
210 struct fileIdentDesc
*udf_get_fileident(void *buffer
, int bufsize
, int *offset
)
212 struct fileIdentDesc
*fi
;
217 if ((!buffer
) || (!offset
)) {
218 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer
,
225 if ((*offset
> 0) && (*offset
< bufsize
))
227 fi
= (struct fileIdentDesc
*)ptr
;
228 if (fi
->descTag
.tagIdent
!= cpu_to_le16(TAG_IDENT_FID
)) {
229 udf_debug("0x%x != TAG_IDENT_FID\n",
230 le16_to_cpu(fi
->descTag
.tagIdent
));
231 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
232 *offset
, (unsigned long)sizeof(struct fileIdentDesc
),
236 if ((*offset
+ sizeof(struct fileIdentDesc
)) > bufsize
)
237 lengthThisIdent
= sizeof(struct fileIdentDesc
);
239 lengthThisIdent
= sizeof(struct fileIdentDesc
) +
240 fi
->lengthFileIdent
+ le16_to_cpu(fi
->lengthOfImpUse
);
242 /* we need to figure padding, too! */
243 padlen
= lengthThisIdent
% UDF_NAME_PAD
;
245 lengthThisIdent
+= (UDF_NAME_PAD
- padlen
);
246 *offset
= *offset
+ lengthThisIdent
;
252 static extent_ad
*udf_get_fileextent(void *buffer
, int bufsize
, int *offset
)
255 struct fileEntry
*fe
;
258 if ((!buffer
) || (!offset
)) {
259 printk(KERN_ERR
"udf: udf_get_fileextent() invalidparms\n");
263 fe
= (struct fileEntry
*)buffer
;
265 if (fe
->descTag
.tagIdent
!= cpu_to_le16(TAG_IDENT_FE
)) {
266 udf_debug("0x%x != TAG_IDENT_FE\n",
267 le16_to_cpu(fe
->descTag
.tagIdent
));
271 ptr
= (uint8_t *)(fe
->extendedAttr
) +
272 le32_to_cpu(fe
->lengthExtendedAttr
);
274 if ((*offset
> 0) && (*offset
< le32_to_cpu(fe
->lengthAllocDescs
)))
277 ext
= (extent_ad
*)ptr
;
279 *offset
= *offset
+ sizeof(extent_ad
);
284 short_ad
*udf_get_fileshortad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
,
289 if ((!ptr
) || (!offset
)) {
290 printk(KERN_ERR
"udf: udf_get_fileshortad() invalidparms\n");
294 if ((*offset
+ sizeof(short_ad
)) > maxoffset
)
297 sa
= (short_ad
*)ptr
;
298 if (sa
->extLength
== 0)
303 *offset
+= sizeof(short_ad
);
307 long_ad
*udf_get_filelongad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
, int inc
)
311 if ((!ptr
) || (!offset
)) {
312 printk(KERN_ERR
"udf: udf_get_filelongad() invalidparms\n");
316 if ((*offset
+ sizeof(long_ad
)) > maxoffset
)
320 if (la
->extLength
== 0)
325 *offset
+= sizeof(long_ad
);