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