forked from torvalds/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dir_f.c
324 lines (268 loc) · 7.16 KB
/
dir_f.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// SPDX-License-Identifier: GPL-2.0-only
/*
* linux/fs/adfs/dir_f.c
*
* Copyright (C) 1997-1999 Russell King
*
* E and F format directory handling
*/
#include "adfs.h"
#include "dir_f.h"
/*
* Read an (unaligned) value of length 1..4 bytes
*/
static inline unsigned int adfs_readval(unsigned char *p, int len)
{
unsigned int val = 0;
switch (len) {
case 4: val |= p[3] << 24;
fallthrough;
case 3: val |= p[2] << 16;
fallthrough;
case 2: val |= p[1] << 8;
fallthrough;
default: val |= p[0];
}
return val;
}
static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
{
switch (len) {
case 4: p[3] = val >> 24;
fallthrough;
case 3: p[2] = val >> 16;
fallthrough;
case 2: p[1] = val >> 8;
fallthrough;
default: p[0] = val;
}
}
#define ror13(v) ((v >> 13) | (v << 19))
#define dir_u8(idx) \
({ int _buf = idx >> blocksize_bits; \
int _off = idx - (_buf << blocksize_bits);\
*(u8 *)(bh[_buf]->b_data + _off); \
})
#define dir_u32(idx) \
({ int _buf = idx >> blocksize_bits; \
int _off = idx - (_buf << blocksize_bits);\
*(__le32 *)(bh[_buf]->b_data + _off); \
})
#define bufoff(_bh,_idx) \
({ int _buf = _idx >> blocksize_bits; \
int _off = _idx - (_buf << blocksize_bits);\
(void *)(_bh[_buf]->b_data + _off); \
})
/*
* There are some algorithms that are nice in
* assembler, but a bitch in C... This is one
* of them.
*/
static u8
adfs_dir_checkbyte(const struct adfs_dir *dir)
{
struct buffer_head * const *bh = dir->bh;
const int blocksize_bits = dir->sb->s_blocksize_bits;
union { __le32 *ptr32; u8 *ptr8; } ptr, end;
u32 dircheck = 0;
int last = 5 - 26;
int i = 0;
/*
* Accumulate each word up to the last whole
* word of the last directory entry. This
* can spread across several buffer heads.
*/
do {
last += 26;
do {
dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
i += sizeof(u32);
} while (i < (last & ~3));
} while (dir_u8(last) != 0);
/*
* Accumulate the last few bytes. These
* bytes will be within the same bh.
*/
if (i != last) {
ptr.ptr8 = bufoff(bh, i);
end.ptr8 = ptr.ptr8 + last - i;
do {
dircheck = *ptr.ptr8++ ^ ror13(dircheck);
} while (ptr.ptr8 < end.ptr8);
}
/*
* The directory tail is in the final bh
* Note that contary to the RISC OS PRMs,
* the first few bytes are NOT included
* in the check. All bytes are in the
* same bh.
*/
ptr.ptr8 = bufoff(bh, 2008);
end.ptr8 = ptr.ptr8 + 36;
do {
__le32 v = *ptr.ptr32++;
dircheck = le32_to_cpu(v) ^ ror13(dircheck);
} while (ptr.ptr32 < end.ptr32);
return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
}
static int adfs_f_validate(struct adfs_dir *dir)
{
struct adfs_dirheader *head = dir->dirhead;
struct adfs_newdirtail *tail = dir->newtail;
if (head->startmasseq != tail->endmasseq ||
tail->dirlastmask || tail->reserved[0] || tail->reserved[1] ||
(memcmp(&head->startname, "Nick", 4) &&
memcmp(&head->startname, "Hugo", 4)) ||
memcmp(&head->startname, &tail->endname, 4) ||
adfs_dir_checkbyte(dir) != tail->dircheckbyte)
return -EIO;
return 0;
}
/* Read and check that a directory is valid */
static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
struct adfs_dir *dir)
{
const unsigned int blocksize_bits = sb->s_blocksize_bits;
int ret;
if (size && size != ADFS_NEWDIR_SIZE)
return -EIO;
ret = adfs_dir_read_buffers(sb, indaddr, ADFS_NEWDIR_SIZE, dir);
if (ret)
return ret;
dir->dirhead = bufoff(dir->bh, 0);
dir->newtail = bufoff(dir->bh, 2007);
if (adfs_f_validate(dir))
goto bad_dir;
dir->parent_id = adfs_readval(dir->newtail->dirparent, 3);
return 0;
bad_dir:
adfs_error(sb, "dir %06x is corrupted", indaddr);
adfs_dir_relse(dir);
return -EIO;
}
/*
* convert a disk-based directory entry to a Linux ADFS directory entry
*/
static inline void
adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
struct adfs_direntry *de)
{
unsigned int name_len;
for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
if (de->dirobname[name_len] < ' ')
break;
obj->name[name_len] = de->dirobname[name_len];
}
obj->name_len = name_len;
obj->indaddr = adfs_readval(de->dirinddiscadd, 3);
obj->loadaddr = adfs_readval(de->dirload, 4);
obj->execaddr = adfs_readval(de->direxec, 4);
obj->size = adfs_readval(de->dirlen, 4);
obj->attr = de->newdiratts;
adfs_object_fixup(dir, obj);
}
/*
* convert a Linux ADFS directory entry to a disk-based directory entry
*/
static inline void
adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
{
adfs_writeval(de->dirinddiscadd, 3, obj->indaddr);
adfs_writeval(de->dirload, 4, obj->loadaddr);
adfs_writeval(de->direxec, 4, obj->execaddr);
adfs_writeval(de->dirlen, 4, obj->size);
de->newdiratts = obj->attr;
}
/*
* get a directory entry. Note that the caller is responsible
* for holding the relevant locks.
*/
static int
__adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
{
struct adfs_direntry de;
int ret;
ret = adfs_dir_copyfrom(&de, dir, pos, 26);
if (ret)
return ret;
if (!de.dirobname[0])
return -ENOENT;
adfs_dir2obj(dir, obj, &de);
return 0;
}
static int
adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
{
if (fpos >= ADFS_NUM_DIR_ENTRIES)
return -ENOENT;
dir->pos = 5 + fpos * 26;
return 0;
}
static int
adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
{
unsigned int ret;
ret = __adfs_dir_get(dir, dir->pos, obj);
if (ret == 0)
dir->pos += 26;
return ret;
}
static int adfs_f_iterate(struct adfs_dir *dir, struct dir_context *ctx)
{
struct object_info obj;
int pos = 5 + (ctx->pos - 2) * 26;
while (ctx->pos < 2 + ADFS_NUM_DIR_ENTRIES) {
if (__adfs_dir_get(dir, pos, &obj))
break;
if (!dir_emit(ctx, obj.name, obj.name_len,
obj.indaddr, DT_UNKNOWN))
break;
pos += 26;
ctx->pos++;
}
return 0;
}
static int adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
{
struct adfs_direntry de;
int offset, ret;
offset = 5 - (int)sizeof(de);
do {
offset += sizeof(de);
ret = adfs_dir_copyfrom(&de, dir, offset, sizeof(de));
if (ret) {
adfs_error(dir->sb, "error reading directory entry");
return -ENOENT;
}
if (!de.dirobname[0]) {
adfs_error(dir->sb, "unable to locate entry to update");
return -ENOENT;
}
} while (adfs_readval(de.dirinddiscadd, 3) != obj->indaddr);
/* Update the directory entry with the new object state */
adfs_obj2dir(&de, obj);
/* Write the directory entry back to the directory */
return adfs_dir_copyto(dir, offset, &de, 26);
}
static int adfs_f_commit(struct adfs_dir *dir)
{
int ret;
/* Increment directory sequence number */
dir->dirhead->startmasseq += 1;
dir->newtail->endmasseq += 1;
/* Update directory check byte */
dir->newtail->dircheckbyte = adfs_dir_checkbyte(dir);
/* Make sure the directory still validates correctly */
ret = adfs_f_validate(dir);
if (ret)
adfs_msg(dir->sb, KERN_ERR, "error: update broke directory");
return ret;
}
const struct adfs_dir_ops adfs_f_dir_ops = {
.read = adfs_f_read,
.iterate = adfs_f_iterate,
.setpos = adfs_f_setpos,
.getnext = adfs_f_getnext,
.update = adfs_f_update,
.commit = adfs_f_commit,
};