blob: 988d5352bdb866e6108a03d3798492c206b034f9 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
Marcin Slusarz4b111112008-02-08 04:20:36 -080020struct fileIdentDesc *udf_fileident_read(struct inode *dir, loff_t *nf_pos,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070021 struct udf_fileident_bh *fibh,
22 struct fileIdentDesc *cfi,
23 struct extent_position *epos,
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +020024 struct kernel_lb_addr *eloc, uint32_t *elen,
Marcin Slusarz4b111112008-02-08 04:20:36 -080025 sector_t *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
27 struct fileIdentDesc *fi;
28 int i, num, block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070029 struct buffer_head *tmp, *bha[16];
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080030 struct udf_inode_info *iinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32 fibh->soffset = fibh->eoffset;
33
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080034 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
35 fi = udf_get_fileident(iinfo->i_ext.i_data -
36 (iinfo->i_efe ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070037 sizeof(struct extendedFileEntry) :
38 sizeof(struct fileEntry)),
Marcin Slusarz4b111112008-02-08 04:20:36 -080039 dir->i_sb->s_blocksize,
40 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 if (!fi)
42 return NULL;
43
Jan Karaaf793292008-02-08 04:20:50 -080044 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070046 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070047 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49 return fi;
50 }
51
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070052 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -070053 int lextoffset = epos->offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -080054 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jan Karaff116fc2007-05-08 00:35:14 -070056 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070057 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Pekka Enberg97e961f2008-10-15 12:29:03 +020060 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070062 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Marcin Slusarz4b111112008-02-08 04:20:36 -080064 if ((*offset << blocksize_bits) >= *elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *offset = 0;
66 else
Jan Karaff116fc2007-05-08 00:35:14 -070067 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jan Kara3bf25cb2007-05-08 00:35:16 -070069 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -080070 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
71 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 return NULL;
73 fibh->soffset = fibh->eoffset = 0;
74
Marcin Slusarz4b111112008-02-08 04:20:36 -080075 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
76 i = 16 >> (blocksize_bits - 9);
77 if (i + *offset > (*elen >> blocksize_bits))
78 i = (*elen >> blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070079 for (num = 0; i > 0; i--) {
Pekka Enberg97e961f2008-10-15 12:29:03 +020080 block = udf_get_lb_pblock(dir->i_sb, eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -080081 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 tmp = udf_tgetblk(dir->i_sb, block);
Marcin Slusarz4b111112008-02-08 04:20:36 -080083 if (tmp && !buffer_uptodate(tmp) &&
84 !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 bha[num++] = tmp;
86 else
87 brelse(tmp);
88 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070089 if (num) {
Christoph Hellwig70246282016-07-19 11:28:41 +020090 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 brelse(bha[i]);
93 }
94 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070095 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070096 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 fibh->sbh = fibh->ebh;
98 }
99
100 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700101 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 if (!fi)
104 return NULL;
105
Jan Karaaf793292008-02-08 04:20:50 -0800106 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700108 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700109 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 sizeof(struct fileIdentDesc));
111 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Jan Karaff116fc2007-05-08 00:35:14 -0700112 int lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Jan Karaff116fc2007-05-08 00:35:14 -0700114 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700115 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Pekka Enberg97e961f2008-10-15 12:29:03 +0200118 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700120 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
123 *offset = 0;
124 else
Jan Karaff116fc2007-05-08 00:35:14 -0700125 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
127 fibh->soffset -= dir->i_sb->s_blocksize;
128 fibh->eoffset -= dir->i_sb->s_blocksize;
129
Marcin Slusarz4b111112008-02-08 04:20:36 -0800130 fibh->ebh = udf_tread(dir->i_sb, block);
131 if (!fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 return NULL;
133
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700134 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 int fi_len;
136
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700137 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800138 memcpy((uint8_t *)cfi - fibh->soffset,
139 fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700140 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141
Marcin Slusarz4b111112008-02-08 04:20:36 -0800142 fi_len = (sizeof(struct fileIdentDesc) +
143 cfi->lengthFileIdent +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700144 le16_to_cpu(cfi->lengthOfImpUse) + 3) & ~3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Jan Karaaf793292008-02-08 04:20:50 -0800146 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700148 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700149 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700150 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152 }
153 return fi;
154}
155
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700156struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 struct fileIdentDesc *fi;
159 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700160 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 int padlen;
162
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 if ((!buffer) || (!offset)) {
Joe Perchesa983f362011-10-10 01:08:07 -0700164 udf_debug("invalidparms, buffer=%p, offset=%p\n",
165 buffer, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 return NULL;
167 }
168
169 ptr = buffer;
170
Marcin Slusarz4b111112008-02-08 04:20:36 -0800171 if ((*offset > 0) && (*offset < bufsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 ptr += *offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700173 fi = (struct fileIdentDesc *)ptr;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800174 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700176 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700178 *offset, (unsigned long)sizeof(struct fileIdentDesc),
179 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 return NULL;
181 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800182 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 lengthThisIdent = sizeof(struct fileIdentDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800184 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700186 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 /* we need to figure padding, too! */
189 padlen = lengthThisIdent % UDF_NAME_PAD;
190 if (padlen)
191 lengthThisIdent += (UDF_NAME_PAD - padlen);
192 *offset = *offset + lengthThisIdent;
193
194 return fi;
195}
196
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200197struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700198 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200200 struct short_ad *sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700202 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700203 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return NULL;
205 }
206
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200207 if ((*offset + sizeof(struct short_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800209 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200210 sa = (struct short_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800211 if (sa->extLength == 0)
212 return NULL;
213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200216 *offset += sizeof(struct short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 return sa;
218}
219
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200220struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200222 struct long_ad *la;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700224 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700225 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 return NULL;
227 }
228
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200229 if ((*offset + sizeof(struct long_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800231 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200232 la = (struct long_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800233 if (la->extLength == 0)
234 return NULL;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200238 *offset += sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return la;
240}