dir_f.c 10 KB

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