blob: 1d2c570704c8f217ecf756a9b08c63457e139484 [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,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020023 uint8_t ad_size, struct kernel_lb_addr fe_loc,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070024 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,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020078 struct kernel_lb_addr *eloc, uint32_t *elen,
Marcin Slusarz4b111112008-02-08 04:20:36 -080079 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];
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080084 struct udf_inode_info *iinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 fibh->soffset = fibh->eoffset;
87
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080088 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
89 fi = udf_get_fileident(iinfo->i_ext.i_data -
90 (iinfo->i_efe ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091 sizeof(struct extendedFileEntry) :
92 sizeof(struct fileEntry)),
Marcin Slusarz4b111112008-02-08 04:20:36 -080093 dir->i_sb->s_blocksize,
94 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (!fi)
96 return NULL;
97
Jan Karaaf793292008-02-08 04:20:50 -080098 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700100 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700101 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 return fi;
104 }
105
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700106 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700107 int lextoffset = epos->offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800108 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Jan Karaff116fc2007-05-08 00:35:14 -0700110 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Pekka Enberg97e961f2008-10-15 12:29:03 +0200114 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Marcin Slusarz4b111112008-02-08 04:20:36 -0800118 if ((*offset << blocksize_bits) >= *elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *offset = 0;
120 else
Jan Karaff116fc2007-05-08 00:35:14 -0700121 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Jan Kara3bf25cb2007-05-08 00:35:16 -0700123 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800124 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
125 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 return NULL;
127 fibh->soffset = fibh->eoffset = 0;
128
Marcin Slusarz4b111112008-02-08 04:20:36 -0800129 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
130 i = 16 >> (blocksize_bits - 9);
131 if (i + *offset > (*elen >> blocksize_bits))
132 i = (*elen >> blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700133 for (num = 0; i > 0; i--) {
Pekka Enberg97e961f2008-10-15 12:29:03 +0200134 block = udf_get_lb_pblock(dir->i_sb, eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -0800135 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 tmp = udf_tgetblk(dir->i_sb, block);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800137 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
Jan Karaaf793292008-02-08 04:20:50 -0800160 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700163 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 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
Pekka Enberg97e961f2008-10-15 12:29:03 +0200172 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
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
Marcin Slusarz4b111112008-02-08 04:20:36 -0800184 fibh->ebh = udf_tread(dir->i_sb, block);
185 if (!fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 return NULL;
187
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700188 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 int fi_len;
190
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700191 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800192 memcpy((uint8_t *)cfi - fibh->soffset,
193 fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700194 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Marcin Slusarz4b111112008-02-08 04:20:36 -0800196 fi_len = (sizeof(struct fileIdentDesc) +
197 cfi->lengthFileIdent +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700198 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
Jan Karaaf793292008-02-08 04:20:50 -0800200 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
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
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200252static struct extent_ad *udf_get_fileextent(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200254 struct 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
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200277 ext = (struct extent_ad *)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200279 *offset = *offset + sizeof(struct extent_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 return ext;
281}
282#endif
283
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200284struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700285 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200287 struct short_ad *sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
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
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200294 if ((*offset + sizeof(struct short_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800296 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200297 sa = (struct short_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800298 if (sa->extLength == 0)
299 return NULL;
300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
302 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200303 *offset += sizeof(struct short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 return sa;
305}
306
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200307struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200309 struct long_ad *la;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
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
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200316 if ((*offset + sizeof(struct long_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800318 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200319 la = (struct long_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800320 if (la->extLength == 0)
321 return NULL;
322 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200325 *offset += sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return la;
327}