1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/adfs/dir_f.c
5 * Copyright (C) 1997-1999 Russell King
7 * E and F format directory handling
9 #include <linux/buffer_head.h>
13 static void adfs_f_free(struct adfs_dir *dir);
16 * Read an (unaligned) value of length 1..4 bytes
18 static inline unsigned int adfs_readval(unsigned char *p, int len)
23 case 4: val |= p[3] << 24;
25 case 3: val |= p[2] << 16;
27 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;
39 case 3: p[2] = val >> 16;
41 case 2: p[1] = val >> 8;
47 #define ror13(v) ((v >> 13) | (v << 19))
50 ({ int _buf = idx >> blocksize_bits; \
51 int _off = idx - (_buf << blocksize_bits);\
52 *(u8 *)(bh[_buf]->b_data + _off); \
55 #define dir_u32(idx) \
56 ({ int _buf = idx >> blocksize_bits; \
57 int _off = idx - (_buf << blocksize_bits);\
58 *(__le32 *)(bh[_buf]->b_data + _off); \
61 #define bufoff(_bh,_idx) \
62 ({ int _buf = _idx >> blocksize_bits; \
63 int _off = _idx - (_buf << blocksize_bits);\
64 (u8 *)(_bh[_buf]->b_data + _off); \
68 * There are some algorithms that are nice in
69 * assembler, but a bitch in C... This is one
73 adfs_dir_checkbyte(const struct adfs_dir *dir)
75 struct buffer_head * const *bh = dir->bh;
76 const int blocksize_bits = dir->sb->s_blocksize_bits;
77 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
83 * Accumulate each word up to the last whole
84 * word of the last directory entry. This
85 * can spread across several buffer heads.
90 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
93 } while (i < (last & ~3));
94 } while (dir_u8(last) != 0);
97 * Accumulate the last few bytes. These
98 * bytes will be within the same bh.
101 ptr.ptr8 = bufoff(bh, i);
102 end.ptr8 = ptr.ptr8 + last - i;
105 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
106 } while (ptr.ptr8 < end.ptr8);
110 * The directory tail is in the final bh
111 * Note that contary to the RISC OS PRMs,
112 * the first few bytes are NOT included
113 * in the check. All bytes are in the
116 ptr.ptr8 = bufoff(bh, 2008);
117 end.ptr8 = ptr.ptr8 + 36;
120 __le32 v = *ptr.ptr32++;
121 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
122 } while (ptr.ptr32 < end.ptr32);
124 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
128 * Read and check that a directory is valid
131 adfs_dir_read(struct super_block *sb, unsigned long object_id,
132 unsigned int size, struct adfs_dir *dir)
134 const unsigned int blocksize_bits = sb->s_blocksize_bits;
138 * Directories which are not a multiple of 2048 bytes
139 * are considered bad v2 [3.6]
144 size >>= blocksize_bits;
149 for (blk = 0; blk < size; blk++) {
152 phys = __adfs_block_map(sb, object_id, blk);
154 adfs_error(sb, "dir object %lX has a hole at offset %d",
156 goto release_buffers;
159 dir->bh[blk] = sb_bread(sb, phys);
161 goto release_buffers;
164 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
165 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
167 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
168 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
171 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
172 memcmp(&dir->dirhead.startname, "Hugo", 4))
175 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
178 dir->nr_buffers = blk;
183 adfs_error(sb, "corrupted directory fragment %lX",
186 for (blk -= 1; blk >= 0; blk -= 1)
187 brelse(dir->bh[blk]);
195 * convert a disk-based directory entry to a Linux ADFS directory entry
198 adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
199 struct adfs_direntry *de)
201 unsigned int name_len;
203 for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
204 if (de->dirobname[name_len] < ' ')
207 obj->name[name_len] = de->dirobname[name_len];
210 obj->name_len = name_len;
211 obj->file_id = adfs_readval(de->dirinddiscadd, 3);
212 obj->loadaddr = adfs_readval(de->dirload, 4);
213 obj->execaddr = adfs_readval(de->direxec, 4);
214 obj->size = adfs_readval(de->dirlen, 4);
215 obj->attr = de->newdiratts;
217 adfs_object_fixup(dir, obj);
221 * convert a Linux ADFS directory entry to a disk-based directory entry
224 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
226 adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
227 adfs_writeval(de->dirload, 4, obj->loadaddr);
228 adfs_writeval(de->direxec, 4, obj->execaddr);
229 adfs_writeval(de->dirlen, 4, obj->size);
230 de->newdiratts = obj->attr;
234 * get a directory entry. Note that the caller is responsible
235 * for holding the relevant locks.
238 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
240 struct super_block *sb = dir->sb;
241 struct adfs_direntry de;
242 int thissize, buffer, offset;
244 buffer = pos >> sb->s_blocksize_bits;
246 if (buffer > dir->nr_buffers)
249 offset = pos & (sb->s_blocksize - 1);
250 thissize = sb->s_blocksize - offset;
254 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
256 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
259 if (!de.dirobname[0])
262 adfs_dir2obj(dir, obj, &de);
268 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
270 struct super_block *sb = dir->sb;
271 struct adfs_direntry de;
272 int thissize, buffer, offset;
274 buffer = pos >> sb->s_blocksize_bits;
276 if (buffer > dir->nr_buffers)
279 offset = pos & (sb->s_blocksize - 1);
280 thissize = sb->s_blocksize - offset;
285 * Get the entry in total
287 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
289 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
295 adfs_obj2dir(&de, obj);
298 * Put the new entry back
300 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
302 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
309 * the caller is responsible for holding the necessary
313 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
319 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
320 struct object_info obj;
322 if (!__adfs_dir_get(dir, pos, &obj))
325 if (obj.file_id == object_id) {
335 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
339 if (sz != ADFS_NEWDIR_SIZE)
342 ret = adfs_dir_read(sb, id, sz, dir);
344 adfs_error(sb, "unable to read directory");
346 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
352 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
354 if (fpos >= ADFS_NUM_DIR_ENTRIES)
357 dir->pos = 5 + fpos * 26;
362 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
366 ret = __adfs_dir_get(dir, dir->pos, obj);
374 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
376 struct super_block *sb = dir->sb;
379 ret = adfs_dir_find_entry(dir, obj->file_id);
381 adfs_error(dir->sb, "unable to locate entry to update");
385 __adfs_dir_put(dir, ret, obj);
388 * Increment directory sequence number
390 dir->bh[0]->b_data[0] += 1;
391 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
393 ret = adfs_dir_checkbyte(dir);
395 * Update directory check byte
397 dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
401 const unsigned int blocksize_bits = sb->s_blocksize_bits;
403 memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
404 memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
406 if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
407 memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
410 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
411 memcmp(&dir->dirhead.startname, "Hugo", 4))
414 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
418 for (i = dir->nr_buffers - 1; i >= 0; i--)
419 mark_buffer_dirty(dir->bh[i]);
426 adfs_error(dir->sb, "whoops! I broke a directory!");
432 adfs_f_sync(struct adfs_dir *dir)
437 for (i = dir->nr_buffers - 1; i >= 0; i--) {
438 struct buffer_head *bh = dir->bh[i];
439 sync_dirty_buffer(bh);
440 if (buffer_req(bh) && !buffer_uptodate(bh))
448 adfs_f_free(struct adfs_dir *dir)
452 for (i = dir->nr_buffers - 1; i >= 0; i--) {
461 const struct adfs_dir_ops adfs_f_dir_ops = {
463 .setpos = adfs_f_setpos,
464 .getnext = adfs_f_getnext,
465 .update = adfs_f_update,