-
Notifications
You must be signed in to change notification settings - Fork 20
/
api.c
636 lines (520 loc) · 17.5 KB
/
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
/*
* Copyright (C) 2012-2013 Samsung Electronics Co., Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/************************************************************************/
/* */
/* PROJECT : exFAT & FAT12/16/32 File System */
/* FILE : sdfat_api.c */
/* PURPOSE : sdFAT volume lock layer */
/* */
/************************************************************************/
#include <linux/version.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/mutex.h>
#include "version.h"
#include "config.h"
#include "sdfat.h"
#include "core.h"
/*----------------------------------------------------------------------*/
/* Internal structures */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Constant & Macro Definitions */
/*----------------------------------------------------------------------*/
static DEFINE_MUTEX(_lock_core);
/*----------------------------------------------------------------------*/
/* Global Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Variable Definitions */
/*----------------------------------------------------------------------*/
/*----------------------------------------------------------------------*/
/* Local Function Declarations */
/*----------------------------------------------------------------------*/
/*======================================================================*/
/* Global Function Definitions */
/* - All functions for global use have same return value format, */
/* that is, 0 on success and minus error number on */
/* various error condition. */
/*======================================================================*/
/*----------------------------------------------------------------------*/
/* sdFAT Filesystem Init & Exit Functions */
/*----------------------------------------------------------------------*/
s32 fsapi_init(void)
{
return fscore_init();
}
s32 fsapi_shutdown(void)
{
return fscore_shutdown();
}
/*----------------------------------------------------------------------*/
/* Volume Management Functions */
/*----------------------------------------------------------------------*/
/* mount the file system volume */
s32 fsapi_mount(struct super_block *sb)
{
s32 err;
/* acquire the core lock for file system ccritical section */
mutex_lock(&_lock_core);
err = meta_cache_init(sb);
if (err)
goto out;
err = fscore_mount(sb);
out:
if (err)
meta_cache_shutdown(sb);
/* release the core lock for file system critical section */
mutex_unlock(&_lock_core);
return err;
}
EXPORT_SYMBOL(fsapi_mount);
/* unmount the file system volume */
s32 fsapi_umount(struct super_block *sb)
{
s32 err;
/* acquire the core lock for file system ccritical section */
mutex_lock(&_lock_core);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_umount(sb);
meta_cache_shutdown(sb);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
/* release the core lock for file system critical section */
mutex_unlock(&_lock_core);
return err;
}
EXPORT_SYMBOL(fsapi_umount);
/* get the information of a file system volume */
s32 fsapi_statfs(struct super_block *sb, VOL_INFO_T *info)
{
FS_INFO_T *fsi = &(SDFAT_SB(sb)->fsi);
/* check the validity of pointer parameters */
ASSERT(info);
if (fsi->used_clusters == (u32) ~0) {
s32 err;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_statfs(sb, info);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
info->FatType = fsi->vol_type;
info->ClusterSize = fsi->cluster_size;
info->NumClusters = fsi->num_clusters - 2; /* clu 0 & 1 */
info->UsedClusters = fsi->used_clusters + fsi->reserved_clusters;
info->FreeClusters = info->NumClusters - info->UsedClusters;
return 0;
}
EXPORT_SYMBOL(fsapi_statfs);
/* synchronize a file system volume */
s32 fsapi_sync_fs(struct super_block *sb, s32 do_sync)
{
s32 err;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_sync_fs(sb, do_sync);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_sync_fs);
s32 fsapi_set_vol_flags(struct super_block *sb, u16 new_flag, s32 always_sync)
{
s32 err;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_set_vol_flags(sb, new_flag, always_sync);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_set_vol_flags);
/*----------------------------------------------------------------------*/
/* File Operation Functions */
/*----------------------------------------------------------------------*/
/* lookup */
s32 fsapi_lookup(struct inode *inode, u8 *path, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && path);
if (unlikely(!strlen(path)))
return -EINVAL;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_lookup(inode, path, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_lookup);
/* create a file */
s32 fsapi_create(struct inode *inode, u8 *path, u8 mode, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && path);
if (unlikely(!strlen(path)))
return -EINVAL;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_create(inode, path, mode, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_create);
/* read the target string of symlink */
s32 fsapi_read_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *rcount)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && buffer);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_read_link(inode, fid, buffer, count, rcount);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_read_link);
/* write the target string of symlink */
s32 fsapi_write_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *wcount)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && buffer);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_write_link(inode, fid, buffer, count, wcount);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_write_link);
/* resize the file length */
s32 fsapi_truncate(struct inode *inode, u64 old_size, u64 new_size)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
TMSG("%s entered (inode %p size %llu)\n", __func__, inode, new_size);
err = fscore_truncate(inode, old_size, new_size);
TMSG("%s exitted (%d)\n", __func__, err);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_truncate);
/* rename or move a old file into a new file */
s32 fsapi_rename(struct inode *old_parent_inode, FILE_ID_T *fid,
struct inode *new_parent_inode, struct dentry *new_dentry)
{
s32 err;
struct super_block *sb = old_parent_inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_rename(old_parent_inode, fid, new_parent_inode, new_dentry);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_rename);
/* remove a file */
s32 fsapi_remove(struct inode *inode, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_remove(inode, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_remove);
/* get the information of a given file */
s32 fsapi_read_inode(struct inode *inode, DIR_ENTRY_T *info)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
TMSG("%s entered (inode %p info %p\n", __func__, inode, info);
err = fscore_read_inode(inode, info);
TMSG("%s exited (err:%d)\n", __func__, err);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_read_inode);
/* set the information of a given file */
s32 fsapi_write_inode(struct inode *inode, DIR_ENTRY_T *info, int sync)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
TMSG("%s entered (inode %p info %p sync:%d\n",
__func__, inode, info, sync);
err = fscore_write_inode(inode, info, sync);
TMSG("%s exited (err:%d)\n", __func__, err);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_write_inode);
/* return the cluster number in the given cluster offset */
s32 fsapi_map_clus(struct inode *inode, u32 clu_offset, u32 *clu, int dest)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(clu);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
TMSG("%s entered (inode:%p clus:%08x dest:%d\n",
__func__, inode, *clu, dest);
err = fscore_map_clus(inode, clu_offset, clu, dest);
TMSG("%s exited (clu:%08x err:%d)\n", __func__, *clu, err);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_map_clus);
/* reserve a cluster */
s32 fsapi_reserve_clus(struct inode *inode)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
TMSG("%s entered (inode:%p)\n", __func__, inode);
err = fscore_reserve_clus(inode);
TMSG("%s exited (err:%d)\n", __func__, err);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_reserve_clus);
/*----------------------------------------------------------------------*/
/* Directory Operation Functions */
/*----------------------------------------------------------------------*/
/* create(make) a directory */
s32 fsapi_mkdir(struct inode *inode, u8 *path, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid && path);
if (unlikely(!strlen(path)))
return -EINVAL;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_mkdir(inode, path, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_mkdir);
/* read a directory entry from the opened directory */
s32 fsapi_readdir(struct inode *inode, DIR_ENTRY_T *dir_entry)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(dir_entry);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_readdir(inode, dir_entry);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_readdir);
/* remove a directory */
s32 fsapi_rmdir(struct inode *inode, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_rmdir(inode, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_rmdir);
/* unlink a file.
* that is, remove an entry from a directory. BUT don't truncate
*/
s32 fsapi_unlink(struct inode *inode, FILE_ID_T *fid)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(fid);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = fscore_unlink(inode, fid);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_unlink);
/* reflect the internal dirty flags to VFS bh dirty flags */
s32 fsapi_cache_flush(struct super_block *sb, int do_sync)
{
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
fcache_flush(sb, do_sync);
dcache_flush(sb, do_sync);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return 0;
}
EXPORT_SYMBOL(fsapi_cache_flush);
/* release FAT & buf cache */
s32 fsapi_cache_release(struct super_block *sb)
{
#ifdef CONFIG_SDFAT_DEBUG
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
fcache_release_all(sb);
dcache_release_all(sb);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
#endif /* CONFIG_SDFAT_DEBUG */
return 0;
}
EXPORT_SYMBOL(fsapi_cache_release);
u32 fsapi_get_au_stat(struct super_block *sb, s32 mode)
{
/* volume lock is not required */
return fscore_get_au_stat(sb, mode);
}
EXPORT_SYMBOL(fsapi_get_au_stat);
/* clear extent cache */
void fsapi_invalidate_extent(struct inode *inode)
{
/* Volume lock is not required,
* because it is only called by evict_inode.
* If any other function can call it,
* you should check whether volume lock is needed or not.
*/
extent_cache_inval_inode(inode);
}
EXPORT_SYMBOL(fsapi_invalidate_extent);
/* check device is ejected */
s32 fsapi_check_bdi_valid(struct super_block *sb)
{
return fscore_check_bdi_valid(sb);
}
EXPORT_SYMBOL(fsapi_check_bdi_valid);
#ifdef CONFIG_SDFAT_DFR
/*----------------------------------------------------------------------*/
/* Defragmentation related */
/*----------------------------------------------------------------------*/
s32 fsapi_dfr_get_info(struct super_block *sb, void *arg)
{
/* volume lock is not required */
return defrag_get_info(sb, (struct defrag_info_arg *)arg);
}
EXPORT_SYMBOL(fsapi_dfr_get_info);
s32 fsapi_dfr_scan_dir(struct super_block *sb, void *args)
{
s32 err;
/* check the validity of pointer parameters */
ASSERT(args);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = defrag_scan_dir(sb, (struct defrag_trav_arg *)args);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_dfr_scan_dir);
s32 fsapi_dfr_validate_clus(struct inode *inode, void *chunk, int skip_prev)
{
s32 err;
struct super_block *sb = inode->i_sb;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = defrag_validate_cluster(inode,
(struct defrag_chunk_info *)chunk, skip_prev);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_dfr_validate_clus);
s32 fsapi_dfr_reserve_clus(struct super_block *sb, s32 nr_clus)
{
s32 err;
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = defrag_reserve_clusters(sb, nr_clus);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_dfr_reserve_clus);
s32 fsapi_dfr_mark_ignore(struct super_block *sb, unsigned int clus)
{
/* volume lock is not required */
return defrag_mark_ignore(sb, clus);
}
EXPORT_SYMBOL(fsapi_dfr_mark_ignore);
void fsapi_dfr_unmark_ignore_all(struct super_block *sb)
{
/* volume lock is not required */
defrag_unmark_ignore_all(sb);
}
EXPORT_SYMBOL(fsapi_dfr_unmark_ignore_all);
s32 fsapi_dfr_map_clus(struct inode *inode, u32 clu_offset, u32 *clu)
{
s32 err;
struct super_block *sb = inode->i_sb;
/* check the validity of pointer parameters */
ASSERT(clu);
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
err = defrag_map_cluster(inode, clu_offset, clu);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
return err;
}
EXPORT_SYMBOL(fsapi_dfr_map_clus);
void fsapi_dfr_writepage_endio(struct page *page)
{
/* volume lock is not required */
defrag_writepage_end_io(page);
}
EXPORT_SYMBOL(fsapi_dfr_writepage_endio);
void fsapi_dfr_update_fat_prev(struct super_block *sb, int force)
{
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
defrag_update_fat_prev(sb, force);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
}
EXPORT_SYMBOL(fsapi_dfr_update_fat_prev);
void fsapi_dfr_update_fat_next(struct super_block *sb)
{
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
defrag_update_fat_next(sb);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
}
EXPORT_SYMBOL(fsapi_dfr_update_fat_next);
void fsapi_dfr_check_discard(struct super_block *sb)
{
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
defrag_check_discard(sb);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
}
EXPORT_SYMBOL(fsapi_dfr_check_discard);
void fsapi_dfr_free_clus(struct super_block *sb, u32 clus)
{
mutex_lock(&(SDFAT_SB(sb)->s_vlock));
defrag_free_cluster(sb, clus);
mutex_unlock(&(SDFAT_SB(sb)->s_vlock));
}
EXPORT_SYMBOL(fsapi_dfr_free_clus);
s32 fsapi_dfr_check_dfr_required(struct super_block *sb, int *totalau, int *cleanau, int *fullau)
{
/* volume lock is not required */
return defrag_check_defrag_required(sb, totalau, cleanau, fullau);
}
EXPORT_SYMBOL(fsapi_dfr_check_dfr_required);
s32 fsapi_dfr_check_dfr_on(struct inode *inode, loff_t start, loff_t end, s32 cancel, const char *caller)
{
/* volume lock is not required */
return defrag_check_defrag_on(inode, start, end, cancel, caller);
}
EXPORT_SYMBOL(fsapi_dfr_check_dfr_on);
#ifdef CONFIG_SDFAT_DFR_DEBUG
void fsapi_dfr_spo_test(struct super_block *sb, int flag, const char *caller)
{
/* volume lock is not required */
defrag_spo_test(sb, flag, caller);
}
EXPORT_SYMBOL(fsapi_dfr_spo_test);
#endif
#endif /* CONFIG_SDFAT_DFR */
/* end of sdfat_api.c */