dir.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. void *dirent, loff_t pos,
  62. unsigned de_inode, filldir_t filldir)
  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. printk(KERN_ERR "qnx6: invalid direntry size (%i).\n",
  73. de->de_size);
  74. return 0;
  75. }
  76. lf = qnx6_longname(s, de, &page);
  77. if (IS_ERR(lf)) {
  78. printk(KERN_ERR "qnx6:Error reading longname\n");
  79. return 0;
  80. }
  81. lf_size = fs16_to_cpu(sbi, lf->lf_size);
  82. if (lf_size > QNX6_LONG_NAME_MAX) {
  83. QNX6DEBUG((KERN_INFO "file %s\n", lf->lf_fname));
  84. printk(KERN_ERR "qnx6:Filename too long (%i)\n", lf_size);
  85. qnx6_put_page(page);
  86. return 0;
  87. }
  88. /* calc & validate longfilename checksum
  89. mmi 3g filesystem does not have that checksum */
  90. if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
  91. qnx6_lfile_checksum(lf->lf_fname, lf_size))
  92. printk(KERN_INFO "qnx6: long filename checksum error.\n");
  93. QNX6DEBUG((KERN_INFO "qnx6_readdir:%.*s inode:%u\n",
  94. lf_size, lf->lf_fname, de_inode));
  95. if (filldir(dirent, lf->lf_fname, lf_size, pos, de_inode,
  96. DT_UNKNOWN) < 0) {
  97. qnx6_put_page(page);
  98. return 0;
  99. }
  100. qnx6_put_page(page);
  101. /* success */
  102. return 1;
  103. }
  104. static int qnx6_readdir(struct file *filp, void *dirent, filldir_t filldir)
  105. {
  106. struct inode *inode = filp->f_path.dentry->d_inode;
  107. struct super_block *s = inode->i_sb;
  108. struct qnx6_sb_info *sbi = QNX6_SB(s);
  109. loff_t pos = filp->f_pos & (QNX6_DIR_ENTRY_SIZE - 1);
  110. unsigned long npages = dir_pages(inode);
  111. unsigned long n = pos >> PAGE_CACHE_SHIFT;
  112. unsigned start = (pos & ~PAGE_CACHE_MASK) / QNX6_DIR_ENTRY_SIZE;
  113. bool done = false;
  114. if (filp->f_pos >= inode->i_size)
  115. return 0;
  116. for ( ; !done && n < npages; n++, start = 0) {
  117. struct page *page = qnx6_get_page(inode, n);
  118. int limit = last_entry(inode, n);
  119. struct qnx6_dir_entry *de;
  120. int i = start;
  121. if (IS_ERR(page)) {
  122. printk(KERN_ERR "qnx6_readdir: read failed\n");
  123. filp->f_pos = (n + 1) << PAGE_CACHE_SHIFT;
  124. return PTR_ERR(page);
  125. }
  126. de = ((struct qnx6_dir_entry *)page_address(page)) + start;
  127. for (; i < limit; i++, de++, pos += QNX6_DIR_ENTRY_SIZE) {
  128. int size = de->de_size;
  129. u32 no_inode = fs32_to_cpu(sbi, de->de_inode);
  130. if (!no_inode || !size)
  131. continue;
  132. if (size > QNX6_SHORT_NAME_MAX) {
  133. /* long filename detected
  134. get the filename from long filename
  135. structure / block */
  136. if (!qnx6_dir_longfilename(inode,
  137. (struct qnx6_long_dir_entry *)de,
  138. dirent, pos, no_inode,
  139. filldir)) {
  140. done = true;
  141. break;
  142. }
  143. } else {
  144. QNX6DEBUG((KERN_INFO "qnx6_readdir:%.*s"
  145. " inode:%u\n", size, de->de_fname,
  146. no_inode));
  147. if (filldir(dirent, de->de_fname, size,
  148. pos, no_inode, DT_UNKNOWN)
  149. < 0) {
  150. done = true;
  151. break;
  152. }
  153. }
  154. }
  155. qnx6_put_page(page);
  156. }
  157. filp->f_pos = pos;
  158. return 0;
  159. }
  160. /*
  161. * check if the long filename is correct.
  162. */
  163. static unsigned qnx6_long_match(int len, const char *name,
  164. struct qnx6_long_dir_entry *de, struct inode *dir)
  165. {
  166. struct super_block *s = dir->i_sb;
  167. struct qnx6_sb_info *sbi = QNX6_SB(s);
  168. struct page *page;
  169. int thislen;
  170. struct qnx6_long_filename *lf = qnx6_longname(s, de, &page);
  171. if (IS_ERR(lf))
  172. return 0;
  173. thislen = fs16_to_cpu(sbi, lf->lf_size);
  174. if (len != thislen) {
  175. qnx6_put_page(page);
  176. return 0;
  177. }
  178. if (memcmp(name, lf->lf_fname, len) == 0) {
  179. qnx6_put_page(page);
  180. return fs32_to_cpu(sbi, de->de_inode);
  181. }
  182. qnx6_put_page(page);
  183. return 0;
  184. }
  185. /*
  186. * check if the filename is correct.
  187. */
  188. static unsigned qnx6_match(struct super_block *s, int len, const char *name,
  189. struct qnx6_dir_entry *de)
  190. {
  191. struct qnx6_sb_info *sbi = QNX6_SB(s);
  192. if (memcmp(name, de->de_fname, len) == 0)
  193. return fs32_to_cpu(sbi, de->de_inode);
  194. return 0;
  195. }
  196. unsigned qnx6_find_entry(int len, struct inode *dir, const char *name,
  197. struct page **res_page)
  198. {
  199. struct super_block *s = dir->i_sb;
  200. struct qnx6_inode_info *ei = QNX6_I(dir);
  201. struct page *page = NULL;
  202. unsigned long start, n;
  203. unsigned long npages = dir_pages(dir);
  204. unsigned ino;
  205. struct qnx6_dir_entry *de;
  206. struct qnx6_long_dir_entry *lde;
  207. *res_page = NULL;
  208. if (npages == 0)
  209. return 0;
  210. start = ei->i_dir_start_lookup;
  211. if (start >= npages)
  212. start = 0;
  213. n = start;
  214. do {
  215. page = qnx6_get_page(dir, n);
  216. if (!IS_ERR(page)) {
  217. int limit = last_entry(dir, n);
  218. int i;
  219. de = (struct qnx6_dir_entry *)page_address(page);
  220. for (i = 0; i < limit; i++, de++) {
  221. if (len <= QNX6_SHORT_NAME_MAX) {
  222. /* short filename */
  223. if (len != de->de_size)
  224. continue;
  225. ino = qnx6_match(s, len, name, de);
  226. if (ino)
  227. goto found;
  228. } else if (de->de_size == 0xff) {
  229. /* deal with long filename */
  230. lde = (struct qnx6_long_dir_entry *)de;
  231. ino = qnx6_long_match(len,
  232. name, lde, dir);
  233. if (ino)
  234. goto found;
  235. } else
  236. printk(KERN_ERR "qnx6: undefined "
  237. "filename size in inode.\n");
  238. }
  239. qnx6_put_page(page);
  240. }
  241. if (++n >= npages)
  242. n = 0;
  243. } while (n != start);
  244. return 0;
  245. found:
  246. *res_page = page;
  247. ei->i_dir_start_lookup = n;
  248. return ino;
  249. }
  250. const struct file_operations qnx6_dir_operations = {
  251. .llseek = generic_file_llseek,
  252. .read = generic_read_dir,
  253. .readdir = qnx6_readdir,
  254. .fsync = generic_file_fsync,
  255. };
  256. const struct inode_operations qnx6_dir_inode_operations = {
  257. .lookup = qnx6_lookup,
  258. };