blob: 48ef184929ecf8a70b76658bb7a097ad43fa181f [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 }
Jan Kara2a1b1232018-06-13 12:09:22 +0200153 /* Got last entry outside of dir size - fs is corrupted! */
154 if (*nf_pos > dir->i_size)
155 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 return fi;
157}
158
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700159struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct fileIdentDesc *fi;
162 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700163 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 int padlen;
165
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700166 if ((!buffer) || (!offset)) {
Joe Perchesa983f362011-10-10 01:08:07 -0700167 udf_debug("invalidparms, buffer=%p, offset=%p\n",
168 buffer, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 return NULL;
170 }
171
172 ptr = buffer;
173
Marcin Slusarz4b111112008-02-08 04:20:36 -0800174 if ((*offset > 0) && (*offset < bufsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 ptr += *offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700176 fi = (struct fileIdentDesc *)ptr;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800177 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700179 le16_to_cpu(fi->descTag.tagIdent));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 udf_debug("offset: %u sizeof: %lu bufsize: %u\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700181 *offset, (unsigned long)sizeof(struct fileIdentDesc),
182 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 return NULL;
184 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800185 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 lengthThisIdent = sizeof(struct fileIdentDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800187 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700189 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191 /* we need to figure padding, too! */
192 padlen = lengthThisIdent % UDF_NAME_PAD;
193 if (padlen)
194 lengthThisIdent += (UDF_NAME_PAD - padlen);
195 *offset = *offset + lengthThisIdent;
196
197 return fi;
198}
199
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200200struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700201 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200203 struct short_ad *sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700205 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700206 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 return NULL;
208 }
209
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200210 if ((*offset + sizeof(struct short_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800212 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200213 sa = (struct short_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800214 if (sa->extLength == 0)
215 return NULL;
216 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
218 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200219 *offset += sizeof(struct short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 return sa;
221}
222
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200223struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200225 struct long_ad *la;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700227 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700228 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 return NULL;
230 }
231
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200232 if ((*offset + sizeof(struct long_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800234 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200235 la = (struct long_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800236 if (la->extLength == 0)
237 return NULL;
238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200241 *offset += sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 return la;
243}