dir.c 6.8 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 void *qnx6_get_folio(struct inode *dir, unsigned long n,
  25. struct folio **foliop)
  26. {
  27. struct folio *folio = read_mapping_folio(dir->i_mapping, n, NULL);
  28. if (IS_ERR(folio))
  29. return folio;
  30. *foliop = folio;
  31. return kmap_local_folio(folio, 0);
  32. }
  33. static unsigned last_entry(struct inode *inode, unsigned long page_nr)
  34. {
  35. unsigned long last_byte = inode->i_size;
  36. last_byte -= page_nr << PAGE_SHIFT;
  37. if (last_byte > PAGE_SIZE)
  38. last_byte = PAGE_SIZE;
  39. return last_byte / QNX6_DIR_ENTRY_SIZE;
  40. }
  41. static struct qnx6_long_filename *qnx6_longname(struct super_block *sb,
  42. struct qnx6_long_dir_entry *de,
  43. struct folio **foliop)
  44. {
  45. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  46. u32 s = fs32_to_cpu(sbi, de->de_long_inode); /* in block units */
  47. u32 n = s >> (PAGE_SHIFT - sb->s_blocksize_bits); /* in pages */
  48. u32 offs;
  49. struct address_space *mapping = sbi->longfile->i_mapping;
  50. struct folio *folio = read_mapping_folio(mapping, n, NULL);
  51. if (IS_ERR(folio))
  52. return ERR_CAST(folio);
  53. offs = offset_in_folio(folio, s << sb->s_blocksize_bits);
  54. *foliop = folio;
  55. return kmap_local_folio(folio, offs);
  56. }
  57. static int qnx6_dir_longfilename(struct inode *inode,
  58. struct qnx6_long_dir_entry *de,
  59. struct dir_context *ctx,
  60. unsigned de_inode)
  61. {
  62. struct qnx6_long_filename *lf;
  63. struct super_block *s = inode->i_sb;
  64. struct qnx6_sb_info *sbi = QNX6_SB(s);
  65. struct folio *folio;
  66. int lf_size;
  67. if (de->de_size != 0xff) {
  68. /* error - long filename entries always have size 0xff
  69. in direntry */
  70. pr_err("invalid direntry size (%i).\n", de->de_size);
  71. return 0;
  72. }
  73. lf = qnx6_longname(s, de, &folio);
  74. if (IS_ERR(lf)) {
  75. pr_err("Error reading longname\n");
  76. return 0;
  77. }
  78. lf_size = fs16_to_cpu(sbi, lf->lf_size);
  79. if (lf_size > QNX6_LONG_NAME_MAX) {
  80. pr_debug("file %s\n", lf->lf_fname);
  81. pr_err("Filename too long (%i)\n", lf_size);
  82. folio_release_kmap(folio, lf);
  83. return 0;
  84. }
  85. /* calc & validate longfilename checksum
  86. mmi 3g filesystem does not have that checksum */
  87. if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
  88. qnx6_lfile_checksum(lf->lf_fname, lf_size))
  89. pr_info("long filename checksum error.\n");
  90. pr_debug("qnx6_readdir:%.*s inode:%u\n",
  91. lf_size, lf->lf_fname, de_inode);
  92. if (!dir_emit(ctx, lf->lf_fname, lf_size, de_inode, DT_UNKNOWN)) {
  93. folio_release_kmap(folio, lf);
  94. return 0;
  95. }
  96. folio_release_kmap(folio, lf);
  97. /* success */
  98. return 1;
  99. }
  100. static int qnx6_readdir(struct file *file, struct dir_context *ctx)
  101. {
  102. struct inode *inode = file_inode(file);
  103. struct super_block *s = inode->i_sb;
  104. struct qnx6_sb_info *sbi = QNX6_SB(s);
  105. loff_t pos = ctx->pos & ~(QNX6_DIR_ENTRY_SIZE - 1);
  106. unsigned long npages = dir_pages(inode);
  107. unsigned long n = pos >> PAGE_SHIFT;
  108. unsigned offset = (pos & ~PAGE_MASK) / QNX6_DIR_ENTRY_SIZE;
  109. bool done = false;
  110. ctx->pos = pos;
  111. if (ctx->pos >= inode->i_size)
  112. return 0;
  113. for ( ; !done && n < npages; n++, offset = 0) {
  114. struct qnx6_dir_entry *de;
  115. struct folio *folio;
  116. char *kaddr = qnx6_get_folio(inode, n, &folio);
  117. char *limit;
  118. if (IS_ERR(kaddr)) {
  119. pr_err("%s(): read failed\n", __func__);
  120. ctx->pos = (n + 1) << PAGE_SHIFT;
  121. return PTR_ERR(kaddr);
  122. }
  123. de = (struct qnx6_dir_entry *)(kaddr + offset);
  124. limit = kaddr + last_entry(inode, n);
  125. for (; (char *)de < limit; de++, ctx->pos += QNX6_DIR_ENTRY_SIZE) {
  126. int size = de->de_size;
  127. u32 no_inode = fs32_to_cpu(sbi, de->de_inode);
  128. if (!no_inode || !size)
  129. continue;
  130. if (size > QNX6_SHORT_NAME_MAX) {
  131. /* long filename detected
  132. get the filename from long filename
  133. structure / block */
  134. if (!qnx6_dir_longfilename(inode,
  135. (struct qnx6_long_dir_entry *)de,
  136. ctx, no_inode)) {
  137. done = true;
  138. break;
  139. }
  140. } else {
  141. pr_debug("%s():%.*s inode:%u\n",
  142. __func__, size, de->de_fname,
  143. no_inode);
  144. if (!dir_emit(ctx, de->de_fname, size,
  145. no_inode, DT_UNKNOWN)) {
  146. done = true;
  147. break;
  148. }
  149. }
  150. }
  151. folio_release_kmap(folio, kaddr);
  152. }
  153. return 0;
  154. }
  155. /*
  156. * check if the long filename is correct.
  157. */
  158. static unsigned qnx6_long_match(int len, const char *name,
  159. struct qnx6_long_dir_entry *de, struct inode *dir)
  160. {
  161. struct super_block *s = dir->i_sb;
  162. struct qnx6_sb_info *sbi = QNX6_SB(s);
  163. struct folio *folio;
  164. int thislen;
  165. struct qnx6_long_filename *lf = qnx6_longname(s, de, &folio);
  166. if (IS_ERR(lf))
  167. return 0;
  168. thislen = fs16_to_cpu(sbi, lf->lf_size);
  169. if (len != thislen) {
  170. folio_release_kmap(folio, lf);
  171. return 0;
  172. }
  173. if (memcmp(name, lf->lf_fname, len) == 0) {
  174. folio_release_kmap(folio, lf);
  175. return fs32_to_cpu(sbi, de->de_inode);
  176. }
  177. folio_release_kmap(folio, lf);
  178. return 0;
  179. }
  180. /*
  181. * check if the filename is correct.
  182. */
  183. static unsigned qnx6_match(struct super_block *s, int len, const char *name,
  184. struct qnx6_dir_entry *de)
  185. {
  186. struct qnx6_sb_info *sbi = QNX6_SB(s);
  187. if (memcmp(name, de->de_fname, len) == 0)
  188. return fs32_to_cpu(sbi, de->de_inode);
  189. return 0;
  190. }
  191. unsigned qnx6_find_ino(int len, struct inode *dir, const char *name)
  192. {
  193. struct super_block *s = dir->i_sb;
  194. struct qnx6_inode_info *ei = QNX6_I(dir);
  195. struct folio *folio;
  196. unsigned long start, n;
  197. unsigned long npages = dir_pages(dir);
  198. unsigned ino;
  199. struct qnx6_dir_entry *de;
  200. struct qnx6_long_dir_entry *lde;
  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. de = qnx6_get_folio(dir, n, &folio);
  209. if (!IS_ERR(de)) {
  210. int limit = last_entry(dir, n);
  211. int i;
  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. folio_release_kmap(folio, de - i);
  231. }
  232. if (++n >= npages)
  233. n = 0;
  234. } while (n != start);
  235. return 0;
  236. found:
  237. ei->i_dir_start_lookup = n;
  238. folio_release_kmap(folio, de);
  239. return ino;
  240. }
  241. const struct file_operations qnx6_dir_operations = {
  242. .llseek = generic_file_llseek,
  243. .read = generic_read_dir,
  244. .iterate_shared = qnx6_readdir,
  245. .fsync = generic_file_fsync,
  246. };
  247. const struct inode_operations qnx6_dir_inode_operations = {
  248. .lookup = qnx6_lookup,
  249. };