• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
__set_sb_dirty(struct super_block * sb)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 char *reserved_names[] = {
24 	"AUX     ", "CON     ", "NUL     ", "PRN     ",
25 	"COM1    ", "COM2    ", "COM3    ", "COM4    ",
26 	"COM5    ", "COM6    ", "COM7    ", "COM8    ", "COM9    ",
27 	"LPT1    ", "LPT2    ", "LPT3    ", "LPT4    ",
28 	"LPT5    ", "LPT6    ", "LPT7    ", "LPT8    ", "LPT9    ",
29 	NULL
30 };
31 
32 static u8 free_bit[] = {
33 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /*   0 ~  19 */
34 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, /*  20 ~  39 */
35 	0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /*  40 ~  59 */
36 	0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /*  60 ~  79 */
37 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, /*  80 ~  99 */
38 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, /* 100 ~ 119 */
39 	0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 120 ~ 139 */
40 	0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, /* 140 ~ 159 */
41 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, /* 160 ~ 179 */
42 	0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, /* 180 ~ 199 */
43 	0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, /* 200 ~ 219 */
44 	0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, /* 220 ~ 239 */
45 	0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0                 /* 240 ~ 254 */
46 };
47 
48 static u8 used_bit[] = {
49 	0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, /*   0 ~  19 */
50 	2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4, /*  20 ~  39 */
51 	2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, /*  40 ~  59 */
52 	4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, /*  60 ~  79 */
53 	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, /*  80 ~  99 */
54 	3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, /* 100 ~ 119 */
55 	4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, /* 120 ~ 139 */
56 	3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, /* 140 ~ 159 */
57 	2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, /* 160 ~ 179 */
58 	4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, /* 180 ~ 199 */
59 	3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, /* 200 ~ 219 */
60 	5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, /* 220 ~ 239 */
61 	4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8              /* 240 ~ 255 */
62 };
63 
64 #define BITMAP_LOC(v)           ((v) >> 3)
65 #define BITMAP_SHIFT(v)         ((v) & 0x07)
66 
exfat_bitmap_test(u8 * bitmap,int i)67 static inline s32 exfat_bitmap_test(u8 *bitmap, int i)
68 {
69 	u8 data;
70 
71 	data = bitmap[BITMAP_LOC(i)];
72 	if ((data >> BITMAP_SHIFT(i)) & 0x01)
73 		return 1;
74 	return 0;
75 }
76 
exfat_bitmap_set(u8 * bitmap,int i)77 static inline void exfat_bitmap_set(u8 *bitmap, int i)
78 {
79 	bitmap[BITMAP_LOC(i)] |= (0x01 << BITMAP_SHIFT(i));
80 }
81 
exfat_bitmap_clear(u8 * bitmap,int i)82 static inline void exfat_bitmap_clear(u8 *bitmap, int i)
83 {
84 	bitmap[BITMAP_LOC(i)] &= ~(0x01 << BITMAP_SHIFT(i));
85 }
86 
87 /*
88  *  File System Management Functions
89  */
90 
fs_set_vol_flags(struct super_block * sb,u32 new_flag)91 void fs_set_vol_flags(struct super_block *sb, u32 new_flag)
92 {
93 	struct pbr_sector_t *p_pbr;
94 	struct bpbex_t *p_bpb;
95 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
96 
97 	if (p_fs->vol_flag == new_flag)
98 		return;
99 
100 	p_fs->vol_flag = new_flag;
101 
102 	if (p_fs->vol_type == EXFAT) {
103 		if (!p_fs->pbr_bh) {
104 			if (sector_read(sb, p_fs->PBR_sector,
105 					&p_fs->pbr_bh, 1) != FFS_SUCCESS)
106 				return;
107 		}
108 
109 		p_pbr = (struct pbr_sector_t *)p_fs->pbr_bh->b_data;
110 		p_bpb = (struct bpbex_t *)p_pbr->bpb;
111 		SET16(p_bpb->vol_flags, (u16)new_flag);
112 
113 		/* XXX duyoung
114 		 * what can we do here? (cuz fs_set_vol_flags() is void)
115 		 */
116 		if ((new_flag == VOL_DIRTY) && (!buffer_dirty(p_fs->pbr_bh)))
117 			sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 1);
118 		else
119 			sector_write(sb, p_fs->PBR_sector, p_fs->pbr_bh, 0);
120 	}
121 }
122 
fs_error(struct super_block * sb)123 void fs_error(struct super_block *sb)
124 {
125 	struct exfat_mount_options *opts = &EXFAT_SB(sb)->options;
126 
127 	if (opts->errors == EXFAT_ERRORS_PANIC) {
128 		panic("[EXFAT] Filesystem panic from previous error\n");
129 	} else if ((opts->errors == EXFAT_ERRORS_RO) && !sb_rdonly(sb)) {
130 		sb->s_flags |= SB_RDONLY;
131 		pr_err("[EXFAT] Filesystem has been set read-only\n");
132 	}
133 }
134 
135 /*
136  *  Cluster Management Functions
137  */
138 
clear_cluster(struct super_block * sb,u32 clu)139 s32 clear_cluster(struct super_block *sb, u32 clu)
140 {
141 	sector_t s, n;
142 	s32 ret = FFS_SUCCESS;
143 	struct buffer_head *tmp_bh = NULL;
144 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
145 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
146 
147 	if (clu == CLUSTER_32(0)) { /* FAT16 root_dir */
148 		s = p_fs->root_start_sector;
149 		n = p_fs->data_start_sector;
150 	} else {
151 		s = START_SECTOR(clu);
152 		n = s + p_fs->sectors_per_clu;
153 	}
154 
155 	for (; s < n; s++) {
156 		ret = sector_read(sb, s, &tmp_bh, 0);
157 		if (ret != FFS_SUCCESS)
158 			return ret;
159 
160 		memset((char *)tmp_bh->b_data, 0x0, p_bd->sector_size);
161 		ret = sector_write(sb, s, tmp_bh, 0);
162 		if (ret != FFS_SUCCESS)
163 			break;
164 	}
165 
166 	brelse(tmp_bh);
167 	return ret;
168 }
169 
fat_alloc_cluster(struct super_block * sb,s32 num_alloc,struct chain_t * p_chain)170 s32 fat_alloc_cluster(struct super_block *sb, s32 num_alloc,
171 		      struct chain_t *p_chain)
172 {
173 	int i, num_clusters = 0;
174 	u32 new_clu, last_clu = CLUSTER_32(~0), read_clu;
175 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
176 
177 	new_clu = p_chain->dir;
178 	if (new_clu == CLUSTER_32(~0))
179 		new_clu = p_fs->clu_srch_ptr;
180 	else if (new_clu >= p_fs->num_clusters)
181 		new_clu = 2;
182 
183 	__set_sb_dirty(sb);
184 
185 	p_chain->dir = CLUSTER_32(~0);
186 
187 	for (i = 2; i < p_fs->num_clusters; i++) {
188 		if (FAT_read(sb, new_clu, &read_clu) != 0)
189 			return -1;
190 
191 		if (read_clu == CLUSTER_32(0)) {
192 			if (FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
193 				return -1;
194 			num_clusters++;
195 
196 			if (p_chain->dir == CLUSTER_32(~0)) {
197 				p_chain->dir = new_clu;
198 			} else {
199 				if (FAT_write(sb, last_clu, new_clu) < 0)
200 					return -1;
201 			}
202 
203 			last_clu = new_clu;
204 
205 			if ((--num_alloc) == 0) {
206 				p_fs->clu_srch_ptr = new_clu;
207 				if (p_fs->used_clusters != UINT_MAX)
208 					p_fs->used_clusters += num_clusters;
209 
210 				return num_clusters;
211 			}
212 		}
213 		if ((++new_clu) >= p_fs->num_clusters)
214 			new_clu = 2;
215 	}
216 
217 	p_fs->clu_srch_ptr = new_clu;
218 	if (p_fs->used_clusters != UINT_MAX)
219 		p_fs->used_clusters += num_clusters;
220 
221 	return num_clusters;
222 }
223 
exfat_alloc_cluster(struct super_block * sb,s32 num_alloc,struct chain_t * p_chain)224 s32 exfat_alloc_cluster(struct super_block *sb, s32 num_alloc,
225 			struct chain_t *p_chain)
226 {
227 	s32 num_clusters = 0;
228 	u32 hint_clu, new_clu, last_clu = CLUSTER_32(~0);
229 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
230 
231 	hint_clu = p_chain->dir;
232 	if (hint_clu == CLUSTER_32(~0)) {
233 		hint_clu = test_alloc_bitmap(sb, p_fs->clu_srch_ptr - 2);
234 		if (hint_clu == CLUSTER_32(~0))
235 			return 0;
236 	} else if (hint_clu >= p_fs->num_clusters) {
237 		hint_clu = 2;
238 		p_chain->flags = 0x01;
239 	}
240 
241 	__set_sb_dirty(sb);
242 
243 	p_chain->dir = CLUSTER_32(~0);
244 
245 	while ((new_clu = test_alloc_bitmap(sb, hint_clu - 2)) != CLUSTER_32(~0)) {
246 		if (new_clu != hint_clu) {
247 			if (p_chain->flags == 0x03) {
248 				exfat_chain_cont_cluster(sb, p_chain->dir,
249 							 num_clusters);
250 				p_chain->flags = 0x01;
251 			}
252 		}
253 
254 		if (set_alloc_bitmap(sb, new_clu - 2) != FFS_SUCCESS)
255 			return -1;
256 
257 		num_clusters++;
258 
259 		if (p_chain->flags == 0x01) {
260 			if (FAT_write(sb, new_clu, CLUSTER_32(~0)) < 0)
261 				return -1;
262 		}
263 
264 		if (p_chain->dir == CLUSTER_32(~0)) {
265 			p_chain->dir = new_clu;
266 		} else {
267 			if (p_chain->flags == 0x01) {
268 				if (FAT_write(sb, last_clu, new_clu) < 0)
269 					return -1;
270 			}
271 		}
272 		last_clu = new_clu;
273 
274 		if ((--num_alloc) == 0) {
275 			p_fs->clu_srch_ptr = hint_clu;
276 			if (p_fs->used_clusters != UINT_MAX)
277 				p_fs->used_clusters += num_clusters;
278 
279 			p_chain->size += num_clusters;
280 			return num_clusters;
281 		}
282 
283 		hint_clu = new_clu + 1;
284 		if (hint_clu >= p_fs->num_clusters) {
285 			hint_clu = 2;
286 
287 			if (p_chain->flags == 0x03) {
288 				exfat_chain_cont_cluster(sb, p_chain->dir,
289 							 num_clusters);
290 				p_chain->flags = 0x01;
291 			}
292 		}
293 	}
294 
295 	p_fs->clu_srch_ptr = hint_clu;
296 	if (p_fs->used_clusters != UINT_MAX)
297 		p_fs->used_clusters += num_clusters;
298 
299 	p_chain->size += num_clusters;
300 	return num_clusters;
301 }
302 
fat_free_cluster(struct super_block * sb,struct chain_t * p_chain,s32 do_relse)303 void fat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
304 		      s32 do_relse)
305 {
306 	s32 num_clusters = 0;
307 	u32 clu, prev;
308 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
309 	int i;
310 	sector_t sector;
311 
312 	if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
313 		return;
314 	__set_sb_dirty(sb);
315 	clu = p_chain->dir;
316 
317 	if (p_chain->size <= 0)
318 		return;
319 
320 	do {
321 		if (p_fs->dev_ejected)
322 			break;
323 
324 		if (do_relse) {
325 			sector = START_SECTOR(clu);
326 			for (i = 0; i < p_fs->sectors_per_clu; i++)
327 				buf_release(sb, sector + i);
328 		}
329 
330 		prev = clu;
331 		if (FAT_read(sb, clu, &clu) == -1)
332 			break;
333 
334 		if (FAT_write(sb, prev, CLUSTER_32(0)) < 0)
335 			break;
336 		num_clusters++;
337 
338 	} while (clu != CLUSTER_32(~0));
339 
340 	if (p_fs->used_clusters != UINT_MAX)
341 		p_fs->used_clusters -= num_clusters;
342 }
343 
exfat_free_cluster(struct super_block * sb,struct chain_t * p_chain,s32 do_relse)344 void exfat_free_cluster(struct super_block *sb, struct chain_t *p_chain,
345 			s32 do_relse)
346 {
347 	s32 num_clusters = 0;
348 	u32 clu;
349 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
350 	int i;
351 	sector_t sector;
352 
353 	if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
354 		return;
355 
356 	if (p_chain->size <= 0) {
357 		pr_err("[EXFAT] free_cluster : skip free-req clu:%u, because of zero-size truncation\n",
358 		       p_chain->dir);
359 		return;
360 	}
361 
362 	__set_sb_dirty(sb);
363 	clu = p_chain->dir;
364 
365 	if (p_chain->flags == 0x03) {
366 		do {
367 			if (do_relse) {
368 				sector = START_SECTOR(clu);
369 				for (i = 0; i < p_fs->sectors_per_clu; i++)
370 					buf_release(sb, sector + i);
371 			}
372 
373 			if (clr_alloc_bitmap(sb, clu - 2) != FFS_SUCCESS)
374 				break;
375 			clu++;
376 
377 			num_clusters++;
378 		} while (num_clusters < p_chain->size);
379 	} else {
380 		do {
381 			if (p_fs->dev_ejected)
382 				break;
383 
384 			if (do_relse) {
385 				sector = START_SECTOR(clu);
386 				for (i = 0; i < p_fs->sectors_per_clu; i++)
387 					buf_release(sb, sector + i);
388 			}
389 
390 			if (clr_alloc_bitmap(sb, clu - 2) != FFS_SUCCESS)
391 				break;
392 
393 			if (FAT_read(sb, clu, &clu) == -1)
394 				break;
395 			num_clusters++;
396 		} while ((clu != CLUSTER_32(0)) && (clu != CLUSTER_32(~0)));
397 	}
398 
399 	if (p_fs->used_clusters != UINT_MAX)
400 		p_fs->used_clusters -= num_clusters;
401 }
402 
find_last_cluster(struct super_block * sb,struct chain_t * p_chain)403 u32 find_last_cluster(struct super_block *sb, struct chain_t *p_chain)
404 {
405 	u32 clu, next;
406 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
407 
408 	clu = p_chain->dir;
409 
410 	if (p_chain->flags == 0x03) {
411 		clu += p_chain->size - 1;
412 	} else {
413 		while ((FAT_read(sb, clu, &next) == 0) &&
414 		       (next != CLUSTER_32(~0))) {
415 			if (p_fs->dev_ejected)
416 				break;
417 			clu = next;
418 		}
419 	}
420 
421 	return clu;
422 }
423 
count_num_clusters(struct super_block * sb,struct chain_t * p_chain)424 s32 count_num_clusters(struct super_block *sb, struct chain_t *p_chain)
425 {
426 	int i, count = 0;
427 	u32 clu;
428 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
429 
430 	if ((p_chain->dir == CLUSTER_32(0)) || (p_chain->dir == CLUSTER_32(~0)))
431 		return 0;
432 
433 	clu = p_chain->dir;
434 
435 	if (p_chain->flags == 0x03) {
436 		count = p_chain->size;
437 	} else {
438 		for (i = 2; i < p_fs->num_clusters; i++) {
439 			count++;
440 			if (FAT_read(sb, clu, &clu) != 0)
441 				return 0;
442 			if (clu == CLUSTER_32(~0))
443 				break;
444 		}
445 	}
446 
447 	return count;
448 }
449 
fat_count_used_clusters(struct super_block * sb)450 s32 fat_count_used_clusters(struct super_block *sb)
451 {
452 	int i, count = 0;
453 	u32 clu;
454 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
455 
456 	for (i = 2; i < p_fs->num_clusters; i++) {
457 		if (FAT_read(sb, i, &clu) != 0)
458 			break;
459 		if (clu != CLUSTER_32(0))
460 			count++;
461 	}
462 
463 	return count;
464 }
465 
exfat_count_used_clusters(struct super_block * sb)466 s32 exfat_count_used_clusters(struct super_block *sb)
467 {
468 	int i, map_i, map_b, count = 0;
469 	u8 k;
470 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
471 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
472 
473 	map_i = map_b = 0;
474 
475 	for (i = 2; i < p_fs->num_clusters; i += 8) {
476 		k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
477 		count += used_bit[k];
478 
479 		if ((++map_b) >= p_bd->sector_size) {
480 			map_i++;
481 			map_b = 0;
482 		}
483 	}
484 
485 	return count;
486 }
487 
exfat_chain_cont_cluster(struct super_block * sb,u32 chain,s32 len)488 void exfat_chain_cont_cluster(struct super_block *sb, u32 chain, s32 len)
489 {
490 	if (len == 0)
491 		return;
492 
493 	while (len > 1) {
494 		if (FAT_write(sb, chain, chain + 1) < 0)
495 			break;
496 		chain++;
497 		len--;
498 	}
499 	FAT_write(sb, chain, CLUSTER_32(~0));
500 }
501 
502 /*
503  *  Allocation Bitmap Management Functions
504  */
505 
load_alloc_bitmap(struct super_block * sb)506 s32 load_alloc_bitmap(struct super_block *sb)
507 {
508 	int i, j, ret;
509 	u32 map_size;
510 	u32 type;
511 	sector_t sector;
512 	struct chain_t clu;
513 	struct bmap_dentry_t *ep;
514 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
515 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
516 
517 	clu.dir = p_fs->root_dir;
518 	clu.flags = 0x01;
519 
520 	while (clu.dir != CLUSTER_32(~0)) {
521 		if (p_fs->dev_ejected)
522 			break;
523 
524 		for (i = 0; i < p_fs->dentries_per_clu; i++) {
525 			ep = (struct bmap_dentry_t *)get_entry_in_dir(sb, &clu,
526 								      i, NULL);
527 			if (!ep)
528 				return FFS_MEDIAERR;
529 
530 			type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
531 
532 			if (type == TYPE_UNUSED)
533 				break;
534 			if (type != TYPE_BITMAP)
535 				continue;
536 
537 			if (ep->flags == 0x0) {
538 				p_fs->map_clu  = GET32_A(ep->start_clu);
539 				map_size = (u32)GET64_A(ep->size);
540 
541 				p_fs->map_sectors = ((map_size - 1) >> p_bd->sector_size_bits) + 1;
542 
543 				p_fs->vol_amap = kmalloc_array(p_fs->map_sectors,
544 							       sizeof(struct buffer_head *),
545 							       GFP_KERNEL);
546 				if (!p_fs->vol_amap)
547 					return FFS_MEMORYERR;
548 
549 				sector = START_SECTOR(p_fs->map_clu);
550 
551 				for (j = 0; j < p_fs->map_sectors; j++) {
552 					p_fs->vol_amap[j] = NULL;
553 					ret = sector_read(sb, sector + j, &(p_fs->vol_amap[j]), 1);
554 					if (ret != FFS_SUCCESS) {
555 						/*  release all buffers and free vol_amap */
556 						i = 0;
557 						while (i < j)
558 							brelse(p_fs->vol_amap[i++]);
559 
560 						kfree(p_fs->vol_amap);
561 						p_fs->vol_amap = NULL;
562 						return ret;
563 					}
564 				}
565 
566 				p_fs->pbr_bh = NULL;
567 				return FFS_SUCCESS;
568 			}
569 		}
570 
571 		if (FAT_read(sb, clu.dir, &clu.dir) != 0)
572 			return FFS_MEDIAERR;
573 	}
574 
575 	return FFS_FORMATERR;
576 }
577 
free_alloc_bitmap(struct super_block * sb)578 void free_alloc_bitmap(struct super_block *sb)
579 {
580 	int i;
581 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
582 
583 	brelse(p_fs->pbr_bh);
584 
585 	for (i = 0; i < p_fs->map_sectors; i++)
586 		__brelse(p_fs->vol_amap[i]);
587 
588 	kfree(p_fs->vol_amap);
589 	p_fs->vol_amap = NULL;
590 }
591 
set_alloc_bitmap(struct super_block * sb,u32 clu)592 s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
593 {
594 	int i, b;
595 	sector_t sector;
596 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
597 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
598 
599 	i = clu >> (p_bd->sector_size_bits + 3);
600 	b = clu & ((p_bd->sector_size << 3) - 1);
601 
602 	sector = START_SECTOR(p_fs->map_clu) + i;
603 
604 	exfat_bitmap_set((u8 *)p_fs->vol_amap[i]->b_data, b);
605 
606 	return sector_write(sb, sector, p_fs->vol_amap[i], 0);
607 }
608 
clr_alloc_bitmap(struct super_block * sb,u32 clu)609 s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
610 {
611 	int i, b;
612 	sector_t sector;
613 #ifdef CONFIG_EXFAT_DISCARD
614 	struct exfat_sb_info *sbi = EXFAT_SB(sb);
615 	struct exfat_mount_options *opts = &sbi->options;
616 	int ret;
617 #endif /* CONFIG_EXFAT_DISCARD */
618 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
619 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
620 
621 	i = clu >> (p_bd->sector_size_bits + 3);
622 	b = clu & ((p_bd->sector_size << 3) - 1);
623 
624 	sector = START_SECTOR(p_fs->map_clu) + i;
625 
626 	exfat_bitmap_clear((u8 *)p_fs->vol_amap[i]->b_data, b);
627 
628 	return sector_write(sb, sector, p_fs->vol_amap[i], 0);
629 
630 #ifdef CONFIG_EXFAT_DISCARD
631 	if (opts->discard) {
632 		ret = sb_issue_discard(sb, START_SECTOR(clu),
633 				       (1 << p_fs->sectors_per_clu_bits),
634 				       GFP_NOFS, 0);
635 		if (ret == -EOPNOTSUPP) {
636 			pr_warn("discard not supported by device, disabling");
637 			opts->discard = 0;
638 		}
639 	}
640 #endif /* CONFIG_EXFAT_DISCARD */
641 }
642 
test_alloc_bitmap(struct super_block * sb,u32 clu)643 u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
644 {
645 	int i, map_i, map_b;
646 	u32 clu_base, clu_free;
647 	u8 k, clu_mask;
648 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
649 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
650 
651 	clu_base = (clu & ~(0x7)) + 2;
652 	clu_mask = (1 << (clu - clu_base + 2)) - 1;
653 
654 	map_i = clu >> (p_bd->sector_size_bits + 3);
655 	map_b = (clu >> 3) & p_bd->sector_size_mask;
656 
657 	for (i = 2; i < p_fs->num_clusters; i += 8) {
658 		k = *(((u8 *)p_fs->vol_amap[map_i]->b_data) + map_b);
659 		if (clu_mask > 0) {
660 			k |= clu_mask;
661 			clu_mask = 0;
662 		}
663 		if (k < 0xFF) {
664 			clu_free = clu_base + free_bit[k];
665 			if (clu_free < p_fs->num_clusters)
666 				return clu_free;
667 		}
668 		clu_base += 8;
669 
670 		if (((++map_b) >= p_bd->sector_size) ||
671 		    (clu_base >= p_fs->num_clusters)) {
672 			if ((++map_i) >= p_fs->map_sectors) {
673 				clu_base = 2;
674 				map_i = 0;
675 			}
676 			map_b = 0;
677 		}
678 	}
679 
680 	return CLUSTER_32(~0);
681 }
682 
sync_alloc_bitmap(struct super_block * sb)683 void sync_alloc_bitmap(struct super_block *sb)
684 {
685 	int i;
686 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
687 
688 	if (!p_fs->vol_amap)
689 		return;
690 
691 	for (i = 0; i < p_fs->map_sectors; i++)
692 		sync_dirty_buffer(p_fs->vol_amap[i]);
693 }
694 
695 /*
696  *  Upcase table Management Functions
697  */
__load_upcase_table(struct super_block * sb,sector_t sector,u32 num_sectors,u32 utbl_checksum)698 static s32 __load_upcase_table(struct super_block *sb, sector_t sector,
699 			       u32 num_sectors, u32 utbl_checksum)
700 {
701 	int i, ret = FFS_ERROR;
702 	u32 j;
703 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
704 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
705 	struct buffer_head *tmp_bh = NULL;
706 	sector_t end_sector = num_sectors + sector;
707 
708 	bool	skip = false;
709 	u32	index = 0;
710 	u16	uni = 0;
711 	u16 **upcase_table;
712 
713 	u32 checksum = 0;
714 
715 	upcase_table = p_fs->vol_utbl = kmalloc(UTBL_COL_COUNT * sizeof(u16 *),
716 						GFP_KERNEL);
717 	if (!upcase_table)
718 		return FFS_MEMORYERR;
719 	memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
720 
721 	while (sector < end_sector) {
722 		ret = sector_read(sb, sector, &tmp_bh, 1);
723 		if (ret != FFS_SUCCESS) {
724 			pr_debug("sector read (0x%llX)fail\n",
725 				 (unsigned long long)sector);
726 			goto error;
727 		}
728 		sector++;
729 
730 		for (i = 0; i < p_bd->sector_size && index <= 0xFFFF; i += 2) {
731 			uni = GET16(((u8 *)tmp_bh->b_data) + i);
732 
733 			checksum = ((checksum & 1) ? 0x80000000 : 0) +
734 				   (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
735 						       i);
736 			checksum = ((checksum & 1) ? 0x80000000 : 0) +
737 				   (checksum >> 1) + *(((u8 *)tmp_bh->b_data) +
738 						       (i + 1));
739 
740 			if (skip) {
741 				pr_debug("skip from 0x%X ", index);
742 				index += uni;
743 				pr_debug("to 0x%X (amount of 0x%X)\n",
744 					 index, uni);
745 				skip = false;
746 			} else if (uni == index) {
747 				index++;
748 			} else if (uni == 0xFFFF) {
749 				skip = true;
750 			} else { /* uni != index , uni != 0xFFFF */
751 				u16 col_index = get_col_index(index);
752 
753 				if (!upcase_table[col_index]) {
754 					pr_debug("alloc = 0x%X\n", col_index);
755 					upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
756 						sizeof(u16), GFP_KERNEL);
757 					if (!upcase_table[col_index]) {
758 						ret = FFS_MEMORYERR;
759 						goto error;
760 					}
761 
762 					for (j = 0; j < UTBL_ROW_COUNT; j++)
763 						upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
764 				}
765 
766 				upcase_table[col_index][get_row_index(index)] = uni;
767 				index++;
768 			}
769 		}
770 	}
771 	if (index >= 0xFFFF && utbl_checksum == checksum) {
772 		if (tmp_bh)
773 			brelse(tmp_bh);
774 		return FFS_SUCCESS;
775 	}
776 	ret = FFS_ERROR;
777 error:
778 	if (tmp_bh)
779 		brelse(tmp_bh);
780 	free_upcase_table(sb);
781 	return ret;
782 }
783 
__load_default_upcase_table(struct super_block * sb)784 static s32 __load_default_upcase_table(struct super_block *sb)
785 {
786 	int i, ret = FFS_ERROR;
787 	u32 j;
788 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
789 
790 	bool	skip = false;
791 	u32	index = 0;
792 	u16	uni = 0;
793 	u16 **upcase_table;
794 
795 	upcase_table = p_fs->vol_utbl = kmalloc(UTBL_COL_COUNT * sizeof(u16 *),
796 						GFP_KERNEL);
797 	if (!upcase_table)
798 		return FFS_MEMORYERR;
799 	memset(upcase_table, 0, UTBL_COL_COUNT * sizeof(u16 *));
800 
801 	for (i = 0; index <= 0xFFFF && i < NUM_UPCASE * 2; i += 2) {
802 		uni = GET16(uni_upcase + i);
803 		if (skip) {
804 			pr_debug("skip from 0x%X ", index);
805 			index += uni;
806 			pr_debug("to 0x%X (amount of 0x%X)\n", index, uni);
807 			skip = false;
808 		} else if (uni == index) {
809 			index++;
810 		} else if (uni == 0xFFFF) {
811 			skip = true;
812 		} else { /* uni != index , uni != 0xFFFF */
813 			u16 col_index = get_col_index(index);
814 
815 			if (!upcase_table[col_index]) {
816 				pr_debug("alloc = 0x%X\n", col_index);
817 				upcase_table[col_index] = kmalloc_array(UTBL_ROW_COUNT,
818 									sizeof(u16),
819 									GFP_KERNEL);
820 				if (!upcase_table[col_index]) {
821 					ret = FFS_MEMORYERR;
822 					goto error;
823 				}
824 
825 				for (j = 0; j < UTBL_ROW_COUNT; j++)
826 					upcase_table[col_index][j] = (col_index << LOW_INDEX_BIT) | j;
827 			}
828 
829 			upcase_table[col_index][get_row_index(index)] = uni;
830 			index++;
831 		}
832 	}
833 
834 	if (index >= 0xFFFF)
835 		return FFS_SUCCESS;
836 
837 error:
838 	/* FATAL error: default upcase table has error */
839 	free_upcase_table(sb);
840 	return ret;
841 }
842 
load_upcase_table(struct super_block * sb)843 s32 load_upcase_table(struct super_block *sb)
844 {
845 	int i;
846 	u32 tbl_clu, tbl_size;
847 	sector_t sector;
848 	u32 type, num_sectors;
849 	struct chain_t clu;
850 	struct case_dentry_t *ep;
851 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
852 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
853 
854 	clu.dir = p_fs->root_dir;
855 	clu.flags = 0x01;
856 
857 	if (p_fs->dev_ejected)
858 		return FFS_MEDIAERR;
859 
860 	while (clu.dir != CLUSTER_32(~0)) {
861 		for (i = 0; i < p_fs->dentries_per_clu; i++) {
862 			ep = (struct case_dentry_t *)get_entry_in_dir(sb, &clu,
863 								      i, NULL);
864 			if (!ep)
865 				return FFS_MEDIAERR;
866 
867 			type = p_fs->fs_func->get_entry_type((struct dentry_t *)ep);
868 
869 			if (type == TYPE_UNUSED)
870 				break;
871 			if (type != TYPE_UPCASE)
872 				continue;
873 
874 			tbl_clu  = GET32_A(ep->start_clu);
875 			tbl_size = (u32)GET64_A(ep->size);
876 
877 			sector = START_SECTOR(tbl_clu);
878 			num_sectors = ((tbl_size - 1) >> p_bd->sector_size_bits) + 1;
879 			if (__load_upcase_table(sb, sector, num_sectors,
880 						GET32_A(ep->checksum)) != FFS_SUCCESS)
881 				break;
882 			return FFS_SUCCESS;
883 		}
884 		if (FAT_read(sb, clu.dir, &clu.dir) != 0)
885 			return FFS_MEDIAERR;
886 	}
887 	/* load default upcase table */
888 	return __load_default_upcase_table(sb);
889 }
890 
free_upcase_table(struct super_block * sb)891 void free_upcase_table(struct super_block *sb)
892 {
893 	u32 i;
894 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
895 	u16 **upcase_table;
896 
897 	upcase_table = p_fs->vol_utbl;
898 	for (i = 0; i < UTBL_COL_COUNT; i++)
899 		kfree(upcase_table[i]);
900 
901 	kfree(p_fs->vol_utbl);
902 	p_fs->vol_utbl = NULL;
903 }
904 
905 /*
906  *  Directory Entry Management Functions
907  */
908 
fat_get_entry_type(struct dentry_t * p_entry)909 u32 fat_get_entry_type(struct dentry_t *p_entry)
910 {
911 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
912 
913 	if (*(ep->name) == 0x0)
914 		return TYPE_UNUSED;
915 
916 	else if (*(ep->name) == 0xE5)
917 		return TYPE_DELETED;
918 
919 	else if (ep->attr == ATTR_EXTEND)
920 		return TYPE_EXTEND;
921 
922 	else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_VOLUME)
923 		return TYPE_VOLUME;
924 
925 	else if ((ep->attr & (ATTR_SUBDIR | ATTR_VOLUME)) == ATTR_SUBDIR)
926 		return TYPE_DIR;
927 
928 	return TYPE_FILE;
929 }
930 
exfat_get_entry_type(struct dentry_t * p_entry)931 u32 exfat_get_entry_type(struct dentry_t *p_entry)
932 {
933 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
934 
935 	if (ep->type == 0x0) {
936 		return TYPE_UNUSED;
937 	} else if (ep->type < 0x80) {
938 		return TYPE_DELETED;
939 	} else if (ep->type == 0x80) {
940 		return TYPE_INVALID;
941 	} else if (ep->type < 0xA0) {
942 		if (ep->type == 0x81) {
943 			return TYPE_BITMAP;
944 		} else if (ep->type == 0x82) {
945 			return TYPE_UPCASE;
946 		} else if (ep->type == 0x83) {
947 			return TYPE_VOLUME;
948 		} else if (ep->type == 0x85) {
949 			if (GET16_A(ep->attr) & ATTR_SUBDIR)
950 				return TYPE_DIR;
951 			else
952 				return TYPE_FILE;
953 		}
954 		return TYPE_CRITICAL_PRI;
955 	} else if (ep->type < 0xC0) {
956 		if (ep->type == 0xA0)
957 			return TYPE_GUID;
958 		else if (ep->type == 0xA1)
959 			return TYPE_PADDING;
960 		else if (ep->type == 0xA2)
961 			return TYPE_ACLTAB;
962 		return TYPE_BENIGN_PRI;
963 	} else if (ep->type < 0xE0) {
964 		if (ep->type == 0xC0)
965 			return TYPE_STREAM;
966 		else if (ep->type == 0xC1)
967 			return TYPE_EXTEND;
968 		else if (ep->type == 0xC2)
969 			return TYPE_ACL;
970 		return TYPE_CRITICAL_SEC;
971 	}
972 
973 	return TYPE_BENIGN_SEC;
974 }
975 
fat_set_entry_type(struct dentry_t * p_entry,u32 type)976 void fat_set_entry_type(struct dentry_t *p_entry, u32 type)
977 {
978 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
979 
980 	if (type == TYPE_UNUSED)
981 		*(ep->name) = 0x0;
982 
983 	else if (type == TYPE_DELETED)
984 		*(ep->name) = 0xE5;
985 
986 	else if (type == TYPE_EXTEND)
987 		ep->attr = ATTR_EXTEND;
988 
989 	else if (type == TYPE_DIR)
990 		ep->attr = ATTR_SUBDIR;
991 
992 	else if (type == TYPE_FILE)
993 		ep->attr = ATTR_ARCHIVE;
994 
995 	else if (type == TYPE_SYMLINK)
996 		ep->attr = ATTR_ARCHIVE | ATTR_SYMLINK;
997 }
998 
exfat_set_entry_type(struct dentry_t * p_entry,u32 type)999 void exfat_set_entry_type(struct dentry_t *p_entry, u32 type)
1000 {
1001 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1002 
1003 	if (type == TYPE_UNUSED) {
1004 		ep->type = 0x0;
1005 	} else if (type == TYPE_DELETED) {
1006 		ep->type &= ~0x80;
1007 	} else if (type == TYPE_STREAM) {
1008 		ep->type = 0xC0;
1009 	} else if (type == TYPE_EXTEND) {
1010 		ep->type = 0xC1;
1011 	} else if (type == TYPE_BITMAP) {
1012 		ep->type = 0x81;
1013 	} else if (type == TYPE_UPCASE) {
1014 		ep->type = 0x82;
1015 	} else if (type == TYPE_VOLUME) {
1016 		ep->type = 0x83;
1017 	} else if (type == TYPE_DIR) {
1018 		ep->type = 0x85;
1019 		SET16_A(ep->attr, ATTR_SUBDIR);
1020 	} else if (type == TYPE_FILE) {
1021 		ep->type = 0x85;
1022 		SET16_A(ep->attr, ATTR_ARCHIVE);
1023 	} else if (type == TYPE_SYMLINK) {
1024 		ep->type = 0x85;
1025 		SET16_A(ep->attr, ATTR_ARCHIVE | ATTR_SYMLINK);
1026 	}
1027 }
1028 
fat_get_entry_attr(struct dentry_t * p_entry)1029 u32 fat_get_entry_attr(struct dentry_t *p_entry)
1030 {
1031 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1032 
1033 	return (u32)ep->attr;
1034 }
1035 
exfat_get_entry_attr(struct dentry_t * p_entry)1036 u32 exfat_get_entry_attr(struct dentry_t *p_entry)
1037 {
1038 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1039 
1040 	return (u32)GET16_A(ep->attr);
1041 }
1042 
fat_set_entry_attr(struct dentry_t * p_entry,u32 attr)1043 void fat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
1044 {
1045 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1046 
1047 	ep->attr = (u8)attr;
1048 }
1049 
exfat_set_entry_attr(struct dentry_t * p_entry,u32 attr)1050 void exfat_set_entry_attr(struct dentry_t *p_entry, u32 attr)
1051 {
1052 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1053 
1054 	SET16_A(ep->attr, (u16)attr);
1055 }
1056 
fat_get_entry_flag(struct dentry_t * p_entry)1057 u8 fat_get_entry_flag(struct dentry_t *p_entry)
1058 {
1059 	return 0x01;
1060 }
1061 
exfat_get_entry_flag(struct dentry_t * p_entry)1062 u8 exfat_get_entry_flag(struct dentry_t *p_entry)
1063 {
1064 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1065 
1066 	return ep->flags;
1067 }
1068 
fat_set_entry_flag(struct dentry_t * p_entry,u8 flags)1069 void fat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
1070 {
1071 }
1072 
exfat_set_entry_flag(struct dentry_t * p_entry,u8 flags)1073 void exfat_set_entry_flag(struct dentry_t *p_entry, u8 flags)
1074 {
1075 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1076 
1077 	ep->flags = flags;
1078 }
1079 
fat_get_entry_clu0(struct dentry_t * p_entry)1080 u32 fat_get_entry_clu0(struct dentry_t *p_entry)
1081 {
1082 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1083 
1084 	return ((u32)GET16_A(ep->start_clu_hi) << 16) |
1085 		GET16_A(ep->start_clu_lo);
1086 }
1087 
exfat_get_entry_clu0(struct dentry_t * p_entry)1088 u32 exfat_get_entry_clu0(struct dentry_t *p_entry)
1089 {
1090 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1091 
1092 	return GET32_A(ep->start_clu);
1093 }
1094 
fat_set_entry_clu0(struct dentry_t * p_entry,u32 start_clu)1095 void fat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
1096 {
1097 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1098 
1099 	SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
1100 	SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
1101 }
1102 
exfat_set_entry_clu0(struct dentry_t * p_entry,u32 start_clu)1103 void exfat_set_entry_clu0(struct dentry_t *p_entry, u32 start_clu)
1104 {
1105 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1106 
1107 	SET32_A(ep->start_clu, start_clu);
1108 }
1109 
fat_get_entry_size(struct dentry_t * p_entry)1110 u64 fat_get_entry_size(struct dentry_t *p_entry)
1111 {
1112 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1113 
1114 	return (u64)GET32_A(ep->size);
1115 }
1116 
exfat_get_entry_size(struct dentry_t * p_entry)1117 u64 exfat_get_entry_size(struct dentry_t *p_entry)
1118 {
1119 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1120 
1121 	return GET64_A(ep->valid_size);
1122 }
1123 
fat_set_entry_size(struct dentry_t * p_entry,u64 size)1124 void fat_set_entry_size(struct dentry_t *p_entry, u64 size)
1125 {
1126 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1127 
1128 	SET32_A(ep->size, (u32)size);
1129 }
1130 
exfat_set_entry_size(struct dentry_t * p_entry,u64 size)1131 void exfat_set_entry_size(struct dentry_t *p_entry, u64 size)
1132 {
1133 	struct strm_dentry_t *ep = (struct strm_dentry_t *)p_entry;
1134 
1135 	SET64_A(ep->valid_size, size);
1136 	SET64_A(ep->size, size);
1137 }
1138 
fat_get_entry_time(struct dentry_t * p_entry,struct timestamp_t * tp,u8 mode)1139 void fat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1140 			u8 mode)
1141 {
1142 	u16 t = 0x00, d = 0x21;
1143 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1144 
1145 	switch (mode) {
1146 	case TM_CREATE:
1147 		t = GET16_A(ep->create_time);
1148 		d = GET16_A(ep->create_date);
1149 		break;
1150 	case TM_MODIFY:
1151 		t = GET16_A(ep->modify_time);
1152 		d = GET16_A(ep->modify_date);
1153 		break;
1154 	}
1155 
1156 	tp->sec  = (t & 0x001F) << 1;
1157 	tp->min  = (t >> 5) & 0x003F;
1158 	tp->hour = (t >> 11);
1159 	tp->day  = (d & 0x001F);
1160 	tp->mon  = (d >> 5) & 0x000F;
1161 	tp->year = (d >> 9);
1162 }
1163 
exfat_get_entry_time(struct dentry_t * p_entry,struct timestamp_t * tp,u8 mode)1164 void exfat_get_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1165 			  u8 mode)
1166 {
1167 	u16 t = 0x00, d = 0x21;
1168 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1169 
1170 	switch (mode) {
1171 	case TM_CREATE:
1172 		t = GET16_A(ep->create_time);
1173 		d = GET16_A(ep->create_date);
1174 		break;
1175 	case TM_MODIFY:
1176 		t = GET16_A(ep->modify_time);
1177 		d = GET16_A(ep->modify_date);
1178 		break;
1179 	case TM_ACCESS:
1180 		t = GET16_A(ep->access_time);
1181 		d = GET16_A(ep->access_date);
1182 		break;
1183 	}
1184 
1185 	tp->sec  = (t & 0x001F) << 1;
1186 	tp->min  = (t >> 5) & 0x003F;
1187 	tp->hour = (t >> 11);
1188 	tp->day  = (d & 0x001F);
1189 	tp->mon  = (d >> 5) & 0x000F;
1190 	tp->year = (d >> 9);
1191 }
1192 
fat_set_entry_time(struct dentry_t * p_entry,struct timestamp_t * tp,u8 mode)1193 void fat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1194 			u8 mode)
1195 {
1196 	u16 t, d;
1197 	struct dos_dentry_t *ep = (struct dos_dentry_t *)p_entry;
1198 
1199 	t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
1200 	d = (tp->year <<  9) | (tp->mon << 5) |  tp->day;
1201 
1202 	switch (mode) {
1203 	case TM_CREATE:
1204 		SET16_A(ep->create_time, t);
1205 		SET16_A(ep->create_date, d);
1206 		break;
1207 	case TM_MODIFY:
1208 		SET16_A(ep->modify_time, t);
1209 		SET16_A(ep->modify_date, d);
1210 		break;
1211 	}
1212 }
1213 
exfat_set_entry_time(struct dentry_t * p_entry,struct timestamp_t * tp,u8 mode)1214 void exfat_set_entry_time(struct dentry_t *p_entry, struct timestamp_t *tp,
1215 			  u8 mode)
1216 {
1217 	u16 t, d;
1218 	struct file_dentry_t *ep = (struct file_dentry_t *)p_entry;
1219 
1220 	t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
1221 	d = (tp->year <<  9) | (tp->mon << 5) |  tp->day;
1222 
1223 	switch (mode) {
1224 	case TM_CREATE:
1225 		SET16_A(ep->create_time, t);
1226 		SET16_A(ep->create_date, d);
1227 		break;
1228 	case TM_MODIFY:
1229 		SET16_A(ep->modify_time, t);
1230 		SET16_A(ep->modify_date, d);
1231 		break;
1232 	case TM_ACCESS:
1233 		SET16_A(ep->access_time, t);
1234 		SET16_A(ep->access_date, d);
1235 		break;
1236 	}
1237 }
1238 
fat_init_dir_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,u32 type,u32 start_clu,u64 size)1239 s32 fat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1240 		       u32 type, u32 start_clu, u64 size)
1241 {
1242 	sector_t sector;
1243 	struct dos_dentry_t *dos_ep;
1244 
1245 	dos_ep = (struct dos_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1246 							 &sector);
1247 	if (!dos_ep)
1248 		return FFS_MEDIAERR;
1249 
1250 	init_dos_entry(dos_ep, type, start_clu);
1251 	buf_modify(sb, sector);
1252 
1253 	return FFS_SUCCESS;
1254 }
1255 
exfat_init_dir_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,u32 type,u32 start_clu,u64 size)1256 s32 exfat_init_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1257 			 s32 entry, u32 type, u32 start_clu, u64 size)
1258 {
1259 	sector_t sector;
1260 	u8 flags;
1261 	struct file_dentry_t *file_ep;
1262 	struct strm_dentry_t *strm_ep;
1263 
1264 	flags = (type == TYPE_FILE) ? 0x01 : 0x03;
1265 
1266 	/* we cannot use get_entry_set_in_dir here because file ep is not initialized yet */
1267 	file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1268 							   &sector);
1269 	if (!file_ep)
1270 		return FFS_MEDIAERR;
1271 
1272 	strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1273 							   &sector);
1274 	if (!strm_ep)
1275 		return FFS_MEDIAERR;
1276 
1277 	init_file_entry(file_ep, type);
1278 	buf_modify(sb, sector);
1279 
1280 	init_strm_entry(strm_ep, flags, start_clu, size);
1281 	buf_modify(sb, sector);
1282 
1283 	return FFS_SUCCESS;
1284 }
1285 
fat_init_ext_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,s32 num_entries,struct uni_name_t * p_uniname,struct dos_name_t * p_dosname)1286 static s32 fat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1287 			      s32 entry, s32 num_entries,
1288 			      struct uni_name_t *p_uniname,
1289 			      struct dos_name_t *p_dosname)
1290 {
1291 	int i;
1292 	sector_t sector;
1293 	u8 chksum;
1294 	u16 *uniname = p_uniname->name;
1295 	struct dos_dentry_t *dos_ep;
1296 	struct ext_dentry_t *ext_ep;
1297 
1298 	dos_ep = (struct dos_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1299 							 &sector);
1300 	if (!dos_ep)
1301 		return FFS_MEDIAERR;
1302 
1303 	dos_ep->lcase = p_dosname->name_case;
1304 	memcpy(dos_ep->name, p_dosname->name, DOS_NAME_LENGTH);
1305 	buf_modify(sb, sector);
1306 
1307 	if ((--num_entries) > 0) {
1308 		chksum = calc_checksum_1byte((void *)dos_ep->name,
1309 					     DOS_NAME_LENGTH, 0);
1310 
1311 		for (i = 1; i < num_entries; i++) {
1312 			ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb,
1313 									 p_dir,
1314 									 entry - i,
1315 									 &sector);
1316 			if (!ext_ep)
1317 				return FFS_MEDIAERR;
1318 
1319 			init_ext_entry(ext_ep, i, chksum, uniname);
1320 			buf_modify(sb, sector);
1321 			uniname += 13;
1322 		}
1323 
1324 		ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir,
1325 								 entry - i,
1326 								 &sector);
1327 		if (!ext_ep)
1328 			return FFS_MEDIAERR;
1329 
1330 		init_ext_entry(ext_ep, i + 0x40, chksum, uniname);
1331 		buf_modify(sb, sector);
1332 	}
1333 
1334 	return FFS_SUCCESS;
1335 }
1336 
exfat_init_ext_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,s32 num_entries,struct uni_name_t * p_uniname,struct dos_name_t * p_dosname)1337 static s32 exfat_init_ext_entry(struct super_block *sb, struct chain_t *p_dir,
1338 				s32 entry, s32 num_entries,
1339 				struct uni_name_t *p_uniname,
1340 				struct dos_name_t *p_dosname)
1341 {
1342 	int i;
1343 	sector_t sector;
1344 	u16 *uniname = p_uniname->name;
1345 	struct file_dentry_t *file_ep;
1346 	struct strm_dentry_t *strm_ep;
1347 	struct name_dentry_t *name_ep;
1348 
1349 	file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1350 							   &sector);
1351 	if (!file_ep)
1352 		return FFS_MEDIAERR;
1353 
1354 	file_ep->num_ext = (u8)(num_entries - 1);
1355 	buf_modify(sb, sector);
1356 
1357 	strm_ep = (struct strm_dentry_t *)get_entry_in_dir(sb, p_dir, entry + 1,
1358 							   &sector);
1359 	if (!strm_ep)
1360 		return FFS_MEDIAERR;
1361 
1362 	strm_ep->name_len = p_uniname->name_len;
1363 	SET16_A(strm_ep->name_hash, p_uniname->name_hash);
1364 	buf_modify(sb, sector);
1365 
1366 	for (i = 2; i < num_entries; i++) {
1367 		name_ep = (struct name_dentry_t *)get_entry_in_dir(sb, p_dir,
1368 								   entry + i,
1369 								   &sector);
1370 		if (!name_ep)
1371 			return FFS_MEDIAERR;
1372 
1373 		init_name_entry(name_ep, uniname);
1374 		buf_modify(sb, sector);
1375 		uniname += 15;
1376 	}
1377 
1378 	update_dir_checksum(sb, p_dir, entry);
1379 
1380 	return FFS_SUCCESS;
1381 }
1382 
init_dos_entry(struct dos_dentry_t * ep,u32 type,u32 start_clu)1383 void init_dos_entry(struct dos_dentry_t *ep, u32 type, u32 start_clu)
1384 {
1385 	struct timestamp_t tm, *tp;
1386 
1387 	fat_set_entry_type((struct dentry_t *)ep, type);
1388 	SET16_A(ep->start_clu_lo, CLUSTER_16(start_clu));
1389 	SET16_A(ep->start_clu_hi, CLUSTER_16(start_clu >> 16));
1390 	SET32_A(ep->size, 0);
1391 
1392 	tp = tm_current(&tm);
1393 	fat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
1394 	fat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
1395 	SET16_A(ep->access_date, 0);
1396 	ep->create_time_ms = 0;
1397 }
1398 
init_ext_entry(struct ext_dentry_t * ep,s32 order,u8 chksum,u16 * uniname)1399 void init_ext_entry(struct ext_dentry_t *ep, s32 order, u8 chksum, u16 *uniname)
1400 {
1401 	int i;
1402 	bool end = false;
1403 
1404 	fat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1405 	ep->order = (u8)order;
1406 	ep->sysid = 0;
1407 	ep->checksum = chksum;
1408 	SET16_A(ep->start_clu, 0);
1409 
1410 	for (i = 0; i < 10; i += 2) {
1411 		if (!end) {
1412 			SET16(ep->unicode_0_4 + i, *uniname);
1413 			if (*uniname == 0x0)
1414 				end = true;
1415 			else
1416 				uniname++;
1417 		} else {
1418 			SET16(ep->unicode_0_4 + i, 0xFFFF);
1419 		}
1420 	}
1421 
1422 	for (i = 0; i < 12; i += 2) {
1423 		if (!end) {
1424 			SET16_A(ep->unicode_5_10 + i, *uniname);
1425 			if (*uniname == 0x0)
1426 				end = true;
1427 			else
1428 				uniname++;
1429 		} else {
1430 			SET16_A(ep->unicode_5_10 + i, 0xFFFF);
1431 		}
1432 	}
1433 
1434 	for (i = 0; i < 4; i += 2) {
1435 		if (!end) {
1436 			SET16_A(ep->unicode_11_12 + i, *uniname);
1437 			if (*uniname == 0x0)
1438 				end = true;
1439 			else
1440 				uniname++;
1441 		} else {
1442 			SET16_A(ep->unicode_11_12 + i, 0xFFFF);
1443 		}
1444 	}
1445 }
1446 
init_file_entry(struct file_dentry_t * ep,u32 type)1447 void init_file_entry(struct file_dentry_t *ep, u32 type)
1448 {
1449 	struct timestamp_t tm, *tp;
1450 
1451 	exfat_set_entry_type((struct dentry_t *)ep, type);
1452 
1453 	tp = tm_current(&tm);
1454 	exfat_set_entry_time((struct dentry_t *)ep, tp, TM_CREATE);
1455 	exfat_set_entry_time((struct dentry_t *)ep, tp, TM_MODIFY);
1456 	exfat_set_entry_time((struct dentry_t *)ep, tp, TM_ACCESS);
1457 	ep->create_time_ms = 0;
1458 	ep->modify_time_ms = 0;
1459 	ep->access_time_ms = 0;
1460 }
1461 
init_strm_entry(struct strm_dentry_t * ep,u8 flags,u32 start_clu,u64 size)1462 void init_strm_entry(struct strm_dentry_t *ep, u8 flags, u32 start_clu, u64 size)
1463 {
1464 	exfat_set_entry_type((struct dentry_t *)ep, TYPE_STREAM);
1465 	ep->flags = flags;
1466 	SET32_A(ep->start_clu, start_clu);
1467 	SET64_A(ep->valid_size, size);
1468 	SET64_A(ep->size, size);
1469 }
1470 
init_name_entry(struct name_dentry_t * ep,u16 * uniname)1471 void init_name_entry(struct name_dentry_t *ep, u16 *uniname)
1472 {
1473 	int i;
1474 
1475 	exfat_set_entry_type((struct dentry_t *)ep, TYPE_EXTEND);
1476 	ep->flags = 0x0;
1477 
1478 	for (i = 0; i < 30; i++, i++) {
1479 		SET16_A(ep->unicode_0_14 + i, *uniname);
1480 		if (*uniname == 0x0)
1481 			break;
1482 		uniname++;
1483 	}
1484 }
1485 
fat_delete_dir_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,s32 order,s32 num_entries)1486 void fat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1487 		s32 entry, s32 order, s32 num_entries)
1488 {
1489 	int i;
1490 	sector_t sector;
1491 	struct dentry_t *ep;
1492 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1493 
1494 	for (i = num_entries - 1; i >= order; i--) {
1495 		ep = get_entry_in_dir(sb, p_dir, entry - i, &sector);
1496 		if (!ep)
1497 			return;
1498 
1499 		p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1500 		buf_modify(sb, sector);
1501 	}
1502 }
1503 
exfat_delete_dir_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,s32 order,s32 num_entries)1504 void exfat_delete_dir_entry(struct super_block *sb, struct chain_t *p_dir,
1505 		s32 entry, s32 order, s32 num_entries)
1506 {
1507 	int i;
1508 	sector_t sector;
1509 	struct dentry_t *ep;
1510 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1511 
1512 	for (i = order; i < num_entries; i++) {
1513 		ep = get_entry_in_dir(sb, p_dir, entry + i, &sector);
1514 		if (!ep)
1515 			return;
1516 
1517 		p_fs->fs_func->set_entry_type(ep, TYPE_DELETED);
1518 		buf_modify(sb, sector);
1519 	}
1520 }
1521 
update_dir_checksum(struct super_block * sb,struct chain_t * p_dir,s32 entry)1522 void update_dir_checksum(struct super_block *sb, struct chain_t *p_dir,
1523 			 s32 entry)
1524 {
1525 	int i, num_entries;
1526 	sector_t sector;
1527 	u16 chksum;
1528 	struct file_dentry_t *file_ep;
1529 	struct dentry_t *ep;
1530 
1531 	file_ep = (struct file_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
1532 							   &sector);
1533 	if (!file_ep)
1534 		return;
1535 
1536 	buf_lock(sb, sector);
1537 
1538 	num_entries = (s32)file_ep->num_ext + 1;
1539 	chksum = calc_checksum_2byte((void *)file_ep, DENTRY_SIZE, 0,
1540 				     CS_DIR_ENTRY);
1541 
1542 	for (i = 1; i < num_entries; i++) {
1543 		ep = get_entry_in_dir(sb, p_dir, entry + i, NULL);
1544 		if (!ep) {
1545 			buf_unlock(sb, sector);
1546 			return;
1547 		}
1548 
1549 		chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1550 					     CS_DEFAULT);
1551 	}
1552 
1553 	SET16_A(file_ep->checksum, chksum);
1554 	buf_modify(sb, sector);
1555 	buf_unlock(sb, sector);
1556 }
1557 
update_dir_checksum_with_entry_set(struct super_block * sb,struct entry_set_cache_t * es)1558 void update_dir_checksum_with_entry_set(struct super_block *sb,
1559 					struct entry_set_cache_t *es)
1560 {
1561 	struct dentry_t *ep;
1562 	u16 chksum = 0;
1563 	s32 chksum_type = CS_DIR_ENTRY, i;
1564 
1565 	ep = (struct dentry_t *)&(es->__buf);
1566 	for (i = 0; i < es->num_entries; i++) {
1567 		pr_debug("%s ep %p\n", __func__, ep);
1568 		chksum = calc_checksum_2byte((void *)ep, DENTRY_SIZE, chksum,
1569 					     chksum_type);
1570 		ep++;
1571 		chksum_type = CS_DEFAULT;
1572 	}
1573 
1574 	ep = (struct dentry_t *)&(es->__buf);
1575 	SET16_A(((struct file_dentry_t *)ep)->checksum, chksum);
1576 	write_whole_entry_set(sb, es);
1577 }
1578 
_walk_fat_chain(struct super_block * sb,struct chain_t * p_dir,s32 byte_offset,u32 * clu)1579 static s32 _walk_fat_chain(struct super_block *sb, struct chain_t *p_dir,
1580 			   s32 byte_offset, u32 *clu)
1581 {
1582 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1583 	s32 clu_offset;
1584 	u32 cur_clu;
1585 
1586 	clu_offset = byte_offset >> p_fs->cluster_size_bits;
1587 	cur_clu = p_dir->dir;
1588 
1589 	if (p_dir->flags == 0x03) {
1590 		cur_clu += clu_offset;
1591 	} else {
1592 		while (clu_offset > 0) {
1593 			if (FAT_read(sb, cur_clu, &cur_clu) == -1)
1594 				return FFS_MEDIAERR;
1595 			clu_offset--;
1596 		}
1597 	}
1598 
1599 	if (clu)
1600 		*clu = cur_clu;
1601 	return FFS_SUCCESS;
1602 }
1603 
find_location(struct super_block * sb,struct chain_t * p_dir,s32 entry,sector_t * sector,s32 * offset)1604 s32 find_location(struct super_block *sb, struct chain_t *p_dir, s32 entry,
1605 		  sector_t *sector, s32 *offset)
1606 {
1607 	s32 off, ret;
1608 	u32 clu = 0;
1609 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1610 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1611 
1612 	off = entry << DENTRY_SIZE_BITS;
1613 
1614 	if (p_dir->dir == CLUSTER_32(0)) { /* FAT16 root_dir */
1615 		*offset = off & p_bd->sector_size_mask;
1616 		*sector = off >> p_bd->sector_size_bits;
1617 		*sector += p_fs->root_start_sector;
1618 	} else {
1619 		ret = _walk_fat_chain(sb, p_dir, off, &clu);
1620 		if (ret != FFS_SUCCESS)
1621 			return ret;
1622 
1623 		/* byte offset in cluster */
1624 		off &= p_fs->cluster_size - 1;
1625 
1626 		/* byte offset in sector    */
1627 		*offset = off & p_bd->sector_size_mask;
1628 
1629 		/* sector offset in cluster */
1630 		*sector = off >> p_bd->sector_size_bits;
1631 		*sector += START_SECTOR(clu);
1632 	}
1633 	return FFS_SUCCESS;
1634 }
1635 
get_entry_with_sector(struct super_block * sb,sector_t sector,s32 offset)1636 struct dentry_t *get_entry_with_sector(struct super_block *sb, sector_t sector,
1637 				       s32 offset)
1638 {
1639 	u8 *buf;
1640 
1641 	buf = buf_getblk(sb, sector);
1642 
1643 	if (!buf)
1644 		return NULL;
1645 
1646 	return (struct dentry_t *)(buf + offset);
1647 }
1648 
get_entry_in_dir(struct super_block * sb,struct chain_t * p_dir,s32 entry,sector_t * sector)1649 struct dentry_t *get_entry_in_dir(struct super_block *sb, struct chain_t *p_dir,
1650 				  s32 entry, sector_t *sector)
1651 {
1652 	s32 off;
1653 	sector_t sec;
1654 	u8 *buf;
1655 
1656 	if (find_location(sb, p_dir, entry, &sec, &off) != FFS_SUCCESS)
1657 		return NULL;
1658 
1659 	buf = buf_getblk(sb, sec);
1660 
1661 	if (!buf)
1662 		return NULL;
1663 
1664 	if (sector)
1665 		*sector = sec;
1666 	return (struct dentry_t *)(buf + off);
1667 }
1668 
1669 /* returns a set of dentries for a file or dir.
1670  * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
1671  * to apply changes made in this entry set to the real device.
1672  * in:
1673  *   sb+p_dir+entry: indicates a file/dir
1674  *   type:  specifies how many dentries should be included.
1675  * out:
1676  *   file_ep: will point the first dentry(= file dentry) on success
1677  * return:
1678  *   pointer of entry set on success,
1679  *   NULL on failure.
1680  */
1681 
1682 #define ES_MODE_STARTED				0
1683 #define ES_MODE_GET_FILE_ENTRY			1
1684 #define ES_MODE_GET_STRM_ENTRY			2
1685 #define ES_MODE_GET_NAME_ENTRY			3
1686 #define ES_MODE_GET_CRITICAL_SEC_ENTRY		4
get_entry_set_in_dir(struct super_block * sb,struct chain_t * p_dir,s32 entry,u32 type,struct dentry_t ** file_ep)1687 struct entry_set_cache_t *get_entry_set_in_dir(struct super_block *sb,
1688 					       struct chain_t *p_dir, s32 entry,
1689 					       u32 type,
1690 					       struct dentry_t **file_ep)
1691 {
1692 	s32 off, ret, byte_offset;
1693 	u32 clu = 0;
1694 	sector_t sec;
1695 	u32 entry_type;
1696 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1697 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1698 	struct entry_set_cache_t *es = NULL;
1699 	struct dentry_t *ep, *pos;
1700 	u8 *buf;
1701 	u8 num_entries;
1702 	s32 mode = ES_MODE_STARTED;
1703 	size_t bufsize;
1704 
1705 	pr_debug("%s entered p_dir dir %u flags %x size %d\n",
1706 		__func__, p_dir->dir, p_dir->flags, p_dir->size);
1707 
1708 	byte_offset = entry << DENTRY_SIZE_BITS;
1709 	ret = _walk_fat_chain(sb, p_dir, byte_offset, &clu);
1710 	if (ret != FFS_SUCCESS)
1711 		return NULL;
1712 
1713 	/* byte offset in cluster */
1714 	byte_offset &= p_fs->cluster_size - 1;
1715 
1716 	/* byte offset in sector    */
1717 	off = byte_offset & p_bd->sector_size_mask;
1718 
1719 	/* sector offset in cluster */
1720 	sec = byte_offset >> p_bd->sector_size_bits;
1721 	sec += START_SECTOR(clu);
1722 
1723 	buf = buf_getblk(sb, sec);
1724 	if (!buf)
1725 		goto err_out;
1726 
1727 	ep = (struct dentry_t *)(buf + off);
1728 	entry_type = p_fs->fs_func->get_entry_type(ep);
1729 
1730 	if ((entry_type != TYPE_FILE)
1731 		&& (entry_type != TYPE_DIR))
1732 		goto err_out;
1733 
1734 	if (type == ES_ALL_ENTRIES)
1735 		num_entries = ((struct file_dentry_t *)ep)->num_ext + 1;
1736 	else
1737 		num_entries = type;
1738 
1739 	bufsize = offsetof(struct entry_set_cache_t, __buf) + (num_entries) *
1740 		  sizeof(struct dentry_t);
1741 	pr_debug("%s: trying to kmalloc %zx bytes for %d entries\n", __func__,
1742 		 bufsize, num_entries);
1743 	es = kmalloc(bufsize, GFP_KERNEL);
1744 	if (!es)
1745 		goto err_out;
1746 
1747 	es->num_entries = num_entries;
1748 	es->sector = sec;
1749 	es->offset = off;
1750 	es->alloc_flag = p_dir->flags;
1751 
1752 	pos = (struct dentry_t *)&es->__buf;
1753 
1754 	while (num_entries) {
1755 		/*
1756 		 * instead of copying whole sector, we will check every entry.
1757 		 * this will provide minimum stablity and consistency.
1758 		 */
1759 		entry_type = p_fs->fs_func->get_entry_type(ep);
1760 
1761 		if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
1762 			goto err_out;
1763 
1764 		switch (mode) {
1765 		case ES_MODE_STARTED:
1766 			if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
1767 				mode = ES_MODE_GET_FILE_ENTRY;
1768 			else
1769 				goto err_out;
1770 			break;
1771 		case ES_MODE_GET_FILE_ENTRY:
1772 			if (entry_type == TYPE_STREAM)
1773 				mode = ES_MODE_GET_STRM_ENTRY;
1774 			else
1775 				goto err_out;
1776 			break;
1777 		case ES_MODE_GET_STRM_ENTRY:
1778 			if (entry_type == TYPE_EXTEND)
1779 				mode = ES_MODE_GET_NAME_ENTRY;
1780 			else
1781 				goto err_out;
1782 			break;
1783 		case ES_MODE_GET_NAME_ENTRY:
1784 			if (entry_type == TYPE_EXTEND)
1785 				break;
1786 			else if (entry_type == TYPE_STREAM)
1787 				goto err_out;
1788 			else if (entry_type & TYPE_CRITICAL_SEC)
1789 				mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
1790 			else
1791 				goto err_out;
1792 			break;
1793 		case ES_MODE_GET_CRITICAL_SEC_ENTRY:
1794 			if ((entry_type == TYPE_EXTEND) ||
1795 			    (entry_type == TYPE_STREAM))
1796 				goto err_out;
1797 			else if ((entry_type & TYPE_CRITICAL_SEC) !=
1798 				 TYPE_CRITICAL_SEC)
1799 				goto err_out;
1800 			break;
1801 		}
1802 
1803 		memcpy(pos, ep, sizeof(struct dentry_t));
1804 
1805 		if (--num_entries == 0)
1806 			break;
1807 
1808 		if (((off + DENTRY_SIZE) & p_bd->sector_size_mask) <
1809 		    (off &  p_bd->sector_size_mask)) {
1810 			/* get the next sector */
1811 			if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1812 				if (es->alloc_flag == 0x03) {
1813 					clu++;
1814 				} else {
1815 					if (FAT_read(sb, clu, &clu) == -1)
1816 						goto err_out;
1817 				}
1818 				sec = START_SECTOR(clu);
1819 			} else {
1820 				sec++;
1821 			}
1822 			buf = buf_getblk(sb, sec);
1823 			if (!buf)
1824 				goto err_out;
1825 			off = 0;
1826 			ep = (struct dentry_t *)(buf);
1827 		} else {
1828 			ep++;
1829 			off += DENTRY_SIZE;
1830 		}
1831 		pos++;
1832 	}
1833 
1834 	if (file_ep)
1835 		*file_ep = (struct dentry_t *)&(es->__buf);
1836 
1837 	pr_debug("%s exiting es %p sec %llu offset %d flags %d, num_entries %u buf ptr %p\n",
1838 		   __func__, es, (unsigned long long)es->sector, es->offset,
1839 		   es->alloc_flag, es->num_entries, &es->__buf);
1840 	return es;
1841 err_out:
1842 	pr_debug("%s exited NULL (es %p)\n", __func__, es);
1843 	kfree(es);
1844 	return NULL;
1845 }
1846 
release_entry_set(struct entry_set_cache_t * es)1847 void release_entry_set(struct entry_set_cache_t *es)
1848 {
1849 	pr_debug("%s es=%p\n", __func__, es);
1850 	kfree(es);
1851 }
1852 
__write_partial_entries_in_entry_set(struct super_block * sb,struct entry_set_cache_t * es,sector_t sec,s32 off,u32 count)1853 static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
1854 						struct entry_set_cache_t *es,
1855 						sector_t sec, s32 off, u32 count)
1856 {
1857 	s32 num_entries, buf_off = (off - es->offset);
1858 	u32 remaining_byte_in_sector, copy_entries;
1859 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1860 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1861 	u32 clu;
1862 	u8 *buf, *esbuf = (u8 *)&(es->__buf);
1863 
1864 	pr_debug("%s entered es %p sec %llu off %d count %d\n",
1865 		__func__, es, (unsigned long long)sec, off, count);
1866 	num_entries = count;
1867 
1868 	while (num_entries) {
1869 		/* white per sector base */
1870 		remaining_byte_in_sector = (1 << p_bd->sector_size_bits) - off;
1871 		copy_entries = min_t(s32,
1872 				     remaining_byte_in_sector >> DENTRY_SIZE_BITS,
1873 				     num_entries);
1874 		buf = buf_getblk(sb, sec);
1875 		if (!buf)
1876 			goto err_out;
1877 		pr_debug("es->buf %p buf_off %u\n", esbuf, buf_off);
1878 		pr_debug("copying %d entries from %p to sector %llu\n",
1879 			copy_entries, (esbuf + buf_off),
1880 			(unsigned long long)sec);
1881 		memcpy(buf + off, esbuf + buf_off,
1882 		       copy_entries << DENTRY_SIZE_BITS);
1883 		buf_modify(sb, sec);
1884 		num_entries -= copy_entries;
1885 
1886 		if (num_entries) {
1887 			/* get next sector */
1888 			if (IS_LAST_SECTOR_IN_CLUSTER(sec)) {
1889 				clu = GET_CLUSTER_FROM_SECTOR(sec);
1890 				if (es->alloc_flag == 0x03) {
1891 					clu++;
1892 				} else {
1893 					if (FAT_read(sb, clu, &clu) == -1)
1894 						goto err_out;
1895 				}
1896 				sec = START_SECTOR(clu);
1897 			} else {
1898 				sec++;
1899 			}
1900 			off = 0;
1901 			buf_off += copy_entries << DENTRY_SIZE_BITS;
1902 		}
1903 	}
1904 
1905 	pr_debug("%s exited successfully\n", __func__);
1906 	return FFS_SUCCESS;
1907 err_out:
1908 	pr_debug("%s failed\n", __func__);
1909 	return FFS_ERROR;
1910 }
1911 
1912 /* write back all entries in entry set */
write_whole_entry_set(struct super_block * sb,struct entry_set_cache_t * es)1913 s32 write_whole_entry_set(struct super_block *sb, struct entry_set_cache_t *es)
1914 {
1915 	return __write_partial_entries_in_entry_set(sb, es, es->sector,
1916 						    es->offset,
1917 						    es->num_entries);
1918 }
1919 
1920 /* write back some entries in entry set */
write_partial_entries_in_entry_set(struct super_block * sb,struct entry_set_cache_t * es,struct dentry_t * ep,u32 count)1921 s32 write_partial_entries_in_entry_set(struct super_block *sb,
1922 	struct entry_set_cache_t *es, struct dentry_t *ep, u32 count)
1923 {
1924 	s32 ret, byte_offset, off;
1925 	u32 clu = 0;
1926 	sector_t sec;
1927 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1928 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
1929 	struct chain_t dir;
1930 
1931 	/* vaidity check */
1932 	if (ep + count  > ((struct dentry_t *)&(es->__buf)) + es->num_entries)
1933 		return FFS_ERROR;
1934 
1935 	dir.dir = GET_CLUSTER_FROM_SECTOR(es->sector);
1936 	dir.flags = es->alloc_flag;
1937 	dir.size = 0xffffffff;		/* XXX */
1938 
1939 	byte_offset = (es->sector - START_SECTOR(dir.dir)) <<
1940 			p_bd->sector_size_bits;
1941 	byte_offset += ((void **)ep - &(es->__buf)) + es->offset;
1942 
1943 	ret = _walk_fat_chain(sb, &dir, byte_offset, &clu);
1944 	if (ret != FFS_SUCCESS)
1945 		return ret;
1946 
1947 	/* byte offset in cluster */
1948 	byte_offset &= p_fs->cluster_size - 1;
1949 
1950 	/* byte offset in sector    */
1951 	off = byte_offset & p_bd->sector_size_mask;
1952 
1953 	/* sector offset in cluster */
1954 	sec = byte_offset >> p_bd->sector_size_bits;
1955 	sec += START_SECTOR(clu);
1956 	return __write_partial_entries_in_entry_set(sb, es, sec, off, count);
1957 }
1958 
1959 /* search EMPTY CONTINUOUS "num_entries" entries */
search_deleted_or_unused_entry(struct super_block * sb,struct chain_t * p_dir,s32 num_entries)1960 s32 search_deleted_or_unused_entry(struct super_block *sb,
1961 				   struct chain_t *p_dir, s32 num_entries)
1962 {
1963 	int i, dentry, num_empty = 0;
1964 	s32 dentries_per_clu;
1965 	u32 type;
1966 	struct chain_t clu;
1967 	struct dentry_t *ep;
1968 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
1969 
1970 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1971 		dentries_per_clu = p_fs->dentries_in_root;
1972 	else
1973 		dentries_per_clu = p_fs->dentries_per_clu;
1974 
1975 	if (p_fs->hint_uentry.dir == p_dir->dir) {
1976 		if (p_fs->hint_uentry.entry == -1)
1977 			return -1;
1978 
1979 		clu.dir = p_fs->hint_uentry.clu.dir;
1980 		clu.size = p_fs->hint_uentry.clu.size;
1981 		clu.flags = p_fs->hint_uentry.clu.flags;
1982 
1983 		dentry = p_fs->hint_uentry.entry;
1984 	} else {
1985 		p_fs->hint_uentry.entry = -1;
1986 
1987 		clu.dir = p_dir->dir;
1988 		clu.size = p_dir->size;
1989 		clu.flags = p_dir->flags;
1990 
1991 		dentry = 0;
1992 	}
1993 
1994 	while (clu.dir != CLUSTER_32(~0)) {
1995 		if (p_fs->dev_ejected)
1996 			break;
1997 
1998 		if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
1999 			i = dentry % dentries_per_clu;
2000 		else
2001 			i = dentry & (dentries_per_clu - 1);
2002 
2003 		for (; i < dentries_per_clu; i++, dentry++) {
2004 			ep = get_entry_in_dir(sb, &clu, i, NULL);
2005 			if (!ep)
2006 				return -1;
2007 
2008 			type = p_fs->fs_func->get_entry_type(ep);
2009 
2010 			if (type == TYPE_UNUSED) {
2011 				num_empty++;
2012 				if (p_fs->hint_uentry.entry == -1) {
2013 					p_fs->hint_uentry.dir = p_dir->dir;
2014 					p_fs->hint_uentry.entry = dentry;
2015 
2016 					p_fs->hint_uentry.clu.dir = clu.dir;
2017 					p_fs->hint_uentry.clu.size = clu.size;
2018 					p_fs->hint_uentry.clu.flags = clu.flags;
2019 				}
2020 			} else if (type == TYPE_DELETED) {
2021 				num_empty++;
2022 			} else {
2023 				num_empty = 0;
2024 			}
2025 
2026 			if (num_empty >= num_entries) {
2027 				p_fs->hint_uentry.dir = CLUSTER_32(~0);
2028 				p_fs->hint_uentry.entry = -1;
2029 
2030 				if (p_fs->vol_type == EXFAT)
2031 					return dentry - (num_entries - 1);
2032 				else
2033 					return dentry;
2034 			}
2035 		}
2036 
2037 		if (p_dir->dir == CLUSTER_32(0))
2038 			break; /* FAT16 root_dir */
2039 
2040 		if (clu.flags == 0x03) {
2041 			if ((--clu.size) > 0)
2042 				clu.dir++;
2043 			else
2044 				clu.dir = CLUSTER_32(~0);
2045 		} else {
2046 			if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2047 				return -1;
2048 		}
2049 	}
2050 
2051 	return -1;
2052 }
2053 
find_empty_entry(struct inode * inode,struct chain_t * p_dir,s32 num_entries)2054 s32 find_empty_entry(struct inode *inode, struct chain_t *p_dir, s32 num_entries)
2055 {
2056 	s32 ret, dentry;
2057 	u32 last_clu;
2058 	sector_t sector;
2059 	u64 size = 0;
2060 	struct chain_t clu;
2061 	struct dentry_t *ep = NULL;
2062 	struct super_block *sb = inode->i_sb;
2063 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2064 	struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2065 
2066 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2067 		return search_deleted_or_unused_entry(sb, p_dir, num_entries);
2068 
2069 	while ((dentry = search_deleted_or_unused_entry(sb, p_dir, num_entries)) < 0) {
2070 		if (p_fs->dev_ejected)
2071 			break;
2072 
2073 		if (p_fs->vol_type == EXFAT) {
2074 			if (p_dir->dir != p_fs->root_dir)
2075 				size = i_size_read(inode);
2076 		}
2077 
2078 		last_clu = find_last_cluster(sb, p_dir);
2079 		clu.dir = last_clu + 1;
2080 		clu.size = 0;
2081 		clu.flags = p_dir->flags;
2082 
2083 		/* (1) allocate a cluster */
2084 		ret = p_fs->fs_func->alloc_cluster(sb, 1, &clu);
2085 		if (ret < 1)
2086 			return -1;
2087 
2088 		if (clear_cluster(sb, clu.dir) != FFS_SUCCESS)
2089 			return -1;
2090 
2091 		/* (2) append to the FAT chain */
2092 		if (clu.flags != p_dir->flags) {
2093 			exfat_chain_cont_cluster(sb, p_dir->dir, p_dir->size);
2094 			p_dir->flags = 0x01;
2095 			p_fs->hint_uentry.clu.flags = 0x01;
2096 		}
2097 		if (clu.flags == 0x01)
2098 			if (FAT_write(sb, last_clu, clu.dir) < 0)
2099 				return -1;
2100 
2101 		if (p_fs->hint_uentry.entry == -1) {
2102 			p_fs->hint_uentry.dir = p_dir->dir;
2103 			p_fs->hint_uentry.entry = p_dir->size << (p_fs->cluster_size_bits - DENTRY_SIZE_BITS);
2104 
2105 			p_fs->hint_uentry.clu.dir = clu.dir;
2106 			p_fs->hint_uentry.clu.size = 0;
2107 			p_fs->hint_uentry.clu.flags = clu.flags;
2108 		}
2109 		p_fs->hint_uentry.clu.size++;
2110 		p_dir->size++;
2111 
2112 		/* (3) update the directory entry */
2113 		if (p_fs->vol_type == EXFAT) {
2114 			if (p_dir->dir != p_fs->root_dir) {
2115 				size += p_fs->cluster_size;
2116 
2117 				ep = get_entry_in_dir(sb, &fid->dir,
2118 						      fid->entry + 1, &sector);
2119 				if (!ep)
2120 					return -1;
2121 				p_fs->fs_func->set_entry_size(ep, size);
2122 				p_fs->fs_func->set_entry_flag(ep, p_dir->flags);
2123 				buf_modify(sb, sector);
2124 
2125 				update_dir_checksum(sb, &(fid->dir),
2126 						    fid->entry);
2127 			}
2128 		}
2129 
2130 		i_size_write(inode, i_size_read(inode) + p_fs->cluster_size);
2131 		EXFAT_I(inode)->mmu_private += p_fs->cluster_size;
2132 		EXFAT_I(inode)->fid.size += p_fs->cluster_size;
2133 		EXFAT_I(inode)->fid.flags = p_dir->flags;
2134 		inode->i_blocks += 1 << (p_fs->cluster_size_bits - 9);
2135 	}
2136 
2137 	return dentry;
2138 }
2139 
2140 /* return values of fat_find_dir_entry()
2141  * >= 0 : return dir entiry position with the name in dir
2142  * -1 : (root dir, ".") it is the root dir itself
2143  * -2 : entry with the name does not exist
2144  */
fat_find_dir_entry(struct super_block * sb,struct chain_t * p_dir,struct uni_name_t * p_uniname,s32 num_entries,struct dos_name_t * p_dosname,u32 type)2145 s32 fat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
2146 		       struct uni_name_t *p_uniname, s32 num_entries,
2147 		       struct dos_name_t *p_dosname, u32 type)
2148 {
2149 	int i, dentry = 0, len;
2150 	s32 order = 0;
2151 	bool is_feasible_entry = true, has_ext_entry = false;
2152 	s32 dentries_per_clu;
2153 	u32 entry_type;
2154 	u16 entry_uniname[14], *uniname = NULL, unichar;
2155 	struct chain_t clu;
2156 	struct dentry_t *ep;
2157 	struct dos_dentry_t *dos_ep;
2158 	struct ext_dentry_t *ext_ep;
2159 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2160 
2161 	if (p_dir->dir == p_fs->root_dir) {
2162 		if ((!nls_uniname_cmp(sb, p_uniname->name,
2163 				      (u16 *)UNI_CUR_DIR_NAME)) ||
2164 			(!nls_uniname_cmp(sb, p_uniname->name,
2165 					  (u16 *)UNI_PAR_DIR_NAME)))
2166 			return -1; // special case, root directory itself
2167 	}
2168 
2169 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2170 		dentries_per_clu = p_fs->dentries_in_root;
2171 	else
2172 		dentries_per_clu = p_fs->dentries_per_clu;
2173 
2174 	clu.dir = p_dir->dir;
2175 	clu.flags = p_dir->flags;
2176 
2177 	while (clu.dir != CLUSTER_32(~0)) {
2178 		if (p_fs->dev_ejected)
2179 			break;
2180 
2181 		for (i = 0; i < dentries_per_clu; i++, dentry++) {
2182 			ep = get_entry_in_dir(sb, &clu, i, NULL);
2183 			if (!ep)
2184 				return -2;
2185 
2186 			entry_type = p_fs->fs_func->get_entry_type(ep);
2187 
2188 			if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
2189 				if ((type == TYPE_ALL) || (type == entry_type)) {
2190 					if (is_feasible_entry && has_ext_entry)
2191 						return dentry;
2192 
2193 					dos_ep = (struct dos_dentry_t *)ep;
2194 					if (!nls_dosname_cmp(sb, p_dosname->name, dos_ep->name))
2195 						return dentry;
2196 				}
2197 				is_feasible_entry = true;
2198 				has_ext_entry = false;
2199 			} else if (entry_type == TYPE_EXTEND) {
2200 				if (is_feasible_entry) {
2201 					ext_ep = (struct ext_dentry_t *)ep;
2202 					if (ext_ep->order > 0x40) {
2203 						order = (s32)(ext_ep->order - 0x40);
2204 						uniname = p_uniname->name + 13 * (order - 1);
2205 					} else {
2206 						order = (s32)ext_ep->order;
2207 						uniname -= 13;
2208 					}
2209 
2210 					len = extract_uni_name_from_ext_entry(ext_ep, entry_uniname, order);
2211 
2212 					unichar = *(uniname + len);
2213 					*(uniname + len) = 0x0;
2214 
2215 					if (nls_uniname_cmp(sb, uniname, entry_uniname))
2216 						is_feasible_entry = false;
2217 
2218 					*(uniname + len) = unichar;
2219 				}
2220 				has_ext_entry = true;
2221 			} else if (entry_type == TYPE_UNUSED) {
2222 				return -2;
2223 			}
2224 			is_feasible_entry = true;
2225 			has_ext_entry = false;
2226 		}
2227 
2228 		if (p_dir->dir == CLUSTER_32(0))
2229 			break; /* FAT16 root_dir */
2230 
2231 		if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2232 			return -2;
2233 	}
2234 
2235 	return -2;
2236 }
2237 
2238 /* return values of exfat_find_dir_entry()
2239  * >= 0 : return dir entiry position with the name in dir
2240  * -1 : (root dir, ".") it is the root dir itself
2241  * -2 : entry with the name does not exist
2242  */
exfat_find_dir_entry(struct super_block * sb,struct chain_t * p_dir,struct uni_name_t * p_uniname,s32 num_entries,struct dos_name_t * p_dosname,u32 type)2243 s32 exfat_find_dir_entry(struct super_block *sb, struct chain_t *p_dir,
2244 			 struct uni_name_t *p_uniname, s32 num_entries,
2245 			 struct dos_name_t *p_dosname, u32 type)
2246 {
2247 	int i = 0, dentry = 0, num_ext_entries = 0, len, step;
2248 	s32 order = 0;
2249 	bool is_feasible_entry = false;
2250 	s32 dentries_per_clu, num_empty = 0;
2251 	u32 entry_type;
2252 	u16 entry_uniname[16], *uniname = NULL, unichar;
2253 	struct chain_t clu;
2254 	struct dentry_t *ep;
2255 	struct file_dentry_t *file_ep;
2256 	struct strm_dentry_t *strm_ep;
2257 	struct name_dentry_t *name_ep;
2258 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2259 
2260 	if (p_dir->dir == p_fs->root_dir) {
2261 		if ((!nls_uniname_cmp(sb, p_uniname->name,
2262 				      (u16 *)UNI_CUR_DIR_NAME)) ||
2263 			(!nls_uniname_cmp(sb, p_uniname->name,
2264 					  (u16 *)UNI_PAR_DIR_NAME)))
2265 			return -1; // special case, root directory itself
2266 	}
2267 
2268 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2269 		dentries_per_clu = p_fs->dentries_in_root;
2270 	else
2271 		dentries_per_clu = p_fs->dentries_per_clu;
2272 
2273 	clu.dir = p_dir->dir;
2274 	clu.size = p_dir->size;
2275 	clu.flags = p_dir->flags;
2276 
2277 	p_fs->hint_uentry.dir = p_dir->dir;
2278 	p_fs->hint_uentry.entry = -1;
2279 
2280 	while (clu.dir != CLUSTER_32(~0)) {
2281 		if (p_fs->dev_ejected)
2282 			break;
2283 
2284 		while (i < dentries_per_clu) {
2285 			ep = get_entry_in_dir(sb, &clu, i, NULL);
2286 			if (!ep)
2287 				return -2;
2288 
2289 			entry_type = p_fs->fs_func->get_entry_type(ep);
2290 			step = 1;
2291 
2292 			if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
2293 				is_feasible_entry = false;
2294 
2295 				if (p_fs->hint_uentry.entry == -1) {
2296 					num_empty++;
2297 
2298 					if (num_empty == 1) {
2299 						p_fs->hint_uentry.clu.dir = clu.dir;
2300 						p_fs->hint_uentry.clu.size = clu.size;
2301 						p_fs->hint_uentry.clu.flags = clu.flags;
2302 					}
2303 					if ((num_empty >= num_entries) || (entry_type == TYPE_UNUSED))
2304 						p_fs->hint_uentry.entry = dentry - (num_empty - 1);
2305 				}
2306 
2307 				if (entry_type == TYPE_UNUSED)
2308 					return -2;
2309 			} else {
2310 				num_empty = 0;
2311 
2312 				if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
2313 					file_ep = (struct file_dentry_t *)ep;
2314 					if ((type == TYPE_ALL) || (type == entry_type)) {
2315 						num_ext_entries = file_ep->num_ext;
2316 						is_feasible_entry = true;
2317 					} else {
2318 						is_feasible_entry = false;
2319 						step = file_ep->num_ext + 1;
2320 					}
2321 				} else if (entry_type == TYPE_STREAM) {
2322 					if (is_feasible_entry) {
2323 						strm_ep = (struct strm_dentry_t *)ep;
2324 						if (p_uniname->name_hash == GET16_A(strm_ep->name_hash) &&
2325 						    p_uniname->name_len == strm_ep->name_len) {
2326 							order = 1;
2327 						} else {
2328 							is_feasible_entry = false;
2329 							step = num_ext_entries;
2330 						}
2331 					}
2332 				} else if (entry_type == TYPE_EXTEND) {
2333 					if (is_feasible_entry) {
2334 						name_ep = (struct name_dentry_t *)ep;
2335 
2336 						if ((++order) == 2)
2337 							uniname = p_uniname->name;
2338 						else
2339 							uniname += 15;
2340 
2341 						len = extract_uni_name_from_name_entry(name_ep,
2342 								entry_uniname, order);
2343 
2344 						unichar = *(uniname + len);
2345 						*(uniname + len) = 0x0;
2346 
2347 						if (nls_uniname_cmp(sb, uniname, entry_uniname)) {
2348 							is_feasible_entry = false;
2349 							step = num_ext_entries - order + 1;
2350 						} else if (order == num_ext_entries) {
2351 							p_fs->hint_uentry.dir = CLUSTER_32(~0);
2352 							p_fs->hint_uentry.entry = -1;
2353 							return dentry - (num_ext_entries);
2354 						}
2355 
2356 						*(uniname + len) = unichar;
2357 					}
2358 				} else {
2359 					is_feasible_entry = false;
2360 				}
2361 			}
2362 
2363 			i += step;
2364 			dentry += step;
2365 		}
2366 
2367 		i -= dentries_per_clu;
2368 
2369 		if (p_dir->dir == CLUSTER_32(0))
2370 			break; /* FAT16 root_dir */
2371 
2372 		if (clu.flags == 0x03) {
2373 			if ((--clu.size) > 0)
2374 				clu.dir++;
2375 			else
2376 				clu.dir = CLUSTER_32(~0);
2377 		} else {
2378 			if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2379 				return -2;
2380 		}
2381 	}
2382 
2383 	return -2;
2384 }
2385 
fat_count_ext_entries(struct super_block * sb,struct chain_t * p_dir,s32 entry,struct dentry_t * p_entry)2386 s32 fat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
2387 			  s32 entry, struct dentry_t *p_entry)
2388 {
2389 	s32 count = 0;
2390 	u8 chksum;
2391 	struct dos_dentry_t *dos_ep = (struct dos_dentry_t *)p_entry;
2392 	struct ext_dentry_t *ext_ep;
2393 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2394 
2395 	chksum = calc_checksum_1byte((void *)dos_ep->name, DOS_NAME_LENGTH, 0);
2396 
2397 	for (entry--; entry >= 0; entry--) {
2398 		ext_ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir,
2399 								 entry, NULL);
2400 		if (!ext_ep)
2401 			return -1;
2402 
2403 		if ((p_fs->fs_func->get_entry_type((struct dentry_t *)ext_ep) ==
2404 		     TYPE_EXTEND) && (ext_ep->checksum == chksum)) {
2405 			count++;
2406 			if (ext_ep->order > 0x40)
2407 				return count;
2408 		} else {
2409 			return count;
2410 		}
2411 	}
2412 
2413 	return count;
2414 }
2415 
exfat_count_ext_entries(struct super_block * sb,struct chain_t * p_dir,s32 entry,struct dentry_t * p_entry)2416 s32 exfat_count_ext_entries(struct super_block *sb, struct chain_t *p_dir,
2417 			    s32 entry, struct dentry_t *p_entry)
2418 {
2419 	int i, count = 0;
2420 	u32 type;
2421 	struct file_dentry_t *file_ep = (struct file_dentry_t *)p_entry;
2422 	struct dentry_t *ext_ep;
2423 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2424 
2425 	for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
2426 		ext_ep = get_entry_in_dir(sb, p_dir, entry, NULL);
2427 		if (!ext_ep)
2428 			return -1;
2429 
2430 		type = p_fs->fs_func->get_entry_type(ext_ep);
2431 		if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
2432 			count++;
2433 		else
2434 			return count;
2435 	}
2436 
2437 	return count;
2438 }
2439 
count_dos_name_entries(struct super_block * sb,struct chain_t * p_dir,u32 type)2440 s32 count_dos_name_entries(struct super_block *sb, struct chain_t *p_dir,
2441 			   u32 type)
2442 {
2443 	int i, count = 0;
2444 	s32 dentries_per_clu;
2445 	u32 entry_type;
2446 	struct chain_t clu;
2447 	struct dentry_t *ep;
2448 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2449 
2450 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2451 		dentries_per_clu = p_fs->dentries_in_root;
2452 	else
2453 		dentries_per_clu = p_fs->dentries_per_clu;
2454 
2455 	clu.dir = p_dir->dir;
2456 	clu.size = p_dir->size;
2457 	clu.flags = p_dir->flags;
2458 
2459 	while (clu.dir != CLUSTER_32(~0)) {
2460 		if (p_fs->dev_ejected)
2461 			break;
2462 
2463 		for (i = 0; i < dentries_per_clu; i++) {
2464 			ep = get_entry_in_dir(sb, &clu, i, NULL);
2465 			if (!ep)
2466 				return -1;
2467 
2468 			entry_type = p_fs->fs_func->get_entry_type(ep);
2469 
2470 			if (entry_type == TYPE_UNUSED)
2471 				return count;
2472 			if (!(type & TYPE_CRITICAL_PRI) &&
2473 			    !(type & TYPE_BENIGN_PRI))
2474 				continue;
2475 
2476 			if ((type == TYPE_ALL) || (type == entry_type))
2477 				count++;
2478 		}
2479 
2480 		if (p_dir->dir == CLUSTER_32(0))
2481 			break; /* FAT16 root_dir */
2482 
2483 		if (clu.flags == 0x03) {
2484 			if ((--clu.size) > 0)
2485 				clu.dir++;
2486 			else
2487 				clu.dir = CLUSTER_32(~0);
2488 		} else {
2489 			if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2490 				return -1;
2491 		}
2492 	}
2493 
2494 	return count;
2495 }
2496 
is_dir_empty(struct super_block * sb,struct chain_t * p_dir)2497 bool is_dir_empty(struct super_block *sb, struct chain_t *p_dir)
2498 {
2499 	int i, count = 0;
2500 	s32 dentries_per_clu;
2501 	u32 type;
2502 	struct chain_t clu;
2503 	struct dentry_t *ep;
2504 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2505 
2506 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2507 		dentries_per_clu = p_fs->dentries_in_root;
2508 	else
2509 		dentries_per_clu = p_fs->dentries_per_clu;
2510 
2511 	clu.dir = p_dir->dir;
2512 	clu.size = p_dir->size;
2513 	clu.flags = p_dir->flags;
2514 
2515 	while (clu.dir != CLUSTER_32(~0)) {
2516 		if (p_fs->dev_ejected)
2517 			break;
2518 
2519 		for (i = 0; i < dentries_per_clu; i++) {
2520 			ep = get_entry_in_dir(sb, &clu, i, NULL);
2521 			if (!ep)
2522 				break;
2523 
2524 			type = p_fs->fs_func->get_entry_type(ep);
2525 
2526 			if (type == TYPE_UNUSED)
2527 				return true;
2528 			if ((type != TYPE_FILE) && (type != TYPE_DIR))
2529 				continue;
2530 
2531 			if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2532 				return false;
2533 
2534 			if (p_fs->vol_type == EXFAT)
2535 				return false;
2536 			if ((p_dir->dir == p_fs->root_dir) || ((++count) > 2))
2537 				return false;
2538 		}
2539 
2540 		if (p_dir->dir == CLUSTER_32(0))
2541 			break; /* FAT16 root_dir */
2542 
2543 		if (clu.flags == 0x03) {
2544 			if ((--clu.size) > 0)
2545 				clu.dir++;
2546 			else
2547 				clu.dir = CLUSTER_32(~0);
2548 		}
2549 		if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2550 			break;
2551 	}
2552 
2553 	return true;
2554 }
2555 
2556 /*
2557  *  Name Conversion Functions
2558  */
2559 
2560 /* input  : dir, uni_name
2561  * output : num_of_entry, dos_name(format : aaaaaa~1.bbb)
2562  */
get_num_entries_and_dos_name(struct super_block * sb,struct chain_t * p_dir,struct uni_name_t * p_uniname,s32 * entries,struct dos_name_t * p_dosname)2563 s32 get_num_entries_and_dos_name(struct super_block *sb, struct chain_t *p_dir,
2564 				 struct uni_name_t *p_uniname, s32 *entries,
2565 				 struct dos_name_t *p_dosname)
2566 {
2567 	s32 ret, num_entries;
2568 	bool lossy = false;
2569 	char **r;
2570 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2571 
2572 	num_entries = p_fs->fs_func->calc_num_entries(p_uniname);
2573 	if (num_entries == 0)
2574 		return FFS_INVALIDPATH;
2575 
2576 	if (p_fs->vol_type != EXFAT) {
2577 		nls_uniname_to_dosname(sb, p_dosname, p_uniname, &lossy);
2578 
2579 		if (lossy) {
2580 			ret = fat_generate_dos_name(sb, p_dir, p_dosname);
2581 			if (ret)
2582 				return ret;
2583 		} else {
2584 			for (r = reserved_names; *r; r++) {
2585 				if (!strncmp((void *)p_dosname->name, *r, 8))
2586 					return FFS_INVALIDPATH;
2587 			}
2588 
2589 			if (p_dosname->name_case != 0xFF)
2590 				num_entries = 1;
2591 		}
2592 
2593 		if (num_entries > 1)
2594 			p_dosname->name_case = 0x0;
2595 	}
2596 
2597 	*entries = num_entries;
2598 
2599 	return FFS_SUCCESS;
2600 }
2601 
get_uni_name_from_dos_entry(struct super_block * sb,struct dos_dentry_t * ep,struct uni_name_t * p_uniname,u8 mode)2602 void get_uni_name_from_dos_entry(struct super_block *sb,
2603 				 struct dos_dentry_t *ep,
2604 				 struct uni_name_t *p_uniname, u8 mode)
2605 {
2606 	struct dos_name_t dos_name;
2607 
2608 	if (mode == 0x0)
2609 		dos_name.name_case = 0x0;
2610 	else
2611 		dos_name.name_case = ep->lcase;
2612 
2613 	memcpy(dos_name.name, ep->name, DOS_NAME_LENGTH);
2614 	nls_dosname_to_uniname(sb, p_uniname, &dos_name);
2615 }
2616 
fat_get_uni_name_from_ext_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,u16 * uniname)2617 void fat_get_uni_name_from_ext_entry(struct super_block *sb,
2618 				     struct chain_t *p_dir, s32 entry,
2619 				     u16 *uniname)
2620 {
2621 	int i;
2622 	struct ext_dentry_t *ep;
2623 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2624 
2625 	for (entry--, i = 1; entry >= 0; entry--, i++) {
2626 		ep = (struct ext_dentry_t *)get_entry_in_dir(sb, p_dir, entry,
2627 							     NULL);
2628 		if (!ep)
2629 			return;
2630 
2631 		if (p_fs->fs_func->get_entry_type((struct dentry_t *)ep) ==
2632 		    TYPE_EXTEND) {
2633 			extract_uni_name_from_ext_entry(ep, uniname, i);
2634 			if (ep->order > 0x40)
2635 				return;
2636 		} else {
2637 			return;
2638 		}
2639 
2640 		uniname += 13;
2641 	}
2642 }
2643 
exfat_get_uni_name_from_ext_entry(struct super_block * sb,struct chain_t * p_dir,s32 entry,u16 * uniname)2644 void exfat_get_uni_name_from_ext_entry(struct super_block *sb,
2645 				       struct chain_t *p_dir, s32 entry,
2646 				       u16 *uniname)
2647 {
2648 	int i;
2649 	struct dentry_t *ep;
2650 	struct entry_set_cache_t *es;
2651 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2652 
2653 	es = get_entry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
2654 	if (!es || es->num_entries < 3) {
2655 		if (es)
2656 			release_entry_set(es);
2657 		return;
2658 	}
2659 
2660 	ep += 2;
2661 
2662 	/*
2663 	 * First entry  : file entry
2664 	 * Second entry : stream-extension entry
2665 	 * Third entry  : first file-name entry
2666 	 * So, the index of first file-name dentry should start from 2.
2667 	 */
2668 	for (i = 2; i < es->num_entries; i++, ep++) {
2669 		if (p_fs->fs_func->get_entry_type(ep) == TYPE_EXTEND)
2670 			extract_uni_name_from_name_entry((struct name_dentry_t *)
2671 							 ep, uniname, i);
2672 		else
2673 			goto out;
2674 		uniname += 15;
2675 	}
2676 
2677 out:
2678 	release_entry_set(es);
2679 }
2680 
extract_uni_name_from_ext_entry(struct ext_dentry_t * ep,u16 * uniname,s32 order)2681 s32 extract_uni_name_from_ext_entry(struct ext_dentry_t *ep, u16 *uniname,
2682 				    s32 order)
2683 {
2684 	int i, len = 0;
2685 
2686 	for (i = 0; i < 10; i += 2) {
2687 		*uniname = GET16(ep->unicode_0_4 + i);
2688 		if (*uniname == 0x0)
2689 			return len;
2690 		uniname++;
2691 		len++;
2692 	}
2693 
2694 	if (order < 20) {
2695 		for (i = 0; i < 12; i += 2) {
2696 			*uniname = GET16_A(ep->unicode_5_10 + i);
2697 			if (*uniname == 0x0)
2698 				return len;
2699 			uniname++;
2700 			len++;
2701 		}
2702 	} else {
2703 		for (i = 0; i < 8; i += 2) {
2704 			*uniname = GET16_A(ep->unicode_5_10 + i);
2705 			if (*uniname == 0x0)
2706 				return len;
2707 			uniname++;
2708 			len++;
2709 		}
2710 		*uniname = 0x0; /* uniname[MAX_NAME_LENGTH-1] */
2711 		return len;
2712 	}
2713 
2714 	for (i = 0; i < 4; i += 2) {
2715 		*uniname = GET16_A(ep->unicode_11_12 + i);
2716 		if (*uniname == 0x0)
2717 			return len;
2718 		uniname++;
2719 		len++;
2720 	}
2721 
2722 	*uniname = 0x0;
2723 	return len;
2724 }
2725 
extract_uni_name_from_name_entry(struct name_dentry_t * ep,u16 * uniname,s32 order)2726 s32 extract_uni_name_from_name_entry(struct name_dentry_t *ep, u16 *uniname,
2727 				     s32 order)
2728 {
2729 	int i, len = 0;
2730 
2731 	for (i = 0; i < 30; i += 2) {
2732 		*uniname = GET16_A(ep->unicode_0_14 + i);
2733 		if (*uniname == 0x0)
2734 			return len;
2735 		uniname++;
2736 		len++;
2737 	}
2738 
2739 	*uniname = 0x0;
2740 	return len;
2741 }
2742 
fat_generate_dos_name(struct super_block * sb,struct chain_t * p_dir,struct dos_name_t * p_dosname)2743 s32 fat_generate_dos_name(struct super_block *sb, struct chain_t *p_dir,
2744 			  struct dos_name_t *p_dosname)
2745 {
2746 	int i, j, count = 0;
2747 	bool count_begin = false;
2748 	s32 dentries_per_clu;
2749 	u32 type;
2750 	u8 bmap[128/* 1 ~ 1023 */];
2751 	struct chain_t clu;
2752 	struct dos_dentry_t *ep;
2753 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2754 
2755 	memset(bmap, 0, sizeof(bmap));
2756 	exfat_bitmap_set(bmap, 0);
2757 
2758 	if (p_dir->dir == CLUSTER_32(0)) /* FAT16 root_dir */
2759 		dentries_per_clu = p_fs->dentries_in_root;
2760 	else
2761 		dentries_per_clu = p_fs->dentries_per_clu;
2762 
2763 	clu.dir = p_dir->dir;
2764 	clu.flags = p_dir->flags;
2765 
2766 	while (clu.dir != CLUSTER_32(~0)) {
2767 		if (p_fs->dev_ejected)
2768 			break;
2769 
2770 		for (i = 0; i < dentries_per_clu; i++) {
2771 			ep = (struct dos_dentry_t *)get_entry_in_dir(sb, &clu,
2772 								     i, NULL);
2773 			if (!ep)
2774 				return FFS_MEDIAERR;
2775 
2776 			type = p_fs->fs_func->get_entry_type((struct dentry_t *)
2777 							     ep);
2778 
2779 			if (type == TYPE_UNUSED)
2780 				break;
2781 			if ((type != TYPE_FILE) && (type != TYPE_DIR))
2782 				continue;
2783 
2784 			count = 0;
2785 			count_begin = false;
2786 
2787 			for (j = 0; j < 8; j++) {
2788 				if (ep->name[j] == ' ')
2789 					break;
2790 
2791 				if (ep->name[j] == '~') {
2792 					count_begin = true;
2793 				} else if (count_begin) {
2794 					if ((ep->name[j] >= '0') &&
2795 					    (ep->name[j] <= '9')) {
2796 						count = count * 10 +
2797 							(ep->name[j] - '0');
2798 					} else {
2799 						count = 0;
2800 						count_begin = false;
2801 					}
2802 				}
2803 			}
2804 
2805 			if ((count > 0) && (count < 1024))
2806 				exfat_bitmap_set(bmap, count);
2807 		}
2808 
2809 		if (p_dir->dir == CLUSTER_32(0))
2810 			break; /* FAT16 root_dir */
2811 
2812 		if (FAT_read(sb, clu.dir, &clu.dir) != 0)
2813 			return FFS_MEDIAERR;
2814 	}
2815 
2816 	count = 0;
2817 	for (i = 0; i < 128; i++) {
2818 		if (bmap[i] != 0xFF) {
2819 			for (j = 0; j < 8; j++) {
2820 				if (exfat_bitmap_test(&bmap[i], j) == 0) {
2821 					count = (i << 3) + j;
2822 					break;
2823 				}
2824 			}
2825 			if (count != 0)
2826 				break;
2827 		}
2828 	}
2829 
2830 	if ((count == 0) || (count >= 1024))
2831 		return FFS_FILEEXIST;
2832 	fat_attach_count_to_dos_name(p_dosname->name, count);
2833 
2834 	/* Now dos_name has DOS~????.EXT */
2835 	return FFS_SUCCESS;
2836 }
2837 
fat_attach_count_to_dos_name(u8 * dosname,s32 count)2838 void fat_attach_count_to_dos_name(u8 *dosname, s32 count)
2839 {
2840 	int i, j, length;
2841 	char str_count[6];
2842 
2843 	snprintf(str_count, sizeof(str_count), "~%d", count);
2844 	length = strlen(str_count);
2845 
2846 	i = 0;
2847 	j = 0;
2848 	while (j <= (8 - length)) {
2849 		i = j;
2850 		if (dosname[j] == ' ')
2851 			break;
2852 		if (dosname[j] & 0x80)
2853 			j += 2;
2854 		else
2855 			j++;
2856 	}
2857 
2858 	for (j = 0; j < length; i++, j++)
2859 		dosname[i] = (u8)str_count[j];
2860 
2861 	if (i == 7)
2862 		dosname[7] = ' ';
2863 }
2864 
fat_calc_num_entries(struct uni_name_t * p_uniname)2865 s32 fat_calc_num_entries(struct uni_name_t *p_uniname)
2866 {
2867 	s32 len;
2868 
2869 	len = p_uniname->name_len;
2870 	if (len == 0)
2871 		return 0;
2872 
2873 	/* 1 dos name entry + extended entries */
2874 	return (len - 1) / 13 + 2;
2875 }
2876 
exfat_calc_num_entries(struct uni_name_t * p_uniname)2877 s32 exfat_calc_num_entries(struct uni_name_t *p_uniname)
2878 {
2879 	s32 len;
2880 
2881 	len = p_uniname->name_len;
2882 	if (len == 0)
2883 		return 0;
2884 
2885 	/* 1 file entry + 1 stream entry + name entries */
2886 	return (len - 1) / 15 + 3;
2887 }
2888 
calc_checksum_1byte(void * data,s32 len,u8 chksum)2889 u8 calc_checksum_1byte(void *data, s32 len, u8 chksum)
2890 {
2891 	int i;
2892 	u8 *c = (u8 *)data;
2893 
2894 	for (i = 0; i < len; i++, c++)
2895 		chksum = (((chksum & 1) << 7) | ((chksum & 0xFE) >> 1)) + *c;
2896 
2897 	return chksum;
2898 }
2899 
calc_checksum_2byte(void * data,s32 len,u16 chksum,s32 type)2900 u16 calc_checksum_2byte(void *data, s32 len, u16 chksum, s32 type)
2901 {
2902 	int i;
2903 	u8 *c = (u8 *)data;
2904 
2905 	switch (type) {
2906 	case CS_DIR_ENTRY:
2907 		for (i = 0; i < len; i++, c++) {
2908 			if ((i == 2) || (i == 3))
2909 				continue;
2910 			chksum = (((chksum & 1) << 15) |
2911 				  ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2912 		}
2913 		break;
2914 	default
2915 			:
2916 		for (i = 0; i < len; i++, c++)
2917 			chksum = (((chksum & 1) << 15) |
2918 				  ((chksum & 0xFFFE) >> 1)) + (u16)*c;
2919 	}
2920 
2921 	return chksum;
2922 }
2923 
calc_checksum_4byte(void * data,s32 len,u32 chksum,s32 type)2924 u32 calc_checksum_4byte(void *data, s32 len, u32 chksum, s32 type)
2925 {
2926 	int i;
2927 	u8 *c = (u8 *)data;
2928 
2929 	switch (type) {
2930 	case CS_PBR_SECTOR:
2931 		for (i = 0; i < len; i++, c++) {
2932 			if ((i == 106) || (i == 107) || (i == 112))
2933 				continue;
2934 			chksum = (((chksum & 1) << 31) |
2935 				  ((chksum & 0xFFFFFFFE) >> 1)) + (u32)*c;
2936 		}
2937 		break;
2938 	default
2939 			:
2940 		for (i = 0; i < len; i++, c++)
2941 			chksum = (((chksum & 1) << 31) |
2942 				  ((chksum & 0xFFFFFFFE) >> 1)) + (u32)*c;
2943 	}
2944 
2945 	return chksum;
2946 }
2947 
2948 /*
2949  *  Name Resolution Functions
2950  */
2951 
2952 /* return values of resolve_path()
2953  * > 0 : return the length of the path
2954  * < 0 : return error
2955  */
resolve_path(struct inode * inode,char * path,struct chain_t * p_dir,struct uni_name_t * p_uniname)2956 s32 resolve_path(struct inode *inode, char *path, struct chain_t *p_dir,
2957 		 struct uni_name_t *p_uniname)
2958 {
2959 	bool lossy = false;
2960 	struct super_block *sb = inode->i_sb;
2961 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
2962 	struct file_id_t *fid = &(EXFAT_I(inode)->fid);
2963 
2964 	if (strscpy(name_buf, path, sizeof(name_buf)) < 0)
2965 		return FFS_INVALIDPATH;
2966 
2967 	nls_cstring_to_uniname(sb, p_uniname, name_buf, &lossy);
2968 	if (lossy)
2969 		return FFS_INVALIDPATH;
2970 
2971 	fid->size = i_size_read(inode);
2972 
2973 	p_dir->dir = fid->start_clu;
2974 	p_dir->size = (s32)(fid->size >> p_fs->cluster_size_bits);
2975 	p_dir->flags = fid->flags;
2976 
2977 	return FFS_SUCCESS;
2978 }
2979 
2980 /*
2981  *  File Operation Functions
2982  */
2983 static struct fs_func fat_fs_func = {
2984 	.alloc_cluster = fat_alloc_cluster,
2985 	.free_cluster = fat_free_cluster,
2986 	.count_used_clusters = fat_count_used_clusters,
2987 
2988 	.init_dir_entry = fat_init_dir_entry,
2989 	.init_ext_entry = fat_init_ext_entry,
2990 	.find_dir_entry = fat_find_dir_entry,
2991 	.delete_dir_entry = fat_delete_dir_entry,
2992 	.get_uni_name_from_ext_entry = fat_get_uni_name_from_ext_entry,
2993 	.count_ext_entries = fat_count_ext_entries,
2994 	.calc_num_entries = fat_calc_num_entries,
2995 
2996 	.get_entry_type = fat_get_entry_type,
2997 	.set_entry_type = fat_set_entry_type,
2998 	.get_entry_attr = fat_get_entry_attr,
2999 	.set_entry_attr = fat_set_entry_attr,
3000 	.get_entry_flag = fat_get_entry_flag,
3001 	.set_entry_flag = fat_set_entry_flag,
3002 	.get_entry_clu0 = fat_get_entry_clu0,
3003 	.set_entry_clu0 = fat_set_entry_clu0,
3004 	.get_entry_size = fat_get_entry_size,
3005 	.set_entry_size = fat_set_entry_size,
3006 	.get_entry_time = fat_get_entry_time,
3007 	.set_entry_time = fat_set_entry_time,
3008 };
3009 
fat16_mount(struct super_block * sb,struct pbr_sector_t * p_pbr)3010 s32 fat16_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3011 {
3012 	s32 num_reserved, num_root_sectors;
3013 	struct bpb16_t *p_bpb = (struct bpb16_t *)p_pbr->bpb;
3014 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3015 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3016 
3017 	if (p_bpb->num_fats == 0)
3018 		return FFS_FORMATERR;
3019 
3020 	num_root_sectors = GET16(p_bpb->num_root_entries) << DENTRY_SIZE_BITS;
3021 	num_root_sectors = ((num_root_sectors - 1) >>
3022 			    p_bd->sector_size_bits) + 1;
3023 
3024 	p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
3025 	p_fs->sectors_per_clu_bits = ilog2(p_bpb->sectors_per_clu);
3026 	p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3027 				  p_bd->sector_size_bits;
3028 	p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3029 
3030 	p_fs->num_FAT_sectors = GET16(p_bpb->num_fat_sectors);
3031 
3032 	p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
3033 	if (p_bpb->num_fats == 1)
3034 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3035 	else
3036 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3037 					  p_fs->num_FAT_sectors;
3038 
3039 	p_fs->root_start_sector = p_fs->FAT2_start_sector +
3040 				  p_fs->num_FAT_sectors;
3041 	p_fs->data_start_sector = p_fs->root_start_sector + num_root_sectors;
3042 
3043 	p_fs->num_sectors = GET16(p_bpb->num_sectors);
3044 	if (p_fs->num_sectors == 0)
3045 		p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
3046 
3047 	num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
3048 	p_fs->num_clusters = ((p_fs->num_sectors - num_reserved) >>
3049 			      p_fs->sectors_per_clu_bits) + 2;
3050 	/* because the cluster index starts with 2 */
3051 
3052 	if (p_fs->num_clusters < FAT12_THRESHOLD)
3053 		p_fs->vol_type = FAT12;
3054 	else
3055 		p_fs->vol_type = FAT16;
3056 	p_fs->vol_id = GET32(p_bpb->vol_serial);
3057 
3058 	p_fs->root_dir = 0;
3059 	p_fs->dentries_in_root = GET16(p_bpb->num_root_entries);
3060 	p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3061 				       DENTRY_SIZE_BITS);
3062 
3063 	p_fs->vol_flag = VOL_CLEAN;
3064 	p_fs->clu_srch_ptr = 2;
3065 	p_fs->used_clusters = UINT_MAX;
3066 
3067 	p_fs->fs_func = &fat_fs_func;
3068 
3069 	return FFS_SUCCESS;
3070 }
3071 
fat32_mount(struct super_block * sb,struct pbr_sector_t * p_pbr)3072 s32 fat32_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3073 {
3074 	s32 num_reserved;
3075 	struct bpb32_t *p_bpb = (struct bpb32_t *)p_pbr->bpb;
3076 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3077 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3078 
3079 	if (p_bpb->num_fats == 0)
3080 		return FFS_FORMATERR;
3081 
3082 	p_fs->sectors_per_clu = p_bpb->sectors_per_clu;
3083 	p_fs->sectors_per_clu_bits = ilog2(p_bpb->sectors_per_clu);
3084 	p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3085 				  p_bd->sector_size_bits;
3086 	p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3087 
3088 	p_fs->num_FAT_sectors = GET32(p_bpb->num_fat32_sectors);
3089 
3090 	p_fs->FAT1_start_sector = p_fs->PBR_sector + GET16(p_bpb->num_reserved);
3091 	if (p_bpb->num_fats == 1)
3092 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3093 	else
3094 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3095 					  p_fs->num_FAT_sectors;
3096 
3097 	p_fs->root_start_sector = p_fs->FAT2_start_sector +
3098 				  p_fs->num_FAT_sectors;
3099 	p_fs->data_start_sector = p_fs->root_start_sector;
3100 
3101 	p_fs->num_sectors = GET32(p_bpb->num_huge_sectors);
3102 	num_reserved = p_fs->data_start_sector - p_fs->PBR_sector;
3103 
3104 	p_fs->num_clusters = ((p_fs->num_sectors - num_reserved) >>
3105 			      p_fs->sectors_per_clu_bits) + 2;
3106 	/* because the cluster index starts with 2 */
3107 
3108 	p_fs->vol_type = FAT32;
3109 	p_fs->vol_id = GET32(p_bpb->vol_serial);
3110 
3111 	p_fs->root_dir = GET32(p_bpb->root_cluster);
3112 	p_fs->dentries_in_root = 0;
3113 	p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3114 				       DENTRY_SIZE_BITS);
3115 
3116 	p_fs->vol_flag = VOL_CLEAN;
3117 	p_fs->clu_srch_ptr = 2;
3118 	p_fs->used_clusters = UINT_MAX;
3119 
3120 	p_fs->fs_func = &fat_fs_func;
3121 
3122 	return FFS_SUCCESS;
3123 }
3124 
3125 static struct fs_func exfat_fs_func = {
3126 	.alloc_cluster = exfat_alloc_cluster,
3127 	.free_cluster = exfat_free_cluster,
3128 	.count_used_clusters = exfat_count_used_clusters,
3129 
3130 	.init_dir_entry = exfat_init_dir_entry,
3131 	.init_ext_entry = exfat_init_ext_entry,
3132 	.find_dir_entry = exfat_find_dir_entry,
3133 	.delete_dir_entry = exfat_delete_dir_entry,
3134 	.get_uni_name_from_ext_entry = exfat_get_uni_name_from_ext_entry,
3135 	.count_ext_entries = exfat_count_ext_entries,
3136 	.calc_num_entries = exfat_calc_num_entries,
3137 
3138 	.get_entry_type = exfat_get_entry_type,
3139 	.set_entry_type = exfat_set_entry_type,
3140 	.get_entry_attr = exfat_get_entry_attr,
3141 	.set_entry_attr = exfat_set_entry_attr,
3142 	.get_entry_flag = exfat_get_entry_flag,
3143 	.set_entry_flag = exfat_set_entry_flag,
3144 	.get_entry_clu0 = exfat_get_entry_clu0,
3145 	.set_entry_clu0 = exfat_set_entry_clu0,
3146 	.get_entry_size = exfat_get_entry_size,
3147 	.set_entry_size = exfat_set_entry_size,
3148 	.get_entry_time = exfat_get_entry_time,
3149 	.set_entry_time = exfat_set_entry_time,
3150 };
3151 
exfat_mount(struct super_block * sb,struct pbr_sector_t * p_pbr)3152 s32 exfat_mount(struct super_block *sb, struct pbr_sector_t *p_pbr)
3153 {
3154 	struct bpbex_t *p_bpb = (struct bpbex_t *)p_pbr->bpb;
3155 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3156 	struct bd_info_t *p_bd = &(EXFAT_SB(sb)->bd_info);
3157 
3158 	if (p_bpb->num_fats == 0)
3159 		return FFS_FORMATERR;
3160 
3161 	p_fs->sectors_per_clu = 1 << p_bpb->sectors_per_clu_bits;
3162 	p_fs->sectors_per_clu_bits = p_bpb->sectors_per_clu_bits;
3163 	p_fs->cluster_size_bits = p_fs->sectors_per_clu_bits +
3164 				  p_bd->sector_size_bits;
3165 	p_fs->cluster_size = 1 << p_fs->cluster_size_bits;
3166 
3167 	p_fs->num_FAT_sectors = GET32(p_bpb->fat_length);
3168 
3169 	p_fs->FAT1_start_sector = p_fs->PBR_sector + GET32(p_bpb->fat_offset);
3170 	if (p_bpb->num_fats == 1)
3171 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector;
3172 	else
3173 		p_fs->FAT2_start_sector = p_fs->FAT1_start_sector +
3174 					  p_fs->num_FAT_sectors;
3175 
3176 	p_fs->root_start_sector = p_fs->PBR_sector + GET32(p_bpb->clu_offset);
3177 	p_fs->data_start_sector = p_fs->root_start_sector;
3178 
3179 	p_fs->num_sectors = GET64(p_bpb->vol_length);
3180 	p_fs->num_clusters = GET32(p_bpb->clu_count) + 2;
3181 	/* because the cluster index starts with 2 */
3182 
3183 	p_fs->vol_type = EXFAT;
3184 	p_fs->vol_id = GET32(p_bpb->vol_serial);
3185 
3186 	p_fs->root_dir = GET32(p_bpb->root_cluster);
3187 	p_fs->dentries_in_root = 0;
3188 	p_fs->dentries_per_clu = 1 << (p_fs->cluster_size_bits -
3189 				       DENTRY_SIZE_BITS);
3190 
3191 	p_fs->vol_flag = (u32)GET16(p_bpb->vol_flags);
3192 	p_fs->clu_srch_ptr = 2;
3193 	p_fs->used_clusters = UINT_MAX;
3194 
3195 	p_fs->fs_func = &exfat_fs_func;
3196 
3197 	return FFS_SUCCESS;
3198 }
3199 
create_dir(struct inode * inode,struct chain_t * p_dir,struct uni_name_t * p_uniname,struct file_id_t * fid)3200 s32 create_dir(struct inode *inode, struct chain_t *p_dir,
3201 	       struct uni_name_t *p_uniname, struct file_id_t *fid)
3202 {
3203 	s32 ret, dentry, num_entries;
3204 	u64 size;
3205 	struct chain_t clu;
3206 	struct dos_name_t dos_name, dot_name;
3207 	struct super_block *sb = inode->i_sb;
3208 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3209 	struct fs_func *fs_func = p_fs->fs_func;
3210 
3211 	ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
3212 					   &dos_name);
3213 	if (ret)
3214 		return ret;
3215 
3216 	/* find_empty_entry must be called before alloc_cluster */
3217 	dentry = find_empty_entry(inode, p_dir, num_entries);
3218 	if (dentry < 0)
3219 		return FFS_FULL;
3220 
3221 	clu.dir = CLUSTER_32(~0);
3222 	clu.size = 0;
3223 	clu.flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3224 
3225 	/* (1) allocate a cluster */
3226 	ret = fs_func->alloc_cluster(sb, 1, &clu);
3227 	if (ret < 0)
3228 		return FFS_MEDIAERR;
3229 	else if (ret == 0)
3230 		return FFS_FULL;
3231 
3232 	ret = clear_cluster(sb, clu.dir);
3233 	if (ret != FFS_SUCCESS)
3234 		return ret;
3235 
3236 	if (p_fs->vol_type == EXFAT) {
3237 		size = p_fs->cluster_size;
3238 	} else {
3239 		size = 0;
3240 
3241 		/* initialize the . and .. entry
3242 		 * Information for . points to itself
3243 		 * Information for .. points to parent dir
3244 		 */
3245 
3246 		dot_name.name_case = 0x0;
3247 		memcpy(dot_name.name, DOS_CUR_DIR_NAME, DOS_NAME_LENGTH);
3248 
3249 		ret = fs_func->init_dir_entry(sb, &clu, 0, TYPE_DIR, clu.dir,
3250 					      0);
3251 		if (ret != FFS_SUCCESS)
3252 			return ret;
3253 
3254 		ret = fs_func->init_ext_entry(sb, &clu, 0, 1, NULL, &dot_name);
3255 		if (ret != FFS_SUCCESS)
3256 			return ret;
3257 
3258 		memcpy(dot_name.name, DOS_PAR_DIR_NAME, DOS_NAME_LENGTH);
3259 
3260 		if (p_dir->dir == p_fs->root_dir)
3261 			ret = fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR,
3262 						      CLUSTER_32(0), 0);
3263 		else
3264 			ret = fs_func->init_dir_entry(sb, &clu, 1, TYPE_DIR,
3265 						      p_dir->dir, 0);
3266 
3267 		if (ret != FFS_SUCCESS)
3268 			return ret;
3269 
3270 		ret = p_fs->fs_func->init_ext_entry(sb, &clu, 1, 1, NULL,
3271 						    &dot_name);
3272 		if (ret != FFS_SUCCESS)
3273 			return ret;
3274 	}
3275 
3276 	/* (2) update the directory entry */
3277 	/* make sub-dir entry in parent directory */
3278 	ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_DIR, clu.dir,
3279 				      size);
3280 	if (ret != FFS_SUCCESS)
3281 		return ret;
3282 
3283 	ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
3284 				      &dos_name);
3285 	if (ret != FFS_SUCCESS)
3286 		return ret;
3287 
3288 	fid->dir.dir = p_dir->dir;
3289 	fid->dir.size = p_dir->size;
3290 	fid->dir.flags = p_dir->flags;
3291 	fid->entry = dentry;
3292 
3293 	fid->attr = ATTR_SUBDIR;
3294 	fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3295 	fid->size = size;
3296 	fid->start_clu = clu.dir;
3297 
3298 	fid->type = TYPE_DIR;
3299 	fid->rwoffset = 0;
3300 	fid->hint_last_off = -1;
3301 
3302 	return FFS_SUCCESS;
3303 }
3304 
create_file(struct inode * inode,struct chain_t * p_dir,struct uni_name_t * p_uniname,u8 mode,struct file_id_t * fid)3305 s32 create_file(struct inode *inode, struct chain_t *p_dir,
3306 		struct uni_name_t *p_uniname, u8 mode, struct file_id_t *fid)
3307 {
3308 	s32 ret, dentry, num_entries;
3309 	struct dos_name_t dos_name;
3310 	struct super_block *sb = inode->i_sb;
3311 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3312 	struct fs_func *fs_func = p_fs->fs_func;
3313 
3314 	ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname, &num_entries,
3315 					   &dos_name);
3316 	if (ret)
3317 		return ret;
3318 
3319 	/* find_empty_entry must be called before alloc_cluster() */
3320 	dentry = find_empty_entry(inode, p_dir, num_entries);
3321 	if (dentry < 0)
3322 		return FFS_FULL;
3323 
3324 	/* (1) update the directory entry */
3325 	/* fill the dos name directory entry information of the created file.
3326 	 * the first cluster is not determined yet. (0)
3327 	 */
3328 	ret = fs_func->init_dir_entry(sb, p_dir, dentry, TYPE_FILE | mode,
3329 				      CLUSTER_32(0), 0);
3330 	if (ret != FFS_SUCCESS)
3331 		return ret;
3332 
3333 	ret = fs_func->init_ext_entry(sb, p_dir, dentry, num_entries, p_uniname,
3334 				      &dos_name);
3335 	if (ret != FFS_SUCCESS)
3336 		return ret;
3337 
3338 	fid->dir.dir = p_dir->dir;
3339 	fid->dir.size = p_dir->size;
3340 	fid->dir.flags = p_dir->flags;
3341 	fid->entry = dentry;
3342 
3343 	fid->attr = ATTR_ARCHIVE | mode;
3344 	fid->flags = (p_fs->vol_type == EXFAT) ? 0x03 : 0x01;
3345 	fid->size = 0;
3346 	fid->start_clu = CLUSTER_32(~0);
3347 
3348 	fid->type = TYPE_FILE;
3349 	fid->rwoffset = 0;
3350 	fid->hint_last_off = -1;
3351 
3352 	return FFS_SUCCESS;
3353 }
3354 
remove_file(struct inode * inode,struct chain_t * p_dir,s32 entry)3355 void remove_file(struct inode *inode, struct chain_t *p_dir, s32 entry)
3356 {
3357 	s32 num_entries;
3358 	sector_t sector;
3359 	struct dentry_t *ep;
3360 	struct super_block *sb = inode->i_sb;
3361 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3362 	struct fs_func *fs_func = p_fs->fs_func;
3363 
3364 	ep = get_entry_in_dir(sb, p_dir, entry, &sector);
3365 	if (!ep)
3366 		return;
3367 
3368 	buf_lock(sb, sector);
3369 
3370 	/* buf_lock() before call count_ext_entries() */
3371 	num_entries = fs_func->count_ext_entries(sb, p_dir, entry, ep);
3372 	if (num_entries < 0) {
3373 		buf_unlock(sb, sector);
3374 		return;
3375 	}
3376 	num_entries++;
3377 
3378 	buf_unlock(sb, sector);
3379 
3380 	/* (1) update the directory entry */
3381 	fs_func->delete_dir_entry(sb, p_dir, entry, 0, num_entries);
3382 }
3383 
exfat_rename_file(struct inode * inode,struct chain_t * p_dir,s32 oldentry,struct uni_name_t * p_uniname,struct file_id_t * fid)3384 s32 exfat_rename_file(struct inode *inode, struct chain_t *p_dir, s32 oldentry,
3385 		      struct uni_name_t *p_uniname, struct file_id_t *fid)
3386 {
3387 	s32 ret, newentry = -1, num_old_entries, num_new_entries;
3388 	sector_t sector_old, sector_new;
3389 	struct dos_name_t dos_name;
3390 	struct dentry_t *epold, *epnew;
3391 	struct super_block *sb = inode->i_sb;
3392 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3393 	struct fs_func *fs_func = p_fs->fs_func;
3394 
3395 	epold = get_entry_in_dir(sb, p_dir, oldentry, &sector_old);
3396 	if (!epold)
3397 		return FFS_MEDIAERR;
3398 
3399 	buf_lock(sb, sector_old);
3400 
3401 	/* buf_lock() before call count_ext_entries() */
3402 	num_old_entries = fs_func->count_ext_entries(sb, p_dir, oldentry,
3403 						     epold);
3404 	if (num_old_entries < 0) {
3405 		buf_unlock(sb, sector_old);
3406 		return FFS_MEDIAERR;
3407 	}
3408 	num_old_entries++;
3409 
3410 	ret = get_num_entries_and_dos_name(sb, p_dir, p_uniname,
3411 					   &num_new_entries, &dos_name);
3412 	if (ret) {
3413 		buf_unlock(sb, sector_old);
3414 		return ret;
3415 	}
3416 
3417 	if (num_old_entries < num_new_entries) {
3418 		newentry = find_empty_entry(inode, p_dir, num_new_entries);
3419 		if (newentry < 0) {
3420 			buf_unlock(sb, sector_old);
3421 			return FFS_FULL;
3422 		}
3423 
3424 		epnew = get_entry_in_dir(sb, p_dir, newentry, &sector_new);
3425 		if (!epnew) {
3426 			buf_unlock(sb, sector_old);
3427 			return FFS_MEDIAERR;
3428 		}
3429 
3430 		memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
3431 		if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
3432 			fs_func->set_entry_attr(epnew,
3433 						fs_func->get_entry_attr(epnew) |
3434 						ATTR_ARCHIVE);
3435 			fid->attr |= ATTR_ARCHIVE;
3436 		}
3437 		buf_modify(sb, sector_new);
3438 		buf_unlock(sb, sector_old);
3439 
3440 		if (p_fs->vol_type == EXFAT) {
3441 			epold = get_entry_in_dir(sb, p_dir, oldentry + 1,
3442 						 &sector_old);
3443 			buf_lock(sb, sector_old);
3444 			epnew = get_entry_in_dir(sb, p_dir, newentry + 1,
3445 						 &sector_new);
3446 
3447 			if (!epold || !epnew) {
3448 				buf_unlock(sb, sector_old);
3449 				return FFS_MEDIAERR;
3450 			}
3451 
3452 			memcpy((void *)epnew, (void *)epold, DENTRY_SIZE);
3453 			buf_modify(sb, sector_new);
3454 			buf_unlock(sb, sector_old);
3455 		}
3456 
3457 		ret = fs_func->init_ext_entry(sb, p_dir, newentry,
3458 					      num_new_entries, p_uniname,
3459 					      &dos_name);
3460 		if (ret != FFS_SUCCESS)
3461 			return ret;
3462 
3463 		fs_func->delete_dir_entry(sb, p_dir, oldentry, 0,
3464 					  num_old_entries);
3465 		fid->entry = newentry;
3466 	} else {
3467 		if (fs_func->get_entry_type(epold) == TYPE_FILE) {
3468 			fs_func->set_entry_attr(epold,
3469 						fs_func->get_entry_attr(epold) |
3470 						ATTR_ARCHIVE);
3471 			fid->attr |= ATTR_ARCHIVE;
3472 		}
3473 		buf_modify(sb, sector_old);
3474 		buf_unlock(sb, sector_old);
3475 
3476 		ret = fs_func->init_ext_entry(sb, p_dir, oldentry,
3477 					      num_new_entries, p_uniname,
3478 					      &dos_name);
3479 		if (ret != FFS_SUCCESS)
3480 			return ret;
3481 
3482 		fs_func->delete_dir_entry(sb, p_dir, oldentry, num_new_entries,
3483 					  num_old_entries);
3484 	}
3485 
3486 	return FFS_SUCCESS;
3487 }
3488 
move_file(struct inode * inode,struct chain_t * p_olddir,s32 oldentry,struct chain_t * p_newdir,struct uni_name_t * p_uniname,struct file_id_t * fid)3489 s32 move_file(struct inode *inode, struct chain_t *p_olddir, s32 oldentry,
3490 	      struct chain_t *p_newdir, struct uni_name_t *p_uniname,
3491 	      struct file_id_t *fid)
3492 {
3493 	s32 ret, newentry, num_new_entries, num_old_entries;
3494 	sector_t sector_mov, sector_new;
3495 	struct chain_t clu;
3496 	struct dos_name_t dos_name;
3497 	struct dentry_t *epmov, *epnew;
3498 	struct super_block *sb = inode->i_sb;
3499 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3500 	struct fs_func *fs_func = p_fs->fs_func;
3501 
3502 	epmov = get_entry_in_dir(sb, p_olddir, oldentry, &sector_mov);
3503 	if (!epmov)
3504 		return FFS_MEDIAERR;
3505 
3506 	/* check if the source and target directory is the same */
3507 	if (fs_func->get_entry_type(epmov) == TYPE_DIR &&
3508 	    fs_func->get_entry_clu0(epmov) == p_newdir->dir)
3509 		return FFS_INVALIDPATH;
3510 
3511 	buf_lock(sb, sector_mov);
3512 
3513 	/* buf_lock() before call count_ext_entries() */
3514 	num_old_entries = fs_func->count_ext_entries(sb, p_olddir, oldentry,
3515 						     epmov);
3516 	if (num_old_entries < 0) {
3517 		buf_unlock(sb, sector_mov);
3518 		return FFS_MEDIAERR;
3519 	}
3520 	num_old_entries++;
3521 
3522 	ret = get_num_entries_and_dos_name(sb, p_newdir, p_uniname,
3523 					   &num_new_entries, &dos_name);
3524 	if (ret) {
3525 		buf_unlock(sb, sector_mov);
3526 		return ret;
3527 	}
3528 
3529 	newentry = find_empty_entry(inode, p_newdir, num_new_entries);
3530 	if (newentry < 0) {
3531 		buf_unlock(sb, sector_mov);
3532 		return FFS_FULL;
3533 	}
3534 
3535 	epnew = get_entry_in_dir(sb, p_newdir, newentry, &sector_new);
3536 	if (!epnew) {
3537 		buf_unlock(sb, sector_mov);
3538 		return FFS_MEDIAERR;
3539 	}
3540 
3541 	memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
3542 	if (fs_func->get_entry_type(epnew) == TYPE_FILE) {
3543 		fs_func->set_entry_attr(epnew, fs_func->get_entry_attr(epnew) |
3544 					ATTR_ARCHIVE);
3545 		fid->attr |= ATTR_ARCHIVE;
3546 	}
3547 	buf_modify(sb, sector_new);
3548 	buf_unlock(sb, sector_mov);
3549 
3550 	if (p_fs->vol_type == EXFAT) {
3551 		epmov = get_entry_in_dir(sb, p_olddir, oldentry + 1,
3552 					 &sector_mov);
3553 		buf_lock(sb, sector_mov);
3554 		epnew = get_entry_in_dir(sb, p_newdir, newentry + 1,
3555 					 &sector_new);
3556 		if (!epmov || !epnew) {
3557 			buf_unlock(sb, sector_mov);
3558 			return FFS_MEDIAERR;
3559 		}
3560 
3561 		memcpy((void *)epnew, (void *)epmov, DENTRY_SIZE);
3562 		buf_modify(sb, sector_new);
3563 		buf_unlock(sb, sector_mov);
3564 	} else if (fs_func->get_entry_type(epnew) == TYPE_DIR) {
3565 		/* change ".." pointer to new parent dir */
3566 		clu.dir = fs_func->get_entry_clu0(epnew);
3567 		clu.flags = 0x01;
3568 
3569 		epnew = get_entry_in_dir(sb, &clu, 1, &sector_new);
3570 		if (!epnew)
3571 			return FFS_MEDIAERR;
3572 
3573 		if (p_newdir->dir == p_fs->root_dir)
3574 			fs_func->set_entry_clu0(epnew, CLUSTER_32(0));
3575 		else
3576 			fs_func->set_entry_clu0(epnew, p_newdir->dir);
3577 		buf_modify(sb, sector_new);
3578 	}
3579 
3580 	ret = fs_func->init_ext_entry(sb, p_newdir, newentry, num_new_entries,
3581 				      p_uniname, &dos_name);
3582 	if (ret != FFS_SUCCESS)
3583 		return ret;
3584 
3585 	fs_func->delete_dir_entry(sb, p_olddir, oldentry, 0, num_old_entries);
3586 
3587 	fid->dir.dir = p_newdir->dir;
3588 	fid->dir.size = p_newdir->size;
3589 	fid->dir.flags = p_newdir->flags;
3590 
3591 	fid->entry = newentry;
3592 
3593 	return FFS_SUCCESS;
3594 }
3595 
3596 /*
3597  *  Sector Read/Write Functions
3598  */
3599 
sector_read(struct super_block * sb,sector_t sec,struct buffer_head ** bh,bool read)3600 int sector_read(struct super_block *sb, sector_t sec, struct buffer_head **bh,
3601 		bool read)
3602 {
3603 	s32 ret = FFS_MEDIAERR;
3604 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3605 
3606 	if ((sec >= (p_fs->PBR_sector + p_fs->num_sectors)) &&
3607 	    (p_fs->num_sectors > 0)) {
3608 		pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
3609 		       __func__, (unsigned long long)sec);
3610 		fs_error(sb);
3611 		return ret;
3612 	}
3613 
3614 	if (!p_fs->dev_ejected) {
3615 		ret = bdev_read(sb, sec, bh, 1, read);
3616 		if (ret != FFS_SUCCESS)
3617 			p_fs->dev_ejected = 1;
3618 	}
3619 
3620 	return ret;
3621 }
3622 
sector_write(struct super_block * sb,sector_t sec,struct buffer_head * bh,bool sync)3623 int sector_write(struct super_block *sb, sector_t sec, struct buffer_head *bh,
3624 		 bool sync)
3625 {
3626 	s32 ret = FFS_MEDIAERR;
3627 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3628 
3629 	if (sec >= (p_fs->PBR_sector + p_fs->num_sectors) &&
3630 	    (p_fs->num_sectors > 0)) {
3631 		pr_err("[EXFAT] %s: out of range error! (sec = %llu)\n",
3632 		       __func__, (unsigned long long)sec);
3633 		fs_error(sb);
3634 		return ret;
3635 	}
3636 
3637 	if (!bh) {
3638 		pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
3639 		fs_error(sb);
3640 		return ret;
3641 	}
3642 
3643 	if (!p_fs->dev_ejected) {
3644 		ret = bdev_write(sb, sec, bh, 1, sync);
3645 		if (ret != FFS_SUCCESS)
3646 			p_fs->dev_ejected = 1;
3647 	}
3648 
3649 	return ret;
3650 }
3651 
multi_sector_read(struct super_block * sb,sector_t sec,struct buffer_head ** bh,s32 num_secs,bool read)3652 int multi_sector_read(struct super_block *sb, sector_t sec,
3653 		      struct buffer_head **bh, s32 num_secs, bool read)
3654 {
3655 	s32 ret = FFS_MEDIAERR;
3656 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3657 
3658 	if (((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors)) &&
3659 	    (p_fs->num_sectors > 0)) {
3660 		pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
3661 		       __func__, (unsigned long long)sec, num_secs);
3662 		fs_error(sb);
3663 		return ret;
3664 	}
3665 
3666 	if (!p_fs->dev_ejected) {
3667 		ret = bdev_read(sb, sec, bh, num_secs, read);
3668 		if (ret != FFS_SUCCESS)
3669 			p_fs->dev_ejected = 1;
3670 	}
3671 
3672 	return ret;
3673 }
3674 
multi_sector_write(struct super_block * sb,sector_t sec,struct buffer_head * bh,s32 num_secs,bool sync)3675 int multi_sector_write(struct super_block *sb, sector_t sec,
3676 		       struct buffer_head *bh, s32 num_secs, bool sync)
3677 {
3678 	s32 ret = FFS_MEDIAERR;
3679 	struct fs_info_t *p_fs = &(EXFAT_SB(sb)->fs_info);
3680 
3681 	if ((sec + num_secs) > (p_fs->PBR_sector + p_fs->num_sectors) &&
3682 	    (p_fs->num_sectors > 0)) {
3683 		pr_err("[EXFAT] %s: out of range error! (sec = %llu, num_secs = %d)\n",
3684 		       __func__, (unsigned long long)sec, num_secs);
3685 		fs_error(sb);
3686 		return ret;
3687 	}
3688 	if (!bh) {
3689 		pr_err("[EXFAT] %s: bh is NULL!\n", __func__);
3690 		fs_error(sb);
3691 		return ret;
3692 	}
3693 
3694 	if (!p_fs->dev_ejected) {
3695 		ret = bdev_write(sb, sec, bh, num_secs, sync);
3696 		if (ret != FFS_SUCCESS)
3697 			p_fs->dev_ejected = 1;
3698 	}
3699 
3700 	return ret;
3701 }
3702