api.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. #ifndef _SDFAT_API_H
  18. #define _SDFAT_API_H
  19. #include "config.h"
  20. #include "sdfat_fs.h"
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif /* __cplusplus */
  24. /*----------------------------------------------------------------------*/
  25. /* Configure Constant & Macro Definitions */
  26. /*----------------------------------------------------------------------*/
  27. /* cache size (in number of sectors) */
  28. /* (should be an exponential value of 2) */
  29. #define FAT_CACHE_SIZE 128
  30. #define FAT_CACHE_HASH_SIZE 64
  31. #define BUF_CACHE_SIZE 256
  32. #define BUF_CACHE_HASH_SIZE 64
  33. /* Read-ahead related */
  34. /* First config vars. should be pow of 2 */
  35. #define FCACHE_MAX_RA_SIZE (PAGE_SIZE)
  36. #define DCACHE_MAX_RA_SIZE (128*1024)
  37. /*----------------------------------------------------------------------*/
  38. /* Constant & Macro Definitions */
  39. /*----------------------------------------------------------------------*/
  40. /* type values */
  41. #define TYPE_UNUSED 0x0000
  42. #define TYPE_DELETED 0x0001
  43. #define TYPE_INVALID 0x0002
  44. #define TYPE_CRITICAL_PRI 0x0100
  45. #define TYPE_BITMAP 0x0101
  46. #define TYPE_UPCASE 0x0102
  47. #define TYPE_VOLUME 0x0103
  48. #define TYPE_DIR 0x0104
  49. #define TYPE_FILE 0x011F
  50. #define TYPE_SYMLINK 0x015F
  51. #define TYPE_CRITICAL_SEC 0x0200
  52. #define TYPE_STREAM 0x0201
  53. #define TYPE_EXTEND 0x0202
  54. #define TYPE_ACL 0x0203
  55. #define TYPE_BENIGN_PRI 0x0400
  56. #define TYPE_GUID 0x0401
  57. #define TYPE_PADDING 0x0402
  58. #define TYPE_ACLTAB 0x0403
  59. #define TYPE_BENIGN_SEC 0x0800
  60. #define TYPE_ALL 0x0FFF
  61. /* eio values */
  62. #define SDFAT_EIO_NONE (0x00000000)
  63. #define SDFAT_EIO_READ (0x00000001)
  64. #define SDFAT_EIO_WRITE (0x00000002)
  65. #define SDFAT_EIO_BDI (0x00000004)
  66. /* modes for volume allocation unit status */
  67. #define VOL_AU_STAT_TOTAL (0)
  68. #define VOL_AU_STAT_CLEAN (1)
  69. #define VOL_AU_STAT_FULL (2)
  70. /*----------------------------------------------------------------------*/
  71. /* NLS Type Definitions */
  72. /*----------------------------------------------------------------------*/
  73. /* DOS name structure */
  74. typedef struct {
  75. u8 name[DOS_NAME_LENGTH];
  76. u8 name_case;
  77. } DOS_NAME_T;
  78. /* unicode name structure */
  79. typedef struct {
  80. u16 name[MAX_NAME_LENGTH+3]; /* +3 for null and for converting */
  81. u16 name_hash;
  82. u8 name_len;
  83. } UNI_NAME_T;
  84. /*----------------------------------------------------------------------*/
  85. /* Type Definitions */
  86. /*----------------------------------------------------------------------*/
  87. /* should be merged it to DATE_TIME_T */
  88. typedef union {
  89. struct {
  90. u8 off : 7;
  91. u8 valid : 1;
  92. };
  93. u8 value;
  94. } TIMEZONE_T;
  95. typedef struct {
  96. u16 sec; /* 0 ~ 59 */
  97. u16 min; /* 0 ~ 59 */
  98. u16 hour; /* 0 ~ 23 */
  99. u16 day; /* 1 ~ 31 */
  100. u16 mon; /* 1 ~ 12 */
  101. u16 year; /* 0 ~ 127 (since 1980) */
  102. TIMEZONE_T tz;
  103. } TIMESTAMP_T;
  104. typedef struct {
  105. u16 Year;
  106. u16 Month;
  107. u16 Day;
  108. u16 Hour;
  109. u16 Minute;
  110. u16 Second;
  111. u16 MilliSecond;
  112. TIMEZONE_T Timezone;
  113. } DATE_TIME_T;
  114. typedef struct {
  115. u64 Offset; // start sector number of the partition
  116. u64 Size; // in sectors
  117. } PART_INFO_T;
  118. typedef struct {
  119. u32 SecSize; // sector size in bytes
  120. u64 DevSize; // block device size in sectors
  121. } DEV_INFO_T;
  122. typedef struct {
  123. u32 FatType;
  124. u32 ClusterSize;
  125. u32 NumClusters;
  126. u32 FreeClusters;
  127. u32 UsedClusters;
  128. } VOL_INFO_T;
  129. /* directory structure */
  130. typedef struct {
  131. u32 dir;
  132. u32 size;
  133. u8 flags;
  134. } CHAIN_T;
  135. /* hint structure */
  136. typedef struct {
  137. u32 clu;
  138. union {
  139. u32 off; // cluster offset
  140. s32 eidx; // entry index
  141. };
  142. } HINT_T;
  143. typedef struct {
  144. spinlock_t cache_lru_lock;
  145. struct list_head cache_lru;
  146. s32 nr_caches;
  147. u32 cache_valid_id; // for avoiding the race between alloc and free
  148. } EXTENT_T;
  149. /* first empty entry hint information */
  150. typedef struct {
  151. s32 eidx; // entry index of a directory
  152. s32 count; // count of continuous empty entry
  153. CHAIN_T cur; // the cluster that first empty slot exists in
  154. } HINT_FEMP_T;
  155. /* file id structure */
  156. typedef struct {
  157. CHAIN_T dir;
  158. s32 entry;
  159. u32 type;
  160. u32 attr;
  161. u32 start_clu;
  162. u64 size;
  163. u8 flags;
  164. u8 reserved[3]; // padding
  165. u32 version; // the copy of low 32bit of i_version to check the validation of hint_stat
  166. s64 rwoffset; // file offset or dentry index for readdir
  167. EXTENT_T extent; // extent cache for a file
  168. HINT_T hint_bmap; // hint for cluster last accessed
  169. HINT_T hint_stat; // hint for entry index we try to lookup next time
  170. HINT_FEMP_T hint_femp; // hint for first empty entry
  171. } FILE_ID_T;
  172. typedef struct {
  173. s8 *lfn;
  174. s8 *sfn;
  175. s32 lfnbuf_len; //usally MAX_UNINAME_BUF_SIZE
  176. s32 sfnbuf_len; //usally MAX_DOSNAME_BUF_SIZE, used only for vfat, not for exfat
  177. } DENTRY_NAMEBUF_T;
  178. typedef struct {
  179. u32 Attr;
  180. u64 Size;
  181. u32 NumSubdirs;
  182. DATE_TIME_T CreateTimestamp;
  183. DATE_TIME_T ModifyTimestamp;
  184. DATE_TIME_T AccessTimestamp;
  185. DENTRY_NAMEBUF_T NameBuf;
  186. } DIR_ENTRY_T;
  187. /* cache information */
  188. typedef struct __cache_entry {
  189. struct __cache_entry *next;
  190. struct __cache_entry *prev;
  191. struct {
  192. struct __cache_entry *next;
  193. struct __cache_entry *prev;
  194. } hash;
  195. u64 sec;
  196. u32 flag;
  197. struct buffer_head *bh;
  198. } cache_ent_t;
  199. /*----------------------------------------------------------------------*/
  200. /* Type Definitions : Wrapper & In-Core */
  201. /*----------------------------------------------------------------------*/
  202. typedef struct __FATENT_OPS_T {
  203. s32 (*ent_get)(struct super_block *sb, u32 loc, u32 *content);
  204. s32 (*ent_set)(struct super_block *sb, u32 loc, u32 content);
  205. } FATENT_OPS_T;
  206. typedef struct {
  207. s32 (*alloc_cluster)(struct super_block *, u32, CHAIN_T *, s32);
  208. s32 (*free_cluster)(struct super_block *, CHAIN_T *, s32);
  209. s32 (*count_used_clusters)(struct super_block *, u32 *);
  210. s32 (*init_dir_entry)(struct super_block *, CHAIN_T *, s32, u32, u32, u64);
  211. s32 (*init_ext_entry)(struct super_block *, CHAIN_T *, s32, s32, UNI_NAME_T *, DOS_NAME_T *);
  212. s32 (*find_dir_entry)(struct super_block *, FILE_ID_T *, CHAIN_T *, UNI_NAME_T *, s32, DOS_NAME_T *, u32);
  213. s32 (*delete_dir_entry)(struct super_block *, CHAIN_T *, s32, s32, s32);
  214. void (*get_uniname_from_ext_entry)(struct super_block *, CHAIN_T *, s32, u16 *);
  215. s32 (*count_ext_entries)(struct super_block *, CHAIN_T *, s32, DENTRY_T *);
  216. s32 (*calc_num_entries)(UNI_NAME_T *);
  217. s32 (*check_max_dentries)(FILE_ID_T *);
  218. u32 (*get_entry_type)(DENTRY_T *);
  219. void (*set_entry_type)(DENTRY_T *, u32);
  220. u32 (*get_entry_attr)(DENTRY_T *);
  221. void (*set_entry_attr)(DENTRY_T *, u32);
  222. u8 (*get_entry_flag)(DENTRY_T *);
  223. void (*set_entry_flag)(DENTRY_T *, u8);
  224. u32 (*get_entry_clu0)(DENTRY_T *);
  225. void (*set_entry_clu0)(DENTRY_T *, u32);
  226. u64 (*get_entry_size)(DENTRY_T *);
  227. void (*set_entry_size)(DENTRY_T *, u64);
  228. void (*get_entry_time)(DENTRY_T *, TIMESTAMP_T *, u8);
  229. void (*set_entry_time)(DENTRY_T *, TIMESTAMP_T *, u8);
  230. u32 (*get_au_stat)(struct super_block *, s32);
  231. } FS_FUNC_T;
  232. typedef struct __FS_INFO_T {
  233. s32 bd_opened; // opened or not
  234. u32 vol_type; // volume FAT type
  235. u32 vol_id; // volume serial number
  236. u64 num_sectors; // num of sectors in volume
  237. u32 num_clusters; // num of clusters in volume
  238. u32 cluster_size; // cluster size in bytes
  239. u32 cluster_size_bits;
  240. u32 sect_per_clus; // cluster size in sectors
  241. u32 sect_per_clus_bits;
  242. u64 FAT1_start_sector; // FAT1 start sector
  243. u64 FAT2_start_sector; // FAT2 start sector
  244. u64 root_start_sector; // root dir start sector
  245. u64 data_start_sector; // data area start sector
  246. u32 num_FAT_sectors; // num of FAT sectors
  247. u32 root_dir; // root dir cluster
  248. u32 dentries_in_root; // num of dentries in root dir
  249. u32 dentries_per_clu; // num of dentries per cluster
  250. u32 vol_flag; // volume dirty flag
  251. struct buffer_head *pbr_bh; // buffer_head of PBR sector
  252. u32 map_clu; // allocation bitmap start cluster
  253. u32 map_sectors; // num of allocation bitmap sectors
  254. struct buffer_head **vol_amap; // allocation bitmap
  255. u16 **vol_utbl; // upcase table
  256. u32 clu_srch_ptr; // cluster search pointer
  257. u32 used_clusters; // number of used clusters
  258. u32 prev_eio; // block device operation error flag
  259. FS_FUNC_T *fs_func;
  260. FATENT_OPS_T *fatent_ops;
  261. s32 reserved_clusters; // # of reserved clusters (DA)
  262. void *amap; // AU Allocation Map
  263. /* fat cache */
  264. struct {
  265. cache_ent_t pool[FAT_CACHE_SIZE];
  266. cache_ent_t lru_list;
  267. cache_ent_t hash_list[FAT_CACHE_HASH_SIZE];
  268. } fcache;
  269. /* meta cache */
  270. struct {
  271. cache_ent_t pool[BUF_CACHE_SIZE];
  272. cache_ent_t lru_list;
  273. cache_ent_t keep_list; // CACHEs in this list will not be kicked by normal lru operations
  274. cache_ent_t hash_list[BUF_CACHE_HASH_SIZE];
  275. } dcache;
  276. } FS_INFO_T;
  277. /*======================================================================*/
  278. /* */
  279. /* API FUNCTION DECLARATIONS */
  280. /* (CHANGE THIS PART IF REQUIRED) */
  281. /* */
  282. /*======================================================================*/
  283. /*----------------------------------------------------------------------*/
  284. /* External Function Declarations */
  285. /*----------------------------------------------------------------------*/
  286. /* file system initialization & shutdown functions */
  287. s32 fsapi_init(void);
  288. s32 fsapi_shutdown(void);
  289. /* volume management functions */
  290. s32 fsapi_mount(struct super_block *sb);
  291. s32 fsapi_umount(struct super_block *sb);
  292. s32 fsapi_statfs(struct super_block *sb, VOL_INFO_T *info);
  293. s32 fsapi_sync_fs(struct super_block *sb, s32 do_sync);
  294. s32 fsapi_set_vol_flags(struct super_block *sb, u16 new_flag, s32 always_sync);
  295. /* file management functions */
  296. s32 fsapi_lookup(struct inode *inode, u8 *path, FILE_ID_T *fid);
  297. s32 fsapi_create(struct inode *inode, u8 *path, u8 mode, FILE_ID_T *fid);
  298. s32 fsapi_read_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *rcount);
  299. s32 fsapi_write_link(struct inode *inode, FILE_ID_T *fid, void *buffer, u64 count, u64 *wcount);
  300. s32 fsapi_remove(struct inode *inode, FILE_ID_T *fid); /* unlink and truncate */
  301. s32 fsapi_truncate(struct inode *inode, u64 old_size, u64 new_size);
  302. s32 fsapi_rename(struct inode *old_parent_inode, FILE_ID_T *fid,
  303. struct inode *new_parent_inode, struct dentry *new_dentry);
  304. s32 fsapi_unlink(struct inode *inode, FILE_ID_T *fid);
  305. s32 fsapi_read_inode(struct inode *inode, DIR_ENTRY_T *info);
  306. s32 fsapi_write_inode(struct inode *inode, DIR_ENTRY_T *info, int sync);
  307. s32 fsapi_map_clus(struct inode *inode, u32 clu_offset, u32 *clu, int dest);
  308. s32 fsapi_reserve_clus(struct inode *inode);
  309. /* directory management functions */
  310. s32 fsapi_mkdir(struct inode *inode, u8 *path, FILE_ID_T *fid);
  311. s32 fsapi_readdir(struct inode *inode, DIR_ENTRY_T *dir_entry);
  312. s32 fsapi_rmdir(struct inode *inode, FILE_ID_T *fid);
  313. /* FAT & buf cache functions */
  314. s32 fsapi_cache_flush(struct super_block *sb, int do_sync);
  315. s32 fsapi_cache_release(struct super_block *sb);
  316. /* extra info functions */
  317. u32 fsapi_get_au_stat(struct super_block *sb, s32 mode);
  318. /* extent cache functions */
  319. void fsapi_invalidate_extent(struct inode *inode);
  320. /* bdev management */
  321. s32 fsapi_check_bdi_valid(struct super_block *sb);
  322. #ifdef CONFIG_SDFAT_DFR
  323. /*----------------------------------------------------------------------*/
  324. /* Defragmentation related */
  325. /*----------------------------------------------------------------------*/
  326. s32 fsapi_dfr_get_info(struct super_block *sb, void *arg);
  327. s32 fsapi_dfr_scan_dir(struct super_block *sb, void *args);
  328. s32 fsapi_dfr_validate_clus(struct inode *inode, void *chunk, int skip_prev);
  329. s32 fsapi_dfr_reserve_clus(struct super_block *sb, s32 nr_clus);
  330. s32 fsapi_dfr_mark_ignore(struct super_block *sb, unsigned int clus);
  331. void fsapi_dfr_unmark_ignore_all(struct super_block *sb);
  332. s32 fsapi_dfr_map_clus(struct inode *inode, u32 clu_offset, u32 *clu);
  333. void fsapi_dfr_writepage_endio(struct page *page);
  334. void fsapi_dfr_update_fat_prev(struct super_block *sb, int force);
  335. void fsapi_dfr_update_fat_next(struct super_block *sb);
  336. void fsapi_dfr_check_discard(struct super_block *sb);
  337. void fsapi_dfr_free_clus(struct super_block *sb, u32 clus);
  338. s32 fsapi_dfr_check_dfr_required(struct super_block *sb, int *totalau, int *cleanau, int *fullau);
  339. s32 fsapi_dfr_check_dfr_on(struct inode *inode, loff_t start, loff_t end, s32 cancel, const char *caller);
  340. #ifdef CONFIG_SDFAT_DFR_DEBUG
  341. void fsapi_dfr_spo_test(struct super_block *sb, int flag, const char *caller);
  342. #endif /* CONFIG_SDFAT_DFR_DEBUG */
  343. #endif /* CONFIG_SDFAT_DFR */
  344. #ifdef __cplusplus
  345. }
  346. #endif /* __cplusplus */
  347. #endif /* _SDFAT_API_H */
  348. /* end of api.h */