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>
20 struct fileIdentDesc
*udf_fileident_read(struct inode
*dir
, loff_t
*nf_pos
,
21 struct udf_fileident_bh
*fibh
,
22 struct fileIdentDesc
*cfi
,
23 struct extent_position
*epos
,
24 struct kernel_lb_addr
*eloc
, uint32_t *elen
,
27 struct fileIdentDesc
*fi
;
29 struct buffer_head
*tmp
, *bha
[16];
30 struct udf_inode_info
*iinfo
= UDF_I(dir
);
32 fibh
->soffset
= fibh
->eoffset
;
34 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
35 fi
= udf_get_fileident(iinfo
->i_ext
.i_data
-
37 sizeof(struct extendedFileEntry
) :
38 sizeof(struct fileEntry
)),
39 dir
->i_sb
->s_blocksize
,
44 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
46 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
47 sizeof(struct fileIdentDesc
));
52 if (fibh
->eoffset
== dir
->i_sb
->s_blocksize
) {
53 int lextoffset
= epos
->offset
;
54 unsigned char blocksize_bits
= dir
->i_sb
->s_blocksize_bits
;
56 if (udf_next_aext(dir
, epos
, eloc
, elen
, 1) !=
57 (EXT_RECORDED_ALLOCATED
>> 30))
60 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, *offset
);
64 if ((*offset
<< blocksize_bits
) >= *elen
)
67 epos
->offset
= lextoffset
;
70 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
73 fibh
->soffset
= fibh
->eoffset
= 0;
75 if (!(*offset
& ((16 >> (blocksize_bits
- 9)) - 1))) {
76 i
= 16 >> (blocksize_bits
- 9);
77 if (i
+ *offset
> (*elen
>> blocksize_bits
))
78 i
= (*elen
>> blocksize_bits
)-*offset
;
79 for (num
= 0; i
> 0; i
--) {
80 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
,
82 tmp
= udf_tgetblk(dir
->i_sb
, block
);
83 if (tmp
&& !buffer_uptodate(tmp
) &&
90 ll_rw_block(READA
, num
, bha
);
91 for (i
= 0; i
< num
; i
++)
95 } else if (fibh
->sbh
!= fibh
->ebh
) {
97 fibh
->sbh
= fibh
->ebh
;
100 fi
= udf_get_fileident(fibh
->sbh
->b_data
, dir
->i_sb
->s_blocksize
,
106 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
108 if (fibh
->eoffset
<= dir
->i_sb
->s_blocksize
) {
109 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
110 sizeof(struct fileIdentDesc
));
111 } else if (fibh
->eoffset
> dir
->i_sb
->s_blocksize
) {
112 int lextoffset
= epos
->offset
;
114 if (udf_next_aext(dir
, epos
, eloc
, elen
, 1) !=
115 (EXT_RECORDED_ALLOCATED
>> 30))
118 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, *offset
);
122 if ((*offset
<< dir
->i_sb
->s_blocksize_bits
) >= *elen
)
125 epos
->offset
= lextoffset
;
127 fibh
->soffset
-= dir
->i_sb
->s_blocksize
;
128 fibh
->eoffset
-= dir
->i_sb
->s_blocksize
;
130 fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
134 if (sizeof(struct fileIdentDesc
) > -fibh
->soffset
) {
137 memcpy((uint8_t *)cfi
, (uint8_t *)fi
, -fibh
->soffset
);
138 memcpy((uint8_t *)cfi
- fibh
->soffset
,
140 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
142 fi_len
= (sizeof(struct fileIdentDesc
) +
143 cfi
->lengthFileIdent
+
144 le16_to_cpu(cfi
->lengthOfImpUse
) + 3) & ~3;
146 *nf_pos
+= fi_len
- (fibh
->eoffset
- fibh
->soffset
);
147 fibh
->eoffset
= fibh
->soffset
+ fi_len
;
149 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
150 sizeof(struct fileIdentDesc
));
156 struct fileIdentDesc
*udf_get_fileident(void *buffer
, int bufsize
, int *offset
)
158 struct fileIdentDesc
*fi
;
163 if ((!buffer
) || (!offset
)) {
164 udf_debug("invalidparms, buffer=%p, offset=%p\n",
171 if ((*offset
> 0) && (*offset
< bufsize
))
173 fi
= (struct fileIdentDesc
*)ptr
;
174 if (fi
->descTag
.tagIdent
!= cpu_to_le16(TAG_IDENT_FID
)) {
175 udf_debug("0x%x != TAG_IDENT_FID\n",
176 le16_to_cpu(fi
->descTag
.tagIdent
));
177 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
178 *offset
, (unsigned long)sizeof(struct fileIdentDesc
),
182 if ((*offset
+ sizeof(struct fileIdentDesc
)) > bufsize
)
183 lengthThisIdent
= sizeof(struct fileIdentDesc
);
185 lengthThisIdent
= sizeof(struct fileIdentDesc
) +
186 fi
->lengthFileIdent
+ le16_to_cpu(fi
->lengthOfImpUse
);
188 /* we need to figure padding, too! */
189 padlen
= lengthThisIdent
% UDF_NAME_PAD
;
191 lengthThisIdent
+= (UDF_NAME_PAD
- padlen
);
192 *offset
= *offset
+ lengthThisIdent
;
197 struct short_ad
*udf_get_fileshortad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
,
202 if ((!ptr
) || (!offset
)) {
203 pr_err("%s: invalidparms\n", __func__
);
207 if ((*offset
+ sizeof(struct short_ad
)) > maxoffset
)
210 sa
= (struct short_ad
*)ptr
;
211 if (sa
->extLength
== 0)
216 *offset
+= sizeof(struct short_ad
);
220 struct long_ad
*udf_get_filelongad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
, int inc
)
224 if ((!ptr
) || (!offset
)) {
225 pr_err("%s: invalidparms\n", __func__
);
229 if ((*offset
+ sizeof(struct long_ad
)) > maxoffset
)
232 la
= (struct long_ad
*)ptr
;
233 if (la
->extLength
== 0)
238 *offset
+= sizeof(struct long_ad
);