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