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;
27 case 3: val |= p[2] << 16;
28 case 2: val |= p[1] << 8;
34 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
37 case 4: p[3] = val >> 24;
38 case 3: p[2] = val >> 16;
39 case 2: p[1] = val >> 8;
44 static inline int adfs_readname(char *buf, char *ptr, int maxlen)
48 while ((unsigned char)*ptr >= ' ' && maxlen--) {
60 #define ror13(v) ((v >> 13) | (v << 19))
63 ({ int _buf = idx >> blocksize_bits; \
64 int _off = idx - (_buf << blocksize_bits);\
65 *(u8 *)(bh[_buf]->b_data + _off); \
68 #define dir_u32(idx) \
69 ({ int _buf = idx >> blocksize_bits; \
70 int _off = idx - (_buf << blocksize_bits);\
71 *(__le32 *)(bh[_buf]->b_data + _off); \
74 #define bufoff(_bh,_idx) \
75 ({ int _buf = _idx >> blocksize_bits; \
76 int _off = _idx - (_buf << blocksize_bits);\
77 (u8 *)(_bh[_buf]->b_data + _off); \
81 * There are some algorithms that are nice in
82 * assembler, but a bitch in C... This is one
86 adfs_dir_checkbyte(const struct adfs_dir *dir)
88 struct buffer_head * const *bh = dir->bh;
89 const int blocksize_bits = dir->sb->s_blocksize_bits;
90 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
96 * Accumulate each word up to the last whole
97 * word of the last directory entry. This
98 * can spread across several buffer heads.
103 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
106 } while (i < (last & ~3));
107 } while (dir_u8(last) != 0);
110 * Accumulate the last few bytes. These
111 * bytes will be within the same bh.
114 ptr.ptr8 = bufoff(bh, i);
115 end.ptr8 = ptr.ptr8 + last - i;
118 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
119 } while (ptr.ptr8 < end.ptr8);
123 * The directory tail is in the final bh
124 * Note that contary to the RISC OS PRMs,
125 * the first few bytes are NOT included
126 * in the check. All bytes are in the
129 ptr.ptr8 = bufoff(bh, 2008);
130 end.ptr8 = ptr.ptr8 + 36;
133 __le32 v = *ptr.ptr32++;
134 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
135 } while (ptr.ptr32 < end.ptr32);
137 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
141 * Read and check that a directory is valid
144 adfs_dir_read(struct super_block *sb, unsigned long object_id,
145 unsigned int size, struct adfs_dir *dir)
147 const unsigned int blocksize_bits = sb->s_blocksize_bits;
151 * Directories which are not a multiple of 2048 bytes
152 * are considered bad v2 [3.6]
157 size >>= blocksize_bits;
162 for (blk = 0; blk < size; blk++) {
165 phys = __adfs_block_map(sb, object_id, blk);
167 adfs_error(sb, "dir object %lX has a hole at offset %d",
169 goto release_buffers;
172 dir->bh[blk] = sb_bread(sb, phys);
174 goto release_buffers;
177 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
178 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
180 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
181 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
184 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
185 memcmp(&dir->dirhead.startname, "Hugo", 4))
188 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
191 dir->nr_buffers = blk;
196 adfs_error(sb, "corrupted directory fragment %lX",
199 for (blk -= 1; blk >= 0; blk -= 1)
200 brelse(dir->bh[blk]);
208 * convert a disk-based directory entry to a Linux ADFS directory entry
211 adfs_dir2obj(struct object_info *obj, struct adfs_direntry *de)
213 obj->name_len = adfs_readname(obj->name, de->dirobname, ADFS_F_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;
222 * convert a Linux ADFS directory entry to a disk-based directory entry
225 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
227 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
228 adfs_writeval(de->dirload, 4, obj->loadaddr);
229 adfs_writeval(de->direxec, 4, obj->execaddr);
230 adfs_writeval(de->dirlen, 4, obj->size);
231 de->newdiratts = obj->attr;
235 * get a directory entry. Note that the caller is responsible
236 * for holding the relevant locks.
239 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
241 struct super_block *sb = dir->sb;
242 struct adfs_direntry de;
243 int thissize, buffer, offset;
245 buffer = pos >> sb->s_blocksize_bits;
247 if (buffer > dir->nr_buffers)
250 offset = pos & (sb->s_blocksize - 1);
251 thissize = sb->s_blocksize - offset;
255 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
257 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
260 if (!de.dirobname[0])
263 adfs_dir2obj(obj, &de);
269 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
271 struct super_block *sb = dir->sb;
272 struct adfs_direntry de;
273 int thissize, buffer, offset;
275 buffer = pos >> sb->s_blocksize_bits;
277 if (buffer > dir->nr_buffers)
280 offset = pos & (sb->s_blocksize - 1);
281 thissize = sb->s_blocksize - offset;
286 * Get the entry in total
288 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
290 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
296 adfs_obj2dir(&de, obj);
299 * Put the new entry back
301 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
303 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
310 * the caller is responsible for holding the necessary
314 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
320 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
321 struct object_info obj;
323 if (!__adfs_dir_get(dir, pos, &obj))
326 if (obj.file_id == object_id) {
336 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
340 if (sz != ADFS_NEWDIR_SIZE)
343 ret = adfs_dir_read(sb, id, sz, dir);
345 adfs_error(sb, "unable to read directory");
347 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
353 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
355 if (fpos >= ADFS_NUM_DIR_ENTRIES)
358 dir->pos = 5 + fpos * 26;
363 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
367 ret = __adfs_dir_get(dir, dir->pos, obj);
375 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
377 struct super_block *sb = dir->sb;
380 ret = adfs_dir_find_entry(dir, obj->file_id);
382 adfs_error(dir->sb, "unable to locate entry to update");
386 __adfs_dir_put(dir, ret, obj);
389 * Increment directory sequence number
391 dir->bh[0]->b_data[0] += 1;
392 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
394 ret = adfs_dir_checkbyte(dir);
396 * Update directory check byte
398 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
402 const unsigned int blocksize_bits = sb->s_blocksize_bits;
404 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
405 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
407 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
408 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
411 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
412 memcmp(&dir->dirhead.startname, "Hugo", 4))
415 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
419 for (i = dir->nr_buffers - 1; i >= 0; i--)
420 mark_buffer_dirty(dir->bh[i]);
427 adfs_error(dir->sb, "whoops! I broke a directory!");
433 adfs_f_sync(struct adfs_dir *dir)
438 for (i = dir->nr_buffers - 1; i >= 0; i--) {
439 struct buffer_head *bh = dir->bh[i];
440 sync_dirty_buffer(bh);
441 if (buffer_req(bh) && !buffer_uptodate(bh))
449 adfs_f_free(struct adfs_dir *dir)
453 for (i = dir->nr_buffers - 1; i >= 0; i--) {
462 struct adfs_dir_ops adfs_f_dir_ops = {
464 .setpos = adfs_f_setpos,
465 .getnext = adfs_f_getnext,
466 .update = adfs_f_update,