Blame view

fs/adfs/dir_f.c 9.71 KB
d2912cb15   Thomas Gleixner   treewide: Replace...
1
  // SPDX-License-Identifier: GPL-2.0-only
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
2
3
4
5
6
  /*
   *  linux/fs/adfs/dir_f.c
   *
   * Copyright (C) 1997-1999 Russell King
   *
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   *  E and F format directory handling
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include "adfs.h"
  #include "dir_f.h"
  
  static void adfs_f_free(struct adfs_dir *dir);
  
  /*
   * Read an (unaligned) value of length 1..4 bytes
   */
  static inline unsigned int adfs_readval(unsigned char *p, int len)
  {
  	unsigned int val = 0;
  
  	switch (len) {
  	case 4:		val |= p[3] << 24;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
23
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
24
  	case 3:		val |= p[2] << 16;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
25
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  	case 2:		val |= p[1] << 8;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
27
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
28
29
30
31
32
33
34
35
36
  	default:	val |= p[0];
  	}
  	return val;
  }
  
  static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
  {
  	switch (len) {
  	case 4:		p[3] = val >> 24;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
37
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
  	case 3:		p[2] = val >> 16;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
39
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
  	case 2:		p[1] = val >> 8;
74f79099e   Gustavo A. R. Silva   adfs: mark expect...
41
  			/* fall through */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
42
43
44
  	default:	p[0] = val;
  	}
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
  #define ror13(v) ((v >> 13) | (v << 19))
  
  #define dir_u8(idx)				\
  	({ int _buf = idx >> blocksize_bits;	\
  	   int _off = idx - (_buf << blocksize_bits);\
  	  *(u8 *)(bh[_buf]->b_data + _off);	\
  	})
  
  #define dir_u32(idx)				\
  	({ int _buf = idx >> blocksize_bits;	\
  	   int _off = idx - (_buf << blocksize_bits);\
  	  *(__le32 *)(bh[_buf]->b_data + _off);	\
  	})
  
  #define bufoff(_bh,_idx)			\
  	({ int _buf = _idx >> blocksize_bits;	\
  	   int _off = _idx - (_buf << blocksize_bits);\
  	  (u8 *)(_bh[_buf]->b_data + _off);	\
  	})
  
  /*
   * There are some algorithms that are nice in
   * assembler, but a bitch in C...  This is one
   * of them.
   */
  static u8
  adfs_dir_checkbyte(const struct adfs_dir *dir)
  {
  	struct buffer_head * const *bh = dir->bh;
  	const int blocksize_bits = dir->sb->s_blocksize_bits;
  	union { __le32 *ptr32; u8 *ptr8; } ptr, end;
  	u32 dircheck = 0;
  	int last = 5 - 26;
  	int i = 0;
  
  	/*
  	 * Accumulate each word up to the last whole
  	 * word of the last directory entry.  This
  	 * can spread across several buffer heads.
  	 */
  	do {
  		last += 26;
  		do {
  			dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
  
  			i += sizeof(u32);
  		} while (i < (last & ~3));
  	} while (dir_u8(last) != 0);
  
  	/*
  	 * Accumulate the last few bytes.  These
  	 * bytes will be within the same bh.
  	 */
  	if (i != last) {
  		ptr.ptr8 = bufoff(bh, i);
  		end.ptr8 = ptr.ptr8 + last - i;
e5949050f   Harvey Harrison   adfs: work around...
101
  		do {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
102
  			dircheck = *ptr.ptr8++ ^ ror13(dircheck);
e5949050f   Harvey Harrison   adfs: work around...
103
  		} while (ptr.ptr8 < end.ptr8);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
  	}
  
  	/*
  	 * The directory tail is in the final bh
  	 * Note that contary to the RISC OS PRMs,
  	 * the first few bytes are NOT included
  	 * in the check.  All bytes are in the
  	 * same bh.
  	 */
  	ptr.ptr8 = bufoff(bh, 2008);
  	end.ptr8 = ptr.ptr8 + 36;
  
  	do {
  		__le32 v = *ptr.ptr32++;
  		dircheck = le32_to_cpu(v) ^ ror13(dircheck);
  	} while (ptr.ptr32 < end.ptr32);
  
  	return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
  }
5ed70bb47   Russell King   fs/adfs: clean up...
123
124
125
  /* Read and check that a directory is valid */
  static int adfs_dir_read(struct super_block *sb, u32 indaddr,
  			 unsigned int size, struct adfs_dir *dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  {
  	const unsigned int blocksize_bits = sb->s_blocksize_bits;
  	int blk = 0;
  
  	/*
  	 * Directories which are not a multiple of 2048 bytes
  	 * are considered bad v2 [3.6]
  	 */
  	if (size & 2047)
  		goto bad_dir;
  
  	size >>= blocksize_bits;
  
  	dir->nr_buffers = 0;
  	dir->sb = sb;
  
  	for (blk = 0; blk < size; blk++) {
  		int phys;
5ed70bb47   Russell King   fs/adfs: clean up...
144
  		phys = __adfs_block_map(sb, indaddr, blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
145
  		if (!phys) {
5ed70bb47   Russell King   fs/adfs: clean up...
146
147
  			adfs_error(sb, "dir %06x has a hole at offset %d",
  				   indaddr, blk);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
  			goto release_buffers;
  		}
  
  		dir->bh[blk] = sb_bread(sb, phys);
  		if (!dir->bh[blk])
  			goto release_buffers;
  	}
  
  	memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
  	memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
  
  	if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
  	    memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
  		goto bad_dir;
  
  	if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
  	    memcmp(&dir->dirhead.startname, "Hugo", 4))
  		goto bad_dir;
  
  	if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
  		goto bad_dir;
  
  	dir->nr_buffers = blk;
  
  	return 0;
  
  bad_dir:
5ed70bb47   Russell King   fs/adfs: clean up...
175
  	adfs_error(sb, "dir %06x is corrupted", indaddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
176
177
178
179
180
181
182
183
184
185
186
187
188
  release_buffers:
  	for (blk -= 1; blk >= 0; blk -= 1)
  		brelse(dir->bh[blk]);
  
  	dir->sb = NULL;
  
  	return -EIO;
  }
  
  /*
   * convert a disk-based directory entry to a Linux ADFS directory entry
   */
  static inline void
da23ef054   Stuart Swales   adfs: add hexadec...
189
190
  adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
  	struct adfs_direntry *de)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
191
  {
adb514a4e   Russell King   fs/adfs: factor o...
192
193
194
195
196
197
198
199
200
201
  	unsigned int name_len;
  
  	for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
  		if (de->dirobname[name_len] < ' ')
  			break;
  
  		obj->name[name_len] = de->dirobname[name_len];
  	}
  
  	obj->name_len =	name_len;
5ed70bb47   Russell King   fs/adfs: clean up...
202
  	obj->indaddr  = adfs_readval(de->dirinddiscadd, 3);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
205
206
  	obj->loadaddr = adfs_readval(de->dirload, 4);
  	obj->execaddr = adfs_readval(de->direxec, 4);
  	obj->size     = adfs_readval(de->dirlen,  4);
  	obj->attr     = de->newdiratts;
da23ef054   Stuart Swales   adfs: add hexadec...
207

411c49bcf   Russell King   fs/adfs: factor o...
208
  	adfs_object_fixup(dir, obj);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209
210
211
212
213
214
215
216
  }
  
  /*
   * convert a Linux ADFS directory entry to a disk-based directory entry
   */
  static inline void
  adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
  {
5ed70bb47   Russell King   fs/adfs: clean up...
217
  	adfs_writeval(de->dirinddiscadd, 3, obj->indaddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
  	adfs_writeval(de->dirload, 4, obj->loadaddr);
  	adfs_writeval(de->direxec, 4, obj->execaddr);
  	adfs_writeval(de->dirlen,  4, obj->size);
  	de->newdiratts = obj->attr;
  }
  
  /*
   * get a directory entry.  Note that the caller is responsible
   * for holding the relevant locks.
   */
  static int
  __adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
  {
  	struct super_block *sb = dir->sb;
  	struct adfs_direntry de;
  	int thissize, buffer, offset;
  
  	buffer = pos >> sb->s_blocksize_bits;
  
  	if (buffer > dir->nr_buffers)
  		return -EINVAL;
  
  	offset = pos & (sb->s_blocksize - 1);
  	thissize = sb->s_blocksize - offset;
  	if (thissize > 26)
  		thissize = 26;
  
  	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
  	if (thissize != 26)
  		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
  		       26 - thissize);
  
  	if (!de.dirobname[0])
  		return -ENOENT;
da23ef054   Stuart Swales   adfs: add hexadec...
252
  	adfs_dir2obj(dir, obj, &de);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  
  	return 0;
  }
  
  static int
  __adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
  {
  	struct super_block *sb = dir->sb;
  	struct adfs_direntry de;
  	int thissize, buffer, offset;
  
  	buffer = pos >> sb->s_blocksize_bits;
  
  	if (buffer > dir->nr_buffers)
  		return -EINVAL;
  
  	offset = pos & (sb->s_blocksize - 1);
  	thissize = sb->s_blocksize - offset;
  	if (thissize > 26)
  		thissize = 26;
  
  	/*
  	 * Get the entry in total
  	 */
  	memcpy(&de, dir->bh[buffer]->b_data + offset, thissize);
  	if (thissize != 26)
  		memcpy(((char *)&de) + thissize, dir->bh[buffer + 1]->b_data,
  		       26 - thissize);
  
  	/*
  	 * update it
  	 */
  	adfs_obj2dir(&de, obj);
  
  	/*
  	 * Put the new entry back
  	 */
  	memcpy(dir->bh[buffer]->b_data + offset, &de, thissize);
  	if (thissize != 26)
  		memcpy(dir->bh[buffer + 1]->b_data, ((char *)&de) + thissize,
  		       26 - thissize);
  
  	return 0;
  }
  
  /*
   * the caller is responsible for holding the necessary
   * locks.
   */
5ed70bb47   Russell King   fs/adfs: clean up...
302
  static int adfs_dir_find_entry(struct adfs_dir *dir, u32 indaddr)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
303
304
305
306
307
308
309
310
311
312
  {
  	int pos, ret;
  
  	ret = -ENOENT;
  
  	for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
  		struct object_info obj;
  
  		if (!__adfs_dir_get(dir, pos, &obj))
  			break;
5ed70bb47   Russell King   fs/adfs: clean up...
313
  		if (obj.indaddr == indaddr) {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
314
315
316
317
318
319
320
  			ret = pos;
  			break;
  		}
  	}
  
  	return ret;
  }
