Driver Core: Move virtual_device_parent() to core.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / adfs / dir_f.c
blobbbfc862592721fe4b79d8f60c36a13386f6c1269
1 /*
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>
13 #include <linux/fs.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>
21 #include "adfs.h"
22 #include "dir_f.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)
31 unsigned int val = 0;
33 switch (len) {
34 case 4: val |= p[3] << 24;
35 case 3: val |= p[2] << 16;
36 case 2: val |= p[1] << 8;
37 default: val |= p[0];
39 return val;
42 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
44 switch (len) {
45 case 4: p[3] = val >> 24;
46 case 3: p[2] = val >> 16;
47 case 2: p[1] = val >> 8;
48 default: p[0] = val;
52 static inline int adfs_readname(char *buf, char *ptr, int maxlen)
54 char *old_buf = buf;
56 while (*ptr >= ' ' && maxlen--) {
57 if (*ptr == '/')
58 *buf++ = '.';
59 else
60 *buf++ = *ptr;
61 ptr++;
63 *buf = '\0';
65 return buf - old_buf;
68 #define ror13(v) ((v >> 13) | (v << 19))
70 #define dir_u8(idx) \
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
91 * of them.
93 static u8
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;
99 u32 dircheck = 0;
100 int last = 5 - 26;
101 int i = 0;
104 * Accumulate each word up to the last whole
105 * word of the last directory entry. This
106 * can spread across several buffer heads.
108 do {
109 last += 26;
110 do {
111 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
113 i += sizeof(u32);
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.
121 if (i != last) {
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
135 * same bh.
137 ptr.ptr8 = bufoff(bh, 2008);
138 end.ptr8 = ptr.ptr8 + 36;
140 do {
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
151 static int
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;
156 int blk = 0;
159 * Directories which are not a multiple of 2048 bytes
160 * are considered bad v2 [3.6]
162 if (size & 2047)
163 goto bad_dir;
165 size >>= blocksize_bits;
167 dir->nr_buffers = 0;
168 dir->sb = sb;
170 for (blk = 0; blk < size; blk++) {
171 int phys;
173 phys = __adfs_block_map(sb, object_id, blk);
174 if (!phys) {
175 adfs_error(sb, "dir object %lX has a hole at offset %d",
176 object_id, blk);
177 goto release_buffers;
180 dir->bh[blk] = sb_bread(sb, phys);
181 if (!dir->bh[blk])
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))
190 goto bad_dir;
192 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
193 memcmp(&dir->dirhead.startname, "Hugo", 4))
194 goto bad_dir;
196 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
197 goto bad_dir;
199 dir->nr_buffers = blk;
201 return 0;
203 bad_dir:
204 adfs_error(sb, "corrupted directory fragment %lX",
205 object_id);
206 release_buffers:
207 for (blk -= 1; blk >= 0; blk -= 1)
208 brelse(dir->bh[blk]);
210 dir->sb = NULL;
212 return -EIO;
216 * convert a disk-based directory entry to a Linux ADFS directory entry
218 static inline void
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
232 static inline void
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.
246 static int
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)
256 return -EINVAL;
258 offset = pos & (sb->s_blocksize - 1);
259 thissize = sb->s_blocksize - offset;
260 if (thissize > 26)
261 thissize = 26;
263 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
264 if (thissize != 26)
265 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
266 26 - thissize);
268 if (!de.dirobname[0])
269 return -ENOENT;
271 adfs_dir2obj(obj, &de);
273 return 0;
276 static int
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)
286 return -EINVAL;
288 offset = pos & (sb->s_blocksize - 1);
289 thissize = sb->s_blocksize - offset;
290 if (thissize > 26)
291 thissize = 26;
294 * Get the entry in total
296 memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
297 if (thissize != 26)
298 memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
299 26 - thissize);
302 * update it
304 adfs_obj2dir(&de, obj);
307 * Put the new entry back
309 memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
310 if (thissize != 26)
311 memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
312 26 - thissize);
314 return 0;
318 * the caller is responsible for holding the necessary
319 * locks.
321 static int
322 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
324 int pos, ret;
326 ret = -ENOENT;
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))
332 break;
334 if (obj.file_id == object_id) {
335 ret = pos;
336 break;
340 return ret;
343 static int
344 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
346 int ret;
348 if (sz != ADFS_NEWDIR_SIZE)
349 return -EIO;
351 ret = adfs_dir_read(sb, id, sz, dir);
352 if (ret)
353 adfs_error(sb, "unable to read directory");
354 else
355 dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
357 return ret;
360 static int
361 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
363 if (fpos >= ADFS_NUM_DIR_ENTRIES)
364 return -ENOENT;
366 dir->pos = 5 + fpos * 26;
367 return 0;
370 static int
371 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
373 unsigned int ret;
375 ret = __adfs_dir_get(dir, dir->pos, obj);
376 if (ret == 0)
377 dir->pos += 26;
379 return ret;
382 static int
383 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
385 struct super_block *sb = dir->sb;
386 int ret, i;
388 ret = adfs_dir_find_entry(dir, obj->file_id);
389 if (ret < 0) {
390 adfs_error(dir->sb, "unable to locate entry to update");
391 goto out;
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;
408 #if 1
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))
417 goto bad_dir;
419 if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
420 memcmp(&dir->dirhead.startname, "Hugo", 4))
421 goto bad_dir;
423 if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
424 goto bad_dir;
426 #endif
427 for (i = dir->nr_buffers - 1; i >= 0; i--)
428 mark_buffer_dirty(dir->bh[i]);
430 ret = 0;
431 out:
432 return ret;
433 #if 1
434 bad_dir:
435 adfs_error(dir->sb, "whoops! I broke a directory!");
436 return -EIO;
437 #endif
440 static void
441 adfs_f_free(struct adfs_dir *dir)
443 int i;
445 for (i = dir->nr_buffers - 1; i >= 0; i--) {
446 brelse(dir->bh[i]);
447 dir->bh[i] = NULL;
450 dir->nr_buffers = 0;
451 dir->sb = NULL;
454 struct adfs_dir_ops adfs_f_dir_ops = {
455 .read = adfs_f_read,
456 .setpos = adfs_f_setpos,
457 .getnext = adfs_f_getnext,
458 .update = adfs_f_update,
459 .free = adfs_f_free