blob: ff8c08fd7bf533ff53d6e659f284edb9c23e5a1d [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 Gorcunov28de7942007-07-21 04:37:18 -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 Gorcunov28de7942007-07-21 04:37:18 -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 Gorcunov28de7942007-07-21 04:37:18 -070063 memcpy((uint8_t *)ad + remainder, (*bh)->b_data, ad_size - remainder);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *offset = ad_size - remainder;
65 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070066
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 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)),
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070089 dir->i_sb->s_blocksize, &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 if (!fi)
91 return NULL;
92
93 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
94
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070095 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070096 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98 return fi;
99 }
100
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700101 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700102 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Jan Karaff116fc2007-05-08 00:35:14 -0700104 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700105 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
109
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
113 *offset = 0;
114 else
Jan Karaff116fc2007-05-08 00:35:14 -0700115 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Jan Kara3bf25cb2007-05-08 00:35:16 -0700117 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 if (!(fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block)))
119 return NULL;
120 fibh->soffset = fibh->eoffset = 0;
121
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700122 if (!(*offset & ((16 >> (dir->i_sb->s_blocksize_bits - 9)) - 1))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 i = 16 >> (dir->i_sb->s_blocksize_bits - 9);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700124 if (i + *offset > (*elen >> dir->i_sb->s_blocksize_bits))
125 i = (*elen >> dir->i_sb->s_blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700126 for (num = 0; i > 0; i--) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700127 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 tmp = udf_tgetblk(dir->i_sb, block);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700129 if (tmp && !buffer_uptodate(tmp) && !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 bha[num++] = tmp;
131 else
132 brelse(tmp);
133 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700134 if (num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 ll_rw_block(READA, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700136 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 brelse(bha[i]);
138 }
139 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700140 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700141 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 fibh->sbh = fibh->ebh;
143 }
144
145 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700146 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 if (!fi)
149 return NULL;
150
151 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
152
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700153 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700154 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700155 sizeof(struct fileIdentDesc));
156 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700157 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
Jan Karaff116fc2007-05-08 00:35:14 -0700159 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
164
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
168 *offset = 0;
169 else
Jan Karaff116fc2007-05-08 00:35:14 -0700170 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171
172 fibh->soffset -= dir->i_sb->s_blocksize;
173 fibh->eoffset -= dir->i_sb->s_blocksize;
174
175 if (!(fibh->ebh = udf_tread(dir->i_sb, block)))
176 return NULL;
177
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700178 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int fi_len;
180
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700181 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
182 memcpy((uint8_t *)cfi - fibh->soffset, fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700183 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700185 fi_len = (sizeof(struct fileIdentDesc) + cfi->lengthFileIdent +
186 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700188 *nf_pos += ((fi_len - (fibh->eoffset - fibh->soffset)) >> 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700190 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700191 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700192 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 }
194 }
195 return fi;
196}
197
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 struct fileIdentDesc *fi;
201 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700202 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 int padlen;
204
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if ((!buffer) || (!offset)) {
206 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
207 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return NULL;
209 }
210
211 ptr = buffer;
212
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700213 if ((*offset > 0) && (*offset < bufsize)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 ptr += *offset;
215 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700216 fi = (struct fileIdentDesc *)ptr;
217 if (le16_to_cpu(fi->descTag.tagIdent) != TAG_IDENT_FID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700219 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700221 *offset, (unsigned long)sizeof(struct fileIdentDesc),
222 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 return NULL;
224 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700225 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 lengthThisIdent = sizeof(struct fileIdentDesc);
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700227 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700229 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 /* we need to figure padding, too! */
233 padlen = lengthThisIdent % UDF_NAME_PAD;
234 if (padlen)
235 lengthThisIdent += (UDF_NAME_PAD - padlen);
236 *offset = *offset + lengthThisIdent;
237
238 return fi;
239}
240
241#if 0
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700242static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700244 extent_ad *ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 struct fileEntry *fe;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700246 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700248 if ((!buffer) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
250 return NULL;
251 }
252
253 fe = (struct fileEntry *)buffer;
254
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700255 if (le16_to_cpu(fe->descTag.tagIdent) != TAG_IDENT_FE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 udf_debug("0x%x != TAG_IDENT_FE\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700257 le16_to_cpu(fe->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 return NULL;
259 }
260
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700261 ptr = (uint8_t *)(fe->extendedAttr) + le32_to_cpu(fe->lengthExtendedAttr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700263 if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 ptr += *offset;
265 }
266
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700267 ext = (extent_ad *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
269 *offset = *offset + sizeof(extent_ad);
270 return ext;
271}
272#endif
273
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700274short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, int *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700275 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
277 short_ad *sa;
278
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700279 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
281 return NULL;
282 }
283
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700284 if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 return NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700286 else if ((sa = (short_ad *)ptr)->extLength == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return NULL;
288
289 if (inc)
290 *offset += sizeof(short_ad);
291 return sa;
292}
293
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700294long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, int *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 long_ad *la;
297
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700298 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
300 return NULL;
301 }
302
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700303 if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return NULL;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700305 else if ((la = (long_ad *)ptr)->extLength == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 return NULL;
307
308 if (inc)
309 *offset += sizeof(long_ad);
310 return la;
311}