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>
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;
30 struct buffer_head *tmp, *bha[16];
31 struct udf_inode_info *iinfo = UDF_I(dir);
33 fibh->soffset = fibh->eoffset;
35 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
36 fi = udf_get_fileident(iinfo->i_ext.i_data -
38 sizeof(struct extendedFileEntry) :
39 sizeof(struct fileEntry)),
40 dir->i_sb->s_blocksize,
45 *nf_pos += fibh->eoffset - fibh->soffset;
47 memcpy((uint8_t *)cfi, (uint8_t *)fi,
48 sizeof(struct fileIdentDesc));
53 if (fibh->eoffset == dir->i_sb->s_blocksize) {
54 int lextoffset = epos->offset;
55 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
57 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
58 (EXT_RECORDED_ALLOCATED >> 30))
61 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
65 if ((*offset << blocksize_bits) >= *elen)
68 epos->offset = lextoffset;
71 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
74 fibh->soffset = fibh->eoffset = 0;
76 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
77 i = 16 >> (blocksize_bits - 9);
78 if (i + *offset > (*elen >> blocksize_bits))
79 i = (*elen >> blocksize_bits)-*offset;
80 for (num = 0; i > 0; i--) {
81 block = udf_get_lb_pblock(dir->i_sb, eloc,
83 tmp = udf_tgetblk(dir->i_sb, block);
84 if (tmp && !buffer_uptodate(tmp) &&
91 ll_rw_block(READA, num, bha);
92 for (i = 0; i < num; i++)
96 } else if (fibh->sbh != fibh->ebh) {
98 fibh->sbh = fibh->ebh;
101 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
107 *nf_pos += fibh->eoffset - fibh->soffset;
109 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
110 memcpy((uint8_t *)cfi, (uint8_t *)fi,
111 sizeof(struct fileIdentDesc));
112 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
113 int lextoffset = epos->offset;
115 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
116 (EXT_RECORDED_ALLOCATED >> 30))
119 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
123 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
126 epos->offset = lextoffset;
128 fibh->soffset -= dir->i_sb->s_blocksize;
129 fibh->eoffset -= dir->i_sb->s_blocksize;
131 fibh->ebh = udf_tread(dir->i_sb, block);
135 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
138 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
139 memcpy((uint8_t *)cfi - fibh->soffset,
141 sizeof(struct fileIdentDesc) + fibh->soffset);
143 fi_len = (sizeof(struct fileIdentDesc) +
144 cfi->lengthFileIdent +
145 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
147 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
148 fibh->eoffset = fibh->soffset + fi_len;
150 memcpy((uint8_t *)cfi, (uint8_t *)fi,
151 sizeof(struct fileIdentDesc));
157 struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
159 struct fileIdentDesc *fi;
164 if ((!buffer) || (!offset)) {
165 udf_debug("invalidparms, buffer=%p, offset=%p\n",
172 if ((*offset > 0) && (*offset < bufsize))
174 fi = (struct fileIdentDesc *)ptr;
175 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
176 udf_debug("0x%x != TAG_IDENT_FID\n",
177 le16_to_cpu(fi->descTag.tagIdent));
178 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
179 *offset, (unsigned long)sizeof(struct fileIdentDesc),
183 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
184 lengthThisIdent = sizeof(struct fileIdentDesc);
186 lengthThisIdent = sizeof(struct fileIdentDesc) +
187 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
189 /* we need to figure padding, too! */
190 padlen = lengthThisIdent % UDF_NAME_PAD;
192 lengthThisIdent += (UDF_NAME_PAD - padlen);
193 *offset = *offset + lengthThisIdent;
198 struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
203 if ((!ptr) || (!offset)) {
204 pr_err("%s: invalidparms\n", __func__);
208 if ((*offset + sizeof(struct short_ad)) > maxoffset)
211 sa = (struct short_ad *)ptr;
212 if (sa->extLength == 0)
217 *offset += sizeof(struct short_ad);
221 struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
225 if ((!ptr) || (!offset)) {
226 pr_err("%s: invalidparms\n", __func__);
230 if ((*offset + sizeof(struct long_ad)) > maxoffset)
233 la = (struct long_ad *)ptr;
234 if (la->extLength == 0)
239 *offset += sizeof(struct long_ad);