blob: 48ea299b6ece60984e21fc680afb5e445a4e25d8 [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/adfs/dir_fplus.c
4 *
5 * Copyright (C) 1997-1999 Russell King
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "adfs.h"
8#include "dir_fplus.h"
9
Russell King0db35a02019-12-09 11:10:52 +000010/* Return the byte offset to directory entry pos */
11static unsigned int adfs_fplus_offset(const struct adfs_bigdirheader *h,
12 unsigned int pos)
13{
14 return offsetof(struct adfs_bigdirheader, bigdirname) +
15 ALIGN(le32_to_cpu(h->bigdirnamelen), 4) +
16 pos * sizeof(struct adfs_bigdirentry);
17}
18
Russell King6674eca2019-12-09 11:10:57 +000019static int adfs_fplus_validate_header(const struct adfs_bigdirheader *h)
20{
21 unsigned int size = le32_to_cpu(h->bigdirsize);
Russell Kingaa3d4e02019-12-09 11:11:02 +000022 unsigned int len;
Russell King6674eca2019-12-09 11:10:57 +000023
24 if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
25 h->bigdirversion[2] != 0 ||
26 h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME) ||
Russell Kingaa3d4e02019-12-09 11:11:02 +000027 !size || size & 2047 || size > SZ_4M)
28 return -EIO;
29
30 size -= sizeof(struct adfs_bigdirtail) +
31 offsetof(struct adfs_bigdirheader, bigdirname);
32
33 /* Check that bigdirnamelen fits within the directory */
34 len = ALIGN(le32_to_cpu(h->bigdirnamelen), 4);
35 if (len > size)
36 return -EIO;
37
38 size -= len;
39
40 /* Check that bigdirnamesize fits within the directory */
41 len = le32_to_cpu(h->bigdirnamesize);
42 if (len > size)
43 return -EIO;
44
45 size -= len;
46
47 /*
48 * Avoid division, we know that absolute maximum number of entries
49 * can not be so large to cause overflow of the multiplication below.
50 */
51 len = le32_to_cpu(h->bigdirentries);
52 if (len > SZ_4M / sizeof(struct adfs_bigdirentry) ||
53 len * sizeof(struct adfs_bigdirentry) > size)
Russell King6674eca2019-12-09 11:10:57 +000054 return -EIO;
55
56 return 0;
57}
58
59static int adfs_fplus_validate_tail(const struct adfs_bigdirheader *h,
60 const struct adfs_bigdirtail *t)
61{
62 if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
63 t->bigdirendmasseq != h->startmasseq ||
64 t->reserved[0] != 0 || t->reserved[1] != 0)
65 return -EIO;
66
67 return 0;
68}
69
Russell Kingd79288b2019-12-09 11:11:08 +000070static u8 adfs_fplus_checkbyte(struct adfs_dir *dir)
71{
72 struct adfs_bigdirheader *h = dir->bighead;
73 struct adfs_bigdirtail *t = dir->bigtail;
74 unsigned int end, bs, bi, i;
75 __le32 *bp;
76 u32 dircheck;
77
78 end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries)) +
79 le32_to_cpu(h->bigdirnamesize);
80
81 /* Accumulate the contents of the header, entries and names */
82 for (dircheck = 0, bi = 0; end; bi++) {
83 bp = (void *)dir->bhs[bi]->b_data;
84 bs = dir->bhs[bi]->b_size;
85 if (bs > end)
86 bs = end;
87
88 for (i = 0; i < bs; i += sizeof(u32))
89 dircheck = ror32(dircheck, 13) ^ le32_to_cpup(bp++);
90
91 end -= bs;
92 }
93
94 /* Accumulate the contents of the tail except for the check byte */
95 dircheck = ror32(dircheck, 13) ^ le32_to_cpu(t->bigdirendname);
96 dircheck = ror32(dircheck, 13) ^ t->bigdirendmasseq;
97 dircheck = ror32(dircheck, 13) ^ t->reserved[0];
98 dircheck = ror32(dircheck, 13) ^ t->reserved[1];
99
100 return dircheck ^ dircheck >> 8 ^ dircheck >> 16 ^ dircheck >> 24;
101}
102
Russell King419a6e52019-12-09 11:09:35 +0000103static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
104 unsigned int size, struct adfs_dir *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct adfs_bigdirheader *h;
107 struct adfs_bigdirtail *t;
Russell King419a6e52019-12-09 11:09:35 +0000108 unsigned int dirsize;
109 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Russell King419a6e52019-12-09 11:09:35 +0000111 /* Read first buffer */
112 ret = adfs_dir_read_buffers(sb, indaddr, sb->s_blocksize, dir);
113 if (ret)
114 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Russell King016936b2019-12-09 11:10:21 +0000116 dir->bighead = h = (void *)dir->bhs[0]->b_data;
Russell King6674eca2019-12-09 11:10:57 +0000117 if (adfs_fplus_validate_header(h)) {
118 adfs_error(sb, "dir %06x has malformed header", indaddr);
119 goto out;
120 }
121
Russell King419a6e52019-12-09 11:09:35 +0000122 dirsize = le32_to_cpu(h->bigdirsize);
Russell Kinga4641522019-12-09 11:11:13 +0000123 if (size && dirsize != size) {
Russell Kingceb3b102019-06-04 14:49:52 +0100124 adfs_msg(sb, KERN_WARNING,
Russell King419a6e52019-12-09 11:09:35 +0000125 "dir %06x header size %X does not match directory size %X",
126 indaddr, dirsize, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 }
128
Russell King419a6e52019-12-09 11:09:35 +0000129 /* Read remaining buffers */
130 ret = adfs_dir_read_buffers(sb, indaddr, dirsize, dir);
131 if (ret)
132 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Russell King016936b2019-12-09 11:10:21 +0000134 dir->bigtail = t = (struct adfs_bigdirtail *)
Russell King419a6e52019-12-09 11:09:35 +0000135 (dir->bhs[dir->nr_buffers - 1]->b_data + (sb->s_blocksize - 8));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Russell King6674eca2019-12-09 11:10:57 +0000137 ret = adfs_fplus_validate_tail(h, t);
138 if (ret) {
Russell King419a6e52019-12-09 11:09:35 +0000139 adfs_error(sb, "dir %06x has malformed tail", indaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 goto out;
Stuart Swales2f097192011-03-22 16:35:04 -0700141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Russell Kingd79288b2019-12-09 11:11:08 +0000143 if (adfs_fplus_checkbyte(dir) != t->bigdircheckbyte) {
144 adfs_error(sb, "dir %06x checkbyte mismatch\n", indaddr);
145 goto out;
146 }
147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 dir->parent_id = le32_to_cpu(h->bigdirparent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 return 0;
Stuart Swales2f097192011-03-22 16:35:04 -0700150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151out:
Russell King1dd9f5b2019-12-09 11:09:20 +0000152 adfs_dir_relse(dir);
Stuart Swales2f097192011-03-22 16:35:04 -0700153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return ret;
155}
156
157static int
158adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
159{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 int ret = -ENOENT;
161
Russell King016936b2019-12-09 11:10:21 +0000162 if (fpos <= le32_to_cpu(dir->bighead->bigdirentries)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 dir->pos = fpos;
164 ret = 0;
165 }
166
167 return ret;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170static int
171adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
172{
Russell King016936b2019-12-09 11:10:21 +0000173 struct adfs_bigdirheader *h = dir->bighead;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 struct adfs_bigdirentry bde;
175 unsigned int offset;
Russell Kinga3171202019-12-09 11:09:30 +0000176 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (dir->pos >= le32_to_cpu(h->bigdirentries))
Russell Kinga3171202019-12-09 11:09:30 +0000179 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Russell King0db35a02019-12-09 11:10:52 +0000181 offset = adfs_fplus_offset(h, dir->pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
Russell Kinga3171202019-12-09 11:09:30 +0000183 ret = adfs_dir_copyfrom(&bde, dir, offset,
184 sizeof(struct adfs_bigdirentry));
185 if (ret)
186 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 obj->loadaddr = le32_to_cpu(bde.bigdirload);
189 obj->execaddr = le32_to_cpu(bde.bigdirexec);
190 obj->size = le32_to_cpu(bde.bigdirlen);
Russell King5ed70bb2019-06-04 14:49:57 +0100191 obj->indaddr = le32_to_cpu(bde.bigdirindaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 obj->attr = le32_to_cpu(bde.bigdirattr);
193 obj->name_len = le32_to_cpu(bde.bigdirobnamelen);
194
Russell King0db35a02019-12-09 11:10:52 +0000195 offset = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 offset += le32_to_cpu(bde.bigdirobnameptr);
197
Russell Kinga3171202019-12-09 11:09:30 +0000198 ret = adfs_dir_copyfrom(obj->name, dir, offset, obj->name_len);
199 if (ret)
200 return ret;
201
Russell King411c49b2019-03-24 12:57:32 +0000202 adfs_object_fixup(dir, obj);
Stuart Swalesda23ef02011-03-22 16:35:06 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 dir->pos += 1;
Russell Kinga3171202019-12-09 11:09:30 +0000205
206 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Russell King4287e4d2019-12-09 11:10:16 +0000209static int adfs_fplus_iterate(struct adfs_dir *dir, struct dir_context *ctx)
210{
211 struct object_info obj;
212
213 if ((ctx->pos - 2) >> 32)
214 return 0;
215
216 if (adfs_fplus_setpos(dir, ctx->pos - 2))
217 return 0;
218
219 while (!adfs_fplus_getnext(dir, &obj)) {
220 if (!dir_emit(ctx, obj.name, obj.name_len,
221 obj.indaddr, DT_UNKNOWN))
222 break;
223 ctx->pos++;
224 }
225
226 return 0;
227}
228
Russell Kinga4641522019-12-09 11:11:13 +0000229static int adfs_fplus_update(struct adfs_dir *dir, struct object_info *obj)
230{
231 struct adfs_bigdirheader *h = dir->bighead;
232 struct adfs_bigdirentry bde;
233 int offset, end, ret;
234
235 offset = adfs_fplus_offset(h, 0) - sizeof(bde);
236 end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
237
238 do {
239 offset += sizeof(bde);
240 if (offset >= end) {
241 adfs_error(dir->sb, "unable to locate entry to update");
242 return -ENOENT;
243 }
244 ret = adfs_dir_copyfrom(&bde, dir, offset, sizeof(bde));
245 if (ret) {
246 adfs_error(dir->sb, "error reading directory entry");
247 return -ENOENT;
248 }
249 } while (le32_to_cpu(bde.bigdirindaddr) != obj->indaddr);
250
251 bde.bigdirload = cpu_to_le32(obj->loadaddr);
252 bde.bigdirexec = cpu_to_le32(obj->execaddr);
253 bde.bigdirlen = cpu_to_le32(obj->size);
254 bde.bigdirindaddr = cpu_to_le32(obj->indaddr);
255 bde.bigdirattr = cpu_to_le32(obj->attr);
256
257 return adfs_dir_copyto(dir, offset, &bde, sizeof(bde));
258}
259
260static int adfs_fplus_commit(struct adfs_dir *dir)
261{
262 int ret;
263
264 /* Increment directory sequence number */
265 dir->bighead->startmasseq += 1;
266 dir->bigtail->bigdirendmasseq += 1;
267
268 /* Update directory check byte */
269 dir->bigtail->bigdircheckbyte = adfs_fplus_checkbyte(dir);
270
271 /* Make sure the directory still validates correctly */
272 ret = adfs_fplus_validate_header(dir->bighead);
273 if (ret == 0)
274 ret = adfs_fplus_validate_tail(dir->bighead, dir->bigtail);
275
276 return ret;
277}
278
Julia Lawall0125f502015-11-21 16:15:37 +0100279const struct adfs_dir_ops adfs_fplus_dir_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 .read = adfs_fplus_read,
Russell King4287e4d2019-12-09 11:10:16 +0000281 .iterate = adfs_fplus_iterate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 .setpos = adfs_fplus_setpos,
283 .getnext = adfs_fplus_getnext,
Russell Kinga4641522019-12-09 11:11:13 +0000284 .update = adfs_fplus_update,
285 .commit = adfs_fplus_commit,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};