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/blkdev.h>
0007 #include <linux/slab.h>
0008 #include <linux/buffer_head.h>
0009 
0010 #include "exfat_raw.h"
0011 #include "exfat_fs.h"
0012 
0013 static const unsigned char free_bit[] = {
0014     0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*  0 ~  19*/
0015     0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~  39*/
0016     0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~  59*/
0017     0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~  79*/
0018     0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~  99*/
0019     0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
0020     0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
0021     0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
0022     0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
0023     0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
0024     0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
0025     0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
0026     0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0                /*240 ~ 254*/
0027 };
0028 
0029 static const unsigned char used_bit[] = {
0030     0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/*  0 ~  19*/
0031     2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~  39*/
0032     2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~  59*/
0033     4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~  79*/
0034     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~  99*/
0035     3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
0036     4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
0037     3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
0038     2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
0039     4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
0040     3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
0041     5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
0042     4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8             /*240 ~ 255*/
0043 };
0044 
0045 /*
0046  *  Allocation Bitmap Management Functions
0047  */
0048 static int exfat_allocate_bitmap(struct super_block *sb,
0049         struct exfat_dentry *ep)
0050 {
0051     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0052     long long map_size;
0053     unsigned int i, need_map_size;
0054     sector_t sector;
0055 
0056     sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu);
0057     map_size = le64_to_cpu(ep->dentry.bitmap.size);
0058     need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE)
0059         + 1;
0060     if (need_map_size != map_size) {
0061         exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)",
0062               need_map_size, map_size);
0063         /*
0064          * Only allowed when bogus allocation
0065          * bitmap size is large
0066          */
0067         if (need_map_size > map_size)
0068             return -EIO;
0069     }
0070     sbi->map_sectors = ((need_map_size - 1) >>
0071             (sb->s_blocksize_bits)) + 1;
0072     sbi->vol_amap = kmalloc_array(sbi->map_sectors,
0073                 sizeof(struct buffer_head *), GFP_KERNEL);
0074     if (!sbi->vol_amap)
0075         return -ENOMEM;
0076 
0077     sector = exfat_cluster_to_sector(sbi, sbi->map_clu);
0078     for (i = 0; i < sbi->map_sectors; i++) {
0079         sbi->vol_amap[i] = sb_bread(sb, sector + i);
0080         if (!sbi->vol_amap[i]) {
0081             /* release all buffers and free vol_amap */
0082             int j = 0;
0083 
0084             while (j < i)
0085                 brelse(sbi->vol_amap[j++]);
0086 
0087             kfree(sbi->vol_amap);
0088             sbi->vol_amap = NULL;
0089             return -EIO;
0090         }
0091     }
0092 
0093     return 0;
0094 }
0095 
0096 int exfat_load_bitmap(struct super_block *sb)
0097 {
0098     unsigned int i, type;
0099     struct exfat_chain clu;
0100     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0101 
0102     exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN);
0103     while (clu.dir != EXFAT_EOF_CLUSTER) {
0104         for (i = 0; i < sbi->dentries_per_clu; i++) {
0105             struct exfat_dentry *ep;
0106             struct buffer_head *bh;
0107 
0108             ep = exfat_get_dentry(sb, &clu, i, &bh);
0109             if (!ep)
0110                 return -EIO;
0111 
0112             type = exfat_get_entry_type(ep);
0113             if (type == TYPE_UNUSED)
0114                 break;
0115             if (type != TYPE_BITMAP)
0116                 continue;
0117             if (ep->dentry.bitmap.flags == 0x0) {
0118                 int err;
0119 
0120                 err = exfat_allocate_bitmap(sb, ep);
0121                 brelse(bh);
0122                 return err;
0123             }
0124             brelse(bh);
0125         }
0126 
0127         if (exfat_get_next_cluster(sb, &clu.dir))
0128             return -EIO;
0129     }
0130 
0131     return -EINVAL;
0132 }
0133 
0134 void exfat_free_bitmap(struct exfat_sb_info *sbi)
0135 {
0136     int i;
0137 
0138     for (i = 0; i < sbi->map_sectors; i++)
0139         __brelse(sbi->vol_amap[i]);
0140 
0141     kfree(sbi->vol_amap);
0142 }
0143 
0144 int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync)
0145 {
0146     int i, b;
0147     unsigned int ent_idx;
0148     struct super_block *sb = inode->i_sb;
0149     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0150 
0151     if (!is_valid_cluster(sbi, clu))
0152         return -EINVAL;
0153 
0154     ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
0155     i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
0156     b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
0157 
0158     set_bit_le(b, sbi->vol_amap[i]->b_data);
0159     exfat_update_bh(sbi->vol_amap[i], sync);
0160     return 0;
0161 }
0162 
0163 void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync)
0164 {
0165     int i, b;
0166     unsigned int ent_idx;
0167     struct super_block *sb = inode->i_sb;
0168     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0169     struct exfat_mount_options *opts = &sbi->options;
0170 
0171     if (!is_valid_cluster(sbi, clu))
0172         return;
0173 
0174     ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
0175     i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
0176     b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx);
0177 
0178     clear_bit_le(b, sbi->vol_amap[i]->b_data);
0179     exfat_update_bh(sbi->vol_amap[i], sync);
0180 
0181     if (opts->discard) {
0182         int ret_discard;
0183 
0184         ret_discard = sb_issue_discard(sb,
0185             exfat_cluster_to_sector(sbi, clu),
0186             (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0);
0187 
0188         if (ret_discard == -EOPNOTSUPP) {
0189             exfat_err(sb, "discard not supported by device, disabling");
0190             opts->discard = 0;
0191         }
0192     }
0193 }
0194 
0195 /*
0196  * If the value of "clu" is 0, it means cluster 2 which is the first cluster of
0197  * the cluster heap.
0198  */
0199 unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu)
0200 {
0201     unsigned int i, map_i, map_b, ent_idx;
0202     unsigned int clu_base, clu_free;
0203     unsigned char k, clu_mask;
0204     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0205 
0206     WARN_ON(clu < EXFAT_FIRST_CLUSTER);
0207     ent_idx = CLUSTER_TO_BITMAP_ENT(clu);
0208     clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK));
0209     clu_mask = IGNORED_BITS_REMAINED(clu, clu_base);
0210 
0211     map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx);
0212     map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx);
0213 
0214     for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters;
0215          i += BITS_PER_BYTE) {
0216         k = *(sbi->vol_amap[map_i]->b_data + map_b);
0217         if (clu_mask > 0) {
0218             k |= clu_mask;
0219             clu_mask = 0;
0220         }
0221         if (k < 0xFF) {
0222             clu_free = clu_base + free_bit[k];
0223             if (clu_free < sbi->num_clusters)
0224                 return clu_free;
0225         }
0226         clu_base += BITS_PER_BYTE;
0227 
0228         if (++map_b >= sb->s_blocksize ||
0229             clu_base >= sbi->num_clusters) {
0230             if (++map_i >= sbi->map_sectors) {
0231                 clu_base = EXFAT_FIRST_CLUSTER;
0232                 map_i = 0;
0233             }
0234             map_b = 0;
0235         }
0236     }
0237 
0238     return EXFAT_EOF_CLUSTER;
0239 }
0240 
0241 int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count)
0242 {
0243     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0244     unsigned int count = 0;
0245     unsigned int i, map_i = 0, map_b = 0;
0246     unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi);
0247     unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK;
0248     unsigned char clu_bits;
0249     const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011,
0250         0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111};
0251 
0252     total_clus &= ~last_mask;
0253     for (i = 0; i < total_clus; i += BITS_PER_BYTE) {
0254         clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
0255         count += used_bit[clu_bits];
0256         if (++map_b >= (unsigned int)sb->s_blocksize) {
0257             map_i++;
0258             map_b = 0;
0259         }
0260     }
0261 
0262     if (last_mask) {
0263         clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b);
0264         clu_bits &= last_bit_mask[last_mask];
0265         count += used_bit[clu_bits];
0266     }
0267 
0268     *ret_count = count;
0269     return 0;
0270 }
0271 
0272 int exfat_trim_fs(struct inode *inode, struct fstrim_range *range)
0273 {
0274     unsigned int trim_begin, trim_end, count, next_free_clu;
0275     u64 clu_start, clu_end, trim_minlen, trimmed_total = 0;
0276     struct super_block *sb = inode->i_sb;
0277     struct exfat_sb_info *sbi = EXFAT_SB(sb);
0278     int err = 0;
0279 
0280     clu_start = max_t(u64, range->start >> sbi->cluster_size_bits,
0281                 EXFAT_FIRST_CLUSTER);
0282     clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1;
0283     trim_minlen = range->minlen >> sbi->cluster_size_bits;
0284 
0285     if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size)
0286         return -EINVAL;
0287 
0288     if (clu_end >= sbi->num_clusters)
0289         clu_end = sbi->num_clusters - 1;
0290 
0291     mutex_lock(&sbi->bitmap_lock);
0292 
0293     trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start);
0294     if (trim_begin == EXFAT_EOF_CLUSTER)
0295         goto unlock;
0296 
0297     next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1);
0298     if (next_free_clu == EXFAT_EOF_CLUSTER)
0299         goto unlock;
0300 
0301     do {
0302         if (next_free_clu == trim_end + 1) {
0303             /* extend trim range for continuous free cluster */
0304             trim_end++;
0305         } else {
0306             /* trim current range if it's larger than trim_minlen */
0307             count = trim_end - trim_begin + 1;
0308             if (count >= trim_minlen) {
0309                 err = sb_issue_discard(sb,
0310                     exfat_cluster_to_sector(sbi, trim_begin),
0311                     count * sbi->sect_per_clus, GFP_NOFS, 0);
0312                 if (err)
0313                     goto unlock;
0314 
0315                 trimmed_total += count;
0316             }
0317 
0318             /* set next start point of the free hole */
0319             trim_begin = trim_end = next_free_clu;
0320         }
0321 
0322         if (next_free_clu >= clu_end)
0323             break;
0324 
0325         if (fatal_signal_pending(current)) {
0326             err = -ERESTARTSYS;
0327             goto unlock;
0328         }
0329 
0330         next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1);
0331     } while (next_free_clu != EXFAT_EOF_CLUSTER &&
0332             next_free_clu > trim_end);
0333 
0334     /* try to trim remainder */
0335     count = trim_end - trim_begin + 1;
0336     if (count >= trim_minlen) {
0337         err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin),
0338             count * sbi->sect_per_clus, GFP_NOFS, 0);
0339         if (err)
0340             goto unlock;
0341 
0342         trimmed_total += count;
0343     }
0344 
0345 unlock:
0346     mutex_unlock(&sbi->bitmap_lock);
0347     range->len = trimmed_total << sbi->cluster_size_bits;
0348 
0349     return err;
0350 }