5ed70bb47   Russell King   fs/adfs: clean up...
321
322
  static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
  		       struct adfs_dir *dir)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
323
324
  {
  	int ret;
5ed70bb47   Russell King   fs/adfs: clean up...
325
  	if (size != ADFS_NEWDIR_SIZE)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
326
  		return -EIO;
5ed70bb47   Russell King   fs/adfs: clean up...
327
  	ret = adfs_dir_read(sb, indaddr, size, dir);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
  	if (ret)
  		adfs_error(sb, "unable to read directory");
  	else
  		dir->parent_id = adfs_readval(dir->dirtail.new.dirparent, 3);
  
  	return ret;
  }
  
  static int
  adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
  {
  	if (fpos >= ADFS_NUM_DIR_ENTRIES)
  		return -ENOENT;
  
  	dir->pos = 5 + fpos * 26;
  	return 0;
  }
  
  static int
  adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
  {
  	unsigned int ret;
  
  	ret = __adfs_dir_get(dir, dir->pos, obj);
  	if (ret == 0)
  		dir->pos += 26;
  
  	return ret;
  }
  
  static int
  adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
  {
  	struct super_block *sb = dir->sb;
  	int ret, i;
5ed70bb47   Russell King   fs/adfs: clean up...
363
  	ret = adfs_dir_find_entry(dir, obj->indaddr);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
  	if (ret < 0) {
  		adfs_error(dir->sb, "unable to locate entry to update");
  		goto out;
  	}
  
  	__adfs_dir_put(dir, ret, obj);
   
  	/*
  	 * Increment directory sequence number
  	 */
  	dir->bh[0]->b_data[0] += 1;
  	dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 6] += 1;
  
  	ret = adfs_dir_checkbyte(dir);
  	/*
  	 * Update directory check byte
  	 */
  	dir->bh[dir->nr_buffers - 1]->b_data[sb->s_blocksize - 1] = ret;
  
  #if 1
  	{
  	const unsigned int blocksize_bits = sb->s_blocksize_bits;
  
  	memcpy(&dir->dirhead, bufoff(dir->bh, 0), sizeof(dir->dirhead));
  	memcpy(&dir->dirtail, bufoff(dir->bh, 2007), sizeof(dir->dirtail));
  
  	if (dir->dirhead.startmasseq != dir->dirtail.new.endmasseq ||
  	    memcmp(&dir->dirhead.startname, &dir->dirtail.new.endname, 4))
  		goto bad_dir;
  
  	if (memcmp(&dir->dirhead.startname, "Nick", 4) &&
  	    memcmp(&dir->dirhead.startname, "Hugo", 4))
  		goto bad_dir;
  
  	if (adfs_dir_checkbyte(dir) != dir->dirtail.new.dircheckbyte)
  		goto bad_dir;
  	}
  #endif
  	for (i = dir->nr_buffers - 1; i >= 0; i--)
  		mark_buffer_dirty(dir->bh[i]);
  
  	ret = 0;
  out:
  	return ret;
  #if 1
  bad_dir:
  	adfs_error(dir->sb, "whoops!  I broke a directory!");
  	return -EIO;
  #endif
  }
