dir.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * QNX6 file system, Linux implementation.
  3. *
  4. * Version : 1.0.0
  5. *
  6. * History :
  7. *
  8. * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release.
  9. * 16-02-2012 pagemap extension by Al Viro
  10. *
  11. */
  12. #include "qnx6.h"
  13. static unsigned qnx6_lfile_checksum(char *name, unsigned size)
  14. {
  15. unsigned crc = 0;
  16. char *end = name + size;
  17. while (name < end) {
  18. crc = ((crc >> 1) + *(name++)) ^
  19. ((crc & 0x00000001) ? 0x80000000 : 0);
  20. }
  21. return crc;
  22. }
  23. static struct page *qnx6_get_page(struct inode *dir, unsigned long n)
  24. {
  25. struct address_space *mapping = dir->i_mapping;
  26. struct page *page = read_mapping_page(mapping, n, NULL);
  27. if (!IS_ERR(page))
  28. kmap(page);
  29. return page;
  30. }
  31. static unsigned last_entry(struct inode *inode, unsigned long page_nr)
  32. {
  33. unsigned long last_byte = inode->i_size;
  34. last_byte -= page_nr << PAGE_CACHE_SHIFT;
  35. if (last_byte > PAGE_CACHE_SIZE)
  36. last_byte = PAGE_CACHE_SIZE;
  37. return last_byte / QNX6_DIR_ENTRY_SIZE;
  38. }
  39. static struct qnx6_long_filename *qnx6_longname(struct super_block *sb,
  40. struct qnx6_long_dir_entry *de,
  41. struct page **p)
  42. {
  43. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  44. u32 s = fs32_to_cpu(sbi, de->de_long_inode); /* in block units */
  45. u32 n = s >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits); /* in pages */
  46. /* within page */
  47. u32 offs = (s << sb->s_blocksize_bits) & ~PAGE_CACHE_MASK;
  48. struct address_space *mapping = sbi->longfile->i_mapping;
  49. struct page *page = read_mapping_page(mapping, n, NULL);
  50. if (IS_ERR(page))
  51. return ERR_CAST(page);
  52. kmap(*p = page);
  53. return (struct qnx6_long_filename *)(page_address(page) + offs);
  54. }
  55. static int qnx6_dir_longfilename(struct inode *inode,
  56. struct qnx6_long_dir_entry *de,
  57. struct dir_context *ctx,
  58. unsigned de_inode)
  59. {
  60. struct qnx6_long_filename *lf;
  61. struct super_block *s = inode->i_sb;
  62. struct qnx6_sb_info *sbi = QNX6_SB(s);
  63. struct page *page;
  64. int lf_size;
  65. if (de->de_size != 0xff) {
  66. /* error - long filename entries always have size 0xff
  67. in direntry */
  68. pr_err("invalid direntry size (%i).\n", de->de_size);
  69. return 0;
  70. }
  71. lf = qnx6_longname(s, de, &page);
  72. if (IS_ERR(lf)) {
  73. pr_err("Error reading longname\n");
  74. return 0;
  75. }
  76. lf_size = fs16_to_cpu(sbi, lf->lf_size);
  77. if (lf_size > QNX6_LONG_NAME_MAX) {
  78. pr_debug("file %s\n", lf->lf_fname);
  79. pr_err("Filename too long (%i)\n", lf_size);
  80. qnx6_put_page(page);
  81. return 0;
  82. }
  83. /* calc & validate longfilename checksum
  84. mmi 3g filesystem does not have that checksum */
  85. if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
  86. qnx6_lfile_checksum(lf->lf_fname, lf_size))
  87. pr_info("long filename checksum error.\n");
  88. pr_debug("qnx6_readdir:%.*s inode:%u\n",
  89. lf_size, lf->lf_fname, de_inode);
  90. if (!dir_emit(ctx, lf->lf_fname, lf_size, de_inode, DT_UNKNOWN)) {
  91. qnx6_put_page(page);
  92. return 0;
  93. }
  94. qnx6_put_page(page);
  95. /* success */
  96. return 1;
  97. }
  98. static int qnx6_readdir(struct file *file, struct dir_context *ctx)
  99. {
  100. struct inode *inode = file_inode(file);
  101. struct super_block *s = inode->i_sb;
  102. struct qnx6_sb_info *sbi = QNX6_SB(s);
  103. loff_t pos = ctx->pos & ~(QNX6_DIR_ENTRY_SIZE - 1);
  104. unsigned long npages = dir_pages(inode);
  105. unsigned long n = pos >> PAGE_CACHE_SHIFT;
  106. unsigned start = (pos & ~PAGE_CACHE_MASK) / QNX6_DIR_ENTRY_SIZE;
  107. bool done = false;
  108. ctx->pos = pos;
  109. if (ctx->pos >= inode->i_size)
  110. return 0;
  111. for ( ; !done && n < npages; n++, start = 0) {
  112. struct page *page = qnx6_get_page(inode, n);
  113. int limit = last_entry(inode, n);
  114. struct qnx6_dir_entry *de;
  115. int i = start;
  116. if (IS_ERR(page)) {
  117. pr_err("%s(): read failed\n", __func__);
  118. ctx->pos = (n + 1) << PAGE_CACHE_SHIFT;
  119. return PTR_ERR(page);
  120. }
  121. de = ((struct qnx6_dir_entry *)page_address(page)) + start;
  122. for (; i < limit; i++, de++, ctx->pos += QNX6_DIR_ENTRY_SIZE) {
  123. int size = de->de_size;
  124. u32 no_inode = fs32_to_cpu(sbi, de->de_inode);
  125. if (!no_inode || !size)
  126. continue;
  127. if (size > QNX6_SHORT_NAME_MAX) {
  128. /* long filename detected
  129. get the filename from long filename
  130. structure / block */
  131. if (!qnx6_dir_longfilename(inode,
  132. (struct qnx6_long_dir_entry *)de,
  133. ctx, no_inode)) {
  134. done = true;
  135. break;
  136. }
  137. } else {
  138. pr_debug("%s():%.*s inode:%u\n",
  139. __func__, size, de->de_fname,
  140. no_inode);
  141. if (!dir_emit(ctx, de->de_fname, size,
  142. no_inode, DT_UNKNOWN)) {
  143. done = true;
  144. break;
  145. }
  146. }
  147. }
  148. qnx6_put_page(page);
  149. }
  150. return 0;
  151. }
  152. /*
  153. * check if the long filename is correct.
  154. */
  155. static unsigned qnx6_long_match(int len, const char *name,
  156. struct qnx6_long_dir_entry *de, struct inode *dir)
  157. {
  158. struct super_block *s = dir->i_sb;
  159. struct qnx6_sb_info *sbi = QNX6_SB(s);
  160. struct page *page;
  161. int thislen;
  162. struct qnx6_long_filename *lf = qnx6_longname(s, de, &page);
  163. if (IS_ERR(lf))
  164. return 0;
  165. thislen = fs16_to_cpu(sbi, lf->lf_size);
  166. if (len != thislen) {
  167. qnx6_put_page(page);
  168. return 0;
  169. }
  170. if (memcmp(name, lf->lf_fname, len) == 0) {
  171. qnx6_put_page(page);
  172. return fs32_to_cpu(sbi, de->de_inode);
  173. }
  174. qnx6_put_page(page);
  175. return 0;
  176. }
  177. /*
  178. * check if the filename is correct.
  179. */
  180. static unsigned qnx6_match(struct super_block *s, int len, const char *name,
  181. struct qnx6_dir_entry *de)
  182. {
  183. struct qnx6_sb_info *sbi = QNX6_SB(s);
  184. if (memcmp(name, de->de_fname, len) == 0)
  185. return fs32_to_cpu(sbi, de->de_inode);
  186. return 0;
  187. }
  188. unsigned qnx6_find_entry(int len, struct inode *dir, const char *name,
  189. struct page **res_page)
  190. {
  191. struct super_block *s = dir->i_sb;
  192. struct qnx6_inode_info *ei = QNX6_I(dir);
  193. struct page *page = NULL;
  194. unsigned long start, n;
  195. unsigned long npages = dir_pages(dir);
  196. unsigned ino;
  197. struct qnx6_dir_entry *de;
  198. struct qnx6_long_dir_entry *lde;
  199. *res_page = NULL;
  200. if (npages == 0)
  201. return 0;
  202. start = ei->i_dir_start_lookup;
  203. if (start >= npages)
  204. start = 0;
  205. n = start;
  206. do {
  207. page = qnx6_get_page(dir, n);
  208. if (!IS_ERR(page)) {
  209. int limit = last_entry(dir, n);
  210. int i;
  211. de = (struct qnx6_dir_entry *)page_address(page);
  212. for (i = 0; i < limit; i++, de++) {
  213. if (len <= QNX6_SHORT_NAME_MAX) {
  214. /* short filename */
  215. if (len != de->de_size)
  216. continue;
  217. ino = qnx6_match(s, len, name, de);
  218. if (ino)
  219. goto found;
  220. } else if (de->de_size == 0xff) {
  221. /* deal with long filename */
  222. lde = (struct qnx6_long_dir_entry *)de;
  223. ino = qnx6_long_match(len,
  224. name, lde, dir);
  225. if (ino)
  226. goto found;
  227. } else
  228. pr_err("undefined filename size in inode.\n");
  229. }
  230. qnx6_put_page(page);
  231. }
  232. if (++n >= npages)
  233. n = 0;
  234. } while (n != start);
  235. return 0;
  236. found:
  237. *res_page = page;
  238. ei->i_dir_start_lookup = n;
  239. return ino;
  240. }
  241. const struct file_operations qnx6_dir_operations = {
  242. .llseek = generic_file_llseek,
  243. .read = generic_read_dir,
  244. .iterate = qnx6_readdir,
  245. .fsync = generic_file_fsync,
  246. };
  247. const struct inode_operations qnx6_dir_inode_operations = {
  248. .lookup = qnx6_lookup,
  249. };