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/bio.h>
21 struct fileIdentDesc
*udf_fileident_read(struct inode
*dir
, loff_t
*nf_pos
,
22 struct udf_fileident_bh
*fibh
,
23 struct fileIdentDesc
*cfi
,
24 struct extent_position
*epos
,
25 struct kernel_lb_addr
*eloc
, uint32_t *elen
,
28 struct fileIdentDesc
*fi
;
31 struct buffer_head
*tmp
, *bha
[16];
32 struct udf_inode_info
*iinfo
= UDF_I(dir
);
34 fibh
->soffset
= fibh
->eoffset
;
36 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
37 fi
= udf_get_fileident(iinfo
->i_ext
.i_data
-
39 sizeof(struct extendedFileEntry
) :
40 sizeof(struct fileEntry
)),
41 dir
->i_sb
->s_blocksize
,
46 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
48 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
49 sizeof(struct fileIdentDesc
));
54 if (fibh
->eoffset
== dir
->i_sb
->s_blocksize
) {
55 uint32_t lextoffset
= epos
->offset
;
56 unsigned char blocksize_bits
= dir
->i_sb
->s_blocksize_bits
;
58 if (udf_next_aext(dir
, epos
, eloc
, elen
, 1) !=
59 (EXT_RECORDED_ALLOCATED
>> 30))
62 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, *offset
);
66 if ((*offset
<< blocksize_bits
) >= *elen
)
69 epos
->offset
= lextoffset
;
72 fibh
->sbh
= fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
75 fibh
->soffset
= fibh
->eoffset
= 0;
77 if (!(*offset
& ((16 >> (blocksize_bits
- 9)) - 1))) {
78 i
= 16 >> (blocksize_bits
- 9);
79 if (i
+ *offset
> (*elen
>> blocksize_bits
))
80 i
= (*elen
>> blocksize_bits
)-*offset
;
81 for (num
= 0; i
> 0; i
--) {
82 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
,
84 tmp
= udf_tgetblk(dir
->i_sb
, block
);
85 if (tmp
&& !buffer_uptodate(tmp
) &&
92 ll_rw_block(REQ_OP_READ
, REQ_RAHEAD
, num
, bha
);
93 for (i
= 0; i
< num
; i
++)
97 } else if (fibh
->sbh
!= fibh
->ebh
) {
99 fibh
->sbh
= fibh
->ebh
;
102 fi
= udf_get_fileident(fibh
->sbh
->b_data
, dir
->i_sb
->s_blocksize
,
108 *nf_pos
+= fibh
->eoffset
- fibh
->soffset
;
110 if (fibh
->eoffset
<= dir
->i_sb
->s_blocksize
) {
111 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
112 sizeof(struct fileIdentDesc
));
113 } else if (fibh
->eoffset
> dir
->i_sb
->s_blocksize
) {
114 uint32_t lextoffset
= epos
->offset
;
116 if (udf_next_aext(dir
, epos
, eloc
, elen
, 1) !=
117 (EXT_RECORDED_ALLOCATED
>> 30))
120 block
= udf_get_lb_pblock(dir
->i_sb
, eloc
, *offset
);
124 if ((*offset
<< dir
->i_sb
->s_blocksize_bits
) >= *elen
)
127 epos
->offset
= lextoffset
;
129 fibh
->soffset
-= dir
->i_sb
->s_blocksize
;
130 fibh
->eoffset
-= dir
->i_sb
->s_blocksize
;
132 fibh
->ebh
= udf_tread(dir
->i_sb
, block
);
136 if (sizeof(struct fileIdentDesc
) > -fibh
->soffset
) {
139 memcpy((uint8_t *)cfi
, (uint8_t *)fi
, -fibh
->soffset
);
140 memcpy((uint8_t *)cfi
- fibh
->soffset
,
142 sizeof(struct fileIdentDesc
) + fibh
->soffset
);
144 fi_len
= udf_dir_entry_len(cfi
);
145 *nf_pos
+= fi_len
- (fibh
->eoffset
- fibh
->soffset
);
146 fibh
->eoffset
= fibh
->soffset
+ fi_len
;
148 memcpy((uint8_t *)cfi
, (uint8_t *)fi
,
149 sizeof(struct fileIdentDesc
));
152 /* Got last entry outside of dir size - fs is corrupted! */
153 if (*nf_pos
> dir
->i_size
)
158 struct fileIdentDesc
*udf_get_fileident(void *buffer
, int bufsize
, int *offset
)
160 struct fileIdentDesc
*fi
;
165 if ((!buffer
) || (!offset
)) {
166 udf_debug("invalidparms, buffer=%p, offset=%p\n",
173 if ((*offset
> 0) && (*offset
< bufsize
))
175 fi
= (struct fileIdentDesc
*)ptr
;
176 if (fi
->descTag
.tagIdent
!= cpu_to_le16(TAG_IDENT_FID
)) {
177 udf_debug("0x%x != TAG_IDENT_FID\n",
178 le16_to_cpu(fi
->descTag
.tagIdent
));
179 udf_debug("offset: %d sizeof: %lu bufsize: %d\n",
180 *offset
, (unsigned long)sizeof(struct fileIdentDesc
),
184 if ((*offset
+ sizeof(struct fileIdentDesc
)) > bufsize
)
185 lengthThisIdent
= sizeof(struct fileIdentDesc
);
187 lengthThisIdent
= sizeof(struct fileIdentDesc
) +
188 fi
->lengthFileIdent
+ le16_to_cpu(fi
->lengthOfImpUse
);
190 /* we need to figure padding, too! */
191 padlen
= lengthThisIdent
% UDF_NAME_PAD
;
193 lengthThisIdent
+= (UDF_NAME_PAD
- padlen
);
194 *offset
= *offset
+ lengthThisIdent
;
199 struct short_ad
*udf_get_fileshortad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
,
204 if ((!ptr
) || (!offset
)) {
205 pr_err("%s: invalidparms\n", __func__
);
209 if ((*offset
+ sizeof(struct short_ad
)) > maxoffset
)
212 sa
= (struct short_ad
*)ptr
;
213 if (sa
->extLength
== 0)
218 *offset
+= sizeof(struct short_ad
);
222 struct long_ad
*udf_get_filelongad(uint8_t *ptr
, int maxoffset
, uint32_t *offset
, int inc
)
226 if ((!ptr
) || (!offset
)) {
227 pr_err("%s: invalidparms\n", __func__
);
231 if ((*offset
+ sizeof(struct long_ad
)) > maxoffset
)
234 la
= (struct long_ad
*)ptr
;
235 if (la
->extLength
== 0)
240 *offset
+= sizeof(struct long_ad
);