Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
0004  */
0005 
0006 #include <linux/slab.h>
0007 #include <linux/compat.h>
0008 #include <linux/bio.h>
0009 #include <linux/buffer_head.h>
0010 
0011 #include "exfat_raw.h"
0012 #include "exfat_fs.h"
0013 
0014 static int exfat_extract_uni_name(struct exfat_dentry *ep,
0015         unsigned short *uniname)
0016 {
0017     int i, len = 0;
0018 
0019     for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
0020         *uniname = le16_to_cpu(ep->dentry.name.unicode_0_14[i]);
0021         if (*uniname == 0x0)
0022             return len;
0023         uniname++;
0024         len++;
0025     }
0026 
0027     *uniname = 0x0;
0028     return len;
0029 
0030 }
0031 
0032 static void exfat_get_uniname_from_ext_entry(struct super_block *sb,
0033         struct exfat_chain *p_dir, int entry, unsigned short *uniname)
0034 {
0035     int i;
0036     struct exfat_entry_set_cache *es;
0037 
0038     es = exfat_get_dentry_set(sb, p_dir, entry, ES_ALL_ENTRIES);
0039     if (!es)
0040         return;
0041 
0042     /*
0043      * First entry  : file entry
0044      * Second entry : stream-extension entry
0045      * Third entry  : first file-name entry
0046      * So, the index of first file-name dentry should start from 2.
0047      */
0048     for (i = 2; i < es->num_entries; i++) {
0049         struct exfat_dentry *ep = exfat_get_dentry_cached(es, i);
0050 
0051         /* end of name entry */
0052         if (exfat_get_entry_type(ep) != TYPE_EXTEND)
0053             break;
0054 
0055         exfat_extract_uni_name(ep, uniname);
0056         uniname += EXFAT_FILE_NAME_LEN;
0057     }
0058 
0059     exfat_free_dentry_set(es, false);
0060 }
0061 
0062 /* read a directory entry from the opened directory */
0063 static int exfat_readdir(struct inode *inode, loff_t *cpos, struct exfat_dir_entry *dir_entry)
0064 {
0065     int i, dentries_per_clu, dentries_per_clu_bits = 0, num_ext;
0066     unsigned int type, clu_offset, max_dentries;
0067     struct exfat_chain dir, clu;
0068     struct exfat_uni_name uni_name;
0069     struct exfat_dentry *ep;
0070     struct super_block *sb = inode->i_sb;
0071     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0072     struct exfat_inode_info *ei = EXFAT_I(inode);
0073     unsigned int dentry = EXFAT_B_TO_DEN(*cpos) & 0xFFFFFFFF;
0074     struct buffer_head *bh;
0075 
0076     /* check if the given file ID is opened */
0077     if (ei->type != TYPE_DIR)
0078         return -EPERM;
0079 
0080     if (ei->entry == -1)
0081         exfat_chain_set(&dir, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
0082     else
0083         exfat_chain_set(&dir, ei->start_clu,
0084             EXFAT_B_TO_CLU(i_size_read(inode), sbi), ei->flags);
0085 
0086     dentries_per_clu = sbi->dentries_per_clu;
0087     dentries_per_clu_bits = ilog2(dentries_per_clu);
0088     max_dentries = (unsigned int)min_t(u64, MAX_EXFAT_DENTRIES,
0089                        (u64)sbi->num_clusters << dentries_per_clu_bits);
0090 
0091     clu_offset = dentry >> dentries_per_clu_bits;
0092     exfat_chain_dup(&clu, &dir);
0093 
0094     if (clu.flags == ALLOC_NO_FAT_CHAIN) {
0095         clu.dir += clu_offset;
0096         clu.size -= clu_offset;
0097     } else {
0098         /* hint_information */
0099         if (clu_offset > 0 && ei->hint_bmap.off != EXFAT_EOF_CLUSTER &&
0100             ei->hint_bmap.off > 0 && clu_offset >= ei->hint_bmap.off) {
0101             clu_offset -= ei->hint_bmap.off;
0102             clu.dir = ei->hint_bmap.clu;
0103         }
0104 
0105         while (clu_offset > 0) {
0106             if (exfat_get_next_cluster(sb, &(clu.dir)))
0107                 return -EIO;
0108 
0109             clu_offset--;
0110         }
0111     }
0112 
0113     while (clu.dir != EXFAT_EOF_CLUSTER && dentry < max_dentries) {
0114         i = dentry & (dentries_per_clu - 1);
0115 
0116         for ( ; i < dentries_per_clu; i++, dentry++) {
0117             ep = exfat_get_dentry(sb, &clu, i, &bh);
0118             if (!ep)
0119                 return -EIO;
0120 
0121             type = exfat_get_entry_type(ep);
0122             if (type == TYPE_UNUSED) {
0123                 brelse(bh);
0124                 break;
0125             }
0126 
0127             if (type != TYPE_FILE && type != TYPE_DIR) {
0128                 brelse(bh);
0129                 continue;
0130             }
0131 
0132             num_ext = ep->dentry.file.num_ext;
0133             dir_entry->attr = le16_to_cpu(ep->dentry.file.attr);
0134             exfat_get_entry_time(sbi, &dir_entry->crtime,
0135                     ep->dentry.file.create_tz,
0136                     ep->dentry.file.create_time,
0137                     ep->dentry.file.create_date,
0138                     ep->dentry.file.create_time_cs);
0139             exfat_get_entry_time(sbi, &dir_entry->mtime,
0140                     ep->dentry.file.modify_tz,
0141                     ep->dentry.file.modify_time,
0142                     ep->dentry.file.modify_date,
0143                     ep->dentry.file.modify_time_cs);
0144             exfat_get_entry_time(sbi, &dir_entry->atime,
0145                     ep->dentry.file.access_tz,
0146                     ep->dentry.file.access_time,
0147                     ep->dentry.file.access_date,
0148                     0);
0149 
0150             *uni_name.name = 0x0;
0151             exfat_get_uniname_from_ext_entry(sb, &clu, i,
0152                 uni_name.name);
0153             exfat_utf16_to_nls(sb, &uni_name,
0154                 dir_entry->namebuf.lfn,
0155                 dir_entry->namebuf.lfnbuf_len);
0156             brelse(bh);
0157 
0158             ep = exfat_get_dentry(sb, &clu, i + 1, &bh);
0159             if (!ep)
0160                 return -EIO;
0161             dir_entry->size =
0162                 le64_to_cpu(ep->dentry.stream.valid_size);
0163             dir_entry->entry = dentry;
0164             brelse(bh);
0165 
0166             ei->hint_bmap.off = dentry >> dentries_per_clu_bits;
0167             ei->hint_bmap.clu = clu.dir;
0168 
0169             *cpos = EXFAT_DEN_TO_B(dentry + 1 + num_ext);
0170             return 0;
0171         }
0172 
0173         if (clu.flags == ALLOC_NO_FAT_CHAIN) {
0174             if (--clu.size > 0)
0175                 clu.dir++;
0176             else
0177                 clu.dir = EXFAT_EOF_CLUSTER;
0178         } else {
0179             if (exfat_get_next_cluster(sb, &(clu.dir)))
0180                 return -EIO;
0181         }
0182     }
0183 
0184     dir_entry->namebuf.lfn[0] = '\0';
0185     *cpos = EXFAT_DEN_TO_B(dentry);
0186     return 0;
0187 }
0188 
0189 static void exfat_init_namebuf(struct exfat_dentry_namebuf *nb)
0190 {
0191     nb->lfn = NULL;
0192     nb->lfnbuf_len = 0;
0193 }
0194 
0195 static int exfat_alloc_namebuf(struct exfat_dentry_namebuf *nb)
0196 {
0197     nb->lfn = __getname();
0198     if (!nb->lfn)
0199         return -ENOMEM;
0200     nb->lfnbuf_len = MAX_VFSNAME_BUF_SIZE;
0201     return 0;
0202 }
0203 
0204 static void exfat_free_namebuf(struct exfat_dentry_namebuf *nb)
0205 {
0206     if (!nb->lfn)
0207         return;
0208 
0209     __putname(nb->lfn);
0210     exfat_init_namebuf(nb);
0211 }
0212 
0213 /* skip iterating emit_dots when dir is empty */
0214 #define ITER_POS_FILLED_DOTS    (2)
0215 static int exfat_iterate(struct file *filp, struct dir_context *ctx)
0216 {
0217     struct inode *inode = filp->f_path.dentry->d_inode;
0218     struct super_block *sb = inode->i_sb;
0219     struct inode *tmp;
0220     struct exfat_dir_entry de;
0221     struct exfat_dentry_namebuf *nb = &(de.namebuf);
0222     struct exfat_inode_info *ei = EXFAT_I(inode);
0223     unsigned long inum;
0224     loff_t cpos, i_pos;
0225     int err = 0, fake_offset = 0;
0226 
0227     exfat_init_namebuf(nb);
0228     mutex_lock(&EXFAT_SB(sb)->s_lock);
0229 
0230     cpos = ctx->pos;
0231     if (!dir_emit_dots(filp, ctx))
0232         goto unlock;
0233 
0234     if (ctx->pos == ITER_POS_FILLED_DOTS) {
0235         cpos = 0;
0236         fake_offset = 1;
0237     }
0238 
0239     if (cpos & (DENTRY_SIZE - 1)) {
0240         err = -ENOENT;
0241         goto unlock;
0242     }
0243 
0244     /* name buffer should be allocated before use */
0245     err = exfat_alloc_namebuf(nb);
0246     if (err)
0247         goto unlock;
0248 get_new:
0249     if (ei->flags == ALLOC_NO_FAT_CHAIN && cpos >= i_size_read(inode))
0250         goto end_of_dir;
0251 
0252     err = exfat_readdir(inode, &cpos, &de);
0253     if (err) {
0254         /*
0255          * At least we tried to read a sector.  Move cpos to next sector
0256          * position (should be aligned).
0257          */
0258         if (err == -EIO) {
0259             cpos += 1 << (sb->s_blocksize_bits);
0260             cpos &= ~(sb->s_blocksize - 1);
0261         }
0262 
0263         err = -EIO;
0264         goto end_of_dir;
0265     }
0266 
0267     if (!nb->lfn[0])
0268         goto end_of_dir;
0269 
0270     i_pos = ((loff_t)ei->start_clu << 32) | (de.entry & 0xffffffff);
0271     tmp = exfat_iget(sb, i_pos);
0272     if (tmp) {
0273         inum = tmp->i_ino;
0274         iput(tmp);
0275     } else {
0276         inum = iunique(sb, EXFAT_ROOT_INO);
0277     }
0278 
0279     /*
0280      * Before calling dir_emit(), sb_lock should be released.
0281      * Because page fault can occur in dir_emit() when the size
0282      * of buffer given from user is larger than one page size.
0283      */
0284     mutex_unlock(&EXFAT_SB(sb)->s_lock);
0285     if (!dir_emit(ctx, nb->lfn, strlen(nb->lfn), inum,
0286             (de.attr & ATTR_SUBDIR) ? DT_DIR : DT_REG))
0287         goto out_unlocked;
0288     mutex_lock(&EXFAT_SB(sb)->s_lock);
0289     ctx->pos = cpos;
0290     goto get_new;
0291 
0292 end_of_dir:
0293     if (!cpos && fake_offset)
0294         cpos = ITER_POS_FILLED_DOTS;
0295     ctx->pos = cpos;
0296 unlock:
0297     mutex_unlock(&EXFAT_SB(sb)->s_lock);
0298 out_unlocked:
0299     /*
0300      * To improve performance, free namebuf after unlock sb_lock.
0301      * If namebuf is not allocated, this function do nothing
0302      */
0303     exfat_free_namebuf(nb);
0304     return err;
0305 }
0306 
0307 const struct file_operations exfat_dir_operations = {
0308     .llseek     = generic_file_llseek,
0309     .read       = generic_read_dir,
0310     .iterate    = exfat_iterate,
0311     .unlocked_ioctl = exfat_ioctl,
0312 #ifdef CONFIG_COMPAT
0313     .compat_ioctl = exfat_compat_ioctl,
0314 #endif
0315     .fsync      = exfat_file_fsync,
0316 };
0317 
0318 int exfat_alloc_new_dir(struct inode *inode, struct exfat_chain *clu)
0319 {
0320     int ret;
0321 
0322     exfat_chain_set(clu, EXFAT_EOF_CLUSTER, 0, ALLOC_NO_FAT_CHAIN);
0323 
0324     ret = exfat_alloc_cluster(inode, 1, clu, IS_DIRSYNC(inode));
0325     if (ret)
0326         return ret;
0327 
0328     return exfat_zeroed_cluster(inode, clu->dir);
0329 }
0330 
0331 int exfat_calc_num_entries(struct exfat_uni_name *p_uniname)
0332 {
0333     int len;
0334 
0335     len = p_uniname->name_len;
0336     if (len == 0)
0337         return -EINVAL;
0338 
0339     /* 1 file entry + 1 stream entry + name entries */
0340     return ((len - 1) / EXFAT_FILE_NAME_LEN + 3);
0341 }
0342 
0343 unsigned int exfat_get_entry_type(struct exfat_dentry *ep)
0344 {
0345     if (ep->type == EXFAT_UNUSED)
0346         return TYPE_UNUSED;
0347     if (IS_EXFAT_DELETED(ep->type))
0348         return TYPE_DELETED;
0349     if (ep->type == EXFAT_INVAL)
0350         return TYPE_INVALID;
0351     if (IS_EXFAT_CRITICAL_PRI(ep->type)) {
0352         if (ep->type == EXFAT_BITMAP)
0353             return TYPE_BITMAP;
0354         if (ep->type == EXFAT_UPCASE)
0355             return TYPE_UPCASE;
0356         if (ep->type == EXFAT_VOLUME)
0357             return TYPE_VOLUME;
0358         if (ep->type == EXFAT_FILE) {
0359             if (le16_to_cpu(ep->dentry.file.attr) & ATTR_SUBDIR)
0360                 return TYPE_DIR;
0361             return TYPE_FILE;
0362         }
0363         return TYPE_CRITICAL_PRI;
0364     }
0365     if (IS_EXFAT_BENIGN_PRI(ep->type)) {
0366         if (ep->type == EXFAT_GUID)
0367             return TYPE_GUID;
0368         if (ep->type == EXFAT_PADDING)
0369             return TYPE_PADDING;
0370         if (ep->type == EXFAT_ACLTAB)
0371             return TYPE_ACLTAB;
0372         return TYPE_BENIGN_PRI;
0373     }
0374     if (IS_EXFAT_CRITICAL_SEC(ep->type)) {
0375         if (ep->type == EXFAT_STREAM)
0376             return TYPE_STREAM;
0377         if (ep->type == EXFAT_NAME)
0378             return TYPE_EXTEND;
0379         if (ep->type == EXFAT_ACL)
0380             return TYPE_ACL;
0381         return TYPE_CRITICAL_SEC;
0382     }
0383     return TYPE_BENIGN_SEC;
0384 }
0385 
0386 static void exfat_set_entry_type(struct exfat_dentry *ep, unsigned int type)
0387 {
0388     if (type == TYPE_UNUSED) {
0389         ep->type = EXFAT_UNUSED;
0390     } else if (type == TYPE_DELETED) {
0391         ep->type &= EXFAT_DELETE;
0392     } else if (type == TYPE_STREAM) {
0393         ep->type = EXFAT_STREAM;
0394     } else if (type == TYPE_EXTEND) {
0395         ep->type = EXFAT_NAME;
0396     } else if (type == TYPE_BITMAP) {
0397         ep->type = EXFAT_BITMAP;
0398     } else if (type == TYPE_UPCASE) {
0399         ep->type = EXFAT_UPCASE;
0400     } else if (type == TYPE_VOLUME) {
0401         ep->type = EXFAT_VOLUME;
0402     } else if (type == TYPE_DIR) {
0403         ep->type = EXFAT_FILE;
0404         ep->dentry.file.attr = cpu_to_le16(ATTR_SUBDIR);
0405     } else if (type == TYPE_FILE) {
0406         ep->type = EXFAT_FILE;
0407         ep->dentry.file.attr = cpu_to_le16(ATTR_ARCHIVE);
0408     }
0409 }
0410 
0411 static void exfat_init_stream_entry(struct exfat_dentry *ep,
0412         unsigned char flags, unsigned int start_clu,
0413         unsigned long long size)
0414 {
0415     exfat_set_entry_type(ep, TYPE_STREAM);
0416     ep->dentry.stream.flags = flags;
0417     ep->dentry.stream.start_clu = cpu_to_le32(start_clu);
0418     ep->dentry.stream.valid_size = cpu_to_le64(size);
0419     ep->dentry.stream.size = cpu_to_le64(size);
0420 }
0421 
0422 static void exfat_init_name_entry(struct exfat_dentry *ep,
0423         unsigned short *uniname)
0424 {
0425     int i;
0426 
0427     exfat_set_entry_type(ep, TYPE_EXTEND);
0428     ep->dentry.name.flags = 0x0;
0429 
0430     for (i = 0; i < EXFAT_FILE_NAME_LEN; i++) {
0431         if (*uniname != 0x0) {
0432             ep->dentry.name.unicode_0_14[i] = cpu_to_le16(*uniname);
0433             uniname++;
0434         } else {
0435             ep->dentry.name.unicode_0_14[i] = 0x0;
0436         }
0437     }
0438 }
0439 
0440 int exfat_init_dir_entry(struct inode *inode, struct exfat_chain *p_dir,
0441         int entry, unsigned int type, unsigned int start_clu,
0442         unsigned long long size)
0443 {
0444     struct super_block *sb = inode->i_sb;
0445     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0446     struct timespec64 ts = current_time(inode);
0447     struct exfat_dentry *ep;
0448     struct buffer_head *bh;
0449 
0450     /*
0451      * We cannot use exfat_get_dentry_set here because file ep is not
0452      * initialized yet.
0453      */
0454     ep = exfat_get_dentry(sb, p_dir, entry, &bh);
0455     if (!ep)
0456         return -EIO;
0457 
0458     exfat_set_entry_type(ep, type);
0459     exfat_set_entry_time(sbi, &ts,
0460             &ep->dentry.file.create_tz,
0461             &ep->dentry.file.create_time,
0462             &ep->dentry.file.create_date,
0463             &ep->dentry.file.create_time_cs);
0464     exfat_set_entry_time(sbi, &ts,
0465             &ep->dentry.file.modify_tz,
0466             &ep->dentry.file.modify_time,
0467             &ep->dentry.file.modify_date,
0468             &ep->dentry.file.modify_time_cs);
0469     exfat_set_entry_time(sbi, &ts,
0470             &ep->dentry.file.access_tz,
0471             &ep->dentry.file.access_time,
0472             &ep->dentry.file.access_date,
0473             NULL);
0474 
0475     exfat_update_bh(bh, IS_DIRSYNC(inode));
0476     brelse(bh);
0477 
0478     ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
0479     if (!ep)
0480         return -EIO;
0481 
0482     exfat_init_stream_entry(ep,
0483         (type == TYPE_FILE) ? ALLOC_FAT_CHAIN : ALLOC_NO_FAT_CHAIN,
0484         start_clu, size);
0485     exfat_update_bh(bh, IS_DIRSYNC(inode));
0486     brelse(bh);
0487 
0488     return 0;
0489 }
0490 
0491 int exfat_update_dir_chksum(struct inode *inode, struct exfat_chain *p_dir,
0492         int entry)
0493 {
0494     struct super_block *sb = inode->i_sb;
0495     int ret = 0;
0496     int i, num_entries;
0497     u16 chksum;
0498     struct exfat_dentry *ep, *fep;
0499     struct buffer_head *fbh, *bh;
0500 
0501     fep = exfat_get_dentry(sb, p_dir, entry, &fbh);
0502     if (!fep)
0503         return -EIO;
0504 
0505     num_entries = fep->dentry.file.num_ext + 1;
0506     chksum = exfat_calc_chksum16(fep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
0507 
0508     for (i = 1; i < num_entries; i++) {
0509         ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
0510         if (!ep) {
0511             ret = -EIO;
0512             goto release_fbh;
0513         }
0514         chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
0515                 CS_DEFAULT);
0516         brelse(bh);
0517     }
0518 
0519     fep->dentry.file.checksum = cpu_to_le16(chksum);
0520     exfat_update_bh(fbh, IS_DIRSYNC(inode));
0521 release_fbh:
0522     brelse(fbh);
0523     return ret;
0524 }
0525 
0526 int exfat_init_ext_entry(struct inode *inode, struct exfat_chain *p_dir,
0527         int entry, int num_entries, struct exfat_uni_name *p_uniname)
0528 {
0529     struct super_block *sb = inode->i_sb;
0530     int i;
0531     unsigned short *uniname = p_uniname->name;
0532     struct exfat_dentry *ep;
0533     struct buffer_head *bh;
0534     int sync = IS_DIRSYNC(inode);
0535 
0536     ep = exfat_get_dentry(sb, p_dir, entry, &bh);
0537     if (!ep)
0538         return -EIO;
0539 
0540     ep->dentry.file.num_ext = (unsigned char)(num_entries - 1);
0541     exfat_update_bh(bh, sync);
0542     brelse(bh);
0543 
0544     ep = exfat_get_dentry(sb, p_dir, entry + 1, &bh);
0545     if (!ep)
0546         return -EIO;
0547 
0548     ep->dentry.stream.name_len = p_uniname->name_len;
0549     ep->dentry.stream.name_hash = cpu_to_le16(p_uniname->name_hash);
0550     exfat_update_bh(bh, sync);
0551     brelse(bh);
0552 
0553     for (i = EXFAT_FIRST_CLUSTER; i < num_entries; i++) {
0554         ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
0555         if (!ep)
0556             return -EIO;
0557 
0558         exfat_init_name_entry(ep, uniname);
0559         exfat_update_bh(bh, sync);
0560         brelse(bh);
0561         uniname += EXFAT_FILE_NAME_LEN;
0562     }
0563 
0564     exfat_update_dir_chksum(inode, p_dir, entry);
0565     return 0;
0566 }
0567 
0568 int exfat_remove_entries(struct inode *inode, struct exfat_chain *p_dir,
0569         int entry, int order, int num_entries)
0570 {
0571     struct super_block *sb = inode->i_sb;
0572     int i;
0573     struct exfat_dentry *ep;
0574     struct buffer_head *bh;
0575 
0576     for (i = order; i < num_entries; i++) {
0577         ep = exfat_get_dentry(sb, p_dir, entry + i, &bh);
0578         if (!ep)
0579             return -EIO;
0580 
0581         exfat_set_entry_type(ep, TYPE_DELETED);
0582         exfat_update_bh(bh, IS_DIRSYNC(inode));
0583         brelse(bh);
0584     }
0585 
0586     return 0;
0587 }
0588 
0589 void exfat_update_dir_chksum_with_entry_set(struct exfat_entry_set_cache *es)
0590 {
0591     int chksum_type = CS_DIR_ENTRY, i;
0592     unsigned short chksum = 0;
0593     struct exfat_dentry *ep;
0594 
0595     for (i = 0; i < es->num_entries; i++) {
0596         ep = exfat_get_dentry_cached(es, i);
0597         chksum = exfat_calc_chksum16(ep, DENTRY_SIZE, chksum,
0598                          chksum_type);
0599         chksum_type = CS_DEFAULT;
0600     }
0601     ep = exfat_get_dentry_cached(es, 0);
0602     ep->dentry.file.checksum = cpu_to_le16(chksum);
0603     es->modified = true;
0604 }
0605 
0606 int exfat_free_dentry_set(struct exfat_entry_set_cache *es, int sync)
0607 {
0608     int i, err = 0;
0609 
0610     if (es->modified)
0611         err = exfat_update_bhs(es->bh, es->num_bh, sync);
0612 
0613     for (i = 0; i < es->num_bh; i++)
0614         if (err)
0615             bforget(es->bh[i]);
0616         else
0617             brelse(es->bh[i]);
0618     kfree(es);
0619     return err;
0620 }
0621 
0622 static int exfat_walk_fat_chain(struct super_block *sb,
0623         struct exfat_chain *p_dir, unsigned int byte_offset,
0624         unsigned int *clu)
0625 {
0626     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0627     unsigned int clu_offset;
0628     unsigned int cur_clu;
0629 
0630     clu_offset = EXFAT_B_TO_CLU(byte_offset, sbi);
0631     cur_clu = p_dir->dir;
0632 
0633     if (p_dir->flags == ALLOC_NO_FAT_CHAIN) {
0634         cur_clu += clu_offset;
0635     } else {
0636         while (clu_offset > 0) {
0637             if (exfat_get_next_cluster(sb, &cur_clu))
0638                 return -EIO;
0639             if (cur_clu == EXFAT_EOF_CLUSTER) {
0640                 exfat_fs_error(sb,
0641                     "invalid dentry access beyond EOF (clu : %u, eidx : %d)",
0642                     p_dir->dir,
0643                     EXFAT_B_TO_DEN(byte_offset));
0644                 return -EIO;
0645             }
0646             clu_offset--;
0647         }
0648     }
0649 
0650     *clu = cur_clu;
0651     return 0;
0652 }
0653 
0654 static int exfat_find_location(struct super_block *sb, struct exfat_chain *p_dir,
0655                    int entry, sector_t *sector, int *offset)
0656 {
0657     int ret;
0658     unsigned int off, clu = 0;
0659     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0660 
0661     off = EXFAT_DEN_TO_B(entry);
0662 
0663     ret = exfat_walk_fat_chain(sb, p_dir, off, &clu);
0664     if (ret)
0665         return ret;
0666 
0667     /* byte offset in cluster */
0668     off = EXFAT_CLU_OFFSET(off, sbi);
0669 
0670     /* byte offset in sector    */
0671     *offset = EXFAT_BLK_OFFSET(off, sb);
0672 
0673     /* sector offset in cluster */
0674     *sector = EXFAT_B_TO_BLK(off, sb);
0675     *sector += exfat_cluster_to_sector(sbi, clu);
0676     return 0;
0677 }
0678 
0679 #define EXFAT_MAX_RA_SIZE     (128*1024)
0680 static int exfat_dir_readahead(struct super_block *sb, sector_t sec)
0681 {
0682     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0683     struct buffer_head *bh;
0684     unsigned int max_ra_count = EXFAT_MAX_RA_SIZE >> sb->s_blocksize_bits;
0685     unsigned int page_ra_count = PAGE_SIZE >> sb->s_blocksize_bits;
0686     unsigned int adj_ra_count = max(sbi->sect_per_clus, page_ra_count);
0687     unsigned int ra_count = min(adj_ra_count, max_ra_count);
0688 
0689     /* Read-ahead is not required */
0690     if (sbi->sect_per_clus == 1)
0691         return 0;
0692 
0693     if (sec < sbi->data_start_sector) {
0694         exfat_err(sb, "requested sector is invalid(sect:%llu, root:%llu)",
0695               (unsigned long long)sec, sbi->data_start_sector);
0696         return -EIO;
0697     }
0698 
0699     /* Not sector aligned with ra_count, resize ra_count to page size */
0700     if ((sec - sbi->data_start_sector) & (ra_count - 1))
0701         ra_count = page_ra_count;
0702 
0703     bh = sb_find_get_block(sb, sec);
0704     if (!bh || !buffer_uptodate(bh)) {
0705         unsigned int i;
0706 
0707         for (i = 0; i < ra_count; i++)
0708             sb_breadahead(sb, (sector_t)(sec + i));
0709     }
0710     brelse(bh);
0711     return 0;
0712 }
0713 
0714 struct exfat_dentry *exfat_get_dentry(struct super_block *sb,
0715         struct exfat_chain *p_dir, int entry, struct buffer_head **bh)
0716 {
0717     unsigned int dentries_per_page = EXFAT_B_TO_DEN(PAGE_SIZE);
0718     int off;
0719     sector_t sec;
0720 
0721     if (p_dir->dir == DIR_DELETED) {
0722         exfat_err(sb, "abnormal access to deleted dentry");
0723         return NULL;
0724     }
0725 
0726     if (exfat_find_location(sb, p_dir, entry, &sec, &off))
0727         return NULL;
0728 
0729     if (p_dir->dir != EXFAT_FREE_CLUSTER &&
0730             !(entry & (dentries_per_page - 1)))
0731         exfat_dir_readahead(sb, sec);
0732 
0733     *bh = sb_bread(sb, sec);
0734     if (!*bh)
0735         return NULL;
0736 
0737     return (struct exfat_dentry *)((*bh)->b_data + off);
0738 }
0739 
0740 enum exfat_validate_dentry_mode {
0741     ES_MODE_STARTED,
0742     ES_MODE_GET_FILE_ENTRY,
0743     ES_MODE_GET_STRM_ENTRY,
0744     ES_MODE_GET_NAME_ENTRY,
0745     ES_MODE_GET_CRITICAL_SEC_ENTRY,
0746 };
0747 
0748 static bool exfat_validate_entry(unsigned int type,
0749         enum exfat_validate_dentry_mode *mode)
0750 {
0751     if (type == TYPE_UNUSED || type == TYPE_DELETED)
0752         return false;
0753 
0754     switch (*mode) {
0755     case ES_MODE_STARTED:
0756         if  (type != TYPE_FILE && type != TYPE_DIR)
0757             return false;
0758         *mode = ES_MODE_GET_FILE_ENTRY;
0759         return true;
0760     case ES_MODE_GET_FILE_ENTRY:
0761         if (type != TYPE_STREAM)
0762             return false;
0763         *mode = ES_MODE_GET_STRM_ENTRY;
0764         return true;
0765     case ES_MODE_GET_STRM_ENTRY:
0766         if (type != TYPE_EXTEND)
0767             return false;
0768         *mode = ES_MODE_GET_NAME_ENTRY;
0769         return true;
0770     case ES_MODE_GET_NAME_ENTRY:
0771         if (type == TYPE_STREAM)
0772             return false;
0773         if (type != TYPE_EXTEND) {
0774             if (!(type & TYPE_CRITICAL_SEC))
0775                 return false;
0776             *mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
0777         }
0778         return true;
0779     case ES_MODE_GET_CRITICAL_SEC_ENTRY:
0780         if (type == TYPE_EXTEND || type == TYPE_STREAM)
0781             return false;
0782         if ((type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
0783             return false;
0784         return true;
0785     default:
0786         WARN_ON_ONCE(1);
0787         return false;
0788     }
0789 }
0790 
0791 struct exfat_dentry *exfat_get_dentry_cached(
0792     struct exfat_entry_set_cache *es, int num)
0793 {
0794     int off = es->start_off + num * DENTRY_SIZE;
0795     struct buffer_head *bh = es->bh[EXFAT_B_TO_BLK(off, es->sb)];
0796     char *p = bh->b_data + EXFAT_BLK_OFFSET(off, es->sb);
0797 
0798     return (struct exfat_dentry *)p;
0799 }
0800 
0801 /*
0802  * Returns a set of dentries for a file or dir.
0803  *
0804  * Note It provides a direct pointer to bh->data via exfat_get_dentry_cached().
0805  * User should call exfat_get_dentry_set() after setting 'modified' to apply
0806  * changes made in this entry set to the real device.
0807  *
0808  * in:
0809  *   sb+p_dir+entry: indicates a file/dir
0810  *   type:  specifies how many dentries should be included.
0811  * return:
0812  *   pointer of entry set on success,
0813  *   NULL on failure.
0814  */
0815 struct exfat_entry_set_cache *exfat_get_dentry_set(struct super_block *sb,
0816         struct exfat_chain *p_dir, int entry, unsigned int type)
0817 {
0818     int ret, i, num_bh;
0819     unsigned int off, byte_offset, clu = 0;
0820     sector_t sec;
0821     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0822     struct exfat_entry_set_cache *es;
0823     struct exfat_dentry *ep;
0824     int num_entries;
0825     enum exfat_validate_dentry_mode mode = ES_MODE_STARTED;
0826     struct buffer_head *bh;
0827 
0828     if (p_dir->dir == DIR_DELETED) {
0829         exfat_err(sb, "access to deleted dentry");
0830         return NULL;
0831     }
0832 
0833     byte_offset = EXFAT_DEN_TO_B(entry);
0834     ret = exfat_walk_fat_chain(sb, p_dir, byte_offset, &clu);
0835     if (ret)
0836         return NULL;
0837 
0838     es = kzalloc(sizeof(*es), GFP_KERNEL);
0839     if (!es)
0840         return NULL;
0841     es->sb = sb;
0842     es->modified = false;
0843 
0844     /* byte offset in cluster */
0845     byte_offset = EXFAT_CLU_OFFSET(byte_offset, sbi);
0846 
0847     /* byte offset in sector */
0848     off = EXFAT_BLK_OFFSET(byte_offset, sb);
0849     es->start_off = off;
0850 
0851     /* sector offset in cluster */
0852     sec = EXFAT_B_TO_BLK(byte_offset, sb);
0853     sec += exfat_cluster_to_sector(sbi, clu);
0854 
0855     bh = sb_bread(sb, sec);
0856     if (!bh)
0857         goto free_es;
0858     es->bh[es->num_bh++] = bh;
0859 
0860     ep = exfat_get_dentry_cached(es, 0);
0861     if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
0862         goto free_es;
0863 
0864     num_entries = type == ES_ALL_ENTRIES ?
0865         ep->dentry.file.num_ext + 1 : type;
0866     es->num_entries = num_entries;
0867 
0868     num_bh = EXFAT_B_TO_BLK_ROUND_UP(off + num_entries * DENTRY_SIZE, sb);
0869     for (i = 1; i < num_bh; i++) {
0870         /* get the next sector */
0871         if (exfat_is_last_sector_in_cluster(sbi, sec)) {
0872             if (p_dir->flags == ALLOC_NO_FAT_CHAIN)
0873                 clu++;
0874             else if (exfat_get_next_cluster(sb, &clu))
0875                 goto free_es;
0876             sec = exfat_cluster_to_sector(sbi, clu);
0877         } else {
0878             sec++;
0879         }
0880 
0881         bh = sb_bread(sb, sec);
0882         if (!bh)
0883             goto free_es;
0884         es->bh[es->num_bh++] = bh;
0885     }
0886 
0887     /* validate cached dentries */
0888     for (i = 1; i < num_entries; i++) {
0889         ep = exfat_get_dentry_cached(es, i);
0890         if (!exfat_validate_entry(exfat_get_entry_type(ep), &mode))
0891             goto free_es;
0892     }
0893     return es;
0894 
0895 free_es:
0896     exfat_free_dentry_set(es, false);
0897     return NULL;
0898 }
0899 
0900 enum {
0901     DIRENT_STEP_FILE,
0902     DIRENT_STEP_STRM,
0903     DIRENT_STEP_NAME,
0904     DIRENT_STEP_SECD,
0905 };
0906 
0907 /*
0908  * @ei:         inode info of parent directory
0909  * @p_dir:      directory structure of parent directory
0910  * @num_entries:entry size of p_uniname
0911  * @hint_opt:   If p_uniname is found, filled with optimized dir/entry
0912  *              for traversing cluster chain.
0913  * @return:
0914  *   >= 0:      file directory entry position where the name exists
0915  *   -ENOENT:   entry with the name does not exist
0916  *   -EIO:      I/O error
0917  */
0918 int exfat_find_dir_entry(struct super_block *sb, struct exfat_inode_info *ei,
0919         struct exfat_chain *p_dir, struct exfat_uni_name *p_uniname,
0920         int num_entries, unsigned int type, struct exfat_hint *hint_opt)
0921 {
0922     int i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
0923     int order, step, name_len = 0;
0924     int dentries_per_clu, num_empty = 0;
0925     unsigned int entry_type;
0926     unsigned short *uniname = NULL;
0927     struct exfat_chain clu;
0928     struct exfat_hint *hint_stat = &ei->hint_stat;
0929     struct exfat_hint_femp candi_empty;
0930     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0931 
0932     dentries_per_clu = sbi->dentries_per_clu;
0933 
0934     exfat_chain_dup(&clu, p_dir);
0935 
0936     if (hint_stat->eidx) {
0937         clu.dir = hint_stat->clu;
0938         dentry = hint_stat->eidx;
0939         end_eidx = dentry;
0940     }
0941 
0942     candi_empty.eidx = EXFAT_HINT_NONE;
0943 rewind:
0944     order = 0;
0945     step = DIRENT_STEP_FILE;
0946     while (clu.dir != EXFAT_EOF_CLUSTER) {
0947         i = dentry & (dentries_per_clu - 1);
0948         for (; i < dentries_per_clu; i++, dentry++) {
0949             struct exfat_dentry *ep;
0950             struct buffer_head *bh;
0951 
0952             if (rewind && dentry == end_eidx)
0953                 goto not_found;
0954 
0955             ep = exfat_get_dentry(sb, &clu, i, &bh);
0956             if (!ep)
0957                 return -EIO;
0958 
0959             entry_type = exfat_get_entry_type(ep);
0960 
0961             if (entry_type == TYPE_UNUSED ||
0962                 entry_type == TYPE_DELETED) {
0963                 step = DIRENT_STEP_FILE;
0964 
0965                 num_empty++;
0966                 if (candi_empty.eidx == EXFAT_HINT_NONE &&
0967                         num_empty == 1) {
0968                     exfat_chain_set(&candi_empty.cur,
0969                         clu.dir, clu.size, clu.flags);
0970                 }
0971 
0972                 if (candi_empty.eidx == EXFAT_HINT_NONE &&
0973                         num_empty >= num_entries) {
0974                     candi_empty.eidx =
0975                         dentry - (num_empty - 1);
0976                     WARN_ON(candi_empty.eidx < 0);
0977                     candi_empty.count = num_empty;
0978 
0979                     if (ei->hint_femp.eidx ==
0980                             EXFAT_HINT_NONE ||
0981                         candi_empty.eidx <=
0982                              ei->hint_femp.eidx)
0983                         ei->hint_femp = candi_empty;
0984                 }
0985 
0986                 brelse(bh);
0987                 if (entry_type == TYPE_UNUSED)
0988                     goto not_found;
0989                 continue;
0990             }
0991 
0992             num_empty = 0;
0993             candi_empty.eidx = EXFAT_HINT_NONE;
0994 
0995             if (entry_type == TYPE_FILE || entry_type == TYPE_DIR) {
0996                 step = DIRENT_STEP_FILE;
0997                 hint_opt->clu = clu.dir;
0998                 hint_opt->eidx = i;
0999                 if (type == TYPE_ALL || type == entry_type) {
1000                     num_ext = ep->dentry.file.num_ext;
1001                     step = DIRENT_STEP_STRM;
1002                 }
1003                 brelse(bh);
1004                 continue;
1005             }
1006 
1007             if (entry_type == TYPE_STREAM) {
1008                 u16 name_hash;
1009 
1010                 if (step != DIRENT_STEP_STRM) {
1011                     step = DIRENT_STEP_FILE;
1012                     brelse(bh);
1013                     continue;
1014                 }
1015                 step = DIRENT_STEP_FILE;
1016                 name_hash = le16_to_cpu(
1017                         ep->dentry.stream.name_hash);
1018                 if (p_uniname->name_hash == name_hash &&
1019                     p_uniname->name_len ==
1020                         ep->dentry.stream.name_len) {
1021                     step = DIRENT_STEP_NAME;
1022                     order = 1;
1023                     name_len = 0;
1024                 }
1025                 brelse(bh);
1026                 continue;
1027             }
1028 
1029             brelse(bh);
1030             if (entry_type == TYPE_EXTEND) {
1031                 unsigned short entry_uniname[16], unichar;
1032 
1033                 if (step != DIRENT_STEP_NAME) {
1034                     step = DIRENT_STEP_FILE;
1035                     continue;
1036                 }
1037 
1038                 if (++order == 2)
1039                     uniname = p_uniname->name;
1040                 else
1041                     uniname += EXFAT_FILE_NAME_LEN;
1042 
1043                 len = exfat_extract_uni_name(ep, entry_uniname);
1044                 name_len += len;
1045 
1046                 unichar = *(uniname+len);
1047                 *(uniname+len) = 0x0;
1048 
1049                 if (exfat_uniname_ncmp(sb, uniname,
1050                     entry_uniname, len)) {
1051                     step = DIRENT_STEP_FILE;
1052                 } else if (p_uniname->name_len == name_len) {
1053                     if (order == num_ext)
1054                         goto found;
1055                     step = DIRENT_STEP_SECD;
1056                 }
1057 
1058                 *(uniname+len) = unichar;
1059                 continue;
1060             }
1061 
1062             if (entry_type &
1063                     (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
1064                 if (step == DIRENT_STEP_SECD) {
1065                     if (++order == num_ext)
1066                         goto found;
1067                     continue;
1068                 }
1069             }
1070             step = DIRENT_STEP_FILE;
1071         }
1072 
1073         if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1074             if (--clu.size > 0)
1075                 clu.dir++;
1076             else
1077                 clu.dir = EXFAT_EOF_CLUSTER;
1078         } else {
1079             if (exfat_get_next_cluster(sb, &clu.dir))
1080                 return -EIO;
1081         }
1082     }
1083 
1084 not_found:
1085     /*
1086      * We started at not 0 index,so we should try to find target
1087      * from 0 index to the index we started at.
1088      */
1089     if (!rewind && end_eidx) {
1090         rewind = 1;
1091         dentry = 0;
1092         clu.dir = p_dir->dir;
1093         /* reset empty hint */
1094         num_empty = 0;
1095         candi_empty.eidx = EXFAT_HINT_NONE;
1096         goto rewind;
1097     }
1098 
1099     /* initialized hint_stat */
1100     hint_stat->clu = p_dir->dir;
1101     hint_stat->eidx = 0;
1102     return -ENOENT;
1103 
1104 found:
1105     /* next dentry we'll find is out of this cluster */
1106     if (!((dentry + 1) & (dentries_per_clu - 1))) {
1107         int ret = 0;
1108 
1109         if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1110             if (--clu.size > 0)
1111                 clu.dir++;
1112             else
1113                 clu.dir = EXFAT_EOF_CLUSTER;
1114         } else {
1115             ret = exfat_get_next_cluster(sb, &clu.dir);
1116         }
1117 
1118         if (ret || clu.dir == EXFAT_EOF_CLUSTER) {
1119             /* just initialized hint_stat */
1120             hint_stat->clu = p_dir->dir;
1121             hint_stat->eidx = 0;
1122             return (dentry - num_ext);
1123         }
1124     }
1125 
1126     hint_stat->clu = clu.dir;
1127     hint_stat->eidx = dentry + 1;
1128     return dentry - num_ext;
1129 }
1130 
1131 int exfat_count_ext_entries(struct super_block *sb, struct exfat_chain *p_dir,
1132         int entry, struct exfat_dentry *ep)
1133 {
1134     int i, count = 0;
1135     unsigned int type;
1136     struct exfat_dentry *ext_ep;
1137     struct buffer_head *bh;
1138 
1139     for (i = 0, entry++; i < ep->dentry.file.num_ext; i++, entry++) {
1140         ext_ep = exfat_get_dentry(sb, p_dir, entry, &bh);
1141         if (!ext_ep)
1142             return -EIO;
1143 
1144         type = exfat_get_entry_type(ext_ep);
1145         brelse(bh);
1146         if (type == TYPE_EXTEND || type == TYPE_STREAM)
1147             count++;
1148         else
1149             break;
1150     }
1151     return count;
1152 }
1153 
1154 int exfat_count_dir_entries(struct super_block *sb, struct exfat_chain *p_dir)
1155 {
1156     int i, count = 0;
1157     int dentries_per_clu;
1158     unsigned int entry_type;
1159     struct exfat_chain clu;
1160     struct exfat_dentry *ep;
1161     struct exfat_sb_info *sbi = EXFAT_SB(sb);
1162     struct buffer_head *bh;
1163 
1164     dentries_per_clu = sbi->dentries_per_clu;
1165 
1166     exfat_chain_dup(&clu, p_dir);
1167 
1168     while (clu.dir != EXFAT_EOF_CLUSTER) {
1169         for (i = 0; i < dentries_per_clu; i++) {
1170             ep = exfat_get_dentry(sb, &clu, i, &bh);
1171             if (!ep)
1172                 return -EIO;
1173             entry_type = exfat_get_entry_type(ep);
1174             brelse(bh);
1175 
1176             if (entry_type == TYPE_UNUSED)
1177                 return count;
1178             if (entry_type != TYPE_DIR)
1179                 continue;
1180             count++;
1181         }
1182 
1183         if (clu.flags == ALLOC_NO_FAT_CHAIN) {
1184             if (--clu.size > 0)
1185                 clu.dir++;
1186             else
1187                 clu.dir = EXFAT_EOF_CLUSTER;
1188         } else {
1189             if (exfat_get_next_cluster(sb, &(clu.dir)))
1190                 return -EIO;
1191         }
1192     }
1193 
1194     return count;
1195 }