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/version.h>
13 #include <linux/errno.h>
15 #include <linux/adfs_fs.h>
16 #include <linux/sched.h>
17 #include <linux/stat.h>
18 #include <linux/spinlock.h>
23 static void adfs_f_free(struct adfs_dir *dir);
26 * Read an (unaligned) value of length 1..4 bytes
28 static inline unsigned int adfs_readval(unsigned char *p, int len)
33 case 4: val |= p[3] << 24;
34 case 3: val |= p[2] << 16;
35 case 2: val |= p[1] << 8;
41 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
44 case 4: p[3] = val >> 24;
45 case 3: p[2] = val >> 16;
46 case 2: p[1] = val >> 8;
51 static inline int adfs_readname(char *buf, char *ptr, int maxlen)
55 while (*ptr >= ' ' && maxlen--) {
67 static inline void adfs_writename(char *to, char *from, int maxlen)
71 for (i = 0; i < maxlen; i++) {
80 for (; i < maxlen; i++)
84 #define ror13(v) ((v >> 13) | (v << 19))
87 ({ int _buf = idx >> blocksize_bits; \
88 int _off = idx - (_buf << blocksize_bits);\
89 *(u8 *)(bh[_buf]->b_data + _off); \
92 #define dir_u32(idx) \
93 ({ int _buf = idx >> blocksize_bits; \
94 int _off = idx - (_buf << blocksize_bits);\
95 *(u32 *)(bh[_buf]->b_data + _off); \
98 #define bufoff(_bh,_idx) \
99 ({ int _buf = _idx >> blocksize_bits; \
100 int _off = _idx - (_buf << blocksize_bits);\
101 (u8 *)(_bh[_buf]->b_data + _off); \
105 * There are some algorithms that are nice in
106 * assembler, but a bitch in C... This is one
110 adfs_dir_checkbyte(const struct adfs_dir *dir)
112 struct buffer_head * const *bh = dir->bh;
113 const int blocksize_bits = dir->sb->s_blocksize_bits;
114 union { u32 *ptr32; u8 *ptr8; } ptr, end;
120 * Accumulate each word up to the last whole
121 * word of the last directory entry. This
122 * can spread across several buffer heads.
127 dircheck = cpu_to_le32(dir_u32(i)) ^ ror13(dircheck);
130 } while (i < (last & ~3));
131 } while (dir_u8(last) != 0);
134 * Accumulate the last few bytes. These
135 * bytes will be within the same bh.
138 ptr.ptr8 = bufoff(bh, i);
139 end.ptr8 = ptr.ptr8 + last - i;
142 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
143 while (ptr.ptr8 < end.ptr8);
147 * The directory tail is in the final bh
148 * Note that contary to the RISC OS PRMs,
149 * the first few bytes are NOT included
150 * in the check. All bytes are in the
153 ptr.ptr8 = bufoff(bh, 2008);
154 end.ptr8 = ptr.ptr8 + 36;
157 unsigned int v = *ptr.ptr32++;
158 dircheck = cpu_to_le32(v) ^ ror13(dircheck);
159 } while (ptr.ptr32 < end.ptr32);
161 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
165 * Read and check that a directory is valid
168 adfs_dir_read(struct super_block *sb, unsigned long object_id,
169 unsigned int size, struct adfs_dir *dir)
171 const unsigned int blocksize_bits = sb->s_blocksize_bits;
175 * Directories which are not a multiple of 2048 bytes
176 * are considered bad v2 [3.6]
181 size >>= blocksize_bits;
186 for (blk = 0; blk < size; blk++) {
189 phys = __adfs_block_map(sb, object_id, blk);
191 adfs_error(sb, "dir object %lX has a hole at offset %d",
193 goto release_buffers;
196 dir->bh[blk] = sb_bread(sb, phys);
198 goto release_buffers;
201 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
202 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
204 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
205 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
208 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
209 memcmp(&dir->dirhead.startname, "Hugo", 4))
212 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
215 dir->nr_buffers = blk;
220 adfs_error(sb, "corrupted directory fragment %lX",
223 for (blk -= 1; blk >= 0; blk -= 1)
224 brelse(dir->bh[blk]);
232 * convert a disk-based directory entry to a Linux ADFS directory entry
235 adfs_dir2obj(struct object_info *obj, struct adfs_direntry *de)
237 obj->name_len = adfs_readname(obj->name, de->dirobname, ADFS_F_NAME_LEN);
238 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
239 obj->loadaddr = adfs_readval(de->dirload, 4);
240 obj->execaddr = adfs_readval(de->direxec, 4);
241 obj->size = adfs_readval(de->dirlen, 4);
242 obj->attr = de->newdiratts;
246 * convert a Linux ADFS directory entry to a disk-based directory entry
249 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
251 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
252 adfs_writeval(de->dirload, 4, obj->loadaddr);
253 adfs_writeval(de->direxec, 4, obj->execaddr);
254 adfs_writeval(de->dirlen, 4, obj->size);
255 de->newdiratts = obj->attr;
259 * get a directory entry. Note that the caller is responsible
260 * for holding the relevent locks.
263 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
265 struct super_block *sb = dir->sb;
266 struct adfs_direntry de;
267 int thissize, buffer, offset;
269 buffer = pos >> sb->s_blocksize_bits;
271 if (buffer > dir->nr_buffers)
274 offset = pos & (sb->s_blocksize - 1);
275 thissize = sb->s_blocksize - offset;
279 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
281 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
284 if (!de.dirobname[0])
287 adfs_dir2obj(obj, &de);
293 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
295 struct super_block *sb = dir->sb;
296 struct adfs_direntry de;
297 int thissize, buffer, offset;
299 buffer = pos >> sb->s_blocksize_bits;
301 if (buffer > dir->nr_buffers)
304 offset = pos & (sb->s_blocksize - 1);
305 thissize = sb->s_blocksize - offset;
310 * Get the entry in total
312 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
314 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
320 adfs_obj2dir(&de, obj);
323 * Put the new entry back
325 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
327 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
334 * the caller is responsible for holding the necessary
338 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
344 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
345 struct object_info obj;
347 if (!__adfs_dir_get(dir, pos, &obj))
350 if (obj.file_id == object_id) {
360 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
364 if (sz != ADFS_NEWDIR_SIZE)
367 ret = adfs_dir_read(sb, id, sz, dir);
369 adfs_error(sb, "unable to read directory");
371 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
377 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
379 if (fpos >= ADFS_NUM_DIR_ENTRIES)
382 dir->pos = 5 + fpos * 26;
387 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
391 ret = __adfs_dir_get(dir, dir->pos, obj);
399 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
401 struct super_block *sb = dir->sb;
404 ret = adfs_dir_find_entry(dir, obj->file_id);
406 adfs_error(dir->sb, "unable to locate entry to update");
410 __adfs_dir_put(dir, ret, obj);
413 * Increment directory sequence number
415 dir->bh[0]->b_data[0] += 1;
416 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
418 ret = adfs_dir_checkbyte(dir);
420 * Update directory check byte
422 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
426 const unsigned int blocksize_bits = sb->s_blocksize_bits;
428 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
429 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
431 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
432 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
435 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
436 memcmp(&dir->dirhead.startname, "Hugo", 4))
439 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
443 for (i = dir->nr_buffers - 1; i >= 0; i--)
444 mark_buffer_dirty(dir->bh[i]);
451 adfs_error(dir->sb, "whoops! I broke a directory!");
457 adfs_f_free(struct adfs_dir *dir)
461 for (i = dir->nr_buffers - 1; i >= 0; i--) {
470 struct adfs_dir_ops adfs_f_dir_ops = {