Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd. |
| 4 | */ |
| 5 | |
Hyeongseok Kim | 42d15a8 | 2021-03-04 08:59:45 +0900 | [diff] [blame] | 6 | #include <linux/version.h> |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 7 | #include <linux/blkdev.h> |
| 8 | #include <linux/slab.h> |
| 9 | #include <linux/buffer_head.h> |
Chukun Pan | e7fd355 | 2022-05-18 13:42:36 +0900 | [diff] [blame] | 10 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 11, 0) |
Hyeongseok Kim | 42d15a8 | 2021-03-04 08:59:45 +0900 | [diff] [blame] | 11 | #include <linux/sched/signal.h> |
Chukun Pan | e7fd355 | 2022-05-18 13:42:36 +0900 | [diff] [blame] | 12 | #else |
| 13 | #include <linux/sched.h> |
Hyeongseok Kim | 42d15a8 | 2021-03-04 08:59:45 +0900 | [diff] [blame] | 14 | #endif |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 15 | |
| 16 | #include "exfat_raw.h" |
| 17 | #include "exfat_fs.h" |
| 18 | |
| 19 | static const unsigned char free_bit[] = { |
| 20 | 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/ |
| 21 | 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/ |
| 22 | 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/ |
| 23 | 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/ |
| 24 | 0,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4, 4, 5,/* 60 ~ 79*/ |
| 40 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/ |
| 41 | 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/ |
| 42 | 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/ |
| 43 | 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/ |
| 44 | 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/ |
| 45 | 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/ |
| 46 | 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/ |
| 47 | 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/ |
| 48 | 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/ |
| 49 | }; |
| 50 | |
| 51 | /* |
| 52 | * Allocation Bitmap Management Functions |
| 53 | */ |
| 54 | static int exfat_allocate_bitmap(struct super_block *sb, |
| 55 | struct exfat_dentry *ep) |
| 56 | { |
| 57 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 58 | long long map_size; |
| 59 | unsigned int i, need_map_size; |
| 60 | sector_t sector; |
| 61 | |
| 62 | sbi->map_clu = le32_to_cpu(ep->dentry.bitmap.start_clu); |
| 63 | map_size = le64_to_cpu(ep->dentry.bitmap.size); |
| 64 | need_map_size = ((EXFAT_DATA_CLUSTER_COUNT(sbi) - 1) / BITS_PER_BYTE) |
| 65 | + 1; |
| 66 | if (need_map_size != map_size) { |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 67 | exfat_err(sb, "bogus allocation bitmap size(need : %u, cur : %lld)", |
| 68 | need_map_size, map_size); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 69 | /* |
| 70 | * Only allowed when bogus allocation |
| 71 | * bitmap size is large |
| 72 | */ |
| 73 | if (need_map_size > map_size) |
| 74 | return -EIO; |
| 75 | } |
| 76 | sbi->map_sectors = ((need_map_size - 1) >> |
| 77 | (sb->s_blocksize_bits)) + 1; |
| 78 | sbi->vol_amap = kmalloc_array(sbi->map_sectors, |
| 79 | sizeof(struct buffer_head *), GFP_KERNEL); |
| 80 | if (!sbi->vol_amap) |
| 81 | return -ENOMEM; |
| 82 | |
| 83 | sector = exfat_cluster_to_sector(sbi, sbi->map_clu); |
| 84 | for (i = 0; i < sbi->map_sectors; i++) { |
| 85 | sbi->vol_amap[i] = sb_bread(sb, sector + i); |
| 86 | if (!sbi->vol_amap[i]) { |
| 87 | /* release all buffers and free vol_amap */ |
| 88 | int j = 0; |
| 89 | |
| 90 | while (j < i) |
| 91 | brelse(sbi->vol_amap[j++]); |
| 92 | |
| 93 | kfree(sbi->vol_amap); |
| 94 | sbi->vol_amap = NULL; |
| 95 | return -EIO; |
| 96 | } |
| 97 | } |
| 98 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | int exfat_load_bitmap(struct super_block *sb) |
| 103 | { |
| 104 | unsigned int i, type; |
| 105 | struct exfat_chain clu; |
| 106 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 107 | |
| 108 | exfat_chain_set(&clu, sbi->root_dir, 0, ALLOC_FAT_CHAIN); |
| 109 | while (clu.dir != EXFAT_EOF_CLUSTER) { |
| 110 | for (i = 0; i < sbi->dentries_per_clu; i++) { |
| 111 | struct exfat_dentry *ep; |
| 112 | struct buffer_head *bh; |
| 113 | |
Yuezhang.Mo | 216624a | 2021-12-16 21:29:58 +0900 | [diff] [blame] | 114 | ep = exfat_get_dentry(sb, &clu, i, &bh); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 115 | if (!ep) |
| 116 | return -EIO; |
| 117 | |
| 118 | type = exfat_get_entry_type(ep); |
| 119 | if (type == TYPE_UNUSED) |
| 120 | break; |
| 121 | if (type != TYPE_BITMAP) |
| 122 | continue; |
| 123 | if (ep->dentry.bitmap.flags == 0x0) { |
| 124 | int err; |
| 125 | |
| 126 | err = exfat_allocate_bitmap(sb, ep); |
| 127 | brelse(bh); |
| 128 | return err; |
| 129 | } |
| 130 | brelse(bh); |
| 131 | } |
| 132 | |
| 133 | if (exfat_get_next_cluster(sb, &clu.dir)) |
| 134 | return -EIO; |
| 135 | } |
| 136 | |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | |
| 140 | void exfat_free_bitmap(struct exfat_sb_info *sbi) |
| 141 | { |
| 142 | int i; |
| 143 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 144 | for (i = 0; i < sbi->map_sectors; i++) |
| 145 | __brelse(sbi->vol_amap[i]); |
| 146 | |
| 147 | kfree(sbi->vol_amap); |
| 148 | } |
| 149 | |
Hyeongseok Kim | 4728c61 | 2021-03-18 15:23:56 +0900 | [diff] [blame] | 150 | int exfat_set_bitmap(struct inode *inode, unsigned int clu, bool sync) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 151 | { |
| 152 | int i, b; |
| 153 | unsigned int ent_idx; |
| 154 | struct super_block *sb = inode->i_sb; |
| 155 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 156 | |
Tadeusz Struk | 54607fb | 2022-05-16 11:44:03 +0900 | [diff] [blame] | 157 | if (!is_valid_cluster(sbi, clu)) |
| 158 | return -EINVAL; |
| 159 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 160 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); |
| 161 | i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); |
| 162 | b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); |
| 163 | |
| 164 | set_bit_le(b, sbi->vol_amap[i]->b_data); |
Hyeongseok Kim | 4728c61 | 2021-03-18 15:23:56 +0900 | [diff] [blame] | 165 | exfat_update_bh(sbi->vol_amap[i], sync); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
Hyeongseok Kim | 898b0ec | 2021-02-01 10:09:48 +0900 | [diff] [blame] | 169 | void exfat_clear_bitmap(struct inode *inode, unsigned int clu, bool sync) |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 170 | { |
| 171 | int i, b; |
| 172 | unsigned int ent_idx; |
| 173 | struct super_block *sb = inode->i_sb; |
| 174 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 175 | struct exfat_mount_options *opts = &sbi->options; |
| 176 | |
Tadeusz Struk | 54607fb | 2022-05-16 11:44:03 +0900 | [diff] [blame] | 177 | if (!is_valid_cluster(sbi, clu)) |
| 178 | return; |
| 179 | |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 180 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); |
| 181 | i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); |
| 182 | b = BITMAP_OFFSET_BIT_IN_SECTOR(sb, ent_idx); |
| 183 | |
| 184 | clear_bit_le(b, sbi->vol_amap[i]->b_data); |
Hyeongseok Kim | 898b0ec | 2021-02-01 10:09:48 +0900 | [diff] [blame] | 185 | exfat_update_bh(sbi->vol_amap[i], sync); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 186 | |
| 187 | if (opts->discard) { |
| 188 | int ret_discard; |
| 189 | |
| 190 | ret_discard = sb_issue_discard(sb, |
Hyeongseok Kim | 73138cb | 2021-03-04 08:42:45 +0900 | [diff] [blame] | 191 | exfat_cluster_to_sector(sbi, clu), |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 192 | (1 << sbi->sect_per_clus_bits), GFP_NOFS, 0); |
| 193 | |
| 194 | if (ret_discard == -EOPNOTSUPP) { |
Joe Perches | e08ed90 | 2020-04-06 12:49:36 +0900 | [diff] [blame] | 195 | exfat_err(sb, "discard not supported by device, disabling"); |
Namjae Jeon | 9273f3d | 2020-02-03 21:47:19 +0900 | [diff] [blame] | 196 | opts->discard = 0; |
| 197 | } |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | /* |
| 202 | * If the value of "clu" is 0, it means cluster 2 which is the first cluster of |
| 203 | * the cluster heap. |
| 204 | */ |
| 205 | unsigned int exfat_find_free_bitmap(struct super_block *sb, unsigned int clu) |
| 206 | { |
| 207 | unsigned int i, map_i, map_b, ent_idx; |
| 208 | unsigned int clu_base, clu_free; |
| 209 | unsigned char k, clu_mask; |
| 210 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 211 | |
| 212 | WARN_ON(clu < EXFAT_FIRST_CLUSTER); |
| 213 | ent_idx = CLUSTER_TO_BITMAP_ENT(clu); |
| 214 | clu_base = BITMAP_ENT_TO_CLUSTER(ent_idx & ~(BITS_PER_BYTE_MASK)); |
| 215 | clu_mask = IGNORED_BITS_REMAINED(clu, clu_base); |
| 216 | |
| 217 | map_i = BITMAP_OFFSET_SECTOR_INDEX(sb, ent_idx); |
| 218 | map_b = BITMAP_OFFSET_BYTE_IN_SECTOR(sb, ent_idx); |
| 219 | |
| 220 | for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; |
| 221 | i += BITS_PER_BYTE) { |
| 222 | k = *(sbi->vol_amap[map_i]->b_data + map_b); |
| 223 | if (clu_mask > 0) { |
| 224 | k |= clu_mask; |
| 225 | clu_mask = 0; |
| 226 | } |
| 227 | if (k < 0xFF) { |
| 228 | clu_free = clu_base + free_bit[k]; |
| 229 | if (clu_free < sbi->num_clusters) |
| 230 | return clu_free; |
| 231 | } |
| 232 | clu_base += BITS_PER_BYTE; |
| 233 | |
| 234 | if (++map_b >= sb->s_blocksize || |
| 235 | clu_base >= sbi->num_clusters) { |
| 236 | if (++map_i >= sbi->map_sectors) { |
| 237 | clu_base = EXFAT_FIRST_CLUSTER; |
| 238 | map_i = 0; |
| 239 | } |
| 240 | map_b = 0; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return EXFAT_EOF_CLUSTER; |
| 245 | } |
| 246 | |
| 247 | int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count) |
| 248 | { |
| 249 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 250 | unsigned int count = 0; |
| 251 | unsigned int i, map_i = 0, map_b = 0; |
| 252 | unsigned int total_clus = EXFAT_DATA_CLUSTER_COUNT(sbi); |
| 253 | unsigned int last_mask = total_clus & BITS_PER_BYTE_MASK; |
| 254 | unsigned char clu_bits; |
| 255 | const unsigned char last_bit_mask[] = {0, 0b00000001, 0b00000011, |
| 256 | 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111}; |
| 257 | |
| 258 | total_clus &= ~last_mask; |
| 259 | for (i = 0; i < total_clus; i += BITS_PER_BYTE) { |
| 260 | clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); |
| 261 | count += used_bit[clu_bits]; |
| 262 | if (++map_b >= (unsigned int)sb->s_blocksize) { |
| 263 | map_i++; |
| 264 | map_b = 0; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | if (last_mask) { |
| 269 | clu_bits = *(sbi->vol_amap[map_i]->b_data + map_b); |
| 270 | clu_bits &= last_bit_mask[last_mask]; |
| 271 | count += used_bit[clu_bits]; |
| 272 | } |
| 273 | |
| 274 | *ret_count = count; |
| 275 | return 0; |
| 276 | } |
Hyeongseok Kim | 42d15a8 | 2021-03-04 08:59:45 +0900 | [diff] [blame] | 277 | |
| 278 | int exfat_trim_fs(struct inode *inode, struct fstrim_range *range) |
| 279 | { |
| 280 | unsigned int trim_begin, trim_end, count, next_free_clu; |
| 281 | u64 clu_start, clu_end, trim_minlen, trimmed_total = 0; |
| 282 | struct super_block *sb = inode->i_sb; |
| 283 | struct exfat_sb_info *sbi = EXFAT_SB(sb); |
| 284 | int err = 0; |
| 285 | |
| 286 | clu_start = max_t(u64, range->start >> sbi->cluster_size_bits, |
| 287 | EXFAT_FIRST_CLUSTER); |
| 288 | clu_end = clu_start + (range->len >> sbi->cluster_size_bits) - 1; |
| 289 | trim_minlen = range->minlen >> sbi->cluster_size_bits; |
| 290 | |
| 291 | if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size) |
| 292 | return -EINVAL; |
| 293 | |
| 294 | if (clu_end >= sbi->num_clusters) |
| 295 | clu_end = sbi->num_clusters - 1; |
| 296 | |
| 297 | mutex_lock(&sbi->bitmap_lock); |
| 298 | |
| 299 | trim_begin = trim_end = exfat_find_free_bitmap(sb, clu_start); |
| 300 | if (trim_begin == EXFAT_EOF_CLUSTER) |
| 301 | goto unlock; |
| 302 | |
| 303 | next_free_clu = exfat_find_free_bitmap(sb, trim_end + 1); |
| 304 | if (next_free_clu == EXFAT_EOF_CLUSTER) |
| 305 | goto unlock; |
| 306 | |
| 307 | do { |
| 308 | if (next_free_clu == trim_end + 1) { |
| 309 | /* extend trim range for continuous free cluster */ |
| 310 | trim_end++; |
| 311 | } else { |
| 312 | /* trim current range if it's larger than trim_minlen */ |
| 313 | count = trim_end - trim_begin + 1; |
| 314 | if (count >= trim_minlen) { |
| 315 | err = sb_issue_discard(sb, |
| 316 | exfat_cluster_to_sector(sbi, trim_begin), |
| 317 | count * sbi->sect_per_clus, GFP_NOFS, 0); |
| 318 | if (err) |
| 319 | goto unlock; |
| 320 | |
| 321 | trimmed_total += count; |
| 322 | } |
| 323 | |
| 324 | /* set next start point of the free hole */ |
| 325 | trim_begin = trim_end = next_free_clu; |
| 326 | } |
| 327 | |
| 328 | if (next_free_clu >= clu_end) |
| 329 | break; |
| 330 | |
| 331 | if (fatal_signal_pending(current)) { |
| 332 | err = -ERESTARTSYS; |
| 333 | goto unlock; |
| 334 | } |
| 335 | |
| 336 | next_free_clu = exfat_find_free_bitmap(sb, next_free_clu + 1); |
| 337 | } while (next_free_clu != EXFAT_EOF_CLUSTER && |
| 338 | next_free_clu > trim_end); |
| 339 | |
| 340 | /* try to trim remainder */ |
| 341 | count = trim_end - trim_begin + 1; |
| 342 | if (count >= trim_minlen) { |
| 343 | err = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, trim_begin), |
| 344 | count * sbi->sect_per_clus, GFP_NOFS, 0); |
| 345 | if (err) |
| 346 | goto unlock; |
| 347 | |
| 348 | trimmed_total += count; |
| 349 | } |
| 350 | |
| 351 | unlock: |
| 352 | mutex_unlock(&sbi->bitmap_lock); |
| 353 | range->len = trimmed_total << sbi->cluster_size_bits; |
| 354 | |
| 355 | return err; |
| 356 | } |