rawfs.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. #ifndef __RAWFS_H__
  2. #define __RAWFS_H__
  3. #include <asm/byteorder.h>
  4. #include <linux/list.h>
  5. #if defined(CONFIG_MT_ENG_BUILD) /* log is only enabled in eng load */
  6. #define RAWFS_DBG pr_debug
  7. #endif
  8. #define RAWFS_BLOCK_FILE
  9. /* #define RAWFS_RAM_DISK */
  10. #define RAWFS_VERSION 0x01
  11. /* Debug Message Mask */
  12. enum rawfs_debug_level_enum {
  13. RAWFS_DBG_SUPER = 0x0001,
  14. RAWFS_DBG_DEVICE = 0x0002,
  15. RAWFS_DBG_INODE = 0x0004,
  16. RAWFS_DBG_FILE = 0x0008,
  17. RAWFS_DBG_DIR = 0x0010,
  18. RAWFS_DBG_DENTRY = 0x0020,
  19. RAWFS_DBG_INIT = 0x0040,
  20. RAWFS_DBG_GC = 0x0080,
  21. RAWFS_DBG_MOUNT = 0x0100
  22. };
  23. extern int rawfs_debug_msg_mask;
  24. #define RAWFS_DEBUG_MSG_DEFAULT (RAWFS_DBG_SUPER | \
  25. RAWFS_DBG_DIR | RAWFS_DBG_DENTRY | \
  26. RAWFS_DBG_INIT | RAWFS_DBG_MOUNT)
  27. #ifdef RAWFS_DBG
  28. #define RAWFS_PRINT(category, str, ...) do { \
  29. if (category & rawfs_debug_msg_mask) { \
  30. RAWFS_DBG("rawfs: " str, ##__VA_ARGS__); \
  31. } } while (0)
  32. #else
  33. #define RAWFS_PRINT(...)
  34. #endif
  35. #define RAWFS_MAX_FILENAME_LEN 60
  36. #define RAWFS_MNT_RAM 0x01
  37. #define RAWFS_MNT_MTD 0x02
  38. #define RAWFS_MNT_CASE 0x10
  39. #define RAWFS_MNT_BLOCKFILE 0x40
  40. #define RAWFS_MNT_FIRSTBOOT 0x80
  41. /* RAW FS super block info */
  42. #define RAWFS_HASH_BITS 2
  43. #define RAWFS_HASH_SIZE (1UL << RAWFS_HASH_BITS)
  44. /* Interface to MTD block device */
  45. struct rawfs_dev {
  46. int (*erase_block)(struct super_block *sb, int block_no);
  47. int (*read_page_user)(struct super_block *sb, int block_no, int addr,
  48. const struct iovec *iov, unsigned long nr_segs, int size);
  49. int (*write_page)(struct super_block *sb, int block_no,
  50. int page_no, void *buffer);
  51. int (*read_page)(struct super_block *sb, int block_no,
  52. int page_no, void *buffer);
  53. };
  54. /* RAW FS super block info */
  55. struct rawfs_sb_info {
  56. /* File System Context */
  57. struct list_head fs_context;
  58. struct super_block *super;
  59. struct proc_dir_entry *s_proc;
  60. /* Driver context */
  61. void *driver_context;
  62. struct rawfs_dev dev;
  63. /* Device Info */
  64. int total_blocks;
  65. int pages_per_block;
  66. int sectors_per_page;
  67. int block_size;
  68. int page_size;
  69. int page_data_size;
  70. /* Management */
  71. int data_block;
  72. int data_block_free_page_index;
  73. int data_block_gcmarker_page_index;
  74. int empty_block;
  75. __u32 sequence_number;
  76. __u32 erase_count_max;
  77. int flags;
  78. char *fake_block;
  79. struct mutex rawfs_lock;
  80. struct nls_table *local_nls; /* Codepage used on disk */
  81. /* File List */
  82. struct list_head folder_list;
  83. struct list_head file_list;
  84. struct mutex file_list_lock;
  85. /* Inode Hash Table */
  86. spinlock_t inode_hash_lock;
  87. struct hlist_head inode_hashtable[RAWFS_HASH_SIZE];
  88. };
  89. #define RAWFS_NAND_BLOCKS(sb) ((sb)?2:0) /* We use only two block */
  90. #define RAWFS_NAND_PAGES(sb) (sb->pages_per_block)
  91. #define RAWFS_NAND_PAGE_SIZE(sb) (sb->page_size)
  92. #define RAWFS_NAND_BLOCK_SIZE(sb) (sb->block_size)
  93. #define RAWFS_NAND_PAGE_SECTORS(sb) (sb->sectors_per_page)
  94. #define RAWFS_NAND_PAGE_DATA_SIZE(sb) (sb->page_data_size)
  95. /* RAW FS inode info */
  96. struct rawfs_inode_info {
  97. spinlock_t cache_lru_lock;
  98. struct list_head cache_lru;
  99. int nr_caches;
  100. unsigned int cache_valid_id;
  101. /* NOTE: mmu_private is 64bits, so must hold ->i_mutex to access */
  102. loff_t mmu_private; /* physically allocated size */
  103. int i_location_block; /* File Location: block */
  104. int i_location_page; /* File Location: starting page */
  105. int i_location_page_count;
  106. int i_id;
  107. int i_parent_folder_id; /* Parent folder ID */
  108. char i_name[RAWFS_MAX_FILENAME_LEN+4];
  109. struct hlist_node i_rawfs_hash; /* hash by i_name */
  110. struct rw_semaphore truncate_lock; /* protect bmap against truncate */
  111. struct inode vfs_inode;
  112. };
  113. static inline struct rawfs_sb_info *
  114. RAWFS_SB(struct super_block *sb)
  115. {
  116. return sb->s_fs_info;
  117. }
  118. static inline struct rawfs_inode_info *
  119. RAWFS_I(struct inode *inode)
  120. {
  121. return container_of(inode, struct rawfs_inode_info, vfs_inode);
  122. }
  123. static inline struct mtd_info *
  124. RAWFS_MTD(struct super_block *sb)
  125. {
  126. return RAWFS_SB(sb)->driver_context;
  127. }
  128. #define RAWFS_CACHE_VALID 0
  129. #define RAWFS_ROOT_DIR_ID 1
  130. /* RAWFS inode number */
  131. #define RAWFS_ROOT_INO 1
  132. #define RAWFS_BLOCK0_INO 2
  133. #define RAWFS_BLOCK1_INO 3
  134. #define RAWFS_MAX_RESERVED_INO 64
  135. /* Page Signatures */
  136. #define RAWFS_NAND_BLOCK_SIG_HEAD 0x44484B42 /* BKHD */
  137. #define RAWFS_NAND_PAGE_SIG_HEAD 0x44484750 /* PGHD */
  138. #define RAWFS_NAND_PAGE_SIG_FOOT 0x54464750 /* PGFT */
  139. #define RAWFS_NAND_GC_MARKER_SIG_HEAD 0x44484347 /* GCHD */
  140. #define RAWFS_NAND_PAGE_SIG_EMPTY 0xFFFFFFFF
  141. enum rawfs_block_stat_enum {
  142. RAWFS_BLOCK_STAT_INVALID_HEAD = 0,
  143. RAWFS_BLOCK_STAT_EMPTY = 1,
  144. RAWFS_BLOCK_STAT_INVALID_DATA = 2,
  145. RAWFS_BLOCK_STAT_DATA = 3
  146. };
  147. enum rawfs_page_stat_enum {
  148. RAWFS_PAGE_STAT_EMPTY = 0,
  149. RAWFS_PAGE_STAT_DELETED = 1,
  150. RAWFS_PAGE_STAT_VALID = 2,
  151. RAWFS_PAGE_STAT_BLOCK_HEAD = 3,
  152. RAWFS_PAGE_STAT_GC_MARKER = 4,
  153. RAWFS_PAGE_STAT_UNCORRECTABLE = 5,
  154. RAWFS_PAGE_STAT_INVALID = 6
  155. };
  156. struct rawfs_block_header {
  157. __u32 i_signature_head;
  158. __u32 i_rawfs_version;
  159. __u32 i_sequence_number;
  160. __u32 i_sequence_number_last;
  161. __u32 i_erase_count;
  162. __u32 i_crc;
  163. } __packed;
  164. struct rawfs_gc_marker_page {
  165. __u32 i_signature_head;
  166. __u32 i_src_block_index;
  167. __u32 i_src_block_sequence_number;
  168. __u32 i_src_block_erase_count;
  169. __u32 i_crc;
  170. } __packed;
  171. struct rawfs_file_info { /* dentry */
  172. char i_name[RAWFS_MAX_FILENAME_LEN+4];
  173. int i_chunk_index;
  174. int i_chunk_total;
  175. struct timespec i_atime;
  176. struct timespec i_mtime;
  177. struct timespec i_ctime;
  178. umode_t i_mode;
  179. uid_t i_uid;
  180. gid_t i_gid;
  181. loff_t i_size;
  182. int i_parent_folder_id;
  183. int i_id; /* 0 for normal file */
  184. };
  185. struct rawfs_file_list_entry {
  186. struct list_head list;
  187. struct rawfs_file_info file_info;
  188. int i_location_block; /* File Location: block */
  189. int i_location_page; /* File Location: starting page */
  190. int i_location_page_count;
  191. };
  192. struct rawfs_page {
  193. __u32 i_signature_head;
  194. __u32 i_crc;
  195. union {
  196. struct rawfs_file_info i_file_info;
  197. __u8 padding[488];
  198. } i_info;
  199. __u8 i_data[1];
  200. } __packed;
  201. /* Inode operations */
  202. int __init rawfs_init_inodecache(void);
  203. void __exit rawfs_destroy_inodecache(void);
  204. void rawfs_hash_init(struct super_block *sb);
  205. struct inode *rawfs_alloc_inode(struct super_block *sb);
  206. void rawfs_destroy_inode(struct inode *inode);
  207. int rawfs_fill_inode(struct inode *inode,
  208. struct rawfs_file_info *file_info, int block_no, int page_no,
  209. umode_t mode, dev_t dev);
  210. struct inode *rawfs_iget(struct super_block *sb, const char *name,
  211. int folder);
  212. /* Mount-time analysis */
  213. int rawfs_block_level_analysis(struct super_block *sb);
  214. int rawfs_page_level_analysis(struct super_block *sb);
  215. int rawfs_file_level_analysis(struct super_block *sb);
  216. int rawfs_page_get(struct super_block *sb, int block_no, int page_no,
  217. struct rawfs_file_info *file_info, void *data);
  218. int rawfs_block_is_valid(struct super_block *sb, int block_no,
  219. struct rawfs_block_header *block_head_out,
  220. struct rawfs_gc_marker_page *gc_page_out);
  221. /* Device Operation */
  222. int rawfs_dev_free_space(struct super_block *sb);
  223. int rawfs_dev_garbage_collection(struct super_block *sb);
  224. void rawfs_page_signature(struct super_block *sb, void *buf);
  225. int rawfs_dev_mtd_erase_block(struct super_block *sb, int block_no);
  226. int rawfs_dev_mtd_read_page_user(struct super_block *sb, int block_no,
  227. int block_offset, const struct iovec *iov, unsigned long nr_segs, int size);
  228. int rawfs_dev_mtd_write_page(struct super_block *sb,
  229. int block_no, int page_no, void *buffer);
  230. int rawfs_dev_mtd_read_page(struct super_block *sb,
  231. int block_no, int page_no, void *buffer);
  232. int rawfs_dev_ram_erase_block(struct super_block *sb, int block_no);
  233. int rawfs_dev_ram_read_page_user(struct super_block *sb, int block_no,
  234. int block_offset, const struct iovec *iov, unsigned long nr_segs, int size);
  235. int rawfs_dev_ram_write_page(struct super_block *sb,
  236. int block_no, int page_no, void *buffer);
  237. int rawfs_dev_ram_read_page(struct super_block *sb,
  238. int block_no, int page_no, void *buffer);
  239. /* File Operations */
  240. int rawfs_reg_file_delete(struct inode *dir, struct dentry *dentry);
  241. int rawfs_reg_file_create(struct inode *dir, struct dentry *dentry,
  242. umode_t mode, struct nameidata *nd);
  243. int rawfs_reg_file_copy(struct inode *src_dir, struct dentry *src_dentry,
  244. struct inode *dest_dir, struct dentry *dest_dentry);
  245. int rawfs_reserve_space(struct super_block *sb, int chunks);
  246. ssize_t
  247. rawfs_reg_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  248. unsigned long nr_segs, loff_t pos);
  249. ssize_t
  250. rawfs_reg_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  251. unsigned long nr_segs, loff_t pos);
  252. ssize_t
  253. rawfs_block_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
  254. unsigned long nr_segs, loff_t pos);
  255. ssize_t rawfs_block_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
  256. unsigned long nr_segs, loff_t pos);
  257. int rawfs_file_sync(struct file *file, loff_t start, loff_t end,
  258. int datasync);
  259. int rawfs_readdir(struct file *filp, struct dir_context *ctx);
  260. /* dentry operations */
  261. int rawfs_delete_dentry(const struct dentry *dentry);
  262. /* File info */
  263. void rawfs_fill_file_info(struct inode *inode,
  264. struct rawfs_file_info *file_info);
  265. void rawfs_fill_fileinfo_by_dentry(struct dentry *dentry,
  266. struct rawfs_file_info *file_info);
  267. /* File and Folder lists */
  268. struct rawfs_file_list_entry *rawfs_file_list_get(struct super_block *sb,
  269. const char *name, int folder_id);
  270. struct rawfs_file_list_entry *rawfs_file_list_get_by_id(
  271. struct super_block *sb, umode_t mode, int id);
  272. void rawfs_file_list_init(struct super_block *sb);
  273. int rawfs_file_list_add(struct super_block *sb,
  274. struct rawfs_file_info *fi, int block_no, int page_no);
  275. void rawfs_file_list_remove(struct super_block *sb,
  276. struct rawfs_file_info *fi);
  277. void rawfs_file_list_destroy(struct super_block *sb);
  278. int rawfs_file_list_count(struct super_block *sb,
  279. unsigned int *entry_count, unsigned int *used_blocks,
  280. unsigned int *free_blocks);
  281. __u32 rawfs_page_crc_data(struct super_block *sb, void *data_page);
  282. __u32 rawfs_page_crc_gcmarker(struct super_block *sb, void *gcmarker_page);
  283. /* Address Space Operations: Block file & Normal file */
  284. int rawfs_readpage(struct file *filp, struct page *page);
  285. int rawfs_write_begin(struct file *filp, struct address_space *mapping,
  286. loff_t pos, unsigned len, unsigned flags,
  287. struct page **pagep, void **fsdata);
  288. int rawfs_write_end(struct file *filp, struct address_space *mapping,
  289. loff_t pos, unsigned len, unsigned copied,
  290. struct page *pg, void *fsdata);
  291. /* Case in-sensitive dentry operations */
  292. int rawfs_ci_hash(const struct dentry *dentry, struct qstr *q);
  293. int rawfs_compare_dentry(const struct dentry *parent,
  294. const struct dentry *dentry, unsigned int len, const char *str,
  295. const struct qstr *name);
  296. int rawfs_delete_dentry(const struct dentry *dentry);
  297. /* Utility */
  298. uint32_t rawfs_div(uint64_t n, uint32_t base);
  299. #endif