fs: sdfat: Update to version 2.4.5
[GitHub/LineageOS/android_kernel_samsung_universal7580.git] / fs / sdfat / core_exfat.c
1 /*
2 * Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /************************************************************************/
19 /* */
20 /* PROJECT : exFAT & FAT12/16/32 File System */
21 /* FILE : core_exfat.c */
22 /* PURPOSE : exFAT-fs core code for sdFAT */
23 /* */
24 /*----------------------------------------------------------------------*/
25 /* NOTES */
26 /* */
27 /* */
28 /************************************************************************/
29
30 #include <linux/version.h>
31 #include <linux/blkdev.h>
32 #include <linux/workqueue.h>
33 #include <linux/kernel.h>
34 #include <linux/log2.h>
35
36 #include "sdfat.h"
37 #include "core.h"
38 #include <asm/byteorder.h>
39 #include <asm/unaligned.h>
40
41 /*----------------------------------------------------------------------*/
42 /* Constant & Macro Definitions */
43 /*----------------------------------------------------------------------*/
44
45 /*----------------------------------------------------------------------*/
46 /* Global Variable Definitions */
47 /*----------------------------------------------------------------------*/
48
49 /*----------------------------------------------------------------------*/
50 /* Local Variable Definitions */
51 /*----------------------------------------------------------------------*/
52 static u8 free_bit[] = {
53 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/* 0 ~ 19*/
54 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3,/* 20 ~ 39*/
55 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/* 40 ~ 59*/
56 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/* 60 ~ 79*/
57 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2,/* 80 ~ 99*/
58 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3,/*100 ~ 119*/
59 0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*120 ~ 139*/
60 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5,/*140 ~ 159*/
61 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2,/*160 ~ 179*/
62 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3,/*180 ~ 199*/
63 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2,/*200 ~ 219*/
64 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4,/*220 ~ 239*/
65 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 /*240 ~ 254*/
66 };
67
68 static u8 used_bit[] = {
69 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3,/* 0 ~ 19*/
70 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2, 3, 3, 4,/* 20 ~ 39*/
71 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5,/* 40 ~ 59*/
72 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,/* 60 ~ 79*/
73 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4,/* 80 ~ 99*/
74 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6,/*100 ~ 119*/
75 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4,/*120 ~ 139*/
76 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,/*140 ~ 159*/
77 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5,/*160 ~ 179*/
78 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5,/*180 ~ 199*/
79 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6,/*200 ~ 219*/
80 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,/*220 ~ 239*/
81 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8 /*240 ~ 255*/
82 };
83
84
85 /*======================================================================*/
86 /* Local Function Definitions */
87 /*======================================================================*/
88 /*
89 * Directory Entry Management Functions
90 */
91 static u32 exfat_get_entry_type(DENTRY_T *p_entry)
92 {
93 FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
94
95 if (ep->type == EXFAT_UNUSED)
96 return TYPE_UNUSED;
97 if (ep->type < 0x80)
98 return TYPE_DELETED;
99 if (ep->type == 0x80)
100 return TYPE_INVALID;
101 if (ep->type < 0xA0) {
102 if (ep->type == 0x81)
103 return TYPE_BITMAP;
104 if (ep->type == 0x82)
105 return TYPE_UPCASE;
106 if (ep->type == 0x83)
107 return TYPE_VOLUME;
108 if (ep->type == 0x85) {
109 if (le16_to_cpu(ep->attr) & ATTR_SUBDIR)
110 return TYPE_DIR;
111 return TYPE_FILE;
112 }
113 return TYPE_CRITICAL_PRI;
114 }
115 if (ep->type < 0xC0) {
116 if (ep->type == 0xA0)
117 return TYPE_GUID;
118 if (ep->type == 0xA1)
119 return TYPE_PADDING;
120 if (ep->type == 0xA2)
121 return TYPE_ACLTAB;
122 return TYPE_BENIGN_PRI;
123 }
124 if (ep->type < 0xE0) {
125 if (ep->type == 0xC0)
126 return TYPE_STREAM;
127 if (ep->type == 0xC1)
128 return TYPE_EXTEND;
129 if (ep->type == 0xC2)
130 return TYPE_ACL;
131 return TYPE_CRITICAL_SEC;
132 }
133 return TYPE_BENIGN_SEC;
134 } /* end of exfat_get_entry_type */
135
136 static void exfat_set_entry_type(DENTRY_T *p_entry, u32 type)
137 {
138 FILE_DENTRY_T *ep = (FILE_DENTRY_T *) p_entry;
139
140 if (type == TYPE_UNUSED) {
141 ep->type = 0x0;
142 } else if (type == TYPE_DELETED) {
143 ep->type &= ~0x80;
144 } else if (type == TYPE_STREAM) {
145 ep->type = 0xC0;
146 } else if (type == TYPE_EXTEND) {
147 ep->type = 0xC1;
148 } else if (type == TYPE_BITMAP) {
149 ep->type = 0x81;
150 } else if (type == TYPE_UPCASE) {
151 ep->type = 0x82;
152 } else if (type == TYPE_VOLUME) {
153 ep->type = 0x83;
154 } else if (type == TYPE_DIR) {
155 ep->type = 0x85;
156 ep->attr = cpu_to_le16(ATTR_SUBDIR);
157 } else if (type == TYPE_FILE) {
158 ep->type = 0x85;
159 ep->attr = cpu_to_le16(ATTR_ARCHIVE);
160 } else if (type == TYPE_SYMLINK) {
161 ep->type = 0x85;
162 ep->attr = cpu_to_le16(ATTR_ARCHIVE | ATTR_SYMLINK);
163 }
164 } /* end of exfat_set_entry_type */
165
166 static u32 exfat_get_entry_attr(DENTRY_T *p_entry)
167 {
168 FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
169
170 return (u32)le16_to_cpu(ep->attr);
171 } /* end of exfat_get_entry_attr */
172
173 static void exfat_set_entry_attr(DENTRY_T *p_entry, u32 attr)
174 {
175 FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
176
177 ep->attr = cpu_to_le16((u16) attr);
178 } /* end of exfat_set_entry_attr */
179
180 static u8 exfat_get_entry_flag(DENTRY_T *p_entry)
181 {
182 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
183
184 return ep->flags;
185 } /* end of exfat_get_entry_flag */
186
187 static void exfat_set_entry_flag(DENTRY_T *p_entry, u8 flags)
188 {
189 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
190
191 ep->flags = flags;
192 } /* end of exfat_set_entry_flag */
193
194 static u32 exfat_get_entry_clu0(DENTRY_T *p_entry)
195 {
196 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
197
198 return (u32)le32_to_cpu(ep->start_clu);
199 } /* end of exfat_get_entry_clu0 */
200
201 static void exfat_set_entry_clu0(DENTRY_T *p_entry, u32 start_clu)
202 {
203 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
204
205 ep->start_clu = cpu_to_le32(start_clu);
206 } /* end of exfat_set_entry_clu0 */
207
208 static u64 exfat_get_entry_size(DENTRY_T *p_entry)
209 {
210 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
211
212 return le64_to_cpu(ep->valid_size);
213 } /* end of exfat_get_entry_size */
214
215 static void exfat_set_entry_size(DENTRY_T *p_entry, u64 size)
216 {
217 STRM_DENTRY_T *ep = (STRM_DENTRY_T *)p_entry;
218
219 ep->valid_size = cpu_to_le64(size);
220 ep->size = cpu_to_le64(size);
221 } /* end of exfat_set_entry_size */
222
223
224 #define TENS_MS_PER_SEC (100)
225 #define SEC_TO_TENS_MS(sec) (((sec) & 0x01) ? TENS_MS_PER_SEC : 0)
226 #define TENS_MS_TO_SEC(tens_ms) (((tens_ms) / TENS_MS_PER_SEC) ? 1 : 0)
227
228 static void exfat_get_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
229 {
230 u16 t = 0x00, d = 0x21, tz = 0x00, s = 0x00;
231 FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
232
233 switch (mode) {
234 case TM_CREATE:
235 t = le16_to_cpu(ep->create_time);
236 d = le16_to_cpu(ep->create_date);
237 s = TENS_MS_TO_SEC(ep->create_time_ms);
238 tz = ep->create_tz;
239 break;
240 case TM_MODIFY:
241 t = le16_to_cpu(ep->modify_time);
242 d = le16_to_cpu(ep->modify_date);
243 s = TENS_MS_TO_SEC(ep->modify_time_ms);
244 tz = ep->modify_tz;
245 break;
246 case TM_ACCESS:
247 t = le16_to_cpu(ep->access_time);
248 d = le16_to_cpu(ep->access_date);
249 tz = ep->access_tz;
250 break;
251 }
252
253 tp->tz.value = tz;
254 tp->sec = ((t & 0x001F) << 1) + s;
255 tp->min = (t >> 5) & 0x003F;
256 tp->hour = (t >> 11);
257 tp->day = (d & 0x001F);
258 tp->mon = (d >> 5) & 0x000F;
259 tp->year = (d >> 9);
260 } /* end of exfat_get_entry_time */
261
262 static void exfat_set_entry_time(DENTRY_T *p_entry, TIMESTAMP_T *tp, u8 mode)
263 {
264 u16 t, d;
265 FILE_DENTRY_T *ep = (FILE_DENTRY_T *)p_entry;
266
267 t = (tp->hour << 11) | (tp->min << 5) | (tp->sec >> 1);
268 d = (tp->year << 9) | (tp->mon << 5) | tp->day;
269
270 switch (mode) {
271 case TM_CREATE:
272 ep->create_time = cpu_to_le16(t);
273 ep->create_time_ms = SEC_TO_TENS_MS(tp->sec);
274 ep->create_date = cpu_to_le16(d);
275 ep->create_tz = tp->tz.value;
276 break;
277 case TM_MODIFY:
278 ep->modify_time = cpu_to_le16(t);
279 ep->modify_date = cpu_to_le16(d);
280 ep->modify_time_ms = (tp->sec & 0x1) ? TENS_MS_PER_SEC : 0;
281 ep->modify_tz = tp->tz.value;
282 break;
283 case TM_ACCESS:
284 ep->access_time = cpu_to_le16(t);
285 ep->access_date = cpu_to_le16(d);
286 ep->access_tz = tp->tz.value;
287 break;
288 }
289 } /* end of exfat_set_entry_time */
290
291
292 static void __init_file_entry(struct super_block *sb, FILE_DENTRY_T *ep, u32 type)
293 {
294 TIMESTAMP_T tm, *tp;
295
296 exfat_set_entry_type((DENTRY_T *) ep, type);
297
298 tp = tm_now_sb(sb, &tm);
299 exfat_set_entry_time((DENTRY_T *) ep, tp, TM_CREATE);
300 exfat_set_entry_time((DENTRY_T *) ep, tp, TM_MODIFY);
301 exfat_set_entry_time((DENTRY_T *) ep, tp, TM_ACCESS);
302 } /* end of __init_file_entry */
303
304 static void __init_strm_entry(STRM_DENTRY_T *ep, u8 flags, u32 start_clu, u64 size)
305 {
306 exfat_set_entry_type((DENTRY_T *) ep, TYPE_STREAM);
307 ep->flags = flags;
308 ep->start_clu = cpu_to_le32(start_clu);
309 ep->valid_size = cpu_to_le64(size);
310 ep->size = cpu_to_le64(size);
311 } /* end of __init_strm_entry */
312
313 static void __init_name_entry(NAME_DENTRY_T *ep, u16 *uniname)
314 {
315 s32 i;
316
317 exfat_set_entry_type((DENTRY_T *) ep, TYPE_EXTEND);
318 ep->flags = 0x0;
319
320 for (i = 0; i < 15; i++) {
321 ep->unicode_0_14[i] = cpu_to_le16(*uniname);
322 if (*uniname == 0x0)
323 break;
324 uniname++;
325 }
326 } /* end of __init_name_entry */
327
328 static s32 exfat_init_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u32 type, u32 start_clu, u64 size)
329 {
330 u64 sector;
331 u8 flags;
332 FILE_DENTRY_T *file_ep;
333 STRM_DENTRY_T *strm_ep;
334
335 flags = (type == TYPE_FILE) ? 0x01 : 0x03;
336
337 /* we cannot use get_dentry_set_in_dir here because file ep is not initialized yet */
338 file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
339 if (!file_ep)
340 return -EIO;
341
342 strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, &sector);
343 if (!strm_ep)
344 return -EIO;
345
346 __init_file_entry(sb, file_ep, type);
347 if (dcache_modify(sb, sector))
348 return -EIO;
349
350 __init_strm_entry(strm_ep, flags, start_clu, size);
351 if (dcache_modify(sb, sector))
352 return -EIO;
353
354 return 0;
355 } /* end of exfat_init_dir_entry */
356
357 s32 update_dir_chksum(struct super_block *sb, CHAIN_T *p_dir, s32 entry)
358 {
359 s32 ret = -EIO;
360 s32 i, num_entries;
361 u64 sector;
362 u16 chksum;
363 FILE_DENTRY_T *file_ep;
364 DENTRY_T *ep;
365
366 file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
367 if (!file_ep)
368 return -EIO;
369
370 dcache_lock(sb, sector);
371
372 num_entries = (s32) file_ep->num_ext + 1;
373 chksum = calc_chksum_2byte((void *) file_ep, DENTRY_SIZE, 0, CS_DIR_ENTRY);
374
375 for (i = 1; i < num_entries; i++) {
376 ep = get_dentry_in_dir(sb, p_dir, entry+i, NULL);
377 if (!ep)
378 goto out_unlock;
379
380 chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, CS_DEFAULT);
381 }
382
383 file_ep->checksum = cpu_to_le16(chksum);
384 ret = dcache_modify(sb, sector);
385 out_unlock:
386 dcache_unlock(sb, sector);
387 return ret;
388
389 } /* end of update_dir_chksum */
390
391
392 static s32 exfat_init_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 num_entries,
393 UNI_NAME_T *p_uniname, DOS_NAME_T *p_dosname)
394 {
395 s32 i;
396 u64 sector;
397 u16 *uniname = p_uniname->name;
398 FILE_DENTRY_T *file_ep;
399 STRM_DENTRY_T *strm_ep;
400 NAME_DENTRY_T *name_ep;
401
402 file_ep = (FILE_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry, &sector);
403 if (!file_ep)
404 return -EIO;
405
406 file_ep->num_ext = (u8)(num_entries - 1);
407 dcache_modify(sb, sector);
408
409 strm_ep = (STRM_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+1, &sector);
410 if (!strm_ep)
411 return -EIO;
412
413 strm_ep->name_len = p_uniname->name_len;
414 strm_ep->name_hash = cpu_to_le16(p_uniname->name_hash);
415 dcache_modify(sb, sector);
416
417 for (i = 2; i < num_entries; i++) {
418 name_ep = (NAME_DENTRY_T *)get_dentry_in_dir(sb, p_dir, entry+i, &sector);
419 if (!name_ep)
420 return -EIO;
421
422 __init_name_entry(name_ep, uniname);
423 dcache_modify(sb, sector);
424 uniname += 15;
425 }
426
427 update_dir_chksum(sb, p_dir, entry);
428
429 return 0;
430 } /* end of exfat_init_ext_entry */
431
432
433 static s32 exfat_delete_dir_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, s32 order, s32 num_entries)
434 {
435 s32 i;
436 u64 sector;
437 DENTRY_T *ep;
438
439 for (i = order; i < num_entries; i++) {
440 ep = get_dentry_in_dir(sb, p_dir, entry+i, &sector);
441 if (!ep)
442 return -EIO;
443
444 exfat_set_entry_type(ep, TYPE_DELETED);
445 if (dcache_modify(sb, sector))
446 return -EIO;
447 }
448
449 return 0;
450 }
451
452 static s32 __write_partial_entries_in_entry_set(struct super_block *sb,
453 ENTRY_SET_CACHE_T *es, u64 sec, u32 off, u32 count)
454 {
455 s32 num_entries;
456 u32 buf_off = (off - es->offset);
457 u32 remaining_byte_in_sector, copy_entries;
458 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
459 u32 clu;
460 u8 *buf, *esbuf = (u8 *)&(es->__buf);
461
462 TMSG("%s entered\n", __func__);
463 MMSG("%s: es %p sec %llu off %u cnt %d\n", __func__, es, sec, off, count);
464 num_entries = count;
465
466 while (num_entries) {
467 /* write per sector base */
468 remaining_byte_in_sector = (1 << sb->s_blocksize_bits) - off;
469 copy_entries = min((s32)(remaining_byte_in_sector >> DENTRY_SIZE_BITS), num_entries);
470 buf = dcache_getblk(sb, sec);
471 if (!buf)
472 goto err_out;
473 MMSG("es->buf %p buf_off %u\n", esbuf, buf_off);
474 MMSG("copying %d entries from %p to sector %llu\n", copy_entries, (esbuf + buf_off), sec);
475 memcpy(buf + off, esbuf + buf_off, copy_entries << DENTRY_SIZE_BITS);
476 dcache_modify(sb, sec);
477 num_entries -= copy_entries;
478
479 if (num_entries) {
480 // get next sector
481 if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
482 clu = SECT_TO_CLUS(fsi, sec);
483 if (es->alloc_flag == 0x03)
484 clu++;
485 else if (get_next_clus_safe(sb, &clu))
486 goto err_out;
487 sec = CLUS_TO_SECT(fsi, clu);
488 } else {
489 sec++;
490 }
491 off = 0;
492 buf_off += copy_entries << DENTRY_SIZE_BITS;
493 }
494 }
495
496 TMSG("%s exited successfully\n", __func__);
497 return 0;
498 err_out:
499 TMSG("%s failed\n", __func__);
500 return -EIO;
501 }
502
503 /* write back all entries in entry set */
504 static s32 __write_whole_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
505 {
506 return __write_partial_entries_in_entry_set(sb, es, es->sector, es->offset, es->num_entries);
507 }
508
509 s32 update_dir_chksum_with_entry_set(struct super_block *sb, ENTRY_SET_CACHE_T *es)
510 {
511 DENTRY_T *ep;
512 u16 chksum = 0;
513 s32 chksum_type = CS_DIR_ENTRY, i;
514
515 ep = (DENTRY_T *)&(es->__buf);
516 for (i = 0; i < es->num_entries; i++) {
517 MMSG("%s %p\n", __func__, ep);
518 chksum = calc_chksum_2byte((void *) ep, DENTRY_SIZE, chksum, chksum_type);
519 ep++;
520 chksum_type = CS_DEFAULT;
521 }
522
523 ep = (DENTRY_T *)&(es->__buf);
524 ((FILE_DENTRY_T *)ep)->checksum = cpu_to_le16(chksum);
525 return __write_whole_entry_set(sb, es);
526 }
527
528 /* returns a set of dentries for a file or dir.
529 * Note that this is a copy (dump) of dentries so that user should call write_entry_set()
530 * to apply changes made in this entry set to the real device.
531 * in:
532 * sb+p_dir+entry: indicates a file/dir
533 * type: specifies how many dentries should be included.
534 * out:
535 * file_ep: will point the first dentry(= file dentry) on success
536 * return:
537 * pointer of entry set on success,
538 * NULL on failure.
539 */
540
541 #define ES_MODE_STARTED 0
542 #define ES_MODE_GET_FILE_ENTRY 1
543 #define ES_MODE_GET_STRM_ENTRY 2
544 #define ES_MODE_GET_NAME_ENTRY 3
545 #define ES_MODE_GET_CRITICAL_SEC_ENTRY 4
546 ENTRY_SET_CACHE_T *get_dentry_set_in_dir(struct super_block *sb,
547 CHAIN_T *p_dir, s32 entry, u32 type, DENTRY_T **file_ep)
548 {
549 s32 ret;
550 u32 off, byte_offset, clu = 0;
551 u32 entry_type;
552 u64 sec;
553 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
554 ENTRY_SET_CACHE_T *es = NULL;
555 DENTRY_T *ep, *pos;
556 u8 *buf;
557 u8 num_entries;
558 s32 mode = ES_MODE_STARTED;
559
560 /* FIXME : is available in error case? */
561 if (p_dir->dir == DIR_DELETED) {
562 EMSG("%s : access to deleted dentry\n", __func__);
563 BUG_ON(!fsi->prev_eio);
564 return NULL;
565 }
566
567 TMSG("%s entered\n", __func__);
568 MMSG("p_dir dir %u flags %x size %d\n", p_dir->dir, p_dir->flags, p_dir->size);
569 MMSG("entry %d type %d\n", entry, type);
570
571 byte_offset = entry << DENTRY_SIZE_BITS;
572 ret = walk_fat_chain(sb, p_dir, byte_offset, &clu);
573 if (ret)
574 return NULL;
575
576 /* byte offset in cluster */
577 byte_offset &= fsi->cluster_size - 1;
578
579 /* byte offset in sector */
580 off = byte_offset & (u32)(sb->s_blocksize - 1);
581
582 /* sector offset in cluster */
583 sec = byte_offset >> (sb->s_blocksize_bits);
584 sec += CLUS_TO_SECT(fsi, clu);
585
586 buf = dcache_getblk(sb, sec);
587 if (!buf)
588 goto err_out;
589
590 ep = (DENTRY_T *)(buf + off);
591 entry_type = exfat_get_entry_type(ep);
592
593 if ((entry_type != TYPE_FILE)
594 && (entry_type != TYPE_DIR))
595 goto err_out;
596
597 if (type == ES_ALL_ENTRIES)
598 num_entries = ((FILE_DENTRY_T *)ep)->num_ext+1;
599 else
600 num_entries = type;
601
602 MMSG("trying to malloc %lx bytes for %d entries\n",
603 (unsigned long)(offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), num_entries);
604 es = kmalloc((offsetof(ENTRY_SET_CACHE_T, __buf) + (num_entries) * sizeof(DENTRY_T)), GFP_KERNEL);
605 if (!es) {
606 EMSG("%s: failed to alloc entryset\n", __func__);
607 goto err_out;
608 }
609
610 es->num_entries = num_entries;
611 es->sector = sec;
612 es->offset = off;
613 es->alloc_flag = p_dir->flags;
614
615 pos = (DENTRY_T *) &(es->__buf);
616
617 while (num_entries) {
618 // instead of copying whole sector, we will check every entry.
619 // this will provide minimum stablity and consistency.
620 entry_type = exfat_get_entry_type(ep);
621
622 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED))
623 goto err_out;
624
625 switch (mode) {
626 case ES_MODE_STARTED:
627 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR))
628 mode = ES_MODE_GET_FILE_ENTRY;
629 else
630 goto err_out;
631 break;
632 case ES_MODE_GET_FILE_ENTRY:
633 if (entry_type == TYPE_STREAM)
634 mode = ES_MODE_GET_STRM_ENTRY;
635 else
636 goto err_out;
637 break;
638 case ES_MODE_GET_STRM_ENTRY:
639 if (entry_type == TYPE_EXTEND)
640 mode = ES_MODE_GET_NAME_ENTRY;
641 else
642 goto err_out;
643 break;
644 case ES_MODE_GET_NAME_ENTRY:
645 if (entry_type == TYPE_EXTEND)
646 break;
647 else if (entry_type == TYPE_STREAM)
648 goto err_out;
649 else if (entry_type & TYPE_CRITICAL_SEC)
650 mode = ES_MODE_GET_CRITICAL_SEC_ENTRY;
651 else
652 goto err_out;
653 break;
654 case ES_MODE_GET_CRITICAL_SEC_ENTRY:
655 if ((entry_type == TYPE_EXTEND) || (entry_type == TYPE_STREAM))
656 goto err_out;
657 else if ((entry_type & TYPE_CRITICAL_SEC) != TYPE_CRITICAL_SEC)
658 goto err_out;
659 break;
660 }
661
662 /* copy dentry */
663 memcpy(pos, ep, sizeof(DENTRY_T));
664
665 if (--num_entries == 0)
666 break;
667
668 if (((off + DENTRY_SIZE) & (u32)(sb->s_blocksize - 1)) <
669 (off & (u32)(sb->s_blocksize - 1))) {
670 // get the next sector
671 if (IS_LAST_SECT_IN_CLUS(fsi, sec)) {
672 if (es->alloc_flag == 0x03)
673 clu++;
674 else if (get_next_clus_safe(sb, &clu))
675 goto err_out;
676 sec = CLUS_TO_SECT(fsi, clu);
677 } else {
678 sec++;
679 }
680 buf = dcache_getblk(sb, sec);
681 if (!buf)
682 goto err_out;
683 off = 0;
684 ep = (DENTRY_T *)(buf);
685 } else {
686 ep++;
687 off += DENTRY_SIZE;
688 }
689 pos++;
690 }
691
692 if (file_ep)
693 *file_ep = (DENTRY_T *)&(es->__buf);
694
695 MMSG("es sec %llu offset %u flags %d, num_entries %u buf ptr %p\n",
696 es->sector, es->offset, es->alloc_flag, es->num_entries, &(es->__buf));
697 TMSG("%s exited %p\n", __func__, es);
698 return es;
699 err_out:
700 TMSG("%s exited (return NULL) (es %p)\n", __func__, es);
701
702 /* kfree(NULL) is safe */
703 kfree(es);
704 es = NULL;
705 return NULL;
706 }
707
708 void release_dentry_set(ENTRY_SET_CACHE_T *es)
709 {
710 TMSG("%s %p\n", __func__, es);
711
712 /* kfree(NULL) is safe */
713 kfree(es);
714 es = NULL;
715 }
716
717 static s32 __extract_uni_name_from_name_entry(NAME_DENTRY_T *ep, u16 *uniname, s32 order)
718 {
719 s32 i, len = 0;
720
721 for (i = 0; i < 15; i++) {
722 /* FIXME : unaligned? */
723 *uniname = le16_to_cpu(ep->unicode_0_14[i]);
724 if (*uniname == 0x0)
725 return len;
726 uniname++;
727 len++;
728 }
729
730 *uniname = 0x0;
731 return len;
732
733 } /* end of __extract_uni_name_from_name_entry */
734
735 #define DIRENT_STEP_FILE (0)
736 #define DIRENT_STEP_STRM (1)
737 #define DIRENT_STEP_NAME (2)
738 #define DIRENT_STEP_SECD (3)
739
740 /* return values of exfat_find_dir_entry()
741 * >= 0 : return dir entiry position with the name in dir
742 * -EEXIST : (root dir, ".") it is the root dir itself
743 * -ENOENT : entry with the name does not exist
744 * -EIO : I/O error
745 */
746 static s32 exfat_find_dir_entry(struct super_block *sb, FILE_ID_T *fid,
747 CHAIN_T *p_dir, UNI_NAME_T *p_uniname, s32 num_entries, DOS_NAME_T *unused, u32 type)
748 {
749 s32 i, rewind = 0, dentry = 0, end_eidx = 0, num_ext = 0, len;
750 s32 order, step, name_len;
751 s32 dentries_per_clu, num_empty = 0;
752 u32 entry_type;
753 u16 entry_uniname[16], *uniname = NULL, unichar;
754 CHAIN_T clu;
755 DENTRY_T *ep;
756 HINT_T *hint_stat = &fid->hint_stat;
757 HINT_FEMP_T candi_empty;
758 FILE_DENTRY_T *file_ep;
759 STRM_DENTRY_T *strm_ep;
760 NAME_DENTRY_T *name_ep;
761 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
762
763 /*
764 * REMARK:
765 * DOT and DOTDOT are handled by VFS layer
766 */
767
768 if (IS_CLUS_FREE(p_dir->dir))
769 return -EIO;
770
771 dentries_per_clu = fsi->dentries_per_clu;
772
773 clu.dir = p_dir->dir;
774 clu.size = p_dir->size;
775 clu.flags = p_dir->flags;
776
777 if (hint_stat->eidx) {
778 clu.dir = hint_stat->clu;
779 dentry = hint_stat->eidx;
780 end_eidx = dentry;
781 }
782
783 candi_empty.eidx = -1;
784 rewind:
785 order = 0;
786 step = DIRENT_STEP_FILE;
787 while (!IS_CLUS_EOF(clu.dir)) {
788 i = dentry & (dentries_per_clu - 1);
789 for (; i < dentries_per_clu; i++, dentry++) {
790 if (rewind && (dentry == end_eidx))
791 goto not_found;
792
793 ep = get_dentry_in_dir(sb, &clu, i, NULL);
794 if (!ep)
795 return -EIO;
796
797 entry_type = exfat_get_entry_type(ep);
798
799 if ((entry_type == TYPE_UNUSED) || (entry_type == TYPE_DELETED)) {
800 step = DIRENT_STEP_FILE;
801
802 num_empty++;
803 if (candi_empty.eidx == -1) {
804 if (num_empty == 1) {
805 candi_empty.cur.dir = clu.dir;
806 candi_empty.cur.size = clu.size;
807 candi_empty.cur.flags = clu.flags;
808 }
809
810 if (num_empty >= num_entries) {
811 candi_empty.eidx = dentry - (num_empty - 1);
812 ASSERT(0 <= candi_empty.eidx);
813 candi_empty.count = num_empty;
814
815 if ((fid->hint_femp.eidx == -1) ||
816 (candi_empty.eidx <= fid->hint_femp.eidx)) {
817 memcpy(&fid->hint_femp,
818 &candi_empty,
819 sizeof(HINT_FEMP_T));
820 }
821 }
822 }
823
824 if (entry_type == TYPE_UNUSED)
825 goto not_found;
826 continue;
827 }
828
829 num_empty = 0;
830 candi_empty.eidx = -1;
831
832 if ((entry_type == TYPE_FILE) || (entry_type == TYPE_DIR)) {
833 step = DIRENT_STEP_FILE;
834 if ((type == TYPE_ALL) || (type == entry_type)) {
835 file_ep = (FILE_DENTRY_T *) ep;
836 num_ext = file_ep->num_ext;
837 step = DIRENT_STEP_STRM;
838 }
839 continue;
840 }
841
842 if (entry_type == TYPE_STREAM) {
843 if (step != DIRENT_STEP_STRM) {
844 step = DIRENT_STEP_FILE;
845 continue;
846 }
847 step = DIRENT_STEP_FILE;
848 strm_ep = (STRM_DENTRY_T *) ep;
849 if ((p_uniname->name_hash == le16_to_cpu(strm_ep->name_hash)) &&
850 (p_uniname->name_len == strm_ep->name_len)) {
851 step = DIRENT_STEP_NAME;
852 order = 1;
853 name_len = 0;
854 }
855 continue;
856 }
857
858 if (entry_type == TYPE_EXTEND) {
859 if (step != DIRENT_STEP_NAME) {
860 step = DIRENT_STEP_FILE;
861 continue;
862 }
863 name_ep = (NAME_DENTRY_T *) ep;
864
865 if ((++order) == 2)
866 uniname = p_uniname->name;
867 else
868 uniname += 15;
869
870 len = __extract_uni_name_from_name_entry(name_ep, entry_uniname, order);
871 name_len += len;
872
873 unichar = *(uniname+len);
874 *(uniname+len) = 0x0;
875
876 if (nls_cmp_uniname(sb, uniname, entry_uniname)) {
877 step = DIRENT_STEP_FILE;
878 } else if (name_len == p_uniname->name_len) {
879 if (order == num_ext) {
880 //fid->hint_femp.eidx = -1;
881 goto found;
882 }
883 step = DIRENT_STEP_SECD;
884 }
885
886 *(uniname+len) = unichar;
887 continue;
888 }
889
890 if (entry_type & (TYPE_CRITICAL_SEC | TYPE_BENIGN_SEC)) {
891 if (step == DIRENT_STEP_SECD) {
892 if (++order == num_ext)
893 goto found;
894 continue;
895 }
896 }
897 step = DIRENT_STEP_FILE;
898 }
899
900 if (clu.flags == 0x03) {
901 if ((--clu.size) > 0)
902 clu.dir++;
903 else
904 clu.dir = CLUS_EOF;
905 } else {
906 if (get_next_clus_safe(sb, &clu.dir))
907 return -EIO;
908 }
909 }
910
911 not_found:
912 /* we started at not 0 index,so we should try to find target
913 * from 0 index to the index we started at.
914 */
915 if (!rewind && end_eidx) {
916 rewind = 1;
917 dentry = 0;
918 clu.dir = p_dir->dir;
919 /* reset empty hint */
920 num_empty = 0;
921 candi_empty.eidx = -1;
922 goto rewind;
923 }
924
925 /* initialized hint_stat */
926 hint_stat->clu = p_dir->dir;
927 hint_stat->eidx = 0;
928 return -ENOENT;
929
930 found:
931 /* next dentry we'll find is out of this cluster */
932 if (!((dentry + 1) & (dentries_per_clu-1))) {
933 int ret = 0;
934
935 if (clu.flags == 0x03) {
936 if ((--clu.size) > 0)
937 clu.dir++;
938 else
939 clu.dir = CLUS_EOF;
940 } else {
941 ret = get_next_clus_safe(sb, &clu.dir);
942 }
943
944 if (ret || IS_CLUS_EOF(clu.dir)) {
945 /* just initialized hint_stat */
946 hint_stat->clu = p_dir->dir;
947 hint_stat->eidx = 0;
948 return (dentry - num_ext);
949 }
950 }
951
952 hint_stat->clu = clu.dir;
953 hint_stat->eidx = dentry + 1;
954 return (dentry - num_ext);
955 } /* end of exfat_find_dir_entry */
956
957 /* returns -EIO on error */
958 static s32 exfat_count_ext_entries(struct super_block *sb, CHAIN_T *p_dir, s32 entry, DENTRY_T *p_entry)
959 {
960 s32 i, count = 0;
961 u32 type;
962 FILE_DENTRY_T *file_ep = (FILE_DENTRY_T *) p_entry;
963 DENTRY_T *ext_ep;
964
965 for (i = 0, entry++; i < file_ep->num_ext; i++, entry++) {
966 ext_ep = get_dentry_in_dir(sb, p_dir, entry, NULL);
967 if (!ext_ep)
968 return -EIO;
969
970 type = exfat_get_entry_type(ext_ep);
971 if ((type == TYPE_EXTEND) || (type == TYPE_STREAM))
972 count++;
973 else
974 return count;
975 }
976
977 return count;
978 } /* end of exfat_count_ext_entries */
979
980
981 /*
982 * Name Conversion Functions
983 */
984 static void exfat_get_uniname_from_ext_entry(struct super_block *sb, CHAIN_T *p_dir, s32 entry, u16 *uniname)
985 {
986 s32 i;
987 DENTRY_T *ep;
988 ENTRY_SET_CACHE_T *es;
989
990 es = get_dentry_set_in_dir(sb, p_dir, entry, ES_ALL_ENTRIES, &ep);
991 if (!es)
992 return;
993
994 if (es->num_entries < 3)
995 goto out;
996
997 ep += 2;
998
999 /*
1000 * First entry : file entry
1001 * Second entry : stream-extension entry
1002 * Third entry : first file-name entry
1003 * So, the index of first file-name dentry should start from 2.
1004 */
1005 for (i = 2; i < es->num_entries; i++, ep++) {
1006 /* end of name entry */
1007 if (exfat_get_entry_type(ep) != TYPE_EXTEND)
1008 goto out;
1009
1010 __extract_uni_name_from_name_entry((NAME_DENTRY_T *)ep, uniname, i);
1011 uniname += 15;
1012 }
1013
1014 out:
1015 release_dentry_set(es);
1016 } /* end of exfat_get_uniname_from_ext_entry */
1017
1018 static s32 exfat_calc_num_entries(UNI_NAME_T *p_uniname)
1019 {
1020 s32 len;
1021
1022 len = p_uniname->name_len;
1023 if (len == 0)
1024 return 0;
1025
1026 /* 1 file entry + 1 stream entry + name entries */
1027 return((len-1) / 15 + 3);
1028
1029 } /* end of exfat_calc_num_entries */
1030
1031 static s32 exfat_check_max_dentries(FILE_ID_T *fid)
1032 {
1033 if ((fid->size >> DENTRY_SIZE_BITS) >= MAX_EXFAT_DENTRIES) {
1034 /* exFAT spec allows a dir to grow upto 8388608(256MB) dentries */
1035 return -ENOSPC;
1036 }
1037 return 0;
1038 } /* end of check_max_dentries */
1039
1040 /*
1041 * Allocation Bitmap Management Functions
1042 */
1043 s32 load_alloc_bmp(struct super_block *sb)
1044 {
1045 s32 ret;
1046 u32 i, j, map_size, type, need_map_size;
1047 u64 sector;
1048 CHAIN_T clu;
1049 BMAP_DENTRY_T *ep;
1050 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1051
1052 clu.dir = fsi->root_dir;
1053 clu.flags = 0x01;
1054
1055 while (!IS_CLUS_EOF(clu.dir)) {
1056 for (i = 0; i < fsi->dentries_per_clu; i++) {
1057 ep = (BMAP_DENTRY_T *) get_dentry_in_dir(sb, &clu, i, NULL);
1058 if (!ep)
1059 return -EIO;
1060
1061 type = exfat_get_entry_type((DENTRY_T *) ep);
1062
1063 if (type == TYPE_UNUSED)
1064 break;
1065 if (type != TYPE_BITMAP)
1066 continue;
1067
1068 if (ep->flags == 0x0) {
1069 fsi->map_clu = le32_to_cpu(ep->start_clu);
1070 map_size = (u32) le64_to_cpu(ep->size);
1071
1072 need_map_size = (((fsi->num_clusters - CLUS_BASE) - 1) >> 3) + 1;
1073 if (need_map_size != map_size) {
1074 sdfat_log_msg(sb, KERN_ERR,
1075 "bogus allocation bitmap size(need : %u, cur : %u)",
1076 need_map_size, map_size);
1077 /* Only allowed when bogus allocation bitmap size is large */
1078 if (need_map_size > map_size)
1079 return -EIO;
1080 }
1081 fsi->map_sectors = ((need_map_size - 1) >> (sb->s_blocksize_bits)) + 1;
1082 fsi->vol_amap =
1083 kmalloc((sizeof(struct buffer_head *) * fsi->map_sectors), GFP_KERNEL);
1084 if (!fsi->vol_amap)
1085 return -ENOMEM;
1086
1087 sector = CLUS_TO_SECT(fsi, fsi->map_clu);
1088
1089 for (j = 0; j < fsi->map_sectors; j++) {
1090 fsi->vol_amap[j] = NULL;
1091 ret = read_sect(sb, sector+j, &(fsi->vol_amap[j]), 1);
1092 if (ret) {
1093 /* release all buffers and free vol_amap */
1094 i = 0;
1095 while (i < j)
1096 brelse(fsi->vol_amap[i++]);
1097
1098 /* kfree(NULL) is safe */
1099 kfree(fsi->vol_amap);
1100 fsi->vol_amap = NULL;
1101 return ret;
1102 }
1103 }
1104
1105 fsi->pbr_bh = NULL;
1106 return 0;
1107 }
1108 }
1109
1110 if (get_next_clus_safe(sb, &clu.dir))
1111 return -EIO;
1112 }
1113
1114 return -EINVAL;
1115 } /* end of load_alloc_bmp */
1116
1117 void free_alloc_bmp(struct super_block *sb)
1118 {
1119 s32 i;
1120 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1121
1122 brelse(fsi->pbr_bh);
1123
1124 for (i = 0; i < fsi->map_sectors; i++)
1125 __brelse(fsi->vol_amap[i]);
1126
1127 /* kfree(NULL) is safe */
1128 kfree(fsi->vol_amap);
1129 fsi->vol_amap = NULL;
1130 }
1131
1132 /* WARN :
1133 * If the value of "clu" is 0, it means cluster 2 which is
1134 * the first cluster of cluster heap.
1135 */
1136 static s32 set_alloc_bitmap(struct super_block *sb, u32 clu)
1137 {
1138 s32 i, b;
1139 u64 sector;
1140 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1141
1142 i = clu >> (sb->s_blocksize_bits + 3);
1143 b = clu & (u32)((sb->s_blocksize << 3) - 1);
1144
1145 sector = CLUS_TO_SECT(fsi, fsi->map_clu) + i;
1146 bitmap_set((unsigned long *)(fsi->vol_amap[i]->b_data), b, 1);
1147
1148 return write_sect(sb, sector, fsi->vol_amap[i], 0);
1149 } /* end of set_alloc_bitmap */
1150
1151 /* WARN :
1152 * If the value of "clu" is 0, it means cluster 2 which is
1153 * the first cluster of cluster heap.
1154 */
1155 static s32 clr_alloc_bitmap(struct super_block *sb, u32 clu)
1156 {
1157 s32 ret;
1158 s32 i, b;
1159 u64 sector;
1160 struct sdfat_sb_info *sbi = SDFAT_SB(sb);
1161 struct sdfat_mount_options *opts = &sbi->options;
1162 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1163
1164 i = clu >> (sb->s_blocksize_bits + 3);
1165 b = clu & (u32)((sb->s_blocksize << 3) - 1);
1166
1167 sector = CLUS_TO_SECT(fsi, fsi->map_clu) + i;
1168
1169 bitmap_clear((unsigned long *)(fsi->vol_amap[i]->b_data), b, 1);
1170
1171 ret = write_sect(sb, sector, fsi->vol_amap[i], 0);
1172
1173 if (opts->discard) {
1174 s32 ret_discard;
1175
1176 TMSG("discard cluster(%08x)\n", clu+2);
1177 ret_discard = sb_issue_discard(sb, CLUS_TO_SECT(fsi, clu+2),
1178 (1 << fsi->sect_per_clus_bits), GFP_NOFS, 0);
1179
1180 if (ret_discard == -EOPNOTSUPP) {
1181 sdfat_msg(sb, KERN_ERR,
1182 "discard not supported by device, disabling");
1183 opts->discard = 0;
1184 }
1185 }
1186
1187 return ret;
1188 } /* end of clr_alloc_bitmap */
1189
1190 /* WARN :
1191 * If the value of "clu" is 0, it means cluster 2 which is
1192 * the first cluster of cluster heap.
1193 */
1194 static u32 test_alloc_bitmap(struct super_block *sb, u32 clu)
1195 {
1196 u32 i, map_i, map_b;
1197 u32 clu_base, clu_free;
1198 u8 k, clu_mask;
1199 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1200
1201 clu_base = (clu & ~(0x7)) + 2;
1202 clu_mask = (1 << (clu - clu_base + 2)) - 1;
1203
1204 map_i = clu >> (sb->s_blocksize_bits + 3);
1205 map_b = (clu >> 3) & (u32)(sb->s_blocksize - 1);
1206
1207 for (i = 2; i < fsi->num_clusters; i += 8) {
1208 k = *(((u8 *) fsi->vol_amap[map_i]->b_data) + map_b);
1209 if (clu_mask > 0) {
1210 k |= clu_mask;
1211 clu_mask = 0;
1212 }
1213 if (k < 0xFF) {
1214 clu_free = clu_base + free_bit[k];
1215 if (clu_free < fsi->num_clusters)
1216 return clu_free;
1217 }
1218 clu_base += 8;
1219
1220 if (((++map_b) >= (u32)sb->s_blocksize) ||
1221 (clu_base >= fsi->num_clusters)) {
1222 if ((++map_i) >= fsi->map_sectors) {
1223 clu_base = 2;
1224 map_i = 0;
1225 }
1226 map_b = 0;
1227 }
1228 }
1229
1230 return CLUS_EOF;
1231 } /* end of test_alloc_bitmap */
1232
1233 void sync_alloc_bmp(struct super_block *sb)
1234 {
1235 s32 i;
1236 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1237
1238 if (fsi->vol_amap == NULL)
1239 return;
1240
1241 for (i = 0; i < fsi->map_sectors; i++)
1242 sync_dirty_buffer(fsi->vol_amap[i]);
1243 }
1244
1245 static s32 exfat_chain_cont_cluster(struct super_block *sb, u32 chain, u32 len)
1246 {
1247 if (!len)
1248 return 0;
1249
1250 while (len > 1) {
1251 if (fat_ent_set(sb, chain, chain+1))
1252 return -EIO;
1253 chain++;
1254 len--;
1255 }
1256
1257 if (fat_ent_set(sb, chain, CLUS_EOF))
1258 return -EIO;
1259 return 0;
1260 }
1261
1262 s32 chain_cont_cluster(struct super_block *sb, u32 chain, u32 len)
1263 {
1264 return exfat_chain_cont_cluster(sb, chain, len);
1265 }
1266
1267
1268 static s32 exfat_free_cluster(struct super_block *sb, CHAIN_T *p_chain, s32 do_relse)
1269 {
1270 s32 ret = -EIO;
1271 u32 num_clusters = 0;
1272 u32 clu;
1273 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1274 s32 i;
1275 u64 sector;
1276
1277 /* invalid cluster number */
1278 if (IS_CLUS_FREE(p_chain->dir) || IS_CLUS_EOF(p_chain->dir))
1279 return 0;
1280
1281 /* no cluster to truncate */
1282 if (p_chain->size == 0) {
1283 DMSG("%s: cluster(%u) truncation is not required.",
1284 __func__, p_chain->dir);
1285 return 0;
1286 }
1287
1288 /* check cluster validation */
1289 if (!is_valid_clus(fsi, p_chain->dir)) {
1290 EMSG("%s: invalid start cluster (%u)\n", __func__, p_chain->dir);
1291 sdfat_debug_bug_on(1);
1292 return -EIO;
1293 }
1294
1295 set_sb_dirty(sb);
1296 clu = p_chain->dir;
1297
1298 if (p_chain->flags == 0x03) {
1299 do {
1300 if (do_relse) {
1301 sector = CLUS_TO_SECT(fsi, clu);
1302 for (i = 0; i < fsi->sect_per_clus; i++) {
1303 if (dcache_release(sb, sector+i) == -EIO)
1304 goto out;
1305 }
1306 }
1307
1308 if (clr_alloc_bitmap(sb, clu-2))
1309 goto out;
1310 clu++;
1311
1312 num_clusters++;
1313 } while (num_clusters < p_chain->size);
1314 } else {
1315 do {
1316 if (do_relse) {
1317 sector = CLUS_TO_SECT(fsi, clu);
1318 for (i = 0; i < fsi->sect_per_clus; i++) {
1319 if (dcache_release(sb, sector+i) == -EIO)
1320 goto out;
1321 }
1322 }
1323
1324 if (clr_alloc_bitmap(sb, (clu - CLUS_BASE)))
1325 goto out;
1326
1327 if (get_next_clus_safe(sb, &clu))
1328 goto out;
1329
1330 num_clusters++;
1331 } while (!IS_CLUS_EOF(clu));
1332 }
1333
1334 /* success */
1335 ret = 0;
1336 out:
1337
1338 fsi->used_clusters -= num_clusters;
1339 return ret;
1340 } /* end of exfat_free_cluster */
1341
1342 static s32 exfat_alloc_cluster(struct super_block *sb, u32 num_alloc, CHAIN_T *p_chain, s32 dest)
1343 {
1344 s32 ret = -ENOSPC;
1345 u32 num_clusters = 0, total_cnt;
1346 u32 hint_clu, new_clu, last_clu = CLUS_EOF;
1347 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1348
1349 total_cnt = fsi->num_clusters - CLUS_BASE;
1350
1351 if (unlikely(total_cnt < fsi->used_clusters)) {
1352 sdfat_fs_error_ratelimit(sb,
1353 "%s: invalid used clusters(t:%u,u:%u)\n",
1354 __func__, total_cnt, fsi->used_clusters);
1355 return -EIO;
1356 }
1357
1358 if (num_alloc > total_cnt - fsi->used_clusters)
1359 return -ENOSPC;
1360
1361 hint_clu = p_chain->dir;
1362 /* find new cluster */
1363 if (IS_CLUS_EOF(hint_clu)) {
1364 if (fsi->clu_srch_ptr < CLUS_BASE) {
1365 EMSG("%s: fsi->clu_srch_ptr is invalid (%u)\n",
1366 __func__, fsi->clu_srch_ptr);
1367 ASSERT(0);
1368 fsi->clu_srch_ptr = CLUS_BASE;
1369 }
1370
1371 hint_clu = test_alloc_bitmap(sb, fsi->clu_srch_ptr - CLUS_BASE);
1372 if (IS_CLUS_EOF(hint_clu))
1373 return -ENOSPC;
1374 }
1375
1376 /* check cluster validation */
1377 if (!is_valid_clus(fsi, hint_clu)) {
1378 /* "last + 1" can be passed as hint_clu. Otherwise, bug_on */
1379 if (hint_clu != fsi->num_clusters) {
1380 EMSG("%s: hint_cluster is invalid (%u)\n",
1381 __func__, hint_clu);
1382 sdfat_debug_bug_on(1);
1383 }
1384 hint_clu = CLUS_BASE;
1385 if (p_chain->flags == 0x03) {
1386 if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters))
1387 return -EIO;
1388 p_chain->flags = 0x01;
1389 }
1390 }
1391
1392 set_sb_dirty(sb);
1393
1394 p_chain->dir = CLUS_EOF;
1395
1396 while ((new_clu = test_alloc_bitmap(sb, hint_clu - CLUS_BASE)) != CLUS_EOF) {
1397 if ((new_clu != hint_clu) && (p_chain->flags == 0x03)) {
1398 if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters)) {
1399 ret = -EIO;
1400 goto error;
1401 }
1402 p_chain->flags = 0x01;
1403 }
1404
1405 /* update allocation bitmap */
1406 if (set_alloc_bitmap(sb, new_clu - CLUS_BASE)) {
1407 ret = -EIO;
1408 goto error;
1409 }
1410
1411 num_clusters++;
1412
1413 /* update FAT table */
1414 if (p_chain->flags == 0x01) {
1415 if (fat_ent_set(sb, new_clu, CLUS_EOF)) {
1416 ret = -EIO;
1417 goto error;
1418 }
1419 }
1420
1421 if (IS_CLUS_EOF(p_chain->dir)) {
1422 p_chain->dir = new_clu;
1423 } else if (p_chain->flags == 0x01) {
1424 if (fat_ent_set(sb, last_clu, new_clu)) {
1425 ret = -EIO;
1426 goto error;
1427 }
1428 }
1429 last_clu = new_clu;
1430
1431 if ((--num_alloc) == 0) {
1432 fsi->clu_srch_ptr = hint_clu;
1433 fsi->used_clusters += num_clusters;
1434
1435 p_chain->size += num_clusters;
1436 return 0;
1437 }
1438
1439 hint_clu = new_clu + 1;
1440 if (hint_clu >= fsi->num_clusters) {
1441 hint_clu = CLUS_BASE;
1442
1443 if (p_chain->flags == 0x03) {
1444 if (exfat_chain_cont_cluster(sb, p_chain->dir, num_clusters)) {
1445 ret = -EIO;
1446 goto error;
1447 }
1448 p_chain->flags = 0x01;
1449 }
1450 }
1451 }
1452 error:
1453 if (num_clusters)
1454 exfat_free_cluster(sb, p_chain, 0);
1455 return ret;
1456 } /* end of exfat_alloc_cluster */
1457
1458 static s32 exfat_count_used_clusters(struct super_block *sb, u32 *ret_count)
1459 {
1460 u32 count = 0;
1461 u32 i, map_i, map_b;
1462 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1463 u32 total_clus = fsi->num_clusters - 2;
1464
1465 map_i = map_b = 0;
1466
1467 for (i = 0; i < total_clus; i += 8) {
1468 u8 k = *(((u8 *) fsi->vol_amap[map_i]->b_data) + map_b);
1469
1470 count += used_bit[k];
1471 if ((++map_b) >= (u32)sb->s_blocksize) {
1472 map_i++;
1473 map_b = 0;
1474 }
1475 }
1476
1477 /* FIXME : abnormal bitmap count should be handled as more smart */
1478 if (total_clus < count)
1479 count = total_clus;
1480
1481 *ret_count = count;
1482 return 0;
1483 } /* end of exfat_count_used_clusters */
1484
1485
1486 /*
1487 * File Operation Functions
1488 */
1489 static FS_FUNC_T exfat_fs_func = {
1490 .alloc_cluster = exfat_alloc_cluster,
1491 .free_cluster = exfat_free_cluster,
1492 .count_used_clusters = exfat_count_used_clusters,
1493
1494 .init_dir_entry = exfat_init_dir_entry,
1495 .init_ext_entry = exfat_init_ext_entry,
1496 .find_dir_entry = exfat_find_dir_entry,
1497 .delete_dir_entry = exfat_delete_dir_entry,
1498 .get_uniname_from_ext_entry = exfat_get_uniname_from_ext_entry,
1499 .count_ext_entries = exfat_count_ext_entries,
1500 .calc_num_entries = exfat_calc_num_entries,
1501 .check_max_dentries = exfat_check_max_dentries,
1502
1503 .get_entry_type = exfat_get_entry_type,
1504 .set_entry_type = exfat_set_entry_type,
1505 .get_entry_attr = exfat_get_entry_attr,
1506 .set_entry_attr = exfat_set_entry_attr,
1507 .get_entry_flag = exfat_get_entry_flag,
1508 .set_entry_flag = exfat_set_entry_flag,
1509 .get_entry_clu0 = exfat_get_entry_clu0,
1510 .set_entry_clu0 = exfat_set_entry_clu0,
1511 .get_entry_size = exfat_get_entry_size,
1512 .set_entry_size = exfat_set_entry_size,
1513 .get_entry_time = exfat_get_entry_time,
1514 .set_entry_time = exfat_set_entry_time,
1515 };
1516
1517 s32 mount_exfat(struct super_block *sb, pbr_t *p_pbr)
1518 {
1519 pbr64_t *p_bpb = (pbr64_t *)p_pbr;
1520 FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
1521
1522 fsi->sect_per_clus = 1 << p_bpb->bsx.sect_per_clus_bits;
1523 fsi->sect_per_clus_bits = p_bpb->bsx.sect_per_clus_bits;
1524 fsi->cluster_size_bits = fsi->sect_per_clus_bits + sb->s_blocksize_bits;
1525 fsi->cluster_size = 1 << fsi->cluster_size_bits;
1526
1527 if (!p_bpb->bsx.num_fats) {
1528 sdfat_msg(sb, KERN_ERR, "bogus number of FAT structure");
1529 return -EINVAL;
1530 }
1531
1532 if (p_bpb->bsx.num_fats >= 2) {
1533 sdfat_msg(sb, KERN_WARNING,
1534 "unsupported number of FAT structure :%u, try with 1",
1535 p_bpb->bsx.num_fats);
1536 }
1537
1538 fsi->num_FAT_sectors = le32_to_cpu(p_bpb->bsx.fat_length);
1539 if (!fsi->num_FAT_sectors) {
1540 sdfat_msg(sb, KERN_ERR, "bogus fat size");
1541 return -EINVAL;
1542 }
1543
1544 fsi->FAT1_start_sector = le32_to_cpu(p_bpb->bsx.fat_offset);
1545 fsi->FAT2_start_sector = fsi->FAT1_start_sector;
1546
1547 fsi->root_start_sector = le32_to_cpu(p_bpb->bsx.clu_offset);
1548 fsi->data_start_sector = fsi->root_start_sector;
1549
1550 fsi->num_sectors = le64_to_cpu(p_bpb->bsx.vol_length);
1551 if (!fsi->num_sectors) {
1552 sdfat_msg(sb, KERN_ERR, "bogus number of total sector count");
1553 return -EINVAL;
1554 }
1555
1556 /* because the cluster index starts with 2 */
1557 fsi->num_clusters = le32_to_cpu(p_bpb->bsx.clu_count) + CLUS_BASE;
1558
1559 fsi->vol_id = le32_to_cpu(p_bpb->bsx.vol_serial);
1560 fsi->root_dir = le32_to_cpu(p_bpb->bsx.root_cluster);
1561 fsi->dentries_in_root = 0;
1562 fsi->dentries_per_clu = 1 << (fsi->cluster_size_bits - DENTRY_SIZE_BITS);
1563 fsi->vol_flag = (u32) le16_to_cpu(p_bpb->bsx.vol_flags);
1564 fsi->clu_srch_ptr = CLUS_BASE;
1565 fsi->used_clusters = (u32) ~0;
1566
1567 fsi->fs_func = &exfat_fs_func;
1568 fsi->vol_type = EXFAT;
1569 fat_ent_ops_init(sb);
1570
1571 if (p_bpb->bsx.vol_flags & VOL_DIRTY) {
1572 fsi->vol_flag |= VOL_DIRTY;
1573 sdfat_log_msg(sb, KERN_WARNING, "Volume was not properly "
1574 "unmounted. Some data may be corrupt. "
1575 "Please run fsck.");
1576 }
1577
1578 return 0;
1579 } /* end of mount_exfat */
1580
1581 /* end of core_exfat.c */