blob: 8adc77c1d579943b7d2b6d7e7c9f8e7875549d58 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * directory.c
3 *
4 * PURPOSE
5 * Directory related functions
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * 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#include <linux/buffer_head.h>
20
21#if 0
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070022static uint8_t *udf_filead_read(struct inode *dir, uint8_t * tmpad,
23 uint8_t ad_size, kernel_lb_addr fe_loc,
24 int *pos, int *offset, struct buffer_head **bh,
25 int *error)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 int loffset = *offset;
28 int block;
29 uint8_t *ad;
30 int remainder;
31
32 *error = 0;
33
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070034 ad = (uint8_t *) (*bh)->b_data + *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 *offset += ad_size;
36
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070037 if (!ad) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070038 brelse(*bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 *error = 1;
40 return NULL;
41 }
42
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070043 if (*offset == dir->i_sb->s_blocksize) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070044 brelse(*bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
46 if (!block)
47 return NULL;
48 if (!(*bh = udf_tread(dir->i_sb, block)))
49 return NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070050 } else if (*offset > dir->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 ad = tmpad;
52
53 remainder = dir->i_sb->s_blocksize - loffset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 memcpy((uint8_t *) ad, (*bh)->b_data + loffset, remainder);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jan Kara3bf25cb2007-05-08 00:35:16 -070056 brelse(*bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
58 if (!block)
59 return NULL;
60 if (!((*bh) = udf_tread(dir->i_sb, block)))
61 return NULL;
62
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 memcpy((uint8_t *) ad + remainder, (*bh)->b_data,
64 ad_size - remainder);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *offset = ad_size - remainder;
66 }
67 return ad;
68}
69#endif
70
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070071struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t * nf_pos,
72 struct udf_fileident_bh *fibh,
73 struct fileIdentDesc *cfi,
74 struct extent_position *epos,
75 kernel_lb_addr * eloc, uint32_t * elen,
76 sector_t * offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct fileIdentDesc *fi;
79 int i, num, block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070080 struct buffer_head *tmp, *bha[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82 fibh->soffset = fibh->eoffset;
83
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070084 if (UDF_I_ALLOCTYPE(dir) == ICBTAG_FLAG_AD_IN_ICB) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 fi = udf_get_fileident(UDF_I_DATA(dir) -
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070086 (UDF_I_EFE(dir) ?
87 sizeof(struct extendedFileEntry) :
88 sizeof(struct fileEntry)),
89 dir->i_sb->s_blocksize,
90 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (!fi)
93 return NULL;
94
95 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
96
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070097 memcpy((uint8_t *) cfi, (uint8_t *) fi,
98 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 return fi;
101 }
102
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700104 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jan Karaff116fc2007-05-08 00:35:14 -0700106 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700107 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
111
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700112 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
115 *offset = 0;
116 else
Jan Karaff116fc2007-05-08 00:35:14 -0700117 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Jan Kara3bf25cb2007-05-08 00:35:16 -0700119 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
121 return NULL;
122 fibh->soffset = fibh->eoffset = 0;
123
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700124 if (!
125 (*offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 {
127 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700128 if (i + *offset >
129 (*elen >> dir->i_sb->s_blocksize_bits))
130 i = (*elen >> dir->i_sb->s_blocksize_bits) -
131 *offset;
132 for (num = 0; i > 0; i--) {
133 block =
134 udf_get_lb_pblock(dir->i_sb, *eloc,
135 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 tmp = udf_tgetblk(dir->i_sb, block);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700137 if (tmp && !buffer_uptodate(tmp)
138 && !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 bha[num++] = tmp;
140 else
141 brelse(tmp);
142 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700143 if (num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 ll_rw_block(READA, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700145 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 brelse(bha[i]);
147 }
148 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700150 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 fibh->sbh = fibh->ebh;
152 }
153
154 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!fi)
158 return NULL;
159
160 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
161
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
163 memcpy((uint8_t *) cfi, (uint8_t *) fi,
164 sizeof(struct fileIdentDesc));
165 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700166 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Jan Karaff116fc2007-05-08 00:35:14 -0700168 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700169 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
173
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700174 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
177 *offset = 0;
178 else
Jan Karaff116fc2007-05-08 00:35:14 -0700179 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 fibh->soffset -= dir->i_sb->s_blocksize;
182 fibh->eoffset -= dir->i_sb->s_blocksize;
183
184 if (!(fibh->ebh = udf_tread(dir->i_sb, block)))
185 return NULL;
186
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700187 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int fi_len;
189
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190 memcpy((uint8_t *) cfi, (uint8_t *) fi, -fibh->soffset);
191 memcpy((uint8_t *) cfi - fibh->soffset,
192 fibh->ebh->b_data,
193 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700195 fi_len =
196 (sizeof(struct fileIdentDesc) +
197 cfi->lengthFileIdent +
198 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700200 *nf_pos +=
201 ((fi_len - (fibh->eoffset - fibh->soffset)) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 } else {
204 memcpy((uint8_t *) cfi, (uint8_t *) fi,
205 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 }
207 }
208 return fi;
209}
210
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700211struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
213 struct fileIdentDesc *fi;
214 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700215 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 int padlen;
217
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700218 if ((!buffer) || (!offset)) {
219 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
220 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 return NULL;
222 }
223
224 ptr = buffer;
225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 if ((*offset > 0) && (*offset < bufsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 ptr += *offset;
228 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700229 fi = (struct fileIdentDesc *)ptr;
230 if (le16_to_cpu(fi->descTag.tagIdent) != TAG_IDENT_FID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700234 *offset, (unsigned long)sizeof(struct fileIdentDesc),
235 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return NULL;
237 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700238 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 lengthThisIdent = sizeof(struct fileIdentDesc);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700240 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700242 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* we need to figure padding, too! */
245 padlen = lengthThisIdent % UDF_NAME_PAD;
246 if (padlen)
247 lengthThisIdent += (UDF_NAME_PAD - padlen);
248 *offset = *offset + lengthThisIdent;
249
250 return fi;
251}
252
253#if 0
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700254static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700256 extent_ad *ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct fileEntry *fe;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700258 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700260 if ((!buffer) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
262 return NULL;
263 }
264
265 fe = (struct fileEntry *)buffer;
266
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 if (le16_to_cpu(fe->descTag.tagIdent) != TAG_IDENT_FE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 udf_debug("0x%x != TAG_IDENT_FE\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700269 le16_to_cpu(fe->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 return NULL;
271 }
272
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700273 ptr =
274 (uint8_t *) (fe->extendedAttr) +
275 le32_to_cpu(fe->lengthExtendedAttr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700277 if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 ptr += *offset;
279 }
280
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700281 ext = (extent_ad *) ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
283 *offset = *offset + sizeof(extent_ad);
284 return ext;
285}
286#endif
287
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700288short_ad *udf_get_fileshortad(uint8_t * ptr, int maxoffset, int *offset,
289 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 short_ad *sa;
292
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700293 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
295 return NULL;
296 }
297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 return NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700300 else if ((sa = (short_ad *) ptr)->extLength == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 return NULL;
302
303 if (inc)
304 *offset += sizeof(short_ad);
305 return sa;
306}
307
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700308long_ad *udf_get_filelongad(uint8_t * ptr, int maxoffset, int *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 long_ad *la;
311
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700312 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
314 return NULL;
315 }
316
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700317 if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 return NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700319 else if ((la = (long_ad *) ptr)->extLength == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 return NULL;
321
322 if (inc)
323 *offset += sizeof(long_ad);
324 return la;
325}