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
));
153 /* Got last entry outside of dir size - fs is corrupted! */
154 if (*nf_pos
> dir
->i_size
)
159 struct fileIdentDesc
*udf_get_fileident(void *buffer
, int bufsize
, int *offset
)
161 struct fileIdentDesc
*fi
;
166 if ((!buffer
) || (!offset
)) {
167 udf_debug("invalidparms, buffer=%p, offset=%p\n",
174 if ((*offset
> 0) && (*offset
< bufsize
))
176 fi
= (struct fileIdentDesc
*)ptr
;
177 if (fi
->descTag
.tagIdent
!= cpu_to_le16(TAG_IDENT_FID
)) {
178 udf_debug("0x%x != TAG_IDENT_FID\n",
179 le16_to_cpu(fi
->descTag
.tagIdent
));
180 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
181 *offset
, (unsigned long)sizeof(struct fileIdentDesc
),
185 if ((*offset
+ sizeof(struct fileIdentDesc
)) > bufsize
)
186 lengthThisIdent
= sizeof(struct fileIdentDesc
);
188 lengthThisIdent
= sizeof(struct fileIdentDesc
) +
189 fi
->lengthFileIdent
+ le16_to_cpu(fi
->lengthOfImpUse
);
191 /* we need to figure padding, too! */
192 padlen
= lengthThisIdent
% UDF_NAME_PAD
;
194 lengthThisIdent
+= (UDF_NAME_PAD
- padlen
);
195 *offset
= *offset
+ lengthThisIdent
;
200 struct short_ad
*udf_get_fileshortad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
,
205 if ((!ptr
) || (!offset
)) {
206 pr_err("%s: invalidparms\n", __func__
);
210 if ((*offset
+ sizeof(struct short_ad
)) > maxoffset
)
213 sa
= (struct short_ad
*)ptr
;
214 if (sa
->extLength
== 0)
219 *offset
+= sizeof(struct short_ad
);
223 struct long_ad
*udf_get_filelongad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
, int inc
)
227 if ((!ptr
) || (!offset
)) {
228 pr_err("%s: invalidparms\n", __func__
);
232 if ((*offset
+ sizeof(struct long_ad
)) > maxoffset
)
235 la
= (struct long_ad
*)ptr
;
236 if (la
->extLength
== 0)
241 *offset
+= sizeof(struct long_ad
);