]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/staging/exfat/exfat_core.c
staging: exfat: fix multiple definition error of `rename_file'
[mirror_ubuntu-jammy-kernel.git] / drivers / staging / exfat / exfat_core.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
4 */
5
6 #include <linux/types.h>
7 #include <linux/buffer_head.h>
8 #include <linux/fs.h>
9 #include <linux/mutex.h>
10 #include <linux/blkdev.h>
11 #include <linux/slab.h>
12 #include "exfat.h"
13
14 static void __set_sb_dirty(struct super_block *sb)
15 {
16 struct exfat_sb_info *sbi = EXFAT_SB(sb);
17
18 sbi->s_dirt = 1;
19 }
20
21 static u8 name_buf[MAX_PATH_LENGTH * MAX_CHARSET_SIZE];
22
23 static u8 free_bit[] = {
24 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 0 ~ 19 */
25 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, /* 20 ~ 39 */
26 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 40 ~ 59 */
27 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 60 ~ 79 */
28 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, /* 80 ~ 99 */
29 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, /* 100 ~ 119 */
30 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 120 ~ 139 */
31 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, /* 140 ~ 159 */
32 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 160 ~ 179 */
33 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, /* 180 ~ 199 */
34 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 200 ~ 219 */
35 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 220 ~ 239 */
36 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /* 240 ~ 254 */
37 };
38
39 static u8 used_bit[] = {
40 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, /* 0 ~ 19 */
41 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, /* 20 ~ 39 */
42 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, /* 40 ~ 59 */
43 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /* 60 ~ 79 */
44 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, /* 80 ~ 99 */
45 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, /* 100 ~ 119 */
46 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, /* 120 ~ 139 */
47 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 140 ~ 159 */
48 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, /* 160 ~ 179 */
49 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, /* 180 ~ 199 */
50 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, /* 200 ~ 219 */
51 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 220 ~ 239 */
52 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /* 240 ~ 255 */
53 };
54
55 #define BITMAP_LOC(v) ((v) >> 3)
56 #define BITMAP_SHIFT(v) ((v) & 0x07)
57
58 static inline s32 exfat_bitmap_test(u8 *bitmap, int i)
59 {
60 u8 data;
61
62 data = bitmap[BITMAP_LOC(i)];
63 if ((data >> BITMAP_SHIFT(i)) & 0x01)
64 return 1;
65 return 0;
66 }
67
68 static inline void exfat_bitmap_set(u8 *bitmap, int i)
69 {
70 bitmap[BITMAP_LOC(i)] |= (0x01 << BITMAP_SHIFT(i));
71 }
72
73 static inline void exfat_bitmap_clear(u8 *bitmap, int i)
74 {
75 bitmap[BITMAP_LOC(i)] &= ~(0x01 << BITMAP_SHIFT(i));
76 }
77
78 /*
79 * File System Management Functions
80 */
81
82 void fs_set_vol_flags(struct super_block *sb, u32 new_flag)
83 {
84 struct pbr_sector_t *p_pbr;
85 struct bpbex_t *p_bpb;
86 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
87
88 if (p_fs->vol_flag == new_flag)
89 return;
90
91 p_fs->vol_flag = new_flag;
92
93 if (!p_fs->pbr_bh) {
94 if (sector_read(sb, p_fs->PBR_sector,
95 &p_fs->pbr_bh, 1) != 0)
96 return;
97 }
98
99 p_pbr = (struct pbr_sector_t *)p_fs->pbr_bh->b_data;
100 p_bpb = (struct bpbex_t *)p_pbr->bpb;
101 SET16(p_bpb->vol_flags, (u16)new_flag);
102
103 /* XXX duyoung
104 * what can we do here? (cuz fs_set_vol_flags() is void)
105 */
106 if ((new_flag == VOL_DIRTY) && (!buffer_dirty(p_fs->pbr_bh)))
107 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 1);
108 else
109 sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 0);
110 }
111
112 void fs_error(struct super_block *sb)
113 {
114 struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
115
116 if (opts->errors == EXFAT_ERRORS_PANIC) {
117 panic("[EXFAT] Filesystem panic from previous error\n");
118 } else if ((opts->errors == EXFAT_ERRORS_RO) && !sb_rdonly(sb)) {
119 sb->s_flags |= SB_RDONLY;
120 pr_err("[EXFAT] Filesystem has been set read-only\n");
121 }
122 }
123
124 /*
125 * Cluster Management Functions
126 */
127
128 static s32 clear_cluster(struct super_block *sb, u32 clu)
129 {
130 sector_t s, n;
131 s32 ret = 0;
132 struct buffer_head *tmp_bh = NULL;
133 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
134 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
135
136 if (clu == CLUSTER_32(0)) { /* FAT16 root_dir */
137 s = p_fs->root_start_sector;
138 n = p_fs->data_start_sector;
139 } else {
140 s = START_SECTOR(clu);
141 n = s + p_fs->sectors_per_clu;
142 }
143
144 for (; s < n; s++) {
145 ret = sector_read(sb, s, &tmp_bh, 0);
146 if (ret != 0)
147 return ret;
148
149 memset((char *)tmp_bh->b_data, 0x0, p_bd->sector_size);
150 ret = sector_write(sb, s, tmp_bh, 0);
151 if (ret != 0)
152 break;
153 }
154
155 brelse(tmp_bh);
156 return ret;
157 }
158
159 static s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
160 {
161 int i, b;
162 sector_t sector;
163 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
164 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
165
166 i = clu >> (p_bd->sector_size_bits + 3);
167 b = clu & ((p_bd->sector_size << 3) - 1);
168
169 sector = START_SECTOR(p_fs->map_clu) + i;
170
171 exfat_bitmap_set((u8 *)p_fs->vol_amap[i]->b_data, b);
172
173 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
174 }
175
176 static s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
177 {
178 int i, b;
179 sector_t sector;
180 #ifdef CONFIG_EXFAT_DISCARD
181 struct exfat_sb_info *sbi = EXFAT_SB(sb);
182 struct exfat_mount_options *opts = &sbi->options;
183 int ret;
184 #endif /* CONFIG_EXFAT_DISCARD */
185 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
186 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
187
188 i = clu >> (p_bd->sector_size_bits + 3);
189 b = clu & ((p_bd->sector_size << 3) - 1);
190
191 sector = START_SECTOR(p_fs->map_clu) + i;
192
193 exfat_bitmap_clear((u8 *)p_fs->vol_amap[i]->b_data, b);
194
195 #ifdef CONFIG_EXFAT_DISCARD
196 if (opts->discard) {
197 ret = sb_issue_discard(sb, START_SECTOR(clu),
198 (1 << p_fs->sectors_per_clu_bits),
199 GFP_NOFS, 0);
200 if (ret == -EOPNOTSUPP) {
201 pr_warn("discard not supported by device, disabling");
202 opts->discard = 0;
203 } else {
204 return ret;
205 }
206 }
207 #endif /* CONFIG_EXFAT_DISCARD */
208
209 return sector_write(sb, sector, p_fs->vol_amap[i], 0);
210 }
211
212 static u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
213 {
214 int i, map_i, map_b;
215 u32 clu_base, clu_free;
216 u8 k, clu_mask;
217 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
218 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
219
220 clu_base = (clu & ~(0x7)) + 2;
221 clu_mask = (1 << (clu - clu_base + 2)) - 1;
222
223 map_i = clu >> (p_bd->sector_size_bits + 3);
224 map_b = (clu >> 3) & p_bd->sector_size_mask;
225
226 for (i = 2; i < p_fs->num_clusters; i += 8) {
227 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
228 if (clu_mask > 0) {
229 k |= clu_mask;
230 clu_mask = 0;
231 }
232 if (k < 0xFF) {
233 clu_free = clu_base + free_bit[k];
234 if (clu_free < p_fs->num_clusters)
235 return clu_free;
236 }
237 clu_base += 8;
238
239 if (((++map_b) >= p_bd->sector_size) ||
240 (clu_base >= p_fs->num_clusters)) {
241 if ((++map_i) >= p_fs->map_sectors) {
242 clu_base = 2;
243 map_i = 0;
244 }
245 map_b = 0;
246 }
247 }
248
249 return CLUSTER_32(~0);
250 }
251
252 static s32 exfat_alloc_cluster(struct super_block *sb, s32 num_alloc,
253 struct chain_t *p_chain)
254 {
255 s32 num_clusters = 0;
256 u32 hint_clu, new_clu, last_clu = CLUSTER_32(~0);
257 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
258
259 hint_clu = p_chain->dir;
260 if (hint_clu == CLUSTER_32(~0)) {
261 hint_clu = test_alloc_bitmap(sb, p_fs->clu_srch_ptr - 2);
262 if (hint_clu == CLUSTER_32(~0))
263 return 0;
264 } else if (hint_clu >= p_fs->num_clusters) {
265 hint_clu = 2;
266 p_chain->flags = 0x01;
267 }
268
269 __set_sb_dirty(sb);
270
271 p_chain->dir = CLUSTER_32(~0);
272
273 while ((new_clu = test_alloc_bitmap(sb, hint_clu - 2)) != CLUSTER_32(~0)) {
274 if (new_clu != hint_clu) {
275 if (p_chain->flags == 0x03) {
276 exfat_chain_cont_cluster(sb, p_chain->dir,
277 num_clusters);
278 p_chain->flags = 0x01;
279 }
280 }
281
282 if (set_alloc_bitmap(sb, new_clu - 2) != 0)
283 return -EIO;
284
285 num_clusters++;
286
287 if (p_chain->flags == 0x01) {
288 if (exfat_fat_write(sb, new_clu, CLUSTER_32(~0)) < 0)
289 return -EIO;
290 }
291
292 if (p_chain->dir == CLUSTER_32(~0)) {
293 p_chain->dir = new_clu;
294 } else {
295 if (p_chain->flags == 0x01) {
296 if (exfat_fat_write(sb, last_clu, new_clu) < 0)
297 return -EIO;
298 }
299 }
300 last_clu = new_clu;
301
302 if ((--num_alloc) == 0) {
303 p_fs->clu_srch_ptr = hint_clu;
304 if (p_fs->used_clusters != UINT_MAX)
305 p_fs->used_clusters += num_clusters;
306
307 p_chain->size += num_clusters;
308 return num_clusters;
309 }
310
311 hint_clu = new_clu + 1;
312 if (hint_clu >= p_fs->num_clusters) {
313 hint_clu = 2;
314
315 if (p_chain->flags == 0x03) {
316 exfat_chain_cont_cluster(sb, p_chain->dir,
317 num_clusters);
318 p_chain->flags = 0x01;
319 }
320 }
321 }
322
323 p_fs->clu_srch_ptr = hint_clu;
324 if (p_fs->used_clusters != UINT_MAX)
325 p_fs->used_clusters += num_clusters;
326
327 p_chain->size += num_clusters;
328 return num_clusters;
329 }
330
331 static void exfat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
332 s32 do_relse)
333 {
334 s32 num_clusters = 0;
335 u32 clu;
336 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
337 int i;
338 sector_t sector;
339
340 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
341 return;
342
343 if (p_chain->size <= 0) {
344 pr_err("[EXFAT] free_cluster : skip free-req clu:%u, because of zero-size truncation\n",
345 p_chain->dir);
346 return;
347 }
348
349 __set_sb_dirty(sb);
350 clu = p_chain->dir;
351
352 if (p_chain->flags == 0x03) {
353 do {
354 if (do_relse) {
355 sector = START_SECTOR(clu);
356 for (i = 0; i < p_fs->sectors_per_clu; i++)
357 exfat_buf_release(sb, sector + i);
358 }
359
360 if (clr_alloc_bitmap(sb, clu - 2) != 0)
361 break;
362 clu++;
363
364 num_clusters++;
365 } while (num_clusters < p_chain->size);
366 } else {
367 do {
368 if (p_fs->dev_ejected)
369 break;
370
371 if (do_relse) {
372 sector = START_SECTOR(clu);
373 for (i = 0; i < p_fs->sectors_per_clu; i++)
374 exfat_buf_release(sb, sector + i);
375 }
376
377 if (clr_alloc_bitmap(sb, clu - 2) != 0)
378 break;
379
380 if (exfat_fat_read(sb, clu, &clu) == -1)
381 break;
382 num_clusters++;
383 } while ((clu != CLUSTER_32(0)) && (clu != CLUSTER_32(~0)));
384 }
385
386 if (p_fs->used_clusters != UINT_MAX)
387 p_fs->used_clusters -= num_clusters;
388 }
389
390 static u32 find_last_cluster(struct super_block *sb, struct chain_t *p_chain)
391 {
392 u32 clu, next;
393 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
394
395 clu = p_chain->dir;
396
397 if (p_chain->flags == 0x03) {
398 clu += p_chain->size - 1;
399 } else {
400 while ((exfat_fat_read(sb, clu, &next) == 0) &&
401 (next != CLUSTER_32(~0))) {
402 if (p_fs->dev_ejected)
403 break;
404 clu = next;
405 }
406 }
407
408 return clu;
409 }
410
411 s32 count_num_clusters(struct super_block *sb, struct chain_t *p_chain)
412 {
413 int i, count = 0;
414 u32 clu;
415 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
416
417 if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
418 return 0;
419
420 clu = p_chain->dir;
421
422 if (p_chain->flags == 0x03) {
423 count = p_chain->size;
424 } else {
425 for (i = 2; i < p_fs->num_clusters; i++) {
426 count++;
427 if (exfat_fat_read(sb, clu, &clu) != 0)
428 return 0;
429 if (clu == CLUSTER_32(~0))
430 break;
431 }
432 }
433
434 return count;
435 }
436
437 static s32 exfat_count_used_clusters(struct super_block *sb)
438 {
439 int i, map_i, map_b, count = 0;
440 u8 k;
441 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
442 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
443
444 map_i = 0;
445 map_b = 0;
446
447 for (i = 2; i < p_fs->num_clusters; i += 8) {
448 k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
449 count += used_bit[k];
450
451 if ((++map_b) >= p_bd->sector_size) {
452 map_i++;
453 map_b = 0;
454 }
455 }
456
457 return count;
458 }
459
460 void exfat_chain_cont_cluster(struct super_block *sb, u32 chain, s32 len)
461 {
462 if (len == 0)
463 return;
464
465 while (len > 1) {
466 if (exfat_fat_write(sb, chain, chain + 1) < 0)
467 break;
468 chain++;
469 len--;
470 }
471 exfat_fat_write(sb, chain, CLUSTER_32(~0));
472 }
473
474 /*
475 * Allocation Bitmap Management Functions
476 */
477
478 s32 load_alloc_bitmap(struct super_block *sb)
479 {
480 int i, j, ret;
481 u32 map_size;
482 u32 type;
483 sector_t sector;
484 struct chain_t clu;
485 struct bmap_dentry_t *ep;
486 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
487 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
488
489 clu.dir = p_fs->root_dir;
490 clu.flags = 0x01;
491
492 while (clu.dir != CLUSTER_32(~0)) {
493 if (p_fs->dev_ejected)
494 break;
495
496 for (i = 0; i < p_fs->dentries_per_clu; i++) {
497 ep = (struct bmap_dentry_t *)get_entry_in_dir(sb, &clu,
498 i, NULL);
499 if (!ep)
500 return -ENOENT;
501
502 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
503
504 if (type == TYPE_UNUSED)
505 break;
506 if (type != TYPE_BITMAP)
507 continue;
508
509 if (ep->flags == 0x0) {
510 p_fs->map_clu = GET32_A(ep->start_clu);
511 map_size = (u32)GET64_A(ep->size);
512
513 p_fs->map_sectors = ((map_size - 1) >> p_bd->sector_size_bits) + 1;
514
515 p_fs->vol_amap = kmalloc_array(p_fs->map_sectors,
516 sizeof(struct buffer_head *),
517 GFP_KERNEL);
518 if (!p_fs->vol_amap)
519 return -ENOMEM;
520
521 sector = START_SECTOR(p_fs->map_clu);
522
523 for (j = 0; j < p_fs->map_sectors; j++) {
524 p_fs->vol_amap[j] = NULL;
525 ret = sector_read(sb, sector + j, &p_fs->vol_amap[j], 1);
526 if (ret != 0) {
527 /* release all buffers and free vol_amap */
528 i = 0;
529 while (i < j)
530 brelse(p_fs->vol_amap[i++]);
531
532 kfree(p_fs->vol_amap);
533 p_fs->vol_amap = NULL;
534 return ret;
535 }
536 }
537
538 p_fs->pbr_bh = NULL;
539 return 0;
540 }
541 }
542
543 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
544 return -EIO;
545 }
546
547 return -EFSCORRUPTED;
548 }
549
550 void free_alloc_bitmap(struct super_block *sb)
551 {
552 int i;
553 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
554
555 brelse(p_fs->pbr_bh);
556
557 for (i = 0; i < p_fs->map_sectors; i++)
558 __brelse(p_fs->vol_amap[i]);
559
560 kfree(p_fs->vol_amap);
561 p_fs->vol_amap = NULL;
562 }
563
564 void sync_alloc_bitmap(struct super_block *sb)
565 {
566 int i;
567 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
568
569 if (!p_fs->vol_amap)
570 return;
571
572 for (i = 0; i < p_fs->map_sectors; i++)
573 sync_dirty_buffer(p_fs->vol_amap[i]);
574 }
575
576 /*
577 * Upcase table Management Functions
578 */
579 static s32 __load_upcase_table(struct super_block *sb, sector_t sector,
580 u32 num_sectors, u32 utbl_checksum)
581 {
582 int i, ret = -EINVAL;
583 u32 j;
584 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
585 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
586 struct buffer_head *tmp_bh = NULL;
587 sector_t end_sector = num_sectors + sector;
588
589 bool skip = false;
590 u32 index = 0;
591 u16 uni = 0;
592 u16 **upcase_table;
593
594 u32 checksum = 0;
595
596 upcase_table = kmalloc_array(UTBL_COL_COUNT, sizeof(u16 *), GFP_KERNEL);
597 p_fs->vol_utbl = upcase_table;
598 if (!upcase_table)
599 return -ENOMEM;
600 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
601
602 while (sector < end_sector) {
603 ret = sector_read(sb, sector, &tmp_bh, 1);
604 if (ret != 0) {
605 pr_debug("sector read (0x%llX)fail\n",
606 (unsigned long long)sector);
607 goto error;
608 }
609 sector++;
610
611 for (i = 0; i < p_bd->sector_size && index <= 0xFFFF; i += 2) {
612 uni = GET16(((u8 *)tmp_bh->b_data) + i);
613
614 checksum = ((checksum & 1) ? 0x80000000 : 0) +
615 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
616 i);
617 checksum = ((checksum & 1) ? 0x80000000 : 0) +
618 (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
619 (i + 1));
620
621 if (skip) {
622 pr_debug("skip from 0x%X ", index);
623 index += uni;
624 pr_debug("to 0x%X (amount of 0x%X)\n",
625 index, uni);
626 skip = false;
627 } else if (uni == index) {
628 index++;
629 } else if (uni == 0xFFFF) {
630 skip = true;
631 } else { /* uni != index , uni != 0xFFFF */
632 u16 col_index = get_col_index(index);
633
634 if (!upcase_table[col_index]) {
635 pr_debug("alloc = 0x%X\n", col_index);
636 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
637 sizeof(u16), GFP_KERNEL);
638 if (!upcase_table[col_index]) {
639 ret = -ENOMEM;
640 goto error;
641 }
642
643 for (j = 0; j < UTBL_ROW_COUNT; j++)
644 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
645 }
646
647 upcase_table[col_index][get_row_index(index)] = uni;
648 index++;
649 }
650 }
651 }
652 if (index >= 0xFFFF && utbl_checksum == checksum) {
653 if (tmp_bh)
654 brelse(tmp_bh);
655 return 0;
656 }
657 ret = -EINVAL;
658 error:
659 if (tmp_bh)
660 brelse(tmp_bh);
661 free_upcase_table(sb);
662 return ret;
663 }
664
665 static s32 __load_default_upcase_table(struct super_block *sb)
666 {
667 int i, ret = -EINVAL;
668 u32 j;
669 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
670
671 bool skip = false;
672 u32 index = 0;
673 u16 uni = 0;
674 u16 **upcase_table;
675
676 upcase_table = kmalloc_array(UTBL_COL_COUNT, sizeof(u16 *), GFP_KERNEL);
677 p_fs->vol_utbl = upcase_table;
678 if (!upcase_table)
679 return -ENOMEM;
680 memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
681
682 for (i = 0; index <= 0xFFFF && i < NUM_UPCASE * 2; i += 2) {
683 uni = GET16(uni_upcase + i);
684 if (skip) {
685 pr_debug("skip from 0x%X ", index);
686 index += uni;
687 pr_debug("to 0x%X (amount of 0x%X)\n", index, uni);
688 skip = false;
689 } else if (uni == index) {
690 index++;
691 } else if (uni == 0xFFFF) {
692 skip = true;
693 } else { /* uni != index , uni != 0xFFFF */
694 u16 col_index = get_col_index(index);
695
696 if (!upcase_table[col_index]) {
697 pr_debug("alloc = 0x%X\n", col_index);
698 upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
699 sizeof(u16),
700 GFP_KERNEL);
701 if (!upcase_table[col_index]) {
702 ret = -ENOMEM;
703 goto error;
704 }
705
706 for (j = 0; j < UTBL_ROW_COUNT; j++)
707 upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
708 }
709
710 upcase_table[col_index][get_row_index(index)] = uni;
711 index++;
712 }
713 }
714
715 if (index >= 0xFFFF)
716 return 0;
717
718 error:
719 /* FATAL error: default upcase table has error */
720 free_upcase_table(sb);
721 return ret;
722 }
723
724 s32 load_upcase_table(struct super_block *sb)
725 {
726 int i;
727 u32 tbl_clu, tbl_size;
728 sector_t sector;
729 u32 type, num_sectors;
730 struct chain_t clu;
731 struct case_dentry_t *ep;
732 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
733 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
734
735 clu.dir = p_fs->root_dir;
736 clu.flags = 0x01;
737
738 if (p_fs->dev_ejected)
739 return -EIO;
740
741 while (clu.dir != CLUSTER_32(~0)) {
742 for (i = 0; i < p_fs->dentries_per_clu; i++) {
743 ep = (struct case_dentry_t *)get_entry_in_dir(sb, &clu,
744 i, NULL);
745 if (!ep)
746 return -ENOENT;
747
748 type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
749
750 if (type == TYPE_UNUSED)
751 break;
752 if (type != TYPE_UPCASE)
753 continue;
754
755 tbl_clu = GET32_A(ep->start_clu);
756 tbl_size = (u32)GET64_A(ep->size);
757
758 sector = START_SECTOR(tbl_clu);
759 num_sectors = ((tbl_size - 1) >> p_bd->sector_size_bits) + 1;
760 if (__load_upcase_table(sb, sector, num_sectors,
761 GET32_A(ep->checksum)) != 0)
762 break;
763 return 0;
764 }
765 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
766 return -EIO;
767 }
768 /* load default upcase table */
769 return __load_default_upcase_table(sb);
770 }
771
772 void free_upcase_table(struct super_block *sb)
773 {
774 u32 i;
775 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
776 u16 **upcase_table;
777
778 upcase_table = p_fs->vol_utbl;
779 for (i = 0; i < UTBL_COL_COUNT; i++)
780 kfree(upcase_table[i]);
781
782 kfree(p_fs->vol_utbl);
783 p_fs->vol_utbl = NULL;
784 }
785
786 /*
787 * Directory Entry Management Functions
788 */
789
790 static u32 exfat_get_entry_type(struct dentry_t *p_entry)
791 {
792 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
793
794 if (ep->type == 0x0) {
795 return TYPE_UNUSED;
796 } else if (ep->type < 0x80) {
797 return TYPE_DELETED;
798 } else if (ep->type == 0x80) {
799 return TYPE_INVALID;
800 } else if (ep->type < 0xA0) {
801 if (ep->type == 0x81) {
802 return TYPE_BITMAP;
803 } else if (ep->type == 0x82) {
804 return TYPE_UPCASE;
805 } else if (ep->type == 0x83) {
806 return TYPE_VOLUME;
807 } else if (ep->type == 0x85) {
808 if (GET16_A(ep->attr) & ATTR_SUBDIR)
809 return TYPE_DIR;
810 else
811 return TYPE_FILE;
812 }
813 return TYPE_CRITICAL_PRI;
814 } else if (ep->type < 0xC0) {
815 if (ep->type == 0xA0)
816 return TYPE_GUID;
817 else if (ep->type == 0xA1)
818 return TYPE_PADDING;
819 else if (ep->type == 0xA2)
820 return TYPE_ACLTAB;
821 return TYPE_BENIGN_PRI;
822 } else if (ep->type < 0xE0) {
823 if (ep->type == 0xC0)
824 return TYPE_STREAM;
825 else if (ep->type == 0xC1)
826 return TYPE_EXTEND;
827 else if (ep->type == 0xC2)
828 return TYPE_ACL;
829 return TYPE_CRITICAL_SEC;
830 }
831
832 return TYPE_BENIGN_SEC;
833 }
834
835 static void exfat_set_entry_type(struct dentry_t *p_entry, u32 type)
836 {
837 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
838
839 if (type == TYPE_UNUSED) {
840 ep->type = 0x0;
841 } else if (type == TYPE_DELETED) {
842 ep->type &= ~0x80;
843 } else if (type == TYPE_STREAM) {
844 ep->type = 0xC0;
845 } else if (type == TYPE_EXTEND) {
846 ep->type = 0xC1;
847 } else if (type == TYPE_BITMAP) {
848 ep->type = 0x81;
849 } else if (type == TYPE_UPCASE) {
850 ep->type = 0x82;
851 } else if (type == TYPE_VOLUME) {
852 ep->type = 0x83;
853 } else if (type == TYPE_DIR) {
854 ep->type = 0x85;
855 SET16_A(ep->attr, ATTR_SUBDIR);
856 } else if (type == TYPE_FILE) {
857 ep->type = 0x85;
858 SET16_A(ep->attr, ATTR_ARCHIVE);
859 } else if (type == TYPE_SYMLINK) {
860 ep->type = 0x85;
861 SET16_A(ep->attr, ATTR_ARCHIVE | ATTR_SYMLINK);
862 }
863 }
864
865 static u32 exfat_get_entry_attr(struct dentry_t *p_entry)
866 {
867 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
868
869 return (u32)GET16_A(ep->attr);
870 }
871
872 static void exfat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
873 {
874 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
875
876 SET16_A(ep->attr, (u16)attr);
877 }
878
879 static u8 exfat_get_entry_flag(struct dentry_t *p_entry)
880 {
881 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
882
883 return ep->flags;
884 }
885
886 static void exfat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
887 {
888 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
889
890 ep->flags = flags;
891 }
892
893 static u32 exfat_get_entry_clu0(struct dentry_t *p_entry)
894 {
895 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
896
897 return GET32_A(ep->start_clu);
898 }
899
900 static void exfat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
901 {
902 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
903
904 SET32_A(ep->start_clu, start_clu);
905 }
906
907 static u64 exfat_get_entry_size(struct dentry_t *p_entry)
908 {
909 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
910
911 return GET64_A(ep->valid_size);
912 }
913
914 static void exfat_set_entry_size(struct dentry_t *p_entry, u64 size)
915 {
916 struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
917
918 SET64_A(ep->valid_size, size);
919 SET64_A(ep->size, size);
920 }
921
922 static void exfat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
923 u8 mode)
924 {
925 u16 t = 0x00, d = 0x21;
926 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
927
928 switch (mode) {
929 case TM_CREATE:
930 t = GET16_A(ep->create_time);
931 d = GET16_A(ep->create_date);
932 break;
933 case TM_MODIFY:
934 t = GET16_A(ep->modify_time);
935 d = GET16_A(ep->modify_date);
936 break;
937 case TM_ACCESS:
938 t = GET16_A(ep->access_time);
939 d = GET16_A(ep->access_date);
940 break;
941 }
942
943 tp->sec = (t & 0x001F) << 1;
944 tp->min = (t >> 5) & 0x003F;
945 tp->hour = (t >> 11);
946 tp->day = (d & 0x001F);
947 tp->mon = (d >> 5) & 0x000F;
948 tp->year = (d >> 9);
949 }
950
951 static void exfat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
952 u8 mode)
953 {
954 u16 t, d;
955 struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
956
957 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
958 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
959
960 switch (mode) {
961 case TM_CREATE:
962 SET16_A(ep->create_time, t);
963 SET16_A(ep->create_date, d);
964 break;
965 case TM_MODIFY:
966 SET16_A(ep->modify_time, t);
967 SET16_A(ep->modify_date, d);
968 break;
969 case TM_ACCESS:
970 SET16_A(ep->access_time, t);
971 SET16_A(ep->access_date, d);
972 break;
973 }
974 }
975
976 static void init_file_entry(struct file_dentry_t *ep, u32 type)
977 {
978 struct timestamp_t tm, *tp;
979
980 exfat_set_entry_type((struct dentry_t *)ep, type);
981
982 tp = tm_current(&tm);
983 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
984 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
985 exfat_set_entry_time((struct dentry_t *)ep, tp, TM_ACCESS);
986 ep->create_time_ms = 0;
987 ep->modify_time_ms = 0;
988 ep->access_time_ms = 0;
989 }
990
991 static void init_strm_entry(struct strm_dentry_t *ep, u8 flags, u32 start_clu, u64 size)
992 {
993 exfat_set_entry_type((struct dentry_t *)ep, TYPE_STREAM);
994 ep->flags = flags;
995 SET32_A(ep->start_clu, start_clu);
996 SET64_A(ep->valid_size, size);
997 SET64_A(ep->size, size);
998 }
999
1000 static void init_name_entry(struct name_dentry_t *ep, u16 *uniname)
1001 {
1002 int i;
1003
1004 exfat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1005 ep->flags = 0x0;
1006
1007 for (i = 0; i < 30; i++, i++) {
1008 SET16_A(ep->unicode_0_14 + i, *uniname);
1009 if (*uniname == 0x0)
1010 break;
1011 uniname++;
1012 }
1013 }
1014
1015 static s32 exfat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1016 s32 entry, u32 type, u32 start_clu, u64 size)
1017 {
1018 sector_t sector;
1019 u8 flags;
1020 struct file_dentry_t *file_ep;
1021 struct strm_dentry_t *strm_ep;
1022
1023 flags = (type == TYPE_FILE) ? 0x01 : 0x03;
1024
1025 /* we cannot use get_entry_set_in_dir here because file ep is not initialized yet */
1026 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1027 &sector);
1028 if (!file_ep)
1029 return -ENOENT;
1030
1031 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1032 &sector);
1033 if (!strm_ep)
1034 return -ENOENT;
1035
1036 init_file_entry(file_ep, type);
1037 exfat_buf_modify(sb, sector);
1038
1039 init_strm_entry(strm_ep, flags, start_clu, size);
1040 exfat_buf_modify(sb, sector);
1041
1042 return 0;
1043 }
1044
1045 static s32 exfat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1046 s32 entry, s32 num_entries,
1047 struct uni_name_t *p_uniname,
1048 struct dos_name_t *p_dosname)
1049 {
1050 int i;
1051 sector_t sector;
1052 u16 *uniname = p_uniname->name;
1053 struct file_dentry_t *file_ep;
1054 struct strm_dentry_t *strm_ep;
1055 struct name_dentry_t *name_ep;
1056
1057 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1058 &sector);
1059 if (!file_ep)
1060 return -ENOENT;
1061
1062 file_ep->num_ext = (u8)(num_entries - 1);
1063 exfat_buf_modify(sb, sector);
1064
1065 strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1066 &sector);
1067 if (!strm_ep)
1068 return -ENOENT;
1069
1070 strm_ep->name_len = p_uniname->name_len;
1071 SET16_A(strm_ep->name_hash, p_uniname->name_hash);
1072 exfat_buf_modify(sb, sector);
1073
1074 for (i = 2; i < num_entries; i++) {
1075 name_ep = (struct name_dentry_t *)get_entry_in_dir(sb, p_dir,
1076 entry + i,
1077 &sector);
1078 if (!name_ep)
1079 return -ENOENT;
1080
1081 init_name_entry(name_ep, uniname);
1082 exfat_buf_modify(sb, sector);
1083 uniname += 15;
1084 }
1085
1086 update_dir_checksum(sb, p_dir, entry);
1087
1088 return 0;
1089 }
1090
1091 static void exfat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1092 s32 entry, s32 order, s32 num_entries)
1093 {
1094 int i;
1095 sector_t sector;
1096 struct dentry_t *ep;
1097 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1098
1099 for (i = order; i < num_entries; i++) {
1100 ep = get_entry_in_dir(sb, p_dir, entry + i, &sector);
1101 if (!ep)
1102 return;
1103
1104 p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1105 exfat_buf_modify(sb, sector);
1106 }
1107 }
1108
1109 void update_dir_checksum(struct super_block *sb, struct chain_t *p_dir,
1110 s32 entry)
1111 {
1112 int i, num_entries;
1113 sector_t sector;
1114 u16 chksum;
1115 struct file_dentry_t *file_ep;
1116 struct dentry_t *ep;
1117
1118 file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1119 &sector);
1120 if (!file_ep)
1121 return;
1122
1123 exfat_buf_lock(sb, sector);
1124
1125 num_entries = (s32)file_ep->num_ext + 1;
1126 chksum = calc_checksum_2byte((void *)file_ep, DENTRY_SIZE, 0,
1127 CS_DIR_ENTRY);
1128
1129 for (i = 1; i < num_entries; i++) {
1130 ep = get_entry_in_dir(sb, p_dir, entry + i, NULL);
1131 if (!ep) {
1132 exfat_buf_unlock(sb, sector);
1133 return;
1134 }
1135
1136 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1137 CS_DEFAULT);
1138 }
1139
1140 SET16_A(file_ep->checksum, chksum);
1141 exfat_buf_modify(sb, sector);
1142 exfat_buf_unlock(sb, sector);
1143 }
1144
1145 static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
1146 struct entry_set_cache_t *es,
1147 sector_t sec, s32 off, u32 count)
1148 {
1149 s32 num_entries, buf_off = (off - es->offset);
1150 u32 remaining_byte_in_sector, copy_entries;
1151 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1152 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1153 u32 clu;
1154 u8 *buf, *esbuf = (u8 *)&es->__buf;
1155
1156 pr_debug("%s entered es %p sec %llu off %d count %d\n",
1157 __func__, es, (unsigned long long)sec, off, count);
1158 num_entries = count;
1159
1160 while (num_entries) {
1161 /* white per sector base */
1162 remaining_byte_in_sector = (1 << p_bd->sector_size_bits) - off;
1163 copy_entries = min_t(s32,
1164 remaining_byte_in_sector >> DENTRY_SIZE_BITS,
1165 num_entries);
1166 buf = exfat_buf_getblk(sb, sec);
1167 if (!buf)
1168 goto err_out;
1169 pr_debug("es->buf %p buf_off %u\n", esbuf, buf_off);
1170 pr_debug("copying %d entries from %p to sector %llu\n",
1171 copy_entries, (esbuf + buf_off),
1172 (unsigned long long)sec);
1173 memcpy(buf + off, esbuf + buf_off,
1174 copy_entries << DENTRY_SIZE_BITS);
1175 exfat_buf_modify(sb, sec);
1176 num_entries -= copy_entries;
1177
1178 if (num_entries) {
1179 /* get next sector */
1180 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1181 clu = GET_CLUSTER_FROM_SECTOR(sec);
1182 if (es->alloc_flag == 0x03) {
1183 clu++;
1184 } else {
1185 if (exfat_fat_read(sb, clu, &clu) == -1)
1186 goto err_out;
1187 }
1188 sec = START_SECTOR(clu);
1189 } else {
1190 sec++;
1191 }
1192 off = 0;
1193 buf_off += copy_entries << DENTRY_SIZE_BITS;
1194 }
1195 }
1196
1197 pr_debug("%s exited successfully\n", __func__);
1198 return 0;
1199 err_out:
1200 pr_debug("%s failed\n", __func__);
1201 return -EINVAL;
1202 }
1203
1204 /* write back all entries in entry set */
1205 static s32 write_whole_entry_set(struct super_block *sb, struct entry_set_cache_t *es)
1206 {
1207 return __write_partial_entries_in_entry_set(sb, es, es->sector,
1208 es->offset,
1209 es->num_entries);
1210 }
1211
1212 void update_dir_checksum_with_entry_set(struct super_block *sb,
1213 struct entry_set_cache_t *es)
1214 {
1215 struct dentry_t *ep;
1216 u16 chksum = 0;
1217 s32 chksum_type = CS_DIR_ENTRY, i;
1218
1219 ep = (struct dentry_t *)&es->__buf;
1220 for (i = 0; i < es->num_entries; i++) {
1221 pr_debug("%s ep %p\n", __func__, ep);
1222 chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1223 chksum_type);
1224 ep++;
1225 chksum_type = CS_DEFAULT;
1226 }
1227
1228 ep = (struct dentry_t *)&es->__buf;
1229 SET16_A(((struct file_dentry_t *)ep)->checksum, chksum);
1230 write_whole_entry_set(sb, es);
1231 }
1232
1233 static s32 _walk_fat_chain(struct super_block *sb, struct chain_t *p_dir,
1234 s32 byte_offset, u32 *clu)
1235 {
1236 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1237 s32 clu_offset;
1238 u32 cur_clu;
1239
1240 clu_offset = byte_offset >> p_fs->cluster_size_bits;
1241 cur_clu = p_dir->dir;
1242
1243 if (p_dir->flags == 0x03) {
1244 cur_clu += clu_offset;
1245 } else {
1246 while (clu_offset > 0) {
1247 if (exfat_fat_read(sb, cur_clu, &cur_clu) == -1)
1248 return -EIO;
1249 clu_offset--;
1250 }
1251 }
1252
1253 if (clu)
1254 *clu = cur_clu;
1255 return 0;
1256 }
1257
1258 static s32 find_location(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1259 sector_t *sector, s32 *offset)
1260 {
1261 s32 off, ret;
1262 u32 clu = 0;
1263 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1264 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1265
1266 off = entry << DENTRY_SIZE_BITS;
1267
1268 if (p_dir->dir == CLUSTER_32(0)) { /* FAT16 root_dir */
1269 *offset = off & p_bd->sector_size_mask;
1270 *sector = off >> p_bd->sector_size_bits;
1271 *sector += p_fs->root_start_sector;
1272 } else {
1273 ret = _walk_fat_chain(sb, p_dir, off, &clu);
1274 if (ret != 0)
1275 return ret;
1276
1277 /* byte offset in cluster */
1278 off &= p_fs->cluster_size - 1;
1279
1280 /* byte offset in sector */
1281 *offset = off & p_bd->sector_size_mask;
1282
1283 /* sector offset in cluster */
1284 *sector = off >> p_bd->sector_size_bits;
1285 *sector += START_SECTOR(clu);
1286 }
1287 return 0;
1288 }
1289
1290 struct dentry_t *get_entry_in_dir(struct super_block *sb, struct chain_t *p_dir,
1291 s32 entry, sector_t *sector)
1292 {
1293 s32 off;
1294 sector_t sec;
1295 u8 *buf;
1296
1297 if (find_location(sb, p_dir, entry, &sec, &off) != 0)
1298 return NULL;
1299
1300 buf = exfat_buf_getblk(sb, sec);
1301
1302 if (!buf)
1303 return NULL;
1304
1305 if (sector)
1306 *sector = sec;
1307 return (struct dentry_t *)(buf + off);
1308 }
1309
1310 /* returns a set of dentries for a file or dir.
1311 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
1312 * to apply changes made in this entry set to the real device.
1313 * in:
1314 * sb+p_dir+entry: indicates a file/dir
1315 * type: specifies how many dentries should be included.
1316 * out:
1317 * file_ep: will point the first dentry(= file dentry) on success
1318 * return:
1319 * pointer of entry set on success,
1320 * NULL on failure.
1321 */
1322
1323 #define ES_MODE_STARTED 0
1324 #define ES_MODE_GET_FILE_ENTRY 1
1325 #define ES_MODE_GET_STRM_ENTRY 2
1326 #define ES_MODE_GET_NAME_ENTRY 3
1327 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
1328 struct entry_set_cache_t *get_entry_set_in_dir(struct super_block *sb,
1329 struct chain_t *p_dir, s32 entry,
1330 u32 type,
1331 struct dentry_t **file_ep)
1332 {
1333 s32 off, ret, byte_offset;
1334 u32 clu = 0;
1335 sector_t sec;
1336 u32 entry_type;
1337 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1338 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1339 struct entry_set_cache_t *es = NULL;
1340 struct dentry_t *ep, *pos;
1341 u8 *buf;
1342 u8 num_entries;
1343 s32 mode = ES_MODE_STARTED;
1344 size_t bufsize;
1345
1346 pr_debug("%s entered p_dir dir %u flags %x size %d\n",
1347 __func__, p_dir->dir, p_dir->flags, p_dir->size);
1348
1349 byte_offset = entry << DENTRY_SIZE_BITS;
1350 ret = _walk_fat_chain(sb, p_dir, byte_offset, &clu);
1351 if (ret != 0)
1352 return NULL;
1353
1354 /* byte offset in cluster */
1355 byte_offset &= p_fs->cluster_size - 1;
1356
1357 /* byte offset in sector */
1358 off = byte_offset & p_bd->sector_size_mask;
1359
1360 /* sector offset in cluster */
1361 sec = byte_offset >> p_bd->sector_size_bits;
1362 sec += START_SECTOR(clu);
1363
1364 buf = exfat_buf_getblk(sb, sec);
1365 if (!buf)
1366 goto err_out;
1367
1368 ep = (struct dentry_t *)(buf + off);
1369 entry_type = p_fs->fs_func->get_entry_type(ep);
1370
1371 if ((entry_type != TYPE_FILE) && (entry_type != TYPE_DIR))
1372 goto err_out;
1373
1374 if (type == ES_ALL_ENTRIES)
1375 num_entries = ((struct file_dentry_t *)ep)->num_ext + 1;
1376 else
1377 num_entries = type;
1378
1379 bufsize = offsetof(struct entry_set_cache_t, __buf) + (num_entries) *
1380 sizeof(struct dentry_t);
1381 pr_debug("%s: trying to kmalloc %zx bytes for %d entries\n", __func__,
1382 bufsize, num_entries);
1383 es = kmalloc(bufsize, GFP_KERNEL);
1384 if (!es)
1385 goto err_out;
1386
1387 es->num_entries = num_entries;
1388 es->sector = sec;
1389 es->offset = off;
1390 es->alloc_flag = p_dir->flags;
1391
1392 pos = (struct dentry_t *)&es->__buf;
1393
1394 while (num_entries) {
1395 /*
1396 * instead of copying whole sector, we will check every entry.
1397 * this will provide minimum stablity and consistency.
1398 */
1399 entry_type = p_fs->fs_func->get_entry_type(ep);
1400
1401 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
1402 goto err_out;
1403
1404 switch (mode) {
1405 case ES_MODE_STARTED:
1406 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
1407 mode = ES_MODE_GET_FILE_ENTRY;
1408 else
1409 goto err_out;
1410 break;
1411 case ES_MODE_GET_FILE_ENTRY:
1412 if (entry_type == TYPE_STREAM)
1413 mode = ES_MODE_GET_STRM_ENTRY;
1414 else
1415 goto err_out;
1416 break;
1417 case ES_MODE_GET_STRM_ENTRY:
1418 if (entry_type == TYPE_EXTEND)
1419 mode = ES_MODE_GET_NAME_ENTRY;
1420 else
1421 goto err_out;
1422 break;
1423 case ES_MODE_GET_NAME_ENTRY:
1424 if (entry_type == TYPE_EXTEND)
1425 break;
1426 else if (entry_type == TYPE_STREAM)
1427 goto err_out;
1428 else if (entry_type & TYPE_CRITICAL_SEC)
1429 mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
1430 else
1431 goto err_out;
1432 break;
1433 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
1434 if ((entry_type == TYPE_EXTEND) ||
1435 (entry_type == TYPE_STREAM))
1436 goto err_out;
1437 else if ((entry_type & TYPE_CRITICAL_SEC) !=
1438 TYPE_CRITICAL_SEC)
1439 goto err_out;
1440 break;
1441 }
1442
1443 memcpy(pos, ep, sizeof(struct dentry_t));
1444
1445 if (--num_entries == 0)
1446 break;
1447
1448 if (((off + DENTRY_SIZE) & p_bd->sector_size_mask) <
1449 (off & p_bd->sector_size_mask)) {
1450 /* get the next sector */
1451 if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1452 if (es->alloc_flag == 0x03) {
1453 clu++;
1454 } else {
1455 if (exfat_fat_read(sb, clu, &clu) == -1)
1456 goto err_out;
1457 }
1458 sec = START_SECTOR(clu);
1459 } else {
1460 sec++;
1461 }
1462 buf = exfat_buf_getblk(sb, sec);
1463 if (!buf)
1464 goto err_out;
1465 off = 0;
1466 ep = (struct dentry_t *)(buf);
1467 } else {
1468 ep++;
1469 off += DENTRY_SIZE;
1470 }
1471 pos++;
1472 }
1473
1474 if (file_ep)
1475 *file_ep = (struct dentry_t *)&es->__buf;
1476
1477 pr_debug("%s exiting es %p sec %llu offset %d flags %d, num_entries %u buf ptr %p\n",
1478 __func__, es, (unsigned long long)es->sector, es->offset,
1479 es->alloc_flag, es->num_entries, &es->__buf);
1480 return es;
1481 err_out:
1482 pr_debug("%s exited NULL (es %p)\n", __func__, es);
1483 kfree(es);
1484 return NULL;
1485 }
1486
1487 void release_entry_set(struct entry_set_cache_t *es)
1488 {
1489 pr_debug("%s es=%p\n", __func__, es);
1490 kfree(es);
1491 }
1492
1493 /* search EMPTY CONTINUOUS "num_entries" entries */
1494 static s32 search_deleted_or_unused_entry(struct super_block *sb,
1495 struct chain_t *p_dir, s32 num_entries)
1496 {
1497 int i, dentry, num_empty = 0;
1498 s32 dentries_per_clu;
1499 u32 type;
1500 struct chain_t clu;
1501 struct dentry_t *ep;
1502 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1503
1504 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1505 dentries_per_clu = p_fs->dentries_in_root;
1506 else
1507 dentries_per_clu = p_fs->dentries_per_clu;
1508
1509 if (p_fs->hint_uentry.dir == p_dir->dir) {
1510 if (p_fs->hint_uentry.entry == -1)
1511 return -1;
1512
1513 clu.dir = p_fs->hint_uentry.clu.dir;
1514 clu.size = p_fs->hint_uentry.clu.size;
1515 clu.flags = p_fs->hint_uentry.clu.flags;
1516
1517 dentry = p_fs->hint_uentry.entry;
1518 } else {
1519 p_fs->hint_uentry.entry = -1;
1520
1521 clu.dir = p_dir->dir;
1522 clu.size = p_dir->size;
1523 clu.flags = p_dir->flags;
1524
1525 dentry = 0;
1526 }
1527
1528 while (clu.dir != CLUSTER_32(~0)) {
1529 if (p_fs->dev_ejected)
1530 break;
1531
1532 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1533 i = dentry % dentries_per_clu;
1534 else
1535 i = dentry & (dentries_per_clu - 1);
1536
1537 for (; i < dentries_per_clu; i++, dentry++) {
1538 ep = get_entry_in_dir(sb, &clu, i, NULL);
1539 if (!ep)
1540 return -1;
1541
1542 type = p_fs->fs_func->get_entry_type(ep);
1543
1544 if (type == TYPE_UNUSED) {
1545 num_empty++;
1546 if (p_fs->hint_uentry.entry == -1) {
1547 p_fs->hint_uentry.dir = p_dir->dir;
1548 p_fs->hint_uentry.entry = dentry;
1549
1550 p_fs->hint_uentry.clu.dir = clu.dir;
1551 p_fs->hint_uentry.clu.size = clu.size;
1552 p_fs->hint_uentry.clu.flags = clu.flags;
1553 }
1554 } else if (type == TYPE_DELETED) {
1555 num_empty++;
1556 } else {
1557 num_empty = 0;
1558 }
1559
1560 if (num_empty >= num_entries) {
1561 p_fs->hint_uentry.dir = CLUSTER_32(~0);
1562 p_fs->hint_uentry.entry = -1;
1563
1564 if (p_fs->vol_type == EXFAT)
1565 return dentry - (num_entries - 1);
1566 else
1567 return dentry;
1568 }
1569 }
1570
1571 if (p_dir->dir == CLUSTER_32(0))
1572 break; /* FAT16 root_dir */
1573
1574 if (clu.flags == 0x03) {
1575 if ((--clu.size) > 0)
1576 clu.dir++;
1577 else
1578 clu.dir = CLUSTER_32(~0);
1579 } else {
1580 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1581 return -1;
1582 }
1583 }
1584
1585 return -1;
1586 }
1587
1588 static s32 find_empty_entry(struct inode *inode, struct chain_t *p_dir, s32 num_entries)
1589 {
1590 s32 ret, dentry;
1591 u32 last_clu;
1592 sector_t sector;
1593 u64 size = 0;
1594 struct chain_t clu;
1595 struct dentry_t *ep = NULL;
1596 struct super_block *sb = inode->i_sb;
1597 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1598 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
1599
1600 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1601 return search_deleted_or_unused_entry(sb, p_dir, num_entries);
1602
1603 while ((dentry = search_deleted_or_unused_entry(sb, p_dir, num_entries)) < 0) {
1604 if (p_fs->dev_ejected)
1605 break;
1606
1607 if (p_dir->dir != p_fs->root_dir)
1608 size = i_size_read(inode);
1609
1610 last_clu = find_last_cluster(sb, p_dir);
1611 clu.dir = last_clu + 1;
1612 clu.size = 0;
1613 clu.flags = p_dir->flags;
1614
1615 /* (1) allocate a cluster */
1616 ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
1617 if (ret < 1)
1618 return -EIO;
1619
1620 if (clear_cluster(sb, clu.dir) != 0)
1621 return -EIO;
1622
1623 /* (2) append to the FAT chain */
1624 if (clu.flags != p_dir->flags) {
1625 exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
1626 p_dir->flags = 0x01;
1627 p_fs->hint_uentry.clu.flags = 0x01;
1628 }
1629 if (clu.flags == 0x01)
1630 if (exfat_fat_write(sb, last_clu, clu.dir) < 0)
1631 return -EIO;
1632
1633 if (p_fs->hint_uentry.entry == -1) {
1634 p_fs->hint_uentry.dir = p_dir->dir;
1635 p_fs->hint_uentry.entry = p_dir->size << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
1636
1637 p_fs->hint_uentry.clu.dir = clu.dir;
1638 p_fs->hint_uentry.clu.size = 0;
1639 p_fs->hint_uentry.clu.flags = clu.flags;
1640 }
1641 p_fs->hint_uentry.clu.size++;
1642 p_dir->size++;
1643
1644 /* (3) update the directory entry */
1645 if (p_dir->dir != p_fs->root_dir) {
1646 size += p_fs->cluster_size;
1647
1648 ep = get_entry_in_dir(sb, &fid->dir,
1649 fid->entry + 1, &sector);
1650 if (!ep)
1651 return -ENOENT;
1652 p_fs->fs_func->set_entry_size(ep, size);
1653 p_fs->fs_func->set_entry_flag(ep, p_dir->flags);
1654 exfat_buf_modify(sb, sector);
1655
1656 update_dir_checksum(sb, &fid->dir,
1657 fid->entry);
1658 }
1659
1660 i_size_write(inode, i_size_read(inode) + p_fs->cluster_size);
1661 EXFAT_I(inode)->mmu_private += p_fs->cluster_size;
1662 EXFAT_I(inode)->fid.size += p_fs->cluster_size;
1663 EXFAT_I(inode)->fid.flags = p_dir->flags;
1664 inode->i_blocks += 1 << (p_fs->cluster_size_bits - 9);
1665 }
1666
1667 return dentry;
1668 }
1669
1670 static s32 extract_uni_name_from_name_entry(struct name_dentry_t *ep, u16 *uniname,
1671 s32 order)
1672 {
1673 int i, len = 0;
1674
1675 for (i = 0; i < 30; i += 2) {
1676 *uniname = GET16_A(ep->unicode_0_14 + i);
1677 if (*uniname == 0x0)
1678 return len;
1679 uniname++;
1680 len++;
1681 }
1682
1683 *uniname = 0x0;
1684 return len;
1685 }
1686
1687 /* return values of exfat_find_dir_entry()
1688 * >= 0 : return dir entiry position with the name in dir
1689 * -1 : (root dir, ".") it is the root dir itself
1690 * -2 : entry with the name does not exist
1691 */
1692 static s32 exfat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1693 struct uni_name_t *p_uniname, s32 num_entries,
1694 struct dos_name_t *p_dosname, u32 type)
1695 {
1696 int i = 0, dentry = 0, num_ext_entries = 0, len, step;
1697 s32 order = 0;
1698 bool is_feasible_entry = false;
1699 s32 dentries_per_clu, num_empty = 0;
1700 u32 entry_type;
1701 u16 entry_uniname[16], *uniname = NULL, unichar;
1702 struct chain_t clu;
1703 struct dentry_t *ep;
1704 struct file_dentry_t *file_ep;
1705 struct strm_dentry_t *strm_ep;
1706 struct name_dentry_t *name_ep;
1707 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1708
1709 if (p_dir->dir == p_fs->root_dir) {
1710 if ((!nls_uniname_cmp(sb, p_uniname->name,
1711 (u16 *)UNI_CUR_DIR_NAME)) ||
1712 (!nls_uniname_cmp(sb, p_uniname->name,
1713 (u16 *)UNI_PAR_DIR_NAME)))
1714 return -1; // special case, root directory itself
1715 }
1716
1717 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1718 dentries_per_clu = p_fs->dentries_in_root;
1719 else
1720 dentries_per_clu = p_fs->dentries_per_clu;
1721
1722 clu.dir = p_dir->dir;
1723 clu.size = p_dir->size;
1724 clu.flags = p_dir->flags;
1725
1726 p_fs->hint_uentry.dir = p_dir->dir;
1727 p_fs->hint_uentry.entry = -1;
1728
1729 while (clu.dir != CLUSTER_32(~0)) {
1730 if (p_fs->dev_ejected)
1731 break;
1732
1733 while (i < dentries_per_clu) {
1734 ep = get_entry_in_dir(sb, &clu, i, NULL);
1735 if (!ep)
1736 return -2;
1737
1738 entry_type = p_fs->fs_func->get_entry_type(ep);
1739 step = 1;
1740
1741 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
1742 is_feasible_entry = false;
1743
1744 if (p_fs->hint_uentry.entry == -1) {
1745 num_empty++;
1746
1747 if (num_empty == 1) {
1748 p_fs->hint_uentry.clu.dir = clu.dir;
1749 p_fs->hint_uentry.clu.size = clu.size;
1750 p_fs->hint_uentry.clu.flags = clu.flags;
1751 }
1752 if ((num_empty >= num_entries) || (entry_type == TYPE_UNUSED))
1753 p_fs->hint_uentry.entry = dentry - (num_empty - 1);
1754 }
1755
1756 if (entry_type == TYPE_UNUSED)
1757 return -2;
1758 } else {
1759 num_empty = 0;
1760
1761 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
1762 file_ep = (struct file_dentry_t *)ep;
1763 if ((type == TYPE_ALL) || (type == entry_type)) {
1764 num_ext_entries = file_ep->num_ext;
1765 is_feasible_entry = true;
1766 } else {
1767 is_feasible_entry = false;
1768 step = file_ep->num_ext + 1;
1769 }
1770 } else if (entry_type == TYPE_STREAM) {
1771 if (is_feasible_entry) {
1772 strm_ep = (struct strm_dentry_t *)ep;
1773 if (p_uniname->name_hash == GET16_A(strm_ep->name_hash) &&
1774 p_uniname->name_len == strm_ep->name_len) {
1775 order = 1;
1776 } else {
1777 is_feasible_entry = false;
1778 step = num_ext_entries;
1779 }
1780 }
1781 } else if (entry_type == TYPE_EXTEND) {
1782 if (is_feasible_entry) {
1783 name_ep = (struct name_dentry_t *)ep;
1784
1785 if ((++order) == 2)
1786 uniname = p_uniname->name;
1787 else
1788 uniname += 15;
1789
1790 len = extract_uni_name_from_name_entry(name_ep,
1791 entry_uniname, order);
1792
1793 unichar = *(uniname + len);
1794 *(uniname + len) = 0x0;
1795
1796 if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
1797 is_feasible_entry = false;
1798 step = num_ext_entries - order + 1;
1799 } else if (order == num_ext_entries) {
1800 p_fs->hint_uentry.dir = CLUSTER_32(~0);
1801 p_fs->hint_uentry.entry = -1;
1802 return dentry - (num_ext_entries);
1803 }
1804
1805 *(uniname + len) = unichar;
1806 }
1807 } else {
1808 is_feasible_entry = false;
1809 }
1810 }
1811
1812 i += step;
1813 dentry += step;
1814 }
1815
1816 i -= dentries_per_clu;
1817
1818 if (p_dir->dir == CLUSTER_32(0))
1819 break; /* FAT16 root_dir */
1820
1821 if (clu.flags == 0x03) {
1822 if ((--clu.size) > 0)
1823 clu.dir++;
1824 else
1825 clu.dir = CLUSTER_32(~0);
1826 } else {
1827 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1828 return -2;
1829 }
1830 }
1831
1832 return -2;
1833 }
1834
1835 static s32 exfat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
1836 s32 entry, struct dentry_t *p_entry)
1837 {
1838 int i, count = 0;
1839 u32 type;
1840 struct file_dentry_t *file_ep = (struct file_dentry_t *)p_entry;
1841 struct dentry_t *ext_ep;
1842 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1843
1844 for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
1845 ext_ep = get_entry_in_dir(sb, p_dir, entry, NULL);
1846 if (!ext_ep)
1847 return -1;
1848
1849 type = p_fs->fs_func->get_entry_type(ext_ep);
1850 if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
1851 count++;
1852 else
1853 return count;
1854 }
1855
1856 return count;
1857 }
1858
1859 s32 count_dos_name_entries(struct super_block *sb, struct chain_t *p_dir,
1860 u32 type)
1861 {
1862 int i, count = 0;
1863 s32 dentries_per_clu;
1864 u32 entry_type;
1865 struct chain_t clu;
1866 struct dentry_t *ep;
1867 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1868
1869 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1870 dentries_per_clu = p_fs->dentries_in_root;
1871 else
1872 dentries_per_clu = p_fs->dentries_per_clu;
1873
1874 clu.dir = p_dir->dir;
1875 clu.size = p_dir->size;
1876 clu.flags = p_dir->flags;
1877
1878 while (clu.dir != CLUSTER_32(~0)) {
1879 if (p_fs->dev_ejected)
1880 break;
1881
1882 for (i = 0; i < dentries_per_clu; i++) {
1883 ep = get_entry_in_dir(sb, &clu, i, NULL);
1884 if (!ep)
1885 return -ENOENT;
1886
1887 entry_type = p_fs->fs_func->get_entry_type(ep);
1888
1889 if (entry_type == TYPE_UNUSED)
1890 return count;
1891 if (!(type & TYPE_CRITICAL_PRI) &&
1892 !(type & TYPE_BENIGN_PRI))
1893 continue;
1894
1895 if ((type == TYPE_ALL) || (type == entry_type))
1896 count++;
1897 }
1898
1899 if (p_dir->dir == CLUSTER_32(0))
1900 break; /* FAT16 root_dir */
1901
1902 if (clu.flags == 0x03) {
1903 if ((--clu.size) > 0)
1904 clu.dir++;
1905 else
1906 clu.dir = CLUSTER_32(~0);
1907 } else {
1908 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1909 return -EIO;
1910 }
1911 }
1912
1913 return count;
1914 }
1915
1916 bool is_dir_empty(struct super_block *sb, struct chain_t *p_dir)
1917 {
1918 int i, count = 0;
1919 s32 dentries_per_clu;
1920 u32 type;
1921 struct chain_t clu;
1922 struct dentry_t *ep;
1923 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1924
1925 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1926 dentries_per_clu = p_fs->dentries_in_root;
1927 else
1928 dentries_per_clu = p_fs->dentries_per_clu;
1929
1930 clu.dir = p_dir->dir;
1931 clu.size = p_dir->size;
1932 clu.flags = p_dir->flags;
1933
1934 while (clu.dir != CLUSTER_32(~0)) {
1935 if (p_fs->dev_ejected)
1936 break;
1937
1938 for (i = 0; i < dentries_per_clu; i++) {
1939 ep = get_entry_in_dir(sb, &clu, i, NULL);
1940 if (!ep)
1941 break;
1942
1943 type = p_fs->fs_func->get_entry_type(ep);
1944
1945 if (type == TYPE_UNUSED)
1946 return true;
1947 if ((type != TYPE_FILE) && (type != TYPE_DIR))
1948 continue;
1949
1950 if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1951 return false;
1952
1953 if (p_fs->vol_type == EXFAT)
1954 return false;
1955 if ((p_dir->dir == p_fs->root_dir) || ((++count) > 2))
1956 return false;
1957 }
1958
1959 if (p_dir->dir == CLUSTER_32(0))
1960 break; /* FAT16 root_dir */
1961
1962 if (clu.flags == 0x03) {
1963 if ((--clu.size) > 0)
1964 clu.dir++;
1965 else
1966 clu.dir = CLUSTER_32(~0);
1967 }
1968 if (exfat_fat_read(sb, clu.dir, &clu.dir) != 0)
1969 break;
1970 }
1971
1972 return true;
1973 }
1974
1975 /*
1976 * Name Conversion Functions
1977 */
1978
1979 /* input : dir, uni_name
1980 * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
1981 */
1982 s32 get_num_entries_and_dos_name(struct super_block *sb, struct chain_t *p_dir,
1983 struct uni_name_t *p_uniname, s32 *entries,
1984 struct dos_name_t *p_dosname)
1985 {
1986 s32 num_entries;
1987 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1988
1989 num_entries = p_fs->fs_func->calc_num_entries(p_uniname);
1990 if (num_entries == 0)
1991 return -EINVAL;
1992
1993 *entries = num_entries;
1994
1995 return 0;
1996 }
1997
1998 static void exfat_get_uni_name_from_ext_entry(struct super_block *sb,
1999 struct chain_t *p_dir, s32 entry,
2000 u16 *uniname)
2001 {
2002 int i;
2003 struct dentry_t *ep;
2004 struct entry_set_cache_t *es;
2005 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2006
2007 es = get_entry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
2008 if (!es || es->num_entries < 3) {
2009 if (es)
2010 release_entry_set(es);
2011 return;
2012 }
2013
2014 ep += 2;
2015
2016 /*
2017 * First entry : file entry
2018 * Second entry : stream-extension entry
2019 * Third entry : first file-name entry
2020 * So, the index of first file-name dentry should start from 2.
2021 */
2022 for (i = 2; i < es->num_entries; i++, ep++) {
2023 if (p_fs->fs_func->get_entry_type(ep) == TYPE_EXTEND)
2024 extract_uni_name_from_name_entry((struct name_dentry_t *)
2025 ep, uniname, i);
2026 else
2027 goto out;
2028 uniname += 15;
2029 }
2030
2031 out:
2032 release_entry_set(es);
2033 }
2034
2035 static s32 exfat_calc_num_entries(struct uni_name_t *p_uniname)
2036 {
2037 s32 len;
2038
2039 len = p_uniname->name_len;
2040 if (len == 0)
2041 return 0;
2042
2043 /* 1 file entry + 1 stream entry + name entries */
2044 return (len - 1) / 15 + 3;
2045 }
2046
2047 u16 calc_checksum_2byte(void *data, s32 len, u16 chksum, s32 type)
2048 {
2049 int i;
2050 u8 *c = (u8 *)data;
2051
2052 switch (type) {
2053 case CS_DIR_ENTRY:
2054 for (i = 0; i < len; i++, c++) {
2055 if ((i == 2) || (i == 3))
2056 continue;
2057 chksum = (((chksum & 1) << 15) |
2058 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2059 }
2060 break;
2061 default
2062 :
2063 for (i = 0; i < len; i++, c++)
2064 chksum = (((chksum & 1) << 15) |
2065 ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2066 }
2067
2068 return chksum;
2069 }
2070
2071 /*
2072 * Name Resolution Functions
2073 */
2074
2075 /* return values of resolve_path()
2076 * > 0 : return the length of the path
2077 * < 0 : return error
2078 */
2079 s32 resolve_path(struct inode *inode, char *path, struct chain_t *p_dir,
2080 struct uni_name_t *p_uniname)
2081 {
2082 bool lossy = false;
2083 struct super_block *sb = inode->i_sb;
2084 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2085 struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2086
2087 if (strscpy(name_buf, path, sizeof(name_buf)) < 0)
2088 return -EINVAL;
2089
2090 nls_cstring_to_uniname(sb, p_uniname, name_buf, &lossy);
2091 if (lossy)
2092 return -EINVAL;
2093
2094 fid->size = i_size_read(inode);
2095
2096 p_dir->dir = fid->start_clu;
2097 p_dir->size = (s32)(fid->size >> p_fs->cluster_size_bits);
2098 p_dir->flags = fid->flags;
2099
2100 return 0;
2101 }
2102
2103 /*
2104 * File Operation Functions
2105 */
2106 static struct fs_func exfat_fs_func = {
2107 .alloc_cluster = exfat_alloc_cluster,
2108 .free_cluster = exfat_free_cluster,
2109 .count_used_clusters = exfat_count_used_clusters,
2110
2111 .init_dir_entry = exfat_init_dir_entry,
2112 .init_ext_entry = exfat_init_ext_entry,
2113 .find_dir_entry = exfat_find_dir_entry,
2114 .delete_dir_entry = exfat_delete_dir_entry,
2115 .get_uni_name_from_ext_entry = exfat_get_uni_name_from_ext_entry,
2116 .count_ext_entries = exfat_count_ext_entries,
2117 .calc_num_entries = exfat_calc_num_entries,
2118
2119 .get_entry_type = exfat_get_entry_type,
2120 .set_entry_type = exfat_set_entry_type,
2121 .get_entry_attr = exfat_get_entry_attr,
2122 .set_entry_attr = exfat_set_entry_attr,
2123 .get_entry_flag = exfat_get_entry_flag,
2124 .set_entry_flag = exfat_set_entry_flag,
2125 .get_entry_clu0 = exfat_get_entry_clu0,
2126 .set_entry_clu0 = exfat_set_entry_clu0,
2127 .get_entry_size = exfat_get_entry_size,
2128 .set_entry_size = exfat_set_entry_size,
2129 .get_entry_time = exfat_get_entry_time,
2130 .set_entry_time = exfat_set_entry_time,
2131 };
2132
2133 s32 exfat_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
2134 {
2135 struct bpbex_t *p_bpb = (struct bpbex_t *)p_pbr->bpb;
2136 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2137 struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
2138
2139 if (p_bpb->num_fats == 0)
2140 return -EFSCORRUPTED;
2141
2142 p_fs->sectors_per_clu = 1 << p_bpb->sectors_per_clu_bits;
2143 p_fs->sectors_per_clu_bits = p_bpb->sectors_per_clu_bits;
2144 p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
2145 p_bd->sector_size_bits;
2146 p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
2147
2148 p_fs->num_FAT_sectors = GET32(p_bpb->fat_length);
2149
2150 p_fs->FAT1_start_sector = p_fs->PBR_sector + GET32(p_bpb->fat_offset);
2151 if (p_bpb->num_fats == 1)
2152 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
2153 else
2154 p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
2155 p_fs->num_FAT_sectors;
2156
2157 p_fs->root_start_sector = p_fs->PBR_sector + GET32(p_bpb->clu_offset);
2158 p_fs->data_start_sector = p_fs->root_start_sector;
2159
2160 p_fs->num_sectors = GET64(p_bpb->vol_length);
2161 p_fs->num_clusters = GET32(p_bpb->clu_count) + 2;
2162 /* because the cluster index starts with 2 */
2163
2164 p_fs->vol_type = EXFAT;
2165 p_fs->vol_id = GET32(p_bpb->vol_serial);
2166
2167 p_fs->root_dir = GET32(p_bpb->root_cluster);
2168 p_fs->dentries_in_root = 0;
2169 p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
2170 DENTRY_SIZE_BITS);
2171
2172 p_fs->vol_flag = (u32)GET16(p_bpb->vol_flags);
2173 p_fs->clu_srch_ptr = 2;
2174 p_fs->used_clusters = UINT_MAX;
2175
2176 p_fs->fs_func = &exfat_fs_func;
2177
2178 return 0;
2179 }
2180
2181 s32 create_dir(struct inode *inode, struct chain_t *p_dir,
2182 struct uni_name_t *p_uniname, struct file_id_t *fid)
2183 {
2184 s32 ret, dentry, num_entries;
2185 u64 size;
2186 struct chain_t clu;
2187 struct dos_name_t dos_name;
2188 struct super_block *sb = inode->i_sb;
2189 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2190 struct fs_func *fs_func = p_fs->fs_func;
2191
2192 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
2193 &dos_name);
2194 if (ret)
2195 return ret;
2196
2197 /* find_empty_entry must be called before alloc_cluster */
2198 dentry = find_empty_entry(inode, p_dir, num_entries);
2199 if (dentry < 0)
2200 return -ENOSPC;
2201
2202 clu.dir = CLUSTER_32(~0);
2203 clu.size = 0;
2204 clu.flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2205
2206 /* (1) allocate a cluster */
2207 ret = fs_func->alloc_cluster(sb, 1, &clu);
2208 if (ret < 0)
2209 return ret;
2210 else if (ret == 0)
2211 return -ENOSPC;
2212
2213 ret = clear_cluster(sb, clu.dir);
2214 if (ret != 0)
2215 return ret;
2216
2217 size = p_fs->cluster_size;
2218
2219 /* (2) update the directory entry */
2220 /* make sub-dir entry in parent directory */
2221 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir,
2222 size);
2223 if (ret != 0)
2224 return ret;
2225
2226 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
2227 &dos_name);
2228 if (ret != 0)
2229 return ret;
2230
2231 fid->dir.dir = p_dir->dir;
2232 fid->dir.size = p_dir->size;
2233 fid->dir.flags = p_dir->flags;
2234 fid->entry = dentry;
2235
2236 fid->attr = ATTR_SUBDIR;
2237 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2238 fid->size = size;
2239 fid->start_clu = clu.dir;
2240
2241 fid->type = TYPE_DIR;
2242 fid->rwoffset = 0;
2243 fid->hint_last_off = -1;
2244
2245 return 0;
2246 }
2247
2248 s32 create_file(struct inode *inode, struct chain_t *p_dir,
2249 struct uni_name_t *p_uniname, u8 mode, struct file_id_t *fid)
2250 {
2251 s32 ret, dentry, num_entries;
2252 struct dos_name_t dos_name;
2253 struct super_block *sb = inode->i_sb;
2254 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2255 struct fs_func *fs_func = p_fs->fs_func;
2256
2257 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
2258 &dos_name);
2259 if (ret)
2260 return ret;
2261
2262 /* find_empty_entry must be called before alloc_cluster() */
2263 dentry = find_empty_entry(inode, p_dir, num_entries);
2264 if (dentry < 0)
2265 return -ENOSPC;
2266
2267 /* (1) update the directory entry */
2268 /* fill the dos name directory entry information of the created file.
2269 * the first cluster is not determined yet. (0)
2270 */
2271 ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode,
2272 CLUSTER_32(0), 0);
2273 if (ret != 0)
2274 return ret;
2275
2276 ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
2277 &dos_name);
2278 if (ret != 0)
2279 return ret;
2280
2281 fid->dir.dir = p_dir->dir;
2282 fid->dir.size = p_dir->size;
2283 fid->dir.flags = p_dir->flags;
2284 fid->entry = dentry;
2285
2286 fid->attr = ATTR_ARCHIVE | mode;
2287 fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
2288 fid->size = 0;
2289 fid->start_clu = CLUSTER_32(~0);
2290
2291 fid->type = TYPE_FILE;
2292 fid->rwoffset = 0;
2293 fid->hint_last_off = -1;
2294
2295 return 0;
2296 }
2297
2298 void remove_file(struct inode *inode, struct chain_t *p_dir, s32 entry)
2299 {
2300 s32 num_entries;
2301 sector_t sector;
2302 struct dentry_t *ep;
2303 struct super_block *sb = inode->i_sb;
2304 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2305 struct fs_func *fs_func = p_fs->fs_func;
2306
2307 ep = get_entry_in_dir(sb, p_dir, entry, &sector);
2308 if (!ep)
2309 return;
2310
2311 exfat_buf_lock(sb, sector);
2312
2313 /* exfat_buf_lock() before call count_ext_entries() */
2314 num_entries = fs_func->count_ext_entries(sb, p_dir, entry, ep);
2315 if (num_entries < 0) {
2316 exfat_buf_unlock(sb, sector);
2317 return;
2318 }
2319 num_entries++;
2320
2321 exfat_buf_unlock(sb, sector);
2322
2323 /* (1) update the directory entry */
2324 fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
2325 }
2326
2327 s32 exfat_rename_file(struct inode *inode, struct chain_t *p_dir, s32 oldentry,
2328 struct uni_name_t *p_uniname, struct file_id_t *fid)
2329 {
2330 s32 ret, newentry = -1, num_old_entries, num_new_entries;
2331 sector_t sector_old, sector_new;
2332 struct dos_name_t dos_name;
2333 struct dentry_t *epold, *epnew;
2334 struct super_block *sb = inode->i_sb;
2335 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2336 struct fs_func *fs_func = p_fs->fs_func;
2337
2338 epold = get_entry_in_dir(sb, p_dir, oldentry, &sector_old);
2339 if (!epold)
2340 return -ENOENT;
2341
2342 exfat_buf_lock(sb, sector_old);
2343
2344 /* exfat_buf_lock() before call count_ext_entries() */
2345 num_old_entries = fs_func->count_ext_entries(sb, p_dir, oldentry,
2346 epold);
2347 if (num_old_entries < 0) {
2348 exfat_buf_unlock(sb, sector_old);
2349 return -ENOENT;
2350 }
2351 num_old_entries++;
2352
2353 ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname,
2354 &num_new_entries, &dos_name);
2355 if (ret) {
2356 exfat_buf_unlock(sb, sector_old);
2357 return ret;
2358 }
2359
2360 if (num_old_entries < num_new_entries) {
2361 newentry = find_empty_entry(inode, p_dir, num_new_entries);
2362 if (newentry < 0) {
2363 exfat_buf_unlock(sb, sector_old);
2364 return -ENOSPC;
2365 }
2366
2367 epnew = get_entry_in_dir(sb, p_dir, newentry, &sector_new);
2368 if (!epnew) {
2369 exfat_buf_unlock(sb, sector_old);
2370 return -ENOENT;
2371 }
2372
2373 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
2374 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
2375 fs_func->set_entry_attr(epnew,
2376 fs_func->get_entry_attr(epnew) |
2377 ATTR_ARCHIVE);
2378 fid->attr |= ATTR_ARCHIVE;
2379 }
2380 exfat_buf_modify(sb, sector_new);
2381 exfat_buf_unlock(sb, sector_old);
2382
2383 epold = get_entry_in_dir(sb, p_dir, oldentry + 1,
2384 &sector_old);
2385 exfat_buf_lock(sb, sector_old);
2386 epnew = get_entry_in_dir(sb, p_dir, newentry + 1,
2387 &sector_new);
2388
2389 if (!epold || !epnew) {
2390 exfat_buf_unlock(sb, sector_old);
2391 return -ENOENT;
2392 }
2393
2394 memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
2395 exfat_buf_modify(sb, sector_new);
2396 exfat_buf_unlock(sb, sector_old);
2397
2398 ret = fs_func->init_ext_entry(sb, p_dir, newentry,
2399 num_new_entries, p_uniname,
2400 &dos_name);
2401 if (ret != 0)
2402 return ret;
2403
2404 fs_func->delete_dir_entry(sb, p_dir, oldentry, 0,
2405 num_old_entries);
2406 fid->entry = newentry;
2407 } else {
2408 if (fs_func->get_entry_type(epold) == TYPE_FILE) {
2409 fs_func->set_entry_attr(epold,
2410 fs_func->get_entry_attr(epold) |
2411 ATTR_ARCHIVE);
2412 fid->attr |= ATTR_ARCHIVE;
2413 }
2414 exfat_buf_modify(sb, sector_old);
2415 exfat_buf_unlock(sb, sector_old);
2416
2417 ret = fs_func->init_ext_entry(sb, p_dir, oldentry,
2418 num_new_entries, p_uniname,
2419 &dos_name);
2420 if (ret != 0)
2421 return ret;
2422
2423 fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries,
2424 num_old_entries);
2425 }
2426
2427 return 0;
2428 }
2429
2430 s32 move_file(struct inode *inode, struct chain_t *p_olddir, s32 oldentry,
2431 struct chain_t *p_newdir, struct uni_name_t *p_uniname,
2432 struct file_id_t *fid)
2433 {
2434 s32 ret, newentry, num_new_entries, num_old_entries;
2435 sector_t sector_mov, sector_new;
2436 struct dos_name_t dos_name;
2437 struct dentry_t *epmov, *epnew;
2438 struct super_block *sb = inode->i_sb;
2439 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2440 struct fs_func *fs_func = p_fs->fs_func;
2441
2442 epmov = get_entry_in_dir(sb, p_olddir, oldentry, &sector_mov);
2443 if (!epmov)
2444 return -ENOENT;
2445
2446 /* check if the source and target directory is the same */
2447 if (fs_func->get_entry_type(epmov) == TYPE_DIR &&
2448 fs_func->get_entry_clu0(epmov) == p_newdir->dir)
2449 return -EINVAL;
2450
2451 exfat_buf_lock(sb, sector_mov);
2452
2453 /* exfat_buf_lock() before call count_ext_entries() */
2454 num_old_entries = fs_func->count_ext_entries(sb, p_olddir, oldentry,
2455 epmov);
2456 if (num_old_entries < 0) {
2457 exfat_buf_unlock(sb, sector_mov);
2458 return -ENOENT;
2459 }
2460 num_old_entries++;
2461
2462 ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname,
2463 &num_new_entries, &dos_name);
2464 if (ret) {
2465 exfat_buf_unlock(sb, sector_mov);
2466 return ret;
2467 }
2468
2469 newentry = find_empty_entry(inode, p_newdir, num_new_entries);
2470 if (newentry < 0) {
2471 exfat_buf_unlock(sb, sector_mov);
2472 return -ENOSPC;
2473 }
2474
2475 epnew = get_entry_in_dir(sb, p_newdir, newentry, &sector_new);
2476 if (!epnew) {
2477 exfat_buf_unlock(sb, sector_mov);
2478 return -ENOENT;
2479 }
2480
2481 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
2482 if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
2483 fs_func->set_entry_attr(epnew, fs_func->get_entry_attr(epnew) |
2484 ATTR_ARCHIVE);
2485 fid->attr |= ATTR_ARCHIVE;
2486 }
2487 exfat_buf_modify(sb, sector_new);
2488 exfat_buf_unlock(sb, sector_mov);
2489
2490 epmov = get_entry_in_dir(sb, p_olddir, oldentry + 1,
2491 &sector_mov);
2492 exfat_buf_lock(sb, sector_mov);
2493 epnew = get_entry_in_dir(sb, p_newdir, newentry + 1,
2494 &sector_new);
2495 if (!epmov || !epnew) {
2496 exfat_buf_unlock(sb, sector_mov);
2497 return -ENOENT;
2498 }
2499
2500 memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
2501 exfat_buf_modify(sb, sector_new);
2502 exfat_buf_unlock(sb, sector_mov);
2503
2504 ret = fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries,
2505 p_uniname, &dos_name);
2506 if (ret != 0)
2507 return ret;
2508
2509 fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
2510
2511 fid->dir.dir = p_newdir->dir;
2512 fid->dir.size = p_newdir->size;
2513 fid->dir.flags = p_newdir->flags;
2514
2515 fid->entry = newentry;
2516
2517 return 0;
2518 }
2519
2520 /*
2521 * Sector Read/Write Functions
2522 */
2523
2524 int sector_read(struct super_block *sb, sector_t sec, struct buffer_head **bh,
2525 bool read)
2526 {
2527 s32 ret = -EIO;
2528 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2529
2530 if ((sec >= (p_fs->PBR_sector + p_fs->num_sectors)) &&
2531 (p_fs->num_sectors > 0)) {
2532 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2533 __func__, (unsigned long long)sec);
2534 fs_error(sb);
2535 return ret;
2536 }
2537
2538 if (!p_fs->dev_ejected) {
2539 ret = exfat_bdev_read(sb, sec, bh, 1, read);
2540 if (ret != 0)
2541 p_fs->dev_ejected = 1;
2542 }
2543
2544 return ret;
2545 }
2546
2547 int sector_write(struct super_block *sb, sector_t sec, struct buffer_head *bh,
2548 bool sync)
2549 {
2550 s32 ret = -EIO;
2551 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2552
2553 if (sec >= (p_fs->PBR_sector + p_fs->num_sectors) &&
2554 (p_fs->num_sectors > 0)) {
2555 pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
2556 __func__, (unsigned long long)sec);
2557 fs_error(sb);
2558 return ret;
2559 }
2560
2561 if (!bh) {
2562 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
2563 fs_error(sb);
2564 return ret;
2565 }
2566
2567 if (!p_fs->dev_ejected) {
2568 ret = exfat_bdev_write(sb, sec, bh, 1, sync);
2569 if (ret != 0)
2570 p_fs->dev_ejected = 1;
2571 }
2572
2573 return ret;
2574 }
2575
2576 int multi_sector_read(struct super_block *sb, sector_t sec,
2577 struct buffer_head **bh, s32 num_secs, bool read)
2578 {
2579 s32 ret = -EIO;
2580 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2581
2582 if (((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors)) &&
2583 (p_fs->num_sectors > 0)) {
2584 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2585 __func__, (unsigned long long)sec, num_secs);
2586 fs_error(sb);
2587 return ret;
2588 }
2589
2590 if (!p_fs->dev_ejected) {
2591 ret = exfat_bdev_read(sb, sec, bh, num_secs, read);
2592 if (ret != 0)
2593 p_fs->dev_ejected = 1;
2594 }
2595
2596 return ret;
2597 }
2598
2599 int multi_sector_write(struct super_block *sb, sector_t sec,
2600 struct buffer_head *bh, s32 num_secs, bool sync)
2601 {
2602 s32 ret = -EIO;
2603 struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2604
2605 if ((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors) &&
2606 (p_fs->num_sectors > 0)) {
2607 pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
2608 __func__, (unsigned long long)sec, num_secs);
2609 fs_error(sb);
2610 return ret;
2611 }
2612 if (!bh) {
2613 pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
2614 fs_error(sb);
2615 return ret;
2616 }
2617
2618 if (!p_fs->dev_ejected) {
2619 ret = exfat_bdev_write(sb, sec, bh, num_secs, sync);
2620 if (ret != 0)
2621 p_fs->dev_ejected = 1;
2622 }
2623
2624 return ret;
2625 }