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 if (!(*bh
= udf_tread(dir
->i_sb
, block
)))
50 } else if (*offset
> dir
->i_sb
->s_blocksize
) {
53 remainder
= dir
->i_sb
->s_blocksize
- loffset
;
54 memcpy((uint8_t *)ad
, (*bh
)->b_data
+ loffset
, remainder
);
57 block
= udf_get_lb_pblock(dir
->i_sb
, fe_loc
, ++*pos
);
60 if (!((*bh
) = udf_tread(dir
->i_sb
, block
)))
63 memcpy((uint8_t *)ad
+ remainder
, (*bh
)->b_data
, ad_size
- remainder
);
64 *offset
= ad_size
- remainder
;
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
,
78 struct fileIdentDesc
*fi
;
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
) -
87 sizeof(struct extendedFileEntry
) :
88 sizeof(struct fileEntry
)),
89 dir
->i_sb
->s_blocksize
, &(fibh
->eoffset
));
93 *nf_pos
+= ((fibh
->eoffset
- fibh
->soffset
) >> 2);
95 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
96 sizeof(struct fileIdentDesc
));
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))
108 block
= udf_get_lb_pblock(dir
->i_sb
, *eloc
, *offset
);
112 if ((*offset
<< dir
->i_sb
->s_blocksize_bits
) >= *elen
)
115 epos
->offset
= lextoffset
;
118 if (!(fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
)))
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
))
135 ll_rw_block(READA
, num
, bha
);
136 for (i
= 0; i
< num
; i
++)
140 } else if (fibh
->sbh
!= fibh
->ebh
) {
142 fibh
->sbh
= fibh
->ebh
;
145 fi
= udf_get_fileident(fibh
->sbh
->b_data
, dir
->i_sb
->s_blocksize
,
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))
163 block
= udf_get_lb_pblock(dir
->i_sb
, *eloc
, *offset
);
167 if ((*offset
<< dir
->i_sb
->s_blocksize_bits
) >= *elen
)
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
)))
178 if (sizeof(struct fileIdentDesc
) > -fibh
->soffset
) {
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
;
191 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
192 sizeof(struct fileIdentDesc
));
198 struct fileIdentDesc
*udf_get_fileident(void *buffer
, int bufsize
, int *offset
)
200 struct fileIdentDesc
*fi
;
205 if ((!buffer
) || (!offset
)) {
206 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer
,
213 if ((*offset
> 0) && (*offset
< bufsize
)) {
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
),
225 if ((*offset
+ sizeof(struct fileIdentDesc
)) > bufsize
) {
226 lengthThisIdent
= sizeof(struct fileIdentDesc
);
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
;
235 lengthThisIdent
+= (UDF_NAME_PAD
- padlen
);
236 *offset
= *offset
+ lengthThisIdent
;
242 static extent_ad
*udf_get_fileextent(void *buffer
, int bufsize
, int *offset
)
245 struct fileEntry
*fe
;
248 if ((!buffer
) || (!offset
)) {
249 printk(KERN_ERR
"udf: udf_get_fileextent() invalidparms\n");
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
));
261 ptr
= (uint8_t *)(fe
->extendedAttr
) + le32_to_cpu(fe
->lengthExtendedAttr
);
263 if ((*offset
> 0) && (*offset
< le32_to_cpu(fe
->lengthAllocDescs
))) {
267 ext
= (extent_ad
*)ptr
;
269 *offset
= *offset
+ sizeof(extent_ad
);
274 short_ad
*udf_get_fileshortad(uint8_t *ptr
, int maxoffset
, int *offset
,
279 if ((!ptr
) || (!offset
)) {
280 printk(KERN_ERR
"udf: udf_get_fileshortad() invalidparms\n");
284 if ((*offset
< 0) || ((*offset
+ sizeof(short_ad
)) > maxoffset
))
286 else if ((sa
= (short_ad
*)ptr
)->extLength
== 0)
290 *offset
+= sizeof(short_ad
);
294 long_ad
*udf_get_filelongad(uint8_t *ptr
, int maxoffset
, int *offset
, int inc
)
298 if ((!ptr
) || (!offset
)) {
299 printk(KERN_ERR
"udf: udf_get_filelongad() invalidparms\n");
303 if ((*offset
< 0) || ((*offset
+ sizeof(long_ad
)) > maxoffset
))
305 else if ((la
= (long_ad
*)ptr
)->extLength
== 0)
309 *offset
+= sizeof(long_ad
);