2 * linux/fs/adfs/dir_f.c
4 * Copyright (C) 1997-1999 Russell King
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.
10 * E and F format directory handling
12 #include <linux/buffer_head.h>
16 static void adfs_f_free(struct adfs_dir *dir);
19 * Read an (unaligned) value of length 1..4 bytes
21 static inline unsigned int adfs_readval(unsigned char *p, int len)
26 case 4: val |= p[3] << 24;
28 case 3: val |= p[2] << 16;
30 case 2: val |= p[1] << 8;
37 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
40 case 4: p[3] = val >> 24;
42 case 3: p[2] = val >> 16;
44 case 2: p[1] = val >> 8;
50 #define ror13(v) ((v >> 13) | (v << 19))
53 ({ int _buf = idx >> blocksize_bits; \
54 int _off = idx - (_buf << blocksize_bits);\
55 *(u8 *)(bh[_buf]->b_data + _off); \
58 #define dir_u32(idx) \
59 ({ int _buf = idx >> blocksize_bits; \
60 int _off = idx - (_buf << blocksize_bits);\
61 *(__le32 *)(bh[_buf]->b_data + _off); \
64 #define bufoff(_bh,_idx) \
65 ({ int _buf = _idx >> blocksize_bits; \
66 int _off = _idx - (_buf << blocksize_bits);\
67 (u8 *)(_bh[_buf]->b_data + _off); \
71 * There are some algorithms that are nice in
72 * assembler, but a bitch in C... This is one
76 adfs_dir_checkbyte(const struct adfs_dir *dir)
78 struct buffer_head * const *bh = dir->bh;
79 const int blocksize_bits = dir->sb->s_blocksize_bits;
80 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
86 * Accumulate each word up to the last whole
87 * word of the last directory entry. This
88 * can spread across several buffer heads.
93 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
96 } while (i < (last & ~3));
97 } while (dir_u8(last) != 0);
100 * Accumulate the last few bytes. These
101 * bytes will be within the same bh.
104 ptr.ptr8 = bufoff(bh, i);
105 end.ptr8 = ptr.ptr8 + last - i;
108 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
109 } while (ptr.ptr8 < end.ptr8);
113 * The directory tail is in the final bh
114 * Note that contary to the RISC OS PRMs,
115 * the first few bytes are NOT included
116 * in the check. All bytes are in the
119 ptr.ptr8 = bufoff(bh, 2008);
120 end.ptr8 = ptr.ptr8 + 36;
123 __le32 v = *ptr.ptr32++;
124 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
125 } while (ptr.ptr32 < end.ptr32);
127 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
131 * Read and check that a directory is valid
134 adfs_dir_read(struct super_block *sb, unsigned long object_id,
135 unsigned int size, struct adfs_dir *dir)
137 const unsigned int blocksize_bits = sb->s_blocksize_bits;
141 * Directories which are not a multiple of 2048 bytes
142 * are considered bad v2 [3.6]
147 size >>= blocksize_bits;
152 for (blk = 0; blk < size; blk++) {
155 phys = __adfs_block_map(sb, object_id, blk);
157 adfs_error(sb, "dir object %lX has a hole at offset %d",
159 goto release_buffers;
162 dir->bh[blk] = sb_bread(sb, phys);
164 goto release_buffers;
167 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
168 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
170 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
171 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
174 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
175 memcmp(&dir->dirhead.startname, "Hugo", 4))
178 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
181 dir->nr_buffers = blk;
186 adfs_error(sb, "corrupted directory fragment %lX",
189 for (blk -= 1; blk >= 0; blk -= 1)
190 brelse(dir->bh[blk]);
198 * convert a disk-based directory entry to a Linux ADFS directory entry
201 adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
202 struct adfs_direntry *de)
204 unsigned int name_len;
206 for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
207 if (de->dirobname[name_len] < ' ')
210 obj->name[name_len] = de->dirobname[name_len];
213 obj->name_len = name_len;
214 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
215 obj->loadaddr = adfs_readval(de->dirload, 4);
216 obj->execaddr = adfs_readval(de->direxec, 4);
217 obj->size = adfs_readval(de->dirlen, 4);
218 obj->attr = de->newdiratts;
220 adfs_object_fixup(dir, obj);
224 * convert a Linux ADFS directory entry to a disk-based directory entry
227 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
229 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
230 adfs_writeval(de->dirload, 4, obj->loadaddr);
231 adfs_writeval(de->direxec, 4, obj->execaddr);
232 adfs_writeval(de->dirlen, 4, obj->size);
233 de->newdiratts = obj->attr;
237 * get a directory entry. Note that the caller is responsible
238 * for holding the relevant locks.
241 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
243 struct super_block *sb = dir->sb;
244 struct adfs_direntry de;
245 int thissize, buffer, offset;
247 buffer = pos >> sb->s_blocksize_bits;
249 if (buffer > dir->nr_buffers)
252 offset = pos & (sb->s_blocksize - 1);
253 thissize = sb->s_blocksize - offset;
257 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
259 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
262 if (!de.dirobname[0])
265 adfs_dir2obj(dir, obj, &de);
271 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
273 struct super_block *sb = dir->sb;
274 struct adfs_direntry de;
275 int thissize, buffer, offset;
277 buffer = pos >> sb->s_blocksize_bits;
279 if (buffer > dir->nr_buffers)
282 offset = pos & (sb->s_blocksize - 1);
283 thissize = sb->s_blocksize - offset;
288 * Get the entry in total
290 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
292 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
298 adfs_obj2dir(&de, obj);
301 * Put the new entry back
303 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
305 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
312 * the caller is responsible for holding the necessary
316 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
322 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
323 struct object_info obj;
325 if (!__adfs_dir_get(dir, pos, &obj))
328 if (obj.file_id == object_id) {
338 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
342 if (sz != ADFS_NEWDIR_SIZE)
345 ret = adfs_dir_read(sb, id, sz, dir);
347 adfs_error(sb, "unable to read directory");
349 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
355 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
357 if (fpos >= ADFS_NUM_DIR_ENTRIES)
360 dir->pos = 5 + fpos * 26;
365 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
369 ret = __adfs_dir_get(dir, dir->pos, obj);
377 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
379 struct super_block *sb = dir->sb;
382 ret = adfs_dir_find_entry(dir, obj->file_id);
384 adfs_error(dir->sb, "unable to locate entry to update");
388 __adfs_dir_put(dir, ret, obj);
391 * Increment directory sequence number
393 dir->bh[0]->b_data[0] += 1;
394 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
396 ret = adfs_dir_checkbyte(dir);
398 * Update directory check byte
400 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
404 const unsigned int blocksize_bits = sb->s_blocksize_bits;
406 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
407 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
409 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
410 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
413 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
414 memcmp(&dir->dirhead.startname, "Hugo", 4))
417 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
421 for (i = dir->nr_buffers - 1; i >= 0; i--)
422 mark_buffer_dirty(dir->bh[i]);
429 adfs_error(dir->sb, "whoops! I broke a directory!");
435 adfs_f_sync(struct adfs_dir *dir)
440 for (i = dir->nr_buffers - 1; i >= 0; i--) {
441 struct buffer_head *bh = dir->bh[i];
442 sync_dirty_buffer(bh);
443 if (buffer_req(bh) && !buffer_uptodate(bh))
451 adfs_f_free(struct adfs_dir *dir)
455 for (i = dir->nr_buffers - 1; i >= 0; i--) {
464 const struct adfs_dir_ops adfs_f_dir_ops = {
466 .setpos = adfs_f_setpos,
467 .getnext = adfs_f_getnext,
468 .update = adfs_f_update,