blob: 3e44f575fb9cbaf39d8f05bb4594807bb0a7a3be [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
Marcin Slusarz4b111112008-02-08 04:20:36 -080021struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070022 struct udf_fileident_bh *fibh,
23 struct fileIdentDesc *cfi,
24 struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020025 struct kernel_lb_addr *eloc, uint32_t *elen,
Marcin Slusarz4b111112008-02-08 04:20:36 -080026 sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
28 struct fileIdentDesc *fi;
29 int i, num, block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070030 struct buffer_head *tmp, *bha[16];
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080031 struct udf_inode_info *iinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 fibh->soffset = fibh->eoffset;
34
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080035 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
36 fi = udf_get_fileident(iinfo->i_ext.i_data -
37 (iinfo->i_efe ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070038 sizeof(struct extendedFileEntry) :
39 sizeof(struct fileEntry)),
Marcin Slusarz4b111112008-02-08 04:20:36 -080040 dir->i_sb->s_blocksize,
41 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (!fi)
43 return NULL;
44
Jan Karaaf793292008-02-08 04:20:50 -080045 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070047 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070048 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50 return fi;
51 }
52
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070053 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -070054 int lextoffset = epos->offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -080055 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Jan Karaff116fc2007-05-08 00:35:14 -070057 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070058 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Pekka Enberg97e961f2008-10-15 12:29:03 +020061 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070063 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Marcin Slusarz4b111112008-02-08 04:20:36 -080065 if ((*offset << blocksize_bits) >= *elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *offset = 0;
67 else
Jan Karaff116fc2007-05-08 00:35:14 -070068 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jan Kara3bf25cb2007-05-08 00:35:16 -070070 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -080071 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
72 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 return NULL;
74 fibh->soffset = fibh->eoffset = 0;
75
Marcin Slusarz4b111112008-02-08 04:20:36 -080076 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
77 i = 16 >> (blocksize_bits - 9);
78 if (i + *offset > (*elen >> blocksize_bits))
79 i = (*elen >> blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070080 for (num = 0; i > 0; i--) {
Pekka Enberg97e961f2008-10-15 12:29:03 +020081 block = udf_get_lb_pblock(dir->i_sb, eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -080082 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 tmp = udf_tgetblk(dir->i_sb, block);
Marcin Slusarz4b111112008-02-08 04:20:36 -080084 if (tmp && !buffer_uptodate(tmp) &&
85 !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 bha[num++] = tmp;
87 else
88 brelse(tmp);
89 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070090 if (num) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 ll_rw_block(READA, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070092 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 brelse(bha[i]);
94 }
95 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070096 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070097 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 fibh->sbh = fibh->ebh;
99 }
100
101 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700102 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (!fi)
105 return NULL;
106
Jan Karaaf793292008-02-08 04:20:50 -0800107 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700109 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700110 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700111 sizeof(struct fileIdentDesc));
112 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700113 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Jan Karaff116fc2007-05-08 00:35:14 -0700115 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700116 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Pekka Enberg97e961f2008-10-15 12:29:03 +0200119 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700121 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
124 *offset = 0;
125 else
Jan Karaff116fc2007-05-08 00:35:14 -0700126 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 fibh->soffset -= dir->i_sb->s_blocksize;
129 fibh->eoffset -= dir->i_sb->s_blocksize;
130
Marcin Slusarz4b111112008-02-08 04:20:36 -0800131 fibh->ebh = udf_tread(dir->i_sb, block);
132 if (!fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 return NULL;
134
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700135 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 int fi_len;
137
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700138 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800139 memcpy((uint8_t *)cfi - fibh->soffset,
140 fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700141 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Marcin Slusarz4b111112008-02-08 04:20:36 -0800143 fi_len = (sizeof(struct fileIdentDesc) +
144 cfi->lengthFileIdent +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700145 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Jan Karaaf793292008-02-08 04:20:50 -0800147 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700150 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700151 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153 }
154 return fi;
155}
156
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700157struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct fileIdentDesc *fi;
160 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700161 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 int padlen;
163
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700164 if ((!buffer) || (!offset)) {
Joe Perchesa983f362011-10-10 01:08:07 -0700165 udf_debug("invalidparms, buffer=%p, offset=%p\n",
166 buffer, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 return NULL;
168 }
169
170 ptr = buffer;
171
Marcin Slusarz4b111112008-02-08 04:20:36 -0800172 if ((*offset > 0) && (*offset < bufsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 ptr += *offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700174 fi = (struct fileIdentDesc *)ptr;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800175 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700177 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179 *offset, (unsigned long)sizeof(struct fileIdentDesc),
180 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return NULL;
182 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800183 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 lengthThisIdent = sizeof(struct fileIdentDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700187 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 /* we need to figure padding, too! */
190 padlen = lengthThisIdent % UDF_NAME_PAD;
191 if (padlen)
192 lengthThisIdent += (UDF_NAME_PAD - padlen);
193 *offset = *offset + lengthThisIdent;
194
195 return fi;
196}
197
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200198struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700199 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200201 struct short_ad *sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700203 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700204 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 return NULL;
206 }
207
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200208 if ((*offset + sizeof(struct short_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800210 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200211 sa = (struct short_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800212 if (sa->extLength == 0)
213 return NULL;
214 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
216 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200217 *offset += sizeof(struct short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return sa;
219}
220
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200221struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200223 struct long_ad *la;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700225 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700226 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return NULL;
228 }
229
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200230 if ((*offset + sizeof(struct long_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800232 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200233 la = (struct long_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800234 if (la->extLength == 0)
235 return NULL;
236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200239 *offset += sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return la;
241}