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/errno.h>
14 #include <linux/adfs_fs.h>
15 #include <linux/time.h>
16 #include <linux/stat.h>
17 #include <linux/spinlock.h>
18 #include <linux/buffer_head.h>
19 #include <linux/string.h>
24 static void adfs_f_free(struct adfs_dir *dir);
27 * Read an (unaligned) value of length 1..4 bytes
29 static inline unsigned int adfs_readval(unsigned char *p, int len)
34 case 4: val |= p[3] << 24;
35 case 3: val |= p[2] << 16;
36 case 2: val |= p[1] << 8;
42 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
45 case 4: p[3] = val >> 24;
46 case 3: p[2] = val >> 16;
47 case 2: p[1] = val >> 8;
52 static inline int adfs_readname(char *buf, char *ptr, int maxlen)
56 while ((unsigned char)*ptr >= ' ' && maxlen--) {
68 #define ror13(v) ((v >> 13) | (v << 19))
71 ({ int _buf = idx >> blocksize_bits; \
72 int _off = idx - (_buf << blocksize_bits);\
73 *(u8 *)(bh[_buf]->b_data + _off); \
76 #define dir_u32(idx) \
77 ({ int _buf = idx >> blocksize_bits; \
78 int _off = idx - (_buf << blocksize_bits);\
79 *(__le32 *)(bh[_buf]->b_data + _off); \
82 #define bufoff(_bh,_idx) \
83 ({ int _buf = _idx >> blocksize_bits; \
84 int _off = _idx - (_buf << blocksize_bits);\
85 (u8 *)(_bh[_buf]->b_data + _off); \
89 * There are some algorithms that are nice in
90 * assembler, but a bitch in C... This is one
94 adfs_dir_checkbyte(const struct adfs_dir *dir)
96 struct buffer_head * const *bh = dir->bh;
97 const int blocksize_bits = dir->sb->s_blocksize_bits;
98 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
104 * Accumulate each word up to the last whole
105 * word of the last directory entry. This
106 * can spread across several buffer heads.
111 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
114 } while (i < (last & ~3));
115 } while (dir_u8(last) != 0);
118 * Accumulate the last few bytes. These
119 * bytes will be within the same bh.
122 ptr.ptr8 = bufoff(bh, i);
123 end.ptr8 = ptr.ptr8 + last - i;
126 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
127 while (ptr.ptr8 < end.ptr8);
131 * The directory tail is in the final bh
132 * Note that contary to the RISC OS PRMs,
133 * the first few bytes are NOT included
134 * in the check. All bytes are in the
137 ptr.ptr8 = bufoff(bh, 2008);
138 end.ptr8 = ptr.ptr8 + 36;
141 __le32 v = *ptr.ptr32++;
142 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
143 } while (ptr.ptr32 < end.ptr32);
145 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
149 * Read and check that a directory is valid
152 adfs_dir_read(struct super_block *sb, unsigned long object_id,
153 unsigned int size, struct adfs_dir *dir)
155 const unsigned int blocksize_bits = sb->s_blocksize_bits;
159 * Directories which are not a multiple of 2048 bytes
160 * are considered bad v2 [3.6]
165 size >>= blocksize_bits;
170 for (blk = 0; blk < size; blk++) {
173 phys = __adfs_block_map(sb, object_id, blk);
175 adfs_error(sb, "dir object %lX has a hole at offset %d",
177 goto release_buffers;
180 dir->bh[blk] = sb_bread(sb, phys);
182 goto release_buffers;
185 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
186 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
188 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
189 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
192 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
193 memcmp(&dir->dirhead.startname, "Hugo", 4))
196 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
199 dir->nr_buffers = blk;
204 adfs_error(sb, "corrupted directory fragment %lX",
207 for (blk -= 1; blk >= 0; blk -= 1)
208 brelse(dir->bh[blk]);
216 * convert a disk-based directory entry to a Linux ADFS directory entry
219 adfs_dir2obj(struct object_info *obj, struct adfs_direntry *de)
221 obj->name_len = adfs_readname(obj->name, de->dirobname, ADFS_F_NAME_LEN);
222 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
223 obj->loadaddr = adfs_readval(de->dirload, 4);
224 obj->execaddr = adfs_readval(de->direxec, 4);
225 obj->size = adfs_readval(de->dirlen, 4);
226 obj->attr = de->newdiratts;
230 * convert a Linux ADFS directory entry to a disk-based directory entry
233 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
235 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
236 adfs_writeval(de->dirload, 4, obj->loadaddr);
237 adfs_writeval(de->direxec, 4, obj->execaddr);
238 adfs_writeval(de->dirlen, 4, obj->size);
239 de->newdiratts = obj->attr;
243 * get a directory entry. Note that the caller is responsible
244 * for holding the relevant locks.
247 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
249 struct super_block *sb = dir->sb;
250 struct adfs_direntry de;
251 int thissize, buffer, offset;
253 buffer = pos >> sb->s_blocksize_bits;
255 if (buffer > dir->nr_buffers)
258 offset = pos & (sb->s_blocksize - 1);
259 thissize = sb->s_blocksize - offset;
263 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
265 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
268 if (!de.dirobname[0])
271 adfs_dir2obj(obj, &de);
277 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
279 struct super_block *sb = dir->sb;
280 struct adfs_direntry de;
281 int thissize, buffer, offset;
283 buffer = pos >> sb->s_blocksize_bits;
285 if (buffer > dir->nr_buffers)
288 offset = pos & (sb->s_blocksize - 1);
289 thissize = sb->s_blocksize - offset;
294 * Get the entry in total
296 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
298 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
304 adfs_obj2dir(&de, obj);
307 * Put the new entry back
309 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
311 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
318 * the caller is responsible for holding the necessary
322 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
328 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
329 struct object_info obj;
331 if (!__adfs_dir_get(dir, pos, &obj))
334 if (obj.file_id == object_id) {
344 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
348 if (sz != ADFS_NEWDIR_SIZE)
351 ret = adfs_dir_read(sb, id, sz, dir);
353 adfs_error(sb, "unable to read directory");
355 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
361 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
363 if (fpos >= ADFS_NUM_DIR_ENTRIES)
366 dir->pos = 5 + fpos * 26;
371 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
375 ret = __adfs_dir_get(dir, dir->pos, obj);
383 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
385 struct super_block *sb = dir->sb;
388 ret = adfs_dir_find_entry(dir, obj->file_id);
390 adfs_error(dir->sb, "unable to locate entry to update");
394 __adfs_dir_put(dir, ret, obj);
397 * Increment directory sequence number
399 dir->bh[0]->b_data[0] += 1;
400 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
402 ret = adfs_dir_checkbyte(dir);
404 * Update directory check byte
406 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
410 const unsigned int blocksize_bits = sb->s_blocksize_bits;
412 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
413 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
415 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
416 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
419 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
420 memcmp(&dir->dirhead.startname, "Hugo", 4))
423 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
427 for (i = dir->nr_buffers - 1; i >= 0; i--)
428 mark_buffer_dirty(dir->bh[i]);
435 adfs_error(dir->sb, "whoops! I broke a directory!");
441 adfs_f_free(struct adfs_dir *dir)
445 for (i = dir->nr_buffers - 1; i >= 0; i--) {
454 struct adfs_dir_ops adfs_f_dir_ops = {
456 .setpos = adfs_f_setpos,
457 .getnext = adfs_f_getnext,
458 .update = adfs_f_update,