dir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 inline unsigned long dir_pages(struct inode *inode)
  32. {
  33. return (inode->i_size+PAGE_CACHE_SIZE-1)>>PAGE_CACHE_SHIFT;
  34. }
  35. static unsigned last_entry(struct inode *inode, unsigned long page_nr)
  36. {
  37. unsigned long last_byte = inode->i_size;
  38. last_byte -= page_nr << PAGE_CACHE_SHIFT;
  39. if (last_byte > PAGE_CACHE_SIZE)
  40. last_byte = PAGE_CACHE_SIZE;
  41. return last_byte / QNX6_DIR_ENTRY_SIZE;
  42. }
  43. static struct qnx6_long_filename *qnx6_longname(struct super_block *sb,
  44. struct qnx6_long_dir_entry *de,
  45. struct page **p)
  46. {
  47. struct qnx6_sb_info *sbi = QNX6_SB(sb);
  48. u32 s = fs32_to_cpu(sbi, de->de_long_inode); /* in block units */
  49. u32 n = s >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits); /* in pages */
  50. /* within page */
  51. u32 offs = (s << sb->s_blocksize_bits) & ~PAGE_CACHE_MASK;
  52. struct address_space *mapping = sbi->longfile->i_mapping;
  53. struct page *page = read_mapping_page(mapping, n, NULL);
  54. if (IS_ERR(page))
  55. return ERR_CAST(page);
  56. kmap(*p = page);
  57. return (struct qnx6_long_filename *)(page_address(page) + offs);
  58. }
  59. static int qnx6_dir_longfilename(struct inode *inode,
  60. struct qnx6_long_dir_entry *de,
  61. struct dir_context *ctx,
  62. unsigned de_inode)
  63. {
  64. struct qnx6_long_filename *lf;
  65. struct super_block *s = inode->i_sb;
  66. struct qnx6_sb_info *sbi = QNX6_SB(s);
  67. struct page *page;
  68. int lf_size;
  69. if (de->de_size != 0xff) {
  70. /* error - long filename entries always have size 0xff
  71. in direntry */
  72. pr_err("invalid direntry size (%i).\n", de->de_size);
  73. return 0;
  74. }
  75. lf = qnx6_longname(s, de, &page);
  76. if (IS_ERR(lf)) {
  77. pr_err("Error reading longname\n");
  78. return 0;
  79. }
  80. lf_size = fs16_to_cpu(sbi, lf->lf_size);
  81. if (lf_size > QNX6_LONG_NAME_MAX) {
  82. pr_debug("file %s\n", lf->lf_fname);
  83. pr_err("Filename too long (%i)\n", lf_size);
  84. qnx6_put_page(page);
  85. return 0;
  86. }
  87. /* calc & validate longfilename checksum
  88. mmi 3g filesystem does not have that checksum */
  89. if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
  90. qnx6_lfile_checksum(lf->lf_fname, lf_size))
  91. pr_info("long filename checksum error.\n");
  92. pr_debug("qnx6_readdir:%.*s inode:%u\n",
  93. lf_size, lf->lf_fname, de_inode);
  94. if (!dir_emit(ctx, lf->lf_fname, lf_size, de_inode, DT_UNKNOWN)) {
  95. qnx6_put_page(page);
  96. return 0;
  97. }
  98. qnx6_put_page(page);
  99. /* success */
  100. return 1;
  101. }
  102. static int qnx6_readdir(struct file *file, struct dir_context *ctx)
  103. {
  104. struct inode *inode = file_inode(file);
  105. struct super_block *s = inode->i_sb;
  106. struct qnx6_sb_info *sbi = QNX6_SB(s);
  107. loff_t pos = ctx->pos & ~(QNX6_DIR_ENTRY_SIZE - 1);
  108. unsigned long npages = dir_pages(inode);
  109. unsigned long n = pos >> PAGE_CACHE_SHIFT;
  110. unsigned start = (pos & ~PAGE_CACHE_MASK) / QNX6_DIR_ENTRY_SIZE;
  111. bool done = false;
  112. ctx->pos = pos;
  113. if (ctx->pos >= inode->i_size)
  114. return 0;
  115. for ( ; !done && n < npages; n++, start = 0) {
  116. struct page *page = qnx6_get_page(inode, n);
  117. int limit = last_entry(inode, n);
  118. struct qnx6_dir_entry *de;
  119. int i = start;
  120. if (IS_ERR(page)) {
  121. pr_err("%s(): read failed\n", __func__);
  122. ctx->pos = (n + 1) << PAGE_CACHE_SHIFT;
  123. return PTR_ERR(page);
  124. }
  125. de = ((struct qnx6_dir_entry *)page_address(page)) + start;
  126. for (; i < limit; i++, 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. qnx6_put_page(page);
  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 page *page;
  165. int thislen;
  166. struct qnx6_long_filename *lf = qnx6_longname(s, de, &page);
  167. if (IS_ERR(lf))
  168. return 0;
  169. thislen = fs16_to_cpu(sbi, lf->lf_size);
  170. if (len != thislen) {
  171. qnx6_put_page(page);
  172. return 0;
  173. }
  174. if (memcmp(name, lf->lf_fname, len) == 0) {
  175. qnx6_put_page(page);
  176. return fs32_to_cpu(sbi, de->de_inode);
  177. }
  178. qnx6_put_page(page);
  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_entry(int len, struct inode *dir, const char *name,
  193. struct page **res_page)
  194. {
  195. struct super_block *s = dir->i_sb;
  196. struct qnx6_inode_info *ei = QNX6_I(dir);
  197. struct page *page = NULL;
  198. unsigned long start, n;
  199. unsigned long npages = dir_pages(dir);
  200. unsigned ino;
  201. struct qnx6_dir_entry *de;
  202. struct qnx6_long_dir_entry *lde;
  203. *res_page = NULL;
  204. if (npages == 0)
  205. return 0;
  206. start = ei->i_dir_start_lookup;
  207. if (start >= npages)
  208. start = 0;
  209. n = start;
  210. do {
  211. page = qnx6_get_page(dir, n);
  212. if (!IS_ERR(page)) {
  213. int limit = last_entry(dir, n);
  214. int i;
  215. de = (struct qnx6_dir_entry *)page_address(page);
  216. for (i = 0; i < limit; i++, de++) {
  217. if (len <= QNX6_SHORT_NAME_MAX) {
  218. /* short filename */
  219. if (len != de->de_size)
  220. continue;
  221. ino = qnx6_match(s, len, name, de);
  222. if (ino)
  223. goto found;
  224. } else if (de->de_size == 0xff) {
  225. /* deal with long filename */
  226. lde = (struct qnx6_long_dir_entry *)de;
  227. ino = qnx6_long_match(len,
  228. name, lde, dir);
  229. if (ino)
  230. goto found;
  231. } else
  232. pr_err("undefined filename size in inode.\n");
  233. }
  234. qnx6_put_page(page);
  235. }
  236. if (++n >= npages)
  237. n = 0;
  238. } while (n != start);
  239. return 0;
  240. found:
  241. *res_page = page;
  242. ei->i_dir_start_lookup = n;
  243. return ino;
  244. }
  245. const struct file_operations qnx6_dir_operations = {
  246. .llseek = generic_file_llseek,
  247. .read = generic_read_dir,
  248. .iterate = qnx6_readdir,
  249. .fsync = generic_file_fsync,
  250. };
  251. const struct inode_operations qnx6_dir_inode_operations = {
  252. .lookup = qnx6_lookup,
  253. };