dir.c 6.8 KB

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