f2fs.h 70 KB

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