f2fs.h 75 KB

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