1 /*
2 * directory.c
3 *
4 * PURPOSE
5 * Directory related functions
6 *
7 * COPYRIGHT
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.
12 */
13
14 #include "udfdecl.h"
15 #include "udf_i.h"
16
17 #include <linux/fs.h>
18 #include <linux/string.h>
19
udf_fileident_read(struct inode * dir,loff_t * nf_pos,struct udf_fileident_bh * fibh,struct fileIdentDesc * cfi,struct extent_position * epos,struct kernel_lb_addr * eloc,uint32_t * elen,sector_t * offset)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,
25 sector_t *offset)
26 {
27 struct fileIdentDesc *fi;
28 int i, num, block;
29 struct buffer_head *tmp, *bha[16];
30 struct udf_inode_info *iinfo = UDF_I(dir);
31
32 fibh->soffset = fibh->eoffset;
33
34 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
35 fi = udf_get_fileident(iinfo->i_ext.i_data -
36 (iinfo->i_efe ?
37 sizeof(struct extendedFileEntry) :
38 sizeof(struct fileEntry)),
39 dir->i_sb->s_blocksize,
40 &(fibh->eoffset));
41 if (!fi)
42 return NULL;
43
44 *nf_pos += fibh->eoffset - fibh->soffset;
45
46 memcpy((uint8_t *)cfi, (uint8_t *)fi,
47 sizeof(struct fileIdentDesc));
48
49 return fi;
50 }
51
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;
55
56 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
57 (EXT_RECORDED_ALLOCATED >> 30))
58 return NULL;
59
60 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
61
62 (*offset)++;
63
64 if ((*offset << blocksize_bits) >= *elen)
65 *offset = 0;
66 else
67 epos->offset = lextoffset;
68
69 brelse(fibh->sbh);
70 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
71 if (!fibh->sbh)
72 return NULL;
73 fibh->soffset = fibh->eoffset = 0;
74
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,
81 *offset + i);
82 tmp = udf_tgetblk(dir->i_sb, block);
83 if (tmp && !buffer_uptodate(tmp) &&
84 !buffer_locked(tmp))
85 bha[num++] = tmp;
86 else
87 brelse(tmp);
88 }
89 if (num) {
90 ll_rw_block(READA, num, bha);
91 for (i = 0; i < num; i++)
92 brelse(bha[i]);
93 }
94 }
95 } else if (fibh->sbh != fibh->ebh) {
96 brelse(fibh->sbh);
97 fibh->sbh = fibh->ebh;
98 }
99
100 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
101 &(fibh->eoffset));
102
103 if (!fi)
104 return NULL;
105
106 *nf_pos += fibh->eoffset - fibh->soffset;
107
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;
113
114 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
115 (EXT_RECORDED_ALLOCATED >> 30))
116 return NULL;
117
118 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
119
120 (*offset)++;
121
122 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
123 *offset = 0;
124 else
125 epos->offset = lextoffset;
126
127 fibh->soffset -= dir->i_sb->s_blocksize;
128 fibh->eoffset -= dir->i_sb->s_blocksize;
129
130 fibh->ebh = udf_tread(dir->i_sb, block);
131 if (!fibh->ebh)
132 return NULL;
133
134 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
135 int fi_len;
136
137 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
138 memcpy((uint8_t *)cfi - fibh->soffset,
139 fibh->ebh->b_data,
140 sizeof(struct fileIdentDesc) + fibh->soffset);
141
142 fi_len = (sizeof(struct fileIdentDesc) +
143 cfi->lengthFileIdent +
144 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
145
146 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
147 fibh->eoffset = fibh->soffset + fi_len;
148 } else {
149 memcpy((uint8_t *)cfi, (uint8_t *)fi,
150 sizeof(struct fileIdentDesc));
151 }
152 }
153 /* Got last entry outside of dir size - fs is corrupted! */
154 if (*nf_pos > dir->i_size)
155 return NULL;
156 return fi;
157 }
158
udf_get_fileident(void * buffer,int bufsize,int * offset)159 struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
160 {
161 struct fileIdentDesc *fi;
162 int lengthThisIdent;
163 uint8_t *ptr;
164 int padlen;
165
166 if ((!buffer) || (!offset)) {
167 udf_debug("invalidparms, buffer=%p, offset=%p\n",
168 buffer, offset);
169 return NULL;
170 }
171
172 ptr = buffer;
173
174 if ((*offset > 0) && (*offset < bufsize))
175 ptr += *offset;
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),
182 bufsize);
183 return NULL;
184 }
185 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
186 lengthThisIdent = sizeof(struct fileIdentDesc);
187 else
188 lengthThisIdent = sizeof(struct fileIdentDesc) +
189 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
190
191 /* we need to figure padding, too! */
192 padlen = lengthThisIdent % UDF_NAME_PAD;
193 if (padlen)
194 lengthThisIdent += (UDF_NAME_PAD - padlen);
195 *offset = *offset + lengthThisIdent;
196
197 return fi;
198 }
199
udf_get_fileshortad(uint8_t * ptr,int maxoffset,uint32_t * offset,int inc)200 struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
201 int inc)
202 {
203 struct short_ad *sa;
204
205 if ((!ptr) || (!offset)) {
206 pr_err("%s: invalidparms\n", __func__);
207 return NULL;
208 }
209
210 if ((*offset + sizeof(struct short_ad)) > maxoffset)
211 return NULL;
212 else {
213 sa = (struct short_ad *)ptr;
214 if (sa->extLength == 0)
215 return NULL;
216 }
217
218 if (inc)
219 *offset += sizeof(struct short_ad);
220 return sa;
221 }
222
udf_get_filelongad(uint8_t * ptr,int maxoffset,uint32_t * offset,int inc)223 struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
224 {
225 struct long_ad *la;
226
227 if ((!ptr) || (!offset)) {
228 pr_err("%s: invalidparms\n", __func__);
229 return NULL;
230 }
231
232 if ((*offset + sizeof(struct long_ad)) > maxoffset)
233 return NULL;
234 else {
235 la = (struct long_ad *)ptr;
236 if (la->extLength == 0)
237 return NULL;
238 }
239
240 if (inc)
241 *offset += sizeof(struct long_ad);
242 return la;
243 }
244