f2fs.h 73 KB

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