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 static inline int adfs_readname(char *buf, char *ptr, int maxlen)
54 while ((unsigned char)*ptr >= ' ' && maxlen--) {
65 #define ror13(v) ((v >> 13) | (v << 19))
68 ({ int _buf = idx >> blocksize_bits; \
69 int _off = idx - (_buf << blocksize_bits);\
70 *(u8 *)(bh[_buf]->b_data + _off); \
73 #define dir_u32(idx) \
74 ({ int _buf = idx >> blocksize_bits; \
75 int _off = idx - (_buf << blocksize_bits);\
76 *(__le32 *)(bh[_buf]->b_data + _off); \
79 #define bufoff(_bh,_idx) \
80 ({ int _buf = _idx >> blocksize_bits; \
81 int _off = _idx - (_buf << blocksize_bits);\
82 (u8 *)(_bh[_buf]->b_data + _off); \
86 * There are some algorithms that are nice in
87 * assembler, but a bitch in C... This is one
91 adfs_dir_checkbyte(const struct adfs_dir *dir)
93 struct buffer_head * const *bh = dir->bh;
94 const int blocksize_bits = dir->sb->s_blocksize_bits;
95 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
101 * Accumulate each word up to the last whole
102 * word of the last directory entry. This
103 * can spread across several buffer heads.
108 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
111 } while (i < (last & ~3));
112 } while (dir_u8(last) != 0);
115 * Accumulate the last few bytes. These
116 * bytes will be within the same bh.
119 ptr.ptr8 = bufoff(bh, i);
120 end.ptr8 = ptr.ptr8 + last - i;
123 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
124 } while (ptr.ptr8 < end.ptr8);
128 * The directory tail is in the final bh
129 * Note that contary to the RISC OS PRMs,
130 * the first few bytes are NOT included
131 * in the check. All bytes are in the
134 ptr.ptr8 = bufoff(bh, 2008);
135 end.ptr8 = ptr.ptr8 + 36;
138 __le32 v = *ptr.ptr32++;
139 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
140 } while (ptr.ptr32 < end.ptr32);
142 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
146 * Read and check that a directory is valid
149 adfs_dir_read(struct super_block *sb, unsigned long object_id,
150 unsigned int size, struct adfs_dir *dir)
152 const unsigned int blocksize_bits = sb->s_blocksize_bits;
156 * Directories which are not a multiple of 2048 bytes
157 * are considered bad v2 [3.6]
162 size >>= blocksize_bits;
167 for (blk = 0; blk < size; blk++) {
170 phys = __adfs_block_map(sb, object_id, blk);
172 adfs_error(sb, "dir object %lX has a hole at offset %d",
174 goto release_buffers;
177 dir->bh[blk] = sb_bread(sb, phys);
179 goto release_buffers;
182 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
183 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
185 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
186 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
189 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
190 memcmp(&dir->dirhead.startname, "Hugo", 4))
193 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
196 dir->nr_buffers = blk;
201 adfs_error(sb, "corrupted directory fragment %lX",
204 for (blk -= 1; blk >= 0; blk -= 1)
205 brelse(dir->bh[blk]);
213 * convert a disk-based directory entry to a Linux ADFS directory entry
216 adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
217 struct adfs_direntry *de)
219 obj->name_len = adfs_readname(obj->name, de->dirobname, ADFS_F_NAME_LEN);
220 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
221 obj->loadaddr = adfs_readval(de->dirload, 4);
222 obj->execaddr = adfs_readval(de->direxec, 4);
223 obj->size = adfs_readval(de->dirlen, 4);
224 obj->attr = de->newdiratts;
228 * object is a file and is filetyped and timestamped?
229 * RISC OS 12-bit filetype is stored in load_address[19:8]
231 if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
232 (0xfff00000 == (0xfff00000 & obj->loadaddr))) {
233 obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);
235 /* optionally append the ,xyz hex filetype suffix */
236 if (ADFS_SB(dir->sb)->s_ftsuffix)
238 append_filetype_suffix(
239 &obj->name[obj->name_len],
245 * convert a Linux ADFS directory entry to a disk-based directory entry
248 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
250 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
251 adfs_writeval(de->dirload, 4, obj->loadaddr);
252 adfs_writeval(de->direxec, 4, obj->execaddr);
253 adfs_writeval(de->dirlen, 4, obj->size);
254 de->newdiratts = obj->attr;
258 * get a directory entry. Note that the caller is responsible
259 * for holding the relevant locks.
262 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
264 struct super_block *sb = dir->sb;
265 struct adfs_direntry de;
266 int thissize, buffer, offset;
268 buffer = pos >> sb->s_blocksize_bits;
270 if (buffer > dir->nr_buffers)
273 offset = pos & (sb->s_blocksize - 1);
274 thissize = sb->s_blocksize - offset;
278 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
280 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
283 if (!de.dirobname[0])
286 adfs_dir2obj(dir, obj, &de);
292 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
294 struct super_block *sb = dir->sb;
295 struct adfs_direntry de;
296 int thissize, buffer, offset;
298 buffer = pos >> sb->s_blocksize_bits;
300 if (buffer > dir->nr_buffers)
303 offset = pos & (sb->s_blocksize - 1);
304 thissize = sb->s_blocksize - offset;
309 * Get the entry in total
311 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
313 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
319 adfs_obj2dir(&de, obj);
322 * Put the new entry back
324 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
326 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
333 * the caller is responsible for holding the necessary
337 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
343 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
344 struct object_info obj;
346 if (!__adfs_dir_get(dir, pos, &obj))
349 if (obj.file_id == object_id) {
359 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
363 if (sz != ADFS_NEWDIR_SIZE)
366 ret = adfs_dir_read(sb, id, sz, dir);
368 adfs_error(sb, "unable to read directory");
370 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
376 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
378 if (fpos >= ADFS_NUM_DIR_ENTRIES)
381 dir->pos = 5 + fpos * 26;
386 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
390 ret = __adfs_dir_get(dir, dir->pos, obj);
398 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
400 struct super_block *sb = dir->sb;
403 ret = adfs_dir_find_entry(dir, obj->file_id);
405 adfs_error(dir->sb, "unable to locate entry to update");
409 __adfs_dir_put(dir, ret, obj);
412 * Increment directory sequence number
414 dir->bh[0]->b_data[0] += 1;
415 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
417 ret = adfs_dir_checkbyte(dir);
419 * Update directory check byte
421 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
425 const unsigned int blocksize_bits = sb->s_blocksize_bits;
427 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
428 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
430 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
431 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
434 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
435 memcmp(&dir->dirhead.startname, "Hugo", 4))
438 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
442 for (i = dir->nr_buffers - 1; i >= 0; i--)
443 mark_buffer_dirty(dir->bh[i]);
450 adfs_error(dir->sb, "whoops! I broke a directory!");
456 adfs_f_sync(struct adfs_dir *dir)
461 for (i = dir->nr_buffers - 1; i >= 0; i--) {
462 struct buffer_head *bh = dir->bh[i];
463 sync_dirty_buffer(bh);
464 if (buffer_req(bh) && !buffer_uptodate(bh))
472 adfs_f_free(struct adfs_dir *dir)
476 for (i = dir->nr_buffers - 1; i >= 0; i--) {
485 const struct adfs_dir_ops adfs_f_dir_ops = {
487 .setpos = adfs_f_setpos,
488 .getnext = adfs_f_getnext,
489 .update = adfs_f_update,