Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * QNX6 file system, Linux implementation.
0004  *
0005  * Version : 1.0.0
0006  *
0007  * History :
0008  *
0009  * 01-02-2012 by Kai Bankett (chaosman@ontika.net) : first release.
0010  * 16-02-2012 pagemap extension by Al Viro
0011  *
0012  */
0013 
0014 #include "qnx6.h"
0015 
0016 static unsigned qnx6_lfile_checksum(char *name, unsigned size)
0017 {
0018     unsigned crc = 0;
0019     char *end = name + size;
0020     while (name < end) {
0021         crc = ((crc >> 1) + *(name++)) ^
0022             ((crc & 0x00000001) ? 0x80000000 : 0);
0023     }
0024     return crc;
0025 }
0026 
0027 static struct page *qnx6_get_page(struct inode *dir, unsigned long n)
0028 {
0029     struct address_space *mapping = dir->i_mapping;
0030     struct page *page = read_mapping_page(mapping, n, NULL);
0031     if (!IS_ERR(page))
0032         kmap(page);
0033     return page;
0034 }
0035 
0036 static unsigned last_entry(struct inode *inode, unsigned long page_nr)
0037 {
0038     unsigned long last_byte = inode->i_size;
0039     last_byte -= page_nr << PAGE_SHIFT;
0040     if (last_byte > PAGE_SIZE)
0041         last_byte = PAGE_SIZE;
0042     return last_byte / QNX6_DIR_ENTRY_SIZE;
0043 }
0044 
0045 static struct qnx6_long_filename *qnx6_longname(struct super_block *sb,
0046                      struct qnx6_long_dir_entry *de,
0047                      struct page **p)
0048 {
0049     struct qnx6_sb_info *sbi = QNX6_SB(sb);
0050     u32 s = fs32_to_cpu(sbi, de->de_long_inode); /* in block units */
0051     u32 n = s >> (PAGE_SHIFT - sb->s_blocksize_bits); /* in pages */
0052     /* within page */
0053     u32 offs = (s << sb->s_blocksize_bits) & ~PAGE_MASK;
0054     struct address_space *mapping = sbi->longfile->i_mapping;
0055     struct page *page = read_mapping_page(mapping, n, NULL);
0056     if (IS_ERR(page))
0057         return ERR_CAST(page);
0058     kmap(*p = page);
0059     return (struct qnx6_long_filename *)(page_address(page) + offs);
0060 }
0061 
0062 static int qnx6_dir_longfilename(struct inode *inode,
0063             struct qnx6_long_dir_entry *de,
0064             struct dir_context *ctx,
0065             unsigned de_inode)
0066 {
0067     struct qnx6_long_filename *lf;
0068     struct super_block *s = inode->i_sb;
0069     struct qnx6_sb_info *sbi = QNX6_SB(s);
0070     struct page *page;
0071     int lf_size;
0072 
0073     if (de->de_size != 0xff) {
0074         /* error - long filename entries always have size 0xff
0075            in direntry */
0076         pr_err("invalid direntry size (%i).\n", de->de_size);
0077         return 0;
0078     }
0079     lf = qnx6_longname(s, de, &page);
0080     if (IS_ERR(lf)) {
0081         pr_err("Error reading longname\n");
0082         return 0;
0083     }
0084 
0085     lf_size = fs16_to_cpu(sbi, lf->lf_size);
0086 
0087     if (lf_size > QNX6_LONG_NAME_MAX) {
0088         pr_debug("file %s\n", lf->lf_fname);
0089         pr_err("Filename too long (%i)\n", lf_size);
0090         qnx6_put_page(page);
0091         return 0;
0092     }
0093 
0094     /* calc & validate longfilename checksum
0095        mmi 3g filesystem does not have that checksum */
0096     if (!test_opt(s, MMI_FS) && fs32_to_cpu(sbi, de->de_checksum) !=
0097             qnx6_lfile_checksum(lf->lf_fname, lf_size))
0098         pr_info("long filename checksum error.\n");
0099 
0100     pr_debug("qnx6_readdir:%.*s inode:%u\n",
0101          lf_size, lf->lf_fname, de_inode);
0102     if (!dir_emit(ctx, lf->lf_fname, lf_size, de_inode, DT_UNKNOWN)) {
0103         qnx6_put_page(page);
0104         return 0;
0105     }
0106 
0107     qnx6_put_page(page);
0108     /* success */
0109     return 1;
0110 }
0111 
0112 static int qnx6_readdir(struct file *file, struct dir_context *ctx)
0113 {
0114     struct inode *inode = file_inode(file);
0115     struct super_block *s = inode->i_sb;
0116     struct qnx6_sb_info *sbi = QNX6_SB(s);
0117     loff_t pos = ctx->pos & ~(QNX6_DIR_ENTRY_SIZE - 1);
0118     unsigned long npages = dir_pages(inode);
0119     unsigned long n = pos >> PAGE_SHIFT;
0120     unsigned start = (pos & ~PAGE_MASK) / QNX6_DIR_ENTRY_SIZE;
0121     bool done = false;
0122 
0123     ctx->pos = pos;
0124     if (ctx->pos >= inode->i_size)
0125         return 0;
0126 
0127     for ( ; !done && n < npages; n++, start = 0) {
0128         struct page *page = qnx6_get_page(inode, n);
0129         int limit = last_entry(inode, n);
0130         struct qnx6_dir_entry *de;
0131         int i = start;
0132 
0133         if (IS_ERR(page)) {
0134             pr_err("%s(): read failed\n", __func__);
0135             ctx->pos = (n + 1) << PAGE_SHIFT;
0136             return PTR_ERR(page);
0137         }
0138         de = ((struct qnx6_dir_entry *)page_address(page)) + start;
0139         for (; i < limit; i++, de++, ctx->pos += QNX6_DIR_ENTRY_SIZE) {
0140             int size = de->de_size;
0141             u32 no_inode = fs32_to_cpu(sbi, de->de_inode);
0142 
0143             if (!no_inode || !size)
0144                 continue;
0145 
0146             if (size > QNX6_SHORT_NAME_MAX) {
0147                 /* long filename detected
0148                    get the filename from long filename
0149                    structure / block */
0150                 if (!qnx6_dir_longfilename(inode,
0151                     (struct qnx6_long_dir_entry *)de,
0152                     ctx, no_inode)) {
0153                     done = true;
0154                     break;
0155                 }
0156             } else {
0157                 pr_debug("%s():%.*s inode:%u\n",
0158                      __func__, size, de->de_fname,
0159                      no_inode);
0160                 if (!dir_emit(ctx, de->de_fname, size,
0161                       no_inode, DT_UNKNOWN)) {
0162                     done = true;
0163                     break;
0164                 }
0165             }
0166         }
0167         qnx6_put_page(page);
0168     }
0169     return 0;
0170 }
0171 
0172 /*
0173  * check if the long filename is correct.
0174  */
0175 static unsigned qnx6_long_match(int len, const char *name,
0176             struct qnx6_long_dir_entry *de, struct inode *dir)
0177 {
0178     struct super_block *s = dir->i_sb;
0179     struct qnx6_sb_info *sbi = QNX6_SB(s);
0180     struct page *page;
0181     int thislen;
0182     struct qnx6_long_filename *lf = qnx6_longname(s, de, &page);
0183 
0184     if (IS_ERR(lf))
0185         return 0;
0186 
0187     thislen = fs16_to_cpu(sbi, lf->lf_size);
0188     if (len != thislen) {
0189         qnx6_put_page(page);
0190         return 0;
0191     }
0192     if (memcmp(name, lf->lf_fname, len) == 0) {
0193         qnx6_put_page(page);
0194         return fs32_to_cpu(sbi, de->de_inode);
0195     }
0196     qnx6_put_page(page);
0197     return 0;
0198 }
0199 
0200 /*
0201  * check if the filename is correct.
0202  */
0203 static unsigned qnx6_match(struct super_block *s, int len, const char *name,
0204             struct qnx6_dir_entry *de)
0205 {
0206     struct qnx6_sb_info *sbi = QNX6_SB(s);
0207     if (memcmp(name, de->de_fname, len) == 0)
0208         return fs32_to_cpu(sbi, de->de_inode);
0209     return 0;
0210 }
0211 
0212 
0213 unsigned qnx6_find_entry(int len, struct inode *dir, const char *name,
0214              struct page **res_page)
0215 {
0216     struct super_block *s = dir->i_sb;
0217     struct qnx6_inode_info *ei = QNX6_I(dir);
0218     struct page *page = NULL;
0219     unsigned long start, n;
0220     unsigned long npages = dir_pages(dir);
0221     unsigned ino;
0222     struct qnx6_dir_entry *de;
0223     struct qnx6_long_dir_entry *lde;
0224 
0225     *res_page = NULL;
0226 
0227     if (npages == 0)
0228         return 0;
0229     start = ei->i_dir_start_lookup;
0230     if (start >= npages)
0231         start = 0;
0232     n = start;
0233 
0234     do {
0235         page = qnx6_get_page(dir, n);
0236         if (!IS_ERR(page)) {
0237             int limit = last_entry(dir, n);
0238             int i;
0239 
0240             de = (struct qnx6_dir_entry *)page_address(page);
0241             for (i = 0; i < limit; i++, de++) {
0242                 if (len <= QNX6_SHORT_NAME_MAX) {
0243                     /* short filename */
0244                     if (len != de->de_size)
0245                         continue;
0246                     ino = qnx6_match(s, len, name, de);
0247                     if (ino)
0248                         goto found;
0249                 } else if (de->de_size == 0xff) {
0250                     /* deal with long filename */
0251                     lde = (struct qnx6_long_dir_entry *)de;
0252                     ino = qnx6_long_match(len,
0253                                 name, lde, dir);
0254                     if (ino)
0255                         goto found;
0256                 } else
0257                     pr_err("undefined filename size in inode.\n");
0258             }
0259             qnx6_put_page(page);
0260         }
0261 
0262         if (++n >= npages)
0263             n = 0;
0264     } while (n != start);
0265     return 0;
0266 
0267 found:
0268     *res_page = page;
0269     ei->i_dir_start_lookup = n;
0270     return ino;
0271 }
0272 
0273 const struct file_operations qnx6_dir_operations = {
0274     .llseek     = generic_file_llseek,
0275     .read       = generic_read_dir,
0276     .iterate_shared = qnx6_readdir,
0277     .fsync      = generic_file_fsync,
0278 };
0279 
0280 const struct inode_operations qnx6_dir_inode_operations = {
0281     .lookup     = qnx6_lookup,
0282 };