]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/exfat/fatent.c
exfat: add error check when updating dir-entries
[mirror_ubuntu-hirsute-kernel.git] / fs / exfat / fatent.c
CommitLineData
31023864
NJ
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
10#include "exfat_raw.h"
11#include "exfat_fs.h"
12
13static int exfat_mirror_bh(struct super_block *sb, sector_t sec,
14 struct buffer_head *bh)
15{
16 struct buffer_head *c_bh;
17 struct exfat_sb_info *sbi = EXFAT_SB(sb);
18 sector_t sec2;
19 int err = 0;
20
21 if (sbi->FAT2_start_sector != sbi->FAT1_start_sector) {
22 sec2 = sec - sbi->FAT1_start_sector + sbi->FAT2_start_sector;
23 c_bh = sb_getblk(sb, sec2);
24 if (!c_bh)
25 return -ENOMEM;
26 memcpy(c_bh->b_data, bh->b_data, sb->s_blocksize);
27 set_buffer_uptodate(c_bh);
28 mark_buffer_dirty(c_bh);
29 if (sb->s_flags & SB_SYNCHRONOUS)
30 err = sync_dirty_buffer(c_bh);
31 brelse(c_bh);
32 }
33
34 return err;
35}
36
37static int __exfat_ent_get(struct super_block *sb, unsigned int loc,
38 unsigned int *content)
39{
40 unsigned int off;
41 sector_t sec;
42 struct buffer_head *bh;
43
44 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
45 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
46
47 bh = sb_bread(sb, sec);
48 if (!bh)
49 return -EIO;
50
51 *content = le32_to_cpu(*(__le32 *)(&bh->b_data[off]));
52
53 /* remap reserved clusters to simplify code */
54 if (*content > EXFAT_BAD_CLUSTER)
55 *content = EXFAT_EOF_CLUSTER;
56
57 brelse(bh);
58 return 0;
59}
60
61int exfat_ent_set(struct super_block *sb, unsigned int loc,
62 unsigned int content)
63{
64 unsigned int off;
65 sector_t sec;
66 __le32 *fat_entry;
67 struct buffer_head *bh;
68
69 sec = FAT_ENT_OFFSET_SECTOR(sb, loc);
70 off = FAT_ENT_OFFSET_BYTE_IN_SECTOR(sb, loc);
71
72 bh = sb_bread(sb, sec);
73 if (!bh)
74 return -EIO;
75
76 fat_entry = (__le32 *)&(bh->b_data[off]);
77 *fat_entry = cpu_to_le32(content);
2c7f8937 78 exfat_update_bh(bh, sb->s_flags & SB_SYNCHRONOUS);
31023864
NJ
79 exfat_mirror_bh(sb, sec, bh);
80 brelse(bh);
81 return 0;
82}
83
84static inline bool is_valid_cluster(struct exfat_sb_info *sbi,
85 unsigned int clus)
86{
87 if (clus < EXFAT_FIRST_CLUSTER || sbi->num_clusters <= clus)
88 return false;
89 return true;
90}
91
92int exfat_ent_get(struct super_block *sb, unsigned int loc,
93 unsigned int *content)
94{
95 struct exfat_sb_info *sbi = EXFAT_SB(sb);
96 int err;
97
98 if (!is_valid_cluster(sbi, loc)) {
99 exfat_fs_error(sb, "invalid access to FAT (entry 0x%08x)",
100 loc);
101 return -EIO;
102 }
103
104 err = __exfat_ent_get(sb, loc, content);
105 if (err) {
106 exfat_fs_error(sb,
107 "failed to access to FAT (entry 0x%08x, err:%d)",
108 loc, err);
109 return err;
110 }
111
112 if (*content == EXFAT_FREE_CLUSTER) {
113 exfat_fs_error(sb,
114 "invalid access to FAT free cluster (entry 0x%08x)",
115 loc);
116 return -EIO;
117 }
118
119 if (*content == EXFAT_BAD_CLUSTER) {
120 exfat_fs_error(sb,
121 "invalid access to FAT bad cluster (entry 0x%08x)",
122 loc);
123 return -EIO;
124 }
125
126 if (*content != EXFAT_EOF_CLUSTER && !is_valid_cluster(sbi, *content)) {
127 exfat_fs_error(sb,
128 "invalid access to FAT (entry 0x%08x) bogus content (0x%08x)",
129 loc, *content);
130 return -EIO;
131 }
132
133 return 0;
134}
135
136int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,
137 unsigned int len)
138{
139 if (!len)
140 return 0;
141
142 while (len > 1) {
143 if (exfat_ent_set(sb, chain, chain + 1))
144 return -EIO;
145 chain++;
146 len--;
147 }
148
149 if (exfat_ent_set(sb, chain, EXFAT_EOF_CLUSTER))
150 return -EIO;
151 return 0;
152}
153
154int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)
155{
156 unsigned int num_clusters = 0;
157 unsigned int clu;
158 struct super_block *sb = inode->i_sb;
159 struct exfat_sb_info *sbi = EXFAT_SB(sb);
160
161 /* invalid cluster number */
162 if (p_chain->dir == EXFAT_FREE_CLUSTER ||
163 p_chain->dir == EXFAT_EOF_CLUSTER ||
164 p_chain->dir < EXFAT_FIRST_CLUSTER)
165 return 0;
166
167 /* no cluster to truncate */
168 if (p_chain->size == 0)
169 return 0;
170
171 /* check cluster validation */
a949824f 172 if (!is_valid_cluster(sbi, p_chain->dir)) {
d1727d55 173 exfat_err(sb, "invalid start cluster (%u)", p_chain->dir);
31023864
NJ
174 return -EIO;
175 }
176
31023864
NJ
177 clu = p_chain->dir;
178
179 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
180 do {
181 exfat_clear_bitmap(inode, clu);
182 clu++;
183
184 num_clusters++;
185 } while (num_clusters < p_chain->size);
186 } else {
187 do {
188 exfat_clear_bitmap(inode, clu);
189
190 if (exfat_get_next_cluster(sb, &clu))
191 goto dec_used_clus;
192
193 num_clusters++;
194 } while (clu != EXFAT_EOF_CLUSTER);
195 }
196
197dec_used_clus:
198 sbi->used_clusters -= num_clusters;
199 return 0;
200}
201
202int exfat_find_last_cluster(struct super_block *sb, struct exfat_chain *p_chain,
203 unsigned int *ret_clu)
204{
205 unsigned int clu, next;
206 unsigned int count = 0;
207
208 next = p_chain->dir;
209 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
210 *ret_clu = next + p_chain->size - 1;
211 return 0;
212 }
213
214 do {
215 count++;
216 clu = next;
217 if (exfat_ent_get(sb, clu, &next))
218 return -EIO;
219 } while (next != EXFAT_EOF_CLUSTER);
220
221 if (p_chain->size != count) {
222 exfat_fs_error(sb,
223 "bogus directory size (clus : ondisk(%d) != counted(%d))",
224 p_chain->size, count);
225 return -EIO;
226 }
227
228 *ret_clu = clu;
229 return 0;
230}
231
232static inline int exfat_sync_bhs(struct buffer_head **bhs, int nr_bhs)
233{
234 int i, err = 0;
235
236 for (i = 0; i < nr_bhs; i++)
237 write_dirty_buffer(bhs[i], 0);
238
239 for (i = 0; i < nr_bhs; i++) {
240 wait_on_buffer(bhs[i]);
241 if (!err && !buffer_uptodate(bhs[i]))
242 err = -EIO;
243 }
244 return err;
245}
246
247int exfat_zeroed_cluster(struct inode *dir, unsigned int clu)
248{
249 struct super_block *sb = dir->i_sb;
250 struct exfat_sb_info *sbi = EXFAT_SB(sb);
251 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
252 int nr_bhs = MAX_BUF_PER_PAGE;
253 sector_t blknr, last_blknr;
254 int err, i, n;
255
256 blknr = exfat_cluster_to_sector(sbi, clu);
257 last_blknr = blknr + sbi->sect_per_clus;
258
259 if (last_blknr > sbi->num_sectors && sbi->num_sectors > 0) {
260 exfat_fs_error_ratelimit(sb,
261 "%s: out of range(sect:%llu len:%u)",
262 __func__, (unsigned long long)blknr,
263 sbi->sect_per_clus);
264 return -EIO;
265 }
266
267 /* Zeroing the unused blocks on this cluster */
268 n = 0;
269 while (blknr < last_blknr) {
270 bhs[n] = sb_getblk(sb, blknr);
271 if (!bhs[n]) {
272 err = -ENOMEM;
273 goto release_bhs;
274 }
275 memset(bhs[n]->b_data, 0, sb->s_blocksize);
2c7f8937 276 exfat_update_bh(bhs[n], 0);
31023864
NJ
277
278 n++;
279 blknr++;
280
281 if (n == nr_bhs) {
282 if (IS_DIRSYNC(dir)) {
283 err = exfat_sync_bhs(bhs, n);
284 if (err)
285 goto release_bhs;
286 }
287
288 for (i = 0; i < n; i++)
289 brelse(bhs[i]);
290 n = 0;
291 }
292 }
293
294 if (IS_DIRSYNC(dir)) {
295 err = exfat_sync_bhs(bhs, n);
296 if (err)
297 goto release_bhs;
298 }
299
300 for (i = 0; i < n; i++)
301 brelse(bhs[i]);
302
303 return 0;
304
305release_bhs:
d1727d55 306 exfat_err(sb, "failed zeroed sect %llu\n", (unsigned long long)blknr);
31023864
NJ
307 for (i = 0; i < n; i++)
308 bforget(bhs[i]);
309 return err;
310}
311
312int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,
313 struct exfat_chain *p_chain)
314{
315 int ret = -ENOSPC;
316 unsigned int num_clusters = 0, total_cnt;
317 unsigned int hint_clu, new_clu, last_clu = EXFAT_EOF_CLUSTER;
318 struct super_block *sb = inode->i_sb;
319 struct exfat_sb_info *sbi = EXFAT_SB(sb);
320
321 total_cnt = EXFAT_DATA_CLUSTER_COUNT(sbi);
322
323 if (unlikely(total_cnt < sbi->used_clusters)) {
324 exfat_fs_error_ratelimit(sb,
325 "%s: invalid used clusters(t:%u,u:%u)\n",
326 __func__, total_cnt, sbi->used_clusters);
327 return -EIO;
328 }
329
330 if (num_alloc > total_cnt - sbi->used_clusters)
331 return -ENOSPC;
332
333 hint_clu = p_chain->dir;
334 /* find new cluster */
335 if (hint_clu == EXFAT_EOF_CLUSTER) {
336 if (sbi->clu_srch_ptr < EXFAT_FIRST_CLUSTER) {
d1727d55
JP
337 exfat_err(sb, "sbi->clu_srch_ptr is invalid (%u)\n",
338 sbi->clu_srch_ptr);
31023864
NJ
339 sbi->clu_srch_ptr = EXFAT_FIRST_CLUSTER;
340 }
341
342 hint_clu = exfat_find_free_bitmap(sb, sbi->clu_srch_ptr);
343 if (hint_clu == EXFAT_EOF_CLUSTER)
344 return -ENOSPC;
345 }
346
347 /* check cluster validation */
a949824f 348 if (!is_valid_cluster(sbi, hint_clu)) {
d1727d55 349 exfat_err(sb, "hint_cluster is invalid (%u)",
31023864
NJ
350 hint_clu);
351 hint_clu = EXFAT_FIRST_CLUSTER;
352 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
353 if (exfat_chain_cont_cluster(sb, p_chain->dir,
354 num_clusters))
355 return -EIO;
356 p_chain->flags = ALLOC_FAT_CHAIN;
357 }
358 }
359
31023864
NJ
360 p_chain->dir = EXFAT_EOF_CLUSTER;
361
362 while ((new_clu = exfat_find_free_bitmap(sb, hint_clu)) !=
363 EXFAT_EOF_CLUSTER) {
364 if (new_clu != hint_clu &&
365 p_chain->flags == ALLOC_NO_FAT_CHAIN) {
366 if (exfat_chain_cont_cluster(sb, p_chain->dir,
367 num_clusters)) {
368 ret = -EIO;
369 goto free_cluster;
370 }
371 p_chain->flags = ALLOC_FAT_CHAIN;
372 }
373
374 /* update allocation bitmap */
375 if (exfat_set_bitmap(inode, new_clu)) {
376 ret = -EIO;
377 goto free_cluster;
378 }
379
380 num_clusters++;
381
382 /* update FAT table */
383 if (p_chain->flags == ALLOC_FAT_CHAIN) {
384 if (exfat_ent_set(sb, new_clu, EXFAT_EOF_CLUSTER)) {
385 ret = -EIO;
386 goto free_cluster;
387 }
388 }
389
390 if (p_chain->dir == EXFAT_EOF_CLUSTER) {
391 p_chain->dir = new_clu;
392 } else if (p_chain->flags == ALLOC_FAT_CHAIN) {
393 if (exfat_ent_set(sb, last_clu, new_clu)) {
394 ret = -EIO;
395 goto free_cluster;
396 }
397 }
398 last_clu = new_clu;
399
400 if (--num_alloc == 0) {
401 sbi->clu_srch_ptr = hint_clu;
402 sbi->used_clusters += num_clusters;
403
404 p_chain->size += num_clusters;
405 return 0;
406 }
407
408 hint_clu = new_clu + 1;
409 if (hint_clu >= sbi->num_clusters) {
410 hint_clu = EXFAT_FIRST_CLUSTER;
411
412 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
413 if (exfat_chain_cont_cluster(sb, p_chain->dir,
414 num_clusters)) {
415 ret = -EIO;
416 goto free_cluster;
417 }
418 p_chain->flags = ALLOC_FAT_CHAIN;
419 }
420 }
421 }
422free_cluster:
423 if (num_clusters)
424 exfat_free_cluster(inode, p_chain);
425 return ret;
426}
427
428int exfat_count_num_clusters(struct super_block *sb,
429 struct exfat_chain *p_chain, unsigned int *ret_count)
430{
431 unsigned int i, count;
432 unsigned int clu;
433 struct exfat_sb_info *sbi = EXFAT_SB(sb);
434
435 if (!p_chain->dir || p_chain->dir == EXFAT_EOF_CLUSTER) {
436 *ret_count = 0;
437 return 0;
438 }
439
440 if (p_chain->flags == ALLOC_NO_FAT_CHAIN) {
441 *ret_count = p_chain->size;
442 return 0;
443 }
444
445 clu = p_chain->dir;
446 count = 0;
447 for (i = EXFAT_FIRST_CLUSTER; i < sbi->num_clusters; i++) {
448 count++;
449 if (exfat_ent_get(sb, clu, &clu))
450 return -EIO;
451 if (clu == EXFAT_EOF_CLUSTER)
452 break;
453 }
454
455 *ret_count = count;
456 return 0;
457}