f2fs.h 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  1. /*
  2. * fs/f2fs/f2fs.h
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef _LINUX_F2FS_H
  12. #define _LINUX_F2FS_H
  13. #include <linux/types.h>
  14. #include <linux/page-flags.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/slab.h>
  17. #include <linux/crc32.h>
  18. #include <linux/magic.h>
  19. #include <linux/kobject.h>
  20. #include <linux/sched.h>
  21. #ifdef CONFIG_F2FS_CHECK_FS
  22. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  23. #define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  24. #else
  25. #define f2fs_bug_on(sbi, condition) \
  26. do { \
  27. if (unlikely(condition)) { \
  28. WARN_ON(1); \
  29. sbi->need_fsck = true; \
  30. } \
  31. } while (0)
  32. #define f2fs_down_write(x, y) down_write(x)
  33. #endif
  34. /*
  35. * For mount options
  36. */
  37. #define F2FS_MOUNT_BG_GC 0x00000001
  38. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  39. #define F2FS_MOUNT_DISCARD 0x00000004
  40. #define F2FS_MOUNT_NOHEAP 0x00000008
  41. #define F2FS_MOUNT_XATTR_USER 0x00000010
  42. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  43. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  44. #define F2FS_MOUNT_INLINE_XATTR 0x00000080
  45. #define F2FS_MOUNT_INLINE_DATA 0x00000100
  46. #define F2FS_MOUNT_FLUSH_MERGE 0x00000200
  47. #define F2FS_MOUNT_NOBARRIER 0x00000400
  48. #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
  49. #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
  50. #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
  51. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  52. typecheck(unsigned long long, b) && \
  53. ((long long)((a) - (b)) > 0))
  54. typedef u32 block_t; /*
  55. * should not change u32, since it is the on-disk block
  56. * address format, __le32.
  57. */
  58. typedef u32 nid_t;
  59. struct f2fs_mount_info {
  60. unsigned int opt;
  61. };
  62. #define CRCPOLY_LE 0xedb88320
  63. static inline __u32 f2fs_crc32(void *buf, size_t len)
  64. {
  65. unsigned char *p = (unsigned char *)buf;
  66. __u32 crc = F2FS_SUPER_MAGIC;
  67. int i;
  68. while (len--) {
  69. crc ^= *p++;
  70. for (i = 0; i < 8; i++)
  71. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  72. }
  73. return crc;
  74. }
  75. static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
  76. {
  77. return f2fs_crc32(buf, buf_size) == blk_crc;
  78. }
  79. /*
  80. * For checkpoint manager
  81. */
  82. enum {
  83. NAT_BITMAP,
  84. SIT_BITMAP
  85. };
  86. enum {
  87. CP_UMOUNT,
  88. CP_SYNC,
  89. CP_DISCARD,
  90. };
  91. struct cp_control {
  92. int reason;
  93. __u64 trim_start;
  94. __u64 trim_end;
  95. __u64 trim_minlen;
  96. __u64 trimmed;
  97. };
  98. /*
  99. * For CP/NAT/SIT/SSA readahead
  100. */
  101. enum {
  102. META_CP,
  103. META_NAT,
  104. META_SIT,
  105. META_SSA,
  106. META_POR,
  107. };
  108. /* for the list of ino */
  109. enum {
  110. ORPHAN_INO, /* for orphan ino list */
  111. APPEND_INO, /* for append ino list */
  112. UPDATE_INO, /* for update ino list */
  113. MAX_INO_ENTRY, /* max. list */
  114. };
  115. struct ino_entry {
  116. struct list_head list; /* list head */
  117. nid_t ino; /* inode number */
  118. };
  119. /* for the list of directory inodes */
  120. struct dir_inode_entry {
  121. struct list_head list; /* list head */
  122. struct inode *inode; /* vfs inode pointer */
  123. };
  124. /* for the list of blockaddresses to be discarded */
  125. struct discard_entry {
  126. struct list_head list; /* list head */
  127. block_t blkaddr; /* block address to be discarded */
  128. int len; /* # of consecutive blocks of the discard */
  129. };
  130. /* for the list of fsync inodes, used only during recovery */
  131. struct fsync_inode_entry {
  132. struct list_head list; /* list head */
  133. struct inode *inode; /* vfs inode pointer */
  134. block_t blkaddr; /* block address locating the last fsync */
  135. block_t last_dentry; /* block address locating the last dentry */
  136. block_t last_inode; /* block address locating the last inode */
  137. };
  138. #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
  139. #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
  140. #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
  141. #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
  142. #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
  143. #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
  144. #define MAX_NAT_JENTRIES(sum) (NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
  145. #define MAX_SIT_JENTRIES(sum) (SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
  146. static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
  147. {
  148. int before = nats_in_cursum(rs);
  149. rs->n_nats = cpu_to_le16(before + i);
  150. return before;
  151. }
  152. static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
  153. {
  154. int before = sits_in_cursum(rs);
  155. rs->n_sits = cpu_to_le16(before + i);
  156. return before;
  157. }
  158. static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
  159. int type)
  160. {
  161. if (type == NAT_JOURNAL)
  162. return size <= MAX_NAT_JENTRIES(sum);
  163. return size <= MAX_SIT_JENTRIES(sum);
  164. }
  165. /*
  166. * ioctl commands
  167. */
  168. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  169. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  170. #define F2FS_IOCTL_MAGIC 0xf5
  171. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  172. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  173. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  174. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  175. /*
  176. * ioctl commands in 32 bit emulation
  177. */
  178. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  179. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  180. #endif
  181. /*
  182. * For INODE and NODE manager
  183. */
  184. /*
  185. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  186. * as its node offset to distinguish from index node blocks.
  187. * But some bits are used to mark the node block.
  188. */
  189. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  190. >> OFFSET_BIT_SHIFT)
  191. enum {
  192. ALLOC_NODE, /* allocate a new node page if needed */
  193. LOOKUP_NODE, /* look up a node without readahead */
  194. LOOKUP_NODE_RA, /*
  195. * look up a node with readahead called
  196. * by get_data_block.
  197. */
  198. };
  199. #define F2FS_LINK_MAX 32000 /* maximum link count per file */
  200. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  201. /* for in-memory extent cache entry */
  202. #define F2FS_MIN_EXTENT_LEN 16 /* minimum extent length */
  203. struct extent_info {
  204. rwlock_t ext_lock; /* rwlock for consistency */
  205. unsigned int fofs; /* start offset in a file */
  206. u32 blk_addr; /* start block address of the extent */
  207. unsigned int len; /* length of the extent */
  208. };
  209. /*
  210. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  211. */
  212. #define FADVISE_COLD_BIT 0x01
  213. #define FADVISE_LOST_PINO_BIT 0x02
  214. #define DEF_DIR_LEVEL 0
  215. struct f2fs_inode_info {
  216. struct inode vfs_inode; /* serve a vfs inode */
  217. unsigned long i_flags; /* keep an inode flags for ioctl */
  218. unsigned char i_advise; /* use to give file attribute hints */
  219. unsigned char i_dir_level; /* use for dentry level for large dir */
  220. unsigned int i_current_depth; /* use only in directory structure */
  221. unsigned int i_pino; /* parent inode number */
  222. umode_t i_acl_mode; /* keep file acl mode temporarily */
  223. /* Use below internally in f2fs*/
  224. unsigned long flags; /* use to pass per-file flags */
  225. struct rw_semaphore i_sem; /* protect fi info */
  226. atomic_t dirty_pages; /* # of dirty pages */
  227. f2fs_hash_t chash; /* hash value of given file name */
  228. unsigned int clevel; /* maximum level of given file name */
  229. nid_t i_xattr_nid; /* node id that contains xattrs */
  230. unsigned long long xattr_ver; /* cp version of xattr modification */
  231. struct extent_info ext; /* in-memory extent cache entry */
  232. struct dir_inode_entry *dirty_dir; /* the pointer of dirty dir */
  233. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  234. struct mutex inmem_lock; /* lock for inmemory pages */
  235. };
  236. static inline void get_extent_info(struct extent_info *ext,
  237. struct f2fs_extent i_ext)
  238. {
  239. write_lock(&ext->ext_lock);
  240. ext->fofs = le32_to_cpu(i_ext.fofs);
  241. ext->blk_addr = le32_to_cpu(i_ext.blk_addr);
  242. ext->len = le32_to_cpu(i_ext.len);
  243. write_unlock(&ext->ext_lock);
  244. }
  245. static inline void set_raw_extent(struct extent_info *ext,
  246. struct f2fs_extent *i_ext)
  247. {
  248. read_lock(&ext->ext_lock);
  249. i_ext->fofs = cpu_to_le32(ext->fofs);
  250. i_ext->blk_addr = cpu_to_le32(ext->blk_addr);
  251. i_ext->len = cpu_to_le32(ext->len);
  252. read_unlock(&ext->ext_lock);
  253. }
  254. struct f2fs_nm_info {
  255. block_t nat_blkaddr; /* base disk address of NAT */
  256. nid_t max_nid; /* maximum possible node ids */
  257. nid_t available_nids; /* maximum available node ids */
  258. nid_t next_scan_nid; /* the next nid to be scanned */
  259. unsigned int ram_thresh; /* control the memory footprint */
  260. /* NAT cache management */
  261. struct radix_tree_root nat_root;/* root of the nat entry cache */
  262. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  263. rwlock_t nat_tree_lock; /* protect nat_tree_lock */
  264. struct list_head nat_entries; /* cached nat entry list (clean) */
  265. unsigned int nat_cnt; /* the # of cached nat entries */
  266. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  267. /* free node ids management */
  268. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  269. struct list_head free_nid_list; /* a list for free nids */
  270. spinlock_t free_nid_list_lock; /* protect free nid list */
  271. unsigned int fcnt; /* the number of free node id */
  272. struct mutex build_lock; /* lock for build free nids */
  273. /* for checkpoint */
  274. char *nat_bitmap; /* NAT bitmap pointer */
  275. int bitmap_size; /* bitmap size */
  276. };
  277. /*
  278. * this structure is used as one of function parameters.
  279. * all the information are dedicated to a given direct node block determined
  280. * by the data offset in a file.
  281. */
  282. struct dnode_of_data {
  283. struct inode *inode; /* vfs inode pointer */
  284. struct page *inode_page; /* its inode page, NULL is possible */
  285. struct page *node_page; /* cached direct node page */
  286. nid_t nid; /* node id of the direct node block */
  287. unsigned int ofs_in_node; /* data offset in the node page */
  288. bool inode_page_locked; /* inode page is locked or not */
  289. block_t data_blkaddr; /* block address of the node block */
  290. };
  291. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  292. struct page *ipage, struct page *npage, nid_t nid)
  293. {
  294. memset(dn, 0, sizeof(*dn));
  295. dn->inode = inode;
  296. dn->inode_page = ipage;
  297. dn->node_page = npage;
  298. dn->nid = nid;
  299. }
  300. /*
  301. * For SIT manager
  302. *
  303. * By default, there are 6 active log areas across the whole main area.
  304. * When considering hot and cold data separation to reduce cleaning overhead,
  305. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  306. * respectively.
  307. * In the current design, you should not change the numbers intentionally.
  308. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  309. * logs individually according to the underlying devices. (default: 6)
  310. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  311. * data and 8 for node logs.
  312. */
  313. #define NR_CURSEG_DATA_TYPE (3)
  314. #define NR_CURSEG_NODE_TYPE (3)
  315. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  316. enum {
  317. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  318. CURSEG_WARM_DATA, /* data blocks */
  319. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  320. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  321. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  322. CURSEG_COLD_NODE, /* indirect node blocks */
  323. NO_CHECK_TYPE
  324. };
  325. struct flush_cmd {
  326. struct completion wait;
  327. struct llist_node llnode;
  328. int ret;
  329. };
  330. struct flush_cmd_control {
  331. struct task_struct *f2fs_issue_flush; /* flush thread */
  332. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  333. struct llist_head issue_list; /* list for command issue */
  334. struct llist_node *dispatch_list; /* list for command dispatch */
  335. };
  336. struct f2fs_sm_info {
  337. struct sit_info *sit_info; /* whole segment information */
  338. struct free_segmap_info *free_info; /* free segment information */
  339. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  340. struct curseg_info *curseg_array; /* active segment information */
  341. block_t seg0_blkaddr; /* block address of 0'th segment */
  342. block_t main_blkaddr; /* start block address of main area */
  343. block_t ssa_blkaddr; /* start block address of SSA area */
  344. unsigned int segment_count; /* total # of segments */
  345. unsigned int main_segments; /* # of segments in main area */
  346. unsigned int reserved_segments; /* # of reserved segments */
  347. unsigned int ovp_segments; /* # of overprovision segments */
  348. /* a threshold to reclaim prefree segments */
  349. unsigned int rec_prefree_segments;
  350. /* for small discard management */
  351. struct list_head discard_list; /* 4KB discard list */
  352. int nr_discards; /* # of discards in the list */
  353. int max_discards; /* max. discards to be issued */
  354. struct list_head sit_entry_set; /* sit entry set list */
  355. unsigned int ipu_policy; /* in-place-update policy */
  356. unsigned int min_ipu_util; /* in-place-update threshold */
  357. unsigned int min_fsync_blocks; /* threshold for fsync */
  358. /* for flush command control */
  359. struct flush_cmd_control *cmd_control_info;
  360. };
  361. /*
  362. * For superblock
  363. */
  364. /*
  365. * COUNT_TYPE for monitoring
  366. *
  367. * f2fs monitors the number of several block types such as on-writeback,
  368. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  369. */
  370. enum count_type {
  371. F2FS_WRITEBACK,
  372. F2FS_DIRTY_DENTS,
  373. F2FS_DIRTY_NODES,
  374. F2FS_DIRTY_META,
  375. NR_COUNT_TYPE,
  376. };
  377. /*
  378. * The below are the page types of bios used in submit_bio().
  379. * The available types are:
  380. * DATA User data pages. It operates as async mode.
  381. * NODE Node pages. It operates as async mode.
  382. * META FS metadata pages such as SIT, NAT, CP.
  383. * NR_PAGE_TYPE The number of page types.
  384. * META_FLUSH Make sure the previous pages are written
  385. * with waiting the bio's completion
  386. * ... Only can be used with META.
  387. */
  388. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  389. enum page_type {
  390. DATA,
  391. NODE,
  392. META,
  393. NR_PAGE_TYPE,
  394. META_FLUSH,
  395. };
  396. struct f2fs_io_info {
  397. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  398. int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
  399. };
  400. #define is_read_io(rw) (((rw) & 1) == READ)
  401. struct f2fs_bio_info {
  402. struct f2fs_sb_info *sbi; /* f2fs superblock */
  403. struct bio *bio; /* bios to merge */
  404. sector_t last_block_in_bio; /* last block number */
  405. struct f2fs_io_info fio; /* store buffered io info. */
  406. struct rw_semaphore io_rwsem; /* blocking op for bio */
  407. };
  408. struct f2fs_sb_info {
  409. struct super_block *sb; /* pointer to VFS super block */
  410. struct proc_dir_entry *s_proc; /* proc entry */
  411. struct buffer_head *raw_super_buf; /* buffer head of raw sb */
  412. struct f2fs_super_block *raw_super; /* raw super block pointer */
  413. int s_dirty; /* dirty flag for checkpoint */
  414. bool need_fsck; /* need fsck.f2fs to fix */
  415. /* for node-related operations */
  416. struct f2fs_nm_info *nm_info; /* node manager */
  417. struct inode *node_inode; /* cache node blocks */
  418. /* for segment-related operations */
  419. struct f2fs_sm_info *sm_info; /* segment manager */
  420. /* for bio operations */
  421. struct f2fs_bio_info read_io; /* for read bios */
  422. struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
  423. struct completion *wait_io; /* for completion bios */
  424. /* for checkpoint */
  425. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  426. struct inode *meta_inode; /* cache meta blocks */
  427. struct mutex cp_mutex; /* checkpoint procedure lock */
  428. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  429. struct rw_semaphore node_write; /* locking node writes */
  430. struct mutex writepages; /* mutex for writepages() */
  431. bool por_doing; /* recovery is doing or not */
  432. wait_queue_head_t cp_wait;
  433. /* for inode management */
  434. struct radix_tree_root ino_root[MAX_INO_ENTRY]; /* ino entry array */
  435. spinlock_t ino_lock[MAX_INO_ENTRY]; /* for ino entry lock */
  436. struct list_head ino_list[MAX_INO_ENTRY]; /* inode list head */
  437. /* for orphan inode, use 0'th array */
  438. unsigned int n_orphans; /* # of orphan inodes */
  439. unsigned int max_orphans; /* max orphan inodes */
  440. /* for directory inode management */
  441. struct list_head dir_inode_list; /* dir inode list */
  442. spinlock_t dir_inode_lock; /* for dir inode list lock */
  443. /* basic filesystem units */
  444. unsigned int log_sectors_per_block; /* log2 sectors per block */
  445. unsigned int log_blocksize; /* log2 block size */
  446. unsigned int blocksize; /* block size */
  447. unsigned int root_ino_num; /* root inode number*/
  448. unsigned int node_ino_num; /* node inode number*/
  449. unsigned int meta_ino_num; /* meta inode number*/
  450. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  451. unsigned int blocks_per_seg; /* blocks per segment */
  452. unsigned int segs_per_sec; /* segments per section */
  453. unsigned int secs_per_zone; /* sections per zone */
  454. unsigned int total_sections; /* total section count */
  455. unsigned int total_node_count; /* total node block count */
  456. unsigned int total_valid_node_count; /* valid node block count */
  457. unsigned int total_valid_inode_count; /* valid inode count */
  458. int active_logs; /* # of active logs */
  459. int dir_level; /* directory level */
  460. block_t user_block_count; /* # of user blocks */
  461. block_t total_valid_block_count; /* # of valid blocks */
  462. block_t alloc_valid_block_count; /* # of allocated blocks */
  463. block_t last_valid_block_count; /* for recovery */
  464. u32 s_next_generation; /* for NFS support */
  465. atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
  466. struct f2fs_mount_info mount_opt; /* mount options */
  467. /* for cleaning operations */
  468. struct mutex gc_mutex; /* mutex for GC */
  469. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  470. unsigned int cur_victim_sec; /* current victim section num */
  471. /* maximum # of trials to find a victim segment for SSR and GC */
  472. unsigned int max_victim_search;
  473. /*
  474. * for stat information.
  475. * one is for the LFS mode, and the other is for the SSR mode.
  476. */
  477. #ifdef CONFIG_F2FS_STAT_FS
  478. struct f2fs_stat_info *stat_info; /* FS status information */
  479. unsigned int segment_count[2]; /* # of allocated segments */
  480. unsigned int block_count[2]; /* # of allocated blocks */
  481. int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
  482. int inline_inode; /* # of inline_data inodes */
  483. int bg_gc; /* background gc calls */
  484. unsigned int n_dirty_dirs; /* # of dir inodes */
  485. #endif
  486. unsigned int last_victim[2]; /* last victim segment # */
  487. spinlock_t stat_lock; /* lock for stat operations */
  488. /* For sysfs suppport */
  489. struct kobject s_kobj;
  490. struct completion s_kobj_unregister;
  491. };
  492. /*
  493. * Inline functions
  494. */
  495. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  496. {
  497. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  498. }
  499. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  500. {
  501. return sb->s_fs_info;
  502. }
  503. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  504. {
  505. return F2FS_SB(inode->i_sb);
  506. }
  507. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  508. {
  509. return F2FS_I_SB(mapping->host);
  510. }
  511. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  512. {
  513. return F2FS_M_SB(page->mapping);
  514. }
  515. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  516. {
  517. return (struct f2fs_super_block *)(sbi->raw_super);
  518. }
  519. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  520. {
  521. return (struct f2fs_checkpoint *)(sbi->ckpt);
  522. }
  523. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  524. {
  525. return (struct f2fs_node *)page_address(page);
  526. }
  527. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  528. {
  529. return &((struct f2fs_node *)page_address(page))->i;
  530. }
  531. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  532. {
  533. return (struct f2fs_nm_info *)(sbi->nm_info);
  534. }
  535. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  536. {
  537. return (struct f2fs_sm_info *)(sbi->sm_info);
  538. }
  539. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  540. {
  541. return (struct sit_info *)(SM_I(sbi)->sit_info);
  542. }
  543. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  544. {
  545. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  546. }
  547. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  548. {
  549. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  550. }
  551. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  552. {
  553. return sbi->meta_inode->i_mapping;
  554. }
  555. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  556. {
  557. return sbi->node_inode->i_mapping;
  558. }
  559. static inline void F2FS_SET_SB_DIRT(struct f2fs_sb_info *sbi)
  560. {
  561. sbi->s_dirty = 1;
  562. }
  563. static inline void F2FS_RESET_SB_DIRT(struct f2fs_sb_info *sbi)
  564. {
  565. sbi->s_dirty = 0;
  566. }
  567. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  568. {
  569. return le64_to_cpu(cp->checkpoint_ver);
  570. }
  571. static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  572. {
  573. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  574. return ckpt_flags & f;
  575. }
  576. static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  577. {
  578. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  579. ckpt_flags |= f;
  580. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  581. }
  582. static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  583. {
  584. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  585. ckpt_flags &= (~f);
  586. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  587. }
  588. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  589. {
  590. down_read(&sbi->cp_rwsem);
  591. }
  592. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  593. {
  594. up_read(&sbi->cp_rwsem);
  595. }
  596. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  597. {
  598. f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  599. }
  600. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  601. {
  602. up_write(&sbi->cp_rwsem);
  603. }
  604. /*
  605. * Check whether the given nid is within node id range.
  606. */
  607. static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  608. {
  609. if (unlikely(nid < F2FS_ROOT_INO(sbi)))
  610. return -EINVAL;
  611. if (unlikely(nid >= NM_I(sbi)->max_nid))
  612. return -EINVAL;
  613. return 0;
  614. }
  615. #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
  616. /*
  617. * Check whether the inode has blocks or not
  618. */
  619. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  620. {
  621. if (F2FS_I(inode)->i_xattr_nid)
  622. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
  623. else
  624. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
  625. }
  626. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  627. {
  628. return ofs == XATTR_NODE_OFFSET;
  629. }
  630. static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
  631. struct inode *inode, blkcnt_t count)
  632. {
  633. block_t valid_block_count;
  634. spin_lock(&sbi->stat_lock);
  635. valid_block_count =
  636. sbi->total_valid_block_count + (block_t)count;
  637. if (unlikely(valid_block_count > sbi->user_block_count)) {
  638. spin_unlock(&sbi->stat_lock);
  639. return false;
  640. }
  641. inode->i_blocks += count;
  642. sbi->total_valid_block_count = valid_block_count;
  643. sbi->alloc_valid_block_count += (block_t)count;
  644. spin_unlock(&sbi->stat_lock);
  645. return true;
  646. }
  647. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  648. struct inode *inode,
  649. blkcnt_t count)
  650. {
  651. spin_lock(&sbi->stat_lock);
  652. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  653. f2fs_bug_on(sbi, inode->i_blocks < count);
  654. inode->i_blocks -= count;
  655. sbi->total_valid_block_count -= (block_t)count;
  656. spin_unlock(&sbi->stat_lock);
  657. }
  658. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  659. {
  660. atomic_inc(&sbi->nr_pages[count_type]);
  661. F2FS_SET_SB_DIRT(sbi);
  662. }
  663. static inline void inode_inc_dirty_pages(struct inode *inode)
  664. {
  665. atomic_inc(&F2FS_I(inode)->dirty_pages);
  666. if (S_ISDIR(inode->i_mode))
  667. inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  668. }
  669. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  670. {
  671. atomic_dec(&sbi->nr_pages[count_type]);
  672. }
  673. static inline void inode_dec_dirty_pages(struct inode *inode)
  674. {
  675. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
  676. return;
  677. atomic_dec(&F2FS_I(inode)->dirty_pages);
  678. if (S_ISDIR(inode->i_mode))
  679. dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  680. }
  681. static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
  682. {
  683. return atomic_read(&sbi->nr_pages[count_type]);
  684. }
  685. static inline int get_dirty_pages(struct inode *inode)
  686. {
  687. return atomic_read(&F2FS_I(inode)->dirty_pages);
  688. }
  689. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  690. {
  691. unsigned int pages_per_sec = sbi->segs_per_sec *
  692. (1 << sbi->log_blocks_per_seg);
  693. return ((get_pages(sbi, block_type) + pages_per_sec - 1)
  694. >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
  695. }
  696. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  697. {
  698. return sbi->total_valid_block_count;
  699. }
  700. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  701. {
  702. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  703. /* return NAT or SIT bitmap */
  704. if (flag == NAT_BITMAP)
  705. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  706. else if (flag == SIT_BITMAP)
  707. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  708. return 0;
  709. }
  710. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  711. {
  712. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  713. int offset;
  714. if (le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload) > 0) {
  715. if (flag == NAT_BITMAP)
  716. return &ckpt->sit_nat_version_bitmap;
  717. else
  718. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  719. } else {
  720. offset = (flag == NAT_BITMAP) ?
  721. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  722. return &ckpt->sit_nat_version_bitmap + offset;
  723. }
  724. }
  725. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  726. {
  727. block_t start_addr;
  728. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  729. unsigned long long ckpt_version = cur_cp_version(ckpt);
  730. start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  731. /*
  732. * odd numbered checkpoint should at cp segment 0
  733. * and even segment must be at cp segment 1
  734. */
  735. if (!(ckpt_version & 1))
  736. start_addr += sbi->blocks_per_seg;
  737. return start_addr;
  738. }
  739. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  740. {
  741. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  742. }
  743. static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
  744. struct inode *inode)
  745. {
  746. block_t valid_block_count;
  747. unsigned int valid_node_count;
  748. spin_lock(&sbi->stat_lock);
  749. valid_block_count = sbi->total_valid_block_count + 1;
  750. if (unlikely(valid_block_count > sbi->user_block_count)) {
  751. spin_unlock(&sbi->stat_lock);
  752. return false;
  753. }
  754. valid_node_count = sbi->total_valid_node_count + 1;
  755. if (unlikely(valid_node_count > sbi->total_node_count)) {
  756. spin_unlock(&sbi->stat_lock);
  757. return false;
  758. }
  759. if (inode)
  760. inode->i_blocks++;
  761. sbi->alloc_valid_block_count++;
  762. sbi->total_valid_node_count++;
  763. sbi->total_valid_block_count++;
  764. spin_unlock(&sbi->stat_lock);
  765. return true;
  766. }
  767. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  768. struct inode *inode)
  769. {
  770. spin_lock(&sbi->stat_lock);
  771. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  772. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  773. f2fs_bug_on(sbi, !inode->i_blocks);
  774. inode->i_blocks--;
  775. sbi->total_valid_node_count--;
  776. sbi->total_valid_block_count--;
  777. spin_unlock(&sbi->stat_lock);
  778. }
  779. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  780. {
  781. return sbi->total_valid_node_count;
  782. }
  783. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  784. {
  785. spin_lock(&sbi->stat_lock);
  786. f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
  787. sbi->total_valid_inode_count++;
  788. spin_unlock(&sbi->stat_lock);
  789. }
  790. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  791. {
  792. spin_lock(&sbi->stat_lock);
  793. f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
  794. sbi->total_valid_inode_count--;
  795. spin_unlock(&sbi->stat_lock);
  796. }
  797. static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
  798. {
  799. return sbi->total_valid_inode_count;
  800. }
  801. static inline void f2fs_put_page(struct page *page, int unlock)
  802. {
  803. if (!page)
  804. return;
  805. if (unlock) {
  806. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  807. unlock_page(page);
  808. }
  809. page_cache_release(page);
  810. }
  811. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  812. {
  813. if (dn->node_page)
  814. f2fs_put_page(dn->node_page, 1);
  815. if (dn->inode_page && dn->node_page != dn->inode_page)
  816. f2fs_put_page(dn->inode_page, 0);
  817. dn->node_page = NULL;
  818. dn->inode_page = NULL;
  819. }
  820. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  821. size_t size)
  822. {
  823. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  824. }
  825. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  826. gfp_t flags)
  827. {
  828. void *entry;
  829. retry:
  830. entry = kmem_cache_alloc(cachep, flags);
  831. if (!entry) {
  832. cond_resched();
  833. goto retry;
  834. }
  835. return entry;
  836. }
  837. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  838. static inline bool IS_INODE(struct page *page)
  839. {
  840. struct f2fs_node *p = F2FS_NODE(page);
  841. return RAW_IS_INODE(p);
  842. }
  843. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  844. {
  845. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  846. }
  847. static inline block_t datablock_addr(struct page *node_page,
  848. unsigned int offset)
  849. {
  850. struct f2fs_node *raw_node;
  851. __le32 *addr_array;
  852. raw_node = F2FS_NODE(node_page);
  853. addr_array = blkaddr_in_node(raw_node);
  854. return le32_to_cpu(addr_array[offset]);
  855. }
  856. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  857. {
  858. int mask;
  859. addr += (nr >> 3);
  860. mask = 1 << (7 - (nr & 0x07));
  861. return mask & *addr;
  862. }
  863. static inline int f2fs_set_bit(unsigned int nr, char *addr)
  864. {
  865. int mask;
  866. int ret;
  867. addr += (nr >> 3);
  868. mask = 1 << (7 - (nr & 0x07));
  869. ret = mask & *addr;
  870. *addr |= mask;
  871. return ret;
  872. }
  873. static inline int f2fs_clear_bit(unsigned int nr, char *addr)
  874. {
  875. int mask;
  876. int ret;
  877. addr += (nr >> 3);
  878. mask = 1 << (7 - (nr & 0x07));
  879. ret = mask & *addr;
  880. *addr &= ~mask;
  881. return ret;
  882. }
  883. /* used for f2fs_inode_info->flags */
  884. enum {
  885. FI_NEW_INODE, /* indicate newly allocated inode */
  886. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  887. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  888. FI_INC_LINK, /* need to increment i_nlink */
  889. FI_ACL_MODE, /* indicate acl mode */
  890. FI_NO_ALLOC, /* should not allocate any blocks */
  891. FI_UPDATE_DIR, /* should update inode block for consistency */
  892. FI_DELAY_IPUT, /* used for the recovery */
  893. FI_NO_EXTENT, /* not to use the extent cache */
  894. FI_INLINE_XATTR, /* used for inline xattr */
  895. FI_INLINE_DATA, /* used for inline data*/
  896. FI_APPEND_WRITE, /* inode has appended data */
  897. FI_UPDATE_WRITE, /* inode has in-place-update data */
  898. FI_NEED_IPU, /* used for ipu per file */
  899. FI_ATOMIC_FILE, /* indicate atomic file */
  900. FI_VOLATILE_FILE, /* indicate volatile file */
  901. };
  902. static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
  903. {
  904. if (!test_bit(flag, &fi->flags))
  905. set_bit(flag, &fi->flags);
  906. }
  907. static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
  908. {
  909. return test_bit(flag, &fi->flags);
  910. }
  911. static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  912. {
  913. if (test_bit(flag, &fi->flags))
  914. clear_bit(flag, &fi->flags);
  915. }
  916. static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
  917. {
  918. fi->i_acl_mode = mode;
  919. set_inode_flag(fi, FI_ACL_MODE);
  920. }
  921. static inline int cond_clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  922. {
  923. if (is_inode_flag_set(fi, FI_ACL_MODE)) {
  924. clear_inode_flag(fi, FI_ACL_MODE);
  925. return 1;
  926. }
  927. return 0;
  928. }
  929. static inline void get_inline_info(struct f2fs_inode_info *fi,
  930. struct f2fs_inode *ri)
  931. {
  932. if (ri->i_inline & F2FS_INLINE_XATTR)
  933. set_inode_flag(fi, FI_INLINE_XATTR);
  934. if (ri->i_inline & F2FS_INLINE_DATA)
  935. set_inode_flag(fi, FI_INLINE_DATA);
  936. }
  937. static inline void set_raw_inline(struct f2fs_inode_info *fi,
  938. struct f2fs_inode *ri)
  939. {
  940. ri->i_inline = 0;
  941. if (is_inode_flag_set(fi, FI_INLINE_XATTR))
  942. ri->i_inline |= F2FS_INLINE_XATTR;
  943. if (is_inode_flag_set(fi, FI_INLINE_DATA))
  944. ri->i_inline |= F2FS_INLINE_DATA;
  945. }
  946. static inline int f2fs_has_inline_xattr(struct inode *inode)
  947. {
  948. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
  949. }
  950. static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
  951. {
  952. if (f2fs_has_inline_xattr(&fi->vfs_inode))
  953. return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
  954. return DEF_ADDRS_PER_INODE;
  955. }
  956. static inline void *inline_xattr_addr(struct page *page)
  957. {
  958. struct f2fs_inode *ri = F2FS_INODE(page);
  959. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  960. F2FS_INLINE_XATTR_ADDRS]);
  961. }
  962. static inline int inline_xattr_size(struct inode *inode)
  963. {
  964. if (f2fs_has_inline_xattr(inode))
  965. return F2FS_INLINE_XATTR_ADDRS << 2;
  966. else
  967. return 0;
  968. }
  969. static inline int f2fs_has_inline_data(struct inode *inode)
  970. {
  971. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
  972. }
  973. static inline bool f2fs_is_atomic_file(struct inode *inode)
  974. {
  975. return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
  976. }
  977. static inline bool f2fs_is_volatile_file(struct inode *inode)
  978. {
  979. return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
  980. }
  981. static inline void *inline_data_addr(struct page *page)
  982. {
  983. struct f2fs_inode *ri = F2FS_INODE(page);
  984. return (void *)&(ri->i_addr[1]);
  985. }
  986. static inline int f2fs_readonly(struct super_block *sb)
  987. {
  988. return sb->s_flags & MS_RDONLY;
  989. }
  990. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  991. {
  992. return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  993. }
  994. static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
  995. {
  996. set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  997. sbi->sb->s_flags |= MS_RDONLY;
  998. }
  999. #define get_inode_mode(i) \
  1000. ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
  1001. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  1002. /* get offset of first page in next direct node */
  1003. #define PGOFS_OF_NEXT_DNODE(pgofs, fi) \
  1004. ((pgofs < ADDRS_PER_INODE(fi)) ? ADDRS_PER_INODE(fi) : \
  1005. (pgofs - ADDRS_PER_INODE(fi) + ADDRS_PER_BLOCK) / \
  1006. ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi))
  1007. /*
  1008. * file.c
  1009. */
  1010. int f2fs_sync_file(struct file *, loff_t, loff_t, int);
  1011. void truncate_data_blocks(struct dnode_of_data *);
  1012. int truncate_blocks(struct inode *, u64, bool);
  1013. void f2fs_truncate(struct inode *);
  1014. int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
  1015. int f2fs_setattr(struct dentry *, struct iattr *);
  1016. int truncate_hole(struct inode *, pgoff_t, pgoff_t);
  1017. int truncate_data_blocks_range(struct dnode_of_data *, int);
  1018. long f2fs_ioctl(struct file *, unsigned int, unsigned long);
  1019. long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
  1020. /*
  1021. * inode.c
  1022. */
  1023. void f2fs_set_inode_flags(struct inode *);
  1024. struct inode *f2fs_iget(struct super_block *, unsigned long);
  1025. int try_to_free_nats(struct f2fs_sb_info *, int);
  1026. void update_inode(struct inode *, struct page *);
  1027. void update_inode_page(struct inode *);
  1028. int f2fs_write_inode(struct inode *, struct writeback_control *);
  1029. void f2fs_evict_inode(struct inode *);
  1030. void handle_failed_inode(struct inode *);
  1031. /*
  1032. * namei.c
  1033. */
  1034. struct dentry *f2fs_get_parent(struct dentry *child);
  1035. /*
  1036. * dir.c
  1037. */
  1038. struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
  1039. struct page **);
  1040. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
  1041. ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
  1042. void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
  1043. struct page *, struct inode *);
  1044. int update_dent_inode(struct inode *, const struct qstr *);
  1045. int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *);
  1046. void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *);
  1047. int f2fs_do_tmpfile(struct inode *, struct inode *);
  1048. int f2fs_make_empty(struct inode *, struct inode *);
  1049. bool f2fs_empty_dir(struct inode *);
  1050. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  1051. {
  1052. return __f2fs_add_link(dentry->d_parent->d_inode, &dentry->d_name,
  1053. inode);
  1054. }
  1055. /*
  1056. * super.c
  1057. */
  1058. int f2fs_sync_fs(struct super_block *, int);
  1059. extern __printf(3, 4)
  1060. void f2fs_msg(struct super_block *, const char *, const char *, ...);
  1061. /*
  1062. * hash.c
  1063. */
  1064. f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
  1065. /*
  1066. * node.c
  1067. */
  1068. struct dnode_of_data;
  1069. struct node_info;
  1070. bool available_free_memory(struct f2fs_sb_info *, int);
  1071. bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
  1072. bool has_fsynced_inode(struct f2fs_sb_info *, nid_t);
  1073. bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
  1074. void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
  1075. int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
  1076. int truncate_inode_blocks(struct inode *, pgoff_t);
  1077. int truncate_xattr_node(struct inode *, struct page *);
  1078. int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
  1079. void remove_inode_page(struct inode *);
  1080. struct page *new_inode_page(struct inode *);
  1081. struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
  1082. void ra_node_page(struct f2fs_sb_info *, nid_t);
  1083. struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
  1084. struct page *get_node_page_ra(struct page *, int);
  1085. void sync_inode_page(struct dnode_of_data *);
  1086. int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
  1087. bool alloc_nid(struct f2fs_sb_info *, nid_t *);
  1088. void alloc_nid_done(struct f2fs_sb_info *, nid_t);
  1089. void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
  1090. void recover_inline_xattr(struct inode *, struct page *);
  1091. void recover_xattr_data(struct inode *, struct page *, block_t);
  1092. int recover_inode_page(struct f2fs_sb_info *, struct page *);
  1093. int restore_node_summary(struct f2fs_sb_info *, unsigned int,
  1094. struct f2fs_summary_block *);
  1095. void flush_nat_entries(struct f2fs_sb_info *);
  1096. int build_node_manager(struct f2fs_sb_info *);
  1097. void destroy_node_manager(struct f2fs_sb_info *);
  1098. int __init create_node_manager_caches(void);
  1099. void destroy_node_manager_caches(void);
  1100. /*
  1101. * segment.c
  1102. */
  1103. void register_inmem_page(struct inode *, struct page *);
  1104. void commit_inmem_pages(struct inode *, bool);
  1105. void f2fs_balance_fs(struct f2fs_sb_info *);
  1106. void f2fs_balance_fs_bg(struct f2fs_sb_info *);
  1107. int f2fs_issue_flush(struct f2fs_sb_info *);
  1108. int create_flush_cmd_control(struct f2fs_sb_info *);
  1109. void destroy_flush_cmd_control(struct f2fs_sb_info *);
  1110. void invalidate_blocks(struct f2fs_sb_info *, block_t);
  1111. void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
  1112. void clear_prefree_segments(struct f2fs_sb_info *);
  1113. void release_discard_addrs(struct f2fs_sb_info *);
  1114. void discard_next_dnode(struct f2fs_sb_info *, block_t);
  1115. int npages_for_summary_flush(struct f2fs_sb_info *);
  1116. void allocate_new_segments(struct f2fs_sb_info *);
  1117. int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
  1118. struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
  1119. void write_meta_page(struct f2fs_sb_info *, struct page *);
  1120. void write_node_page(struct f2fs_sb_info *, struct page *,
  1121. struct f2fs_io_info *, unsigned int, block_t, block_t *);
  1122. void write_data_page(struct page *, struct dnode_of_data *, block_t *,
  1123. struct f2fs_io_info *);
  1124. void rewrite_data_page(struct page *, block_t, struct f2fs_io_info *);
  1125. void recover_data_page(struct f2fs_sb_info *, struct page *,
  1126. struct f2fs_summary *, block_t, block_t);
  1127. void allocate_data_block(struct f2fs_sb_info *, struct page *,
  1128. block_t, block_t *, struct f2fs_summary *, int);
  1129. void f2fs_wait_on_page_writeback(struct page *, enum page_type);
  1130. void write_data_summaries(struct f2fs_sb_info *, block_t);
  1131. void write_node_summaries(struct f2fs_sb_info *, block_t);
  1132. int lookup_journal_in_cursum(struct f2fs_summary_block *,
  1133. int, unsigned int, int);
  1134. void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
  1135. int build_segment_manager(struct f2fs_sb_info *);
  1136. void destroy_segment_manager(struct f2fs_sb_info *);
  1137. int __init create_segment_manager_caches(void);
  1138. void destroy_segment_manager_caches(void);
  1139. /*
  1140. * checkpoint.c
  1141. */
  1142. struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
  1143. struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
  1144. struct page *get_meta_page_ra(struct f2fs_sb_info *, pgoff_t);
  1145. int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
  1146. long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
  1147. void add_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1148. void remove_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1149. void release_dirty_inode(struct f2fs_sb_info *);
  1150. bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
  1151. int acquire_orphan_inode(struct f2fs_sb_info *);
  1152. void release_orphan_inode(struct f2fs_sb_info *);
  1153. void add_orphan_inode(struct f2fs_sb_info *, nid_t);
  1154. void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
  1155. void recover_orphan_inodes(struct f2fs_sb_info *);
  1156. int get_valid_checkpoint(struct f2fs_sb_info *);
  1157. void update_dirty_page(struct inode *, struct page *);
  1158. void add_dirty_dir_inode(struct inode *);
  1159. void remove_dirty_dir_inode(struct inode *);
  1160. void sync_dirty_dir_inodes(struct f2fs_sb_info *);
  1161. void write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
  1162. void init_ino_entry_info(struct f2fs_sb_info *);
  1163. int __init create_checkpoint_caches(void);
  1164. void destroy_checkpoint_caches(void);
  1165. /*
  1166. * data.c
  1167. */
  1168. void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
  1169. int f2fs_submit_page_bio(struct f2fs_sb_info *, struct page *, block_t, int);
  1170. void f2fs_submit_page_mbio(struct f2fs_sb_info *, struct page *, block_t,
  1171. struct f2fs_io_info *);
  1172. int reserve_new_block(struct dnode_of_data *);
  1173. int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
  1174. void update_extent_cache(block_t, struct dnode_of_data *);
  1175. struct page *find_data_page(struct inode *, pgoff_t, bool);
  1176. struct page *get_lock_data_page(struct inode *, pgoff_t);
  1177. struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
  1178. int do_write_data_page(struct page *, struct f2fs_io_info *);
  1179. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
  1180. /*
  1181. * gc.c
  1182. */
  1183. int start_gc_thread(struct f2fs_sb_info *);
  1184. void stop_gc_thread(struct f2fs_sb_info *);
  1185. block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
  1186. int f2fs_gc(struct f2fs_sb_info *);
  1187. void build_gc_manager(struct f2fs_sb_info *);
  1188. int __init create_gc_caches(void);
  1189. void destroy_gc_caches(void);
  1190. /*
  1191. * recovery.c
  1192. */
  1193. int recover_fsync_data(struct f2fs_sb_info *);
  1194. bool space_for_roll_forward(struct f2fs_sb_info *);
  1195. /*
  1196. * debug.c
  1197. */
  1198. #ifdef CONFIG_F2FS_STAT_FS
  1199. struct f2fs_stat_info {
  1200. struct list_head stat_list;
  1201. struct f2fs_sb_info *sbi;
  1202. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  1203. int main_area_segs, main_area_sections, main_area_zones;
  1204. int hit_ext, total_ext;
  1205. int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
  1206. int nats, sits, fnids;
  1207. int total_count, utilization;
  1208. int bg_gc, inline_inode;
  1209. unsigned int valid_count, valid_node_count, valid_inode_count;
  1210. unsigned int bimodal, avg_vblocks;
  1211. int util_free, util_valid, util_invalid;
  1212. int rsvd_segs, overp_segs;
  1213. int dirty_count, node_pages, meta_pages;
  1214. int prefree_count, call_count, cp_count;
  1215. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  1216. int tot_blks, data_blks, node_blks;
  1217. int curseg[NR_CURSEG_TYPE];
  1218. int cursec[NR_CURSEG_TYPE];
  1219. int curzone[NR_CURSEG_TYPE];
  1220. unsigned int segment_count[2];
  1221. unsigned int block_count[2];
  1222. unsigned base_mem, cache_mem;
  1223. };
  1224. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  1225. {
  1226. return (struct f2fs_stat_info *)sbi->stat_info;
  1227. }
  1228. #define stat_inc_cp_count(si) ((si)->cp_count++)
  1229. #define stat_inc_call_count(si) ((si)->call_count++)
  1230. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  1231. #define stat_inc_dirty_dir(sbi) ((sbi)->n_dirty_dirs++)
  1232. #define stat_dec_dirty_dir(sbi) ((sbi)->n_dirty_dirs--)
  1233. #define stat_inc_total_hit(sb) ((F2FS_SB(sb))->total_hit_ext++)
  1234. #define stat_inc_read_hit(sb) ((F2FS_SB(sb))->read_hit_ext++)
  1235. #define stat_inc_inline_inode(inode) \
  1236. do { \
  1237. if (f2fs_has_inline_data(inode)) \
  1238. ((F2FS_I_SB(inode))->inline_inode++); \
  1239. } while (0)
  1240. #define stat_dec_inline_inode(inode) \
  1241. do { \
  1242. if (f2fs_has_inline_data(inode)) \
  1243. ((F2FS_I_SB(inode))->inline_inode--); \
  1244. } while (0)
  1245. #define stat_inc_seg_type(sbi, curseg) \
  1246. ((sbi)->segment_count[(curseg)->alloc_type]++)
  1247. #define stat_inc_block_count(sbi, curseg) \
  1248. ((sbi)->block_count[(curseg)->alloc_type]++)
  1249. #define stat_inc_seg_count(sbi, type) \
  1250. do { \
  1251. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1252. (si)->tot_segs++; \
  1253. if (type == SUM_TYPE_DATA) \
  1254. si->data_segs++; \
  1255. else \
  1256. si->node_segs++; \
  1257. } while (0)
  1258. #define stat_inc_tot_blk_count(si, blks) \
  1259. (si->tot_blks += (blks))
  1260. #define stat_inc_data_blk_count(sbi, blks) \
  1261. do { \
  1262. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1263. stat_inc_tot_blk_count(si, blks); \
  1264. si->data_blks += (blks); \
  1265. } while (0)
  1266. #define stat_inc_node_blk_count(sbi, blks) \
  1267. do { \
  1268. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1269. stat_inc_tot_blk_count(si, blks); \
  1270. si->node_blks += (blks); \
  1271. } while (0)
  1272. int f2fs_build_stats(struct f2fs_sb_info *);
  1273. void f2fs_destroy_stats(struct f2fs_sb_info *);
  1274. void __init f2fs_create_root_stats(void);
  1275. void f2fs_destroy_root_stats(void);
  1276. #else
  1277. #define stat_inc_cp_count(si)
  1278. #define stat_inc_call_count(si)
  1279. #define stat_inc_bggc_count(si)
  1280. #define stat_inc_dirty_dir(sbi)
  1281. #define stat_dec_dirty_dir(sbi)
  1282. #define stat_inc_total_hit(sb)
  1283. #define stat_inc_read_hit(sb)
  1284. #define stat_inc_inline_inode(inode)
  1285. #define stat_dec_inline_inode(inode)
  1286. #define stat_inc_seg_type(sbi, curseg)
  1287. #define stat_inc_block_count(sbi, curseg)
  1288. #define stat_inc_seg_count(si, type)
  1289. #define stat_inc_tot_blk_count(si, blks)
  1290. #define stat_inc_data_blk_count(si, blks)
  1291. #define stat_inc_node_blk_count(sbi, blks)
  1292. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  1293. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  1294. static inline void __init f2fs_create_root_stats(void) { }
  1295. static inline void f2fs_destroy_root_stats(void) { }
  1296. #endif
  1297. extern const struct file_operations f2fs_dir_operations;
  1298. extern const struct file_operations f2fs_file_operations;
  1299. extern const struct inode_operations f2fs_file_inode_operations;
  1300. extern const struct address_space_operations f2fs_dblock_aops;
  1301. extern const struct address_space_operations f2fs_node_aops;
  1302. extern const struct address_space_operations f2fs_meta_aops;
  1303. extern const struct inode_operations f2fs_dir_inode_operations;
  1304. extern const struct inode_operations f2fs_symlink_inode_operations;
  1305. extern const struct inode_operations f2fs_special_inode_operations;
  1306. /*
  1307. * inline.c
  1308. */
  1309. bool f2fs_may_inline(struct inode *);
  1310. int f2fs_read_inline_data(struct inode *, struct page *);
  1311. int f2fs_convert_inline_data(struct inode *, pgoff_t, struct page *);
  1312. int f2fs_write_inline_data(struct inode *, struct page *, unsigned int);
  1313. void truncate_inline_data(struct inode *, u64);
  1314. bool recover_inline_data(struct inode *, struct page *);
  1315. #endif