dir.c 6.7 KB

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