blob: 196706d581bf6d9bed9e0caadd3e1be023a72aaf [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/adfs/dir_f.c
4 *
5 * Copyright (C) 1997-1999 Russell King
6 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * E and F format directory handling
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include "adfs.h"
10#include "dir_f.h"
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012/*
13 * Read an (unaligned) value of length 1..4 bytes
14 */
15static inline unsigned int adfs_readval(unsigned char *p, int len)
16{
17 unsigned int val = 0;
18
19 switch (len) {
20 case 4: val |= p[3] << 24;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060021 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 case 3: val |= p[2] << 16;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060023 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 case 2: val |= p[1] << 8;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060025 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 default: val |= p[0];
27 }
28 return val;
29}
30
31static inline void adfs_writeval(unsigned char *p, int len, unsigned int val)
32{
33 switch (len) {
34 case 4: p[3] = val >> 24;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060035 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 case 3: p[2] = val >> 16;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060037 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 case 2: p[1] = val >> 8;
Gustavo A. R. Silva74f79092019-01-14 14:30:36 -060039 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 default: p[0] = val;
41 }
42}
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#define ror13(v) ((v >> 13) | (v << 19))
45
46#define dir_u8(idx) \
47 ({ int _buf = idx >> blocksize_bits; \
48 int _off = idx - (_buf << blocksize_bits);\
49 *(u8 *)(bh[_buf]->b_data + _off); \
50 })
51
52#define dir_u32(idx) \
53 ({ int _buf = idx >> blocksize_bits; \
54 int _off = idx - (_buf << blocksize_bits);\
55 *(__le32 *)(bh[_buf]->b_data + _off); \
56 })
57
58#define bufoff(_bh,_idx) \
59 ({ int _buf = _idx >> blocksize_bits; \
60 int _off = _idx - (_buf << blocksize_bits);\
Russell King016936b2019-12-09 11:10:21 +000061 (void *)(_bh[_buf]->b_data + _off); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 })
63
64/*
65 * There are some algorithms that are nice in
66 * assembler, but a bitch in C... This is one
67 * of them.
68 */
69static u8
70adfs_dir_checkbyte(const struct adfs_dir *dir)
71{
72 struct buffer_head * const *bh = dir->bh;
73 const int blocksize_bits = dir->sb->s_blocksize_bits;
74 union { __le32 *ptr32; u8 *ptr8; } ptr, end;
75 u32 dircheck = 0;
76 int last = 5 - 26;
77 int i = 0;
78
79 /*
80 * Accumulate each word up to the last whole
81 * word of the last directory entry. This
82 * can spread across several buffer heads.
83 */
84 do {
85 last += 26;
86 do {
87 dircheck = le32_to_cpu(dir_u32(i)) ^ ror13(dircheck);
88
89 i += sizeof(u32);
90 } while (i < (last & ~3));
91 } while (dir_u8(last) != 0);
92
93 /*
94 * Accumulate the last few bytes. These
95 * bytes will be within the same bh.
96 */
97 if (i != last) {
98 ptr.ptr8 = bufoff(bh, i);
99 end.ptr8 = ptr.ptr8 + last - i;
100
Harvey Harrisone5949052008-04-29 00:58:41 -0700101 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 dircheck = *ptr.ptr8++ ^ ror13(dircheck);
Harvey Harrisone5949052008-04-29 00:58:41 -0700103 } while (ptr.ptr8 < end.ptr8);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 }
105
106 /*
107 * The directory tail is in the final bh
108 * Note that contary to the RISC OS PRMs,
109 * the first few bytes are NOT included
110 * in the check. All bytes are in the
111 * same bh.
112 */
113 ptr.ptr8 = bufoff(bh, 2008);
114 end.ptr8 = ptr.ptr8 + 36;
115
116 do {
117 __le32 v = *ptr.ptr32++;
118 dircheck = le32_to_cpu(v) ^ ror13(dircheck);
119 } while (ptr.ptr32 < end.ptr32);
120
121 return (dircheck ^ (dircheck >> 8) ^ (dircheck >> 16) ^ (dircheck >> 24)) & 0xff;
122}
123
Russell Kingffc8df32019-12-09 11:10:27 +0000124static int adfs_f_validate(struct adfs_dir *dir)
125{
126 struct adfs_dirheader *head = dir->dirhead;
127 struct adfs_newdirtail *tail = dir->newtail;
128
129 if (head->startmasseq != tail->endmasseq ||
130 (memcmp(&head->startname, "Nick", 4) &&
131 memcmp(&head->startname, "Hugo", 4)) ||
132 memcmp(&head->startname, &tail->endname, 4) ||
133 adfs_dir_checkbyte(dir) != tail->dircheckbyte)
134 return -EIO;
135
136 return 0;
137}
138
Russell King5ed70bb2019-06-04 14:49:57 +0100139/* Read and check that a directory is valid */
140static int adfs_dir_read(struct super_block *sb, u32 indaddr,
141 unsigned int size, struct adfs_dir *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 const unsigned int blocksize_bits = sb->s_blocksize_bits;
Russell King419a6e52019-12-09 11:09:35 +0000144 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
146 /*
147 * Directories which are not a multiple of 2048 bytes
148 * are considered bad v2 [3.6]
149 */
150 if (size & 2047)
151 goto bad_dir;
152
Russell King419a6e52019-12-09 11:09:35 +0000153 ret = adfs_dir_read_buffers(sb, indaddr, size, dir);
154 if (ret)
155 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Russell King016936b2019-12-09 11:10:21 +0000157 dir->dirhead = bufoff(dir->bh, 0);
158 dir->newtail = bufoff(dir->bh, 2007);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
Russell Kingffc8df32019-12-09 11:10:27 +0000160 if (adfs_f_validate(dir))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 goto bad_dir;
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 return 0;
164
165bad_dir:
Russell King5ed70bb2019-06-04 14:49:57 +0100166 adfs_error(sb, "dir %06x is corrupted", indaddr);
Russell King1dd9f5b2019-12-09 11:09:20 +0000167 adfs_dir_relse(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
169 return -EIO;
170}
171
172/*
173 * convert a disk-based directory entry to a Linux ADFS directory entry
174 */
175static inline void
Stuart Swalesda23ef02011-03-22 16:35:06 -0700176adfs_dir2obj(struct adfs_dir *dir, struct object_info *obj,
177 struct adfs_direntry *de)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Russell Kingadb514a2019-03-24 13:22:28 +0000179 unsigned int name_len;
180
181 for (name_len = 0; name_len < ADFS_F_NAME_LEN; name_len++) {
182 if (de->dirobname[name_len] < ' ')
183 break;
184
185 obj->name[name_len] = de->dirobname[name_len];
186 }
187
188 obj->name_len = name_len;
Russell King5ed70bb2019-06-04 14:49:57 +0100189 obj->indaddr = adfs_readval(de->dirinddiscadd, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 obj->loadaddr = adfs_readval(de->dirload, 4);
191 obj->execaddr = adfs_readval(de->direxec, 4);
192 obj->size = adfs_readval(de->dirlen, 4);
193 obj->attr = de->newdiratts;
Stuart Swalesda23ef02011-03-22 16:35:06 -0700194
Russell King411c49b2019-03-24 12:57:32 +0000195 adfs_object_fixup(dir, obj);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196}
197
198/*
199 * convert a Linux ADFS directory entry to a disk-based directory entry
200 */
201static inline void
202adfs_obj2dir(struct adfs_direntry *de, struct object_info *obj)
203{
Russell King5ed70bb2019-06-04 14:49:57 +0100204 adfs_writeval(de->dirinddiscadd, 3, obj->indaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 adfs_writeval(de->dirload, 4, obj->loadaddr);
206 adfs_writeval(de->direxec, 4, obj->execaddr);
207 adfs_writeval(de->dirlen, 4, obj->size);
208 de->newdiratts = obj->attr;
209}
210
211/*
212 * get a directory entry. Note that the caller is responsible
213 * for holding the relevant locks.
214 */
215static int
216__adfs_dir_get(struct adfs_dir *dir, int pos, struct object_info *obj)
217{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 struct adfs_direntry de;
Russell Kinga3171202019-12-09 11:09:30 +0000219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Russell Kinga3171202019-12-09 11:09:30 +0000221 ret = adfs_dir_copyfrom(&de, dir, pos, 26);
222 if (ret)
223 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
225 if (!de.dirobname[0])
226 return -ENOENT;
227
Stuart Swalesda23ef02011-03-22 16:35:06 -0700228 adfs_dir2obj(dir, obj, &de);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 return 0;
231}
232
233static int
234__adfs_dir_put(struct adfs_dir *dir, int pos, struct object_info *obj)
235{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 struct adfs_direntry de;
Russell Kinga3171202019-12-09 11:09:30 +0000237 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Russell Kinga3171202019-12-09 11:09:30 +0000239 ret = adfs_dir_copyfrom(&de, dir, pos, 26);
240 if (ret)
241 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 adfs_obj2dir(&de, obj);
244
Russell Kinga3171202019-12-09 11:09:30 +0000245 return adfs_dir_copyto(dir, pos, &de, 26);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/*
249 * the caller is responsible for holding the necessary
250 * locks.
251 */
Russell King5ed70bb2019-06-04 14:49:57 +0100252static int adfs_dir_find_entry(struct adfs_dir *dir, u32 indaddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int pos, ret;
255
256 ret = -ENOENT;
257
258 for (pos = 5; pos < ADFS_NUM_DIR_ENTRIES * 26 + 5; pos += 26) {
259 struct object_info obj;
260
261 if (!__adfs_dir_get(dir, pos, &obj))
262 break;
263
Russell King5ed70bb2019-06-04 14:49:57 +0100264 if (obj.indaddr == indaddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 ret = pos;
266 break;
267 }
268 }
269
270 return ret;
271}
272
Russell King5ed70bb2019-06-04 14:49:57 +0100273static int adfs_f_read(struct super_block *sb, u32 indaddr, unsigned int size,
274 struct adfs_dir *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ret;
277
Russell King5ed70bb2019-06-04 14:49:57 +0100278 if (size != ADFS_NEWDIR_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 return -EIO;
280
Russell King5ed70bb2019-06-04 14:49:57 +0100281 ret = adfs_dir_read(sb, indaddr, size, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 if (ret)
283 adfs_error(sb, "unable to read directory");
284 else
Russell King016936b2019-12-09 11:10:21 +0000285 dir->parent_id = adfs_readval(dir->newtail->dirparent, 3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
287 return ret;
288}
289
290static int
291adfs_f_setpos(struct adfs_dir *dir, unsigned int fpos)
292{
293 if (fpos >= ADFS_NUM_DIR_ENTRIES)
294 return -ENOENT;
295
296 dir->pos = 5 + fpos * 26;
297 return 0;
298}
299
300static int
301adfs_f_getnext(struct adfs_dir *dir, struct object_info *obj)
302{
303 unsigned int ret;
304
305 ret = __adfs_dir_get(dir, dir->pos, obj);
306 if (ret == 0)
307 dir->pos += 26;
308
309 return ret;
310}
311
Russell King4287e4d2019-12-09 11:10:16 +0000312static int adfs_f_iterate(struct adfs_dir *dir, struct dir_context *ctx)
313{
314 struct object_info obj;
315 int pos = 5 + (ctx->pos - 2) * 26;
316
317 while (ctx->pos < 2 + ADFS_NUM_DIR_ENTRIES) {
318 if (__adfs_dir_get(dir, pos, &obj))
319 break;
320 if (!dir_emit(ctx, obj.name, obj.name_len,
321 obj.indaddr, DT_UNKNOWN))
322 break;
323 pos += 26;
324 ctx->pos++;
325 }
326 return 0;
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int
330adfs_f_update(struct adfs_dir *dir, struct object_info *obj)
331{
Russell Kingc3c81492019-12-09 11:09:45 +0000332 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Russell King5ed70bb2019-06-04 14:49:57 +0100334 ret = adfs_dir_find_entry(dir, obj->indaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (ret < 0) {
336 adfs_error(dir->sb, "unable to locate entry to update");
Russell Kingffc8df32019-12-09 11:10:27 +0000337 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339
340 __adfs_dir_put(dir, ret, obj);
341
342 /*
343 * Increment directory sequence number
344 */
Russell King016936b2019-12-09 11:10:21 +0000345 dir->dirhead->startmasseq += 1;
346 dir->newtail->endmasseq += 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 ret = adfs_dir_checkbyte(dir);
349 /*
350 * Update directory check byte
351 */
Russell King016936b2019-12-09 11:10:21 +0000352 dir->newtail->dircheckbyte = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Russell Kingffc8df32019-12-09 11:10:27 +0000354 ret = adfs_f_validate(dir);
355 if (ret)
356 adfs_error(dir->sb, "whoops! I broke a directory!");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Julia Lawall0125f502015-11-21 16:15:37 +0100361const struct adfs_dir_ops adfs_f_dir_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 .read = adfs_f_read,
Russell King4287e4d2019-12-09 11:10:16 +0000363 .iterate = adfs_f_iterate,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .setpos = adfs_f_setpos,
365 .getnext = adfs_f_getnext,
366 .update = adfs_f_update,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};