f2fs.h 76 KB

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