blob: d9523013096f978c9d4a3ca1d8fdd23b55eeb275 [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>
Christoph Hellwig2f8b5442016-11-01 07:40:13 -060019#include <linux/bio.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
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;
Steve Magnanib490bdd2017-10-12 08:48:40 -050029 int i, num;
30 udf_pblk_t block;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070031 struct buffer_head *tmp, *bha[16];
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080032 struct udf_inode_info *iinfo = UDF_I(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
34 fibh->soffset = fibh->eoffset;
35
Marcin Slusarz48d6d8f2008-02-08 04:20:44 -080036 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
37 fi = udf_get_fileident(iinfo->i_ext.i_data -
38 (iinfo->i_efe ?
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070039 sizeof(struct extendedFileEntry) :
40 sizeof(struct fileEntry)),
Marcin Slusarz4b111112008-02-08 04:20:36 -080041 dir->i_sb->s_blocksize,
42 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (!fi)
44 return NULL;
45
Jan Karaaf793292008-02-08 04:20:50 -080046 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Cyrill Gorcunov28de7942007-07-21 04:37:18 -070048 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070049 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51 return fi;
52 }
53
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070054 if (fibh->eoffset == dir->i_sb->s_blocksize) {
Steve Magnani89a4d972017-10-12 08:48:42 -050055 uint32_t lextoffset = epos->offset;
Marcin Slusarz4b111112008-02-08 04:20:36 -080056 unsigned char blocksize_bits = dir->i_sb->s_blocksize_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Jan Karaff116fc2007-05-08 00:35:14 -070058 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070059 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Pekka Enberg97e961f2008-10-15 12:29:03 +020062 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070064 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Marcin Slusarz4b111112008-02-08 04:20:36 -080066 if ((*offset << blocksize_bits) >= *elen)
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 *offset = 0;
68 else
Jan Karaff116fc2007-05-08 00:35:14 -070069 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Jan Kara3bf25cb2007-05-08 00:35:16 -070071 brelse(fibh->sbh);
Marcin Slusarz4b111112008-02-08 04:20:36 -080072 fibh->sbh = fibh->ebh = udf_tread(dir->i_sb, block);
73 if (!fibh->sbh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 return NULL;
75 fibh->soffset = fibh->eoffset = 0;
76
Marcin Slusarz4b111112008-02-08 04:20:36 -080077 if (!(*offset & ((16 >> (blocksize_bits - 9)) - 1))) {
78 i = 16 >> (blocksize_bits - 9);
79 if (i + *offset > (*elen >> blocksize_bits))
80 i = (*elen >> blocksize_bits)-*offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070081 for (num = 0; i > 0; i--) {
Pekka Enberg97e961f2008-10-15 12:29:03 +020082 block = udf_get_lb_pblock(dir->i_sb, eloc,
Marcin Slusarz4b111112008-02-08 04:20:36 -080083 *offset + i);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 tmp = udf_tgetblk(dir->i_sb, block);
Marcin Slusarz4b111112008-02-08 04:20:36 -080085 if (tmp && !buffer_uptodate(tmp) &&
86 !buffer_locked(tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 bha[num++] = tmp;
88 else
89 brelse(tmp);
90 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070091 if (num) {
Christoph Hellwig70246282016-07-19 11:28:41 +020092 ll_rw_block(REQ_OP_READ, REQ_RAHEAD, num, bha);
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070093 for (i = 0; i < num; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 brelse(bha[i]);
95 }
96 }
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -070097 } else if (fibh->sbh != fibh->ebh) {
Jan Kara3bf25cb2007-05-08 00:35:16 -070098 brelse(fibh->sbh);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 fibh->sbh = fibh->ebh;
100 }
101
102 fi = udf_get_fileident(fibh->sbh->b_data, dir->i_sb->s_blocksize,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700103 &(fibh->eoffset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105 if (!fi)
106 return NULL;
107
Jan Karaaf793292008-02-08 04:20:50 -0800108 *nf_pos += fibh->eoffset - fibh->soffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700110 if (fibh->eoffset <= dir->i_sb->s_blocksize) {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700111 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700112 sizeof(struct fileIdentDesc));
113 } else if (fibh->eoffset > dir->i_sb->s_blocksize) {
Steve Magnani89a4d972017-10-12 08:48:42 -0500114 uint32_t lextoffset = epos->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Jan Karaff116fc2007-05-08 00:35:14 -0700116 if (udf_next_aext(dir, epos, eloc, elen, 1) !=
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700117 (EXT_RECORDED_ALLOCATED >> 30))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Pekka Enberg97e961f2008-10-15 12:29:03 +0200120 block = udf_get_lb_pblock(dir->i_sb, eloc, *offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700122 (*offset)++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 if ((*offset << dir->i_sb->s_blocksize_bits) >= *elen)
125 *offset = 0;
126 else
Jan Karaff116fc2007-05-08 00:35:14 -0700127 epos->offset = lextoffset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 fibh->soffset -= dir->i_sb->s_blocksize;
130 fibh->eoffset -= dir->i_sb->s_blocksize;
131
Marcin Slusarz4b111112008-02-08 04:20:36 -0800132 fibh->ebh = udf_tread(dir->i_sb, block);
133 if (!fibh->ebh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 return NULL;
135
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700136 if (sizeof(struct fileIdentDesc) > -fibh->soffset) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 int fi_len;
138
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700139 memcpy((uint8_t *)cfi, (uint8_t *)fi, -fibh->soffset);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800140 memcpy((uint8_t *)cfi - fibh->soffset,
141 fibh->ebh->b_data,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700142 sizeof(struct fileIdentDesc) + fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Jan Karaf2e83342018-06-13 17:30:14 +0200144 fi_len = udf_dir_entry_len(cfi);
Jan Karaaf793292008-02-08 04:20:50 -0800145 *nf_pos += fi_len - (fibh->eoffset - fibh->soffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 fibh->eoffset = fibh->soffset + fi_len;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700147 } else {
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700148 memcpy((uint8_t *)cfi, (uint8_t *)fi,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700149 sizeof(struct fileIdentDesc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 }
151 }
Jan Karafa656532018-06-13 12:09:22 +0200152 /* Got last entry outside of dir size - fs is corrupted! */
153 if (*nf_pos > dir->i_size)
154 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return fi;
156}
157
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700158struct fileIdentDesc *udf_get_fileident(void *buffer, int bufsize, int *offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
160 struct fileIdentDesc *fi;
161 int lengthThisIdent;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700162 uint8_t *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int padlen;
164
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700165 if ((!buffer) || (!offset)) {
Joe Perchesa983f362011-10-10 01:08:07 -0700166 udf_debug("invalidparms, buffer=%p, offset=%p\n",
167 buffer, offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 return NULL;
169 }
170
171 ptr = buffer;
172
Marcin Slusarz4b111112008-02-08 04:20:36 -0800173 if ((*offset > 0) && (*offset < bufsize))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 ptr += *offset;
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700175 fi = (struct fileIdentDesc *)ptr;
Marcin Slusarz5e0f0012008-02-08 04:20:41 -0800176 if (fi->descTag.tagIdent != cpu_to_le16(TAG_IDENT_FID)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 udf_debug("0x%x != TAG_IDENT_FID\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700178 le16_to_cpu(fi->descTag.tagIdent));
Steve Magnanifcbf7632017-10-12 08:48:41 -0500179 udf_debug("offset: %d sizeof: %lu bufsize: %d\n",
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700180 *offset, (unsigned long)sizeof(struct fileIdentDesc),
181 bufsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return NULL;
183 }
Marcin Slusarz4b111112008-02-08 04:20:36 -0800184 if ((*offset + sizeof(struct fileIdentDesc)) > bufsize)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 lengthThisIdent = sizeof(struct fileIdentDesc);
Marcin Slusarz4b111112008-02-08 04:20:36 -0800186 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 lengthThisIdent = sizeof(struct fileIdentDesc) +
Cyrill Gorcunov28de7942007-07-21 04:37:18 -0700188 fi->lengthFileIdent + le16_to_cpu(fi->lengthOfImpUse);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190 /* we need to figure padding, too! */
191 padlen = lengthThisIdent % UDF_NAME_PAD;
192 if (padlen)
193 lengthThisIdent += (UDF_NAME_PAD - padlen);
194 *offset = *offset + lengthThisIdent;
195
196 return fi;
197}
198
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200199struct short_ad *udf_get_fileshortad(uint8_t *ptr, int maxoffset, uint32_t *offset,
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700200 int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200202 struct short_ad *sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700204 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700205 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 return NULL;
207 }
208
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200209 if ((*offset + sizeof(struct short_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800211 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200212 sa = (struct short_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800213 if (sa->extLength == 0)
214 return NULL;
215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200218 *offset += sizeof(struct short_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 return sa;
220}
221
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200222struct long_ad *udf_get_filelongad(uint8_t *ptr, int maxoffset, uint32_t *offset, int inc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200224 struct long_ad *la;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Cyrill Gorcunovcb00ea32007-07-19 01:47:43 -0700226 if ((!ptr) || (!offset)) {
Joe Perches78ace702011-10-10 01:08:05 -0700227 pr_err("%s: invalidparms\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return NULL;
229 }
230
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200231 if ((*offset + sizeof(struct long_ad)) > maxoffset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 return NULL;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800233 else {
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200234 la = (struct long_ad *)ptr;
Marcin Slusarz4b111112008-02-08 04:20:36 -0800235 if (la->extLength == 0)
236 return NULL;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (inc)
Pekka Enberg5ca4e4b2008-10-15 12:28:03 +0200240 *offset += sizeof(struct long_ad);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return la;
242}