xref: /openbmc/linux/fs/adfs/dir_f.c (revision b7019ac5)
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/buffer_head.h>
13 #include "adfs.h"
14 #include "dir_f.h"
15 
16 static void adfs_f_free(struct adfs_dir *dir);
17 
18 /*
19  * Read an (unaligned) value of length 1..4 bytes
20  */
21 static inline unsigned int adfs_readval(unsigned char *p, int len)
22 {
23 	unsigned int val = 0;
24 
25 	switch (len) {
26 	case 4:		val |= p[3] << 24;
27 			/* fall through */
28 	case 3:		val |= p[2] << 16;
29 			/* fall through */
30 	case 2:		val |= p[1] << 8;
31 			/* fall through */
32 	default:	val |= p[0];
33 	}
34 	return val;
35 }
36 
37 static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
38 {
39 	switch (len) {
40 	case 4:		p[3] = val >> 24;
41 			/* fall through */
42 	case 3:		p[2] = val >> 16;
43 			/* fall through */
44 	case 2:		p[1] = val >> 8;
45 			/* fall through */
46 	default:	p[0] = val;
47 	}
48 }
49 
50 #define ror13(v) ((v >> 13) | (v << 19))
51 
52 #define dir_u8(idx)				\
53 	({ int _buf = idx >> blocksize_bits;	\
54 	   int _off = idx - (_buf << blocksize_bits);\
55 	  *(u8 *)(bh[_buf]->b_data + _off);	\
56 	})
57 
58 #define dir_u32(idx)				\
59 	({ int _buf = idx >> blocksize_bits;	\
60 	   int _off = idx - (_buf << blocksize_bits);\
61 	  *(__le32 *)(bh[_buf]->b_data + _off);	\
62 	})
63 
64 #define bufoff(_bh,_idx)			\
65 	({ int _buf = _idx >> blocksize_bits;	\
66 	   int _off = _idx - (_buf << blocksize_bits);\
67 	  (u8 *)(_bh[_buf]->b_data + _off);	\
68 	})
69 
70 /*
71  * There are some algorithms that are nice in
72  * assembler, but a bitch in C...  This is one
73  * of them.
74  */
75 static u8
76 adfs_dir_checkbyte(const struct adfs_dir *dir)
77 {
78 	struct buffer_head * const *bh = dir->bh;
79 	const int blocksize_bits = dir->sb->s_blocksize_bits;
80 	union { __le32 *ptr32; u8 *ptr8; } ptr, end;
81 	u32 dircheck = 0;
82 	int last = 5 - 26;
83 	int i = 0;
84 
85 	/*
86 	 * Accumulate each word up to the last whole
87 	 * word of the last directory entry.  This
88 	 * can spread across several buffer heads.
89 	 */
90 	do {
91 		last += 26;
92 		do {
93 			dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
94 
95 			i += sizeof(u32);
96 		} while (i < (last & ~3));
97 	} while (dir_u8(last) != 0);
98 
99 	/*
100 	 * Accumulate the last few bytes.  These
101 	 * bytes will be within the same bh.
102 	 */
103 	if (i != last) {
104 		ptr.ptr8 = bufoff(bh, i);
105 		end.ptr8 = ptr.ptr8 + last - i;
106 
107 		do {
108 			dircheck = *ptr.ptr8++ ^ ror13(dircheck);
109 		} while (ptr.ptr8 < end.ptr8);
110 	}
111 
112 	/*
113 	 * The directory tail is in the final bh
114 	 * Note that contary to the RISC OS PRMs,
115 	 * the first few bytes are NOT included
116 	 * in the check.  All bytes are in the
117 	 * same bh.
118 	 */
119 	ptr.ptr8 = bufoff(bh, 2008);
120 	end.ptr8 = ptr.ptr8 + 36;
121 
122 	do {
123 		__le32 v = *ptr.ptr32++;
124 		dircheck = le32_to_cpu(v) ^ ror13(dircheck);
125 	} while (ptr.ptr32 < end.ptr32);
126 
127 	return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
128 }
129 
130 /*
131  * Read and check that a directory is valid
132  */
133 static int
134 adfs_dir_read(struct super_block *sb, unsigned long object_id,
135 	      unsigned int size, struct adfs_dir *dir)
136 {
137 	const unsigned int blocksize_bits = sb->s_blocksize_bits;
138 	int blk = 0;
139 
140 	/*
141 	 * Directories which are not a multiple of 2048 bytes
142 	 * are considered bad v2 [3.6]
143 	 */
144 	if (size & 2047)
145 		goto bad_dir;
146 
147 	size >>= blocksize_bits;
148 
149 	dir->nr_buffers = 0;
150 	dir->sb = sb;
151 
152 	for (blk = 0; blk < size; blk++) {
153 		int phys;
154 
155 		phys = __adfs_block_map(sb, object_id, blk);
156 		if (!phys) {
157 			adfs_error(sb, "dir object %lX has a hole at offset %d",
158 				   object_id, blk);
159 			goto release_buffers;
160 		}
161 
162 		dir->bh[blk] = sb_bread(sb, phys);
163 		if (!dir->bh[blk])
164 			goto release_buffers;
165 	}
166 
167 	memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
168 	memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
169 
170 	if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
171 	    memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
172 		goto bad_dir;
173 
174 	if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
175 	    memcmp(&dir->dirhead.startname, "Hugo", 4))
176 		goto bad_dir;
177 
178 	if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
179 		goto bad_dir;
180 
181 	dir->nr_buffers = blk;
182 
183 	return 0;
184 
185 bad_dir:
186 	adfs_error(sb, "corrupted directory fragment %lX",
187 		   object_id);
188 release_buffers:
189 	for (blk -= 1; blk >= 0; blk -= 1)
190 		brelse(dir->bh[blk]);
191 
192 	dir->sb = NULL;
193 
194 	return -EIO;
195 }
196 
197 /*
198  * convert a disk-based directory entry to a Linux ADFS directory entry
199  */
200 static inline void
201 adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
202 	struct adfs_direntry *de)
203 {
204 	unsigned int name_len;
205 
206 	for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
207 		if (de->dirobname[name_len] < ' ')
208 			break;
209 
210 		obj->name[name_len] = de->dirobname[name_len];
211 	}
212 
213 	obj->name_len =	name_len;
214 	obj->file_id  = adfs_readval(de->dirinddiscadd, 3);
215 	obj->loadaddr = adfs_readval(de->dirload, 4);
216 	obj->execaddr = adfs_readval(de->direxec, 4);
217 	obj->size     = adfs_readval(de->dirlen,  4);
218 	obj->attr     = de->newdiratts;
219 
220 	adfs_object_fixup(dir, obj);
221 }
222 
223 /*
224  * convert a Linux ADFS directory entry to a disk-based directory entry
225  */
226 static inline void
227 adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
228 {
229 	adfs_writeval(de->dirinddiscadd, 3, obj->file_id);
230 	adfs_writeval(de->dirload, 4, obj->loadaddr);
231 	adfs_writeval(de->direxec, 4, obj->execaddr);
232 	adfs_writeval(de->dirlen,  4, obj->size);
233 	de->newdiratts = obj->attr;
234 }
235 
236 /*
237  * get a directory entry.  Note that the caller is responsible
238  * for holding the relevant locks.
239  */
240 static int
241 __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
242 {
243 	struct super_block *sb = dir->sb;
244 	struct adfs_direntry de;
245 	int thissize, buffer, offset;
246 
247 	buffer = pos >> sb->s_blocksize_bits;
248 
249 	if (buffer > dir->nr_buffers)
250 		return -EINVAL;
251 
252 	offset = pos & (sb->s_blocksize - 1);
253 	thissize = sb->s_blocksize - offset;
254 	if (thissize > 26)
255 		thissize = 26;
256 
257 	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
258 	if (thissize != 26)
259 		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
260 		       26 - thissize);
261 
262 	if (!de.dirobname[0])
263 		return -ENOENT;
264 
265 	adfs_dir2obj(dir, obj, &de);
266 
267 	return 0;
268 }
269 
270 static int
271 __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
272 {
273 	struct super_block *sb = dir->sb;
274 	struct adfs_direntry de;
275 	int thissize, buffer, offset;
276 
277 	buffer = pos >> sb->s_blocksize_bits;
278 
279 	if (buffer > dir->nr_buffers)
280 		return -EINVAL;
281 
282 	offset = pos & (sb->s_blocksize - 1);
283 	thissize = sb->s_blocksize - offset;
284 	if (thissize > 26)
285 		thissize = 26;
286 
287 	/*
288 	 * Get the entry in total
289 	 */
290 	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
291 	if (thissize != 26)
292 		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
293 		       26 - thissize);
294 
295 	/*
296 	 * update it
297 	 */
298 	adfs_obj2dir(&de, obj);
299 
300 	/*
301 	 * Put the new entry back
302 	 */
303 	memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
304 	if (thissize != 26)
305 		memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
306 		       26 - thissize);
307 
308 	return 0;
309 }
310 
311 /*
312  * the caller is responsible for holding the necessary
313  * locks.
314  */
315 static int
316 adfs_dir_find_entry(struct adfs_dir *dir, unsigned long object_id)
317 {
318 	int pos, ret;
319 
320 	ret = -ENOENT;
321 
322 	for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
323 		struct object_info obj;
324 
325 		if (!__adfs_dir_get(dir, pos, &obj))
326 			break;
327 
328 		if (obj.file_id == object_id) {
329 			ret = pos;
330 			break;
331 		}
332 	}
333 
334 	return ret;
335 }
336 
337 static int
338 adfs_f_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
339 {
340 	int ret;
341 
342 	if (sz != ADFS_NEWDIR_SIZE)
343 		return -EIO;
344 
345 	ret = adfs_dir_read(sb, id, sz, dir);
346 	if (ret)
347 		adfs_error(sb, "unable to read directory");
348 	else
349 		dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
350 
351 	return ret;
352 }
353 
354 static int
355 adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
356 {
357 	if (fpos >= ADFS_NUM_DIR_ENTRIES)
358 		return -ENOENT;
359 
360 	dir->pos = 5 + fpos * 26;
361 	return 0;
362 }
363 
364 static int
365 adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
366 {
367 	unsigned int ret;
368 
369 	ret = __adfs_dir_get(dir, dir->pos, obj);
370 	if (ret == 0)
371 		dir->pos += 26;
372 
373 	return ret;
374 }
375 
376 static int
377 adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
378 {
379 	struct super_block *sb = dir->sb;
380 	int ret, i;
381 
382 	ret = adfs_dir_find_entry(dir, obj->file_id);
383 	if (ret < 0) {
384 		adfs_error(dir->sb, "unable to locate entry to update");
385 		goto out;
386 	}
387 
388 	__adfs_dir_put(dir, ret, obj);
389 
390 	/*
391 	 * Increment directory sequence number
392 	 */
393 	dir->bh[0]->b_data[0] += 1;
394 	dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
395 
396 	ret = adfs_dir_checkbyte(dir);
397 	/*
398 	 * Update directory check byte
399 	 */
400 	dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
401 
402 #if 1
403 	{
404 	const unsigned int blocksize_bits = sb->s_blocksize_bits;
405 
406 	memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
407 	memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
408 
409 	if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
410 	    memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
411 		goto bad_dir;
412 
413 	if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
414 	    memcmp(&dir->dirhead.startname, "Hugo", 4))
415 		goto bad_dir;
416 
417 	if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
418 		goto bad_dir;
419 	}
420 #endif
421 	for (i = dir->nr_buffers - 1; i >= 0; i--)
422 		mark_buffer_dirty(dir->bh[i]);
423 
424 	ret = 0;
425 out:
426 	return ret;
427 #if 1
428 bad_dir:
429 	adfs_error(dir->sb, "whoops!  I broke a directory!");
430 	return -EIO;
431 #endif
432 }
433 
434 static int
435 adfs_f_sync(struct adfs_dir *dir)
436 {
437 	int err = 0;
438 	int i;
439 
440 	for (i = dir->nr_buffers - 1; i >= 0; i--) {
441 		struct buffer_head *bh = dir->bh[i];
442 		sync_dirty_buffer(bh);
443 		if (buffer_req(bh) && !buffer_uptodate(bh))
444 			err = -EIO;
445 	}
446 
447 	return err;
448 }
449 
450 static void
451 adfs_f_free(struct adfs_dir *dir)
452 {
453 	int i;
454 
455 	for (i = dir->nr_buffers - 1; i >= 0; i--) {
456 		brelse(dir->bh[i]);
457 		dir->bh[i] = NULL;
458 	}
459 
460 	dir->nr_buffers = 0;
461 	dir->sb = NULL;
462 }
463 
464 const struct adfs_dir_ops adfs_f_dir_ops = {
465 	.read		= adfs_f_read,
466 	.setpos		= adfs_f_setpos,
467 	.getnext	= adfs_f_getnext,
468 	.update		= adfs_f_update,
469 	.sync		= adfs_f_sync,
470 	.free		= adfs_f_free
471 };
472