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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/*
* linux/fs/adfs/super.c
*
* Copyright (C) 1997-1999 Russell King
*/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/adfs_fs.h>
#include <linux/malloc.h>
#include <linux/sched.h>
#include <linux/stat.h>
#include <linux/string.h>
#include <linux/locks.h>
#include <linux/init.h>
#include <asm/bitops.h>
#include <asm/uaccess.h>
#include <asm/system.h>
#include <stdarg.h>
#include "adfs.h"
#include "dir_f.h"
#include "dir_fplus.h"
void __adfs_error(struct super_block *sb, const char *function, const char *fmt, ...)
{
char error_buf[128];
va_list args;
va_start(args, fmt);
vsprintf(error_buf, fmt, args);
va_end(args);
printk(KERN_CRIT "ADFS-fs error (device %s)%s%s: %s\n",
kdevname(sb->s_dev), function ? ": " : "",
function ? function : "", error_buf);
}
static int adfs_checkdiscrecord(struct adfs_discrecord *dr)
{
int i;
/* sector size must be 256, 512 or 1024 bytes */
if (dr->log2secsize != 8 &&
dr->log2secsize != 9 &&
dr->log2secsize != 10)
return 1;
/* idlen must be at least log2secsize + 3 */
if (dr->idlen < dr->log2secsize + 3)
return 1;
/* we cannot have such a large disc that we
* are unable to represent sector offsets in
* 32 bits. This works out at 2.0 TB.
*/
if (dr->disc_size_high >> dr->log2secsize)
return 1;
/*
* The following checks are not required for F+
* stage 1.
*/
#if 0
/* idlen must be smaller be no greater than 15 */
if (dr->idlen > 15)
return 1;
/* nzones must be less than 128 for the root
* directory to be addressable
*/
if (dr->nzones >= 128 && dr->nzones_high == 0)
return 1;
/* root must be of the form 0x2.. */
if ((le32_to_cpu(dr->root) & 0xffffff00) != 0x00000200)
return 1;
#else
/*
* Stage 2 F+ does not require the following check
*/
#if 0
/* idlen must be no greater than 16 v2 [1.0] */
if (dr->idlen > 16)
return 1;
/* we can't handle F+ discs yet */
if (dr->format_version || dr->root_size)
return 1;
#else
/* idlen must be no greater than 19 v2 [1.0] */
if (dr->idlen > 19)
return 1;
#endif
#endif
/* reserved bytes should be zero */
for (i = 0; i < sizeof(dr->unused52); i++)
if (dr->unused52[i] != 0)
return 1;
return 0;
}
static unsigned char adfs_calczonecheck(struct super_block *sb, unsigned char *map)
{
unsigned int v0, v1, v2, v3;
int i;
v0 = v1 = v2 = v3 = 0;
for (i = sb->s_blocksize - 4; i; i -= 4) {
v0 += map[i] + (v3 >> 8);
v3 &= 0xff;
v1 += map[i + 1] + (v0 >> 8);
v0 &= 0xff;
v2 += map[i + 2] + (v1 >> 8);
v1 &= 0xff;
v3 += map[i + 3] + (v2 >> 8);
v2 &= 0xff;
}
v0 += v3 >> 8;
v1 += map[1] + (v0 >> 8);
v2 += map[2] + (v1 >> 8);
v3 += map[3] + (v2 >> 8);
return v0 ^ v1 ^ v2 ^ v3;
}
static int adfs_checkmap(struct super_block *sb, struct adfs_discmap *dm)
{
unsigned char crosscheck = 0, zonecheck = 1;
int i;
for (i = 0; i < sb->u.adfs_sb.s_map_size; i++) {
unsigned char *map;
map = dm[i].dm_bh->b_data;
if (adfs_calczonecheck(sb, map) != map[0]) {
adfs_error(sb, "zone %d fails zonecheck", i);
zonecheck = 0;
}
crosscheck ^= map[3];
}
if (crosscheck != 0xff)
adfs_error(sb, "crosscheck != 0xff");
return crosscheck == 0xff && zonecheck;
}
static void adfs_put_super(struct super_block *sb)
{
int i;
for (i = 0; i < sb->u.adfs_sb.s_map_size; i++)
brelse(sb->u.adfs_sb.s_map[i].dm_bh);
kfree(sb->u.adfs_sb.s_map);
}
static int parse_options(struct super_block *sb, char *options)
{
char *value, *opt;
if (!options)
return 0;
for (opt = strtok(options, ","); opt != NULL; opt = strtok(NULL, ",")) {
value = strchr(opt, '=');
if (value)
*value++ = '\0';
if (!strcmp(opt, "uid")) { /* owner of all files */
if (!value || !*value)
return -EINVAL;
sb->u.adfs_sb.s_uid = simple_strtoul(value, &value, 0);
if (*value)
return -EINVAL;
} else
if (!strcmp(opt, "gid")) { /* group owner of all files */
if (!value || !*value)
return -EINVAL;
sb->u.adfs_sb.s_gid = simple_strtoul(value, &value, 0);
if (*value)
return -EINVAL;
} else
if (!strcmp(opt, "ownmask")) { /* owner permission mask */
if (!value || !*value)
return -EINVAL;
sb->u.adfs_sb.s_owner_mask = simple_strtoul(value, &value, 8);
if (*value)
return -EINVAL;
} else
if (!strcmp(opt, "othmask")) { /* others permission mask */
if (!value || !*value)
return -EINVAL;
sb->u.adfs_sb.s_other_mask = simple_strtoul(value, &value, 8);
if (*value)
return -EINVAL;
} else { /* eh? say again. */
printk("ADFS-fs: unrecognised mount option %s\n", opt);
return -EINVAL;
}
}
return 0;
}
static int adfs_remount(struct super_block *sb, int *flags, char *data)
{
return parse_options(sb, data);
}
static int adfs_statfs(struct super_block *sb, struct statfs *buf)
{
struct adfs_sb_info *asb = &sb->u.adfs_sb;
buf->f_type = ADFS_SUPER_MAGIC;
buf->f_namelen = asb->s_namelen;
buf->f_bsize = sb->s_blocksize;
buf->f_blocks = asb->s_size;
buf->f_files = asb->s_ids_per_zone * asb->s_map_size;
buf->f_bavail =
buf->f_bfree = adfs_map_free(sb);
buf->f_ffree = buf->f_bfree * buf->f_files / buf->f_blocks;
return 0;
}
static struct super_operations adfs_sops = {
write_inode: adfs_write_inode,
put_super: adfs_put_super,
statfs: adfs_statfs,
remount_fs: adfs_remount,
};
static struct adfs_discmap *adfs_read_map(struct super_block *sb, struct adfs_discrecord *dr)
{
struct adfs_discmap *dm;
unsigned int map_addr, zone_size, nzones;
int i, zone;
nzones = sb->u.adfs_sb.s_map_size;
zone_size = (8 << dr->log2secsize) - le16_to_cpu(dr->zone_spare);
map_addr = (nzones >> 1) * zone_size -
((nzones > 1) ? ADFS_DR_SIZE_BITS : 0);
map_addr = signed_asl(map_addr, sb->u.adfs_sb.s_map2blk);
sb->u.adfs_sb.s_ids_per_zone = zone_size / (sb->u.adfs_sb.s_idlen + 1);
dm = kmalloc(nzones * sizeof(*dm), GFP_KERNEL);
if (dm == NULL) {
adfs_error(sb, "not enough memory");
return NULL;
}
for (zone = 0; zone < nzones; zone++, map_addr++) {
dm[zone].dm_startbit = 0;
dm[zone].dm_endbit = zone_size;
dm[zone].dm_startblk = zone * zone_size - ADFS_DR_SIZE_BITS;
dm[zone].dm_bh = bread(sb->s_dev, map_addr, sb->s_blocksize);
if (!dm[zone].dm_bh) {
adfs_error(sb, "unable to read map");
goto error_free;
}
}
/* adjust the limits for the first and last map zones */
i = zone - 1;
dm[0].dm_startblk = 0;
dm[0].dm_startbit = ADFS_DR_SIZE_BITS;
dm[i].dm_endbit = (dr->disc_size_high << (32 - dr->log2bpmb)) +
(dr->disc_size >> dr->log2bpmb) +
(ADFS_DR_SIZE_BITS - i * zone_size);
if (adfs_checkmap(sb, dm))
return dm;
adfs_error(sb, NULL, "map corrupted");
error_free:
while (--zone >= 0)
brelse(dm[zone].dm_bh);
kfree(dm);
return NULL;
}
static inline unsigned long adfs_discsize(struct adfs_discrecord *dr, int block_bits)
{
unsigned long discsize;
discsize = le32_to_cpu(dr->disc_size_high) << (32 - block_bits);
discsize |= le32_to_cpu(dr->disc_size) >> block_bits;
return discsize;
}
struct super_block *adfs_read_super(struct super_block *sb, void *data, int silent)
{
struct adfs_discrecord *dr;
struct buffer_head *bh;
struct object_info root_obj;
unsigned char *b_data;
kdev_t dev = sb->s_dev;
/* set default options */
sb->u.adfs_sb.s_uid = 0;
sb->u.adfs_sb.s_gid = 0;
sb->u.adfs_sb.s_owner_mask = S_IRWXU;
sb->u.adfs_sb.s_other_mask = S_IRWXG | S_IRWXO;
if (parse_options(sb, data))
goto error;
set_blocksize(dev, BLOCK_SIZE);
if (!(bh = bread(dev, ADFS_DISCRECORD / BLOCK_SIZE, BLOCK_SIZE))) {
adfs_error(sb, "unable to read superblock");
goto error_unlock;
}
b_data = bh->b_data + (ADFS_DISCRECORD % BLOCK_SIZE);
if (adfs_checkbblk(b_data)) {
if (!silent)
printk("VFS: Can't find an adfs filesystem on dev "
"%s.\n", kdevname(dev));
goto error_free_bh;
}
dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
/*
* Do some sanity checks on the ADFS disc record
*/
if (adfs_checkdiscrecord(dr)) {
if (!silent)
printk("VPS: Can't find an adfs filesystem on dev "
"%s.\n", kdevname(dev));
goto error_free_bh;
}
sb->s_blocksize_bits = dr->log2secsize;
sb->s_blocksize = 1 << sb->s_blocksize_bits;
if (sb->s_blocksize != BLOCK_SIZE &&
(sb->s_blocksize == 512 || sb->s_blocksize == 1024 ||
sb->s_blocksize == 2048 || sb->s_blocksize == 4096)) {
brelse(bh);
set_blocksize(dev, sb->s_blocksize);
bh = bread(dev, ADFS_DISCRECORD / sb->s_blocksize, sb->s_blocksize);
if (!bh) {
adfs_error(sb, "couldn't read superblock on "
"2nd try.");
goto error_unlock;
}
b_data = bh->b_data + (ADFS_DISCRECORD % sb->s_blocksize);
if (adfs_checkbblk(b_data)) {
adfs_error(sb, "disc record mismatch, very weird!");
goto error_free_bh;
}
dr = (struct adfs_discrecord *)(b_data + ADFS_DR_OFFSET);
}
if (sb->s_blocksize != bh->b_size) {
if (!silent)
printk(KERN_ERR "VFS: Unsupported blocksize on dev "
"%s.\n", kdevname(dev));
goto error_free_bh;
}
/*
* blocksize on this device should now be set to the ADFS log2secsize
*/
sb->s_magic = ADFS_SUPER_MAGIC;
sb->u.adfs_sb.s_idlen = dr->idlen;
sb->u.adfs_sb.s_map_size = dr->nzones | (dr->nzones_high << 8);
sb->u.adfs_sb.s_map2blk = dr->log2bpmb - dr->log2secsize;
sb->u.adfs_sb.s_size = adfs_discsize(dr, sb->s_blocksize_bits);
sb->u.adfs_sb.s_version = dr->format_version;
sb->u.adfs_sb.s_log2sharesize = dr->log2sharesize;
sb->u.adfs_sb.s_map = adfs_read_map(sb, dr);
if (!sb->u.adfs_sb.s_map)
goto error_free_bh;
brelse(bh);
/*
* set up enough so that we can read an inode
*/
sb->s_op = &adfs_sops;
dr = (struct adfs_discrecord *)(sb->u.adfs_sb.s_map[0].dm_bh->b_data + 4);
root_obj.parent_id = root_obj.file_id = le32_to_cpu(dr->root);
root_obj.name_len = 0;
root_obj.loadaddr = 0;
root_obj.execaddr = 0;
root_obj.size = ADFS_NEWDIR_SIZE;
root_obj.attr = ADFS_NDA_DIRECTORY | ADFS_NDA_OWNER_READ |
ADFS_NDA_OWNER_WRITE | ADFS_NDA_PUBLIC_READ;
/*
* If this is a F+ disk with variable length directories,
* get the root_size from the disc record.
*/
if (sb->u.adfs_sb.s_version) {
root_obj.size = dr->root_size;
sb->u.adfs_sb.s_dir = &adfs_fplus_dir_ops;
sb->u.adfs_sb.s_namelen = ADFS_FPLUS_NAME_LEN;
} else {
sb->u.adfs_sb.s_dir = &adfs_f_dir_ops;
sb->u.adfs_sb.s_namelen = ADFS_F_NAME_LEN;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,3,0)
sb->s_root = d_alloc_root(adfs_iget(sb, &root_obj));
#else
sb->s_root = d_alloc_root(adfs_iget(sb, &root_obj), NULL);
#endif
if (!sb->s_root) {
int i;
for (i = 0; i < sb->u.adfs_sb.s_map_size; i++)
brelse(sb->u.adfs_sb.s_map[i].dm_bh);
kfree(sb->u.adfs_sb.s_map);
adfs_error(sb, "get root inode failed\n");
goto error_dec_use;
}
return sb;
error_free_bh:
brelse(bh);
error_unlock:
error_dec_use:
error:
return NULL;
}
static DECLARE_FSTYPE_DEV(adfs_fs_type, "adfs", adfs_read_super);
static int __init init_adfs_fs(void)
{
return register_filesystem(&adfs_fs_type);
}
static void __exit exit_adfs_fs(void)
{
unregister_filesystem(&adfs_fs_type);
}
EXPORT_NO_SYMBOLS;
module_init(init_adfs_fs)
module_exit(exit_adfs_fs)
|