diff options
Diffstat (limited to 'fs/adfs/dir_f.c')
-rw-r--r-- | fs/adfs/dir_f.c | 460 |
1 files changed, 460 insertions, 0 deletions
diff --git a/fs/adfs/dir_f.c b/fs/adfs/dir_f.c new file mode 100644 index 000000000000..bbfc86259272 --- /dev/null +++ b/fs/adfs/dir_f.c | |||
@@ -0,0 +1,460 @@ | |||
1 | /* | ||
2 | * linux/fs/adfs/dir_f.c | ||
3 | * | ||
4 | * Copyright (C) 1997-1999 Russell King | ||
5 | * | ||
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. | ||
9 | * | ||
10 | * E and F format directory handling | ||
11 | */ | ||
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> | ||
20 | |||
21 | #include "adfs.h" | ||
22 | #include "dir_f.h" | ||
23 | |||
24 | static void adfs_f_free(struct adfs_dir *dir); | ||
25 | |||
26 | /* | ||
27 | * Read an (unaligned) value of length 1..4 bytes | ||
28 | */ | ||
29 | static inline unsigned int adfs_readval(unsigned char *p, int len) | ||
30 | { | ||
31 | unsigned int val = 0; | ||
32 | |||
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]; | ||
38 | } | ||
39 | return val; | ||
40 | } | ||
41 | |||
42 | static inline void adfs_writeval(unsigned char *p, int len, unsigned int val) | ||
43 | { | ||
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; | ||
49 | } | ||
50 | } | ||
51 | |||
52 | static inline int adfs_readname(char *buf, char *ptr, int maxlen) | ||
53 | { | ||
54 | char *old_buf = buf; | ||
55 | |||
56 | while (*ptr >= ' ' && maxlen--) { | ||
57 | if (*ptr == '/') | ||
58 | *buf++ = '.'; | ||
59 | else | ||
60 | *buf++ = *ptr; | ||
61 | ptr++; | ||
62 | } | ||
63 | *buf = '\0'; | ||
64 | |||
65 | return buf - old_buf; | ||
66 | } | ||
67 | |||
68 | #define ror13(v) ((v >> 13) | (v << 19)) | ||
69 | |||
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); \ | ||
74 | }) | ||
75 | |||
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); \ | ||
80 | }) | ||
81 | |||
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); \ | ||
86 | }) | ||
87 | |||
88 | /* | ||
89 | * There are some algorithms that are nice in | ||
90 | * assembler, but a bitch in C... This is one | ||
91 | * of them. | ||
92 | */ | ||
93 | static u8 | ||
94 | adfs_dir_checkbyte(const struct adfs_dir *dir) | ||
95 | { | ||
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; | ||
102 | |||
103 | /* | ||
104 | * Accumulate each word up to the last whole | ||
105 | * word of the last directory entry. This | ||
106 | * can spread across several buffer heads. | ||
107 | */ | ||
108 | do { | ||
109 | last += 26; | ||
110 | do { | ||
111 | dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck); | ||
112 | |||
113 | i += sizeof(u32); | ||
114 | } while (i < (last & ~3)); | ||
115 | } while (dir_u8(last) != 0); | ||
116 | |||
117 | /* | ||
118 | * Accumulate the last few bytes. These | ||
119 | * bytes will be within the same bh. | ||
120 | */ | ||
121 | if (i != last) { | ||
122 | ptr.ptr8 = bufoff(bh, i); | ||
123 | end.ptr8 = ptr.ptr8 + last - i; | ||
124 | |||
125 | do | ||
126 | dircheck = *ptr.ptr8++ ^ ror13(dircheck); | ||
127 | while (ptr.ptr8 < end.ptr8); | ||
128 | } | ||
129 | |||
130 | /* | ||
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. | ||
136 | */ | ||
137 | ptr.ptr8 = bufoff(bh, 2008); | ||
138 | end.ptr8 = ptr.ptr8 + 36; | ||
139 | |||
140 | do { | ||
141 | __le32 v = *ptr.ptr32++; | ||
142 | dircheck = le32_to_cpu(v) ^ ror13(dircheck); | ||
143 | } while (ptr.ptr32 < end.ptr32); | ||
144 | |||
145 | return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff; | ||
146 | } | ||
147 | |||
148 | /* | ||
149 | * Read and check that a directory is valid | ||
150 | */ | ||
151 | static int | ||
152 | adfs_dir_read(struct super_block *sb, unsigned long object_id, | ||
153 | unsigned int size, struct adfs_dir *dir) | ||
154 | { | ||
155 | const unsigned int blocksize_bits = sb->s_blocksize_bits; | ||
156 | int blk = 0; | ||
157 | |||
158 | /* | ||
159 | * Directories which are not a multiple of 2048 bytes | ||
160 | * are considered bad v2 [3.6] | ||
161 | */ | ||
162 | if (size & 2047) | ||
163 | goto bad_dir; | ||
164 | |||
165 | size >>= blocksize_bits; | ||
166 | |||
167 | dir->nr_buffers = 0; | ||
168 | dir->sb = sb; | ||
169 | |||
170 | for (blk = 0; blk < size; blk++) { | ||
171 | int phys; | ||
172 | |||
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; | ||
178 | } | ||
179 | |||
180 | dir->bh[blk] = sb_bread(sb, phys); | ||
181 | if (!dir->bh[blk]) | ||
182 | goto release_buffers; | ||
183 | } | ||
184 | |||
185 | memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead)); | ||
186 | memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail)); | ||
187 | |||
188 | if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq || | ||
189 | memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4)) | ||
190 | goto bad_dir; | ||
191 | |||
192 | if (memcmp(&dir->dirhead.startname, "Nick", 4) && | ||
193 | memcmp(&dir->dirhead.startname, "Hugo", 4)) | ||
194 | goto bad_dir; | ||
195 | |||
196 | if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte) | ||
197 | goto bad_dir; | ||
198 | |||
199 | dir->nr_buffers = blk; | ||
200 | |||
201 | return 0; | ||
202 | |||
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]); | ||
209 | |||
210 | dir->sb = NULL; | ||
211 | |||
212 | return -EIO; | ||
213 | } | ||
214 | |||
215 | /* | ||
216 | * convert a disk-based directory entry to a Linux ADFS directory entry | ||
217 | */ | ||
218 | static inline void | ||
219 | adfs_dir2obj(struct object_info *obj, struct adfs_direntry *de) | ||
220 | { | ||
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; | ||
227 | } | ||
228 | |||
229 | /* | ||
230 | * convert a Linux ADFS directory entry to a disk-based directory entry | ||
231 | */ | ||
232 | static inline void | ||
233 | adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj) | ||
234 | { | ||
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; | ||
240 | } | ||
241 | |||
242 | /* | ||
243 | * get a directory entry. Note that the caller is responsible | ||
244 | * for holding the relevant locks. | ||
245 | */ | ||
246 | static int | ||
247 | __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj) | ||
248 | { | ||
249 | struct super_block *sb = dir->sb; | ||
250 | struct adfs_direntry de; | ||
251 | int thissize, buffer, offset; | ||
252 | |||
253 | buffer = pos >> sb->s_blocksize_bits; | ||
254 | |||
255 | if (buffer > dir->nr_buffers) | ||
256 | return -EINVAL; | ||
257 | |||
258 | offset = pos & (sb->s_blocksize - 1); | ||
259 | thissize = sb->s_blocksize - offset; | ||
260 | if (thissize > 26) | ||
261 | thissize = 26; | ||
262 | |||
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); | ||
267 | |||
268 | if (!de.dirobname[0]) | ||
269 | return -ENOENT; | ||
270 | |||
271 | adfs_dir2obj(obj, &de); | ||
272 | |||
273 | return 0; | ||
274 | } | ||
275 | |||
276 | static int | ||
277 | __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj) | ||
278 | { | ||
279 | struct super_block *sb = dir->sb; | ||
280 | struct adfs_direntry de; | ||
281 | int thissize, buffer, offset; | ||
282 | |||
283 | buffer = pos >> sb->s_blocksize_bits; | ||
284 | |||
285 | if (buffer > dir->nr_buffers) | ||
286 | return -EINVAL; | ||
287 | |||
288 | offset = pos & (sb->s_blocksize - 1); | ||
289 | thissize = sb->s_blocksize - offset; | ||
290 | if (thissize > 26) | ||
291 | thissize = 26; | ||
292 | |||
293 | /* | ||
294 | * Get the entry in total | ||
295 | */ | ||
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); | ||
300 | |||
301 | /* | ||
302 | * update it | ||
303 | */ | ||
304 | adfs_obj2dir(&de, obj); | ||
305 | |||
306 | /* | ||
307 | * Put the new entry back | ||
308 | */ | ||
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); | ||
313 | |||
314 | return 0; | ||
315 | } | ||
316 | |||
317 | /* | ||
318 | * the caller is responsible for holding the necessary | ||
319 | * locks. | ||
320 | */ | ||
321 | static int | ||
322 | adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id) | ||
323 | { | ||
324 | int pos, ret; | ||
325 | |||
326 | ret = -ENOENT; | ||
327 | |||
328 | for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) { | ||
329 | struct object_info obj; | ||
330 | |||
331 | if (!__adfs_dir_get(dir, pos, &obj)) | ||
332 | break; | ||
333 | |||
334 | if (obj.file_id == object_id) { | ||
335 | ret = pos; | ||
336 | break; | ||
337 | } | ||
338 | } | ||
339 | |||
340 | return ret; | ||
341 | } | ||
342 | |||
343 | static int | ||
344 | adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir) | ||
345 | { | ||
346 | int ret; | ||
347 | |||
348 | if (sz != ADFS_NEWDIR_SIZE) | ||
349 | return -EIO; | ||
350 | |||
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); | ||
356 | |||
357 | return ret; | ||
358 | } | ||
359 | |||
360 | static int | ||
361 | adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos) | ||
362 | { | ||
363 | if (fpos >= ADFS_NUM_DIR_ENTRIES) | ||
364 | return -ENOENT; | ||
365 | |||
366 | dir->pos = 5 + fpos * 26; | ||
367 | return 0; | ||
368 | } | ||
369 | |||
370 | static int | ||
371 | adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj) | ||
372 | { | ||
373 | unsigned int ret; | ||
374 | |||
375 | ret = __adfs_dir_get(dir, dir->pos, obj); | ||
376 | if (ret == 0) | ||
377 | dir->pos += 26; | ||
378 | |||
379 | return ret; | ||
380 | } | ||
381 | |||
382 | static int | ||
383 | adfs_f_update(struct adfs_dir *dir, struct object_info *obj) | ||
384 | { | ||
385 | struct super_block *sb = dir->sb; | ||
386 | int ret, i; | ||
387 | |||
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; | ||
392 | } | ||
393 | |||
394 | __adfs_dir_put(dir, ret, obj); | ||
395 | |||
396 | /* | ||
397 | * Increment directory sequence number | ||
398 | */ | ||
399 | dir->bh[0]->b_data[0] += 1; | ||
400 | dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1; | ||
401 | |||
402 | ret = adfs_dir_checkbyte(dir); | ||
403 | /* | ||
404 | * Update directory check byte | ||
405 | */ | ||
406 | dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret; | ||
407 | |||
408 | #if 1 | ||
409 | { | ||
410 | const unsigned int blocksize_bits = sb->s_blocksize_bits; | ||
411 | |||
412 | memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead)); | ||
413 | memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail)); | ||
414 | |||
415 | if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq || | ||
416 | memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4)) | ||
417 | goto bad_dir; | ||
418 | |||
419 | if (memcmp(&dir->dirhead.startname, "Nick", 4) && | ||
420 | memcmp(&dir->dirhead.startname, "Hugo", 4)) | ||
421 | goto bad_dir; | ||
422 | |||
423 | if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte) | ||
424 | goto bad_dir; | ||
425 | } | ||
426 | #endif | ||
427 | for (i = dir->nr_buffers - 1; i >= 0; i--) | ||
428 | mark_buffer_dirty(dir->bh[i]); | ||
429 | |||
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 | ||
438 | } | ||
439 | |||
440 | static void | ||
441 | adfs_f_free(struct adfs_dir *dir) | ||
442 | { | ||
443 | int i; | ||
444 | |||
445 | for (i = dir->nr_buffers - 1; i >= 0; i--) { | ||
446 | brelse(dir->bh[i]); | ||
447 | dir->bh[i] = NULL; | ||
448 | } | ||
449 | |||
450 | dir->nr_buffers = 0; | ||
451 | dir->sb = NULL; | ||
452 | } | ||
453 | |||
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 | ||
460 | }; | ||