blob: 949a5930f6d4480cfb87aadf65fe2c7308aabd42 [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
Marcin Slusarz4b111112008-02-08 04:20:36 -080022static uint8_t *udf_filead_read(struct inode *dir, uint8_t *tmpad,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070023 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;
Marcin Slusarz4b111112008-02-08 04:20:36 -080048 *bh = udf_tread(dir->i_sb, block);
49 if (!*bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return NULL;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070051 } else if (*offset > dir->i_sb->s_blocksize) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 ad = tmpad;
53
54 remainder = dir->i_sb->s_blocksize - loffset;
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070055 memcpy((uint8_t *)ad, (*bh)->b_data + loffset, remainder);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jan Kara3bf25cb2007-05-08 00:35:16 -070057 brelse(*bh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 block = udf_get_lb_pblock(dir->i_sb, fe_loc, ++*pos);
59 if (!block)
60 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -080061 (*bh) = udf_tread(dir->i_sb, block);
62 if (!*bh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 return NULL;
64
Marcin Slusarz4b111112008-02-08 04:20:36 -080065 memcpy((uint8_t *)ad + remainder, (*bh)->b_data,
66 ad_size - remainder);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *offset = ad_size - remainder;
68 }
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 return ad;
71}
72#endif
73
Marcin Slusarz4b111112008-02-08 04:20:36 -080074struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070075 struct udf_fileident_bh *fibh,
76 struct fileIdentDesc *cfi,
77 struct extent_position *epos,
Marcin Slusarz4b111112008-02-08 04:20:36 -080078 kernel_lb_addr *eloc, uint32_t *elen,
79 sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct fileIdentDesc *fi;
82 int i, num, block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070083 struct buffer_head *tmp, *bha[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 fibh->soffset = fibh->eoffset;
86
Marcin Slusarzc0b34432008-02-08 04:20:42 -080087 if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
88 fi = udf_get_fileident(UDF_I(dir)->i_ext.i_data -
89 (UDF_I(dir)->i_efe ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090 sizeof(struct extendedFileEntry) :
91 sizeof(struct fileEntry)),
Marcin Slusarz4b111112008-02-08 04:20:36 -080092 dir->i_sb->s_blocksize,
93 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (!fi)
95 return NULL;
96
97 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
98
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070099 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700100 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 return fi;
103 }
104
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700105 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700106 int lextoffset = epos->offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800107 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Jan Karaff116fc2007-05-08 00:35:14 -0700109 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
114
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Marcin Slusarz4b111112008-02-08 04:20:36 -0800117 if ((*offset << blocksize_bits) >= *elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 *offset = 0;
119 else
Jan Karaff116fc2007-05-08 00:35:14 -0700120 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Jan Kara3bf25cb2007-05-08 00:35:16 -0700122 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800123 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
124 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 return NULL;
126 fibh->soffset = fibh->eoffset = 0;
127
Marcin Slusarz4b111112008-02-08 04:20:36 -0800128 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
129 i = 16 >> (blocksize_bits - 9);
130 if (i + *offset > (*elen >> blocksize_bits))
131 i = (*elen >> blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700132 for (num = 0; i > 0; i--) {
Marcin Slusarz4b111112008-02-08 04:20:36 -0800133 block = udf_get_lb_pblock(dir->i_sb, *eloc,
134 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 tmp = udf_tgetblk(dir->i_sb, block);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800136 if (tmp && !buffer_uptodate(tmp) &&
137 !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 bha[num++] = tmp;
139 else
140 brelse(tmp);
141 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700142 if (num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 ll_rw_block(READA, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700144 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 brelse(bha[i]);
146 }
147 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -0700149 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 fibh->sbh = fibh->ebh;
151 }
152
153 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700154 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!fi)
157 return NULL;
158
159 *nf_pos += ((fibh->eoffset - fibh->soffset) >> 2);
160
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700161 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700162 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 sizeof(struct fileIdentDesc));
164 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700165 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Jan Karaff116fc2007-05-08 00:35:14 -0700167 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700168 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 block = udf_get_lb_pblock(dir->i_sb, *eloc, *offset);
172
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700173 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
176 *offset = 0;
177 else
Jan Karaff116fc2007-05-08 00:35:14 -0700178 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 fibh->soffset -= dir->i_sb->s_blocksize;
181 fibh->eoffset -= dir->i_sb->s_blocksize;
182
Marcin Slusarz4b111112008-02-08 04:20:36 -0800183 fibh->ebh = udf_tread(dir->i_sb, block);
184 if (!fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 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 Gorcunov28de7942007-07-21 04:37:18 -0700190 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800191 memcpy((uint8_t *)cfi - fibh->soffset,
192 fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700193 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Marcin Slusarz4b111112008-02-08 04:20:36 -0800195 fi_len = (sizeof(struct fileIdentDesc) +
196 cfi->lengthFileIdent +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700197 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Marcin Slusarz4b111112008-02-08 04:20:36 -0800199 *nf_pos += (fi_len - (fibh->eoffset - fibh->soffset))
200 >> 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700202 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700203 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700204 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206 }
207 return fi;
208}
209
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700210struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct fileIdentDesc *fi;
213 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700214 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 int padlen;
216
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700217 if ((!buffer) || (!offset)) {
218 udf_debug("invalidparms\n, buffer=%p, offset=%p\n", buffer,
219 offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return NULL;
221 }
222
223 ptr = buffer;
224
Marcin Slusarz4b111112008-02-08 04:20:36 -0800225 if ((*offset > 0) && (*offset < bufsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 ptr += *offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 fi = (struct fileIdentDesc *)ptr;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800228 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700230 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700232 *offset, (unsigned long)sizeof(struct fileIdentDesc),
233 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return NULL;
235 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800236 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 lengthThisIdent = sizeof(struct fileIdentDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800238 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700240 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /* we need to figure padding, too! */
243 padlen = lengthThisIdent % UDF_NAME_PAD;
244 if (padlen)
245 lengthThisIdent += (UDF_NAME_PAD - padlen);
246 *offset = *offset + lengthThisIdent;
247
248 return fi;
249}
250
251#if 0
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700252static extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700254 extent_ad *ext;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 struct fileEntry *fe;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700256 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700258 if ((!buffer) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 printk(KERN_ERR "udf: udf_get_fileextent() invalidparms\n");
260 return NULL;
261 }
262
263 fe = (struct fileEntry *)buffer;
264
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800265 if (fe->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 udf_debug("0x%x != TAG_IDENT_FE\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700267 le16_to_cpu(fe->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return NULL;
269 }
270
Marcin Slusarz4b111112008-02-08 04:20:36 -0800271 ptr = (uint8_t *)(fe->extendedAttr) +
272 le32_to_cpu(fe->lengthExtendedAttr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Marcin Slusarz4b111112008-02-08 04:20:36 -0800274 if ((*offset > 0) && (*offset < le32_to_cpu(fe->lengthAllocDescs)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 ptr += *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700277 ext = (extent_ad *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 *offset = *offset + sizeof(extent_ad);
280 return ext;
281}
282#endif
283
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700284short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, int *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 short_ad *sa;
288
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700289 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 printk(KERN_ERR "udf: udf_get_fileshortad() invalidparms\n");
291 return NULL;
292 }
293
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700294 if ((*offset < 0) || ((*offset + sizeof(short_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800296 else {
297 sa = (short_ad *)ptr;
298 if (sa->extLength == 0)
299 return NULL;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (inc)
303 *offset += sizeof(short_ad);
304 return sa;
305}
306
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700307long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, int *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 long_ad *la;
310
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700311 if ((!ptr) || (!offset)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 printk(KERN_ERR "udf: udf_get_filelongad() invalidparms\n");
313 return NULL;
314 }
315
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700316 if ((*offset < 0) || ((*offset + sizeof(long_ad)) > maxoffset))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800318 else {
319 la = (long_ad *)ptr;
320 if (la->extLength == 0)
321 return NULL;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (inc)
325 *offset += sizeof(long_ad);
326 return la;
327}