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 <asm/unaligned.h>
8 #include <linux/buffer_head.h>
9 #include <linux/blk_types.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(sb, "invalid access to FAT (entry 0x%08x)",
93 loc);
94 return -EIO;
95 }
96
97 err = __exfat_ent_get(sb, loc, content);
98 if (err) {
99 exfat_fs_error(sb,
100 "failed to access to FAT (entry 0x%08x, err:%d)",
101 loc, err);
102 return err;
103 }
104
105 if (*content == EXFAT_FREE_CLUSTER) {
106 exfat_fs_error(sb,
107 "invalid access to FAT free cluster (entry 0x%08x)",
108 loc);
109 return -EIO;
110 }
111
112 if (*content == EXFAT_BAD_CLUSTER) {
113 exfat_fs_error(sb,
114 "invalid access to FAT bad cluster (entry 0x%08x)",
115 loc);
116 return -EIO;
117 }
118
119 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
120 exfat_fs_error(sb,
121 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
122 loc, *content);
123 return -EIO;
124 }
125
126 return 0;
127 }
128
exfat_chain_cont_cluster(struct super_block * sb,unsigned int chain,unsigned int len)129 int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
130 unsigned int len)
131 {
132 if (!len)
133 return 0;
134
135 while (len > 1) {
136 if (exfat_ent_set(sb, chain, chain + 1))
137 return -EIO;
138 chain++;
139 len--;
140 }
141
142 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
143 return -EIO;
144 return 0;
145 }
146
exfat_free_cluster(struct inode * inode,struct exfat_chain * p_chain)147 int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
148 {
149 unsigned int num_clusters = 0;
150 unsigned int clu;
151 struct super_block *sb = inode->i_sb;
152 struct exfat_sb_info *sbi = EXFAT_SB(sb);
153
154 /* invalid cluster number */
155 if (p_chain->dir == EXFAT_FREE_CLUSTER ||
156 p_chain->dir == EXFAT_EOF_CLUSTER ||
157 p_chain->dir < EXFAT_FIRST_CLUSTER)
158 return 0;
159
160 /* no cluster to truncate */
161 if (p_chain->size == 0)
162 return 0;
163
164 /* check cluster validation */
165 if (!is_valid_cluster(sbi, p_chain->dir)) {
166 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
167 return -EIO;
168 }
169
170 clu = p_chain->dir;
171
172 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
173 do {
174 exfat_clear_bitmap(inode, clu);
175 clu++;
176
177 num_clusters++;
178 } while (num_clusters < p_chain->size);
179 } else {
180 do {
181 exfat_clear_bitmap(inode, clu);
182
183 if (exfat_get_next_cluster(sb, &clu))
184 goto dec_used_clus;
185
186 num_clusters++;
187 } while (clu != EXFAT_EOF_CLUSTER);
188 }
189
190 dec_used_clus:
191 sbi->used_clusters -= num_clusters;
192 return 0;
193 }
194
exfat_find_last_cluster(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_clu)195 int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
196 unsigned int *ret_clu)
197 {
198 unsigned int clu, next;
199 unsigned int count = 0;
200
201 next = p_chain->dir;
202 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
203 *ret_clu = next + p_chain->size - 1;
204 return 0;
205 }
206
207 do {
208 count++;
209 clu = next;
210 if (exfat_ent_get(sb, clu, &next))
211 return -EIO;
212 } while (next != EXFAT_EOF_CLUSTER);
213
214 if (p_chain->size != count) {
215 exfat_fs_error(sb,
216 "bogus directory size (clus : ondisk(%d) != counted(%d))",
217 p_chain->size, count);
218 return -EIO;
219 }
220
221 *ret_clu = clu;
222 return 0;
223 }
224
exfat_zeroed_cluster(struct inode * dir,unsigned int clu)225 int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
226 {
227 struct super_block *sb = dir->i_sb;
228 struct exfat_sb_info *sbi = EXFAT_SB(sb);
229 struct buffer_head *bh;
230 struct address_space *mapping = sb->s_bdev->bd_inode->i_mapping;
231 sector_t blknr, last_blknr, i;
232
233 blknr = exfat_cluster_to_sector(sbi, clu);
234 last_blknr = blknr + sbi->sect_per_clus;
235
236 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
237 exfat_fs_error_ratelimit(sb,
238 "%s: out of range(sect:%llu len:%u)",
239 __func__, (unsigned long long)blknr,
240 sbi->sect_per_clus);
241 return -EIO;
242 }
243
244 /* Zeroing the unused blocks on this cluster */
245 for (i = blknr; i < last_blknr; i++) {
246 bh = sb_getblk(sb, i);
247 if (!bh)
248 return -ENOMEM;
249
250 memset(bh->b_data, 0, sb->s_blocksize);
251 set_buffer_uptodate(bh);
252 mark_buffer_dirty(bh);
253 brelse(bh);
254 }
255 if (IS_DIRSYNC(dir))
256 return filemap_write_and_wait_range(mapping,
257 EXFAT_BLK_TO_B(blknr, sb),
258 EXFAT_BLK_TO_B(last_blknr, sb) - 1);
259
260 return 0;
261 }
262
exfat_alloc_cluster(struct inode * inode,unsigned int num_alloc,struct exfat_chain * p_chain,bool sync_bmap)263 int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
264 struct exfat_chain *p_chain, bool sync_bmap)
265 {
266 int ret = -ENOSPC;
267 unsigned int num_clusters = 0, total_cnt;
268 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
269 struct super_block *sb = inode->i_sb;
270 struct exfat_sb_info *sbi = EXFAT_SB(sb);
271
272 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
273
274 if (unlikely(total_cnt < sbi->used_clusters)) {
275 exfat_fs_error_ratelimit(sb,
276 "%s: invalid used clusters(t:%u,u:%u)\n",
277 __func__, total_cnt, sbi->used_clusters);
278 return -EIO;
279 }
280
281 if (num_alloc > total_cnt - sbi->used_clusters)
282 return -ENOSPC;
283
284 hint_clu = p_chain->dir;
285 /* find new cluster */
286 if (hint_clu == EXFAT_EOF_CLUSTER) {
287 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
288 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
289 sbi->clu_srch_ptr);
290 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
291 }
292
293 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
294 if (hint_clu == EXFAT_EOF_CLUSTER)
295 return -ENOSPC;
296 }
297
298 /* check cluster validation */
299 if (!is_valid_cluster(sbi, hint_clu)) {
300 exfat_err(sb, "hint_cluster is invalid (%u)",
301 hint_clu);
302 hint_clu = EXFAT_FIRST_CLUSTER;
303 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
304 if (exfat_chain_cont_cluster(sb, p_chain->dir,
305 num_clusters))
306 return -EIO;
307 p_chain->flags = ALLOC_FAT_CHAIN;
308 }
309 }
310
311 p_chain->dir = EXFAT_EOF_CLUSTER;
312
313 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
314 EXFAT_EOF_CLUSTER) {
315 if (new_clu != hint_clu &&
316 p_chain->flags == ALLOC_NO_FAT_CHAIN) {
317 if (exfat_chain_cont_cluster(sb, p_chain->dir,
318 num_clusters)) {
319 ret = -EIO;
320 goto free_cluster;
321 }
322 p_chain->flags = ALLOC_FAT_CHAIN;
323 }
324
325 /* update allocation bitmap */
326 if (exfat_set_bitmap(inode, new_clu, sync_bmap)) {
327 ret = -EIO;
328 goto free_cluster;
329 }
330
331 num_clusters++;
332
333 /* update FAT table */
334 if (p_chain->flags == ALLOC_FAT_CHAIN) {
335 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
336 ret = -EIO;
337 goto free_cluster;
338 }
339 }
340
341 if (p_chain->dir == EXFAT_EOF_CLUSTER) {
342 p_chain->dir = new_clu;
343 } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
344 if (exfat_ent_set(sb, last_clu, new_clu)) {
345 ret = -EIO;
346 goto free_cluster;
347 }
348 }
349 last_clu = new_clu;
350
351 if (--num_alloc == 0) {
352 sbi->clu_srch_ptr = hint_clu;
353 sbi->used_clusters += num_clusters;
354
355 p_chain->size += num_clusters;
356 return 0;
357 }
358
359 hint_clu = new_clu + 1;
360 if (hint_clu >= sbi->num_clusters) {
361 hint_clu = EXFAT_FIRST_CLUSTER;
362
363 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
364 if (exfat_chain_cont_cluster(sb, p_chain->dir,
365 num_clusters)) {
366 ret = -EIO;
367 goto free_cluster;
368 }
369 p_chain->flags = ALLOC_FAT_CHAIN;
370 }
371 }
372 }
373 free_cluster:
374 if (num_clusters)
375 exfat_free_cluster(inode, p_chain);
376 return ret;
377 }
378
exfat_count_num_clusters(struct super_block * sb,struct exfat_chain * p_chain,unsigned int * ret_count)379 int exfat_count_num_clusters(struct super_block *sb,
380 struct exfat_chain *p_chain, unsigned int *ret_count)
381 {
382 unsigned int i, count;
383 unsigned int clu;
384 struct exfat_sb_info *sbi = EXFAT_SB(sb);
385
386 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
387 *ret_count = 0;
388 return 0;
389 }
390
391 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
392 *ret_count = p_chain->size;
393 return 0;
394 }
395
396 clu = p_chain->dir;
397 count = 0;
398 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
399 count++;
400 if (exfat_ent_get(sb, clu, &clu))
401 return -EIO;
402 if (clu == EXFAT_EOF_CLUSTER)
403 break;
404 }
405
406 *ret_count = count;
407 return 0;
408 }
409