f2fs.h 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032
  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/quotaops.h>
  25. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  26. #include <linux/fscrypt_supp.h>
  27. #else
  28. #include <linux/fscrypt_notsupp.h>
  29. #endif
  30. #include <crypto/hash.h>
  31. #ifdef CONFIG_F2FS_CHECK_FS
  32. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  33. #else
  34. #define f2fs_bug_on(sbi, condition) \
  35. do { \
  36. if (unlikely(condition)) { \
  37. WARN_ON(1); \
  38. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  39. } \
  40. } while (0)
  41. #endif
  42. #ifdef CONFIG_F2FS_FAULT_INJECTION
  43. enum {
  44. FAULT_KMALLOC,
  45. FAULT_PAGE_ALLOC,
  46. FAULT_ALLOC_NID,
  47. FAULT_ORPHAN,
  48. FAULT_BLOCK,
  49. FAULT_DIR_DEPTH,
  50. FAULT_EVICT_INODE,
  51. FAULT_TRUNCATE,
  52. FAULT_IO,
  53. FAULT_CHECKPOINT,
  54. FAULT_MAX,
  55. };
  56. struct f2fs_fault_info {
  57. atomic_t inject_ops;
  58. unsigned int inject_rate;
  59. unsigned int inject_type;
  60. };
  61. extern char *fault_name[FAULT_MAX];
  62. #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
  63. #endif
  64. /*
  65. * For mount options
  66. */
  67. #define F2FS_MOUNT_BG_GC 0x00000001
  68. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  69. #define F2FS_MOUNT_DISCARD 0x00000004
  70. #define F2FS_MOUNT_NOHEAP 0x00000008
  71. #define F2FS_MOUNT_XATTR_USER 0x00000010
  72. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  73. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  74. #define F2FS_MOUNT_INLINE_XATTR 0x00000080
  75. #define F2FS_MOUNT_INLINE_DATA 0x00000100
  76. #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
  77. #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
  78. #define F2FS_MOUNT_NOBARRIER 0x00000800
  79. #define F2FS_MOUNT_FASTBOOT 0x00001000
  80. #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
  81. #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
  82. #define F2FS_MOUNT_DATA_FLUSH 0x00008000
  83. #define F2FS_MOUNT_FAULT_INJECTION 0x00010000
  84. #define F2FS_MOUNT_ADAPTIVE 0x00020000
  85. #define F2FS_MOUNT_LFS 0x00040000
  86. #define F2FS_MOUNT_USRQUOTA 0x00080000
  87. #define F2FS_MOUNT_GRPQUOTA 0x00100000
  88. #define F2FS_MOUNT_PRJQUOTA 0x00200000
  89. #define F2FS_MOUNT_QUOTA 0x00400000
  90. #define clear_opt(sbi, option) ((sbi)->mount_opt.opt &= ~F2FS_MOUNT_##option)
  91. #define set_opt(sbi, option) ((sbi)->mount_opt.opt |= F2FS_MOUNT_##option)
  92. #define test_opt(sbi, option) ((sbi)->mount_opt.opt & F2FS_MOUNT_##option)
  93. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  94. typecheck(unsigned long long, b) && \
  95. ((long long)((a) - (b)) > 0))
  96. typedef u32 block_t; /*
  97. * should not change u32, since it is the on-disk block
  98. * address format, __le32.
  99. */
  100. typedef u32 nid_t;
  101. struct f2fs_mount_info {
  102. unsigned int opt;
  103. };
  104. #define F2FS_FEATURE_ENCRYPT 0x0001
  105. #define F2FS_FEATURE_BLKZONED 0x0002
  106. #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
  107. #define F2FS_FEATURE_EXTRA_ATTR 0x0008
  108. #define F2FS_FEATURE_PRJQUOTA 0x0010
  109. #define F2FS_FEATURE_INODE_CHKSUM 0x0020
  110. #define F2FS_HAS_FEATURE(sb, mask) \
  111. ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
  112. #define F2FS_SET_FEATURE(sb, mask) \
  113. (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
  114. #define F2FS_CLEAR_FEATURE(sb, mask) \
  115. (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
  116. /*
  117. * For checkpoint manager
  118. */
  119. enum {
  120. NAT_BITMAP,
  121. SIT_BITMAP
  122. };
  123. #define CP_UMOUNT 0x00000001
  124. #define CP_FASTBOOT 0x00000002
  125. #define CP_SYNC 0x00000004
  126. #define CP_RECOVERY 0x00000008
  127. #define CP_DISCARD 0x00000010
  128. #define CP_TRIMMED 0x00000020
  129. #define DEF_BATCHED_TRIM_SECTIONS 2048
  130. #define BATCHED_TRIM_SEGMENTS(sbi) \
  131. (GET_SEG_FROM_SEC(sbi, SM_I(sbi)->trim_sections))
  132. #define BATCHED_TRIM_BLOCKS(sbi) \
  133. (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
  134. #define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
  135. #define DISCARD_ISSUE_RATE 8
  136. #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
  137. #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
  138. #define DEF_CP_INTERVAL 60 /* 60 secs */
  139. #define DEF_IDLE_INTERVAL 5 /* 5 secs */
  140. struct cp_control {
  141. int reason;
  142. __u64 trim_start;
  143. __u64 trim_end;
  144. __u64 trim_minlen;
  145. __u64 trimmed;
  146. };
  147. /*
  148. * For CP/NAT/SIT/SSA readahead
  149. */
  150. enum {
  151. META_CP,
  152. META_NAT,
  153. META_SIT,
  154. META_SSA,
  155. META_POR,
  156. };
  157. /* for the list of ino */
  158. enum {
  159. ORPHAN_INO, /* for orphan ino list */
  160. APPEND_INO, /* for append ino list */
  161. UPDATE_INO, /* for update ino list */
  162. MAX_INO_ENTRY, /* max. list */
  163. };
  164. struct ino_entry {
  165. struct list_head list; /* list head */
  166. nid_t ino; /* inode number */
  167. };
  168. /* for the list of inodes to be GCed */
  169. struct inode_entry {
  170. struct list_head list; /* list head */
  171. struct inode *inode; /* vfs inode pointer */
  172. };
  173. /* for the bitmap indicate blocks to be discarded */
  174. struct discard_entry {
  175. struct list_head list; /* list head */
  176. block_t start_blkaddr; /* start blockaddr of current segment */
  177. unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
  178. };
  179. /* default discard granularity of inner discard thread, unit: block count */
  180. #define DEFAULT_DISCARD_GRANULARITY 16
  181. /* max discard pend list number */
  182. #define MAX_PLIST_NUM 512
  183. #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
  184. (MAX_PLIST_NUM - 1) : (blk_num - 1))
  185. #define P_ACTIVE 0x01
  186. #define P_TRIM 0x02
  187. #define plist_issue(tag) (((tag) & P_ACTIVE) || ((tag) & P_TRIM))
  188. enum {
  189. D_PREP,
  190. D_SUBMIT,
  191. D_DONE,
  192. };
  193. struct discard_info {
  194. block_t lstart; /* logical start address */
  195. block_t len; /* length */
  196. block_t start; /* actual start address in dev */
  197. };
  198. struct discard_cmd {
  199. struct rb_node rb_node; /* rb node located in rb-tree */
  200. union {
  201. struct {
  202. block_t lstart; /* logical start address */
  203. block_t len; /* length */
  204. block_t start; /* actual start address in dev */
  205. };
  206. struct discard_info di; /* discard info */
  207. };
  208. struct list_head list; /* command list */
  209. struct completion wait; /* compleation */
  210. struct block_device *bdev; /* bdev */
  211. unsigned short ref; /* reference count */
  212. unsigned char state; /* state */
  213. int error; /* bio error */
  214. };
  215. struct discard_cmd_control {
  216. struct task_struct *f2fs_issue_discard; /* discard thread */
  217. struct list_head entry_list; /* 4KB discard entry list */
  218. struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
  219. unsigned char pend_list_tag[MAX_PLIST_NUM];/* tag for pending entries */
  220. struct list_head wait_list; /* store on-flushing entries */
  221. wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
  222. unsigned int discard_wake; /* to wake up discard thread */
  223. struct mutex cmd_lock;
  224. unsigned int nr_discards; /* # of discards in the list */
  225. unsigned int max_discards; /* max. discards to be issued */
  226. unsigned int discard_granularity; /* discard granularity */
  227. unsigned int undiscard_blks; /* # of undiscard blocks */
  228. atomic_t issued_discard; /* # of issued discard */
  229. atomic_t issing_discard; /* # of issing discard */
  230. atomic_t discard_cmd_cnt; /* # of cached cmd count */
  231. struct rb_root root; /* root of discard rb-tree */
  232. };
  233. /* for the list of fsync inodes, used only during recovery */
  234. struct fsync_inode_entry {
  235. struct list_head list; /* list head */
  236. struct inode *inode; /* vfs inode pointer */
  237. block_t blkaddr; /* block address locating the last fsync */
  238. block_t last_dentry; /* block address locating the last dentry */
  239. };
  240. #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
  241. #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
  242. #define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
  243. #define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
  244. #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
  245. #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
  246. #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
  247. #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
  248. static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
  249. {
  250. int before = nats_in_cursum(journal);
  251. journal->n_nats = cpu_to_le16(before + i);
  252. return before;
  253. }
  254. static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
  255. {
  256. int before = sits_in_cursum(journal);
  257. journal->n_sits = cpu_to_le16(before + i);
  258. return before;
  259. }
  260. static inline bool __has_cursum_space(struct f2fs_journal *journal,
  261. int size, int type)
  262. {
  263. if (type == NAT_JOURNAL)
  264. return size <= MAX_NAT_JENTRIES(journal);
  265. return size <= MAX_SIT_JENTRIES(journal);
  266. }
  267. /*
  268. * ioctl commands
  269. */
  270. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  271. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  272. #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
  273. #define F2FS_IOCTL_MAGIC 0xf5
  274. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  275. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  276. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  277. #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
  278. #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
  279. #define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
  280. #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
  281. #define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
  282. struct f2fs_defragment)
  283. #define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
  284. struct f2fs_move_range)
  285. #define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
  286. struct f2fs_flush_device)
  287. #define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
  288. struct f2fs_gc_range)
  289. #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
  290. #define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
  291. #define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
  292. #define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
  293. /*
  294. * should be same as XFS_IOC_GOINGDOWN.
  295. * Flags for going down operation used by FS_IOC_GOINGDOWN
  296. */
  297. #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
  298. #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
  299. #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
  300. #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
  301. #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
  302. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  303. /*
  304. * ioctl commands in 32 bit emulation
  305. */
  306. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  307. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  308. #define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
  309. #endif
  310. #define F2FS_IOC_FSGETXATTR FS_IOC_FSGETXATTR
  311. #define F2FS_IOC_FSSETXATTR FS_IOC_FSSETXATTR
  312. struct f2fs_gc_range {
  313. u32 sync;
  314. u64 start;
  315. u64 len;
  316. };
  317. struct f2fs_defragment {
  318. u64 start;
  319. u64 len;
  320. };
  321. struct f2fs_move_range {
  322. u32 dst_fd; /* destination fd */
  323. u64 pos_in; /* start position in src_fd */
  324. u64 pos_out; /* start position in dst_fd */
  325. u64 len; /* size to move */
  326. };
  327. struct f2fs_flush_device {
  328. u32 dev_num; /* device number to flush */
  329. u32 segments; /* # of segments to flush */
  330. };
  331. /* for inline stuff */
  332. #define DEF_INLINE_RESERVED_SIZE 1
  333. static inline int get_extra_isize(struct inode *inode);
  334. #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
  335. (CUR_ADDRS_PER_INODE(inode) - \
  336. DEF_INLINE_RESERVED_SIZE - \
  337. F2FS_INLINE_XATTR_ADDRS))
  338. /* for inline dir */
  339. #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
  340. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  341. BITS_PER_BYTE + 1))
  342. #define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
  343. BITS_PER_BYTE - 1) / BITS_PER_BYTE)
  344. #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
  345. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  346. NR_INLINE_DENTRY(inode) + \
  347. INLINE_DENTRY_BITMAP_SIZE(inode)))
  348. /*
  349. * For INODE and NODE manager
  350. */
  351. /* for directory operations */
  352. struct f2fs_dentry_ptr {
  353. struct inode *inode;
  354. void *bitmap;
  355. struct f2fs_dir_entry *dentry;
  356. __u8 (*filename)[F2FS_SLOT_LEN];
  357. int max;
  358. int nr_bitmap;
  359. };
  360. static inline void make_dentry_ptr_block(struct inode *inode,
  361. struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
  362. {
  363. d->inode = inode;
  364. d->max = NR_DENTRY_IN_BLOCK;
  365. d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
  366. d->bitmap = &t->dentry_bitmap;
  367. d->dentry = t->dentry;
  368. d->filename = t->filename;
  369. }
  370. static inline void make_dentry_ptr_inline(struct inode *inode,
  371. struct f2fs_dentry_ptr *d, void *t)
  372. {
  373. int entry_cnt = NR_INLINE_DENTRY(inode);
  374. int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
  375. int reserved_size = INLINE_RESERVED_SIZE(inode);
  376. d->inode = inode;
  377. d->max = entry_cnt;
  378. d->nr_bitmap = bitmap_size;
  379. d->bitmap = t;
  380. d->dentry = t + bitmap_size + reserved_size;
  381. d->filename = t + bitmap_size + reserved_size +
  382. SIZE_OF_DIR_ENTRY * entry_cnt;
  383. }
  384. /*
  385. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  386. * as its node offset to distinguish from index node blocks.
  387. * But some bits are used to mark the node block.
  388. */
  389. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  390. >> OFFSET_BIT_SHIFT)
  391. enum {
  392. ALLOC_NODE, /* allocate a new node page if needed */
  393. LOOKUP_NODE, /* look up a node without readahead */
  394. LOOKUP_NODE_RA, /*
  395. * look up a node with readahead called
  396. * by get_data_block.
  397. */
  398. };
  399. #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
  400. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  401. /* vector size for gang look-up from extent cache that consists of radix tree */
  402. #define EXT_TREE_VEC_SIZE 64
  403. /* for in-memory extent cache entry */
  404. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  405. /* number of extent info in extent cache we try to shrink */
  406. #define EXTENT_CACHE_SHRINK_NUMBER 128
  407. struct rb_entry {
  408. struct rb_node rb_node; /* rb node located in rb-tree */
  409. unsigned int ofs; /* start offset of the entry */
  410. unsigned int len; /* length of the entry */
  411. };
  412. struct extent_info {
  413. unsigned int fofs; /* start offset in a file */
  414. unsigned int len; /* length of the extent */
  415. u32 blk; /* start block address of the extent */
  416. };
  417. struct extent_node {
  418. struct rb_node rb_node;
  419. union {
  420. struct {
  421. unsigned int fofs;
  422. unsigned int len;
  423. u32 blk;
  424. };
  425. struct extent_info ei; /* extent info */
  426. };
  427. struct list_head list; /* node in global extent list of sbi */
  428. struct extent_tree *et; /* extent tree pointer */
  429. };
  430. struct extent_tree {
  431. nid_t ino; /* inode number */
  432. struct rb_root root; /* root of extent info rb-tree */
  433. struct extent_node *cached_en; /* recently accessed extent node */
  434. struct extent_info largest; /* largested extent info */
  435. struct list_head list; /* to be used by sbi->zombie_list */
  436. rwlock_t lock; /* protect extent info rb-tree */
  437. atomic_t node_cnt; /* # of extent node in rb-tree*/
  438. };
  439. /*
  440. * This structure is taken from ext4_map_blocks.
  441. *
  442. * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
  443. */
  444. #define F2FS_MAP_NEW (1 << BH_New)
  445. #define F2FS_MAP_MAPPED (1 << BH_Mapped)
  446. #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
  447. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  448. F2FS_MAP_UNWRITTEN)
  449. struct f2fs_map_blocks {
  450. block_t m_pblk;
  451. block_t m_lblk;
  452. unsigned int m_len;
  453. unsigned int m_flags;
  454. pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
  455. };
  456. /* for flag in get_data_block */
  457. enum {
  458. F2FS_GET_BLOCK_DEFAULT,
  459. F2FS_GET_BLOCK_FIEMAP,
  460. F2FS_GET_BLOCK_BMAP,
  461. F2FS_GET_BLOCK_PRE_DIO,
  462. F2FS_GET_BLOCK_PRE_AIO,
  463. };
  464. /*
  465. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  466. */
  467. #define FADVISE_COLD_BIT 0x01
  468. #define FADVISE_LOST_PINO_BIT 0x02
  469. #define FADVISE_ENCRYPT_BIT 0x04
  470. #define FADVISE_ENC_NAME_BIT 0x08
  471. #define FADVISE_KEEP_SIZE_BIT 0x10
  472. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  473. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  474. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  475. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  476. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  477. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  478. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  479. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  480. #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
  481. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  482. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  483. #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
  484. #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
  485. #define DEF_DIR_LEVEL 0
  486. struct f2fs_inode_info {
  487. struct inode vfs_inode; /* serve a vfs inode */
  488. unsigned long i_flags; /* keep an inode flags for ioctl */
  489. unsigned char i_advise; /* use to give file attribute hints */
  490. unsigned char i_dir_level; /* use for dentry level for large dir */
  491. unsigned int i_current_depth; /* use only in directory structure */
  492. unsigned int i_pino; /* parent inode number */
  493. umode_t i_acl_mode; /* keep file acl mode temporarily */
  494. /* Use below internally in f2fs*/
  495. unsigned long flags; /* use to pass per-file flags */
  496. struct rw_semaphore i_sem; /* protect fi info */
  497. atomic_t dirty_pages; /* # of dirty pages */
  498. f2fs_hash_t chash; /* hash value of given file name */
  499. unsigned int clevel; /* maximum level of given file name */
  500. struct task_struct *task; /* lookup and create consistency */
  501. struct task_struct *cp_task; /* separate cp/wb IO stats*/
  502. nid_t i_xattr_nid; /* node id that contains xattrs */
  503. loff_t last_disk_size; /* lastly written file size */
  504. #ifdef CONFIG_QUOTA
  505. struct dquot *i_dquot[MAXQUOTAS];
  506. /* quota space reservation, managed internally by quota code */
  507. qsize_t i_reserved_quota;
  508. #endif
  509. struct list_head dirty_list; /* dirty list for dirs and files */
  510. struct list_head gdirty_list; /* linked in global dirty list */
  511. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  512. struct task_struct *inmem_task; /* store inmemory task */
  513. struct mutex inmem_lock; /* lock for inmemory pages */
  514. struct extent_tree *extent_tree; /* cached extent_tree entry */
  515. struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
  516. struct rw_semaphore i_mmap_sem;
  517. struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
  518. int i_extra_isize; /* size of extra space located in i_addr */
  519. kprojid_t i_projid; /* id for project quota */
  520. };
  521. static inline void get_extent_info(struct extent_info *ext,
  522. struct f2fs_extent *i_ext)
  523. {
  524. ext->fofs = le32_to_cpu(i_ext->fofs);
  525. ext->blk = le32_to_cpu(i_ext->blk);
  526. ext->len = le32_to_cpu(i_ext->len);
  527. }
  528. static inline void set_raw_extent(struct extent_info *ext,
  529. struct f2fs_extent *i_ext)
  530. {
  531. i_ext->fofs = cpu_to_le32(ext->fofs);
  532. i_ext->blk = cpu_to_le32(ext->blk);
  533. i_ext->len = cpu_to_le32(ext->len);
  534. }
  535. static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
  536. u32 blk, unsigned int len)
  537. {
  538. ei->fofs = fofs;
  539. ei->blk = blk;
  540. ei->len = len;
  541. }
  542. static inline bool __is_discard_mergeable(struct discard_info *back,
  543. struct discard_info *front)
  544. {
  545. return back->lstart + back->len == front->lstart;
  546. }
  547. static inline bool __is_discard_back_mergeable(struct discard_info *cur,
  548. struct discard_info *back)
  549. {
  550. return __is_discard_mergeable(back, cur);
  551. }
  552. static inline bool __is_discard_front_mergeable(struct discard_info *cur,
  553. struct discard_info *front)
  554. {
  555. return __is_discard_mergeable(cur, front);
  556. }
  557. static inline bool __is_extent_mergeable(struct extent_info *back,
  558. struct extent_info *front)
  559. {
  560. return (back->fofs + back->len == front->fofs &&
  561. back->blk + back->len == front->blk);
  562. }
  563. static inline bool __is_back_mergeable(struct extent_info *cur,
  564. struct extent_info *back)
  565. {
  566. return __is_extent_mergeable(back, cur);
  567. }
  568. static inline bool __is_front_mergeable(struct extent_info *cur,
  569. struct extent_info *front)
  570. {
  571. return __is_extent_mergeable(cur, front);
  572. }
  573. extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
  574. static inline void __try_update_largest_extent(struct inode *inode,
  575. struct extent_tree *et, struct extent_node *en)
  576. {
  577. if (en->ei.len > et->largest.len) {
  578. et->largest = en->ei;
  579. f2fs_mark_inode_dirty_sync(inode, true);
  580. }
  581. }
  582. enum nid_list {
  583. FREE_NID_LIST,
  584. ALLOC_NID_LIST,
  585. MAX_NID_LIST,
  586. };
  587. struct f2fs_nm_info {
  588. block_t nat_blkaddr; /* base disk address of NAT */
  589. nid_t max_nid; /* maximum possible node ids */
  590. nid_t available_nids; /* # of available node ids */
  591. nid_t next_scan_nid; /* the next nid to be scanned */
  592. unsigned int ram_thresh; /* control the memory footprint */
  593. unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
  594. unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
  595. /* NAT cache management */
  596. struct radix_tree_root nat_root;/* root of the nat entry cache */
  597. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  598. struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
  599. struct list_head nat_entries; /* cached nat entry list (clean) */
  600. unsigned int nat_cnt; /* the # of cached nat entries */
  601. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  602. unsigned int nat_blocks; /* # of nat blocks */
  603. /* free node ids management */
  604. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  605. struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
  606. unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
  607. spinlock_t nid_list_lock; /* protect nid lists ops */
  608. struct mutex build_lock; /* lock for build free nids */
  609. unsigned char (*free_nid_bitmap)[NAT_ENTRY_BITMAP_SIZE];
  610. unsigned char *nat_block_bitmap;
  611. unsigned short *free_nid_count; /* free nid count of NAT block */
  612. /* for checkpoint */
  613. char *nat_bitmap; /* NAT bitmap pointer */
  614. unsigned int nat_bits_blocks; /* # of nat bits blocks */
  615. unsigned char *nat_bits; /* NAT bits blocks */
  616. unsigned char *full_nat_bits; /* full NAT pages */
  617. unsigned char *empty_nat_bits; /* empty NAT pages */
  618. #ifdef CONFIG_F2FS_CHECK_FS
  619. char *nat_bitmap_mir; /* NAT bitmap mirror */
  620. #endif
  621. int bitmap_size; /* bitmap size */
  622. };
  623. /*
  624. * this structure is used as one of function parameters.
  625. * all the information are dedicated to a given direct node block determined
  626. * by the data offset in a file.
  627. */
  628. struct dnode_of_data {
  629. struct inode *inode; /* vfs inode pointer */
  630. struct page *inode_page; /* its inode page, NULL is possible */
  631. struct page *node_page; /* cached direct node page */
  632. nid_t nid; /* node id of the direct node block */
  633. unsigned int ofs_in_node; /* data offset in the node page */
  634. bool inode_page_locked; /* inode page is locked or not */
  635. bool node_changed; /* is node block changed */
  636. char cur_level; /* level of hole node page */
  637. char max_level; /* level of current page located */
  638. block_t data_blkaddr; /* block address of the node block */
  639. };
  640. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  641. struct page *ipage, struct page *npage, nid_t nid)
  642. {
  643. memset(dn, 0, sizeof(*dn));
  644. dn->inode = inode;
  645. dn->inode_page = ipage;
  646. dn->node_page = npage;
  647. dn->nid = nid;
  648. }
  649. /*
  650. * For SIT manager
  651. *
  652. * By default, there are 6 active log areas across the whole main area.
  653. * When considering hot and cold data separation to reduce cleaning overhead,
  654. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  655. * respectively.
  656. * In the current design, you should not change the numbers intentionally.
  657. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  658. * logs individually according to the underlying devices. (default: 6)
  659. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  660. * data and 8 for node logs.
  661. */
  662. #define NR_CURSEG_DATA_TYPE (3)
  663. #define NR_CURSEG_NODE_TYPE (3)
  664. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  665. enum {
  666. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  667. CURSEG_WARM_DATA, /* data blocks */
  668. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  669. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  670. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  671. CURSEG_COLD_NODE, /* indirect node blocks */
  672. NO_CHECK_TYPE,
  673. };
  674. struct flush_cmd {
  675. struct completion wait;
  676. struct llist_node llnode;
  677. int ret;
  678. };
  679. struct flush_cmd_control {
  680. struct task_struct *f2fs_issue_flush; /* flush thread */
  681. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  682. atomic_t issued_flush; /* # of issued flushes */
  683. atomic_t issing_flush; /* # of issing flushes */
  684. struct llist_head issue_list; /* list for command issue */
  685. struct llist_node *dispatch_list; /* list for command dispatch */
  686. };
  687. struct f2fs_sm_info {
  688. struct sit_info *sit_info; /* whole segment information */
  689. struct free_segmap_info *free_info; /* free segment information */
  690. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  691. struct curseg_info *curseg_array; /* active segment information */
  692. block_t seg0_blkaddr; /* block address of 0'th segment */
  693. block_t main_blkaddr; /* start block address of main area */
  694. block_t ssa_blkaddr; /* start block address of SSA area */
  695. unsigned int segment_count; /* total # of segments */
  696. unsigned int main_segments; /* # of segments in main area */
  697. unsigned int reserved_segments; /* # of reserved segments */
  698. unsigned int ovp_segments; /* # of overprovision segments */
  699. /* a threshold to reclaim prefree segments */
  700. unsigned int rec_prefree_segments;
  701. /* for batched trimming */
  702. unsigned int trim_sections; /* # of sections to trim */
  703. struct list_head sit_entry_set; /* sit entry set list */
  704. unsigned int ipu_policy; /* in-place-update policy */
  705. unsigned int min_ipu_util; /* in-place-update threshold */
  706. unsigned int min_fsync_blocks; /* threshold for fsync */
  707. unsigned int min_hot_blocks; /* threshold for hot block allocation */
  708. /* for flush command control */
  709. struct flush_cmd_control *fcc_info;
  710. /* for discard command control */
  711. struct discard_cmd_control *dcc_info;
  712. };
  713. /*
  714. * For superblock
  715. */
  716. /*
  717. * COUNT_TYPE for monitoring
  718. *
  719. * f2fs monitors the number of several block types such as on-writeback,
  720. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  721. */
  722. #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
  723. enum count_type {
  724. F2FS_DIRTY_DENTS,
  725. F2FS_DIRTY_DATA,
  726. F2FS_DIRTY_NODES,
  727. F2FS_DIRTY_META,
  728. F2FS_INMEM_PAGES,
  729. F2FS_DIRTY_IMETA,
  730. F2FS_WB_CP_DATA,
  731. F2FS_WB_DATA,
  732. NR_COUNT_TYPE,
  733. };
  734. /*
  735. * The below are the page types of bios used in submit_bio().
  736. * The available types are:
  737. * DATA User data pages. It operates as async mode.
  738. * NODE Node pages. It operates as async mode.
  739. * META FS metadata pages such as SIT, NAT, CP.
  740. * NR_PAGE_TYPE The number of page types.
  741. * META_FLUSH Make sure the previous pages are written
  742. * with waiting the bio's completion
  743. * ... Only can be used with META.
  744. */
  745. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  746. enum page_type {
  747. DATA,
  748. NODE,
  749. META,
  750. NR_PAGE_TYPE,
  751. META_FLUSH,
  752. INMEM, /* the below types are used by tracepoints only. */
  753. INMEM_DROP,
  754. INMEM_INVALIDATE,
  755. INMEM_REVOKE,
  756. IPU,
  757. OPU,
  758. };
  759. enum temp_type {
  760. HOT = 0, /* must be zero for meta bio */
  761. WARM,
  762. COLD,
  763. NR_TEMP_TYPE,
  764. };
  765. enum need_lock_type {
  766. LOCK_REQ = 0,
  767. LOCK_DONE,
  768. LOCK_RETRY,
  769. };
  770. enum iostat_type {
  771. APP_DIRECT_IO, /* app direct IOs */
  772. APP_BUFFERED_IO, /* app buffered IOs */
  773. APP_WRITE_IO, /* app write IOs */
  774. APP_MAPPED_IO, /* app mapped IOs */
  775. FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
  776. FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
  777. FS_META_IO, /* meta IOs from kworker/reclaimer */
  778. FS_GC_DATA_IO, /* data IOs from forground gc */
  779. FS_GC_NODE_IO, /* node IOs from forground gc */
  780. FS_CP_DATA_IO, /* data IOs from checkpoint */
  781. FS_CP_NODE_IO, /* node IOs from checkpoint */
  782. FS_CP_META_IO, /* meta IOs from checkpoint */
  783. FS_DISCARD, /* discard */
  784. NR_IO_TYPE,
  785. };
  786. struct f2fs_io_info {
  787. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  788. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  789. enum temp_type temp; /* contains HOT/WARM/COLD */
  790. int op; /* contains REQ_OP_ */
  791. int op_flags; /* req_flag_bits */
  792. block_t new_blkaddr; /* new block address to be written */
  793. block_t old_blkaddr; /* old block address before Cow */
  794. struct page *page; /* page to be written */
  795. struct page *encrypted_page; /* encrypted page */
  796. struct list_head list; /* serialize IOs */
  797. bool submitted; /* indicate IO submission */
  798. int need_lock; /* indicate we need to lock cp_rwsem */
  799. bool in_list; /* indicate fio is in io_list */
  800. enum iostat_type io_type; /* io type */
  801. };
  802. #define is_read_io(rw) ((rw) == READ)
  803. struct f2fs_bio_info {
  804. struct f2fs_sb_info *sbi; /* f2fs superblock */
  805. struct bio *bio; /* bios to merge */
  806. sector_t last_block_in_bio; /* last block number */
  807. struct f2fs_io_info fio; /* store buffered io info. */
  808. struct rw_semaphore io_rwsem; /* blocking op for bio */
  809. spinlock_t io_lock; /* serialize DATA/NODE IOs */
  810. struct list_head io_list; /* track fios */
  811. };
  812. #define FDEV(i) (sbi->devs[i])
  813. #define RDEV(i) (raw_super->devs[i])
  814. struct f2fs_dev_info {
  815. struct block_device *bdev;
  816. char path[MAX_PATH_LEN];
  817. unsigned int total_segments;
  818. block_t start_blk;
  819. block_t end_blk;
  820. #ifdef CONFIG_BLK_DEV_ZONED
  821. unsigned int nr_blkz; /* Total number of zones */
  822. u8 *blkz_type; /* Array of zones type */
  823. #endif
  824. };
  825. enum inode_type {
  826. DIR_INODE, /* for dirty dir inode */
  827. FILE_INODE, /* for dirty regular/symlink inode */
  828. DIRTY_META, /* for all dirtied inode metadata */
  829. NR_INODE_TYPE,
  830. };
  831. /* for inner inode cache management */
  832. struct inode_management {
  833. struct radix_tree_root ino_root; /* ino entry array */
  834. spinlock_t ino_lock; /* for ino entry lock */
  835. struct list_head ino_list; /* inode list head */
  836. unsigned long ino_num; /* number of entries */
  837. };
  838. /* For s_flag in struct f2fs_sb_info */
  839. enum {
  840. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  841. SBI_IS_CLOSE, /* specify unmounting */
  842. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  843. SBI_POR_DOING, /* recovery is doing or not */
  844. SBI_NEED_SB_WRITE, /* need to recover superblock */
  845. SBI_NEED_CP, /* need to checkpoint */
  846. };
  847. enum {
  848. CP_TIME,
  849. REQ_TIME,
  850. MAX_TIME,
  851. };
  852. struct f2fs_sb_info {
  853. struct super_block *sb; /* pointer to VFS super block */
  854. struct proc_dir_entry *s_proc; /* proc entry */
  855. struct f2fs_super_block *raw_super; /* raw super block pointer */
  856. int valid_super_block; /* valid super block no */
  857. unsigned long s_flag; /* flags for sbi */
  858. #ifdef CONFIG_BLK_DEV_ZONED
  859. unsigned int blocks_per_blkz; /* F2FS blocks per zone */
  860. unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
  861. #endif
  862. /* for node-related operations */
  863. struct f2fs_nm_info *nm_info; /* node manager */
  864. struct inode *node_inode; /* cache node blocks */
  865. /* for segment-related operations */
  866. struct f2fs_sm_info *sm_info; /* segment manager */
  867. /* for bio operations */
  868. struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
  869. struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
  870. /* bio ordering for NODE/DATA */
  871. int write_io_size_bits; /* Write IO size bits */
  872. mempool_t *write_io_dummy; /* Dummy pages */
  873. /* for checkpoint */
  874. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  875. int cur_cp_pack; /* remain current cp pack */
  876. spinlock_t cp_lock; /* for flag in ckpt */
  877. struct inode *meta_inode; /* cache meta blocks */
  878. struct mutex cp_mutex; /* checkpoint procedure lock */
  879. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  880. struct rw_semaphore node_write; /* locking node writes */
  881. struct rw_semaphore node_change; /* locking node change */
  882. wait_queue_head_t cp_wait;
  883. unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
  884. long interval_time[MAX_TIME]; /* to store thresholds */
  885. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  886. /* for orphan inode, use 0'th array */
  887. unsigned int max_orphans; /* max orphan inodes */
  888. /* for inode management */
  889. struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
  890. spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
  891. /* for extent tree cache */
  892. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  893. struct mutex extent_tree_lock; /* locking extent radix tree */
  894. struct list_head extent_list; /* lru list for shrinker */
  895. spinlock_t extent_lock; /* locking extent lru list */
  896. atomic_t total_ext_tree; /* extent tree count */
  897. struct list_head zombie_list; /* extent zombie tree list */
  898. atomic_t total_zombie_tree; /* extent zombie tree count */
  899. atomic_t total_ext_node; /* extent info count */
  900. /* basic filesystem units */
  901. unsigned int log_sectors_per_block; /* log2 sectors per block */
  902. unsigned int log_blocksize; /* log2 block size */
  903. unsigned int blocksize; /* block size */
  904. unsigned int root_ino_num; /* root inode number*/
  905. unsigned int node_ino_num; /* node inode number*/
  906. unsigned int meta_ino_num; /* meta inode number*/
  907. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  908. unsigned int blocks_per_seg; /* blocks per segment */
  909. unsigned int segs_per_sec; /* segments per section */
  910. unsigned int secs_per_zone; /* sections per zone */
  911. unsigned int total_sections; /* total section count */
  912. unsigned int total_node_count; /* total node block count */
  913. unsigned int total_valid_node_count; /* valid node block count */
  914. loff_t max_file_blocks; /* max block index of file */
  915. int active_logs; /* # of active logs */
  916. int dir_level; /* directory level */
  917. block_t user_block_count; /* # of user blocks */
  918. block_t total_valid_block_count; /* # of valid blocks */
  919. block_t discard_blks; /* discard command candidats */
  920. block_t last_valid_block_count; /* for recovery */
  921. block_t reserved_blocks; /* configurable reserved blocks */
  922. u32 s_next_generation; /* for NFS support */
  923. /* # of pages, see count_type */
  924. atomic_t nr_pages[NR_COUNT_TYPE];
  925. /* # of allocated blocks */
  926. struct percpu_counter alloc_valid_block_count;
  927. /* writeback control */
  928. atomic_t wb_sync_req; /* count # of WB_SYNC threads */
  929. /* valid inode count */
  930. struct percpu_counter total_valid_inode_count;
  931. struct f2fs_mount_info mount_opt; /* mount options */
  932. /* for cleaning operations */
  933. struct mutex gc_mutex; /* mutex for GC */
  934. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  935. unsigned int cur_victim_sec; /* current victim section num */
  936. /* threshold for converting bg victims for fg */
  937. u64 fggc_threshold;
  938. /* maximum # of trials to find a victim segment for SSR and GC */
  939. unsigned int max_victim_search;
  940. /*
  941. * for stat information.
  942. * one is for the LFS mode, and the other is for the SSR mode.
  943. */
  944. #ifdef CONFIG_F2FS_STAT_FS
  945. struct f2fs_stat_info *stat_info; /* FS status information */
  946. unsigned int segment_count[2]; /* # of allocated segments */
  947. unsigned int block_count[2]; /* # of allocated blocks */
  948. atomic_t inplace_count; /* # of inplace update */
  949. atomic64_t total_hit_ext; /* # of lookup extent cache */
  950. atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
  951. atomic64_t read_hit_largest; /* # of hit largest extent node */
  952. atomic64_t read_hit_cached; /* # of hit cached extent node */
  953. atomic_t inline_xattr; /* # of inline_xattr inodes */
  954. atomic_t inline_inode; /* # of inline_data inodes */
  955. atomic_t inline_dir; /* # of inline_dentry inodes */
  956. atomic_t aw_cnt; /* # of atomic writes */
  957. atomic_t vw_cnt; /* # of volatile writes */
  958. atomic_t max_aw_cnt; /* max # of atomic writes */
  959. atomic_t max_vw_cnt; /* max # of volatile writes */
  960. int bg_gc; /* background gc calls */
  961. unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
  962. #endif
  963. spinlock_t stat_lock; /* lock for stat operations */
  964. /* For app/fs IO statistics */
  965. spinlock_t iostat_lock;
  966. unsigned long long write_iostat[NR_IO_TYPE];
  967. bool iostat_enable;
  968. /* For sysfs suppport */
  969. struct kobject s_kobj;
  970. struct completion s_kobj_unregister;
  971. /* For shrinker support */
  972. struct list_head s_list;
  973. int s_ndevs; /* number of devices */
  974. struct f2fs_dev_info *devs; /* for device list */
  975. struct mutex umount_mutex;
  976. unsigned int shrinker_run_no;
  977. /* For write statistics */
  978. u64 sectors_written_start;
  979. u64 kbytes_written;
  980. /* Reference to checksum algorithm driver via cryptoapi */
  981. struct crypto_shash *s_chksum_driver;
  982. /* Precomputed FS UUID checksum for seeding other checksums */
  983. __u32 s_chksum_seed;
  984. /* For fault injection */
  985. #ifdef CONFIG_F2FS_FAULT_INJECTION
  986. struct f2fs_fault_info fault_info;
  987. #endif
  988. #ifdef CONFIG_QUOTA
  989. /* Names of quota files with journalled quota */
  990. char *s_qf_names[MAXQUOTAS];
  991. int s_jquota_fmt; /* Format of quota to use */
  992. #endif
  993. };
  994. #ifdef CONFIG_F2FS_FAULT_INJECTION
  995. #define f2fs_show_injection_info(type) \
  996. printk("%sF2FS-fs : inject %s in %s of %pF\n", \
  997. KERN_INFO, fault_name[type], \
  998. __func__, __builtin_return_address(0))
  999. static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
  1000. {
  1001. struct f2fs_fault_info *ffi = &sbi->fault_info;
  1002. if (!ffi->inject_rate)
  1003. return false;
  1004. if (!IS_FAULT_SET(ffi, type))
  1005. return false;
  1006. atomic_inc(&ffi->inject_ops);
  1007. if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
  1008. atomic_set(&ffi->inject_ops, 0);
  1009. return true;
  1010. }
  1011. return false;
  1012. }
  1013. #endif
  1014. /* For write statistics. Suppose sector size is 512 bytes,
  1015. * and the return value is in kbytes. s is of struct f2fs_sb_info.
  1016. */
  1017. #define BD_PART_WRITTEN(s) \
  1018. (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[1]) - \
  1019. (s)->sectors_written_start) >> 1)
  1020. static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
  1021. {
  1022. sbi->last_time[type] = jiffies;
  1023. }
  1024. static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
  1025. {
  1026. struct timespec ts = {sbi->interval_time[type], 0};
  1027. unsigned long interval = timespec_to_jiffies(&ts);
  1028. return time_after(jiffies, sbi->last_time[type] + interval);
  1029. }
  1030. static inline bool is_idle(struct f2fs_sb_info *sbi)
  1031. {
  1032. struct block_device *bdev = sbi->sb->s_bdev;
  1033. struct request_queue *q = bdev_get_queue(bdev);
  1034. struct request_list *rl = &q->root_rl;
  1035. if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
  1036. return 0;
  1037. return f2fs_time_over(sbi, REQ_TIME);
  1038. }
  1039. /*
  1040. * Inline functions
  1041. */
  1042. static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
  1043. unsigned int length)
  1044. {
  1045. SHASH_DESC_ON_STACK(shash, sbi->s_chksum_driver);
  1046. u32 *ctx = (u32 *)shash_desc_ctx(shash);
  1047. u32 retval;
  1048. int err;
  1049. shash->tfm = sbi->s_chksum_driver;
  1050. shash->flags = 0;
  1051. *ctx = F2FS_SUPER_MAGIC;
  1052. err = crypto_shash_update(shash, address, length);
  1053. BUG_ON(err);
  1054. retval = *ctx;
  1055. barrier_data(ctx);
  1056. return retval;
  1057. }
  1058. static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
  1059. void *buf, size_t buf_size)
  1060. {
  1061. return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
  1062. }
  1063. static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
  1064. const void *address, unsigned int length)
  1065. {
  1066. struct {
  1067. struct shash_desc shash;
  1068. char ctx[4];
  1069. } desc;
  1070. int err;
  1071. BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
  1072. desc.shash.tfm = sbi->s_chksum_driver;
  1073. desc.shash.flags = 0;
  1074. *(u32 *)desc.ctx = crc;
  1075. err = crypto_shash_update(&desc.shash, address, length);
  1076. BUG_ON(err);
  1077. return *(u32 *)desc.ctx;
  1078. }
  1079. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  1080. {
  1081. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  1082. }
  1083. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  1084. {
  1085. return sb->s_fs_info;
  1086. }
  1087. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  1088. {
  1089. return F2FS_SB(inode->i_sb);
  1090. }
  1091. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  1092. {
  1093. return F2FS_I_SB(mapping->host);
  1094. }
  1095. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  1096. {
  1097. return F2FS_M_SB(page->mapping);
  1098. }
  1099. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  1100. {
  1101. return (struct f2fs_super_block *)(sbi->raw_super);
  1102. }
  1103. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  1104. {
  1105. return (struct f2fs_checkpoint *)(sbi->ckpt);
  1106. }
  1107. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  1108. {
  1109. return (struct f2fs_node *)page_address(page);
  1110. }
  1111. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  1112. {
  1113. return &((struct f2fs_node *)page_address(page))->i;
  1114. }
  1115. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  1116. {
  1117. return (struct f2fs_nm_info *)(sbi->nm_info);
  1118. }
  1119. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  1120. {
  1121. return (struct f2fs_sm_info *)(sbi->sm_info);
  1122. }
  1123. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  1124. {
  1125. return (struct sit_info *)(SM_I(sbi)->sit_info);
  1126. }
  1127. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  1128. {
  1129. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  1130. }
  1131. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  1132. {
  1133. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  1134. }
  1135. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  1136. {
  1137. return sbi->meta_inode->i_mapping;
  1138. }
  1139. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  1140. {
  1141. return sbi->node_inode->i_mapping;
  1142. }
  1143. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  1144. {
  1145. return test_bit(type, &sbi->s_flag);
  1146. }
  1147. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1148. {
  1149. set_bit(type, &sbi->s_flag);
  1150. }
  1151. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1152. {
  1153. clear_bit(type, &sbi->s_flag);
  1154. }
  1155. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  1156. {
  1157. return le64_to_cpu(cp->checkpoint_ver);
  1158. }
  1159. static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
  1160. {
  1161. size_t crc_offset = le32_to_cpu(cp->checksum_offset);
  1162. return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
  1163. }
  1164. static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1165. {
  1166. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1167. return ckpt_flags & f;
  1168. }
  1169. static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1170. {
  1171. return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
  1172. }
  1173. static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1174. {
  1175. unsigned int ckpt_flags;
  1176. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1177. ckpt_flags |= f;
  1178. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  1179. }
  1180. static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1181. {
  1182. unsigned long flags;
  1183. spin_lock_irqsave(&sbi->cp_lock, flags);
  1184. __set_ckpt_flags(F2FS_CKPT(sbi), f);
  1185. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1186. }
  1187. static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1188. {
  1189. unsigned int ckpt_flags;
  1190. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1191. ckpt_flags &= (~f);
  1192. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  1193. }
  1194. static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1195. {
  1196. unsigned long flags;
  1197. spin_lock_irqsave(&sbi->cp_lock, flags);
  1198. __clear_ckpt_flags(F2FS_CKPT(sbi), f);
  1199. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1200. }
  1201. static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
  1202. {
  1203. unsigned long flags;
  1204. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1205. if (lock)
  1206. spin_lock_irqsave(&sbi->cp_lock, flags);
  1207. __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
  1208. kfree(NM_I(sbi)->nat_bits);
  1209. NM_I(sbi)->nat_bits = NULL;
  1210. if (lock)
  1211. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1212. }
  1213. static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
  1214. struct cp_control *cpc)
  1215. {
  1216. bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
  1217. return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
  1218. }
  1219. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  1220. {
  1221. down_read(&sbi->cp_rwsem);
  1222. }
  1223. static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
  1224. {
  1225. return down_read_trylock(&sbi->cp_rwsem);
  1226. }
  1227. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  1228. {
  1229. up_read(&sbi->cp_rwsem);
  1230. }
  1231. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  1232. {
  1233. down_write(&sbi->cp_rwsem);
  1234. }
  1235. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  1236. {
  1237. up_write(&sbi->cp_rwsem);
  1238. }
  1239. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  1240. {
  1241. int reason = CP_SYNC;
  1242. if (test_opt(sbi, FASTBOOT))
  1243. reason = CP_FASTBOOT;
  1244. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  1245. reason = CP_UMOUNT;
  1246. return reason;
  1247. }
  1248. static inline bool __remain_node_summaries(int reason)
  1249. {
  1250. return (reason & (CP_UMOUNT | CP_FASTBOOT));
  1251. }
  1252. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  1253. {
  1254. return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
  1255. is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
  1256. }
  1257. /*
  1258. * Check whether the given nid is within node id range.
  1259. */
  1260. static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  1261. {
  1262. if (unlikely(nid < F2FS_ROOT_INO(sbi)))
  1263. return -EINVAL;
  1264. if (unlikely(nid >= NM_I(sbi)->max_nid))
  1265. return -EINVAL;
  1266. return 0;
  1267. }
  1268. /*
  1269. * Check whether the inode has blocks or not
  1270. */
  1271. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  1272. {
  1273. block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
  1274. return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
  1275. }
  1276. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  1277. {
  1278. return ofs == XATTR_NODE_OFFSET;
  1279. }
  1280. static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
  1281. static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
  1282. struct inode *inode, blkcnt_t *count)
  1283. {
  1284. blkcnt_t diff = 0, release = 0;
  1285. block_t avail_user_block_count;
  1286. int ret;
  1287. ret = dquot_reserve_block(inode, *count);
  1288. if (ret)
  1289. return ret;
  1290. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1291. if (time_to_inject(sbi, FAULT_BLOCK)) {
  1292. f2fs_show_injection_info(FAULT_BLOCK);
  1293. release = *count;
  1294. goto enospc;
  1295. }
  1296. #endif
  1297. /*
  1298. * let's increase this in prior to actual block count change in order
  1299. * for f2fs_sync_file to avoid data races when deciding checkpoint.
  1300. */
  1301. percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
  1302. spin_lock(&sbi->stat_lock);
  1303. sbi->total_valid_block_count += (block_t)(*count);
  1304. avail_user_block_count = sbi->user_block_count - sbi->reserved_blocks;
  1305. if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
  1306. diff = sbi->total_valid_block_count - avail_user_block_count;
  1307. *count -= diff;
  1308. release = diff;
  1309. sbi->total_valid_block_count = avail_user_block_count;
  1310. if (!*count) {
  1311. spin_unlock(&sbi->stat_lock);
  1312. percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
  1313. goto enospc;
  1314. }
  1315. }
  1316. spin_unlock(&sbi->stat_lock);
  1317. if (release)
  1318. dquot_release_reservation_block(inode, release);
  1319. f2fs_i_blocks_write(inode, *count, true, true);
  1320. return 0;
  1321. enospc:
  1322. dquot_release_reservation_block(inode, release);
  1323. return -ENOSPC;
  1324. }
  1325. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  1326. struct inode *inode,
  1327. block_t count)
  1328. {
  1329. blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
  1330. spin_lock(&sbi->stat_lock);
  1331. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  1332. f2fs_bug_on(sbi, inode->i_blocks < sectors);
  1333. sbi->total_valid_block_count -= (block_t)count;
  1334. spin_unlock(&sbi->stat_lock);
  1335. f2fs_i_blocks_write(inode, count, false, true);
  1336. }
  1337. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  1338. {
  1339. atomic_inc(&sbi->nr_pages[count_type]);
  1340. if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
  1341. count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
  1342. return;
  1343. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1344. }
  1345. static inline void inode_inc_dirty_pages(struct inode *inode)
  1346. {
  1347. atomic_inc(&F2FS_I(inode)->dirty_pages);
  1348. inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1349. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1350. }
  1351. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  1352. {
  1353. atomic_dec(&sbi->nr_pages[count_type]);
  1354. }
  1355. static inline void inode_dec_dirty_pages(struct inode *inode)
  1356. {
  1357. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  1358. !S_ISLNK(inode->i_mode))
  1359. return;
  1360. atomic_dec(&F2FS_I(inode)->dirty_pages);
  1361. dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1362. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1363. }
  1364. static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
  1365. {
  1366. return atomic_read(&sbi->nr_pages[count_type]);
  1367. }
  1368. static inline int get_dirty_pages(struct inode *inode)
  1369. {
  1370. return atomic_read(&F2FS_I(inode)->dirty_pages);
  1371. }
  1372. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  1373. {
  1374. unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
  1375. unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
  1376. sbi->log_blocks_per_seg;
  1377. return segs / sbi->segs_per_sec;
  1378. }
  1379. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  1380. {
  1381. return sbi->total_valid_block_count;
  1382. }
  1383. static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
  1384. {
  1385. return sbi->discard_blks;
  1386. }
  1387. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  1388. {
  1389. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1390. /* return NAT or SIT bitmap */
  1391. if (flag == NAT_BITMAP)
  1392. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  1393. else if (flag == SIT_BITMAP)
  1394. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  1395. return 0;
  1396. }
  1397. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  1398. {
  1399. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  1400. }
  1401. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  1402. {
  1403. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1404. int offset;
  1405. if (__cp_payload(sbi) > 0) {
  1406. if (flag == NAT_BITMAP)
  1407. return &ckpt->sit_nat_version_bitmap;
  1408. else
  1409. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  1410. } else {
  1411. offset = (flag == NAT_BITMAP) ?
  1412. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  1413. return &ckpt->sit_nat_version_bitmap + offset;
  1414. }
  1415. }
  1416. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  1417. {
  1418. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1419. if (sbi->cur_cp_pack == 2)
  1420. start_addr += sbi->blocks_per_seg;
  1421. return start_addr;
  1422. }
  1423. static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
  1424. {
  1425. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1426. if (sbi->cur_cp_pack == 1)
  1427. start_addr += sbi->blocks_per_seg;
  1428. return start_addr;
  1429. }
  1430. static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
  1431. {
  1432. sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
  1433. }
  1434. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  1435. {
  1436. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  1437. }
  1438. static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
  1439. struct inode *inode, bool is_inode)
  1440. {
  1441. block_t valid_block_count;
  1442. unsigned int valid_node_count;
  1443. bool quota = inode && !is_inode;
  1444. if (quota) {
  1445. int ret = dquot_reserve_block(inode, 1);
  1446. if (ret)
  1447. return ret;
  1448. }
  1449. spin_lock(&sbi->stat_lock);
  1450. valid_block_count = sbi->total_valid_block_count + 1;
  1451. if (unlikely(valid_block_count + sbi->reserved_blocks >
  1452. sbi->user_block_count)) {
  1453. spin_unlock(&sbi->stat_lock);
  1454. goto enospc;
  1455. }
  1456. valid_node_count = sbi->total_valid_node_count + 1;
  1457. if (unlikely(valid_node_count > sbi->total_node_count)) {
  1458. spin_unlock(&sbi->stat_lock);
  1459. goto enospc;
  1460. }
  1461. sbi->total_valid_node_count++;
  1462. sbi->total_valid_block_count++;
  1463. spin_unlock(&sbi->stat_lock);
  1464. if (inode) {
  1465. if (is_inode)
  1466. f2fs_mark_inode_dirty_sync(inode, true);
  1467. else
  1468. f2fs_i_blocks_write(inode, 1, true, true);
  1469. }
  1470. percpu_counter_inc(&sbi->alloc_valid_block_count);
  1471. return 0;
  1472. enospc:
  1473. if (quota)
  1474. dquot_release_reservation_block(inode, 1);
  1475. return -ENOSPC;
  1476. }
  1477. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  1478. struct inode *inode, bool is_inode)
  1479. {
  1480. spin_lock(&sbi->stat_lock);
  1481. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  1482. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  1483. f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
  1484. sbi->total_valid_node_count--;
  1485. sbi->total_valid_block_count--;
  1486. spin_unlock(&sbi->stat_lock);
  1487. if (!is_inode)
  1488. f2fs_i_blocks_write(inode, 1, false, true);
  1489. }
  1490. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  1491. {
  1492. return sbi->total_valid_node_count;
  1493. }
  1494. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  1495. {
  1496. percpu_counter_inc(&sbi->total_valid_inode_count);
  1497. }
  1498. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  1499. {
  1500. percpu_counter_dec(&sbi->total_valid_inode_count);
  1501. }
  1502. static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
  1503. {
  1504. return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
  1505. }
  1506. static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
  1507. pgoff_t index, bool for_write)
  1508. {
  1509. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1510. struct page *page = find_lock_page(mapping, index);
  1511. if (page)
  1512. return page;
  1513. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
  1514. f2fs_show_injection_info(FAULT_PAGE_ALLOC);
  1515. return NULL;
  1516. }
  1517. #endif
  1518. if (!for_write)
  1519. return grab_cache_page(mapping, index);
  1520. return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
  1521. }
  1522. static inline void f2fs_copy_page(struct page *src, struct page *dst)
  1523. {
  1524. char *src_kaddr = kmap(src);
  1525. char *dst_kaddr = kmap(dst);
  1526. memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
  1527. kunmap(dst);
  1528. kunmap(src);
  1529. }
  1530. static inline void f2fs_put_page(struct page *page, int unlock)
  1531. {
  1532. if (!page)
  1533. return;
  1534. if (unlock) {
  1535. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  1536. unlock_page(page);
  1537. }
  1538. put_page(page);
  1539. }
  1540. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  1541. {
  1542. if (dn->node_page)
  1543. f2fs_put_page(dn->node_page, 1);
  1544. if (dn->inode_page && dn->node_page != dn->inode_page)
  1545. f2fs_put_page(dn->inode_page, 0);
  1546. dn->node_page = NULL;
  1547. dn->inode_page = NULL;
  1548. }
  1549. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  1550. size_t size)
  1551. {
  1552. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1553. }
  1554. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  1555. gfp_t flags)
  1556. {
  1557. void *entry;
  1558. entry = kmem_cache_alloc(cachep, flags);
  1559. if (!entry)
  1560. entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
  1561. return entry;
  1562. }
  1563. static inline struct bio *f2fs_bio_alloc(int npages)
  1564. {
  1565. struct bio *bio;
  1566. /* No failure on bio allocation */
  1567. bio = bio_alloc(GFP_NOIO, npages);
  1568. if (!bio)
  1569. bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
  1570. return bio;
  1571. }
  1572. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  1573. unsigned long index, void *item)
  1574. {
  1575. while (radix_tree_insert(root, index, item))
  1576. cond_resched();
  1577. }
  1578. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  1579. static inline bool IS_INODE(struct page *page)
  1580. {
  1581. struct f2fs_node *p = F2FS_NODE(page);
  1582. return RAW_IS_INODE(p);
  1583. }
  1584. static inline int offset_in_addr(struct f2fs_inode *i)
  1585. {
  1586. return (i->i_inline & F2FS_EXTRA_ATTR) ?
  1587. (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
  1588. }
  1589. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  1590. {
  1591. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  1592. }
  1593. static inline int f2fs_has_extra_attr(struct inode *inode);
  1594. static inline block_t datablock_addr(struct inode *inode,
  1595. struct page *node_page, unsigned int offset)
  1596. {
  1597. struct f2fs_node *raw_node;
  1598. __le32 *addr_array;
  1599. int base = 0;
  1600. bool is_inode = IS_INODE(node_page);
  1601. raw_node = F2FS_NODE(node_page);
  1602. /* from GC path only */
  1603. if (!inode) {
  1604. if (is_inode)
  1605. base = offset_in_addr(&raw_node->i);
  1606. } else if (f2fs_has_extra_attr(inode) && is_inode) {
  1607. base = get_extra_isize(inode);
  1608. }
  1609. addr_array = blkaddr_in_node(raw_node);
  1610. return le32_to_cpu(addr_array[base + offset]);
  1611. }
  1612. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  1613. {
  1614. int mask;
  1615. addr += (nr >> 3);
  1616. mask = 1 << (7 - (nr & 0x07));
  1617. return mask & *addr;
  1618. }
  1619. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  1620. {
  1621. int mask;
  1622. addr += (nr >> 3);
  1623. mask = 1 << (7 - (nr & 0x07));
  1624. *addr |= mask;
  1625. }
  1626. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  1627. {
  1628. int mask;
  1629. addr += (nr >> 3);
  1630. mask = 1 << (7 - (nr & 0x07));
  1631. *addr &= ~mask;
  1632. }
  1633. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  1634. {
  1635. int mask;
  1636. int ret;
  1637. addr += (nr >> 3);
  1638. mask = 1 << (7 - (nr & 0x07));
  1639. ret = mask & *addr;
  1640. *addr |= mask;
  1641. return ret;
  1642. }
  1643. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  1644. {
  1645. int mask;
  1646. int ret;
  1647. addr += (nr >> 3);
  1648. mask = 1 << (7 - (nr & 0x07));
  1649. ret = mask & *addr;
  1650. *addr &= ~mask;
  1651. return ret;
  1652. }
  1653. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  1654. {
  1655. int mask;
  1656. addr += (nr >> 3);
  1657. mask = 1 << (7 - (nr & 0x07));
  1658. *addr ^= mask;
  1659. }
  1660. #define F2FS_REG_FLMASK (~(FS_DIRSYNC_FL | FS_TOPDIR_FL))
  1661. #define F2FS_OTHER_FLMASK (FS_NODUMP_FL | FS_NOATIME_FL)
  1662. #define F2FS_FL_INHERITED (FS_PROJINHERIT_FL)
  1663. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  1664. {
  1665. if (S_ISDIR(mode))
  1666. return flags;
  1667. else if (S_ISREG(mode))
  1668. return flags & F2FS_REG_FLMASK;
  1669. else
  1670. return flags & F2FS_OTHER_FLMASK;
  1671. }
  1672. /* used for f2fs_inode_info->flags */
  1673. enum {
  1674. FI_NEW_INODE, /* indicate newly allocated inode */
  1675. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  1676. FI_AUTO_RECOVER, /* indicate inode is recoverable */
  1677. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  1678. FI_INC_LINK, /* need to increment i_nlink */
  1679. FI_ACL_MODE, /* indicate acl mode */
  1680. FI_NO_ALLOC, /* should not allocate any blocks */
  1681. FI_FREE_NID, /* free allocated nide */
  1682. FI_NO_EXTENT, /* not to use the extent cache */
  1683. FI_INLINE_XATTR, /* used for inline xattr */
  1684. FI_INLINE_DATA, /* used for inline data*/
  1685. FI_INLINE_DENTRY, /* used for inline dentry */
  1686. FI_APPEND_WRITE, /* inode has appended data */
  1687. FI_UPDATE_WRITE, /* inode has in-place-update data */
  1688. FI_NEED_IPU, /* used for ipu per file */
  1689. FI_ATOMIC_FILE, /* indicate atomic file */
  1690. FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
  1691. FI_VOLATILE_FILE, /* indicate volatile file */
  1692. FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
  1693. FI_DROP_CACHE, /* drop dirty page cache */
  1694. FI_DATA_EXIST, /* indicate data exists */
  1695. FI_INLINE_DOTS, /* indicate inline dot dentries */
  1696. FI_DO_DEFRAG, /* indicate defragment is running */
  1697. FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
  1698. FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
  1699. FI_HOT_DATA, /* indicate file is hot */
  1700. FI_EXTRA_ATTR, /* indicate file has extra attribute */
  1701. FI_PROJ_INHERIT, /* indicate file inherits projectid */
  1702. };
  1703. static inline void __mark_inode_dirty_flag(struct inode *inode,
  1704. int flag, bool set)
  1705. {
  1706. switch (flag) {
  1707. case FI_INLINE_XATTR:
  1708. case FI_INLINE_DATA:
  1709. case FI_INLINE_DENTRY:
  1710. if (set)
  1711. return;
  1712. case FI_DATA_EXIST:
  1713. case FI_INLINE_DOTS:
  1714. f2fs_mark_inode_dirty_sync(inode, true);
  1715. }
  1716. }
  1717. static inline void set_inode_flag(struct inode *inode, int flag)
  1718. {
  1719. if (!test_bit(flag, &F2FS_I(inode)->flags))
  1720. set_bit(flag, &F2FS_I(inode)->flags);
  1721. __mark_inode_dirty_flag(inode, flag, true);
  1722. }
  1723. static inline int is_inode_flag_set(struct inode *inode, int flag)
  1724. {
  1725. return test_bit(flag, &F2FS_I(inode)->flags);
  1726. }
  1727. static inline void clear_inode_flag(struct inode *inode, int flag)
  1728. {
  1729. if (test_bit(flag, &F2FS_I(inode)->flags))
  1730. clear_bit(flag, &F2FS_I(inode)->flags);
  1731. __mark_inode_dirty_flag(inode, flag, false);
  1732. }
  1733. static inline void set_acl_inode(struct inode *inode, umode_t mode)
  1734. {
  1735. F2FS_I(inode)->i_acl_mode = mode;
  1736. set_inode_flag(inode, FI_ACL_MODE);
  1737. f2fs_mark_inode_dirty_sync(inode, false);
  1738. }
  1739. static inline void f2fs_i_links_write(struct inode *inode, bool inc)
  1740. {
  1741. if (inc)
  1742. inc_nlink(inode);
  1743. else
  1744. drop_nlink(inode);
  1745. f2fs_mark_inode_dirty_sync(inode, true);
  1746. }
  1747. static inline void f2fs_i_blocks_write(struct inode *inode,
  1748. block_t diff, bool add, bool claim)
  1749. {
  1750. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  1751. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  1752. /* add = 1, claim = 1 should be dquot_reserve_block in pair */
  1753. if (add) {
  1754. if (claim)
  1755. dquot_claim_block(inode, diff);
  1756. else
  1757. dquot_alloc_block_nofail(inode, diff);
  1758. } else {
  1759. dquot_free_block(inode, diff);
  1760. }
  1761. f2fs_mark_inode_dirty_sync(inode, true);
  1762. if (clean || recover)
  1763. set_inode_flag(inode, FI_AUTO_RECOVER);
  1764. }
  1765. static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
  1766. {
  1767. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  1768. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  1769. if (i_size_read(inode) == i_size)
  1770. return;
  1771. i_size_write(inode, i_size);
  1772. f2fs_mark_inode_dirty_sync(inode, true);
  1773. if (clean || recover)
  1774. set_inode_flag(inode, FI_AUTO_RECOVER);
  1775. }
  1776. static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
  1777. {
  1778. F2FS_I(inode)->i_current_depth = depth;
  1779. f2fs_mark_inode_dirty_sync(inode, true);
  1780. }
  1781. static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
  1782. {
  1783. F2FS_I(inode)->i_xattr_nid = xnid;
  1784. f2fs_mark_inode_dirty_sync(inode, true);
  1785. }
  1786. static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
  1787. {
  1788. F2FS_I(inode)->i_pino = pino;
  1789. f2fs_mark_inode_dirty_sync(inode, true);
  1790. }
  1791. static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
  1792. {
  1793. struct f2fs_inode_info *fi = F2FS_I(inode);
  1794. if (ri->i_inline & F2FS_INLINE_XATTR)
  1795. set_bit(FI_INLINE_XATTR, &fi->flags);
  1796. if (ri->i_inline & F2FS_INLINE_DATA)
  1797. set_bit(FI_INLINE_DATA, &fi->flags);
  1798. if (ri->i_inline & F2FS_INLINE_DENTRY)
  1799. set_bit(FI_INLINE_DENTRY, &fi->flags);
  1800. if (ri->i_inline & F2FS_DATA_EXIST)
  1801. set_bit(FI_DATA_EXIST, &fi->flags);
  1802. if (ri->i_inline & F2FS_INLINE_DOTS)
  1803. set_bit(FI_INLINE_DOTS, &fi->flags);
  1804. if (ri->i_inline & F2FS_EXTRA_ATTR)
  1805. set_bit(FI_EXTRA_ATTR, &fi->flags);
  1806. }
  1807. static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
  1808. {
  1809. ri->i_inline = 0;
  1810. if (is_inode_flag_set(inode, FI_INLINE_XATTR))
  1811. ri->i_inline |= F2FS_INLINE_XATTR;
  1812. if (is_inode_flag_set(inode, FI_INLINE_DATA))
  1813. ri->i_inline |= F2FS_INLINE_DATA;
  1814. if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
  1815. ri->i_inline |= F2FS_INLINE_DENTRY;
  1816. if (is_inode_flag_set(inode, FI_DATA_EXIST))
  1817. ri->i_inline |= F2FS_DATA_EXIST;
  1818. if (is_inode_flag_set(inode, FI_INLINE_DOTS))
  1819. ri->i_inline |= F2FS_INLINE_DOTS;
  1820. if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
  1821. ri->i_inline |= F2FS_EXTRA_ATTR;
  1822. }
  1823. static inline int f2fs_has_extra_attr(struct inode *inode)
  1824. {
  1825. return is_inode_flag_set(inode, FI_EXTRA_ATTR);
  1826. }
  1827. static inline int f2fs_has_inline_xattr(struct inode *inode)
  1828. {
  1829. return is_inode_flag_set(inode, FI_INLINE_XATTR);
  1830. }
  1831. static inline unsigned int addrs_per_inode(struct inode *inode)
  1832. {
  1833. if (f2fs_has_inline_xattr(inode))
  1834. return CUR_ADDRS_PER_INODE(inode) - F2FS_INLINE_XATTR_ADDRS;
  1835. return CUR_ADDRS_PER_INODE(inode);
  1836. }
  1837. static inline void *inline_xattr_addr(struct page *page)
  1838. {
  1839. struct f2fs_inode *ri = F2FS_INODE(page);
  1840. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  1841. F2FS_INLINE_XATTR_ADDRS]);
  1842. }
  1843. static inline int inline_xattr_size(struct inode *inode)
  1844. {
  1845. if (f2fs_has_inline_xattr(inode))
  1846. return F2FS_INLINE_XATTR_ADDRS << 2;
  1847. else
  1848. return 0;
  1849. }
  1850. static inline int f2fs_has_inline_data(struct inode *inode)
  1851. {
  1852. return is_inode_flag_set(inode, FI_INLINE_DATA);
  1853. }
  1854. static inline int f2fs_exist_data(struct inode *inode)
  1855. {
  1856. return is_inode_flag_set(inode, FI_DATA_EXIST);
  1857. }
  1858. static inline int f2fs_has_inline_dots(struct inode *inode)
  1859. {
  1860. return is_inode_flag_set(inode, FI_INLINE_DOTS);
  1861. }
  1862. static inline bool f2fs_is_atomic_file(struct inode *inode)
  1863. {
  1864. return is_inode_flag_set(inode, FI_ATOMIC_FILE);
  1865. }
  1866. static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
  1867. {
  1868. return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
  1869. }
  1870. static inline bool f2fs_is_volatile_file(struct inode *inode)
  1871. {
  1872. return is_inode_flag_set(inode, FI_VOLATILE_FILE);
  1873. }
  1874. static inline bool f2fs_is_first_block_written(struct inode *inode)
  1875. {
  1876. return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
  1877. }
  1878. static inline bool f2fs_is_drop_cache(struct inode *inode)
  1879. {
  1880. return is_inode_flag_set(inode, FI_DROP_CACHE);
  1881. }
  1882. static inline void *inline_data_addr(struct inode *inode, struct page *page)
  1883. {
  1884. struct f2fs_inode *ri = F2FS_INODE(page);
  1885. int extra_size = get_extra_isize(inode);
  1886. return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
  1887. }
  1888. static inline int f2fs_has_inline_dentry(struct inode *inode)
  1889. {
  1890. return is_inode_flag_set(inode, FI_INLINE_DENTRY);
  1891. }
  1892. static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
  1893. {
  1894. if (!f2fs_has_inline_dentry(dir))
  1895. kunmap(page);
  1896. }
  1897. static inline int is_file(struct inode *inode, int type)
  1898. {
  1899. return F2FS_I(inode)->i_advise & type;
  1900. }
  1901. static inline void set_file(struct inode *inode, int type)
  1902. {
  1903. F2FS_I(inode)->i_advise |= type;
  1904. f2fs_mark_inode_dirty_sync(inode, true);
  1905. }
  1906. static inline void clear_file(struct inode *inode, int type)
  1907. {
  1908. F2FS_I(inode)->i_advise &= ~type;
  1909. f2fs_mark_inode_dirty_sync(inode, true);
  1910. }
  1911. static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
  1912. {
  1913. if (dsync) {
  1914. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1915. bool ret;
  1916. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1917. ret = list_empty(&F2FS_I(inode)->gdirty_list);
  1918. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1919. return ret;
  1920. }
  1921. if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
  1922. file_keep_isize(inode) ||
  1923. i_size_read(inode) & PAGE_MASK)
  1924. return false;
  1925. return F2FS_I(inode)->last_disk_size == i_size_read(inode);
  1926. }
  1927. static inline int f2fs_readonly(struct super_block *sb)
  1928. {
  1929. return sb->s_flags & MS_RDONLY;
  1930. }
  1931. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  1932. {
  1933. return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
  1934. }
  1935. static inline bool is_dot_dotdot(const struct qstr *str)
  1936. {
  1937. if (str->len == 1 && str->name[0] == '.')
  1938. return true;
  1939. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  1940. return true;
  1941. return false;
  1942. }
  1943. static inline bool f2fs_may_extent_tree(struct inode *inode)
  1944. {
  1945. if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
  1946. is_inode_flag_set(inode, FI_NO_EXTENT))
  1947. return false;
  1948. return S_ISREG(inode->i_mode);
  1949. }
  1950. static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
  1951. size_t size, gfp_t flags)
  1952. {
  1953. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1954. if (time_to_inject(sbi, FAULT_KMALLOC)) {
  1955. f2fs_show_injection_info(FAULT_KMALLOC);
  1956. return NULL;
  1957. }
  1958. #endif
  1959. return kmalloc(size, flags);
  1960. }
  1961. static inline int get_extra_isize(struct inode *inode)
  1962. {
  1963. return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
  1964. }
  1965. #define get_inode_mode(i) \
  1966. ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
  1967. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  1968. #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
  1969. (offsetof(struct f2fs_inode, i_extra_end) - \
  1970. offsetof(struct f2fs_inode, i_extra_isize)) \
  1971. #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
  1972. #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
  1973. ((offsetof(typeof(*f2fs_inode), field) + \
  1974. sizeof((f2fs_inode)->field)) \
  1975. <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
  1976. static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
  1977. {
  1978. int i;
  1979. spin_lock(&sbi->iostat_lock);
  1980. for (i = 0; i < NR_IO_TYPE; i++)
  1981. sbi->write_iostat[i] = 0;
  1982. spin_unlock(&sbi->iostat_lock);
  1983. }
  1984. static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
  1985. enum iostat_type type, unsigned long long io_bytes)
  1986. {
  1987. if (!sbi->iostat_enable)
  1988. return;
  1989. spin_lock(&sbi->iostat_lock);
  1990. sbi->write_iostat[type] += io_bytes;
  1991. if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
  1992. sbi->write_iostat[APP_BUFFERED_IO] =
  1993. sbi->write_iostat[APP_WRITE_IO] -
  1994. sbi->write_iostat[APP_DIRECT_IO];
  1995. spin_unlock(&sbi->iostat_lock);
  1996. }
  1997. /*
  1998. * file.c
  1999. */
  2000. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
  2001. void truncate_data_blocks(struct dnode_of_data *dn);
  2002. int truncate_blocks(struct inode *inode, u64 from, bool lock);
  2003. int f2fs_truncate(struct inode *inode);
  2004. int f2fs_getattr(const struct path *path, struct kstat *stat,
  2005. u32 request_mask, unsigned int flags);
  2006. int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
  2007. int truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
  2008. int truncate_data_blocks_range(struct dnode_of_data *dn, int count);
  2009. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  2010. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  2011. /*
  2012. * inode.c
  2013. */
  2014. void f2fs_set_inode_flags(struct inode *inode);
  2015. bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
  2016. void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
  2017. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
  2018. struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
  2019. int try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
  2020. int update_inode(struct inode *inode, struct page *node_page);
  2021. int update_inode_page(struct inode *inode);
  2022. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
  2023. void f2fs_evict_inode(struct inode *inode);
  2024. void handle_failed_inode(struct inode *inode);
  2025. /*
  2026. * namei.c
  2027. */
  2028. struct dentry *f2fs_get_parent(struct dentry *child);
  2029. /*
  2030. * dir.c
  2031. */
  2032. void set_de_type(struct f2fs_dir_entry *de, umode_t mode);
  2033. unsigned char get_de_type(struct f2fs_dir_entry *de);
  2034. struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *fname,
  2035. f2fs_hash_t namehash, int *max_slots,
  2036. struct f2fs_dentry_ptr *d);
  2037. int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  2038. unsigned int start_pos, struct fscrypt_str *fstr);
  2039. void do_make_empty_dir(struct inode *inode, struct inode *parent,
  2040. struct f2fs_dentry_ptr *d);
  2041. struct page *init_inode_metadata(struct inode *inode, struct inode *dir,
  2042. const struct qstr *new_name,
  2043. const struct qstr *orig_name, struct page *dpage);
  2044. void update_parent_metadata(struct inode *dir, struct inode *inode,
  2045. unsigned int current_depth);
  2046. int room_for_filename(const void *bitmap, int slots, int max_slots);
  2047. void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
  2048. struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
  2049. struct fscrypt_name *fname, struct page **res_page);
  2050. struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
  2051. const struct qstr *child, struct page **res_page);
  2052. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
  2053. ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
  2054. struct page **page);
  2055. void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
  2056. struct page *page, struct inode *inode);
  2057. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
  2058. const struct qstr *name, f2fs_hash_t name_hash,
  2059. unsigned int bit_pos);
  2060. int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
  2061. const struct qstr *orig_name,
  2062. struct inode *inode, nid_t ino, umode_t mode);
  2063. int __f2fs_do_add_link(struct inode *dir, struct fscrypt_name *fname,
  2064. struct inode *inode, nid_t ino, umode_t mode);
  2065. int __f2fs_add_link(struct inode *dir, const struct qstr *name,
  2066. struct inode *inode, nid_t ino, umode_t mode);
  2067. void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
  2068. struct inode *dir, struct inode *inode);
  2069. int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
  2070. bool f2fs_empty_dir(struct inode *dir);
  2071. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  2072. {
  2073. return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  2074. inode, inode->i_ino, inode->i_mode);
  2075. }
  2076. /*
  2077. * super.c
  2078. */
  2079. int f2fs_inode_dirtied(struct inode *inode, bool sync);
  2080. void f2fs_inode_synced(struct inode *inode);
  2081. void f2fs_enable_quota_files(struct f2fs_sb_info *sbi);
  2082. void f2fs_quota_off_umount(struct super_block *sb);
  2083. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
  2084. int f2fs_sync_fs(struct super_block *sb, int sync);
  2085. extern __printf(3, 4)
  2086. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  2087. int sanity_check_ckpt(struct f2fs_sb_info *sbi);
  2088. /*
  2089. * hash.c
  2090. */
  2091. f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
  2092. struct fscrypt_name *fname);
  2093. /*
  2094. * node.c
  2095. */
  2096. struct dnode_of_data;
  2097. struct node_info;
  2098. bool available_free_memory(struct f2fs_sb_info *sbi, int type);
  2099. int need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
  2100. bool is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
  2101. bool need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
  2102. void get_node_info(struct f2fs_sb_info *sbi, nid_t nid, struct node_info *ni);
  2103. pgoff_t get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
  2104. int get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
  2105. int truncate_inode_blocks(struct inode *inode, pgoff_t from);
  2106. int truncate_xattr_node(struct inode *inode, struct page *page);
  2107. int wait_on_node_pages_writeback(struct f2fs_sb_info *sbi, nid_t ino);
  2108. int remove_inode_page(struct inode *inode);
  2109. struct page *new_inode_page(struct inode *inode);
  2110. struct page *new_node_page(struct dnode_of_data *dn, unsigned int ofs);
  2111. void ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
  2112. struct page *get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
  2113. struct page *get_node_page_ra(struct page *parent, int start);
  2114. void move_node_page(struct page *node_page, int gc_type);
  2115. int fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  2116. struct writeback_control *wbc, bool atomic);
  2117. int sync_node_pages(struct f2fs_sb_info *sbi, struct writeback_control *wbc,
  2118. bool do_balance, enum iostat_type io_type);
  2119. void build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
  2120. bool alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
  2121. void alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
  2122. void alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
  2123. int try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
  2124. void recover_inline_xattr(struct inode *inode, struct page *page);
  2125. int recover_xattr_data(struct inode *inode, struct page *page,
  2126. block_t blkaddr);
  2127. int recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
  2128. int restore_node_summary(struct f2fs_sb_info *sbi,
  2129. unsigned int segno, struct f2fs_summary_block *sum);
  2130. void flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2131. int build_node_manager(struct f2fs_sb_info *sbi);
  2132. void destroy_node_manager(struct f2fs_sb_info *sbi);
  2133. int __init create_node_manager_caches(void);
  2134. void destroy_node_manager_caches(void);
  2135. /*
  2136. * segment.c
  2137. */
  2138. bool need_SSR(struct f2fs_sb_info *sbi);
  2139. void register_inmem_page(struct inode *inode, struct page *page);
  2140. void drop_inmem_pages(struct inode *inode);
  2141. void drop_inmem_page(struct inode *inode, struct page *page);
  2142. int commit_inmem_pages(struct inode *inode);
  2143. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
  2144. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
  2145. int f2fs_issue_flush(struct f2fs_sb_info *sbi);
  2146. int create_flush_cmd_control(struct f2fs_sb_info *sbi);
  2147. void destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
  2148. void invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
  2149. bool is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
  2150. void refresh_sit_entry(struct f2fs_sb_info *sbi, block_t old, block_t new);
  2151. void stop_discard_thread(struct f2fs_sb_info *sbi);
  2152. void f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
  2153. void clear_prefree_segments(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2154. void release_discard_addrs(struct f2fs_sb_info *sbi);
  2155. int npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
  2156. void allocate_new_segments(struct f2fs_sb_info *sbi);
  2157. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
  2158. bool exist_trim_candidates(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2159. struct page *get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
  2160. void update_meta_page(struct f2fs_sb_info *sbi, void *src, block_t blk_addr);
  2161. void write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
  2162. enum iostat_type io_type);
  2163. void write_node_page(unsigned int nid, struct f2fs_io_info *fio);
  2164. void write_data_page(struct dnode_of_data *dn, struct f2fs_io_info *fio);
  2165. int rewrite_data_page(struct f2fs_io_info *fio);
  2166. void __f2fs_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  2167. block_t old_blkaddr, block_t new_blkaddr,
  2168. bool recover_curseg, bool recover_newaddr);
  2169. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  2170. block_t old_addr, block_t new_addr,
  2171. unsigned char version, bool recover_curseg,
  2172. bool recover_newaddr);
  2173. void allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  2174. block_t old_blkaddr, block_t *new_blkaddr,
  2175. struct f2fs_summary *sum, int type,
  2176. struct f2fs_io_info *fio, bool add_list);
  2177. void f2fs_wait_on_page_writeback(struct page *page,
  2178. enum page_type type, bool ordered);
  2179. void f2fs_wait_on_block_writeback(struct f2fs_sb_info *sbi, block_t blkaddr);
  2180. void write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2181. void write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2182. int lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  2183. unsigned int val, int alloc);
  2184. void flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2185. int build_segment_manager(struct f2fs_sb_info *sbi);
  2186. void destroy_segment_manager(struct f2fs_sb_info *sbi);
  2187. int __init create_segment_manager_caches(void);
  2188. void destroy_segment_manager_caches(void);
  2189. /*
  2190. * checkpoint.c
  2191. */
  2192. void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
  2193. struct page *grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2194. struct page *get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2195. struct page *get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2196. bool is_valid_blkaddr(struct f2fs_sb_info *sbi, block_t blkaddr, int type);
  2197. int ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  2198. int type, bool sync);
  2199. void ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
  2200. long sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  2201. long nr_to_write, enum iostat_type io_type);
  2202. void add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2203. void remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2204. void release_ino_entry(struct f2fs_sb_info *sbi, bool all);
  2205. bool exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
  2206. int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
  2207. int acquire_orphan_inode(struct f2fs_sb_info *sbi);
  2208. void release_orphan_inode(struct f2fs_sb_info *sbi);
  2209. void add_orphan_inode(struct inode *inode);
  2210. void remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
  2211. int recover_orphan_inodes(struct f2fs_sb_info *sbi);
  2212. int get_valid_checkpoint(struct f2fs_sb_info *sbi);
  2213. void update_dirty_page(struct inode *inode, struct page *page);
  2214. void remove_dirty_inode(struct inode *inode);
  2215. int sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
  2216. int write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2217. void init_ino_entry_info(struct f2fs_sb_info *sbi);
  2218. int __init create_checkpoint_caches(void);
  2219. void destroy_checkpoint_caches(void);
  2220. /*
  2221. * data.c
  2222. */
  2223. void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
  2224. void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
  2225. struct inode *inode, nid_t ino, pgoff_t idx,
  2226. enum page_type type);
  2227. void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
  2228. int f2fs_submit_page_bio(struct f2fs_io_info *fio);
  2229. int f2fs_submit_page_write(struct f2fs_io_info *fio);
  2230. struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
  2231. block_t blk_addr, struct bio *bio);
  2232. int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
  2233. void set_data_blkaddr(struct dnode_of_data *dn);
  2234. void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
  2235. int reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
  2236. int reserve_new_block(struct dnode_of_data *dn);
  2237. int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
  2238. int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
  2239. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
  2240. struct page *get_read_data_page(struct inode *inode, pgoff_t index,
  2241. int op_flags, bool for_write);
  2242. struct page *find_data_page(struct inode *inode, pgoff_t index);
  2243. struct page *get_lock_data_page(struct inode *inode, pgoff_t index,
  2244. bool for_write);
  2245. struct page *get_new_data_page(struct inode *inode,
  2246. struct page *ipage, pgoff_t index, bool new_i_size);
  2247. int do_write_data_page(struct f2fs_io_info *fio);
  2248. int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
  2249. int create, int flag);
  2250. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2251. u64 start, u64 len);
  2252. void f2fs_set_page_dirty_nobuffers(struct page *page);
  2253. int __f2fs_write_data_pages(struct address_space *mapping,
  2254. struct writeback_control *wbc,
  2255. enum iostat_type io_type);
  2256. void f2fs_invalidate_page(struct page *page, unsigned int offset,
  2257. unsigned int length);
  2258. int f2fs_release_page(struct page *page, gfp_t wait);
  2259. #ifdef CONFIG_MIGRATION
  2260. int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
  2261. struct page *page, enum migrate_mode mode);
  2262. #endif
  2263. /*
  2264. * gc.c
  2265. */
  2266. int start_gc_thread(struct f2fs_sb_info *sbi);
  2267. void stop_gc_thread(struct f2fs_sb_info *sbi);
  2268. block_t start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
  2269. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
  2270. unsigned int segno);
  2271. void build_gc_manager(struct f2fs_sb_info *sbi);
  2272. /*
  2273. * recovery.c
  2274. */
  2275. int recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
  2276. bool space_for_roll_forward(struct f2fs_sb_info *sbi);
  2277. /*
  2278. * debug.c
  2279. */
  2280. #ifdef CONFIG_F2FS_STAT_FS
  2281. struct f2fs_stat_info {
  2282. struct list_head stat_list;
  2283. struct f2fs_sb_info *sbi;
  2284. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  2285. int main_area_segs, main_area_sections, main_area_zones;
  2286. unsigned long long hit_largest, hit_cached, hit_rbtree;
  2287. unsigned long long hit_total, total_ext;
  2288. int ext_tree, zombie_tree, ext_node;
  2289. int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
  2290. int inmem_pages;
  2291. unsigned int ndirty_dirs, ndirty_files, ndirty_all;
  2292. int nats, dirty_nats, sits, dirty_sits;
  2293. int free_nids, avail_nids, alloc_nids;
  2294. int total_count, utilization;
  2295. int bg_gc, nr_wb_cp_data, nr_wb_data;
  2296. int nr_flushing, nr_flushed, nr_discarding, nr_discarded;
  2297. int nr_discard_cmd;
  2298. unsigned int undiscard_blks;
  2299. int inline_xattr, inline_inode, inline_dir, append, update, orphans;
  2300. int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
  2301. unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
  2302. unsigned int bimodal, avg_vblocks;
  2303. int util_free, util_valid, util_invalid;
  2304. int rsvd_segs, overp_segs;
  2305. int dirty_count, node_pages, meta_pages;
  2306. int prefree_count, call_count, cp_count, bg_cp_count;
  2307. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  2308. int bg_node_segs, bg_data_segs;
  2309. int tot_blks, data_blks, node_blks;
  2310. int bg_data_blks, bg_node_blks;
  2311. int curseg[NR_CURSEG_TYPE];
  2312. int cursec[NR_CURSEG_TYPE];
  2313. int curzone[NR_CURSEG_TYPE];
  2314. unsigned int segment_count[2];
  2315. unsigned int block_count[2];
  2316. unsigned int inplace_count;
  2317. unsigned long long base_mem, cache_mem, page_mem;
  2318. };
  2319. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  2320. {
  2321. return (struct f2fs_stat_info *)sbi->stat_info;
  2322. }
  2323. #define stat_inc_cp_count(si) ((si)->cp_count++)
  2324. #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
  2325. #define stat_inc_call_count(si) ((si)->call_count++)
  2326. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  2327. #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
  2328. #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
  2329. #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
  2330. #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
  2331. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  2332. #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
  2333. #define stat_inc_inline_xattr(inode) \
  2334. do { \
  2335. if (f2fs_has_inline_xattr(inode)) \
  2336. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  2337. } while (0)
  2338. #define stat_dec_inline_xattr(inode) \
  2339. do { \
  2340. if (f2fs_has_inline_xattr(inode)) \
  2341. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  2342. } while (0)
  2343. #define stat_inc_inline_inode(inode) \
  2344. do { \
  2345. if (f2fs_has_inline_data(inode)) \
  2346. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  2347. } while (0)
  2348. #define stat_dec_inline_inode(inode) \
  2349. do { \
  2350. if (f2fs_has_inline_data(inode)) \
  2351. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  2352. } while (0)
  2353. #define stat_inc_inline_dir(inode) \
  2354. do { \
  2355. if (f2fs_has_inline_dentry(inode)) \
  2356. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  2357. } while (0)
  2358. #define stat_dec_inline_dir(inode) \
  2359. do { \
  2360. if (f2fs_has_inline_dentry(inode)) \
  2361. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  2362. } while (0)
  2363. #define stat_inc_seg_type(sbi, curseg) \
  2364. ((sbi)->segment_count[(curseg)->alloc_type]++)
  2365. #define stat_inc_block_count(sbi, curseg) \
  2366. ((sbi)->block_count[(curseg)->alloc_type]++)
  2367. #define stat_inc_inplace_blocks(sbi) \
  2368. (atomic_inc(&(sbi)->inplace_count))
  2369. #define stat_inc_atomic_write(inode) \
  2370. (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
  2371. #define stat_dec_atomic_write(inode) \
  2372. (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
  2373. #define stat_update_max_atomic_write(inode) \
  2374. do { \
  2375. int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
  2376. int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
  2377. if (cur > max) \
  2378. atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
  2379. } while (0)
  2380. #define stat_inc_volatile_write(inode) \
  2381. (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
  2382. #define stat_dec_volatile_write(inode) \
  2383. (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
  2384. #define stat_update_max_volatile_write(inode) \
  2385. do { \
  2386. int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
  2387. int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
  2388. if (cur > max) \
  2389. atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
  2390. } while (0)
  2391. #define stat_inc_seg_count(sbi, type, gc_type) \
  2392. do { \
  2393. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2394. si->tot_segs++; \
  2395. if ((type) == SUM_TYPE_DATA) { \
  2396. si->data_segs++; \
  2397. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  2398. } else { \
  2399. si->node_segs++; \
  2400. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  2401. } \
  2402. } while (0)
  2403. #define stat_inc_tot_blk_count(si, blks) \
  2404. ((si)->tot_blks += (blks))
  2405. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  2406. do { \
  2407. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2408. stat_inc_tot_blk_count(si, blks); \
  2409. si->data_blks += (blks); \
  2410. si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2411. } while (0)
  2412. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  2413. do { \
  2414. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2415. stat_inc_tot_blk_count(si, blks); \
  2416. si->node_blks += (blks); \
  2417. si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2418. } while (0)
  2419. int f2fs_build_stats(struct f2fs_sb_info *sbi);
  2420. void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
  2421. int __init f2fs_create_root_stats(void);
  2422. void f2fs_destroy_root_stats(void);
  2423. #else
  2424. #define stat_inc_cp_count(si) do { } while (0)
  2425. #define stat_inc_bg_cp_count(si) do { } while (0)
  2426. #define stat_inc_call_count(si) do { } while (0)
  2427. #define stat_inc_bggc_count(si) do { } while (0)
  2428. #define stat_inc_dirty_inode(sbi, type) do { } while (0)
  2429. #define stat_dec_dirty_inode(sbi, type) do { } while (0)
  2430. #define stat_inc_total_hit(sb) do { } while (0)
  2431. #define stat_inc_rbtree_node_hit(sb) do { } while (0)
  2432. #define stat_inc_largest_node_hit(sbi) do { } while (0)
  2433. #define stat_inc_cached_node_hit(sbi) do { } while (0)
  2434. #define stat_inc_inline_xattr(inode) do { } while (0)
  2435. #define stat_dec_inline_xattr(inode) do { } while (0)
  2436. #define stat_inc_inline_inode(inode) do { } while (0)
  2437. #define stat_dec_inline_inode(inode) do { } while (0)
  2438. #define stat_inc_inline_dir(inode) do { } while (0)
  2439. #define stat_dec_inline_dir(inode) do { } while (0)
  2440. #define stat_inc_atomic_write(inode) do { } while (0)
  2441. #define stat_dec_atomic_write(inode) do { } while (0)
  2442. #define stat_update_max_atomic_write(inode) do { } while (0)
  2443. #define stat_inc_volatile_write(inode) do { } while (0)
  2444. #define stat_dec_volatile_write(inode) do { } while (0)
  2445. #define stat_update_max_volatile_write(inode) do { } while (0)
  2446. #define stat_inc_seg_type(sbi, curseg) do { } while (0)
  2447. #define stat_inc_block_count(sbi, curseg) do { } while (0)
  2448. #define stat_inc_inplace_blocks(sbi) do { } while (0)
  2449. #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
  2450. #define stat_inc_tot_blk_count(si, blks) do { } while (0)
  2451. #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
  2452. #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
  2453. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  2454. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  2455. static inline int __init f2fs_create_root_stats(void) { return 0; }
  2456. static inline void f2fs_destroy_root_stats(void) { }
  2457. #endif
  2458. extern const struct file_operations f2fs_dir_operations;
  2459. extern const struct file_operations f2fs_file_operations;
  2460. extern const struct inode_operations f2fs_file_inode_operations;
  2461. extern const struct address_space_operations f2fs_dblock_aops;
  2462. extern const struct address_space_operations f2fs_node_aops;
  2463. extern const struct address_space_operations f2fs_meta_aops;
  2464. extern const struct inode_operations f2fs_dir_inode_operations;
  2465. extern const struct inode_operations f2fs_symlink_inode_operations;
  2466. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  2467. extern const struct inode_operations f2fs_special_inode_operations;
  2468. extern struct kmem_cache *inode_entry_slab;
  2469. /*
  2470. * inline.c
  2471. */
  2472. bool f2fs_may_inline_data(struct inode *inode);
  2473. bool f2fs_may_inline_dentry(struct inode *inode);
  2474. void read_inline_data(struct page *page, struct page *ipage);
  2475. void truncate_inline_inode(struct inode *inode, struct page *ipage, u64 from);
  2476. int f2fs_read_inline_data(struct inode *inode, struct page *page);
  2477. int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
  2478. int f2fs_convert_inline_inode(struct inode *inode);
  2479. int f2fs_write_inline_data(struct inode *inode, struct page *page);
  2480. bool recover_inline_data(struct inode *inode, struct page *npage);
  2481. struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
  2482. struct fscrypt_name *fname, struct page **res_page);
  2483. int make_empty_inline_dir(struct inode *inode, struct inode *parent,
  2484. struct page *ipage);
  2485. int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
  2486. const struct qstr *orig_name,
  2487. struct inode *inode, nid_t ino, umode_t mode);
  2488. void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
  2489. struct inode *dir, struct inode *inode);
  2490. bool f2fs_empty_inline_dir(struct inode *dir);
  2491. int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
  2492. struct fscrypt_str *fstr);
  2493. int f2fs_inline_data_fiemap(struct inode *inode,
  2494. struct fiemap_extent_info *fieinfo,
  2495. __u64 start, __u64 len);
  2496. /*
  2497. * shrinker.c
  2498. */
  2499. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  2500. struct shrink_control *sc);
  2501. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  2502. struct shrink_control *sc);
  2503. void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
  2504. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
  2505. /*
  2506. * extent_cache.c
  2507. */
  2508. struct rb_entry *__lookup_rb_tree(struct rb_root *root,
  2509. struct rb_entry *cached_re, unsigned int ofs);
  2510. struct rb_node **__lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
  2511. struct rb_root *root, struct rb_node **parent,
  2512. unsigned int ofs);
  2513. struct rb_entry *__lookup_rb_tree_ret(struct rb_root *root,
  2514. struct rb_entry *cached_re, unsigned int ofs,
  2515. struct rb_entry **prev_entry, struct rb_entry **next_entry,
  2516. struct rb_node ***insert_p, struct rb_node **insert_parent,
  2517. bool force);
  2518. bool __check_rb_tree_consistence(struct f2fs_sb_info *sbi,
  2519. struct rb_root *root);
  2520. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
  2521. bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
  2522. void f2fs_drop_extent_tree(struct inode *inode);
  2523. unsigned int f2fs_destroy_extent_node(struct inode *inode);
  2524. void f2fs_destroy_extent_tree(struct inode *inode);
  2525. bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
  2526. struct extent_info *ei);
  2527. void f2fs_update_extent_cache(struct dnode_of_data *dn);
  2528. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  2529. pgoff_t fofs, block_t blkaddr, unsigned int len);
  2530. void init_extent_cache_info(struct f2fs_sb_info *sbi);
  2531. int __init create_extent_cache(void);
  2532. void destroy_extent_cache(void);
  2533. /*
  2534. * sysfs.c
  2535. */
  2536. int __init f2fs_init_sysfs(void);
  2537. void f2fs_exit_sysfs(void);
  2538. int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
  2539. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
  2540. /*
  2541. * crypto support
  2542. */
  2543. static inline bool f2fs_encrypted_inode(struct inode *inode)
  2544. {
  2545. return file_is_encrypt(inode);
  2546. }
  2547. static inline bool f2fs_encrypted_file(struct inode *inode)
  2548. {
  2549. return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
  2550. }
  2551. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  2552. {
  2553. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2554. file_set_encrypt(inode);
  2555. #endif
  2556. }
  2557. static inline bool f2fs_bio_encrypted(struct bio *bio)
  2558. {
  2559. return bio->bi_private != NULL;
  2560. }
  2561. static inline int f2fs_sb_has_crypto(struct super_block *sb)
  2562. {
  2563. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
  2564. }
  2565. static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
  2566. {
  2567. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
  2568. }
  2569. static inline int f2fs_sb_has_extra_attr(struct super_block *sb)
  2570. {
  2571. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_EXTRA_ATTR);
  2572. }
  2573. static inline int f2fs_sb_has_project_quota(struct super_block *sb)
  2574. {
  2575. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_PRJQUOTA);
  2576. }
  2577. static inline int f2fs_sb_has_inode_chksum(struct super_block *sb)
  2578. {
  2579. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_INODE_CHKSUM);
  2580. }
  2581. #ifdef CONFIG_BLK_DEV_ZONED
  2582. static inline int get_blkz_type(struct f2fs_sb_info *sbi,
  2583. struct block_device *bdev, block_t blkaddr)
  2584. {
  2585. unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
  2586. int i;
  2587. for (i = 0; i < sbi->s_ndevs; i++)
  2588. if (FDEV(i).bdev == bdev)
  2589. return FDEV(i).blkz_type[zno];
  2590. return -EINVAL;
  2591. }
  2592. #endif
  2593. static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
  2594. {
  2595. struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
  2596. return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
  2597. }
  2598. static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
  2599. {
  2600. clear_opt(sbi, ADAPTIVE);
  2601. clear_opt(sbi, LFS);
  2602. switch (mt) {
  2603. case F2FS_MOUNT_ADAPTIVE:
  2604. set_opt(sbi, ADAPTIVE);
  2605. break;
  2606. case F2FS_MOUNT_LFS:
  2607. set_opt(sbi, LFS);
  2608. break;
  2609. }
  2610. }
  2611. static inline bool f2fs_may_encrypt(struct inode *inode)
  2612. {
  2613. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2614. umode_t mode = inode->i_mode;
  2615. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  2616. #else
  2617. return 0;
  2618. #endif
  2619. }
  2620. #endif