f2fs.h 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082
  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. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  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_INLINE_DENTRY 0x00000200
  47. #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
  48. #define F2FS_MOUNT_NOBARRIER 0x00000800
  49. #define F2FS_MOUNT_FASTBOOT 0x00001000
  50. #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
  51. #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
  52. #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
  53. #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
  54. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  55. typecheck(unsigned long long, b) && \
  56. ((long long)((a) - (b)) > 0))
  57. typedef u32 block_t; /*
  58. * should not change u32, since it is the on-disk block
  59. * address format, __le32.
  60. */
  61. typedef u32 nid_t;
  62. struct f2fs_mount_info {
  63. unsigned int opt;
  64. };
  65. #define F2FS_FEATURE_ENCRYPT 0x0001
  66. #define F2FS_HAS_FEATURE(sb, mask) \
  67. ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
  68. #define F2FS_SET_FEATURE(sb, mask) \
  69. F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
  70. #define F2FS_CLEAR_FEATURE(sb, mask) \
  71. F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
  72. #define CRCPOLY_LE 0xedb88320
  73. static inline __u32 f2fs_crc32(void *buf, size_t len)
  74. {
  75. unsigned char *p = (unsigned char *)buf;
  76. __u32 crc = F2FS_SUPER_MAGIC;
  77. int i;
  78. while (len--) {
  79. crc ^= *p++;
  80. for (i = 0; i < 8; i++)
  81. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  82. }
  83. return crc;
  84. }
  85. static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
  86. {
  87. return f2fs_crc32(buf, buf_size) == blk_crc;
  88. }
  89. /*
  90. * For checkpoint manager
  91. */
  92. enum {
  93. NAT_BITMAP,
  94. SIT_BITMAP
  95. };
  96. enum {
  97. CP_UMOUNT,
  98. CP_FASTBOOT,
  99. CP_SYNC,
  100. CP_RECOVERY,
  101. CP_DISCARD,
  102. };
  103. #define DEF_BATCHED_TRIM_SECTIONS 32
  104. #define BATCHED_TRIM_SEGMENTS(sbi) \
  105. (SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
  106. #define BATCHED_TRIM_BLOCKS(sbi) \
  107. (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
  108. struct cp_control {
  109. int reason;
  110. __u64 trim_start;
  111. __u64 trim_end;
  112. __u64 trim_minlen;
  113. __u64 trimmed;
  114. };
  115. /*
  116. * For CP/NAT/SIT/SSA readahead
  117. */
  118. enum {
  119. META_CP,
  120. META_NAT,
  121. META_SIT,
  122. META_SSA,
  123. META_POR,
  124. };
  125. /* for the list of ino */
  126. enum {
  127. ORPHAN_INO, /* for orphan ino list */
  128. APPEND_INO, /* for append ino list */
  129. UPDATE_INO, /* for update ino list */
  130. MAX_INO_ENTRY, /* max. list */
  131. };
  132. struct ino_entry {
  133. struct list_head list; /* list head */
  134. nid_t ino; /* inode number */
  135. };
  136. /*
  137. * for the list of directory inodes or gc inodes.
  138. * NOTE: there are two slab users for this structure, if we add/modify/delete
  139. * fields in structure for one of slab users, it may affect fields or size of
  140. * other one, in this condition, it's better to split both of slab and related
  141. * data structure.
  142. */
  143. struct inode_entry {
  144. struct list_head list; /* list head */
  145. struct inode *inode; /* vfs inode pointer */
  146. };
  147. /* for the list of blockaddresses to be discarded */
  148. struct discard_entry {
  149. struct list_head list; /* list head */
  150. block_t blkaddr; /* block address to be discarded */
  151. int len; /* # of consecutive blocks of the discard */
  152. };
  153. /* for the list of fsync inodes, used only during recovery */
  154. struct fsync_inode_entry {
  155. struct list_head list; /* list head */
  156. struct inode *inode; /* vfs inode pointer */
  157. block_t blkaddr; /* block address locating the last fsync */
  158. block_t last_dentry; /* block address locating the last dentry */
  159. block_t last_inode; /* block address locating the last inode */
  160. };
  161. #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
  162. #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
  163. #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
  164. #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
  165. #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
  166. #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
  167. #define MAX_NAT_JENTRIES(sum) (NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
  168. #define MAX_SIT_JENTRIES(sum) (SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
  169. static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
  170. {
  171. int before = nats_in_cursum(rs);
  172. rs->n_nats = cpu_to_le16(before + i);
  173. return before;
  174. }
  175. static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
  176. {
  177. int before = sits_in_cursum(rs);
  178. rs->n_sits = cpu_to_le16(before + i);
  179. return before;
  180. }
  181. static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
  182. int type)
  183. {
  184. if (type == NAT_JOURNAL)
  185. return size <= MAX_NAT_JENTRIES(sum);
  186. return size <= MAX_SIT_JENTRIES(sum);
  187. }
  188. /*
  189. * ioctl commands
  190. */
  191. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  192. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  193. #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
  194. #define F2FS_IOCTL_MAGIC 0xf5
  195. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  196. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  197. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  198. #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
  199. #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
  200. #define F2FS_IOC_SET_ENCRYPTION_POLICY \
  201. _IOR('f', 19, struct f2fs_encryption_policy)
  202. #define F2FS_IOC_GET_ENCRYPTION_PWSALT \
  203. _IOW('f', 20, __u8[16])
  204. #define F2FS_IOC_GET_ENCRYPTION_POLICY \
  205. _IOW('f', 21, struct f2fs_encryption_policy)
  206. /*
  207. * should be same as XFS_IOC_GOINGDOWN.
  208. * Flags for going down operation used by FS_IOC_GOINGDOWN
  209. */
  210. #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
  211. #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
  212. #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
  213. #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
  214. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  215. /*
  216. * ioctl commands in 32 bit emulation
  217. */
  218. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  219. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  220. #endif
  221. /*
  222. * For INODE and NODE manager
  223. */
  224. /* for directory operations */
  225. struct f2fs_str {
  226. unsigned char *name;
  227. u32 len;
  228. };
  229. struct f2fs_filename {
  230. const struct qstr *usr_fname;
  231. struct f2fs_str disk_name;
  232. f2fs_hash_t hash;
  233. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  234. struct f2fs_str crypto_buf;
  235. #endif
  236. };
  237. #define FSTR_INIT(n, l) { .name = n, .len = l }
  238. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  239. #define fname_name(p) ((p)->disk_name.name)
  240. #define fname_len(p) ((p)->disk_name.len)
  241. struct f2fs_dentry_ptr {
  242. struct inode *inode;
  243. const void *bitmap;
  244. struct f2fs_dir_entry *dentry;
  245. __u8 (*filename)[F2FS_SLOT_LEN];
  246. int max;
  247. };
  248. static inline void make_dentry_ptr(struct inode *inode,
  249. struct f2fs_dentry_ptr *d, void *src, int type)
  250. {
  251. d->inode = inode;
  252. if (type == 1) {
  253. struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
  254. d->max = NR_DENTRY_IN_BLOCK;
  255. d->bitmap = &t->dentry_bitmap;
  256. d->dentry = t->dentry;
  257. d->filename = t->filename;
  258. } else {
  259. struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
  260. d->max = NR_INLINE_DENTRY;
  261. d->bitmap = &t->dentry_bitmap;
  262. d->dentry = t->dentry;
  263. d->filename = t->filename;
  264. }
  265. }
  266. /*
  267. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  268. * as its node offset to distinguish from index node blocks.
  269. * But some bits are used to mark the node block.
  270. */
  271. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  272. >> OFFSET_BIT_SHIFT)
  273. enum {
  274. ALLOC_NODE, /* allocate a new node page if needed */
  275. LOOKUP_NODE, /* look up a node without readahead */
  276. LOOKUP_NODE_RA, /*
  277. * look up a node with readahead called
  278. * by get_data_block.
  279. */
  280. };
  281. #define F2FS_LINK_MAX 32000 /* maximum link count per file */
  282. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  283. /* vector size for gang look-up from extent cache that consists of radix tree */
  284. #define EXT_TREE_VEC_SIZE 64
  285. /* for in-memory extent cache entry */
  286. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  287. /* number of extent info in extent cache we try to shrink */
  288. #define EXTENT_CACHE_SHRINK_NUMBER 128
  289. struct extent_info {
  290. unsigned int fofs; /* start offset in a file */
  291. u32 blk; /* start block address of the extent */
  292. unsigned int len; /* length of the extent */
  293. };
  294. struct extent_node {
  295. struct rb_node rb_node; /* rb node located in rb-tree */
  296. struct list_head list; /* node in global extent list of sbi */
  297. struct extent_info ei; /* extent info */
  298. };
  299. struct extent_tree {
  300. nid_t ino; /* inode number */
  301. struct rb_root root; /* root of extent info rb-tree */
  302. struct extent_node *cached_en; /* recently accessed extent node */
  303. rwlock_t lock; /* protect extent info rb-tree */
  304. atomic_t refcount; /* reference count of rb-tree */
  305. unsigned int count; /* # of extent node in rb-tree*/
  306. };
  307. /*
  308. * This structure is taken from ext4_map_blocks.
  309. *
  310. * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
  311. */
  312. #define F2FS_MAP_NEW (1 << BH_New)
  313. #define F2FS_MAP_MAPPED (1 << BH_Mapped)
  314. #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
  315. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  316. F2FS_MAP_UNWRITTEN)
  317. struct f2fs_map_blocks {
  318. block_t m_pblk;
  319. block_t m_lblk;
  320. unsigned int m_len;
  321. unsigned int m_flags;
  322. };
  323. /*
  324. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  325. */
  326. #define FADVISE_COLD_BIT 0x01
  327. #define FADVISE_LOST_PINO_BIT 0x02
  328. #define FADVISE_ENCRYPT_BIT 0x04
  329. #define FADVISE_ENC_NAME_BIT 0x08
  330. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  331. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  332. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  333. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  334. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  335. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  336. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  337. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  338. #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
  339. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  340. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  341. /* Encryption algorithms */
  342. #define F2FS_ENCRYPTION_MODE_INVALID 0
  343. #define F2FS_ENCRYPTION_MODE_AES_256_XTS 1
  344. #define F2FS_ENCRYPTION_MODE_AES_256_GCM 2
  345. #define F2FS_ENCRYPTION_MODE_AES_256_CBC 3
  346. #define F2FS_ENCRYPTION_MODE_AES_256_CTS 4
  347. #include "f2fs_crypto.h"
  348. #define DEF_DIR_LEVEL 0
  349. struct f2fs_inode_info {
  350. struct inode vfs_inode; /* serve a vfs inode */
  351. unsigned long i_flags; /* keep an inode flags for ioctl */
  352. unsigned char i_advise; /* use to give file attribute hints */
  353. unsigned char i_dir_level; /* use for dentry level for large dir */
  354. unsigned int i_current_depth; /* use only in directory structure */
  355. unsigned int i_pino; /* parent inode number */
  356. umode_t i_acl_mode; /* keep file acl mode temporarily */
  357. /* Use below internally in f2fs*/
  358. unsigned long flags; /* use to pass per-file flags */
  359. struct rw_semaphore i_sem; /* protect fi info */
  360. atomic_t dirty_pages; /* # of dirty pages */
  361. f2fs_hash_t chash; /* hash value of given file name */
  362. unsigned int clevel; /* maximum level of given file name */
  363. nid_t i_xattr_nid; /* node id that contains xattrs */
  364. unsigned long long xattr_ver; /* cp version of xattr modification */
  365. struct extent_info ext; /* in-memory extent cache entry */
  366. rwlock_t ext_lock; /* rwlock for single extent cache */
  367. struct inode_entry *dirty_dir; /* the pointer of dirty dir */
  368. struct radix_tree_root inmem_root; /* radix tree for inmem pages */
  369. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  370. struct mutex inmem_lock; /* lock for inmemory pages */
  371. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  372. /* Encryption params */
  373. struct f2fs_crypt_info *i_crypt_info;
  374. #endif
  375. };
  376. static inline void get_extent_info(struct extent_info *ext,
  377. struct f2fs_extent i_ext)
  378. {
  379. ext->fofs = le32_to_cpu(i_ext.fofs);
  380. ext->blk = le32_to_cpu(i_ext.blk);
  381. ext->len = le32_to_cpu(i_ext.len);
  382. }
  383. static inline void set_raw_extent(struct extent_info *ext,
  384. struct f2fs_extent *i_ext)
  385. {
  386. i_ext->fofs = cpu_to_le32(ext->fofs);
  387. i_ext->blk = cpu_to_le32(ext->blk);
  388. i_ext->len = cpu_to_le32(ext->len);
  389. }
  390. static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
  391. u32 blk, unsigned int len)
  392. {
  393. ei->fofs = fofs;
  394. ei->blk = blk;
  395. ei->len = len;
  396. }
  397. static inline bool __is_extent_same(struct extent_info *ei1,
  398. struct extent_info *ei2)
  399. {
  400. return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
  401. ei1->len == ei2->len);
  402. }
  403. static inline bool __is_extent_mergeable(struct extent_info *back,
  404. struct extent_info *front)
  405. {
  406. return (back->fofs + back->len == front->fofs &&
  407. back->blk + back->len == front->blk);
  408. }
  409. static inline bool __is_back_mergeable(struct extent_info *cur,
  410. struct extent_info *back)
  411. {
  412. return __is_extent_mergeable(back, cur);
  413. }
  414. static inline bool __is_front_mergeable(struct extent_info *cur,
  415. struct extent_info *front)
  416. {
  417. return __is_extent_mergeable(cur, front);
  418. }
  419. struct f2fs_nm_info {
  420. block_t nat_blkaddr; /* base disk address of NAT */
  421. nid_t max_nid; /* maximum possible node ids */
  422. nid_t available_nids; /* maximum available node ids */
  423. nid_t next_scan_nid; /* the next nid to be scanned */
  424. unsigned int ram_thresh; /* control the memory footprint */
  425. /* NAT cache management */
  426. struct radix_tree_root nat_root;/* root of the nat entry cache */
  427. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  428. struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
  429. struct list_head nat_entries; /* cached nat entry list (clean) */
  430. unsigned int nat_cnt; /* the # of cached nat entries */
  431. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  432. /* free node ids management */
  433. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  434. struct list_head free_nid_list; /* a list for free nids */
  435. spinlock_t free_nid_list_lock; /* protect free nid list */
  436. unsigned int fcnt; /* the number of free node id */
  437. struct mutex build_lock; /* lock for build free nids */
  438. /* for checkpoint */
  439. char *nat_bitmap; /* NAT bitmap pointer */
  440. int bitmap_size; /* bitmap size */
  441. };
  442. /*
  443. * this structure is used as one of function parameters.
  444. * all the information are dedicated to a given direct node block determined
  445. * by the data offset in a file.
  446. */
  447. struct dnode_of_data {
  448. struct inode *inode; /* vfs inode pointer */
  449. struct page *inode_page; /* its inode page, NULL is possible */
  450. struct page *node_page; /* cached direct node page */
  451. nid_t nid; /* node id of the direct node block */
  452. unsigned int ofs_in_node; /* data offset in the node page */
  453. bool inode_page_locked; /* inode page is locked or not */
  454. block_t data_blkaddr; /* block address of the node block */
  455. };
  456. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  457. struct page *ipage, struct page *npage, nid_t nid)
  458. {
  459. memset(dn, 0, sizeof(*dn));
  460. dn->inode = inode;
  461. dn->inode_page = ipage;
  462. dn->node_page = npage;
  463. dn->nid = nid;
  464. }
  465. /*
  466. * For SIT manager
  467. *
  468. * By default, there are 6 active log areas across the whole main area.
  469. * When considering hot and cold data separation to reduce cleaning overhead,
  470. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  471. * respectively.
  472. * In the current design, you should not change the numbers intentionally.
  473. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  474. * logs individually according to the underlying devices. (default: 6)
  475. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  476. * data and 8 for node logs.
  477. */
  478. #define NR_CURSEG_DATA_TYPE (3)
  479. #define NR_CURSEG_NODE_TYPE (3)
  480. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  481. enum {
  482. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  483. CURSEG_WARM_DATA, /* data blocks */
  484. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  485. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  486. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  487. CURSEG_COLD_NODE, /* indirect node blocks */
  488. NO_CHECK_TYPE,
  489. CURSEG_DIRECT_IO, /* to use for the direct IO path */
  490. };
  491. struct flush_cmd {
  492. struct completion wait;
  493. struct llist_node llnode;
  494. int ret;
  495. };
  496. struct flush_cmd_control {
  497. struct task_struct *f2fs_issue_flush; /* flush thread */
  498. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  499. struct llist_head issue_list; /* list for command issue */
  500. struct llist_node *dispatch_list; /* list for command dispatch */
  501. };
  502. struct f2fs_sm_info {
  503. struct sit_info *sit_info; /* whole segment information */
  504. struct free_segmap_info *free_info; /* free segment information */
  505. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  506. struct curseg_info *curseg_array; /* active segment information */
  507. block_t seg0_blkaddr; /* block address of 0'th segment */
  508. block_t main_blkaddr; /* start block address of main area */
  509. block_t ssa_blkaddr; /* start block address of SSA area */
  510. unsigned int segment_count; /* total # of segments */
  511. unsigned int main_segments; /* # of segments in main area */
  512. unsigned int reserved_segments; /* # of reserved segments */
  513. unsigned int ovp_segments; /* # of overprovision segments */
  514. /* a threshold to reclaim prefree segments */
  515. unsigned int rec_prefree_segments;
  516. /* for small discard management */
  517. struct list_head discard_list; /* 4KB discard list */
  518. int nr_discards; /* # of discards in the list */
  519. int max_discards; /* max. discards to be issued */
  520. /* for batched trimming */
  521. unsigned int trim_sections; /* # of sections to trim */
  522. struct list_head sit_entry_set; /* sit entry set list */
  523. unsigned int ipu_policy; /* in-place-update policy */
  524. unsigned int min_ipu_util; /* in-place-update threshold */
  525. unsigned int min_fsync_blocks; /* threshold for fsync */
  526. /* for flush command control */
  527. struct flush_cmd_control *cmd_control_info;
  528. };
  529. /*
  530. * For superblock
  531. */
  532. /*
  533. * COUNT_TYPE for monitoring
  534. *
  535. * f2fs monitors the number of several block types such as on-writeback,
  536. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  537. */
  538. enum count_type {
  539. F2FS_WRITEBACK,
  540. F2FS_DIRTY_DENTS,
  541. F2FS_DIRTY_NODES,
  542. F2FS_DIRTY_META,
  543. F2FS_INMEM_PAGES,
  544. NR_COUNT_TYPE,
  545. };
  546. /*
  547. * The below are the page types of bios used in submit_bio().
  548. * The available types are:
  549. * DATA User data pages. It operates as async mode.
  550. * NODE Node pages. It operates as async mode.
  551. * META FS metadata pages such as SIT, NAT, CP.
  552. * NR_PAGE_TYPE The number of page types.
  553. * META_FLUSH Make sure the previous pages are written
  554. * with waiting the bio's completion
  555. * ... Only can be used with META.
  556. */
  557. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  558. enum page_type {
  559. DATA,
  560. NODE,
  561. META,
  562. NR_PAGE_TYPE,
  563. META_FLUSH,
  564. INMEM, /* the below types are used by tracepoints only. */
  565. INMEM_DROP,
  566. IPU,
  567. OPU,
  568. };
  569. struct f2fs_io_info {
  570. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  571. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  572. int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
  573. block_t blk_addr; /* block address to be written */
  574. struct page *page; /* page to be written */
  575. struct page *encrypted_page; /* encrypted page */
  576. };
  577. #define is_read_io(rw) (((rw) & 1) == READ)
  578. struct f2fs_bio_info {
  579. struct f2fs_sb_info *sbi; /* f2fs superblock */
  580. struct bio *bio; /* bios to merge */
  581. sector_t last_block_in_bio; /* last block number */
  582. struct f2fs_io_info fio; /* store buffered io info. */
  583. struct rw_semaphore io_rwsem; /* blocking op for bio */
  584. };
  585. /* for inner inode cache management */
  586. struct inode_management {
  587. struct radix_tree_root ino_root; /* ino entry array */
  588. spinlock_t ino_lock; /* for ino entry lock */
  589. struct list_head ino_list; /* inode list head */
  590. unsigned long ino_num; /* number of entries */
  591. };
  592. /* For s_flag in struct f2fs_sb_info */
  593. enum {
  594. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  595. SBI_IS_CLOSE, /* specify unmounting */
  596. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  597. SBI_POR_DOING, /* recovery is doing or not */
  598. };
  599. struct f2fs_sb_info {
  600. struct super_block *sb; /* pointer to VFS super block */
  601. struct proc_dir_entry *s_proc; /* proc entry */
  602. struct buffer_head *raw_super_buf; /* buffer head of raw sb */
  603. struct f2fs_super_block *raw_super; /* raw super block pointer */
  604. int s_flag; /* flags for sbi */
  605. /* for node-related operations */
  606. struct f2fs_nm_info *nm_info; /* node manager */
  607. struct inode *node_inode; /* cache node blocks */
  608. /* for segment-related operations */
  609. struct f2fs_sm_info *sm_info; /* segment manager */
  610. /* for bio operations */
  611. struct f2fs_bio_info read_io; /* for read bios */
  612. struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
  613. /* for checkpoint */
  614. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  615. struct inode *meta_inode; /* cache meta blocks */
  616. struct mutex cp_mutex; /* checkpoint procedure lock */
  617. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  618. struct rw_semaphore node_write; /* locking node writes */
  619. struct mutex writepages; /* mutex for writepages() */
  620. wait_queue_head_t cp_wait;
  621. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  622. /* for orphan inode, use 0'th array */
  623. unsigned int max_orphans; /* max orphan inodes */
  624. /* for directory inode management */
  625. struct list_head dir_inode_list; /* dir inode list */
  626. spinlock_t dir_inode_lock; /* for dir inode list lock */
  627. /* for extent tree cache */
  628. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  629. struct rw_semaphore extent_tree_lock; /* locking extent radix tree */
  630. struct list_head extent_list; /* lru list for shrinker */
  631. spinlock_t extent_lock; /* locking extent lru list */
  632. int total_ext_tree; /* extent tree count */
  633. atomic_t total_ext_node; /* extent info count */
  634. /* basic filesystem units */
  635. unsigned int log_sectors_per_block; /* log2 sectors per block */
  636. unsigned int log_blocksize; /* log2 block size */
  637. unsigned int blocksize; /* block size */
  638. unsigned int root_ino_num; /* root inode number*/
  639. unsigned int node_ino_num; /* node inode number*/
  640. unsigned int meta_ino_num; /* meta inode number*/
  641. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  642. unsigned int blocks_per_seg; /* blocks per segment */
  643. unsigned int segs_per_sec; /* segments per section */
  644. unsigned int secs_per_zone; /* sections per zone */
  645. unsigned int total_sections; /* total section count */
  646. unsigned int total_node_count; /* total node block count */
  647. unsigned int total_valid_node_count; /* valid node block count */
  648. unsigned int total_valid_inode_count; /* valid inode count */
  649. int active_logs; /* # of active logs */
  650. int dir_level; /* directory level */
  651. block_t user_block_count; /* # of user blocks */
  652. block_t total_valid_block_count; /* # of valid blocks */
  653. block_t alloc_valid_block_count; /* # of allocated blocks */
  654. block_t discard_blks; /* discard command candidats */
  655. block_t last_valid_block_count; /* for recovery */
  656. u32 s_next_generation; /* for NFS support */
  657. atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
  658. struct f2fs_mount_info mount_opt; /* mount options */
  659. /* for cleaning operations */
  660. struct mutex gc_mutex; /* mutex for GC */
  661. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  662. unsigned int cur_victim_sec; /* current victim section num */
  663. /* maximum # of trials to find a victim segment for SSR and GC */
  664. unsigned int max_victim_search;
  665. /*
  666. * for stat information.
  667. * one is for the LFS mode, and the other is for the SSR mode.
  668. */
  669. #ifdef CONFIG_F2FS_STAT_FS
  670. struct f2fs_stat_info *stat_info; /* FS status information */
  671. unsigned int segment_count[2]; /* # of allocated segments */
  672. unsigned int block_count[2]; /* # of allocated blocks */
  673. atomic_t inplace_count; /* # of inplace update */
  674. int total_hit_ext, read_hit_ext; /* extent cache hit ratio */
  675. atomic_t inline_inode; /* # of inline_data inodes */
  676. atomic_t inline_dir; /* # of inline_dentry inodes */
  677. int bg_gc; /* background gc calls */
  678. unsigned int n_dirty_dirs; /* # of dir inodes */
  679. #endif
  680. unsigned int last_victim[2]; /* last victim segment # */
  681. spinlock_t stat_lock; /* lock for stat operations */
  682. /* For sysfs suppport */
  683. struct kobject s_kobj;
  684. struct completion s_kobj_unregister;
  685. };
  686. /*
  687. * Inline functions
  688. */
  689. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  690. {
  691. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  692. }
  693. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  694. {
  695. return sb->s_fs_info;
  696. }
  697. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  698. {
  699. return F2FS_SB(inode->i_sb);
  700. }
  701. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  702. {
  703. return F2FS_I_SB(mapping->host);
  704. }
  705. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  706. {
  707. return F2FS_M_SB(page->mapping);
  708. }
  709. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  710. {
  711. return (struct f2fs_super_block *)(sbi->raw_super);
  712. }
  713. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  714. {
  715. return (struct f2fs_checkpoint *)(sbi->ckpt);
  716. }
  717. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  718. {
  719. return (struct f2fs_node *)page_address(page);
  720. }
  721. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  722. {
  723. return &((struct f2fs_node *)page_address(page))->i;
  724. }
  725. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  726. {
  727. return (struct f2fs_nm_info *)(sbi->nm_info);
  728. }
  729. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  730. {
  731. return (struct f2fs_sm_info *)(sbi->sm_info);
  732. }
  733. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  734. {
  735. return (struct sit_info *)(SM_I(sbi)->sit_info);
  736. }
  737. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  738. {
  739. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  740. }
  741. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  742. {
  743. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  744. }
  745. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  746. {
  747. return sbi->meta_inode->i_mapping;
  748. }
  749. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  750. {
  751. return sbi->node_inode->i_mapping;
  752. }
  753. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  754. {
  755. return sbi->s_flag & (0x01 << type);
  756. }
  757. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  758. {
  759. sbi->s_flag |= (0x01 << type);
  760. }
  761. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  762. {
  763. sbi->s_flag &= ~(0x01 << type);
  764. }
  765. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  766. {
  767. return le64_to_cpu(cp->checkpoint_ver);
  768. }
  769. static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  770. {
  771. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  772. return ckpt_flags & f;
  773. }
  774. static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  775. {
  776. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  777. ckpt_flags |= f;
  778. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  779. }
  780. static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  781. {
  782. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  783. ckpt_flags &= (~f);
  784. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  785. }
  786. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  787. {
  788. down_read(&sbi->cp_rwsem);
  789. }
  790. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  791. {
  792. up_read(&sbi->cp_rwsem);
  793. }
  794. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  795. {
  796. f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  797. }
  798. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  799. {
  800. up_write(&sbi->cp_rwsem);
  801. }
  802. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  803. {
  804. int reason = CP_SYNC;
  805. if (test_opt(sbi, FASTBOOT))
  806. reason = CP_FASTBOOT;
  807. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  808. reason = CP_UMOUNT;
  809. return reason;
  810. }
  811. static inline bool __remain_node_summaries(int reason)
  812. {
  813. return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
  814. }
  815. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  816. {
  817. return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
  818. is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
  819. }
  820. /*
  821. * Check whether the given nid is within node id range.
  822. */
  823. static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  824. {
  825. if (unlikely(nid < F2FS_ROOT_INO(sbi)))
  826. return -EINVAL;
  827. if (unlikely(nid >= NM_I(sbi)->max_nid))
  828. return -EINVAL;
  829. return 0;
  830. }
  831. #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
  832. /*
  833. * Check whether the inode has blocks or not
  834. */
  835. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  836. {
  837. if (F2FS_I(inode)->i_xattr_nid)
  838. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
  839. else
  840. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
  841. }
  842. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  843. {
  844. return ofs == XATTR_NODE_OFFSET;
  845. }
  846. static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
  847. struct inode *inode, blkcnt_t count)
  848. {
  849. block_t valid_block_count;
  850. spin_lock(&sbi->stat_lock);
  851. valid_block_count =
  852. sbi->total_valid_block_count + (block_t)count;
  853. if (unlikely(valid_block_count > sbi->user_block_count)) {
  854. spin_unlock(&sbi->stat_lock);
  855. return false;
  856. }
  857. inode->i_blocks += count;
  858. sbi->total_valid_block_count = valid_block_count;
  859. sbi->alloc_valid_block_count += (block_t)count;
  860. spin_unlock(&sbi->stat_lock);
  861. return true;
  862. }
  863. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  864. struct inode *inode,
  865. blkcnt_t count)
  866. {
  867. spin_lock(&sbi->stat_lock);
  868. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  869. f2fs_bug_on(sbi, inode->i_blocks < count);
  870. inode->i_blocks -= count;
  871. sbi->total_valid_block_count -= (block_t)count;
  872. spin_unlock(&sbi->stat_lock);
  873. }
  874. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  875. {
  876. atomic_inc(&sbi->nr_pages[count_type]);
  877. set_sbi_flag(sbi, SBI_IS_DIRTY);
  878. }
  879. static inline void inode_inc_dirty_pages(struct inode *inode)
  880. {
  881. atomic_inc(&F2FS_I(inode)->dirty_pages);
  882. if (S_ISDIR(inode->i_mode))
  883. inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  884. }
  885. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  886. {
  887. atomic_dec(&sbi->nr_pages[count_type]);
  888. }
  889. static inline void inode_dec_dirty_pages(struct inode *inode)
  890. {
  891. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode))
  892. return;
  893. atomic_dec(&F2FS_I(inode)->dirty_pages);
  894. if (S_ISDIR(inode->i_mode))
  895. dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  896. }
  897. static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
  898. {
  899. return atomic_read(&sbi->nr_pages[count_type]);
  900. }
  901. static inline int get_dirty_pages(struct inode *inode)
  902. {
  903. return atomic_read(&F2FS_I(inode)->dirty_pages);
  904. }
  905. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  906. {
  907. unsigned int pages_per_sec = sbi->segs_per_sec *
  908. (1 << sbi->log_blocks_per_seg);
  909. return ((get_pages(sbi, block_type) + pages_per_sec - 1)
  910. >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
  911. }
  912. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  913. {
  914. return sbi->total_valid_block_count;
  915. }
  916. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  917. {
  918. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  919. /* return NAT or SIT bitmap */
  920. if (flag == NAT_BITMAP)
  921. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  922. else if (flag == SIT_BITMAP)
  923. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  924. return 0;
  925. }
  926. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  927. {
  928. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  929. }
  930. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  931. {
  932. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  933. int offset;
  934. if (__cp_payload(sbi) > 0) {
  935. if (flag == NAT_BITMAP)
  936. return &ckpt->sit_nat_version_bitmap;
  937. else
  938. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  939. } else {
  940. offset = (flag == NAT_BITMAP) ?
  941. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  942. return &ckpt->sit_nat_version_bitmap + offset;
  943. }
  944. }
  945. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  946. {
  947. block_t start_addr;
  948. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  949. unsigned long long ckpt_version = cur_cp_version(ckpt);
  950. start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  951. /*
  952. * odd numbered checkpoint should at cp segment 0
  953. * and even segment must be at cp segment 1
  954. */
  955. if (!(ckpt_version & 1))
  956. start_addr += sbi->blocks_per_seg;
  957. return start_addr;
  958. }
  959. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  960. {
  961. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  962. }
  963. static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
  964. struct inode *inode)
  965. {
  966. block_t valid_block_count;
  967. unsigned int valid_node_count;
  968. spin_lock(&sbi->stat_lock);
  969. valid_block_count = sbi->total_valid_block_count + 1;
  970. if (unlikely(valid_block_count > sbi->user_block_count)) {
  971. spin_unlock(&sbi->stat_lock);
  972. return false;
  973. }
  974. valid_node_count = sbi->total_valid_node_count + 1;
  975. if (unlikely(valid_node_count > sbi->total_node_count)) {
  976. spin_unlock(&sbi->stat_lock);
  977. return false;
  978. }
  979. if (inode)
  980. inode->i_blocks++;
  981. sbi->alloc_valid_block_count++;
  982. sbi->total_valid_node_count++;
  983. sbi->total_valid_block_count++;
  984. spin_unlock(&sbi->stat_lock);
  985. return true;
  986. }
  987. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  988. struct inode *inode)
  989. {
  990. spin_lock(&sbi->stat_lock);
  991. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  992. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  993. f2fs_bug_on(sbi, !inode->i_blocks);
  994. inode->i_blocks--;
  995. sbi->total_valid_node_count--;
  996. sbi->total_valid_block_count--;
  997. spin_unlock(&sbi->stat_lock);
  998. }
  999. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  1000. {
  1001. return sbi->total_valid_node_count;
  1002. }
  1003. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  1004. {
  1005. spin_lock(&sbi->stat_lock);
  1006. f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
  1007. sbi->total_valid_inode_count++;
  1008. spin_unlock(&sbi->stat_lock);
  1009. }
  1010. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  1011. {
  1012. spin_lock(&sbi->stat_lock);
  1013. f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
  1014. sbi->total_valid_inode_count--;
  1015. spin_unlock(&sbi->stat_lock);
  1016. }
  1017. static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
  1018. {
  1019. return sbi->total_valid_inode_count;
  1020. }
  1021. static inline void f2fs_put_page(struct page *page, int unlock)
  1022. {
  1023. if (!page)
  1024. return;
  1025. if (unlock) {
  1026. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  1027. unlock_page(page);
  1028. }
  1029. page_cache_release(page);
  1030. }
  1031. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  1032. {
  1033. if (dn->node_page)
  1034. f2fs_put_page(dn->node_page, 1);
  1035. if (dn->inode_page && dn->node_page != dn->inode_page)
  1036. f2fs_put_page(dn->inode_page, 0);
  1037. dn->node_page = NULL;
  1038. dn->inode_page = NULL;
  1039. }
  1040. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  1041. size_t size)
  1042. {
  1043. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1044. }
  1045. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  1046. gfp_t flags)
  1047. {
  1048. void *entry;
  1049. retry:
  1050. entry = kmem_cache_alloc(cachep, flags);
  1051. if (!entry) {
  1052. cond_resched();
  1053. goto retry;
  1054. }
  1055. return entry;
  1056. }
  1057. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  1058. unsigned long index, void *item)
  1059. {
  1060. while (radix_tree_insert(root, index, item))
  1061. cond_resched();
  1062. }
  1063. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  1064. static inline bool IS_INODE(struct page *page)
  1065. {
  1066. struct f2fs_node *p = F2FS_NODE(page);
  1067. return RAW_IS_INODE(p);
  1068. }
  1069. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  1070. {
  1071. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  1072. }
  1073. static inline block_t datablock_addr(struct page *node_page,
  1074. unsigned int offset)
  1075. {
  1076. struct f2fs_node *raw_node;
  1077. __le32 *addr_array;
  1078. raw_node = F2FS_NODE(node_page);
  1079. addr_array = blkaddr_in_node(raw_node);
  1080. return le32_to_cpu(addr_array[offset]);
  1081. }
  1082. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  1083. {
  1084. int mask;
  1085. addr += (nr >> 3);
  1086. mask = 1 << (7 - (nr & 0x07));
  1087. return mask & *addr;
  1088. }
  1089. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  1090. {
  1091. int mask;
  1092. addr += (nr >> 3);
  1093. mask = 1 << (7 - (nr & 0x07));
  1094. *addr |= mask;
  1095. }
  1096. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  1097. {
  1098. int mask;
  1099. addr += (nr >> 3);
  1100. mask = 1 << (7 - (nr & 0x07));
  1101. *addr &= ~mask;
  1102. }
  1103. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  1104. {
  1105. int mask;
  1106. int ret;
  1107. addr += (nr >> 3);
  1108. mask = 1 << (7 - (nr & 0x07));
  1109. ret = mask & *addr;
  1110. *addr |= mask;
  1111. return ret;
  1112. }
  1113. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  1114. {
  1115. int mask;
  1116. int ret;
  1117. addr += (nr >> 3);
  1118. mask = 1 << (7 - (nr & 0x07));
  1119. ret = mask & *addr;
  1120. *addr &= ~mask;
  1121. return ret;
  1122. }
  1123. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  1124. {
  1125. int mask;
  1126. addr += (nr >> 3);
  1127. mask = 1 << (7 - (nr & 0x07));
  1128. *addr ^= mask;
  1129. }
  1130. /* used for f2fs_inode_info->flags */
  1131. enum {
  1132. FI_NEW_INODE, /* indicate newly allocated inode */
  1133. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  1134. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  1135. FI_INC_LINK, /* need to increment i_nlink */
  1136. FI_ACL_MODE, /* indicate acl mode */
  1137. FI_NO_ALLOC, /* should not allocate any blocks */
  1138. FI_UPDATE_DIR, /* should update inode block for consistency */
  1139. FI_DELAY_IPUT, /* used for the recovery */
  1140. FI_NO_EXTENT, /* not to use the extent cache */
  1141. FI_INLINE_XATTR, /* used for inline xattr */
  1142. FI_INLINE_DATA, /* used for inline data*/
  1143. FI_INLINE_DENTRY, /* used for inline dentry */
  1144. FI_APPEND_WRITE, /* inode has appended data */
  1145. FI_UPDATE_WRITE, /* inode has in-place-update data */
  1146. FI_NEED_IPU, /* used for ipu per file */
  1147. FI_ATOMIC_FILE, /* indicate atomic file */
  1148. FI_VOLATILE_FILE, /* indicate volatile file */
  1149. FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
  1150. FI_DROP_CACHE, /* drop dirty page cache */
  1151. FI_DATA_EXIST, /* indicate data exists */
  1152. FI_INLINE_DOTS, /* indicate inline dot dentries */
  1153. };
  1154. static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
  1155. {
  1156. if (!test_bit(flag, &fi->flags))
  1157. set_bit(flag, &fi->flags);
  1158. }
  1159. static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
  1160. {
  1161. return test_bit(flag, &fi->flags);
  1162. }
  1163. static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  1164. {
  1165. if (test_bit(flag, &fi->flags))
  1166. clear_bit(flag, &fi->flags);
  1167. }
  1168. static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
  1169. {
  1170. fi->i_acl_mode = mode;
  1171. set_inode_flag(fi, FI_ACL_MODE);
  1172. }
  1173. static inline void get_inline_info(struct f2fs_inode_info *fi,
  1174. struct f2fs_inode *ri)
  1175. {
  1176. if (ri->i_inline & F2FS_INLINE_XATTR)
  1177. set_inode_flag(fi, FI_INLINE_XATTR);
  1178. if (ri->i_inline & F2FS_INLINE_DATA)
  1179. set_inode_flag(fi, FI_INLINE_DATA);
  1180. if (ri->i_inline & F2FS_INLINE_DENTRY)
  1181. set_inode_flag(fi, FI_INLINE_DENTRY);
  1182. if (ri->i_inline & F2FS_DATA_EXIST)
  1183. set_inode_flag(fi, FI_DATA_EXIST);
  1184. if (ri->i_inline & F2FS_INLINE_DOTS)
  1185. set_inode_flag(fi, FI_INLINE_DOTS);
  1186. }
  1187. static inline void set_raw_inline(struct f2fs_inode_info *fi,
  1188. struct f2fs_inode *ri)
  1189. {
  1190. ri->i_inline = 0;
  1191. if (is_inode_flag_set(fi, FI_INLINE_XATTR))
  1192. ri->i_inline |= F2FS_INLINE_XATTR;
  1193. if (is_inode_flag_set(fi, FI_INLINE_DATA))
  1194. ri->i_inline |= F2FS_INLINE_DATA;
  1195. if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
  1196. ri->i_inline |= F2FS_INLINE_DENTRY;
  1197. if (is_inode_flag_set(fi, FI_DATA_EXIST))
  1198. ri->i_inline |= F2FS_DATA_EXIST;
  1199. if (is_inode_flag_set(fi, FI_INLINE_DOTS))
  1200. ri->i_inline |= F2FS_INLINE_DOTS;
  1201. }
  1202. static inline int f2fs_has_inline_xattr(struct inode *inode)
  1203. {
  1204. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
  1205. }
  1206. static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
  1207. {
  1208. if (f2fs_has_inline_xattr(&fi->vfs_inode))
  1209. return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
  1210. return DEF_ADDRS_PER_INODE;
  1211. }
  1212. static inline void *inline_xattr_addr(struct page *page)
  1213. {
  1214. struct f2fs_inode *ri = F2FS_INODE(page);
  1215. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  1216. F2FS_INLINE_XATTR_ADDRS]);
  1217. }
  1218. static inline int inline_xattr_size(struct inode *inode)
  1219. {
  1220. if (f2fs_has_inline_xattr(inode))
  1221. return F2FS_INLINE_XATTR_ADDRS << 2;
  1222. else
  1223. return 0;
  1224. }
  1225. static inline int f2fs_has_inline_data(struct inode *inode)
  1226. {
  1227. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
  1228. }
  1229. static inline void f2fs_clear_inline_inode(struct inode *inode)
  1230. {
  1231. clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
  1232. clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  1233. }
  1234. static inline int f2fs_exist_data(struct inode *inode)
  1235. {
  1236. return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
  1237. }
  1238. static inline int f2fs_has_inline_dots(struct inode *inode)
  1239. {
  1240. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
  1241. }
  1242. static inline bool f2fs_is_atomic_file(struct inode *inode)
  1243. {
  1244. return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
  1245. }
  1246. static inline bool f2fs_is_volatile_file(struct inode *inode)
  1247. {
  1248. return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
  1249. }
  1250. static inline bool f2fs_is_first_block_written(struct inode *inode)
  1251. {
  1252. return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
  1253. }
  1254. static inline bool f2fs_is_drop_cache(struct inode *inode)
  1255. {
  1256. return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
  1257. }
  1258. static inline void *inline_data_addr(struct page *page)
  1259. {
  1260. struct f2fs_inode *ri = F2FS_INODE(page);
  1261. return (void *)&(ri->i_addr[1]);
  1262. }
  1263. static inline int f2fs_has_inline_dentry(struct inode *inode)
  1264. {
  1265. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
  1266. }
  1267. static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
  1268. {
  1269. if (!f2fs_has_inline_dentry(dir))
  1270. kunmap(page);
  1271. }
  1272. static inline int is_file(struct inode *inode, int type)
  1273. {
  1274. return F2FS_I(inode)->i_advise & type;
  1275. }
  1276. static inline void set_file(struct inode *inode, int type)
  1277. {
  1278. F2FS_I(inode)->i_advise |= type;
  1279. }
  1280. static inline void clear_file(struct inode *inode, int type)
  1281. {
  1282. F2FS_I(inode)->i_advise &= ~type;
  1283. }
  1284. static inline int f2fs_readonly(struct super_block *sb)
  1285. {
  1286. return sb->s_flags & MS_RDONLY;
  1287. }
  1288. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  1289. {
  1290. return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  1291. }
  1292. static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
  1293. {
  1294. set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  1295. sbi->sb->s_flags |= MS_RDONLY;
  1296. }
  1297. static inline bool is_dot_dotdot(const struct qstr *str)
  1298. {
  1299. if (str->len == 1 && str->name[0] == '.')
  1300. return true;
  1301. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  1302. return true;
  1303. return false;
  1304. }
  1305. #define get_inode_mode(i) \
  1306. ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
  1307. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  1308. /* get offset of first page in next direct node */
  1309. #define PGOFS_OF_NEXT_DNODE(pgofs, fi) \
  1310. ((pgofs < ADDRS_PER_INODE(fi)) ? ADDRS_PER_INODE(fi) : \
  1311. (pgofs - ADDRS_PER_INODE(fi) + ADDRS_PER_BLOCK) / \
  1312. ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi))
  1313. /*
  1314. * file.c
  1315. */
  1316. int f2fs_sync_file(struct file *, loff_t, loff_t, int);
  1317. void truncate_data_blocks(struct dnode_of_data *);
  1318. int truncate_blocks(struct inode *, u64, bool);
  1319. void f2fs_truncate(struct inode *);
  1320. int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
  1321. int f2fs_setattr(struct dentry *, struct iattr *);
  1322. int truncate_hole(struct inode *, pgoff_t, pgoff_t);
  1323. int truncate_data_blocks_range(struct dnode_of_data *, int);
  1324. long f2fs_ioctl(struct file *, unsigned int, unsigned long);
  1325. long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
  1326. /*
  1327. * inode.c
  1328. */
  1329. void f2fs_set_inode_flags(struct inode *);
  1330. struct inode *f2fs_iget(struct super_block *, unsigned long);
  1331. int try_to_free_nats(struct f2fs_sb_info *, int);
  1332. void update_inode(struct inode *, struct page *);
  1333. void update_inode_page(struct inode *);
  1334. int f2fs_write_inode(struct inode *, struct writeback_control *);
  1335. void f2fs_evict_inode(struct inode *);
  1336. void handle_failed_inode(struct inode *);
  1337. /*
  1338. * namei.c
  1339. */
  1340. struct dentry *f2fs_get_parent(struct dentry *child);
  1341. /*
  1342. * dir.c
  1343. */
  1344. extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
  1345. void set_de_type(struct f2fs_dir_entry *, umode_t);
  1346. struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *,
  1347. f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
  1348. bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
  1349. unsigned int, struct f2fs_str *);
  1350. void do_make_empty_dir(struct inode *, struct inode *,
  1351. struct f2fs_dentry_ptr *);
  1352. struct page *init_inode_metadata(struct inode *, struct inode *,
  1353. const struct qstr *, struct page *);
  1354. void update_parent_metadata(struct inode *, struct inode *, unsigned int);
  1355. int room_for_filename(const void *, int, int);
  1356. void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
  1357. struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
  1358. struct page **);
  1359. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
  1360. ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
  1361. void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
  1362. struct page *, struct inode *);
  1363. int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
  1364. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
  1365. const struct qstr *, f2fs_hash_t , unsigned int);
  1366. int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
  1367. umode_t);
  1368. void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
  1369. struct inode *);
  1370. int f2fs_do_tmpfile(struct inode *, struct inode *);
  1371. bool f2fs_empty_dir(struct inode *);
  1372. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  1373. {
  1374. return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  1375. inode, inode->i_ino, inode->i_mode);
  1376. }
  1377. /*
  1378. * super.c
  1379. */
  1380. int f2fs_commit_super(struct f2fs_sb_info *, bool);
  1381. int f2fs_sync_fs(struct super_block *, int);
  1382. extern __printf(3, 4)
  1383. void f2fs_msg(struct super_block *, const char *, const char *, ...);
  1384. /*
  1385. * hash.c
  1386. */
  1387. f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
  1388. /*
  1389. * node.c
  1390. */
  1391. struct dnode_of_data;
  1392. struct node_info;
  1393. bool available_free_memory(struct f2fs_sb_info *, int);
  1394. int need_dentry_mark(struct f2fs_sb_info *, nid_t);
  1395. bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
  1396. bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
  1397. void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
  1398. int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
  1399. int truncate_inode_blocks(struct inode *, pgoff_t);
  1400. int truncate_xattr_node(struct inode *, struct page *);
  1401. int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
  1402. void remove_inode_page(struct inode *);
  1403. struct page *new_inode_page(struct inode *);
  1404. struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
  1405. void ra_node_page(struct f2fs_sb_info *, nid_t);
  1406. struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
  1407. struct page *get_node_page_ra(struct page *, int);
  1408. void sync_inode_page(struct dnode_of_data *);
  1409. int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
  1410. bool alloc_nid(struct f2fs_sb_info *, nid_t *);
  1411. void alloc_nid_done(struct f2fs_sb_info *, nid_t);
  1412. void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
  1413. void recover_inline_xattr(struct inode *, struct page *);
  1414. void recover_xattr_data(struct inode *, struct page *, block_t);
  1415. int recover_inode_page(struct f2fs_sb_info *, struct page *);
  1416. int restore_node_summary(struct f2fs_sb_info *, unsigned int,
  1417. struct f2fs_summary_block *);
  1418. void flush_nat_entries(struct f2fs_sb_info *);
  1419. int build_node_manager(struct f2fs_sb_info *);
  1420. void destroy_node_manager(struct f2fs_sb_info *);
  1421. int __init create_node_manager_caches(void);
  1422. void destroy_node_manager_caches(void);
  1423. /*
  1424. * segment.c
  1425. */
  1426. void register_inmem_page(struct inode *, struct page *);
  1427. void commit_inmem_pages(struct inode *, bool);
  1428. void f2fs_balance_fs(struct f2fs_sb_info *);
  1429. void f2fs_balance_fs_bg(struct f2fs_sb_info *);
  1430. int f2fs_issue_flush(struct f2fs_sb_info *);
  1431. int create_flush_cmd_control(struct f2fs_sb_info *);
  1432. void destroy_flush_cmd_control(struct f2fs_sb_info *);
  1433. void invalidate_blocks(struct f2fs_sb_info *, block_t);
  1434. void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
  1435. void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
  1436. void release_discard_addrs(struct f2fs_sb_info *);
  1437. void discard_next_dnode(struct f2fs_sb_info *, block_t);
  1438. int npages_for_summary_flush(struct f2fs_sb_info *, bool);
  1439. void allocate_new_segments(struct f2fs_sb_info *);
  1440. int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
  1441. struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
  1442. void update_meta_page(struct f2fs_sb_info *, void *, block_t);
  1443. void write_meta_page(struct f2fs_sb_info *, struct page *);
  1444. void write_node_page(unsigned int, struct f2fs_io_info *);
  1445. void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
  1446. void rewrite_data_page(struct f2fs_io_info *);
  1447. void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
  1448. block_t, block_t, unsigned char, bool);
  1449. void allocate_data_block(struct f2fs_sb_info *, struct page *,
  1450. block_t, block_t *, struct f2fs_summary *, int);
  1451. void f2fs_wait_on_page_writeback(struct page *, enum page_type);
  1452. void write_data_summaries(struct f2fs_sb_info *, block_t);
  1453. void write_node_summaries(struct f2fs_sb_info *, block_t);
  1454. int lookup_journal_in_cursum(struct f2fs_summary_block *,
  1455. int, unsigned int, int);
  1456. void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
  1457. int build_segment_manager(struct f2fs_sb_info *);
  1458. void destroy_segment_manager(struct f2fs_sb_info *);
  1459. int __init create_segment_manager_caches(void);
  1460. void destroy_segment_manager_caches(void);
  1461. /*
  1462. * checkpoint.c
  1463. */
  1464. struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
  1465. struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
  1466. bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
  1467. int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int);
  1468. void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
  1469. long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
  1470. void add_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1471. void remove_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1472. void release_dirty_inode(struct f2fs_sb_info *);
  1473. bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
  1474. int acquire_orphan_inode(struct f2fs_sb_info *);
  1475. void release_orphan_inode(struct f2fs_sb_info *);
  1476. void add_orphan_inode(struct f2fs_sb_info *, nid_t);
  1477. void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
  1478. void recover_orphan_inodes(struct f2fs_sb_info *);
  1479. int get_valid_checkpoint(struct f2fs_sb_info *);
  1480. void update_dirty_page(struct inode *, struct page *);
  1481. void add_dirty_dir_inode(struct inode *);
  1482. void remove_dirty_dir_inode(struct inode *);
  1483. void sync_dirty_dir_inodes(struct f2fs_sb_info *);
  1484. void write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
  1485. void init_ino_entry_info(struct f2fs_sb_info *);
  1486. int __init create_checkpoint_caches(void);
  1487. void destroy_checkpoint_caches(void);
  1488. /*
  1489. * data.c
  1490. */
  1491. void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
  1492. int f2fs_submit_page_bio(struct f2fs_io_info *);
  1493. void f2fs_submit_page_mbio(struct f2fs_io_info *);
  1494. void set_data_blkaddr(struct dnode_of_data *);
  1495. int reserve_new_block(struct dnode_of_data *);
  1496. int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
  1497. void f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
  1498. void f2fs_destroy_extent_tree(struct inode *);
  1499. void f2fs_init_extent_cache(struct inode *, struct f2fs_extent *);
  1500. void f2fs_update_extent_cache(struct dnode_of_data *);
  1501. void f2fs_preserve_extent_tree(struct inode *);
  1502. struct page *get_read_data_page(struct inode *, pgoff_t, int);
  1503. struct page *find_data_page(struct inode *, pgoff_t);
  1504. struct page *get_lock_data_page(struct inode *, pgoff_t);
  1505. struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
  1506. int do_write_data_page(struct f2fs_io_info *);
  1507. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
  1508. void init_extent_cache_info(struct f2fs_sb_info *);
  1509. int __init create_extent_cache(void);
  1510. void destroy_extent_cache(void);
  1511. void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
  1512. int f2fs_release_page(struct page *, gfp_t);
  1513. /*
  1514. * gc.c
  1515. */
  1516. int start_gc_thread(struct f2fs_sb_info *);
  1517. void stop_gc_thread(struct f2fs_sb_info *);
  1518. block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
  1519. int f2fs_gc(struct f2fs_sb_info *);
  1520. void build_gc_manager(struct f2fs_sb_info *);
  1521. /*
  1522. * recovery.c
  1523. */
  1524. int recover_fsync_data(struct f2fs_sb_info *);
  1525. bool space_for_roll_forward(struct f2fs_sb_info *);
  1526. /*
  1527. * debug.c
  1528. */
  1529. #ifdef CONFIG_F2FS_STAT_FS
  1530. struct f2fs_stat_info {
  1531. struct list_head stat_list;
  1532. struct f2fs_sb_info *sbi;
  1533. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  1534. int main_area_segs, main_area_sections, main_area_zones;
  1535. int hit_ext, total_ext, ext_tree, ext_node;
  1536. int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
  1537. int nats, dirty_nats, sits, dirty_sits, fnids;
  1538. int total_count, utilization;
  1539. int bg_gc, inline_inode, inline_dir, inmem_pages, wb_pages;
  1540. unsigned int valid_count, valid_node_count, valid_inode_count;
  1541. unsigned int bimodal, avg_vblocks;
  1542. int util_free, util_valid, util_invalid;
  1543. int rsvd_segs, overp_segs;
  1544. int dirty_count, node_pages, meta_pages;
  1545. int prefree_count, call_count, cp_count;
  1546. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  1547. int bg_node_segs, bg_data_segs;
  1548. int tot_blks, data_blks, node_blks;
  1549. int bg_data_blks, bg_node_blks;
  1550. int curseg[NR_CURSEG_TYPE];
  1551. int cursec[NR_CURSEG_TYPE];
  1552. int curzone[NR_CURSEG_TYPE];
  1553. unsigned int segment_count[2];
  1554. unsigned int block_count[2];
  1555. unsigned int inplace_count;
  1556. unsigned base_mem, cache_mem, page_mem;
  1557. };
  1558. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  1559. {
  1560. return (struct f2fs_stat_info *)sbi->stat_info;
  1561. }
  1562. #define stat_inc_cp_count(si) ((si)->cp_count++)
  1563. #define stat_inc_call_count(si) ((si)->call_count++)
  1564. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  1565. #define stat_inc_dirty_dir(sbi) ((sbi)->n_dirty_dirs++)
  1566. #define stat_dec_dirty_dir(sbi) ((sbi)->n_dirty_dirs--)
  1567. #define stat_inc_total_hit(sb) ((F2FS_SB(sb))->total_hit_ext++)
  1568. #define stat_inc_read_hit(sb) ((F2FS_SB(sb))->read_hit_ext++)
  1569. #define stat_inc_inline_inode(inode) \
  1570. do { \
  1571. if (f2fs_has_inline_data(inode)) \
  1572. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  1573. } while (0)
  1574. #define stat_dec_inline_inode(inode) \
  1575. do { \
  1576. if (f2fs_has_inline_data(inode)) \
  1577. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  1578. } while (0)
  1579. #define stat_inc_inline_dir(inode) \
  1580. do { \
  1581. if (f2fs_has_inline_dentry(inode)) \
  1582. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  1583. } while (0)
  1584. #define stat_dec_inline_dir(inode) \
  1585. do { \
  1586. if (f2fs_has_inline_dentry(inode)) \
  1587. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  1588. } while (0)
  1589. #define stat_inc_seg_type(sbi, curseg) \
  1590. ((sbi)->segment_count[(curseg)->alloc_type]++)
  1591. #define stat_inc_block_count(sbi, curseg) \
  1592. ((sbi)->block_count[(curseg)->alloc_type]++)
  1593. #define stat_inc_inplace_blocks(sbi) \
  1594. (atomic_inc(&(sbi)->inplace_count))
  1595. #define stat_inc_seg_count(sbi, type, gc_type) \
  1596. do { \
  1597. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1598. (si)->tot_segs++; \
  1599. if (type == SUM_TYPE_DATA) { \
  1600. si->data_segs++; \
  1601. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  1602. } else { \
  1603. si->node_segs++; \
  1604. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  1605. } \
  1606. } while (0)
  1607. #define stat_inc_tot_blk_count(si, blks) \
  1608. (si->tot_blks += (blks))
  1609. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  1610. do { \
  1611. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1612. stat_inc_tot_blk_count(si, blks); \
  1613. si->data_blks += (blks); \
  1614. si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0; \
  1615. } while (0)
  1616. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  1617. do { \
  1618. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1619. stat_inc_tot_blk_count(si, blks); \
  1620. si->node_blks += (blks); \
  1621. si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0; \
  1622. } while (0)
  1623. int f2fs_build_stats(struct f2fs_sb_info *);
  1624. void f2fs_destroy_stats(struct f2fs_sb_info *);
  1625. void __init f2fs_create_root_stats(void);
  1626. void f2fs_destroy_root_stats(void);
  1627. #else
  1628. #define stat_inc_cp_count(si)
  1629. #define stat_inc_call_count(si)
  1630. #define stat_inc_bggc_count(si)
  1631. #define stat_inc_dirty_dir(sbi)
  1632. #define stat_dec_dirty_dir(sbi)
  1633. #define stat_inc_total_hit(sb)
  1634. #define stat_inc_read_hit(sb)
  1635. #define stat_inc_inline_inode(inode)
  1636. #define stat_dec_inline_inode(inode)
  1637. #define stat_inc_inline_dir(inode)
  1638. #define stat_dec_inline_dir(inode)
  1639. #define stat_inc_seg_type(sbi, curseg)
  1640. #define stat_inc_block_count(sbi, curseg)
  1641. #define stat_inc_inplace_blocks(sbi)
  1642. #define stat_inc_seg_count(sbi, type, gc_type)
  1643. #define stat_inc_tot_blk_count(si, blks)
  1644. #define stat_inc_data_blk_count(sbi, blks, gc_type)
  1645. #define stat_inc_node_blk_count(sbi, blks, gc_type)
  1646. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  1647. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  1648. static inline void __init f2fs_create_root_stats(void) { }
  1649. static inline void f2fs_destroy_root_stats(void) { }
  1650. #endif
  1651. extern const struct file_operations f2fs_dir_operations;
  1652. extern const struct file_operations f2fs_file_operations;
  1653. extern const struct inode_operations f2fs_file_inode_operations;
  1654. extern const struct address_space_operations f2fs_dblock_aops;
  1655. extern const struct address_space_operations f2fs_node_aops;
  1656. extern const struct address_space_operations f2fs_meta_aops;
  1657. extern const struct inode_operations f2fs_dir_inode_operations;
  1658. extern const struct inode_operations f2fs_symlink_inode_operations;
  1659. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  1660. extern const struct inode_operations f2fs_special_inode_operations;
  1661. extern struct kmem_cache *inode_entry_slab;
  1662. /*
  1663. * inline.c
  1664. */
  1665. bool f2fs_may_inline_data(struct inode *);
  1666. bool f2fs_may_inline_dentry(struct inode *);
  1667. void read_inline_data(struct page *, struct page *);
  1668. bool truncate_inline_inode(struct page *, u64);
  1669. int f2fs_read_inline_data(struct inode *, struct page *);
  1670. int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
  1671. int f2fs_convert_inline_inode(struct inode *);
  1672. int f2fs_write_inline_data(struct inode *, struct page *);
  1673. bool recover_inline_data(struct inode *, struct page *);
  1674. struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
  1675. struct f2fs_filename *, struct page **);
  1676. struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
  1677. int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
  1678. int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
  1679. nid_t, umode_t);
  1680. void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
  1681. struct inode *, struct inode *);
  1682. bool f2fs_empty_inline_dir(struct inode *);
  1683. int f2fs_read_inline_dir(struct file *, struct dir_context *,
  1684. struct f2fs_str *);
  1685. /*
  1686. * crypto support
  1687. */
  1688. static inline int f2fs_encrypted_inode(struct inode *inode)
  1689. {
  1690. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1691. return file_is_encrypt(inode);
  1692. #else
  1693. return 0;
  1694. #endif
  1695. }
  1696. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  1697. {
  1698. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1699. file_set_encrypt(inode);
  1700. #endif
  1701. }
  1702. static inline bool f2fs_bio_encrypted(struct bio *bio)
  1703. {
  1704. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1705. return unlikely(bio->bi_private != NULL);
  1706. #else
  1707. return false;
  1708. #endif
  1709. }
  1710. static inline int f2fs_sb_has_crypto(struct super_block *sb)
  1711. {
  1712. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1713. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
  1714. #else
  1715. return 0;
  1716. #endif
  1717. }
  1718. static inline bool f2fs_may_encrypt(struct inode *inode)
  1719. {
  1720. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1721. mode_t mode = inode->i_mode;
  1722. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  1723. #else
  1724. return 0;
  1725. #endif
  1726. }
  1727. /* crypto_policy.c */
  1728. int f2fs_is_child_context_consistent_with_parent(struct inode *,
  1729. struct inode *);
  1730. int f2fs_inherit_context(struct inode *, struct inode *, struct page *);
  1731. int f2fs_process_policy(const struct f2fs_encryption_policy *, struct inode *);
  1732. int f2fs_get_policy(struct inode *, struct f2fs_encryption_policy *);
  1733. /* crypt.c */
  1734. extern struct kmem_cache *f2fs_crypt_info_cachep;
  1735. bool f2fs_valid_contents_enc_mode(uint32_t);
  1736. uint32_t f2fs_validate_encryption_key_size(uint32_t, uint32_t);
  1737. struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *);
  1738. void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *);
  1739. struct page *f2fs_encrypt(struct inode *, struct page *);
  1740. int f2fs_decrypt(struct f2fs_crypto_ctx *, struct page *);
  1741. int f2fs_decrypt_one(struct inode *, struct page *);
  1742. void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
  1743. /* crypto_key.c */
  1744. void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
  1745. int _f2fs_get_encryption_info(struct inode *inode);
  1746. /* crypto_fname.c */
  1747. bool f2fs_valid_filenames_enc_mode(uint32_t);
  1748. u32 f2fs_fname_crypto_round_up(u32, u32);
  1749. int f2fs_fname_crypto_alloc_buffer(struct inode *, u32, struct f2fs_str *);
  1750. int f2fs_fname_disk_to_usr(struct inode *, f2fs_hash_t *,
  1751. const struct f2fs_str *, struct f2fs_str *);
  1752. int f2fs_fname_usr_to_disk(struct inode *, const struct qstr *,
  1753. struct f2fs_str *);
  1754. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1755. void f2fs_restore_and_release_control_page(struct page **);
  1756. void f2fs_restore_control_page(struct page *);
  1757. int __init f2fs_init_crypto(void);
  1758. int f2fs_crypto_initialize(void);
  1759. void f2fs_exit_crypto(void);
  1760. int f2fs_has_encryption_key(struct inode *);
  1761. static inline int f2fs_get_encryption_info(struct inode *inode)
  1762. {
  1763. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  1764. if (!ci ||
  1765. (ci->ci_keyring_key &&
  1766. (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
  1767. (1 << KEY_FLAG_REVOKED) |
  1768. (1 << KEY_FLAG_DEAD)))))
  1769. return _f2fs_get_encryption_info(inode);
  1770. return 0;
  1771. }
  1772. void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
  1773. int f2fs_fname_setup_filename(struct inode *, const struct qstr *,
  1774. int lookup, struct f2fs_filename *);
  1775. void f2fs_fname_free_filename(struct f2fs_filename *);
  1776. #else
  1777. static inline void f2fs_restore_and_release_control_page(struct page **p) { }
  1778. static inline void f2fs_restore_control_page(struct page *p) { }
  1779. static inline int __init f2fs_init_crypto(void) { return 0; }
  1780. static inline void f2fs_exit_crypto(void) { }
  1781. static inline int f2fs_has_encryption_key(struct inode *i) { return 0; }
  1782. static inline int f2fs_get_encryption_info(struct inode *i) { return 0; }
  1783. static inline void f2fs_fname_crypto_free_buffer(struct f2fs_str *p) { }
  1784. static inline int f2fs_fname_setup_filename(struct inode *dir,
  1785. const struct qstr *iname,
  1786. int lookup, struct f2fs_filename *fname)
  1787. {
  1788. memset(fname, 0, sizeof(struct f2fs_filename));
  1789. fname->usr_fname = iname;
  1790. fname->disk_name.name = (unsigned char *)iname->name;
  1791. fname->disk_name.len = iname->len;
  1792. return 0;
  1793. }
  1794. static inline void f2fs_fname_free_filename(struct f2fs_filename *fname) { }
  1795. #endif
  1796. #endif