hc
2024-10-12 a5969cabbb4660eab42b6ef0412cbbd1200cf14d
kernel/fs/adfs/dir_fplus.c
....@@ -1,132 +1,166 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * linux/fs/adfs/dir_fplus.c
34 *
45 * Copyright (C) 1997-1999 Russell King
5
- *
6
- * This program is free software; you can redistribute it and/or modify
7
- * it under the terms of the GNU General Public License version 2 as
8
- * published by the Free Software Foundation.
96 */
10
-#include <linux/buffer_head.h>
11
-#include <linux/slab.h>
127 #include "adfs.h"
138 #include "dir_fplus.h"
149
15
-static int
16
-adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
10
+/* Return the byte offset to directory entry pos */
11
+static 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
+
19
+static int adfs_fplus_validate_header(const struct adfs_bigdirheader *h)
20
+{
21
+ unsigned int size = le32_to_cpu(h->bigdirsize);
22
+ unsigned int len;
23
+
24
+ if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
25
+ h->bigdirversion[2] != 0 ||
26
+ h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME) ||
27
+ !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)
54
+ return -EIO;
55
+
56
+ return 0;
57
+}
58
+
59
+static 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
+
70
+static 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
+
103
+static int adfs_fplus_read(struct super_block *sb, u32 indaddr,
104
+ unsigned int size, struct adfs_dir *dir)
17105 {
18106 struct adfs_bigdirheader *h;
19107 struct adfs_bigdirtail *t;
20
- unsigned long block;
21
- unsigned int blk, size;
22
- int i, ret = -EIO;
108
+ unsigned int dirsize;
109
+ int ret;
23110
24
- dir->nr_buffers = 0;
111
+ /* Read first buffer */
112
+ ret = adfs_dir_read_buffers(sb, indaddr, sb->s_blocksize, dir);
113
+ if (ret)
114
+ return ret;
25115
26
- /* start off using fixed bh set - only alloc for big dirs */
27
- dir->bh_fplus = &dir->bh[0];
28
-
29
- block = __adfs_block_map(sb, id, 0);
30
- if (!block) {
31
- adfs_error(sb, "dir object %X has a hole at offset 0", id);
116
+ dir->bighead = h = (void *)dir->bhs[0]->b_data;
117
+ ret = adfs_fplus_validate_header(h);
118
+ if (ret) {
119
+ adfs_error(sb, "dir %06x has malformed header", indaddr);
32120 goto out;
33121 }
34122
35
- dir->bh_fplus[0] = sb_bread(sb, block);
36
- if (!dir->bh_fplus[0])
37
- goto out;
38
- dir->nr_buffers += 1;
39
-
40
- h = (struct adfs_bigdirheader *)dir->bh_fplus[0]->b_data;
41
- size = le32_to_cpu(h->bigdirsize);
42
- if (size != sz) {
43
- printk(KERN_WARNING "adfs: adfs_fplus_read:"
44
- " directory header size %X\n"
45
- " does not match directory size %X\n",
46
- size, sz);
123
+ dirsize = le32_to_cpu(h->bigdirsize);
124
+ if (size && dirsize != size) {
125
+ adfs_msg(sb, KERN_WARNING,
126
+ "dir %06x header size %X does not match directory size %X",
127
+ indaddr, dirsize, size);
47128 }
48129
49
- if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
50
- h->bigdirversion[2] != 0 || size & 2047 ||
51
- h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) {
52
- printk(KERN_WARNING "adfs: dir object %X has"
53
- " malformed dir header\n", id);
130
+ /* Read remaining buffers */
131
+ ret = adfs_dir_read_buffers(sb, indaddr, dirsize, dir);
132
+ if (ret)
133
+ return ret;
134
+
135
+ dir->bigtail = t = (struct adfs_bigdirtail *)
136
+ (dir->bhs[dir->nr_buffers - 1]->b_data + (sb->s_blocksize - 8));
137
+
138
+ ret = adfs_fplus_validate_tail(h, t);
139
+ if (ret) {
140
+ adfs_error(sb, "dir %06x has malformed tail", indaddr);
54141 goto out;
55142 }
56143
57
- size >>= sb->s_blocksize_bits;
58
- if (size > ARRAY_SIZE(dir->bh)) {
59
- /* this directory is too big for fixed bh set, must allocate */
60
- struct buffer_head **bh_fplus =
61
- kcalloc(size, sizeof(struct buffer_head *),
62
- GFP_KERNEL);
63
- if (!bh_fplus) {
64
- ret = -ENOMEM;
65
- adfs_error(sb, "not enough memory for"
66
- " dir object %X (%d blocks)", id, size);
67
- goto out;
68
- }
69
- dir->bh_fplus = bh_fplus;
70
- /* copy over the pointer to the block that we've already read */
71
- dir->bh_fplus[0] = dir->bh[0];
72
- }
73
-
74
- for (blk = 1; blk < size; blk++) {
75
- block = __adfs_block_map(sb, id, blk);
76
- if (!block) {
77
- adfs_error(sb, "dir object %X has a hole at offset %d", id, blk);
78
- goto out;
79
- }
80
-
81
- dir->bh_fplus[blk] = sb_bread(sb, block);
82
- if (!dir->bh_fplus[blk]) {
83
- adfs_error(sb, "dir object %x failed read for offset %d, mapped block %lX",
84
- id, blk, block);
85
- goto out;
86
- }
87
-
88
- dir->nr_buffers += 1;
89
- }
90
-
91
- t = (struct adfs_bigdirtail *)
92
- (dir->bh_fplus[size - 1]->b_data + (sb->s_blocksize - 8));
93
-
94
- if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
95
- t->bigdirendmasseq != h->startmasseq ||
96
- t->reserved[0] != 0 || t->reserved[1] != 0) {
97
- printk(KERN_WARNING "adfs: dir object %X has "
98
- "malformed dir end\n", id);
144
+ if (adfs_fplus_checkbyte(dir) != t->bigdircheckbyte) {
145
+ adfs_error(sb, "dir %06x checkbyte mismatch\n", indaddr);
99146 goto out;
100147 }
101148
102149 dir->parent_id = le32_to_cpu(h->bigdirparent);
103
- dir->sb = sb;
104150 return 0;
105151
106152 out:
107
- if (dir->bh_fplus) {
108
- for (i = 0; i < dir->nr_buffers; i++)
109
- brelse(dir->bh_fplus[i]);
153
+ adfs_dir_relse(dir);
110154
111
- if (&dir->bh[0] != dir->bh_fplus)
112
- kfree(dir->bh_fplus);
113
-
114
- dir->bh_fplus = NULL;
115
- }
116
-
117
- dir->nr_buffers = 0;
118
- dir->sb = NULL;
119155 return ret;
120156 }
121157
122158 static int
123159 adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
124160 {
125
- struct adfs_bigdirheader *h =
126
- (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
127161 int ret = -ENOENT;
128162
129
- if (fpos <= le32_to_cpu(h->bigdirentries)) {
163
+ if (fpos <= le32_to_cpu(dir->bighead->bigdirentries)) {
130164 dir->pos = fpos;
131165 ret = 0;
132166 }
....@@ -134,132 +168,120 @@
134168 return ret;
135169 }
136170
137
-static void
138
-dir_memcpy(struct adfs_dir *dir, unsigned int offset, void *to, int len)
139
-{
140
- struct super_block *sb = dir->sb;
141
- unsigned int buffer, partial, remainder;
142
-
143
- buffer = offset >> sb->s_blocksize_bits;
144
- offset &= sb->s_blocksize - 1;
145
-
146
- partial = sb->s_blocksize - offset;
147
-
148
- if (partial >= len)
149
- memcpy(to, dir->bh_fplus[buffer]->b_data + offset, len);
150
- else {
151
- char *c = (char *)to;
152
-
153
- remainder = len - partial;
154
-
155
- memcpy(c,
156
- dir->bh_fplus[buffer]->b_data + offset,
157
- partial);
158
-
159
- memcpy(c + partial,
160
- dir->bh_fplus[buffer + 1]->b_data,
161
- remainder);
162
- }
163
-}
164
-
165171 static int
166172 adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
167173 {
168
- struct adfs_bigdirheader *h =
169
- (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
174
+ struct adfs_bigdirheader *h = dir->bighead;
170175 struct adfs_bigdirentry bde;
171176 unsigned int offset;
172
- int i, ret = -ENOENT;
177
+ int ret;
173178
174179 if (dir->pos >= le32_to_cpu(h->bigdirentries))
175
- goto out;
180
+ return -ENOENT;
176181
177
- offset = offsetof(struct adfs_bigdirheader, bigdirname);
178
- offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
179
- offset += dir->pos * sizeof(struct adfs_bigdirentry);
182
+ offset = adfs_fplus_offset(h, dir->pos);
180183
181
- dir_memcpy(dir, offset, &bde, sizeof(struct adfs_bigdirentry));
184
+ ret = adfs_dir_copyfrom(&bde, dir, offset,
185
+ sizeof(struct adfs_bigdirentry));
186
+ if (ret)
187
+ return ret;
182188
183189 obj->loadaddr = le32_to_cpu(bde.bigdirload);
184190 obj->execaddr = le32_to_cpu(bde.bigdirexec);
185191 obj->size = le32_to_cpu(bde.bigdirlen);
186
- obj->file_id = le32_to_cpu(bde.bigdirindaddr);
192
+ obj->indaddr = le32_to_cpu(bde.bigdirindaddr);
187193 obj->attr = le32_to_cpu(bde.bigdirattr);
188194 obj->name_len = le32_to_cpu(bde.bigdirobnamelen);
189195
190
- offset = offsetof(struct adfs_bigdirheader, bigdirname);
191
- offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
192
- offset += le32_to_cpu(h->bigdirentries) * sizeof(struct adfs_bigdirentry);
196
+ offset = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
193197 offset += le32_to_cpu(bde.bigdirobnameptr);
194198
195
- dir_memcpy(dir, offset, obj->name, obj->name_len);
196
- for (i = 0; i < obj->name_len; i++)
197
- if (obj->name[i] == '/')
198
- obj->name[i] = '.';
199
+ ret = adfs_dir_copyfrom(obj->name, dir, offset, obj->name_len);
200
+ if (ret)
201
+ return ret;
199202
200
- obj->filetype = -1;
201
-
202
- /*
203
- * object is a file and is filetyped and timestamped?
204
- * RISC OS 12-bit filetype is stored in load_address[19:8]
205
- */
206
- if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
207
- (0xfff00000 == (0xfff00000 & obj->loadaddr))) {
208
- obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);
209
-
210
- /* optionally append the ,xyz hex filetype suffix */
211
- if (ADFS_SB(dir->sb)->s_ftsuffix)
212
- obj->name_len +=
213
- append_filetype_suffix(
214
- &obj->name[obj->name_len],
215
- obj->filetype);
216
- }
203
+ adfs_object_fixup(dir, obj);
217204
218205 dir->pos += 1;
219
- ret = 0;
220
-out:
206
+
207
+ return 0;
208
+}
209
+
210
+static int adfs_fplus_iterate(struct adfs_dir *dir, struct dir_context *ctx)
211
+{
212
+ struct object_info obj;
213
+
214
+ if ((ctx->pos - 2) >> 32)
215
+ return 0;
216
+
217
+ if (adfs_fplus_setpos(dir, ctx->pos - 2))
218
+ return 0;
219
+
220
+ while (!adfs_fplus_getnext(dir, &obj)) {
221
+ if (!dir_emit(ctx, obj.name, obj.name_len,
222
+ obj.indaddr, DT_UNKNOWN))
223
+ break;
224
+ ctx->pos++;
225
+ }
226
+
227
+ return 0;
228
+}
229
+
230
+static int adfs_fplus_update(struct adfs_dir *dir, struct object_info *obj)
231
+{
232
+ struct adfs_bigdirheader *h = dir->bighead;
233
+ struct adfs_bigdirentry bde;
234
+ int offset, end, ret;
235
+
236
+ offset = adfs_fplus_offset(h, 0) - sizeof(bde);
237
+ end = adfs_fplus_offset(h, le32_to_cpu(h->bigdirentries));
238
+
239
+ do {
240
+ offset += sizeof(bde);
241
+ if (offset >= end) {
242
+ adfs_error(dir->sb, "unable to locate entry to update");
243
+ return -ENOENT;
244
+ }
245
+ ret = adfs_dir_copyfrom(&bde, dir, offset, sizeof(bde));
246
+ if (ret) {
247
+ adfs_error(dir->sb, "error reading directory entry");
248
+ return -ENOENT;
249
+ }
250
+ } while (le32_to_cpu(bde.bigdirindaddr) != obj->indaddr);
251
+
252
+ bde.bigdirload = cpu_to_le32(obj->loadaddr);
253
+ bde.bigdirexec = cpu_to_le32(obj->execaddr);
254
+ bde.bigdirlen = cpu_to_le32(obj->size);
255
+ bde.bigdirindaddr = cpu_to_le32(obj->indaddr);
256
+ bde.bigdirattr = cpu_to_le32(obj->attr);
257
+
258
+ return adfs_dir_copyto(dir, offset, &bde, sizeof(bde));
259
+}
260
+
261
+static int adfs_fplus_commit(struct adfs_dir *dir)
262
+{
263
+ int ret;
264
+
265
+ /* Increment directory sequence number */
266
+ dir->bighead->startmasseq += 1;
267
+ dir->bigtail->bigdirendmasseq += 1;
268
+
269
+ /* Update directory check byte */
270
+ dir->bigtail->bigdircheckbyte = adfs_fplus_checkbyte(dir);
271
+
272
+ /* Make sure the directory still validates correctly */
273
+ ret = adfs_fplus_validate_header(dir->bighead);
274
+ if (ret == 0)
275
+ ret = adfs_fplus_validate_tail(dir->bighead, dir->bigtail);
276
+
221277 return ret;
222
-}
223
-
224
-static int
225
-adfs_fplus_sync(struct adfs_dir *dir)
226
-{
227
- int err = 0;
228
- int i;
229
-
230
- for (i = dir->nr_buffers - 1; i >= 0; i--) {
231
- struct buffer_head *bh = dir->bh_fplus[i];
232
- sync_dirty_buffer(bh);
233
- if (buffer_req(bh) && !buffer_uptodate(bh))
234
- err = -EIO;
235
- }
236
-
237
- return err;
238
-}
239
-
240
-static void
241
-adfs_fplus_free(struct adfs_dir *dir)
242
-{
243
- int i;
244
-
245
- if (dir->bh_fplus) {
246
- for (i = 0; i < dir->nr_buffers; i++)
247
- brelse(dir->bh_fplus[i]);
248
-
249
- if (&dir->bh[0] != dir->bh_fplus)
250
- kfree(dir->bh_fplus);
251
-
252
- dir->bh_fplus = NULL;
253
- }
254
-
255
- dir->nr_buffers = 0;
256
- dir->sb = NULL;
257278 }
258279
259280 const struct adfs_dir_ops adfs_fplus_dir_ops = {
260281 .read = adfs_fplus_read,
282
+ .iterate = adfs_fplus_iterate,
261283 .setpos = adfs_fplus_setpos,
262284 .getnext = adfs_fplus_getnext,
263
- .sync = adfs_fplus_sync,
264
- .free = adfs_fplus_free
285
+ .update = adfs_fplus_update,
286
+ .commit = adfs_fplus_commit,
265287 };