1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6 #include <linux/slab.h>
7 #include <linux/unaligned.h>
8 #include <linux/buffer_head.h>
9 #include <linux/blkdev.h>
10
11 #include "exfat_raw.h"
12 #include "exfat_fs.h"
13
exfat_mirror_bh(struct super_block * sb,sector_t sec,struct buffer_head * bh)14 static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
15 struct buffer_head *bh)
16 {
17 struct buffer_head *c_bh;
18 struct exfat_sb_info *sbi = EXFAT_SB(sb);
19 sector_t sec2;
20 int err = 0;
21
22 if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
23 sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
24 c_bh = sb_getblk(sb, sec2);
25 if (!c_bh)
26 return -ENOMEM;
27 memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
28 set_buffer_uptodate(c_bh);
29 mark_buffer_dirty(c_bh);
30 if (sb->s_flags & SB_SYNCHRONOUS)
31 err = sync_dirty_buffer(c_bh);
32 brelse(c_bh);
33 }
34
35 return err;
36 }
37
__exfat_ent_get(struct super_block * sb,unsigned int loc,unsigned int * content)38 static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
39 unsigned int *content)
40 {
41 unsigned int off;
42 sector_t sec;
43 struct buffer_head *bh;
44
45 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
46 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
47
48 bh = sb_bread(sb, sec);
49 if (!bh)
50 return -EIO;
51
52 *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
53
54 /* remap reserved clusters to simplify code */
55 if (*content > EXFAT_BAD_CLUSTER)
56 *content = EXFAT_EOF_CLUSTER;
57
58 brelse(bh);
59 return 0;
60 }
61
exfat_ent_set(struct super_block * sb,unsigned int loc,unsigned int content)62 int exfat_ent_set(struct super_block *sb, unsigned int loc,
63 unsigned int content)
64 {
65 unsigned int off;
66 sector_t sec;
67 __le32 *fat_entry;
68 struct buffer_head *bh;
69
70 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
71 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
72
73 bh = sb_bread(sb, sec);
74 if (!bh)
75 return -EIO;
76
77 fat_entry = (__le32 *)&(bh->b_data[off]);
78 *fat_entry = cpu_to_le32(content);
79 exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
80 exfat_mirror_bh(sb, sec, bh);
81 brelse(bh);
82 return 0;
83 }
84
exfat_ent_get(struct super_block * sb,unsigned int loc,unsigned int * content)85 int exfat_ent_get(struct super_block *sb, unsigned int loc,
86 unsigned int *content)
87 {
88 struct exfat_sb_info *sbi = EXFAT_SB(sb);
89 int err;
90
91 if (!is_valid_cluster(sbi, loc)) {
92 exfat_fs_error_ratelimit(sb,
93 "invalid access to FAT (entry 0x%08x)",
94 loc);
95 return -EIO;
96 }
97
98 err = __exfat_ent_get(sb, loc, content);
99 if (err) {
100 exfat_fs_error_ratelimit(sb,
101 "failed to access to FAT (entry 0x%08x, err:%d)",
102 loc, err);
103 return err;
104 }
105
106 if (*content == EXFAT_FREE_CLUSTER) {
107 exfat_fs_error_ratelimit(sb,
108 "invalid access to FAT free cluster (entry 0x%08x)",
109 loc);
110 return -EIO;
111 }
112
113 if (*content == EXFAT_BAD_CLUSTER) {
114 exfat_fs_error_ratelimit(sb,
115 "invalid access to FAT bad cluster (entry 0x%08x)",
116 loc);
117 return -EIO;
118 }
119
120 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
121 exfat_fs_error_ratelimit(sb,
122 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
123 loc, *content);
124 return -EIO;
125 }
126
127 return 0;
128 }
129
exfat_chain_cont_cluster(struct super_block * sb,unsigned int chain,unsigned int len)130 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
131 unsigned int len)
132 {
133 if (!len)
134 return 0;
135
136 while (len > 1) {
137 if (exfat_ent_set(sb, chain, chain + 1))
138 return -EIO;
139 chain++;
140 len--;
141 }
142
143 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
144 return -EIO;
145 return 0;
146 }
147
exfat_discard_cluster(struct super_block * sb,unsigned int clu,unsigned int num_clusters)148 static inline void exfat_discard_cluster(struct super_block *sb,
149 unsigned int clu, unsigned int num_clusters)
150 {
151 int ret;
152 struct exfat_sb_info *sbi = EXFAT_SB(sb);
153
154 ret = sb_issue_discard(sb, exfat_cluster_to_sector(sbi, clu),
155 sbi->sect_per_clus * num_clusters, GFP_NOFS, 0);
156 if (ret == -EOPNOTSUPP) {
157 exfat_err(sb, "discard not supported by device, disabling");
158 sbi->options.discard = 0;
159 }
160 }
161
162 /* This function must be called with bitmap_lock held */
__exfat_free_cluster(struct inode * inode,struct exfat_chain * p_chain)163 static int __exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
164 {
165 struct super_block *sb = inode->i_sb;
166 struct exfat_sb_info *sbi = EXFAT_SB(sb);
167 int cur_cmap_i, next_cmap_i;
168 unsigned int num_clusters = 0;
169 unsigned int clu;
170
171 /* invalid cluster number */
172 if (p_chain->dir == EXFAT_FREE_CLUSTER ||
173 p_chain->dir == EXFAT_EOF_CLUSTER ||
174 p_chain->dir < EXFAT_FIRST_CLUSTER)
175 return 0;
176
177 /* no cluster to truncate */
178 if (p_chain->size == 0)
179 return 0;
180
181 /* check cluster validation */
182 if (!is_valid_cluster(sbi, p_chain->dir)) {
183 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
184 return -EIO;
185 }
186
187 clu = p_chain->dir;
188
189 cur_cmap_i = next_cmap_i =
190 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu));
191
192 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
193 int err;
194 unsigned int last_cluster = p_chain->dir + p_chain->size - 1;
195 do {
196 bool sync = false;
197
198 if (clu < last_cluster)
199 next_cmap_i =
200 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(clu+1));
201
202 /* flush bitmap only if index would be changed or for last cluster */
203 if (clu == last_cluster || cur_cmap_i != next_cmap_i) {
204 sync = true;
205 cur_cmap_i = next_cmap_i;
206 }
207
208 err = exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode)));
209 if (err)
210 break;
211 clu++;
212 num_clusters++;
213 } while (num_clusters < p_chain->size);
214
215 if (sbi->options.discard)
216 exfat_discard_cluster(sb, p_chain->dir, p_chain->size);
217 } else {
218 unsigned int nr_clu = 1;
219
220 do {
221 bool sync = false;
222 unsigned int n_clu = clu;
223 int err = exfat_get_next_cluster(sb, &n_clu);
224
225 if (err || n_clu == EXFAT_EOF_CLUSTER)
226 sync = true;
227 else
228 next_cmap_i =
229 BITMAP_OFFSET_SECTOR_INDEX(sb, CLUSTER_TO_BITMAP_ENT(n_clu));
230
231 if (cur_cmap_i != next_cmap_i) {
232 sync = true;
233 cur_cmap_i = next_cmap_i;
234 }
235
236 if (exfat_clear_bitmap(inode, clu, (sync && IS_DIRSYNC(inode))))
237 break;
238
239 if (sbi->options.discard) {
240 if (n_clu == clu + 1)
241 nr_clu++;
242 else {
243 exfat_discard_cluster(sb, clu - nr_clu + 1, nr_clu);
244 nr_clu = 1;
245 }
246 }
247
248 clu = n_clu;
249 num_clusters++;
250
251 if (err)
252 break;
253
254 if (num_clusters >= sbi->num_clusters - EXFAT_FIRST_CLUSTER) {
255 /*
256 * The cluster chain includes a loop, scan the
257 * bitmap to get the number of used clusters.
258 */
259 exfat_count_used_clusters(sb, &sbi->used_clusters);
260
261 return 0;
262 }
263 } while (clu != EXFAT_EOF_CLUSTER);
264 }
265
266 sbi->used_clusters -= num_clusters;
267 return 0;
268 }
269
exfat_free_cluster(struct inode * inode,struct exfat_chain * p_chain)270 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
271 {
272 int ret = 0;
273
274 mutex_lock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
275 ret = __exfat_free_cluster(inode, p_chain);
276 mutex_unlock(&EXFAT_SB(inode->i_sb)->bitmap_lock);
277
278 return ret;
279 }
280
exfat_find_last_cluster(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_clu)281 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
282 unsigned int *ret_clu)
283 {
284 unsigned int clu, next;
285 unsigned int count = 0;
286
287 next = p_chain->dir;
288 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
289 *ret_clu = next + p_chain->size - 1;
290 return 0;
291 }
292
293 do {
294 count++;
295 clu = next;
296 if (exfat_ent_get(sb, clu, &next))
297 return -EIO;
298 } while (next != EXFAT_EOF_CLUSTER && count <= p_chain->size);
299
300 if (p_chain->size != count) {
301 exfat_fs_error(sb,
302 "bogus directory size (clus : ondisk(%d) != counted(%d))",
303 p_chain->size, count);
304 return -EIO;
305 }
306
307 *ret_clu = clu;
308 return 0;
309 }
310
exfat_zeroed_cluster(struct inode * dir,unsigned int clu)311 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
312 {
313 struct super_block *sb = dir->i_sb;
314 struct exfat_sb_info *sbi = EXFAT_SB(sb);
315 struct buffer_head *bh;
316 sector_t blknr, last_blknr, i;
317
318 blknr = exfat_cluster_to_sector(sbi, clu);
319 last_blknr = blknr + sbi->sect_per_clus;
320
321 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
322 exfat_fs_error_ratelimit(sb,
323 "%s: out of range(sect:%llu len:%u)",
324 __func__, (unsigned long long)blknr,
325 sbi->sect_per_clus);
326 return -EIO;
327 }
328
329 /* Zeroing the unused blocks on this cluster */
330 for (i = blknr; i < last_blknr; i++) {
331 bh = sb_getblk(sb, i);
332 if (!bh)
333 return -ENOMEM;
334
335 memset(bh->b_data, 0, sb->s_blocksize);
336 set_buffer_uptodate(bh);
337 mark_buffer_dirty(bh);
338 brelse(bh);
339 }
340
341 if (IS_DIRSYNC(dir))
342 return sync_blockdev_range(sb->s_bdev,
343 EXFAT_BLK_TO_B(blknr, sb),
344 EXFAT_BLK_TO_B(last_blknr, sb) - 1);
345
346 return 0;
347 }
348
exfat_alloc_cluster(struct inode * inode,unsigned int num_alloc,struct exfat_chain * p_chain,bool sync_bmap)349 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
350 struct exfat_chain *p_chain, bool sync_bmap)
351 {
352 int ret = -ENOSPC;
353 unsigned int total_cnt;
354 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
355 struct super_block *sb = inode->i_sb;
356 struct exfat_sb_info *sbi = EXFAT_SB(sb);
357
358 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
359
360 if (unlikely(total_cnt < sbi->used_clusters)) {
361 exfat_fs_error_ratelimit(sb,
362 "%s: invalid used clusters(t:%u,u:%u)\n",
363 __func__, total_cnt, sbi->used_clusters);
364 return -EIO;
365 }
366
367 if (num_alloc > total_cnt - sbi->used_clusters)
368 return -ENOSPC;
369
370 mutex_lock(&sbi->bitmap_lock);
371
372 hint_clu = p_chain->dir;
373 /* find new cluster */
374 if (hint_clu == EXFAT_EOF_CLUSTER) {
375 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
376 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)",
377 sbi->clu_srch_ptr);
378 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
379 }
380
381 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
382 if (hint_clu == EXFAT_EOF_CLUSTER) {
383 ret = -ENOSPC;
384 goto unlock;
385 }
386 }
387
388 /* check cluster validation */
389 if (!is_valid_cluster(sbi, hint_clu)) {
390 if (hint_clu != sbi->num_clusters)
391 exfat_err(sb, "hint_cluster is invalid (%u), rewind to the first cluster",
392 hint_clu);
393 hint_clu = EXFAT_FIRST_CLUSTER;
394 p_chain->flags = ALLOC_FAT_CHAIN;
395 }
396
397 p_chain->dir = EXFAT_EOF_CLUSTER;
398
399 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
400 EXFAT_EOF_CLUSTER) {
401 if (new_clu != hint_clu &&
402 p_chain->flags == ALLOC_NO_FAT_CHAIN) {
403 if (exfat_chain_cont_cluster(sb, p_chain->dir,
404 p_chain->size)) {
405 ret = -EIO;
406 goto free_cluster;
407 }
408 p_chain->flags = ALLOC_FAT_CHAIN;
409 }
410
411 /* update allocation bitmap */
412 if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
413 ret = -EIO;
414 goto free_cluster;
415 }
416
417 /* update FAT table */
418 if (p_chain->flags == ALLOC_FAT_CHAIN) {
419 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
420 ret = -EIO;
421 goto free_cluster;
422 }
423 }
424
425 if (p_chain->dir == EXFAT_EOF_CLUSTER) {
426 p_chain->dir = new_clu;
427 } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
428 if (exfat_ent_set(sb, last_clu, new_clu)) {
429 ret = -EIO;
430 goto free_cluster;
431 }
432 }
433 p_chain->size++;
434
435 last_clu = new_clu;
436
437 if (p_chain->size == num_alloc) {
438 sbi->clu_srch_ptr = hint_clu;
439 sbi->used_clusters += num_alloc;
440
441 mutex_unlock(&sbi->bitmap_lock);
442 return 0;
443 }
444
445 hint_clu = new_clu + 1;
446 if (hint_clu >= sbi->num_clusters) {
447 hint_clu = EXFAT_FIRST_CLUSTER;
448
449 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
450 if (exfat_chain_cont_cluster(sb, p_chain->dir,
451 p_chain->size)) {
452 ret = -EIO;
453 goto free_cluster;
454 }
455 p_chain->flags = ALLOC_FAT_CHAIN;
456 }
457 }
458 }
459 free_cluster:
460 __exfat_free_cluster(inode, p_chain);
461 unlock:
462 mutex_unlock(&sbi->bitmap_lock);
463 return ret;
464 }
465
exfat_count_num_clusters(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_count)466 int exfat_count_num_clusters(struct super_block *sb,
467 struct exfat_chain *p_chain, unsigned int *ret_count)
468 {
469 unsigned int i, count;
470 unsigned int clu;
471 struct exfat_sb_info *sbi = EXFAT_SB(sb);
472
473 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
474 *ret_count = 0;
475 return 0;
476 }
477
478 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
479 *ret_count = p_chain->size;
480 return 0;
481 }
482
483 clu = p_chain->dir;
484 count = 0;
485 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
486 count++;
487 if (exfat_ent_get(sb, clu, &clu))
488 return -EIO;
489 if (clu == EXFAT_EOF_CLUSTER)
490 break;
491 }
492
493 *ret_count = count;
494
495 /*
496 * since exfat_count_used_clusters() is not called, sbi->used_clusters
497 * cannot be used here.
498 */
499 if (unlikely(i == sbi->num_clusters && clu != EXFAT_EOF_CLUSTER)) {
500 exfat_fs_error(sb, "The cluster chain has a loop");
501 return -EIO;
502 }
503
504 return 0;
505 }
506