blob: 033884541a632ccfbc0928d39cb5358826a312e0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/adfs/dir_f.c
3 *
4 * 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.
9 *
10 * E and F format directory handling
11 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/buffer_head.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "adfs.h"
14#include "dir_f.h"
15
16static void adfs_f_free(struct adfs_dir *dir);
17
18/*
19 * Read an (unaligned) value of length 1..4 bytes
20 */
21static inline unsigned int adfs_readval(unsigned char *p, int len)
22{
23 unsigned int val = 0;
24
25 switch (len) {
26 case 4: val |= p[3] << 24;
27 case 3: val |= p[2] << 16;
28 case 2: val |= p[1] << 8;
29 default: val |= p[0];
30 }
31 return val;
32}
33
34static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
35{
36 switch (len) {
37 case 4: p[3] = val >> 24;
38 case 3: p[2] = val >> 16;
39 case 2: p[1] = val >> 8;
40 default: p[0] = val;
41 }
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ror13(v) ((v >> 13) | (v << 19))
45
46#define dir_u8(idx) \
47 ({ int _buf = idx >> blocksize_bits; \
48 int _off = idx - (_buf << blocksize_bits);\
49 *(u8 *)(bh[_buf]->b_data + _off); \
50 })
51
52#define dir_u32(idx) \
53 ({ int _buf = idx >> blocksize_bits; \
54 int _off = idx - (_buf << blocksize_bits);\
55 *(__le32 *)(bh[_buf]->b_data + _off); \
56 })
57
58#define bufoff(_bh,_idx) \
59 ({ int _buf = _idx >> blocksize_bits; \
60 int _off = _idx - (_buf << blocksize_bits);\
61 (u8 *)(_bh[_buf]->b_data + _off); \
62 })
63
64/*
65 * There are some algorithms that are nice in
66 * assembler, but a bitch in C... This is one
67 * of them.
68 */
69static u8
70adfs_dir_checkbyte(const struct adfs_dir *dir)
71{
72 struct buffer_head * const *bh = dir->bh;
73 const int blocksize_bits = dir->sb->s_blocksize_bits;
74 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
75 u32 dircheck = 0;
76 int last = 5 - 26;
77 int i = 0;
78
79 /*
80 * Accumulate each word up to the last whole
81 * word of the last directory entry. This
82 * can spread across several buffer heads.
83 */
84 do {
85 last += 26;
86 do {
87 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
88
89 i += sizeof(u32);
90 } while (i < (last & ~3));
91 } while (dir_u8(last) != 0);
92
93 /*
94 * Accumulate the last few bytes. These
95 * bytes will be within the same bh.
96 */
97 if (i != last) {
98 ptr.ptr8 = bufoff(bh, i);
99 end.ptr8 = ptr.ptr8 + last - i;
100
Harvey Harrisone5949052008-04-29 00:58:41 -0700101 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
Harvey Harrisone5949052008-04-29 00:58:41 -0700103 } while (ptr.ptr8 < end.ptr8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 /*
107 * The directory tail is in the final bh
108 * Note that contary to the RISC OS PRMs,
109 * the first few bytes are NOT included
110 * in the check. All bytes are in the
111 * same bh.
112 */
113 ptr.ptr8 = bufoff(bh, 2008);
114 end.ptr8 = ptr.ptr8 + 36;
115
116 do {
117 __le32 v = *ptr.ptr32++;
118 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
119 } while (ptr.ptr32 < end.ptr32);
120
121 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
122}
123
124/*
125 * Read and check that a directory is valid
126 */
127static int
128adfs_dir_read(struct super_block *sb, unsigned long object_id,
129 unsigned int size, struct adfs_dir *dir)
130{
131 const unsigned int blocksize_bits = sb->s_blocksize_bits;
132 int blk = 0;
133
134 /*
135 * Directories which are not a multiple of 2048 bytes
136 * are considered bad v2 [3.6]
137 */
138 if (size & 2047)
139 goto bad_dir;
140
141 size >>= blocksize_bits;
142
143 dir->nr_buffers = 0;
144 dir->sb = sb;
145
146 for (blk = 0; blk < size; blk++) {
147 int phys;
148
149 phys = __adfs_block_map(sb, object_id, blk);
150 if (!phys) {
151 adfs_error(sb, "dir object %lX has a hole at offset %d",
152 object_id, blk);
153 goto release_buffers;
154 }
155
156 dir->bh[blk] = sb_bread(sb, phys);
157 if (!dir->bh[blk])
158 goto release_buffers;
159 }
160
161 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
162 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
163
164 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
165 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
166 goto bad_dir;
167
168 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
169 memcmp(&dir->dirhead.startname, "Hugo", 4))
170 goto bad_dir;
171
172 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
173 goto bad_dir;
174
175 dir->nr_buffers = blk;
176
177 return 0;
178
179bad_dir:
180 adfs_error(sb, "corrupted directory fragment %lX",
181 object_id);
182release_buffers:
183 for (blk -= 1; blk >= 0; blk -= 1)
184 brelse(dir->bh[blk]);
185
186 dir->sb = NULL;
187
188 return -EIO;
189}
190
191/*
192 * convert a disk-based directory entry to a Linux ADFS directory entry
193 */
194static inline void
Stuart Swalesda23ef02011-03-22 16:35:06 -0700195adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
196 struct adfs_direntry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Russell Kingadb514a2019-03-24 13:22:28 +0000198 unsigned int name_len;
199
200 for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
201 if (de->dirobname[name_len] < ' ')
202 break;
203
204 obj->name[name_len] = de->dirobname[name_len];
205 }
206
207 obj->name_len = name_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
209 obj->loadaddr = adfs_readval(de->dirload, 4);
210 obj->execaddr = adfs_readval(de->direxec, 4);
211 obj->size = adfs_readval(de->dirlen, 4);
212 obj->attr = de->newdiratts;
Stuart Swalesda23ef02011-03-22 16:35:06 -0700213
Russell King411c49b2019-03-24 12:57:32 +0000214 adfs_object_fixup(dir, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
217/*
218 * convert a Linux ADFS directory entry to a disk-based directory entry
219 */
220static inline void
221adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
222{
223 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
224 adfs_writeval(de->dirload, 4, obj->loadaddr);
225 adfs_writeval(de->direxec, 4, obj->execaddr);
226 adfs_writeval(de->dirlen, 4, obj->size);
227 de->newdiratts = obj->attr;
228}
229
230/*
231 * get a directory entry. Note that the caller is responsible
232 * for holding the relevant locks.
233 */
234static int
235__adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
236{
237 struct super_block *sb = dir->sb;
238 struct adfs_direntry de;
239 int thissize, buffer, offset;
240
241 buffer = pos >> sb->s_blocksize_bits;
242
243 if (buffer > dir->nr_buffers)
244 return -EINVAL;
245
246 offset = pos & (sb->s_blocksize - 1);
247 thissize = sb->s_blocksize - offset;
248 if (thissize > 26)
249 thissize = 26;
250
251 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
252 if (thissize != 26)
253 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
254 26 - thissize);
255
256 if (!de.dirobname[0])
257 return -ENOENT;
258
Stuart Swalesda23ef02011-03-22 16:35:06 -0700259 adfs_dir2obj(dir, obj, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 return 0;
262}
263
264static int
265__adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
266{
267 struct super_block *sb = dir->sb;
268 struct adfs_direntry de;
269 int thissize, buffer, offset;
270
271 buffer = pos >> sb->s_blocksize_bits;
272
273 if (buffer > dir->nr_buffers)
274 return -EINVAL;
275
276 offset = pos & (sb->s_blocksize - 1);
277 thissize = sb->s_blocksize - offset;
278 if (thissize > 26)
279 thissize = 26;
280
281 /*
282 * Get the entry in total
283 */
284 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
285 if (thissize != 26)
286 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
287 26 - thissize);
288
289 /*
290 * update it
291 */
292 adfs_obj2dir(&de, obj);
293
294 /*
295 * Put the new entry back
296 */
297 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
298 if (thissize != 26)
299 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
300 26 - thissize);
301
302 return 0;
303}
304
305/*
306 * the caller is responsible for holding the necessary
307 * locks.
308 */
309static int
310adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
311{
312 int pos, ret;
313
314 ret = -ENOENT;
315
316 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
317 struct object_info obj;
318
319 if (!__adfs_dir_get(dir, pos, &obj))
320 break;
321
322 if (obj.file_id == object_id) {
323 ret = pos;
324 break;
325 }
326 }
327
328 return ret;
329}
330
331static int
332adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
333{
334 int ret;
335
336 if (sz != ADFS_NEWDIR_SIZE)
337 return -EIO;
338
339 ret = adfs_dir_read(sb, id, sz, dir);
340 if (ret)
341 adfs_error(sb, "unable to read directory");
342 else
343 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
344
345 return ret;
346}
347
348static int
349adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
350{
351 if (fpos >= ADFS_NUM_DIR_ENTRIES)
352 return -ENOENT;
353
354 dir->pos = 5 + fpos * 26;
355 return 0;
356}
357
358static int
359adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
360{
361 unsigned int ret;
362
363 ret = __adfs_dir_get(dir, dir->pos, obj);
364 if (ret == 0)
365 dir->pos += 26;
366
367 return ret;
368}
369
370static int
371adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
372{
373 struct super_block *sb = dir->sb;
374 int ret, i;
375
376 ret = adfs_dir_find_entry(dir, obj->file_id);
377 if (ret < 0) {
378 adfs_error(dir->sb, "unable to locate entry to update");
379 goto out;
380 }
381
382 __adfs_dir_put(dir, ret, obj);
383
384 /*
385 * Increment directory sequence number
386 */
387 dir->bh[0]->b_data[0] += 1;
388 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
389
390 ret = adfs_dir_checkbyte(dir);
391 /*
392 * Update directory check byte
393 */
394 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
395
396#if 1
397 {
398 const unsigned int blocksize_bits = sb->s_blocksize_bits;
399
400 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
401 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
402
403 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
404 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
405 goto bad_dir;
406
407 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
408 memcmp(&dir->dirhead.startname, "Hugo", 4))
409 goto bad_dir;
410
411 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
412 goto bad_dir;
413 }
414#endif
415 for (i = dir->nr_buffers - 1; i >= 0; i--)
416 mark_buffer_dirty(dir->bh[i]);
417
418 ret = 0;
419out:
420 return ret;
421#if 1
422bad_dir:
423 adfs_error(dir->sb, "whoops! I broke a directory!");
424 return -EIO;
425#endif
426}
427
Al Viroffdc9062009-06-08 00:44:42 -0400428static int
429adfs_f_sync(struct adfs_dir *dir)
430{
431 int err = 0;
432 int i;
433
434 for (i = dir->nr_buffers - 1; i >= 0; i--) {
435 struct buffer_head *bh = dir->bh[i];
436 sync_dirty_buffer(bh);
437 if (buffer_req(bh) && !buffer_uptodate(bh))
438 err = -EIO;
439 }
440
441 return err;
442}
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444static void
445adfs_f_free(struct adfs_dir *dir)
446{
447 int i;
448
449 for (i = dir->nr_buffers - 1; i >= 0; i--) {
450 brelse(dir->bh[i]);
451 dir->bh[i] = NULL;
452 }
453
454 dir->nr_buffers = 0;
455 dir->sb = NULL;
456}
457
Julia Lawall0125f502015-11-21 16:15:37 +0100458const struct adfs_dir_ops adfs_f_dir_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 .read = adfs_f_read,
460 .setpos = adfs_f_setpos,
461 .getnext = adfs_f_getnext,
462 .update = adfs_f_update,
Al Viroffdc9062009-06-08 00:44:42 -0400463 .sync = adfs_f_sync,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 .free = adfs_f_free
465};