2 * QNX6 file system, Linux implementation.
8 * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release.
9 * 16-02-2012 pagemap extension by Al Viro
15 static unsigned qnx6_lfile_checksum(char *name, unsigned size)
18 char *end = name + size;
20 crc = ((crc >> 1) + *(name++)) ^
21 ((crc & 0x00000001) ? 0x80000000 : 0);
26 static struct page *qnx6_get_page(struct inode *dir, unsigned long n)
28 struct address_space *mapping = dir->i_mapping;
29 struct page *page = read_mapping_page(mapping, n, NULL);
35 static unsigned last_entry(struct inode *inode, unsigned long page_nr)
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;
44 static struct qnx6_long_filename *qnx6_longname(struct super_block *sb,
45 struct qnx6_long_dir_entry *de,
48 struct qnx6_sb_info *sbi = QNX6_SB(sb);
49 u32 s = fs32_to_cpu(sbi, de->de_long_inode); /* in block units */
50 u32 n = s >> (PAGE_CACHE_SHIFT - sb->s_blocksize_bits); /* in pages */
52 u32 offs = (s << sb->s_blocksize_bits) & ~PAGE_CACHE_MASK;
53 struct address_space *mapping = sbi->longfile->i_mapping;
54 struct page *page = read_mapping_page(mapping, n, NULL);
56 return ERR_CAST(page);
58 return (struct qnx6_long_filename *)(page_address(page) + offs);
61 static int qnx6_dir_longfilename(struct inode *inode,
62 struct qnx6_long_dir_entry *de,
63 struct dir_context *ctx,
66 struct qnx6_long_filename *lf;
67 struct super_block *s = inode->i_sb;
68 struct qnx6_sb_info *sbi = QNX6_SB(s);
72 if (de->de_size != 0xff) {
73 /* error - long filename entries always have size 0xff
75 pr_err("invalid direntry size (%i).\n", de->de_size);
78 lf = qnx6_longname(s, de, &page);
80 pr_err("Error reading longname\n");
84 lf_size = fs16_to_cpu(sbi, lf->lf_size);
86 if (lf_size > QNX6_LONG_NAME_MAX) {
87 pr_debug("file %s\n", lf->lf_fname);
88 pr_err("Filename too long (%i)\n", lf_size);
93 /* calc & validate longfilename checksum
94 mmi 3g filesystem does not have that checksum */
95 if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
96 qnx6_lfile_checksum(lf->lf_fname, lf_size))
97 pr_info("long filename checksum error.\n");
99 pr_debug("qnx6_readdir:%.*s inode:%u\n",
100 lf_size, lf->lf_fname, de_inode);
101 if (!dir_emit(ctx, lf->lf_fname, lf_size, de_inode, DT_UNKNOWN)) {
111 static int qnx6_readdir(struct file *file, struct dir_context *ctx)
113 struct inode *inode = file_inode(file);
114 struct super_block *s = inode->i_sb;
115 struct qnx6_sb_info *sbi = QNX6_SB(s);
116 loff_t pos = ctx->pos & ~(QNX6_DIR_ENTRY_SIZE - 1);
117 unsigned long npages = dir_pages(inode);
118 unsigned long n = pos >> PAGE_CACHE_SHIFT;
119 unsigned start = (pos & ~PAGE_CACHE_MASK) / QNX6_DIR_ENTRY_SIZE;
123 if (ctx->pos >= inode->i_size)
126 for ( ; !done && n < npages; n++, start = 0) {
127 struct page *page = qnx6_get_page(inode, n);
128 int limit = last_entry(inode, n);
129 struct qnx6_dir_entry *de;
133 pr_err("%s(): read failed\n", __func__);
134 ctx->pos = (n + 1) << PAGE_CACHE_SHIFT;
135 return PTR_ERR(page);
137 de = ((struct qnx6_dir_entry *)page_address(page)) + start;
138 for (; i < limit; i++, de++, ctx->pos += QNX6_DIR_ENTRY_SIZE) {
139 int size = de->de_size;
140 u32 no_inode = fs32_to_cpu(sbi, de->de_inode);
142 if (!no_inode || !size)
145 if (size > QNX6_SHORT_NAME_MAX) {
146 /* long filename detected
147 get the filename from long filename
149 if (!qnx6_dir_longfilename(inode,
150 (struct qnx6_long_dir_entry *)de,
156 pr_debug("%s():%.*s inode:%u\n",
157 __func__, size, de->de_fname,
159 if (!dir_emit(ctx, de->de_fname, size,
160 no_inode, DT_UNKNOWN)) {
172 * check if the long filename is correct.
174 static unsigned qnx6_long_match(int len, const char *name,
175 struct qnx6_long_dir_entry *de, struct inode *dir)
177 struct super_block *s = dir->i_sb;
178 struct qnx6_sb_info *sbi = QNX6_SB(s);
181 struct qnx6_long_filename *lf = qnx6_longname(s, de, &page);
186 thislen = fs16_to_cpu(sbi, lf->lf_size);
187 if (len != thislen) {
191 if (memcmp(name, lf->lf_fname, len) == 0) {
193 return fs32_to_cpu(sbi, de->de_inode);
200 * check if the filename is correct.
202 static unsigned qnx6_match(struct super_block *s, int len, const char *name,
203 struct qnx6_dir_entry *de)
205 struct qnx6_sb_info *sbi = QNX6_SB(s);
206 if (memcmp(name, de->de_fname, len) == 0)
207 return fs32_to_cpu(sbi, de->de_inode);
212 unsigned qnx6_find_entry(int len, struct inode *dir, const char *name,
213 struct page **res_page)
215 struct super_block *s = dir->i_sb;
216 struct qnx6_inode_info *ei = QNX6_I(dir);
217 struct page *page = NULL;
218 unsigned long start, n;
219 unsigned long npages = dir_pages(dir);
221 struct qnx6_dir_entry *de;
222 struct qnx6_long_dir_entry *lde;
228 start = ei->i_dir_start_lookup;
234 page = qnx6_get_page(dir, n);
236 int limit = last_entry(dir, n);
239 de = (struct qnx6_dir_entry *)page_address(page);
240 for (i = 0; i < limit; i++, de++) {
241 if (len <= QNX6_SHORT_NAME_MAX) {
243 if (len != de->de_size)
245 ino = qnx6_match(s, len, name, de);
248 } else if (de->de_size == 0xff) {
249 /* deal with long filename */
250 lde = (struct qnx6_long_dir_entry *)de;
251 ino = qnx6_long_match(len,
256 pr_err("undefined filename size in inode.\n");
263 } while (n != start);
268 ei->i_dir_start_lookup = n;
272 const struct file_operations qnx6_dir_operations = {
273 .llseek = generic_file_llseek,
274 .read = generic_read_dir,
275 .iterate = qnx6_readdir,
276 .fsync = generic_file_fsync,
279 const struct inode_operations qnx6_dir_inode_operations = {
280 .lookup = qnx6_lookup,