ffdc9064f   Al Viro   repair adfs ->wri...
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
  static int
  adfs_f_sync(struct adfs_dir *dir)
  {
  	int err = 0;
  	int i;
  
  	for (i = dir->nr_buffers - 1; i >= 0; i--) {
  		struct buffer_head *bh = dir->bh[i];
  		sync_dirty_buffer(bh);
  		if (buffer_req(bh) && !buffer_uptodate(bh))
  			err = -EIO;
  	}
  
  	return err;
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
429
430
431
432
433
434
435
436
437
438
439
440
441
  static void
  adfs_f_free(struct adfs_dir *dir)
  {
  	int i;
  
  	for (i = dir->nr_buffers - 1; i >= 0; i--) {
  		brelse(dir->bh[i]);
  		dir->bh[i] = NULL;
  	}
  
  	dir->nr_buffers = 0;
  	dir->sb = NULL;
  }
0125f504e   Julia Lawall   adfs: constify ad...
442
  const struct adfs_dir_ops adfs_f_dir_ops = {
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
443
444
445
446
  	.read		= adfs_f_read,
  	.setpos		= adfs_f_setpos,
  	.getnext	= adfs_f_getnext,
  	.update		= adfs_f_update,
ffdc9064f   Al Viro   repair adfs ->wri...
447
  	.sync		= adfs_f_sync,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
448
449
  	.free		= adfs_f_free
  };