dir_fplus.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * linux/fs/adfs/dir_fplus.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. #include <linux/buffer_head.h>
  11. #include <linux/slab.h>
  12. #include "adfs.h"
  13. #include "dir_fplus.h"
  14. static int
  15. adfs_fplus_read(struct super_block *sb, unsigned int id, unsigned int sz, struct adfs_dir *dir)
  16. {
  17. struct adfs_bigdirheader *h;
  18. struct adfs_bigdirtail *t;
  19. unsigned long block;
  20. unsigned int blk, size;
  21. int i, ret = -EIO;
  22. dir->nr_buffers = 0;
  23. /* start off using fixed bh set - only alloc for big dirs */
  24. dir->bh_fplus = &dir->bh[0];
  25. block = __adfs_block_map(sb, id, 0);
  26. if (!block) {
  27. adfs_error(sb, "dir object %X has a hole at offset 0", id);
  28. goto out;
  29. }
  30. dir->bh_fplus[0] = sb_bread(sb, block);
  31. if (!dir->bh_fplus[0])
  32. goto out;
  33. dir->nr_buffers += 1;
  34. h = (struct adfs_bigdirheader *)dir->bh_fplus[0]->b_data;
  35. size = le32_to_cpu(h->bigdirsize);
  36. if (size != sz) {
  37. printk(KERN_WARNING "adfs: adfs_fplus_read:"
  38. " directory header size %X\n"
  39. " does not match directory size %X\n",
  40. size, sz);
  41. }
  42. if (h->bigdirversion[0] != 0 || h->bigdirversion[1] != 0 ||
  43. h->bigdirversion[2] != 0 || size & 2047 ||
  44. h->bigdirstartname != cpu_to_le32(BIGDIRSTARTNAME)) {
  45. printk(KERN_WARNING "adfs: dir object %X has"
  46. " malformed dir header\n", id);
  47. goto out;
  48. }
  49. size >>= sb->s_blocksize_bits;
  50. if (size > ARRAY_SIZE(dir->bh)) {
  51. /* this directory is too big for fixed bh set, must allocate */
  52. struct buffer_head **bh_fplus =
  53. kcalloc(size, sizeof(struct buffer_head *),
  54. GFP_KERNEL);
  55. if (!bh_fplus) {
  56. ret = -ENOMEM;
  57. adfs_error(sb, "not enough memory for"
  58. " dir object %X (%d blocks)", id, size);
  59. goto out;
  60. }
  61. dir->bh_fplus = bh_fplus;
  62. /* copy over the pointer to the block that we've already read */
  63. dir->bh_fplus[0] = dir->bh[0];
  64. }
  65. for (blk = 1; blk < size; blk++) {
  66. block = __adfs_block_map(sb, id, blk);
  67. if (!block) {
  68. adfs_error(sb, "dir object %X has a hole at offset %d", id, blk);
  69. goto out;
  70. }
  71. dir->bh_fplus[blk] = sb_bread(sb, block);
  72. if (!dir->bh_fplus[blk]) {
  73. adfs_error(sb, "dir object %x failed read for offset %d, mapped block %lX",
  74. id, blk, block);
  75. goto out;
  76. }
  77. dir->nr_buffers += 1;
  78. }
  79. t = (struct adfs_bigdirtail *)
  80. (dir->bh_fplus[size - 1]->b_data + (sb->s_blocksize - 8));
  81. if (t->bigdirendname != cpu_to_le32(BIGDIRENDNAME) ||
  82. t->bigdirendmasseq != h->startmasseq ||
  83. t->reserved[0] != 0 || t->reserved[1] != 0) {
  84. printk(KERN_WARNING "adfs: dir object %X has "
  85. "malformed dir end\n", id);
  86. goto out;
  87. }
  88. dir->parent_id = le32_to_cpu(h->bigdirparent);
  89. dir->sb = sb;
  90. return 0;
  91. out:
  92. if (dir->bh_fplus) {
  93. for (i = 0; i < dir->nr_buffers; i++)
  94. brelse(dir->bh_fplus[i]);
  95. if (&dir->bh[0] != dir->bh_fplus)
  96. kfree(dir->bh_fplus);
  97. dir->bh_fplus = NULL;
  98. }
  99. dir->nr_buffers = 0;
  100. dir->sb = NULL;
  101. return ret;
  102. }
  103. static int
  104. adfs_fplus_setpos(struct adfs_dir *dir, unsigned int fpos)
  105. {
  106. struct adfs_bigdirheader *h =
  107. (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
  108. int ret = -ENOENT;
  109. if (fpos <= le32_to_cpu(h->bigdirentries)) {
  110. dir->pos = fpos;
  111. ret = 0;
  112. }
  113. return ret;
  114. }
  115. static void
  116. dir_memcpy(struct adfs_dir *dir, unsigned int offset, void *to, int len)
  117. {
  118. struct super_block *sb = dir->sb;
  119. unsigned int buffer, partial, remainder;
  120. buffer = offset >> sb->s_blocksize_bits;
  121. offset &= sb->s_blocksize - 1;
  122. partial = sb->s_blocksize - offset;
  123. if (partial >= len)
  124. memcpy(to, dir->bh_fplus[buffer]->b_data + offset, len);
  125. else {
  126. char *c = (char *)to;
  127. remainder = len - partial;
  128. memcpy(c,
  129. dir->bh_fplus[buffer]->b_data + offset,
  130. partial);
  131. memcpy(c + partial,
  132. dir->bh_fplus[buffer + 1]->b_data,
  133. remainder);
  134. }
  135. }
  136. static int
  137. adfs_fplus_getnext(struct adfs_dir *dir, struct object_info *obj)
  138. {
  139. struct adfs_bigdirheader *h =
  140. (struct adfs_bigdirheader *) dir->bh_fplus[0]->b_data;
  141. struct adfs_bigdirentry bde;
  142. unsigned int offset;
  143. int i, ret = -ENOENT;
  144. if (dir->pos >= le32_to_cpu(h->bigdirentries))
  145. goto out;
  146. offset = offsetof(struct adfs_bigdirheader, bigdirname);
  147. offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
  148. offset += dir->pos * sizeof(struct adfs_bigdirentry);
  149. dir_memcpy(dir, offset, &bde, sizeof(struct adfs_bigdirentry));
  150. obj->loadaddr = le32_to_cpu(bde.bigdirload);
  151. obj->execaddr = le32_to_cpu(bde.bigdirexec);
  152. obj->size = le32_to_cpu(bde.bigdirlen);
  153. obj->file_id = le32_to_cpu(bde.bigdirindaddr);
  154. obj->attr = le32_to_cpu(bde.bigdirattr);
  155. obj->name_len = le32_to_cpu(bde.bigdirobnamelen);
  156. offset = offsetof(struct adfs_bigdirheader, bigdirname);
  157. offset += ((le32_to_cpu(h->bigdirnamelen) + 4) & ~3);
  158. offset += le32_to_cpu(h->bigdirentries) * sizeof(struct adfs_bigdirentry);
  159. offset += le32_to_cpu(bde.bigdirobnameptr);
  160. dir_memcpy(dir, offset, obj->name, obj->name_len);
  161. for (i = 0; i < obj->name_len; i++)
  162. if (obj->name[i] == '/')
  163. obj->name[i] = '.';
  164. obj->filetype = -1;
  165. /*
  166. * object is a file and is filetyped and timestamped?
  167. * RISC OS 12-bit filetype is stored in load_address[19:8]
  168. */
  169. if ((0 == (obj->attr & ADFS_NDA_DIRECTORY)) &&
  170. (0xfff00000 == (0xfff00000 & obj->loadaddr))) {
  171. obj->filetype = (__u16) ((0x000fff00 & obj->loadaddr) >> 8);
  172. /* optionally append the ,xyz hex filetype suffix */
  173. if (ADFS_SB(dir->sb)->s_ftsuffix)
  174. obj->name_len +=
  175. append_filetype_suffix(
  176. &obj->name[obj->name_len],
  177. obj->filetype);
  178. }
  179. dir->pos += 1;
  180. ret = 0;
  181. out:
  182. return ret;
  183. }
  184. static int
  185. adfs_fplus_sync(struct adfs_dir *dir)
  186. {
  187. int err = 0;
  188. int i;
  189. for (i = dir->nr_buffers - 1; i >= 0; i--) {
  190. struct buffer_head *bh = dir->bh_fplus[i];
  191. sync_dirty_buffer(bh);
  192. if (buffer_req(bh) && !buffer_uptodate(bh))
  193. err = -EIO;
  194. }
  195. return err;
  196. }
  197. static void
  198. adfs_fplus_free(struct adfs_dir *dir)
  199. {
  200. int i;
  201. if (dir->bh_fplus) {
  202. for (i = 0; i < dir->nr_buffers; i++)
  203. brelse(dir->bh_fplus[i]);
  204. if (&dir->bh[0] != dir->bh_fplus)
  205. kfree(dir->bh_fplus);
  206. dir->bh_fplus = NULL;
  207. }
  208. dir->nr_buffers = 0;
  209. dir->sb = NULL;
  210. }
  211. struct adfs_dir_ops adfs_fplus_dir_ops = {
  212. .read = adfs_fplus_read,
  213. .setpos = adfs_fplus_setpos,
  214. .getnext = adfs_fplus_getnext,
  215. .sync = adfs_fplus_sync,
  216. .free = adfs_fplus_free
  217. };