forked from dorimanx/exfat-nofuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exfat_api.c
575 lines (420 loc) · 16 KB
/
exfat_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : exfat_api.c */
/* PURPOSE : exFAT API Glue Layer */
/* */
/*----------------------------------------------------------------------*/
/* NOTES */
/* */
/*----------------------------------------------------------------------*/
/* REVISION HISTORY (Ver 0.9) */
/* */
/* - 2010.11.15 [Joosun Hahn] : first writing */
/* */
/************************************************************************/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include "exfat_version.h"
#include "exfat_config.h"
#include "exfat_global.h"
#include "exfat_data.h"
#include "exfat_oal.h"
#include "exfat_part.h"
#include "exfat_nls.h"
#include "exfat_api.h"
#include "exfat_super.h"
#include "exfat.h"
/*----------------------------------------------------------------------*/
/* Constant & Macro Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
extern FS_STRUCT_T fs_struct[];
extern struct semaphore z_sem;
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Function Declarations */
/*----------------------------------------------------------------------*/
/*======================================================================*/
/* Global Function Definitions */
/* - All functions for global use have same return value format, */
/* that is, FFS_SUCCESS on success and several FS error code on */
/* various error condition. */
/*======================================================================*/
/*----------------------------------------------------------------------*/
/* exFAT Filesystem Init & Exit Functions */
/*----------------------------------------------------------------------*/
INT32 FsInit(void)
{
INT32 i;
/* initialize all volumes as un-mounted */
for (i = 0; i < MAX_DRIVE; i++) {
fs_struct[i].mounted = FALSE;
fs_struct[i].sb = NULL;
sm_init(&(fs_struct[i].v_sem));
}
return(ffsInit());
}
INT32 FsShutdown(void)
{
INT32 i;
/* unmount all volumes */
for (i = 0; i < MAX_DRIVE; i++) {
if (!fs_struct[i].mounted) continue;
ffsUmountVol(fs_struct[i].sb);
}
return(ffsShutdown());
}
/*----------------------------------------------------------------------*/
/* Volume Management Functions */
/*----------------------------------------------------------------------*/
/* FsMountVol : mount the file system volume */
INT32 FsMountVol(struct super_block *sb)
{
INT32 err, drv;
sm_P(&z_sem);
for (drv = 0; drv < MAX_DRIVE; drv++) {
if (!fs_struct[drv].mounted) break;
}
if (drv >= MAX_DRIVE) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[drv].v_sem));
err = buf_init(sb);
if (!err) {
err = ffsMountVol(sb, drv);
}
/* release the lock for file system critical section */
sm_V(&(fs_struct[drv].v_sem));
if (!err) {
fs_struct[drv].mounted = TRUE;
fs_struct[drv].sb = sb;
} else {
buf_shutdown(sb);
}
sm_V(&z_sem);
return(err);
} /* end of FsMountVol */
/* FsUmountVol : unmount the file system volume */
INT32 FsUmountVol(struct super_block *sb)
{
INT32 err;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
sm_P(&z_sem);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsUmountVol(sb);
buf_shutdown(sb);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
fs_struct[p_fs->drv].mounted = FALSE;
fs_struct[p_fs->drv].sb = NULL;
sm_V(&z_sem);
return(err);
} /* end of FsUmountVol */
/* FsGetVolInfo : get the information of a file system volume */
INT32 FsGetVolInfo(struct super_block *sb, VOL_INFO_T *info)
{
INT32 err;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if (info == NULL) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsGetVolInfo(sb, info);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsGetVolInfo */
/* FsSyncVol : synchronize a file system volume */
INT32 FsSyncVol(struct super_block *sb, INT32 do_sync)
{
INT32 err;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsSyncVol(sb, do_sync);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsSyncVol */
/*----------------------------------------------------------------------*/
/* File Operation Functions */
/*----------------------------------------------------------------------*/
/* FsCreateFile : create a file */
INT32 FsLookupFile(struct inode *inode, UINT8 *path, FILE_ID_T *fid)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if ((fid == NULL) || (path == NULL) || (STRLEN(path) == 0))
return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsLookupFile(inode, path, fid);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsLookupFile */
/* FsCreateFile : create a file */
INT32 FsCreateFile(struct inode *inode, UINT8 *path, UINT8 mode, FILE_ID_T *fid)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if ((fid == NULL) || (path == NULL) || (STRLEN(path) == 0))
return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsCreateFile(inode, path, mode, fid);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsCreateFile */
INT32 FsReadFile(struct inode *inode, FILE_ID_T *fid, void *buffer, UINT64 count, UINT64 *rcount)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of the given file id */
if (fid == NULL) return(FFS_INVALIDFID);
/* check the validity of pointer parameters */
if (buffer == NULL) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsReadFile(inode, fid, buffer, count, rcount);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsReadFile */
INT32 FsWriteFile(struct inode *inode, FILE_ID_T *fid, void *buffer, UINT64 count, UINT64 *wcount)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of the given file id */
if (fid == NULL) return(FFS_INVALIDFID);
/* check the validity of pointer parameters */
if (buffer == NULL) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsWriteFile(inode, fid, buffer, count, wcount);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsWriteFile */
/* FsTruncateFile : resize the file length */
INT32 FsTruncateFile(struct inode *inode, UINT64 new_size)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
PRINTK("FsTruncateFile entered (inode %p size %llu\n", inode, new_size);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsTruncateFile(inode, new_size);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
PRINTK("FsTruncateFile exitted (%d)\n", err);
return(err);
} /* end of FsTruncateFile */
/* FsMoveFile : move(rename) a old file into a new file */
INT32 FsMoveFile(struct inode *old_parent_inode, FILE_ID_T *fid, struct inode *new_parent_inode, struct dentry *new_dentry)
{
INT32 err;
struct super_block *sb = old_parent_inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of the given file id */
if (fid == NULL) return(FFS_INVALIDFID);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsMoveFile(old_parent_inode, fid, new_parent_inode, new_dentry);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsMoveFile */
/* FsRemoveFile : remove a file */
INT32 FsRemoveFile(struct inode *inode, FILE_ID_T *fid)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of the given file id */
if (fid == NULL) return(FFS_INVALIDFID);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsRemoveFile(inode, fid);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsRemoveFile */
/* FsSetAttr : set the attribute of a given file */
INT32 FsSetAttr(struct inode *inode, UINT32 attr)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsSetAttr(inode, attr);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsSetAttr */
/* FsReadStat : get the information of a given file */
INT32 FsReadStat(struct inode *inode, DIR_ENTRY_T *info)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsGetStat(inode, info);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsReadStat */
/* FsWriteStat : set the information of a given file */
INT32 FsWriteStat(struct inode *inode, DIR_ENTRY_T *info)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
PRINTK("FsWriteStat entered (inode %p info %p\n", inode, info);
err = ffsSetStat(inode, info);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
PRINTK("FsWriteStat exited (%d)\n", err);
return(err);
} /* end of FsWriteStat */
/* FsMapCluster : return the cluster number in the given cluster offset */
INT32 FsMapCluster(struct inode *inode, INT32 clu_offset, UINT32 *clu)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if (clu == NULL) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsMapCluster(inode, clu_offset, clu);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsMapCluster */
/*----------------------------------------------------------------------*/
/* Directory Operation Functions */
/*----------------------------------------------------------------------*/
/* FsCreateDir : create(make) a directory */
INT32 FsCreateDir(struct inode *inode, UINT8 *path, FILE_ID_T *fid)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if ((fid == NULL) || (path == NULL) || (STRLEN(path) == 0))
return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsCreateDir(inode, path, fid);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsCreateDir */
/* FsReadDir : read a directory entry from the opened directory */
INT32 FsReadDir(struct inode *inode, DIR_ENTRY_T *dir_entry)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of pointer parameters */
if (dir_entry == NULL) return(FFS_ERROR);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsReadDir(inode, dir_entry);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsReadDir */
/* FsRemoveDir : remove a directory */
INT32 FsRemoveDir(struct inode *inode, FILE_ID_T *fid)
{
INT32 err;
struct super_block *sb = inode->i_sb;
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* check the validity of the given file id */
if (fid == NULL) return(FFS_INVALIDFID);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
err = ffsRemoveDir(inode, fid);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return(err);
} /* end of FsRemoveDir */
EXPORT_SYMBOL(FsMountVol);
EXPORT_SYMBOL(FsUmountVol);
EXPORT_SYMBOL(FsGetVolInfo);
EXPORT_SYMBOL(FsSyncVol);
EXPORT_SYMBOL(FsLookupFile);
EXPORT_SYMBOL(FsCreateFile);
EXPORT_SYMBOL(FsReadFile);
EXPORT_SYMBOL(FsWriteFile);
EXPORT_SYMBOL(FsTruncateFile);
EXPORT_SYMBOL(FsMoveFile);
EXPORT_SYMBOL(FsRemoveFile);
EXPORT_SYMBOL(FsSetAttr);
EXPORT_SYMBOL(FsReadStat);
EXPORT_SYMBOL(FsWriteStat);
EXPORT_SYMBOL(FsMapCluster);
EXPORT_SYMBOL(FsCreateDir);
EXPORT_SYMBOL(FsReadDir);
EXPORT_SYMBOL(FsRemoveDir);
#if EXFAT_CONFIG_KERNEL_DEBUG
/* FsReleaseCache: Release FAT & buf cache */
INT32 FsReleaseCache(struct super_block *sb)
{
FS_INFO_T *p_fs = &(EXFAT_SB(sb)->fs_info);
/* acquire the lock for file system critical section */
sm_P(&(fs_struct[p_fs->drv].v_sem));
FAT_release_all(sb);
buf_release_all(sb);
/* release the lock for file system critical section */
sm_V(&(fs_struct[p_fs->drv].v_sem));
return 0;
}
/* FsReleaseCache */
EXPORT_SYMBOL(FsReleaseCache);
#endif /* EXFAT_CONFIG_KERNEL_DEBUG */
static int __init init_exfat_core(void)
{
int err;
printk(KERN_INFO "exFAT: Core Version %s\n", EXFAT_VERSION);
err = FsInit();
if (err) {
if (err == FFS_MEMORYERR)
return -ENOMEM;
else
return -EIO;
}
return 0;
}
static void __exit exit_exfat_core(void)
{
FsShutdown();
}
module_init(init_exfat_core);
module_exit(exit_exfat_core);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("exFAT Filesystem Core");
/*======================================================================*/
/* Local Function Definitions */
/*======================================================================*/
/* end of exfat_api.c */