f2fs.h 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503
  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/cred.h>
  22. #include <linux/vmalloc.h>
  23. #include <linux/bio.h>
  24. #include <linux/blkdev.h>
  25. #include <linux/quotaops.h>
  26. #include <crypto/hash.h>
  27. #define __FS_HAS_ENCRYPTION IS_ENABLED(CONFIG_F2FS_FS_ENCRYPTION)
  28. #include <linux/fscrypt.h>
  29. #ifdef CONFIG_F2FS_CHECK_FS
  30. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  31. #else
  32. #define f2fs_bug_on(sbi, condition) \
  33. do { \
  34. if (unlikely(condition)) { \
  35. WARN_ON(1); \
  36. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  37. } \
  38. } while (0)
  39. #endif
  40. enum {
  41. FAULT_KMALLOC,
  42. FAULT_KVMALLOC,
  43. FAULT_PAGE_ALLOC,
  44. FAULT_PAGE_GET,
  45. FAULT_ALLOC_BIO,
  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_DISCARD,
  55. FAULT_MAX,
  56. };
  57. #ifdef CONFIG_F2FS_FAULT_INJECTION
  58. #define F2FS_ALL_FAULT_TYPE ((1 << FAULT_MAX) - 1)
  59. struct f2fs_fault_info {
  60. atomic_t inject_ops;
  61. unsigned int inject_rate;
  62. unsigned int inject_type;
  63. };
  64. extern char *f2fs_fault_name[FAULT_MAX];
  65. #define IS_FAULT_SET(fi, type) ((fi)->inject_type & (1 << (type)))
  66. #endif
  67. /*
  68. * For mount options
  69. */
  70. #define F2FS_MOUNT_BG_GC 0x00000001
  71. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  72. #define F2FS_MOUNT_DISCARD 0x00000004
  73. #define F2FS_MOUNT_NOHEAP 0x00000008
  74. #define F2FS_MOUNT_XATTR_USER 0x00000010
  75. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  76. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  77. #define F2FS_MOUNT_INLINE_XATTR 0x00000080
  78. #define F2FS_MOUNT_INLINE_DATA 0x00000100
  79. #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
  80. #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
  81. #define F2FS_MOUNT_NOBARRIER 0x00000800
  82. #define F2FS_MOUNT_FASTBOOT 0x00001000
  83. #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
  84. #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
  85. #define F2FS_MOUNT_DATA_FLUSH 0x00008000
  86. #define F2FS_MOUNT_FAULT_INJECTION 0x00010000
  87. #define F2FS_MOUNT_ADAPTIVE 0x00020000
  88. #define F2FS_MOUNT_LFS 0x00040000
  89. #define F2FS_MOUNT_USRQUOTA 0x00080000
  90. #define F2FS_MOUNT_GRPQUOTA 0x00100000
  91. #define F2FS_MOUNT_PRJQUOTA 0x00200000
  92. #define F2FS_MOUNT_QUOTA 0x00400000
  93. #define F2FS_MOUNT_INLINE_XATTR_SIZE 0x00800000
  94. #define F2FS_MOUNT_RESERVE_ROOT 0x01000000
  95. #define F2FS_OPTION(sbi) ((sbi)->mount_opt)
  96. #define clear_opt(sbi, option) (F2FS_OPTION(sbi).opt &= ~F2FS_MOUNT_##option)
  97. #define set_opt(sbi, option) (F2FS_OPTION(sbi).opt |= F2FS_MOUNT_##option)
  98. #define test_opt(sbi, option) (F2FS_OPTION(sbi).opt & F2FS_MOUNT_##option)
  99. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  100. typecheck(unsigned long long, b) && \
  101. ((long long)((a) - (b)) > 0))
  102. typedef u32 block_t; /*
  103. * should not change u32, since it is the on-disk block
  104. * address format, __le32.
  105. */
  106. typedef u32 nid_t;
  107. struct f2fs_mount_info {
  108. unsigned int opt;
  109. int write_io_size_bits; /* Write IO size bits */
  110. block_t root_reserved_blocks; /* root reserved blocks */
  111. kuid_t s_resuid; /* reserved blocks for uid */
  112. kgid_t s_resgid; /* reserved blocks for gid */
  113. int active_logs; /* # of active logs */
  114. int inline_xattr_size; /* inline xattr size */
  115. #ifdef CONFIG_F2FS_FAULT_INJECTION
  116. struct f2fs_fault_info fault_info; /* For fault injection */
  117. #endif
  118. #ifdef CONFIG_QUOTA
  119. /* Names of quota files with journalled quota */
  120. char *s_qf_names[MAXQUOTAS];
  121. int s_jquota_fmt; /* Format of quota to use */
  122. #endif
  123. /* For which write hints are passed down to block layer */
  124. int whint_mode;
  125. int alloc_mode; /* segment allocation policy */
  126. int fsync_mode; /* fsync policy */
  127. bool test_dummy_encryption; /* test dummy encryption */
  128. };
  129. #define F2FS_FEATURE_ENCRYPT 0x0001
  130. #define F2FS_FEATURE_BLKZONED 0x0002
  131. #define F2FS_FEATURE_ATOMIC_WRITE 0x0004
  132. #define F2FS_FEATURE_EXTRA_ATTR 0x0008
  133. #define F2FS_FEATURE_PRJQUOTA 0x0010
  134. #define F2FS_FEATURE_INODE_CHKSUM 0x0020
  135. #define F2FS_FEATURE_FLEXIBLE_INLINE_XATTR 0x0040
  136. #define F2FS_FEATURE_QUOTA_INO 0x0080
  137. #define F2FS_FEATURE_INODE_CRTIME 0x0100
  138. #define F2FS_FEATURE_LOST_FOUND 0x0200
  139. #define F2FS_FEATURE_VERITY 0x0400 /* reserved */
  140. #define F2FS_HAS_FEATURE(sb, mask) \
  141. ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
  142. #define F2FS_SET_FEATURE(sb, mask) \
  143. (F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask))
  144. #define F2FS_CLEAR_FEATURE(sb, mask) \
  145. (F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask))
  146. /*
  147. * Default values for user and/or group using reserved blocks
  148. */
  149. #define F2FS_DEF_RESUID 0
  150. #define F2FS_DEF_RESGID 0
  151. /*
  152. * For checkpoint manager
  153. */
  154. enum {
  155. NAT_BITMAP,
  156. SIT_BITMAP
  157. };
  158. #define CP_UMOUNT 0x00000001
  159. #define CP_FASTBOOT 0x00000002
  160. #define CP_SYNC 0x00000004
  161. #define CP_RECOVERY 0x00000008
  162. #define CP_DISCARD 0x00000010
  163. #define CP_TRIMMED 0x00000020
  164. #define MAX_DISCARD_BLOCKS(sbi) BLKS_PER_SEC(sbi)
  165. #define DEF_MAX_DISCARD_REQUEST 8 /* issue 8 discards per round */
  166. #define DEF_MIN_DISCARD_ISSUE_TIME 50 /* 50 ms, if exists */
  167. #define DEF_MID_DISCARD_ISSUE_TIME 500 /* 500 ms, if device busy */
  168. #define DEF_MAX_DISCARD_ISSUE_TIME 60000 /* 60 s, if no candidates */
  169. #define DEF_DISCARD_URGENT_UTIL 80 /* do more discard over 80% */
  170. #define DEF_CP_INTERVAL 60 /* 60 secs */
  171. #define DEF_IDLE_INTERVAL 5 /* 5 secs */
  172. struct cp_control {
  173. int reason;
  174. __u64 trim_start;
  175. __u64 trim_end;
  176. __u64 trim_minlen;
  177. };
  178. /*
  179. * indicate meta/data type
  180. */
  181. enum {
  182. META_CP,
  183. META_NAT,
  184. META_SIT,
  185. META_SSA,
  186. META_POR,
  187. DATA_GENERIC,
  188. META_GENERIC,
  189. };
  190. /* for the list of ino */
  191. enum {
  192. ORPHAN_INO, /* for orphan ino list */
  193. APPEND_INO, /* for append ino list */
  194. UPDATE_INO, /* for update ino list */
  195. TRANS_DIR_INO, /* for trasactions dir ino list */
  196. FLUSH_INO, /* for multiple device flushing */
  197. MAX_INO_ENTRY, /* max. list */
  198. };
  199. struct ino_entry {
  200. struct list_head list; /* list head */
  201. nid_t ino; /* inode number */
  202. unsigned int dirty_device; /* dirty device bitmap */
  203. };
  204. /* for the list of inodes to be GCed */
  205. struct inode_entry {
  206. struct list_head list; /* list head */
  207. struct inode *inode; /* vfs inode pointer */
  208. };
  209. struct fsync_node_entry {
  210. struct list_head list; /* list head */
  211. struct page *page; /* warm node page pointer */
  212. unsigned int seq_id; /* sequence id */
  213. };
  214. /* for the bitmap indicate blocks to be discarded */
  215. struct discard_entry {
  216. struct list_head list; /* list head */
  217. block_t start_blkaddr; /* start blockaddr of current segment */
  218. unsigned char discard_map[SIT_VBLOCK_MAP_SIZE]; /* segment discard bitmap */
  219. };
  220. /* default discard granularity of inner discard thread, unit: block count */
  221. #define DEFAULT_DISCARD_GRANULARITY 16
  222. /* max discard pend list number */
  223. #define MAX_PLIST_NUM 512
  224. #define plist_idx(blk_num) ((blk_num) >= MAX_PLIST_NUM ? \
  225. (MAX_PLIST_NUM - 1) : (blk_num - 1))
  226. enum {
  227. D_PREP, /* initial */
  228. D_PARTIAL, /* partially submitted */
  229. D_SUBMIT, /* all submitted */
  230. D_DONE, /* finished */
  231. };
  232. struct discard_info {
  233. block_t lstart; /* logical start address */
  234. block_t len; /* length */
  235. block_t start; /* actual start address in dev */
  236. };
  237. struct discard_cmd {
  238. struct rb_node rb_node; /* rb node located in rb-tree */
  239. union {
  240. struct {
  241. block_t lstart; /* logical start address */
  242. block_t len; /* length */
  243. block_t start; /* actual start address in dev */
  244. };
  245. struct discard_info di; /* discard info */
  246. };
  247. struct list_head list; /* command list */
  248. struct completion wait; /* compleation */
  249. struct block_device *bdev; /* bdev */
  250. unsigned short ref; /* reference count */
  251. unsigned char state; /* state */
  252. unsigned char issuing; /* issuing discard */
  253. int error; /* bio error */
  254. spinlock_t lock; /* for state/bio_ref updating */
  255. unsigned short bio_ref; /* bio reference count */
  256. };
  257. enum {
  258. DPOLICY_BG,
  259. DPOLICY_FORCE,
  260. DPOLICY_FSTRIM,
  261. DPOLICY_UMOUNT,
  262. MAX_DPOLICY,
  263. };
  264. struct discard_policy {
  265. int type; /* type of discard */
  266. unsigned int min_interval; /* used for candidates exist */
  267. unsigned int mid_interval; /* used for device busy */
  268. unsigned int max_interval; /* used for candidates not exist */
  269. unsigned int max_requests; /* # of discards issued per round */
  270. unsigned int io_aware_gran; /* minimum granularity discard not be aware of I/O */
  271. bool io_aware; /* issue discard in idle time */
  272. bool sync; /* submit discard with REQ_SYNC flag */
  273. bool ordered; /* issue discard by lba order */
  274. unsigned int granularity; /* discard granularity */
  275. };
  276. struct discard_cmd_control {
  277. struct task_struct *f2fs_issue_discard; /* discard thread */
  278. struct list_head entry_list; /* 4KB discard entry list */
  279. struct list_head pend_list[MAX_PLIST_NUM];/* store pending entries */
  280. struct list_head wait_list; /* store on-flushing entries */
  281. struct list_head fstrim_list; /* in-flight discard from fstrim */
  282. wait_queue_head_t discard_wait_queue; /* waiting queue for wake-up */
  283. unsigned int discard_wake; /* to wake up discard thread */
  284. struct mutex cmd_lock;
  285. unsigned int nr_discards; /* # of discards in the list */
  286. unsigned int max_discards; /* max. discards to be issued */
  287. unsigned int discard_granularity; /* discard granularity */
  288. unsigned int undiscard_blks; /* # of undiscard blocks */
  289. unsigned int next_pos; /* next discard position */
  290. atomic_t issued_discard; /* # of issued discard */
  291. atomic_t issing_discard; /* # of issing discard */
  292. atomic_t discard_cmd_cnt; /* # of cached cmd count */
  293. struct rb_root root; /* root of discard rb-tree */
  294. bool rbtree_check; /* config for consistence check */
  295. };
  296. /* for the list of fsync inodes, used only during recovery */
  297. struct fsync_inode_entry {
  298. struct list_head list; /* list head */
  299. struct inode *inode; /* vfs inode pointer */
  300. block_t blkaddr; /* block address locating the last fsync */
  301. block_t last_dentry; /* block address locating the last dentry */
  302. };
  303. #define nats_in_cursum(jnl) (le16_to_cpu((jnl)->n_nats))
  304. #define sits_in_cursum(jnl) (le16_to_cpu((jnl)->n_sits))
  305. #define nat_in_journal(jnl, i) ((jnl)->nat_j.entries[i].ne)
  306. #define nid_in_journal(jnl, i) ((jnl)->nat_j.entries[i].nid)
  307. #define sit_in_journal(jnl, i) ((jnl)->sit_j.entries[i].se)
  308. #define segno_in_journal(jnl, i) ((jnl)->sit_j.entries[i].segno)
  309. #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
  310. #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
  311. static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
  312. {
  313. int before = nats_in_cursum(journal);
  314. journal->n_nats = cpu_to_le16(before + i);
  315. return before;
  316. }
  317. static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
  318. {
  319. int before = sits_in_cursum(journal);
  320. journal->n_sits = cpu_to_le16(before + i);
  321. return before;
  322. }
  323. static inline bool __has_cursum_space(struct f2fs_journal *journal,
  324. int size, int type)
  325. {
  326. if (type == NAT_JOURNAL)
  327. return size <= MAX_NAT_JENTRIES(journal);
  328. return size <= MAX_SIT_JENTRIES(journal);
  329. }
  330. /*
  331. * ioctl commands
  332. */
  333. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  334. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  335. #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
  336. #define F2FS_IOCTL_MAGIC 0xf5
  337. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  338. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  339. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  340. #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
  341. #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
  342. #define F2FS_IOC_GARBAGE_COLLECT _IOW(F2FS_IOCTL_MAGIC, 6, __u32)
  343. #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
  344. #define F2FS_IOC_DEFRAGMENT _IOWR(F2FS_IOCTL_MAGIC, 8, \
  345. struct f2fs_defragment)
  346. #define F2FS_IOC_MOVE_RANGE _IOWR(F2FS_IOCTL_MAGIC, 9, \
  347. struct f2fs_move_range)
  348. #define F2FS_IOC_FLUSH_DEVICE _IOW(F2FS_IOCTL_MAGIC, 10, \
  349. struct f2fs_flush_device)
  350. #define F2FS_IOC_GARBAGE_COLLECT_RANGE _IOW(F2FS_IOCTL_MAGIC, 11, \
  351. struct f2fs_gc_range)
  352. #define F2FS_IOC_GET_FEATURES _IOR(F2FS_IOCTL_MAGIC, 12, __u32)
  353. #define F2FS_IOC_SET_PIN_FILE _IOW(F2FS_IOCTL_MAGIC, 13, __u32)
  354. #define F2FS_IOC_GET_PIN_FILE _IOR(F2FS_IOCTL_MAGIC, 14, __u32)
  355. #define F2FS_IOC_PRECACHE_EXTENTS _IO(F2FS_IOCTL_MAGIC, 15)
  356. #define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
  357. #define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
  358. #define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
  359. /*
  360. * should be same as XFS_IOC_GOINGDOWN.
  361. * Flags for going down operation used by FS_IOC_GOINGDOWN
  362. */
  363. #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
  364. #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
  365. #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
  366. #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
  367. #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
  368. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  369. /*
  370. * ioctl commands in 32 bit emulation
  371. */
  372. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  373. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  374. #define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
  375. #endif
  376. #define F2FS_IOC_FSGETXATTR FS_IOC_FSGETXATTR
  377. #define F2FS_IOC_FSSETXATTR FS_IOC_FSSETXATTR
  378. struct f2fs_gc_range {
  379. u32 sync;
  380. u64 start;
  381. u64 len;
  382. };
  383. struct f2fs_defragment {
  384. u64 start;
  385. u64 len;
  386. };
  387. struct f2fs_move_range {
  388. u32 dst_fd; /* destination fd */
  389. u64 pos_in; /* start position in src_fd */
  390. u64 pos_out; /* start position in dst_fd */
  391. u64 len; /* size to move */
  392. };
  393. struct f2fs_flush_device {
  394. u32 dev_num; /* device number to flush */
  395. u32 segments; /* # of segments to flush */
  396. };
  397. /* for inline stuff */
  398. #define DEF_INLINE_RESERVED_SIZE 1
  399. static inline int get_extra_isize(struct inode *inode);
  400. static inline int get_inline_xattr_addrs(struct inode *inode);
  401. #define MAX_INLINE_DATA(inode) (sizeof(__le32) * \
  402. (CUR_ADDRS_PER_INODE(inode) - \
  403. get_inline_xattr_addrs(inode) - \
  404. DEF_INLINE_RESERVED_SIZE))
  405. /* for inline dir */
  406. #define NR_INLINE_DENTRY(inode) (MAX_INLINE_DATA(inode) * BITS_PER_BYTE / \
  407. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  408. BITS_PER_BYTE + 1))
  409. #define INLINE_DENTRY_BITMAP_SIZE(inode) ((NR_INLINE_DENTRY(inode) + \
  410. BITS_PER_BYTE - 1) / BITS_PER_BYTE)
  411. #define INLINE_RESERVED_SIZE(inode) (MAX_INLINE_DATA(inode) - \
  412. ((SIZE_OF_DIR_ENTRY + F2FS_SLOT_LEN) * \
  413. NR_INLINE_DENTRY(inode) + \
  414. INLINE_DENTRY_BITMAP_SIZE(inode)))
  415. /*
  416. * For INODE and NODE manager
  417. */
  418. /* for directory operations */
  419. struct f2fs_dentry_ptr {
  420. struct inode *inode;
  421. void *bitmap;
  422. struct f2fs_dir_entry *dentry;
  423. __u8 (*filename)[F2FS_SLOT_LEN];
  424. int max;
  425. int nr_bitmap;
  426. };
  427. static inline void make_dentry_ptr_block(struct inode *inode,
  428. struct f2fs_dentry_ptr *d, struct f2fs_dentry_block *t)
  429. {
  430. d->inode = inode;
  431. d->max = NR_DENTRY_IN_BLOCK;
  432. d->nr_bitmap = SIZE_OF_DENTRY_BITMAP;
  433. d->bitmap = t->dentry_bitmap;
  434. d->dentry = t->dentry;
  435. d->filename = t->filename;
  436. }
  437. static inline void make_dentry_ptr_inline(struct inode *inode,
  438. struct f2fs_dentry_ptr *d, void *t)
  439. {
  440. int entry_cnt = NR_INLINE_DENTRY(inode);
  441. int bitmap_size = INLINE_DENTRY_BITMAP_SIZE(inode);
  442. int reserved_size = INLINE_RESERVED_SIZE(inode);
  443. d->inode = inode;
  444. d->max = entry_cnt;
  445. d->nr_bitmap = bitmap_size;
  446. d->bitmap = t;
  447. d->dentry = t + bitmap_size + reserved_size;
  448. d->filename = t + bitmap_size + reserved_size +
  449. SIZE_OF_DIR_ENTRY * entry_cnt;
  450. }
  451. /*
  452. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  453. * as its node offset to distinguish from index node blocks.
  454. * But some bits are used to mark the node block.
  455. */
  456. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  457. >> OFFSET_BIT_SHIFT)
  458. enum {
  459. ALLOC_NODE, /* allocate a new node page if needed */
  460. LOOKUP_NODE, /* look up a node without readahead */
  461. LOOKUP_NODE_RA, /*
  462. * look up a node with readahead called
  463. * by get_data_block.
  464. */
  465. };
  466. #define DEFAULT_RETRY_IO_COUNT 8 /* maximum retry read IO count */
  467. #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
  468. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  469. /* for in-memory extent cache entry */
  470. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  471. /* number of extent info in extent cache we try to shrink */
  472. #define EXTENT_CACHE_SHRINK_NUMBER 128
  473. struct rb_entry {
  474. struct rb_node rb_node; /* rb node located in rb-tree */
  475. unsigned int ofs; /* start offset of the entry */
  476. unsigned int len; /* length of the entry */
  477. };
  478. struct extent_info {
  479. unsigned int fofs; /* start offset in a file */
  480. unsigned int len; /* length of the extent */
  481. u32 blk; /* start block address of the extent */
  482. };
  483. struct extent_node {
  484. struct rb_node rb_node;
  485. union {
  486. struct {
  487. unsigned int fofs;
  488. unsigned int len;
  489. u32 blk;
  490. };
  491. struct extent_info ei; /* extent info */
  492. };
  493. struct list_head list; /* node in global extent list of sbi */
  494. struct extent_tree *et; /* extent tree pointer */
  495. };
  496. struct extent_tree {
  497. nid_t ino; /* inode number */
  498. struct rb_root root; /* root of extent info rb-tree */
  499. struct extent_node *cached_en; /* recently accessed extent node */
  500. struct extent_info largest; /* largested extent info */
  501. struct list_head list; /* to be used by sbi->zombie_list */
  502. rwlock_t lock; /* protect extent info rb-tree */
  503. atomic_t node_cnt; /* # of extent node in rb-tree*/
  504. bool largest_updated; /* largest extent updated */
  505. };
  506. /*
  507. * This structure is taken from ext4_map_blocks.
  508. *
  509. * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
  510. */
  511. #define F2FS_MAP_NEW (1 << BH_New)
  512. #define F2FS_MAP_MAPPED (1 << BH_Mapped)
  513. #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
  514. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  515. F2FS_MAP_UNWRITTEN)
  516. struct f2fs_map_blocks {
  517. block_t m_pblk;
  518. block_t m_lblk;
  519. unsigned int m_len;
  520. unsigned int m_flags;
  521. pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
  522. pgoff_t *m_next_extent; /* point to next possible extent */
  523. int m_seg_type;
  524. };
  525. /* for flag in get_data_block */
  526. enum {
  527. F2FS_GET_BLOCK_DEFAULT,
  528. F2FS_GET_BLOCK_FIEMAP,
  529. F2FS_GET_BLOCK_BMAP,
  530. F2FS_GET_BLOCK_DIO,
  531. F2FS_GET_BLOCK_PRE_DIO,
  532. F2FS_GET_BLOCK_PRE_AIO,
  533. F2FS_GET_BLOCK_PRECACHE,
  534. };
  535. /*
  536. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  537. */
  538. #define FADVISE_COLD_BIT 0x01
  539. #define FADVISE_LOST_PINO_BIT 0x02
  540. #define FADVISE_ENCRYPT_BIT 0x04
  541. #define FADVISE_ENC_NAME_BIT 0x08
  542. #define FADVISE_KEEP_SIZE_BIT 0x10
  543. #define FADVISE_HOT_BIT 0x20
  544. #define FADVISE_VERITY_BIT 0x40 /* reserved */
  545. #define FADVISE_MODIFIABLE_BITS (FADVISE_COLD_BIT | FADVISE_HOT_BIT)
  546. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  547. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  548. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  549. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  550. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  551. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  552. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  553. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  554. #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
  555. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  556. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  557. #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
  558. #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
  559. #define file_is_hot(inode) is_file(inode, FADVISE_HOT_BIT)
  560. #define file_set_hot(inode) set_file(inode, FADVISE_HOT_BIT)
  561. #define file_clear_hot(inode) clear_file(inode, FADVISE_HOT_BIT)
  562. #define DEF_DIR_LEVEL 0
  563. enum {
  564. GC_FAILURE_PIN,
  565. GC_FAILURE_ATOMIC,
  566. MAX_GC_FAILURE
  567. };
  568. struct f2fs_inode_info {
  569. struct inode vfs_inode; /* serve a vfs inode */
  570. unsigned long i_flags; /* keep an inode flags for ioctl */
  571. unsigned char i_advise; /* use to give file attribute hints */
  572. unsigned char i_dir_level; /* use for dentry level for large dir */
  573. unsigned int i_current_depth; /* only for directory depth */
  574. /* for gc failure statistic */
  575. unsigned int i_gc_failures[MAX_GC_FAILURE];
  576. unsigned int i_pino; /* parent inode number */
  577. umode_t i_acl_mode; /* keep file acl mode temporarily */
  578. /* Use below internally in f2fs*/
  579. unsigned long flags; /* use to pass per-file flags */
  580. struct rw_semaphore i_sem; /* protect fi info */
  581. atomic_t dirty_pages; /* # of dirty pages */
  582. f2fs_hash_t chash; /* hash value of given file name */
  583. unsigned int clevel; /* maximum level of given file name */
  584. struct task_struct *task; /* lookup and create consistency */
  585. struct task_struct *cp_task; /* separate cp/wb IO stats*/
  586. nid_t i_xattr_nid; /* node id that contains xattrs */
  587. loff_t last_disk_size; /* lastly written file size */
  588. #ifdef CONFIG_QUOTA
  589. struct dquot *i_dquot[MAXQUOTAS];
  590. /* quota space reservation, managed internally by quota code */
  591. qsize_t i_reserved_quota;
  592. #endif
  593. struct list_head dirty_list; /* dirty list for dirs and files */
  594. struct list_head gdirty_list; /* linked in global dirty list */
  595. struct list_head inmem_ilist; /* list for inmem inodes */
  596. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  597. struct task_struct *inmem_task; /* store inmemory task */
  598. struct mutex inmem_lock; /* lock for inmemory pages */
  599. struct extent_tree *extent_tree; /* cached extent_tree entry */
  600. /* avoid racing between foreground op and gc */
  601. struct rw_semaphore i_gc_rwsem[2];
  602. struct rw_semaphore i_mmap_sem;
  603. struct rw_semaphore i_xattr_sem; /* avoid racing between reading and changing EAs */
  604. int i_extra_isize; /* size of extra space located in i_addr */
  605. kprojid_t i_projid; /* id for project quota */
  606. int i_inline_xattr_size; /* inline xattr size */
  607. struct timespec64 i_crtime; /* inode creation time */
  608. struct timespec64 i_disk_time[4];/* inode disk times */
  609. };
  610. static inline void get_extent_info(struct extent_info *ext,
  611. struct f2fs_extent *i_ext)
  612. {
  613. ext->fofs = le32_to_cpu(i_ext->fofs);
  614. ext->blk = le32_to_cpu(i_ext->blk);
  615. ext->len = le32_to_cpu(i_ext->len);
  616. }
  617. static inline void set_raw_extent(struct extent_info *ext,
  618. struct f2fs_extent *i_ext)
  619. {
  620. i_ext->fofs = cpu_to_le32(ext->fofs);
  621. i_ext->blk = cpu_to_le32(ext->blk);
  622. i_ext->len = cpu_to_le32(ext->len);
  623. }
  624. static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
  625. u32 blk, unsigned int len)
  626. {
  627. ei->fofs = fofs;
  628. ei->blk = blk;
  629. ei->len = len;
  630. }
  631. static inline bool __is_discard_mergeable(struct discard_info *back,
  632. struct discard_info *front, unsigned int max_len)
  633. {
  634. return (back->lstart + back->len == front->lstart) &&
  635. (back->len + front->len <= max_len);
  636. }
  637. static inline bool __is_discard_back_mergeable(struct discard_info *cur,
  638. struct discard_info *back, unsigned int max_len)
  639. {
  640. return __is_discard_mergeable(back, cur, max_len);
  641. }
  642. static inline bool __is_discard_front_mergeable(struct discard_info *cur,
  643. struct discard_info *front, unsigned int max_len)
  644. {
  645. return __is_discard_mergeable(cur, front, max_len);
  646. }
  647. static inline bool __is_extent_mergeable(struct extent_info *back,
  648. struct extent_info *front)
  649. {
  650. return (back->fofs + back->len == front->fofs &&
  651. back->blk + back->len == front->blk);
  652. }
  653. static inline bool __is_back_mergeable(struct extent_info *cur,
  654. struct extent_info *back)
  655. {
  656. return __is_extent_mergeable(back, cur);
  657. }
  658. static inline bool __is_front_mergeable(struct extent_info *cur,
  659. struct extent_info *front)
  660. {
  661. return __is_extent_mergeable(cur, front);
  662. }
  663. extern void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync);
  664. static inline void __try_update_largest_extent(struct extent_tree *et,
  665. struct extent_node *en)
  666. {
  667. if (en->ei.len > et->largest.len) {
  668. et->largest = en->ei;
  669. et->largest_updated = true;
  670. }
  671. }
  672. /*
  673. * For free nid management
  674. */
  675. enum nid_state {
  676. FREE_NID, /* newly added to free nid list */
  677. PREALLOC_NID, /* it is preallocated */
  678. MAX_NID_STATE,
  679. };
  680. struct f2fs_nm_info {
  681. block_t nat_blkaddr; /* base disk address of NAT */
  682. nid_t max_nid; /* maximum possible node ids */
  683. nid_t available_nids; /* # of available node ids */
  684. nid_t next_scan_nid; /* the next nid to be scanned */
  685. unsigned int ram_thresh; /* control the memory footprint */
  686. unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
  687. unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
  688. /* NAT cache management */
  689. struct radix_tree_root nat_root;/* root of the nat entry cache */
  690. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  691. struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
  692. struct list_head nat_entries; /* cached nat entry list (clean) */
  693. spinlock_t nat_list_lock; /* protect clean nat entry list */
  694. unsigned int nat_cnt; /* the # of cached nat entries */
  695. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  696. unsigned int nat_blocks; /* # of nat blocks */
  697. /* free node ids management */
  698. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  699. struct list_head free_nid_list; /* list for free nids excluding preallocated nids */
  700. unsigned int nid_cnt[MAX_NID_STATE]; /* the number of free node id */
  701. spinlock_t nid_list_lock; /* protect nid lists ops */
  702. struct mutex build_lock; /* lock for build free nids */
  703. unsigned char **free_nid_bitmap;
  704. unsigned char *nat_block_bitmap;
  705. unsigned short *free_nid_count; /* free nid count of NAT block */
  706. /* for checkpoint */
  707. char *nat_bitmap; /* NAT bitmap pointer */
  708. unsigned int nat_bits_blocks; /* # of nat bits blocks */
  709. unsigned char *nat_bits; /* NAT bits blocks */
  710. unsigned char *full_nat_bits; /* full NAT pages */
  711. unsigned char *empty_nat_bits; /* empty NAT pages */
  712. #ifdef CONFIG_F2FS_CHECK_FS
  713. char *nat_bitmap_mir; /* NAT bitmap mirror */
  714. #endif
  715. int bitmap_size; /* bitmap size */
  716. };
  717. /*
  718. * this structure is used as one of function parameters.
  719. * all the information are dedicated to a given direct node block determined
  720. * by the data offset in a file.
  721. */
  722. struct dnode_of_data {
  723. struct inode *inode; /* vfs inode pointer */
  724. struct page *inode_page; /* its inode page, NULL is possible */
  725. struct page *node_page; /* cached direct node page */
  726. nid_t nid; /* node id of the direct node block */
  727. unsigned int ofs_in_node; /* data offset in the node page */
  728. bool inode_page_locked; /* inode page is locked or not */
  729. bool node_changed; /* is node block changed */
  730. char cur_level; /* level of hole node page */
  731. char max_level; /* level of current page located */
  732. block_t data_blkaddr; /* block address of the node block */
  733. };
  734. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  735. struct page *ipage, struct page *npage, nid_t nid)
  736. {
  737. memset(dn, 0, sizeof(*dn));
  738. dn->inode = inode;
  739. dn->inode_page = ipage;
  740. dn->node_page = npage;
  741. dn->nid = nid;
  742. }
  743. /*
  744. * For SIT manager
  745. *
  746. * By default, there are 6 active log areas across the whole main area.
  747. * When considering hot and cold data separation to reduce cleaning overhead,
  748. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  749. * respectively.
  750. * In the current design, you should not change the numbers intentionally.
  751. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  752. * logs individually according to the underlying devices. (default: 6)
  753. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  754. * data and 8 for node logs.
  755. */
  756. #define NR_CURSEG_DATA_TYPE (3)
  757. #define NR_CURSEG_NODE_TYPE (3)
  758. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  759. enum {
  760. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  761. CURSEG_WARM_DATA, /* data blocks */
  762. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  763. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  764. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  765. CURSEG_COLD_NODE, /* indirect node blocks */
  766. NO_CHECK_TYPE,
  767. };
  768. struct flush_cmd {
  769. struct completion wait;
  770. struct llist_node llnode;
  771. nid_t ino;
  772. int ret;
  773. };
  774. struct flush_cmd_control {
  775. struct task_struct *f2fs_issue_flush; /* flush thread */
  776. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  777. atomic_t issued_flush; /* # of issued flushes */
  778. atomic_t issing_flush; /* # of issing flushes */
  779. struct llist_head issue_list; /* list for command issue */
  780. struct llist_node *dispatch_list; /* list for command dispatch */
  781. };
  782. struct f2fs_sm_info {
  783. struct sit_info *sit_info; /* whole segment information */
  784. struct free_segmap_info *free_info; /* free segment information */
  785. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  786. struct curseg_info *curseg_array; /* active segment information */
  787. struct rw_semaphore curseg_lock; /* for preventing curseg change */
  788. block_t seg0_blkaddr; /* block address of 0'th segment */
  789. block_t main_blkaddr; /* start block address of main area */
  790. block_t ssa_blkaddr; /* start block address of SSA area */
  791. unsigned int segment_count; /* total # of segments */
  792. unsigned int main_segments; /* # of segments in main area */
  793. unsigned int reserved_segments; /* # of reserved segments */
  794. unsigned int ovp_segments; /* # of overprovision segments */
  795. /* a threshold to reclaim prefree segments */
  796. unsigned int rec_prefree_segments;
  797. /* for batched trimming */
  798. unsigned int trim_sections; /* # of sections to trim */
  799. struct list_head sit_entry_set; /* sit entry set list */
  800. unsigned int ipu_policy; /* in-place-update policy */
  801. unsigned int min_ipu_util; /* in-place-update threshold */
  802. unsigned int min_fsync_blocks; /* threshold for fsync */
  803. unsigned int min_seq_blocks; /* threshold for sequential blocks */
  804. unsigned int min_hot_blocks; /* threshold for hot block allocation */
  805. unsigned int min_ssr_sections; /* threshold to trigger SSR allocation */
  806. /* for flush command control */
  807. struct flush_cmd_control *fcc_info;
  808. /* for discard command control */
  809. struct discard_cmd_control *dcc_info;
  810. };
  811. /*
  812. * For superblock
  813. */
  814. /*
  815. * COUNT_TYPE for monitoring
  816. *
  817. * f2fs monitors the number of several block types such as on-writeback,
  818. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  819. */
  820. #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
  821. enum count_type {
  822. F2FS_DIRTY_DENTS,
  823. F2FS_DIRTY_DATA,
  824. F2FS_DIRTY_QDATA,
  825. F2FS_DIRTY_NODES,
  826. F2FS_DIRTY_META,
  827. F2FS_INMEM_PAGES,
  828. F2FS_DIRTY_IMETA,
  829. F2FS_WB_CP_DATA,
  830. F2FS_WB_DATA,
  831. NR_COUNT_TYPE,
  832. };
  833. /*
  834. * The below are the page types of bios used in submit_bio().
  835. * The available types are:
  836. * DATA User data pages. It operates as async mode.
  837. * NODE Node pages. It operates as async mode.
  838. * META FS metadata pages such as SIT, NAT, CP.
  839. * NR_PAGE_TYPE The number of page types.
  840. * META_FLUSH Make sure the previous pages are written
  841. * with waiting the bio's completion
  842. * ... Only can be used with META.
  843. */
  844. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  845. enum page_type {
  846. DATA,
  847. NODE,
  848. META,
  849. NR_PAGE_TYPE,
  850. META_FLUSH,
  851. INMEM, /* the below types are used by tracepoints only. */
  852. INMEM_DROP,
  853. INMEM_INVALIDATE,
  854. INMEM_REVOKE,
  855. IPU,
  856. OPU,
  857. };
  858. enum temp_type {
  859. HOT = 0, /* must be zero for meta bio */
  860. WARM,
  861. COLD,
  862. NR_TEMP_TYPE,
  863. };
  864. enum need_lock_type {
  865. LOCK_REQ = 0,
  866. LOCK_DONE,
  867. LOCK_RETRY,
  868. };
  869. enum cp_reason_type {
  870. CP_NO_NEEDED,
  871. CP_NON_REGULAR,
  872. CP_HARDLINK,
  873. CP_SB_NEED_CP,
  874. CP_WRONG_PINO,
  875. CP_NO_SPC_ROLL,
  876. CP_NODE_NEED_CP,
  877. CP_FASTBOOT_MODE,
  878. CP_SPEC_LOG_NUM,
  879. CP_RECOVER_DIR,
  880. };
  881. enum iostat_type {
  882. APP_DIRECT_IO, /* app direct IOs */
  883. APP_BUFFERED_IO, /* app buffered IOs */
  884. APP_WRITE_IO, /* app write IOs */
  885. APP_MAPPED_IO, /* app mapped IOs */
  886. FS_DATA_IO, /* data IOs from kworker/fsync/reclaimer */
  887. FS_NODE_IO, /* node IOs from kworker/fsync/reclaimer */
  888. FS_META_IO, /* meta IOs from kworker/reclaimer */
  889. FS_GC_DATA_IO, /* data IOs from forground gc */
  890. FS_GC_NODE_IO, /* node IOs from forground gc */
  891. FS_CP_DATA_IO, /* data IOs from checkpoint */
  892. FS_CP_NODE_IO, /* node IOs from checkpoint */
  893. FS_CP_META_IO, /* meta IOs from checkpoint */
  894. FS_DISCARD, /* discard */
  895. NR_IO_TYPE,
  896. };
  897. struct f2fs_io_info {
  898. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  899. nid_t ino; /* inode number */
  900. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  901. enum temp_type temp; /* contains HOT/WARM/COLD */
  902. int op; /* contains REQ_OP_ */
  903. int op_flags; /* req_flag_bits */
  904. block_t new_blkaddr; /* new block address to be written */
  905. block_t old_blkaddr; /* old block address before Cow */
  906. struct page *page; /* page to be written */
  907. struct page *encrypted_page; /* encrypted page */
  908. struct list_head list; /* serialize IOs */
  909. bool submitted; /* indicate IO submission */
  910. int need_lock; /* indicate we need to lock cp_rwsem */
  911. bool in_list; /* indicate fio is in io_list */
  912. bool is_meta; /* indicate borrow meta inode mapping or not */
  913. bool retry; /* need to reallocate block address */
  914. enum iostat_type io_type; /* io type */
  915. struct writeback_control *io_wbc; /* writeback control */
  916. unsigned char version; /* version of the node */
  917. };
  918. #define is_read_io(rw) ((rw) == READ)
  919. struct f2fs_bio_info {
  920. struct f2fs_sb_info *sbi; /* f2fs superblock */
  921. struct bio *bio; /* bios to merge */
  922. sector_t last_block_in_bio; /* last block number */
  923. struct f2fs_io_info fio; /* store buffered io info. */
  924. struct rw_semaphore io_rwsem; /* blocking op for bio */
  925. spinlock_t io_lock; /* serialize DATA/NODE IOs */
  926. struct list_head io_list; /* track fios */
  927. };
  928. #define FDEV(i) (sbi->devs[i])
  929. #define RDEV(i) (raw_super->devs[i])
  930. struct f2fs_dev_info {
  931. struct block_device *bdev;
  932. char path[MAX_PATH_LEN];
  933. unsigned int total_segments;
  934. block_t start_blk;
  935. block_t end_blk;
  936. #ifdef CONFIG_BLK_DEV_ZONED
  937. unsigned int nr_blkz; /* Total number of zones */
  938. u8 *blkz_type; /* Array of zones type */
  939. #endif
  940. };
  941. enum inode_type {
  942. DIR_INODE, /* for dirty dir inode */
  943. FILE_INODE, /* for dirty regular/symlink inode */
  944. DIRTY_META, /* for all dirtied inode metadata */
  945. ATOMIC_FILE, /* for all atomic files */
  946. NR_INODE_TYPE,
  947. };
  948. /* for inner inode cache management */
  949. struct inode_management {
  950. struct radix_tree_root ino_root; /* ino entry array */
  951. spinlock_t ino_lock; /* for ino entry lock */
  952. struct list_head ino_list; /* inode list head */
  953. unsigned long ino_num; /* number of entries */
  954. };
  955. /* For s_flag in struct f2fs_sb_info */
  956. enum {
  957. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  958. SBI_IS_CLOSE, /* specify unmounting */
  959. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  960. SBI_POR_DOING, /* recovery is doing or not */
  961. SBI_NEED_SB_WRITE, /* need to recover superblock */
  962. SBI_NEED_CP, /* need to checkpoint */
  963. SBI_IS_SHUTDOWN, /* shutdown by ioctl */
  964. SBI_IS_RECOVERED, /* recovered orphan/data */
  965. };
  966. enum {
  967. CP_TIME,
  968. REQ_TIME,
  969. MAX_TIME,
  970. };
  971. enum {
  972. GC_NORMAL,
  973. GC_IDLE_CB,
  974. GC_IDLE_GREEDY,
  975. GC_URGENT,
  976. };
  977. enum {
  978. WHINT_MODE_OFF, /* not pass down write hints */
  979. WHINT_MODE_USER, /* try to pass down hints given by users */
  980. WHINT_MODE_FS, /* pass down hints with F2FS policy */
  981. };
  982. enum {
  983. ALLOC_MODE_DEFAULT, /* stay default */
  984. ALLOC_MODE_REUSE, /* reuse segments as much as possible */
  985. };
  986. enum fsync_mode {
  987. FSYNC_MODE_POSIX, /* fsync follows posix semantics */
  988. FSYNC_MODE_STRICT, /* fsync behaves in line with ext4 */
  989. FSYNC_MODE_NOBARRIER, /* fsync behaves nobarrier based on posix */
  990. };
  991. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  992. #define DUMMY_ENCRYPTION_ENABLED(sbi) \
  993. (unlikely(F2FS_OPTION(sbi).test_dummy_encryption))
  994. #else
  995. #define DUMMY_ENCRYPTION_ENABLED(sbi) (0)
  996. #endif
  997. struct f2fs_sb_info {
  998. struct super_block *sb; /* pointer to VFS super block */
  999. struct proc_dir_entry *s_proc; /* proc entry */
  1000. struct f2fs_super_block *raw_super; /* raw super block pointer */
  1001. struct rw_semaphore sb_lock; /* lock for raw super block */
  1002. int valid_super_block; /* valid super block no */
  1003. unsigned long s_flag; /* flags for sbi */
  1004. struct mutex writepages; /* mutex for writepages() */
  1005. #ifdef CONFIG_BLK_DEV_ZONED
  1006. unsigned int blocks_per_blkz; /* F2FS blocks per zone */
  1007. unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
  1008. #endif
  1009. /* for node-related operations */
  1010. struct f2fs_nm_info *nm_info; /* node manager */
  1011. struct inode *node_inode; /* cache node blocks */
  1012. /* for segment-related operations */
  1013. struct f2fs_sm_info *sm_info; /* segment manager */
  1014. /* for bio operations */
  1015. struct f2fs_bio_info *write_io[NR_PAGE_TYPE]; /* for write bios */
  1016. struct mutex wio_mutex[NR_PAGE_TYPE - 1][NR_TEMP_TYPE];
  1017. /* bio ordering for NODE/DATA */
  1018. /* keep migration IO order for LFS mode */
  1019. struct rw_semaphore io_order_lock;
  1020. mempool_t *write_io_dummy; /* Dummy pages */
  1021. /* for checkpoint */
  1022. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  1023. int cur_cp_pack; /* remain current cp pack */
  1024. spinlock_t cp_lock; /* for flag in ckpt */
  1025. struct inode *meta_inode; /* cache meta blocks */
  1026. struct mutex cp_mutex; /* checkpoint procedure lock */
  1027. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  1028. struct rw_semaphore node_write; /* locking node writes */
  1029. struct rw_semaphore node_change; /* locking node change */
  1030. wait_queue_head_t cp_wait;
  1031. unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
  1032. long interval_time[MAX_TIME]; /* to store thresholds */
  1033. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  1034. spinlock_t fsync_node_lock; /* for node entry lock */
  1035. struct list_head fsync_node_list; /* node list head */
  1036. unsigned int fsync_seg_id; /* sequence id */
  1037. unsigned int fsync_node_num; /* number of node entries */
  1038. /* for orphan inode, use 0'th array */
  1039. unsigned int max_orphans; /* max orphan inodes */
  1040. /* for inode management */
  1041. struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
  1042. spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
  1043. /* for extent tree cache */
  1044. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  1045. struct mutex extent_tree_lock; /* locking extent radix tree */
  1046. struct list_head extent_list; /* lru list for shrinker */
  1047. spinlock_t extent_lock; /* locking extent lru list */
  1048. atomic_t total_ext_tree; /* extent tree count */
  1049. struct list_head zombie_list; /* extent zombie tree list */
  1050. atomic_t total_zombie_tree; /* extent zombie tree count */
  1051. atomic_t total_ext_node; /* extent info count */
  1052. /* basic filesystem units */
  1053. unsigned int log_sectors_per_block; /* log2 sectors per block */
  1054. unsigned int log_blocksize; /* log2 block size */
  1055. unsigned int blocksize; /* block size */
  1056. unsigned int root_ino_num; /* root inode number*/
  1057. unsigned int node_ino_num; /* node inode number*/
  1058. unsigned int meta_ino_num; /* meta inode number*/
  1059. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  1060. unsigned int blocks_per_seg; /* blocks per segment */
  1061. unsigned int segs_per_sec; /* segments per section */
  1062. unsigned int secs_per_zone; /* sections per zone */
  1063. unsigned int total_sections; /* total section count */
  1064. unsigned int total_node_count; /* total node block count */
  1065. unsigned int total_valid_node_count; /* valid node block count */
  1066. loff_t max_file_blocks; /* max block index of file */
  1067. int dir_level; /* directory level */
  1068. unsigned int trigger_ssr_threshold; /* threshold to trigger ssr */
  1069. int readdir_ra; /* readahead inode in readdir */
  1070. block_t user_block_count; /* # of user blocks */
  1071. block_t total_valid_block_count; /* # of valid blocks */
  1072. block_t discard_blks; /* discard command candidats */
  1073. block_t last_valid_block_count; /* for recovery */
  1074. block_t reserved_blocks; /* configurable reserved blocks */
  1075. block_t current_reserved_blocks; /* current reserved blocks */
  1076. unsigned int nquota_files; /* # of quota sysfile */
  1077. u32 s_next_generation; /* for NFS support */
  1078. /* # of pages, see count_type */
  1079. atomic_t nr_pages[NR_COUNT_TYPE];
  1080. /* # of allocated blocks */
  1081. struct percpu_counter alloc_valid_block_count;
  1082. /* writeback control */
  1083. atomic_t wb_sync_req[META]; /* count # of WB_SYNC threads */
  1084. /* valid inode count */
  1085. struct percpu_counter total_valid_inode_count;
  1086. struct f2fs_mount_info mount_opt; /* mount options */
  1087. /* for cleaning operations */
  1088. struct mutex gc_mutex; /* mutex for GC */
  1089. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  1090. unsigned int cur_victim_sec; /* current victim section num */
  1091. unsigned int gc_mode; /* current GC state */
  1092. /* for skip statistic */
  1093. unsigned long long skipped_atomic_files[2]; /* FG_GC and BG_GC */
  1094. unsigned long long skipped_gc_rwsem; /* FG_GC only */
  1095. /* threshold for gc trials on pinned files */
  1096. u64 gc_pin_file_threshold;
  1097. /* maximum # of trials to find a victim segment for SSR and GC */
  1098. unsigned int max_victim_search;
  1099. /*
  1100. * for stat information.
  1101. * one is for the LFS mode, and the other is for the SSR mode.
  1102. */
  1103. #ifdef CONFIG_F2FS_STAT_FS
  1104. struct f2fs_stat_info *stat_info; /* FS status information */
  1105. unsigned int segment_count[2]; /* # of allocated segments */
  1106. unsigned int block_count[2]; /* # of allocated blocks */
  1107. atomic_t inplace_count; /* # of inplace update */
  1108. atomic64_t total_hit_ext; /* # of lookup extent cache */
  1109. atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
  1110. atomic64_t read_hit_largest; /* # of hit largest extent node */
  1111. atomic64_t read_hit_cached; /* # of hit cached extent node */
  1112. atomic_t inline_xattr; /* # of inline_xattr inodes */
  1113. atomic_t inline_inode; /* # of inline_data inodes */
  1114. atomic_t inline_dir; /* # of inline_dentry inodes */
  1115. atomic_t aw_cnt; /* # of atomic writes */
  1116. atomic_t vw_cnt; /* # of volatile writes */
  1117. atomic_t max_aw_cnt; /* max # of atomic writes */
  1118. atomic_t max_vw_cnt; /* max # of volatile writes */
  1119. int bg_gc; /* background gc calls */
  1120. unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
  1121. #endif
  1122. spinlock_t stat_lock; /* lock for stat operations */
  1123. /* For app/fs IO statistics */
  1124. spinlock_t iostat_lock;
  1125. unsigned long long write_iostat[NR_IO_TYPE];
  1126. bool iostat_enable;
  1127. /* For sysfs suppport */
  1128. struct kobject s_kobj;
  1129. struct completion s_kobj_unregister;
  1130. /* For shrinker support */
  1131. struct list_head s_list;
  1132. int s_ndevs; /* number of devices */
  1133. struct f2fs_dev_info *devs; /* for device list */
  1134. unsigned int dirty_device; /* for checkpoint data flush */
  1135. spinlock_t dev_lock; /* protect dirty_device */
  1136. struct mutex umount_mutex;
  1137. unsigned int shrinker_run_no;
  1138. /* For write statistics */
  1139. u64 sectors_written_start;
  1140. u64 kbytes_written;
  1141. /* Reference to checksum algorithm driver via cryptoapi */
  1142. struct crypto_shash *s_chksum_driver;
  1143. /* Precomputed FS UUID checksum for seeding other checksums */
  1144. __u32 s_chksum_seed;
  1145. };
  1146. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1147. #define f2fs_show_injection_info(type) \
  1148. printk("%sF2FS-fs : inject %s in %s of %pF\n", \
  1149. KERN_INFO, f2fs_fault_name[type], \
  1150. __func__, __builtin_return_address(0))
  1151. static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
  1152. {
  1153. struct f2fs_fault_info *ffi = &F2FS_OPTION(sbi).fault_info;
  1154. if (!ffi->inject_rate)
  1155. return false;
  1156. if (!IS_FAULT_SET(ffi, type))
  1157. return false;
  1158. atomic_inc(&ffi->inject_ops);
  1159. if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
  1160. atomic_set(&ffi->inject_ops, 0);
  1161. return true;
  1162. }
  1163. return false;
  1164. }
  1165. #else
  1166. #define f2fs_show_injection_info(type) do { } while (0)
  1167. static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
  1168. {
  1169. return false;
  1170. }
  1171. #endif
  1172. /*
  1173. * Test if the mounted volume is a multi-device volume.
  1174. * - For a single regular disk volume, sbi->s_ndevs is 0.
  1175. * - For a single zoned disk volume, sbi->s_ndevs is 1.
  1176. * - For a multi-device volume, sbi->s_ndevs is always 2 or more.
  1177. */
  1178. static inline bool f2fs_is_multi_device(struct f2fs_sb_info *sbi)
  1179. {
  1180. return sbi->s_ndevs > 1;
  1181. }
  1182. /* For write statistics. Suppose sector size is 512 bytes,
  1183. * and the return value is in kbytes. s is of struct f2fs_sb_info.
  1184. */
  1185. #define BD_PART_WRITTEN(s) \
  1186. (((u64)part_stat_read((s)->sb->s_bdev->bd_part, sectors[STAT_WRITE]) - \
  1187. (s)->sectors_written_start) >> 1)
  1188. static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
  1189. {
  1190. sbi->last_time[type] = jiffies;
  1191. }
  1192. static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
  1193. {
  1194. unsigned long interval = sbi->interval_time[type] * HZ;
  1195. return time_after(jiffies, sbi->last_time[type] + interval);
  1196. }
  1197. static inline bool is_idle(struct f2fs_sb_info *sbi)
  1198. {
  1199. struct block_device *bdev = sbi->sb->s_bdev;
  1200. struct request_queue *q = bdev_get_queue(bdev);
  1201. struct request_list *rl = &q->root_rl;
  1202. if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
  1203. return false;
  1204. return f2fs_time_over(sbi, REQ_TIME);
  1205. }
  1206. /*
  1207. * Inline functions
  1208. */
  1209. static inline u32 __f2fs_crc32(struct f2fs_sb_info *sbi, u32 crc,
  1210. const void *address, unsigned int length)
  1211. {
  1212. struct {
  1213. struct shash_desc shash;
  1214. char ctx[4];
  1215. } desc;
  1216. int err;
  1217. BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver) != sizeof(desc.ctx));
  1218. desc.shash.tfm = sbi->s_chksum_driver;
  1219. desc.shash.flags = 0;
  1220. *(u32 *)desc.ctx = crc;
  1221. err = crypto_shash_update(&desc.shash, address, length);
  1222. BUG_ON(err);
  1223. return *(u32 *)desc.ctx;
  1224. }
  1225. static inline u32 f2fs_crc32(struct f2fs_sb_info *sbi, const void *address,
  1226. unsigned int length)
  1227. {
  1228. return __f2fs_crc32(sbi, F2FS_SUPER_MAGIC, address, length);
  1229. }
  1230. static inline bool f2fs_crc_valid(struct f2fs_sb_info *sbi, __u32 blk_crc,
  1231. void *buf, size_t buf_size)
  1232. {
  1233. return f2fs_crc32(sbi, buf, buf_size) == blk_crc;
  1234. }
  1235. static inline u32 f2fs_chksum(struct f2fs_sb_info *sbi, u32 crc,
  1236. const void *address, unsigned int length)
  1237. {
  1238. return __f2fs_crc32(sbi, crc, address, length);
  1239. }
  1240. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  1241. {
  1242. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  1243. }
  1244. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  1245. {
  1246. return sb->s_fs_info;
  1247. }
  1248. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  1249. {
  1250. return F2FS_SB(inode->i_sb);
  1251. }
  1252. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  1253. {
  1254. return F2FS_I_SB(mapping->host);
  1255. }
  1256. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  1257. {
  1258. return F2FS_M_SB(page->mapping);
  1259. }
  1260. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  1261. {
  1262. return (struct f2fs_super_block *)(sbi->raw_super);
  1263. }
  1264. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  1265. {
  1266. return (struct f2fs_checkpoint *)(sbi->ckpt);
  1267. }
  1268. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  1269. {
  1270. return (struct f2fs_node *)page_address(page);
  1271. }
  1272. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  1273. {
  1274. return &((struct f2fs_node *)page_address(page))->i;
  1275. }
  1276. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  1277. {
  1278. return (struct f2fs_nm_info *)(sbi->nm_info);
  1279. }
  1280. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  1281. {
  1282. return (struct f2fs_sm_info *)(sbi->sm_info);
  1283. }
  1284. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  1285. {
  1286. return (struct sit_info *)(SM_I(sbi)->sit_info);
  1287. }
  1288. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  1289. {
  1290. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  1291. }
  1292. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  1293. {
  1294. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  1295. }
  1296. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  1297. {
  1298. return sbi->meta_inode->i_mapping;
  1299. }
  1300. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  1301. {
  1302. return sbi->node_inode->i_mapping;
  1303. }
  1304. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  1305. {
  1306. return test_bit(type, &sbi->s_flag);
  1307. }
  1308. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1309. {
  1310. set_bit(type, &sbi->s_flag);
  1311. }
  1312. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  1313. {
  1314. clear_bit(type, &sbi->s_flag);
  1315. }
  1316. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  1317. {
  1318. return le64_to_cpu(cp->checkpoint_ver);
  1319. }
  1320. static inline unsigned long f2fs_qf_ino(struct super_block *sb, int type)
  1321. {
  1322. if (type < F2FS_MAX_QUOTAS)
  1323. return le32_to_cpu(F2FS_SB(sb)->raw_super->qf_ino[type]);
  1324. return 0;
  1325. }
  1326. static inline __u64 cur_cp_crc(struct f2fs_checkpoint *cp)
  1327. {
  1328. size_t crc_offset = le32_to_cpu(cp->checksum_offset);
  1329. return le32_to_cpu(*((__le32 *)((unsigned char *)cp + crc_offset)));
  1330. }
  1331. static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1332. {
  1333. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1334. return ckpt_flags & f;
  1335. }
  1336. static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1337. {
  1338. return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
  1339. }
  1340. static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1341. {
  1342. unsigned int ckpt_flags;
  1343. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1344. ckpt_flags |= f;
  1345. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  1346. }
  1347. static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1348. {
  1349. unsigned long flags;
  1350. spin_lock_irqsave(&sbi->cp_lock, flags);
  1351. __set_ckpt_flags(F2FS_CKPT(sbi), f);
  1352. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1353. }
  1354. static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  1355. {
  1356. unsigned int ckpt_flags;
  1357. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  1358. ckpt_flags &= (~f);
  1359. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  1360. }
  1361. static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1362. {
  1363. unsigned long flags;
  1364. spin_lock_irqsave(&sbi->cp_lock, flags);
  1365. __clear_ckpt_flags(F2FS_CKPT(sbi), f);
  1366. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1367. }
  1368. static inline void disable_nat_bits(struct f2fs_sb_info *sbi, bool lock)
  1369. {
  1370. unsigned long flags;
  1371. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1372. if (lock)
  1373. spin_lock_irqsave(&sbi->cp_lock, flags);
  1374. __clear_ckpt_flags(F2FS_CKPT(sbi), CP_NAT_BITS_FLAG);
  1375. kfree(NM_I(sbi)->nat_bits);
  1376. NM_I(sbi)->nat_bits = NULL;
  1377. if (lock)
  1378. spin_unlock_irqrestore(&sbi->cp_lock, flags);
  1379. }
  1380. static inline bool enabled_nat_bits(struct f2fs_sb_info *sbi,
  1381. struct cp_control *cpc)
  1382. {
  1383. bool set = is_set_ckpt_flags(sbi, CP_NAT_BITS_FLAG);
  1384. return (cpc) ? (cpc->reason & CP_UMOUNT) && set : set;
  1385. }
  1386. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  1387. {
  1388. down_read(&sbi->cp_rwsem);
  1389. }
  1390. static inline int f2fs_trylock_op(struct f2fs_sb_info *sbi)
  1391. {
  1392. return down_read_trylock(&sbi->cp_rwsem);
  1393. }
  1394. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  1395. {
  1396. up_read(&sbi->cp_rwsem);
  1397. }
  1398. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  1399. {
  1400. down_write(&sbi->cp_rwsem);
  1401. }
  1402. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  1403. {
  1404. up_write(&sbi->cp_rwsem);
  1405. }
  1406. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  1407. {
  1408. int reason = CP_SYNC;
  1409. if (test_opt(sbi, FASTBOOT))
  1410. reason = CP_FASTBOOT;
  1411. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  1412. reason = CP_UMOUNT;
  1413. return reason;
  1414. }
  1415. static inline bool __remain_node_summaries(int reason)
  1416. {
  1417. return (reason & (CP_UMOUNT | CP_FASTBOOT));
  1418. }
  1419. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  1420. {
  1421. return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
  1422. is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
  1423. }
  1424. /*
  1425. * Check whether the inode has blocks or not
  1426. */
  1427. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  1428. {
  1429. block_t xattr_block = F2FS_I(inode)->i_xattr_nid ? 1 : 0;
  1430. return (inode->i_blocks >> F2FS_LOG_SECTORS_PER_BLOCK) > xattr_block;
  1431. }
  1432. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  1433. {
  1434. return ofs == XATTR_NODE_OFFSET;
  1435. }
  1436. static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi,
  1437. struct inode *inode, bool cap)
  1438. {
  1439. if (!inode)
  1440. return true;
  1441. if (!test_opt(sbi, RESERVE_ROOT))
  1442. return false;
  1443. if (IS_NOQUOTA(inode))
  1444. return true;
  1445. if (uid_eq(F2FS_OPTION(sbi).s_resuid, current_fsuid()))
  1446. return true;
  1447. if (!gid_eq(F2FS_OPTION(sbi).s_resgid, GLOBAL_ROOT_GID) &&
  1448. in_group_p(F2FS_OPTION(sbi).s_resgid))
  1449. return true;
  1450. if (cap && capable(CAP_SYS_RESOURCE))
  1451. return true;
  1452. return false;
  1453. }
  1454. static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool);
  1455. static inline int inc_valid_block_count(struct f2fs_sb_info *sbi,
  1456. struct inode *inode, blkcnt_t *count)
  1457. {
  1458. blkcnt_t diff = 0, release = 0;
  1459. block_t avail_user_block_count;
  1460. int ret;
  1461. ret = dquot_reserve_block(inode, *count);
  1462. if (ret)
  1463. return ret;
  1464. if (time_to_inject(sbi, FAULT_BLOCK)) {
  1465. f2fs_show_injection_info(FAULT_BLOCK);
  1466. release = *count;
  1467. goto release_quota;
  1468. }
  1469. /*
  1470. * let's increase this in prior to actual block count change in order
  1471. * for f2fs_sync_file to avoid data races when deciding checkpoint.
  1472. */
  1473. percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
  1474. spin_lock(&sbi->stat_lock);
  1475. sbi->total_valid_block_count += (block_t)(*count);
  1476. avail_user_block_count = sbi->user_block_count -
  1477. sbi->current_reserved_blocks;
  1478. if (!__allow_reserved_blocks(sbi, inode, true))
  1479. avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
  1480. if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) {
  1481. diff = sbi->total_valid_block_count - avail_user_block_count;
  1482. if (diff > *count)
  1483. diff = *count;
  1484. *count -= diff;
  1485. release = diff;
  1486. sbi->total_valid_block_count -= diff;
  1487. if (!*count) {
  1488. spin_unlock(&sbi->stat_lock);
  1489. goto enospc;
  1490. }
  1491. }
  1492. spin_unlock(&sbi->stat_lock);
  1493. if (unlikely(release)) {
  1494. percpu_counter_sub(&sbi->alloc_valid_block_count, release);
  1495. dquot_release_reservation_block(inode, release);
  1496. }
  1497. f2fs_i_blocks_write(inode, *count, true, true);
  1498. return 0;
  1499. enospc:
  1500. percpu_counter_sub(&sbi->alloc_valid_block_count, release);
  1501. release_quota:
  1502. dquot_release_reservation_block(inode, release);
  1503. return -ENOSPC;
  1504. }
  1505. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  1506. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  1507. struct inode *inode,
  1508. block_t count)
  1509. {
  1510. blkcnt_t sectors = count << F2FS_LOG_SECTORS_PER_BLOCK;
  1511. spin_lock(&sbi->stat_lock);
  1512. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  1513. sbi->total_valid_block_count -= (block_t)count;
  1514. if (sbi->reserved_blocks &&
  1515. sbi->current_reserved_blocks < sbi->reserved_blocks)
  1516. sbi->current_reserved_blocks = min(sbi->reserved_blocks,
  1517. sbi->current_reserved_blocks + count);
  1518. spin_unlock(&sbi->stat_lock);
  1519. if (unlikely(inode->i_blocks < sectors)) {
  1520. f2fs_msg(sbi->sb, KERN_WARNING,
  1521. "Inconsistent i_blocks, ino:%lu, iblocks:%llu, sectors:%llu",
  1522. inode->i_ino,
  1523. (unsigned long long)inode->i_blocks,
  1524. (unsigned long long)sectors);
  1525. set_sbi_flag(sbi, SBI_NEED_FSCK);
  1526. return;
  1527. }
  1528. f2fs_i_blocks_write(inode, count, false, true);
  1529. }
  1530. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  1531. {
  1532. atomic_inc(&sbi->nr_pages[count_type]);
  1533. if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
  1534. count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
  1535. return;
  1536. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1537. }
  1538. static inline void inode_inc_dirty_pages(struct inode *inode)
  1539. {
  1540. atomic_inc(&F2FS_I(inode)->dirty_pages);
  1541. inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1542. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1543. if (IS_NOQUOTA(inode))
  1544. inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
  1545. }
  1546. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  1547. {
  1548. atomic_dec(&sbi->nr_pages[count_type]);
  1549. }
  1550. static inline void inode_dec_dirty_pages(struct inode *inode)
  1551. {
  1552. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  1553. !S_ISLNK(inode->i_mode))
  1554. return;
  1555. atomic_dec(&F2FS_I(inode)->dirty_pages);
  1556. dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1557. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1558. if (IS_NOQUOTA(inode))
  1559. dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_QDATA);
  1560. }
  1561. static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
  1562. {
  1563. return atomic_read(&sbi->nr_pages[count_type]);
  1564. }
  1565. static inline int get_dirty_pages(struct inode *inode)
  1566. {
  1567. return atomic_read(&F2FS_I(inode)->dirty_pages);
  1568. }
  1569. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  1570. {
  1571. unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
  1572. unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
  1573. sbi->log_blocks_per_seg;
  1574. return segs / sbi->segs_per_sec;
  1575. }
  1576. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  1577. {
  1578. return sbi->total_valid_block_count;
  1579. }
  1580. static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
  1581. {
  1582. return sbi->discard_blks;
  1583. }
  1584. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  1585. {
  1586. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1587. /* return NAT or SIT bitmap */
  1588. if (flag == NAT_BITMAP)
  1589. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  1590. else if (flag == SIT_BITMAP)
  1591. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  1592. return 0;
  1593. }
  1594. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  1595. {
  1596. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  1597. }
  1598. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  1599. {
  1600. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1601. int offset;
  1602. if (is_set_ckpt_flags(sbi, CP_LARGE_NAT_BITMAP_FLAG)) {
  1603. offset = (flag == SIT_BITMAP) ?
  1604. le32_to_cpu(ckpt->nat_ver_bitmap_bytesize) : 0;
  1605. return &ckpt->sit_nat_version_bitmap + offset;
  1606. }
  1607. if (__cp_payload(sbi) > 0) {
  1608. if (flag == NAT_BITMAP)
  1609. return &ckpt->sit_nat_version_bitmap;
  1610. else
  1611. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  1612. } else {
  1613. offset = (flag == NAT_BITMAP) ?
  1614. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  1615. return &ckpt->sit_nat_version_bitmap + offset;
  1616. }
  1617. }
  1618. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  1619. {
  1620. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1621. if (sbi->cur_cp_pack == 2)
  1622. start_addr += sbi->blocks_per_seg;
  1623. return start_addr;
  1624. }
  1625. static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
  1626. {
  1627. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1628. if (sbi->cur_cp_pack == 1)
  1629. start_addr += sbi->blocks_per_seg;
  1630. return start_addr;
  1631. }
  1632. static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
  1633. {
  1634. sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
  1635. }
  1636. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  1637. {
  1638. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  1639. }
  1640. static inline int inc_valid_node_count(struct f2fs_sb_info *sbi,
  1641. struct inode *inode, bool is_inode)
  1642. {
  1643. block_t valid_block_count;
  1644. unsigned int valid_node_count;
  1645. bool quota = inode && !is_inode;
  1646. if (quota) {
  1647. int ret = dquot_reserve_block(inode, 1);
  1648. if (ret)
  1649. return ret;
  1650. }
  1651. if (time_to_inject(sbi, FAULT_BLOCK)) {
  1652. f2fs_show_injection_info(FAULT_BLOCK);
  1653. goto enospc;
  1654. }
  1655. spin_lock(&sbi->stat_lock);
  1656. valid_block_count = sbi->total_valid_block_count +
  1657. sbi->current_reserved_blocks + 1;
  1658. if (!__allow_reserved_blocks(sbi, inode, false))
  1659. valid_block_count += F2FS_OPTION(sbi).root_reserved_blocks;
  1660. if (unlikely(valid_block_count > sbi->user_block_count)) {
  1661. spin_unlock(&sbi->stat_lock);
  1662. goto enospc;
  1663. }
  1664. valid_node_count = sbi->total_valid_node_count + 1;
  1665. if (unlikely(valid_node_count > sbi->total_node_count)) {
  1666. spin_unlock(&sbi->stat_lock);
  1667. goto enospc;
  1668. }
  1669. sbi->total_valid_node_count++;
  1670. sbi->total_valid_block_count++;
  1671. spin_unlock(&sbi->stat_lock);
  1672. if (inode) {
  1673. if (is_inode)
  1674. f2fs_mark_inode_dirty_sync(inode, true);
  1675. else
  1676. f2fs_i_blocks_write(inode, 1, true, true);
  1677. }
  1678. percpu_counter_inc(&sbi->alloc_valid_block_count);
  1679. return 0;
  1680. enospc:
  1681. if (quota)
  1682. dquot_release_reservation_block(inode, 1);
  1683. return -ENOSPC;
  1684. }
  1685. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  1686. struct inode *inode, bool is_inode)
  1687. {
  1688. spin_lock(&sbi->stat_lock);
  1689. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  1690. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  1691. f2fs_bug_on(sbi, !is_inode && !inode->i_blocks);
  1692. sbi->total_valid_node_count--;
  1693. sbi->total_valid_block_count--;
  1694. if (sbi->reserved_blocks &&
  1695. sbi->current_reserved_blocks < sbi->reserved_blocks)
  1696. sbi->current_reserved_blocks++;
  1697. spin_unlock(&sbi->stat_lock);
  1698. if (!is_inode)
  1699. f2fs_i_blocks_write(inode, 1, false, true);
  1700. }
  1701. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  1702. {
  1703. return sbi->total_valid_node_count;
  1704. }
  1705. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  1706. {
  1707. percpu_counter_inc(&sbi->total_valid_inode_count);
  1708. }
  1709. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  1710. {
  1711. percpu_counter_dec(&sbi->total_valid_inode_count);
  1712. }
  1713. static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
  1714. {
  1715. return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
  1716. }
  1717. static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
  1718. pgoff_t index, bool for_write)
  1719. {
  1720. struct page *page;
  1721. if (IS_ENABLED(CONFIG_F2FS_FAULT_INJECTION)) {
  1722. if (!for_write)
  1723. page = find_get_page_flags(mapping, index,
  1724. FGP_LOCK | FGP_ACCESSED);
  1725. else
  1726. page = find_lock_page(mapping, index);
  1727. if (page)
  1728. return page;
  1729. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC)) {
  1730. f2fs_show_injection_info(FAULT_PAGE_ALLOC);
  1731. return NULL;
  1732. }
  1733. }
  1734. if (!for_write)
  1735. return grab_cache_page(mapping, index);
  1736. return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
  1737. }
  1738. static inline struct page *f2fs_pagecache_get_page(
  1739. struct address_space *mapping, pgoff_t index,
  1740. int fgp_flags, gfp_t gfp_mask)
  1741. {
  1742. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_GET)) {
  1743. f2fs_show_injection_info(FAULT_PAGE_GET);
  1744. return NULL;
  1745. }
  1746. return pagecache_get_page(mapping, index, fgp_flags, gfp_mask);
  1747. }
  1748. static inline void f2fs_copy_page(struct page *src, struct page *dst)
  1749. {
  1750. char *src_kaddr = kmap(src);
  1751. char *dst_kaddr = kmap(dst);
  1752. memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
  1753. kunmap(dst);
  1754. kunmap(src);
  1755. }
  1756. static inline void f2fs_put_page(struct page *page, int unlock)
  1757. {
  1758. if (!page)
  1759. return;
  1760. if (unlock) {
  1761. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  1762. unlock_page(page);
  1763. }
  1764. put_page(page);
  1765. }
  1766. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  1767. {
  1768. if (dn->node_page)
  1769. f2fs_put_page(dn->node_page, 1);
  1770. if (dn->inode_page && dn->node_page != dn->inode_page)
  1771. f2fs_put_page(dn->inode_page, 0);
  1772. dn->node_page = NULL;
  1773. dn->inode_page = NULL;
  1774. }
  1775. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  1776. size_t size)
  1777. {
  1778. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1779. }
  1780. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  1781. gfp_t flags)
  1782. {
  1783. void *entry;
  1784. entry = kmem_cache_alloc(cachep, flags);
  1785. if (!entry)
  1786. entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
  1787. return entry;
  1788. }
  1789. static inline struct bio *f2fs_bio_alloc(struct f2fs_sb_info *sbi,
  1790. int npages, bool no_fail)
  1791. {
  1792. struct bio *bio;
  1793. if (no_fail) {
  1794. /* No failure on bio allocation */
  1795. bio = bio_alloc(GFP_NOIO, npages);
  1796. if (!bio)
  1797. bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
  1798. return bio;
  1799. }
  1800. if (time_to_inject(sbi, FAULT_ALLOC_BIO)) {
  1801. f2fs_show_injection_info(FAULT_ALLOC_BIO);
  1802. return NULL;
  1803. }
  1804. return bio_alloc(GFP_KERNEL, npages);
  1805. }
  1806. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  1807. unsigned long index, void *item)
  1808. {
  1809. while (radix_tree_insert(root, index, item))
  1810. cond_resched();
  1811. }
  1812. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  1813. static inline bool IS_INODE(struct page *page)
  1814. {
  1815. struct f2fs_node *p = F2FS_NODE(page);
  1816. return RAW_IS_INODE(p);
  1817. }
  1818. static inline int offset_in_addr(struct f2fs_inode *i)
  1819. {
  1820. return (i->i_inline & F2FS_EXTRA_ATTR) ?
  1821. (le16_to_cpu(i->i_extra_isize) / sizeof(__le32)) : 0;
  1822. }
  1823. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  1824. {
  1825. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  1826. }
  1827. static inline int f2fs_has_extra_attr(struct inode *inode);
  1828. static inline block_t datablock_addr(struct inode *inode,
  1829. struct page *node_page, unsigned int offset)
  1830. {
  1831. struct f2fs_node *raw_node;
  1832. __le32 *addr_array;
  1833. int base = 0;
  1834. bool is_inode = IS_INODE(node_page);
  1835. raw_node = F2FS_NODE(node_page);
  1836. /* from GC path only */
  1837. if (is_inode) {
  1838. if (!inode)
  1839. base = offset_in_addr(&raw_node->i);
  1840. else if (f2fs_has_extra_attr(inode))
  1841. base = get_extra_isize(inode);
  1842. }
  1843. addr_array = blkaddr_in_node(raw_node);
  1844. return le32_to_cpu(addr_array[base + offset]);
  1845. }
  1846. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  1847. {
  1848. int mask;
  1849. addr += (nr >> 3);
  1850. mask = 1 << (7 - (nr & 0x07));
  1851. return mask & *addr;
  1852. }
  1853. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  1854. {
  1855. int mask;
  1856. addr += (nr >> 3);
  1857. mask = 1 << (7 - (nr & 0x07));
  1858. *addr |= mask;
  1859. }
  1860. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  1861. {
  1862. int mask;
  1863. addr += (nr >> 3);
  1864. mask = 1 << (7 - (nr & 0x07));
  1865. *addr &= ~mask;
  1866. }
  1867. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  1868. {
  1869. int mask;
  1870. int ret;
  1871. addr += (nr >> 3);
  1872. mask = 1 << (7 - (nr & 0x07));
  1873. ret = mask & *addr;
  1874. *addr |= mask;
  1875. return ret;
  1876. }
  1877. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  1878. {
  1879. int mask;
  1880. int ret;
  1881. addr += (nr >> 3);
  1882. mask = 1 << (7 - (nr & 0x07));
  1883. ret = mask & *addr;
  1884. *addr &= ~mask;
  1885. return ret;
  1886. }
  1887. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  1888. {
  1889. int mask;
  1890. addr += (nr >> 3);
  1891. mask = 1 << (7 - (nr & 0x07));
  1892. *addr ^= mask;
  1893. }
  1894. /*
  1895. * Inode flags
  1896. */
  1897. #define F2FS_SECRM_FL 0x00000001 /* Secure deletion */
  1898. #define F2FS_UNRM_FL 0x00000002 /* Undelete */
  1899. #define F2FS_COMPR_FL 0x00000004 /* Compress file */
  1900. #define F2FS_SYNC_FL 0x00000008 /* Synchronous updates */
  1901. #define F2FS_IMMUTABLE_FL 0x00000010 /* Immutable file */
  1902. #define F2FS_APPEND_FL 0x00000020 /* writes to file may only append */
  1903. #define F2FS_NODUMP_FL 0x00000040 /* do not dump file */
  1904. #define F2FS_NOATIME_FL 0x00000080 /* do not update atime */
  1905. /* Reserved for compression usage... */
  1906. #define F2FS_DIRTY_FL 0x00000100
  1907. #define F2FS_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
  1908. #define F2FS_NOCOMPR_FL 0x00000400 /* Don't compress */
  1909. #define F2FS_ENCRYPT_FL 0x00000800 /* encrypted file */
  1910. /* End compression flags --- maybe not all used */
  1911. #define F2FS_INDEX_FL 0x00001000 /* hash-indexed directory */
  1912. #define F2FS_IMAGIC_FL 0x00002000 /* AFS directory */
  1913. #define F2FS_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
  1914. #define F2FS_NOTAIL_FL 0x00008000 /* file tail should not be merged */
  1915. #define F2FS_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
  1916. #define F2FS_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
  1917. #define F2FS_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
  1918. #define F2FS_EXTENTS_FL 0x00080000 /* Inode uses extents */
  1919. #define F2FS_EA_INODE_FL 0x00200000 /* Inode used for large EA */
  1920. #define F2FS_EOFBLOCKS_FL 0x00400000 /* Blocks allocated beyond EOF */
  1921. #define F2FS_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
  1922. #define F2FS_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
  1923. #define F2FS_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
  1924. #define F2FS_FL_USER_VISIBLE 0x304BDFFF /* User visible flags */
  1925. #define F2FS_FL_USER_MODIFIABLE 0x204BC0FF /* User modifiable flags */
  1926. /* Flags we can manipulate with through F2FS_IOC_FSSETXATTR */
  1927. #define F2FS_FL_XFLAG_VISIBLE (F2FS_SYNC_FL | \
  1928. F2FS_IMMUTABLE_FL | \
  1929. F2FS_APPEND_FL | \
  1930. F2FS_NODUMP_FL | \
  1931. F2FS_NOATIME_FL | \
  1932. F2FS_PROJINHERIT_FL)
  1933. /* Flags that should be inherited by new inodes from their parent. */
  1934. #define F2FS_FL_INHERITED (F2FS_SECRM_FL | F2FS_UNRM_FL | F2FS_COMPR_FL |\
  1935. F2FS_SYNC_FL | F2FS_NODUMP_FL | F2FS_NOATIME_FL |\
  1936. F2FS_NOCOMPR_FL | F2FS_JOURNAL_DATA_FL |\
  1937. F2FS_NOTAIL_FL | F2FS_DIRSYNC_FL |\
  1938. F2FS_PROJINHERIT_FL)
  1939. /* Flags that are appropriate for regular files (all but dir-specific ones). */
  1940. #define F2FS_REG_FLMASK (~(F2FS_DIRSYNC_FL | F2FS_TOPDIR_FL))
  1941. /* Flags that are appropriate for non-directories/regular files. */
  1942. #define F2FS_OTHER_FLMASK (F2FS_NODUMP_FL | F2FS_NOATIME_FL)
  1943. static inline __u32 f2fs_mask_flags(umode_t mode, __u32 flags)
  1944. {
  1945. if (S_ISDIR(mode))
  1946. return flags;
  1947. else if (S_ISREG(mode))
  1948. return flags & F2FS_REG_FLMASK;
  1949. else
  1950. return flags & F2FS_OTHER_FLMASK;
  1951. }
  1952. /* used for f2fs_inode_info->flags */
  1953. enum {
  1954. FI_NEW_INODE, /* indicate newly allocated inode */
  1955. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  1956. FI_AUTO_RECOVER, /* indicate inode is recoverable */
  1957. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  1958. FI_INC_LINK, /* need to increment i_nlink */
  1959. FI_ACL_MODE, /* indicate acl mode */
  1960. FI_NO_ALLOC, /* should not allocate any blocks */
  1961. FI_FREE_NID, /* free allocated nide */
  1962. FI_NO_EXTENT, /* not to use the extent cache */
  1963. FI_INLINE_XATTR, /* used for inline xattr */
  1964. FI_INLINE_DATA, /* used for inline data*/
  1965. FI_INLINE_DENTRY, /* used for inline dentry */
  1966. FI_APPEND_WRITE, /* inode has appended data */
  1967. FI_UPDATE_WRITE, /* inode has in-place-update data */
  1968. FI_NEED_IPU, /* used for ipu per file */
  1969. FI_ATOMIC_FILE, /* indicate atomic file */
  1970. FI_ATOMIC_COMMIT, /* indicate the state of atomical committing */
  1971. FI_VOLATILE_FILE, /* indicate volatile file */
  1972. FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
  1973. FI_DROP_CACHE, /* drop dirty page cache */
  1974. FI_DATA_EXIST, /* indicate data exists */
  1975. FI_INLINE_DOTS, /* indicate inline dot dentries */
  1976. FI_DO_DEFRAG, /* indicate defragment is running */
  1977. FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
  1978. FI_NO_PREALLOC, /* indicate skipped preallocated blocks */
  1979. FI_HOT_DATA, /* indicate file is hot */
  1980. FI_EXTRA_ATTR, /* indicate file has extra attribute */
  1981. FI_PROJ_INHERIT, /* indicate file inherits projectid */
  1982. FI_PIN_FILE, /* indicate file should not be gced */
  1983. FI_ATOMIC_REVOKE_REQUEST, /* request to drop atomic data */
  1984. };
  1985. static inline void __mark_inode_dirty_flag(struct inode *inode,
  1986. int flag, bool set)
  1987. {
  1988. switch (flag) {
  1989. case FI_INLINE_XATTR:
  1990. case FI_INLINE_DATA:
  1991. case FI_INLINE_DENTRY:
  1992. case FI_NEW_INODE:
  1993. if (set)
  1994. return;
  1995. case FI_DATA_EXIST:
  1996. case FI_INLINE_DOTS:
  1997. case FI_PIN_FILE:
  1998. f2fs_mark_inode_dirty_sync(inode, true);
  1999. }
  2000. }
  2001. static inline void set_inode_flag(struct inode *inode, int flag)
  2002. {
  2003. if (!test_bit(flag, &F2FS_I(inode)->flags))
  2004. set_bit(flag, &F2FS_I(inode)->flags);
  2005. __mark_inode_dirty_flag(inode, flag, true);
  2006. }
  2007. static inline int is_inode_flag_set(struct inode *inode, int flag)
  2008. {
  2009. return test_bit(flag, &F2FS_I(inode)->flags);
  2010. }
  2011. static inline void clear_inode_flag(struct inode *inode, int flag)
  2012. {
  2013. if (test_bit(flag, &F2FS_I(inode)->flags))
  2014. clear_bit(flag, &F2FS_I(inode)->flags);
  2015. __mark_inode_dirty_flag(inode, flag, false);
  2016. }
  2017. static inline void set_acl_inode(struct inode *inode, umode_t mode)
  2018. {
  2019. F2FS_I(inode)->i_acl_mode = mode;
  2020. set_inode_flag(inode, FI_ACL_MODE);
  2021. f2fs_mark_inode_dirty_sync(inode, false);
  2022. }
  2023. static inline void f2fs_i_links_write(struct inode *inode, bool inc)
  2024. {
  2025. if (inc)
  2026. inc_nlink(inode);
  2027. else
  2028. drop_nlink(inode);
  2029. f2fs_mark_inode_dirty_sync(inode, true);
  2030. }
  2031. static inline void f2fs_i_blocks_write(struct inode *inode,
  2032. block_t diff, bool add, bool claim)
  2033. {
  2034. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  2035. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  2036. /* add = 1, claim = 1 should be dquot_reserve_block in pair */
  2037. if (add) {
  2038. if (claim)
  2039. dquot_claim_block(inode, diff);
  2040. else
  2041. dquot_alloc_block_nofail(inode, diff);
  2042. } else {
  2043. dquot_free_block(inode, diff);
  2044. }
  2045. f2fs_mark_inode_dirty_sync(inode, true);
  2046. if (clean || recover)
  2047. set_inode_flag(inode, FI_AUTO_RECOVER);
  2048. }
  2049. static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
  2050. {
  2051. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  2052. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  2053. if (i_size_read(inode) == i_size)
  2054. return;
  2055. i_size_write(inode, i_size);
  2056. f2fs_mark_inode_dirty_sync(inode, true);
  2057. if (clean || recover)
  2058. set_inode_flag(inode, FI_AUTO_RECOVER);
  2059. }
  2060. static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
  2061. {
  2062. F2FS_I(inode)->i_current_depth = depth;
  2063. f2fs_mark_inode_dirty_sync(inode, true);
  2064. }
  2065. static inline void f2fs_i_gc_failures_write(struct inode *inode,
  2066. unsigned int count)
  2067. {
  2068. F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN] = count;
  2069. f2fs_mark_inode_dirty_sync(inode, true);
  2070. }
  2071. static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
  2072. {
  2073. F2FS_I(inode)->i_xattr_nid = xnid;
  2074. f2fs_mark_inode_dirty_sync(inode, true);
  2075. }
  2076. static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
  2077. {
  2078. F2FS_I(inode)->i_pino = pino;
  2079. f2fs_mark_inode_dirty_sync(inode, true);
  2080. }
  2081. static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
  2082. {
  2083. struct f2fs_inode_info *fi = F2FS_I(inode);
  2084. if (ri->i_inline & F2FS_INLINE_XATTR)
  2085. set_bit(FI_INLINE_XATTR, &fi->flags);
  2086. if (ri->i_inline & F2FS_INLINE_DATA)
  2087. set_bit(FI_INLINE_DATA, &fi->flags);
  2088. if (ri->i_inline & F2FS_INLINE_DENTRY)
  2089. set_bit(FI_INLINE_DENTRY, &fi->flags);
  2090. if (ri->i_inline & F2FS_DATA_EXIST)
  2091. set_bit(FI_DATA_EXIST, &fi->flags);
  2092. if (ri->i_inline & F2FS_INLINE_DOTS)
  2093. set_bit(FI_INLINE_DOTS, &fi->flags);
  2094. if (ri->i_inline & F2FS_EXTRA_ATTR)
  2095. set_bit(FI_EXTRA_ATTR, &fi->flags);
  2096. if (ri->i_inline & F2FS_PIN_FILE)
  2097. set_bit(FI_PIN_FILE, &fi->flags);
  2098. }
  2099. static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
  2100. {
  2101. ri->i_inline = 0;
  2102. if (is_inode_flag_set(inode, FI_INLINE_XATTR))
  2103. ri->i_inline |= F2FS_INLINE_XATTR;
  2104. if (is_inode_flag_set(inode, FI_INLINE_DATA))
  2105. ri->i_inline |= F2FS_INLINE_DATA;
  2106. if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
  2107. ri->i_inline |= F2FS_INLINE_DENTRY;
  2108. if (is_inode_flag_set(inode, FI_DATA_EXIST))
  2109. ri->i_inline |= F2FS_DATA_EXIST;
  2110. if (is_inode_flag_set(inode, FI_INLINE_DOTS))
  2111. ri->i_inline |= F2FS_INLINE_DOTS;
  2112. if (is_inode_flag_set(inode, FI_EXTRA_ATTR))
  2113. ri->i_inline |= F2FS_EXTRA_ATTR;
  2114. if (is_inode_flag_set(inode, FI_PIN_FILE))
  2115. ri->i_inline |= F2FS_PIN_FILE;
  2116. }
  2117. static inline int f2fs_has_extra_attr(struct inode *inode)
  2118. {
  2119. return is_inode_flag_set(inode, FI_EXTRA_ATTR);
  2120. }
  2121. static inline int f2fs_has_inline_xattr(struct inode *inode)
  2122. {
  2123. return is_inode_flag_set(inode, FI_INLINE_XATTR);
  2124. }
  2125. static inline unsigned int addrs_per_inode(struct inode *inode)
  2126. {
  2127. return CUR_ADDRS_PER_INODE(inode) - get_inline_xattr_addrs(inode);
  2128. }
  2129. static inline void *inline_xattr_addr(struct inode *inode, struct page *page)
  2130. {
  2131. struct f2fs_inode *ri = F2FS_INODE(page);
  2132. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  2133. get_inline_xattr_addrs(inode)]);
  2134. }
  2135. static inline int inline_xattr_size(struct inode *inode)
  2136. {
  2137. if (f2fs_has_inline_xattr(inode))
  2138. return get_inline_xattr_addrs(inode) * sizeof(__le32);
  2139. return 0;
  2140. }
  2141. static inline int f2fs_has_inline_data(struct inode *inode)
  2142. {
  2143. return is_inode_flag_set(inode, FI_INLINE_DATA);
  2144. }
  2145. static inline int f2fs_exist_data(struct inode *inode)
  2146. {
  2147. return is_inode_flag_set(inode, FI_DATA_EXIST);
  2148. }
  2149. static inline int f2fs_has_inline_dots(struct inode *inode)
  2150. {
  2151. return is_inode_flag_set(inode, FI_INLINE_DOTS);
  2152. }
  2153. static inline bool f2fs_is_pinned_file(struct inode *inode)
  2154. {
  2155. return is_inode_flag_set(inode, FI_PIN_FILE);
  2156. }
  2157. static inline bool f2fs_is_atomic_file(struct inode *inode)
  2158. {
  2159. return is_inode_flag_set(inode, FI_ATOMIC_FILE);
  2160. }
  2161. static inline bool f2fs_is_commit_atomic_write(struct inode *inode)
  2162. {
  2163. return is_inode_flag_set(inode, FI_ATOMIC_COMMIT);
  2164. }
  2165. static inline bool f2fs_is_volatile_file(struct inode *inode)
  2166. {
  2167. return is_inode_flag_set(inode, FI_VOLATILE_FILE);
  2168. }
  2169. static inline bool f2fs_is_first_block_written(struct inode *inode)
  2170. {
  2171. return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
  2172. }
  2173. static inline bool f2fs_is_drop_cache(struct inode *inode)
  2174. {
  2175. return is_inode_flag_set(inode, FI_DROP_CACHE);
  2176. }
  2177. static inline void *inline_data_addr(struct inode *inode, struct page *page)
  2178. {
  2179. struct f2fs_inode *ri = F2FS_INODE(page);
  2180. int extra_size = get_extra_isize(inode);
  2181. return (void *)&(ri->i_addr[extra_size + DEF_INLINE_RESERVED_SIZE]);
  2182. }
  2183. static inline int f2fs_has_inline_dentry(struct inode *inode)
  2184. {
  2185. return is_inode_flag_set(inode, FI_INLINE_DENTRY);
  2186. }
  2187. static inline int is_file(struct inode *inode, int type)
  2188. {
  2189. return F2FS_I(inode)->i_advise & type;
  2190. }
  2191. static inline void set_file(struct inode *inode, int type)
  2192. {
  2193. F2FS_I(inode)->i_advise |= type;
  2194. f2fs_mark_inode_dirty_sync(inode, true);
  2195. }
  2196. static inline void clear_file(struct inode *inode, int type)
  2197. {
  2198. F2FS_I(inode)->i_advise &= ~type;
  2199. f2fs_mark_inode_dirty_sync(inode, true);
  2200. }
  2201. static inline bool f2fs_is_time_consistent(struct inode *inode)
  2202. {
  2203. if (!timespec64_equal(F2FS_I(inode)->i_disk_time, &inode->i_atime))
  2204. return false;
  2205. if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 1, &inode->i_ctime))
  2206. return false;
  2207. if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 2, &inode->i_mtime))
  2208. return false;
  2209. if (!timespec64_equal(F2FS_I(inode)->i_disk_time + 3,
  2210. &F2FS_I(inode)->i_crtime))
  2211. return false;
  2212. return true;
  2213. }
  2214. static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
  2215. {
  2216. bool ret;
  2217. if (dsync) {
  2218. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2219. spin_lock(&sbi->inode_lock[DIRTY_META]);
  2220. ret = list_empty(&F2FS_I(inode)->gdirty_list);
  2221. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  2222. return ret;
  2223. }
  2224. if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
  2225. file_keep_isize(inode) ||
  2226. i_size_read(inode) & ~PAGE_MASK)
  2227. return false;
  2228. if (!f2fs_is_time_consistent(inode))
  2229. return false;
  2230. down_read(&F2FS_I(inode)->i_sem);
  2231. ret = F2FS_I(inode)->last_disk_size == i_size_read(inode);
  2232. up_read(&F2FS_I(inode)->i_sem);
  2233. return ret;
  2234. }
  2235. static inline bool f2fs_readonly(struct super_block *sb)
  2236. {
  2237. return sb_rdonly(sb);
  2238. }
  2239. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  2240. {
  2241. return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
  2242. }
  2243. static inline bool is_dot_dotdot(const struct qstr *str)
  2244. {
  2245. if (str->len == 1 && str->name[0] == '.')
  2246. return true;
  2247. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  2248. return true;
  2249. return false;
  2250. }
  2251. static inline bool f2fs_may_extent_tree(struct inode *inode)
  2252. {
  2253. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  2254. if (!test_opt(sbi, EXTENT_CACHE) ||
  2255. is_inode_flag_set(inode, FI_NO_EXTENT))
  2256. return false;
  2257. /*
  2258. * for recovered files during mount do not create extents
  2259. * if shrinker is not registered.
  2260. */
  2261. if (list_empty(&sbi->s_list))
  2262. return false;
  2263. return S_ISREG(inode->i_mode);
  2264. }
  2265. static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
  2266. size_t size, gfp_t flags)
  2267. {
  2268. if (time_to_inject(sbi, FAULT_KMALLOC)) {
  2269. f2fs_show_injection_info(FAULT_KMALLOC);
  2270. return NULL;
  2271. }
  2272. return kmalloc(size, flags);
  2273. }
  2274. static inline void *f2fs_kzalloc(struct f2fs_sb_info *sbi,
  2275. size_t size, gfp_t flags)
  2276. {
  2277. return f2fs_kmalloc(sbi, size, flags | __GFP_ZERO);
  2278. }
  2279. static inline void *f2fs_kvmalloc(struct f2fs_sb_info *sbi,
  2280. size_t size, gfp_t flags)
  2281. {
  2282. if (time_to_inject(sbi, FAULT_KVMALLOC)) {
  2283. f2fs_show_injection_info(FAULT_KVMALLOC);
  2284. return NULL;
  2285. }
  2286. return kvmalloc(size, flags);
  2287. }
  2288. static inline void *f2fs_kvzalloc(struct f2fs_sb_info *sbi,
  2289. size_t size, gfp_t flags)
  2290. {
  2291. return f2fs_kvmalloc(sbi, size, flags | __GFP_ZERO);
  2292. }
  2293. static inline int get_extra_isize(struct inode *inode)
  2294. {
  2295. return F2FS_I(inode)->i_extra_isize / sizeof(__le32);
  2296. }
  2297. static inline int get_inline_xattr_addrs(struct inode *inode)
  2298. {
  2299. return F2FS_I(inode)->i_inline_xattr_size;
  2300. }
  2301. #define f2fs_get_inode_mode(i) \
  2302. ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
  2303. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  2304. #define F2FS_TOTAL_EXTRA_ATTR_SIZE \
  2305. (offsetof(struct f2fs_inode, i_extra_end) - \
  2306. offsetof(struct f2fs_inode, i_extra_isize)) \
  2307. #define F2FS_OLD_ATTRIBUTE_SIZE (offsetof(struct f2fs_inode, i_addr))
  2308. #define F2FS_FITS_IN_INODE(f2fs_inode, extra_isize, field) \
  2309. ((offsetof(typeof(*f2fs_inode), field) + \
  2310. sizeof((f2fs_inode)->field)) \
  2311. <= (F2FS_OLD_ATTRIBUTE_SIZE + extra_isize)) \
  2312. static inline void f2fs_reset_iostat(struct f2fs_sb_info *sbi)
  2313. {
  2314. int i;
  2315. spin_lock(&sbi->iostat_lock);
  2316. for (i = 0; i < NR_IO_TYPE; i++)
  2317. sbi->write_iostat[i] = 0;
  2318. spin_unlock(&sbi->iostat_lock);
  2319. }
  2320. static inline void f2fs_update_iostat(struct f2fs_sb_info *sbi,
  2321. enum iostat_type type, unsigned long long io_bytes)
  2322. {
  2323. if (!sbi->iostat_enable)
  2324. return;
  2325. spin_lock(&sbi->iostat_lock);
  2326. sbi->write_iostat[type] += io_bytes;
  2327. if (type == APP_WRITE_IO || type == APP_DIRECT_IO)
  2328. sbi->write_iostat[APP_BUFFERED_IO] =
  2329. sbi->write_iostat[APP_WRITE_IO] -
  2330. sbi->write_iostat[APP_DIRECT_IO];
  2331. spin_unlock(&sbi->iostat_lock);
  2332. }
  2333. #define __is_meta_io(fio) (PAGE_TYPE_OF_BIO(fio->type) == META && \
  2334. (!is_read_io(fio->op) || fio->is_meta))
  2335. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  2336. block_t blkaddr, int type);
  2337. static inline void verify_blkaddr(struct f2fs_sb_info *sbi,
  2338. block_t blkaddr, int type)
  2339. {
  2340. if (!f2fs_is_valid_blkaddr(sbi, blkaddr, type)) {
  2341. f2fs_msg(sbi->sb, KERN_ERR,
  2342. "invalid blkaddr: %u, type: %d, run fsck to fix.",
  2343. blkaddr, type);
  2344. f2fs_bug_on(sbi, 1);
  2345. }
  2346. }
  2347. static inline bool __is_valid_data_blkaddr(block_t blkaddr)
  2348. {
  2349. if (blkaddr == NEW_ADDR || blkaddr == NULL_ADDR)
  2350. return false;
  2351. return true;
  2352. }
  2353. static inline bool is_valid_data_blkaddr(struct f2fs_sb_info *sbi,
  2354. block_t blkaddr)
  2355. {
  2356. if (!__is_valid_data_blkaddr(blkaddr))
  2357. return false;
  2358. verify_blkaddr(sbi, blkaddr, DATA_GENERIC);
  2359. return true;
  2360. }
  2361. /*
  2362. * file.c
  2363. */
  2364. int f2fs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
  2365. void f2fs_truncate_data_blocks(struct dnode_of_data *dn);
  2366. int f2fs_truncate_blocks(struct inode *inode, u64 from, bool lock);
  2367. int f2fs_truncate(struct inode *inode);
  2368. int f2fs_getattr(const struct path *path, struct kstat *stat,
  2369. u32 request_mask, unsigned int flags);
  2370. int f2fs_setattr(struct dentry *dentry, struct iattr *attr);
  2371. int f2fs_truncate_hole(struct inode *inode, pgoff_t pg_start, pgoff_t pg_end);
  2372. void f2fs_truncate_data_blocks_range(struct dnode_of_data *dn, int count);
  2373. int f2fs_precache_extents(struct inode *inode);
  2374. long f2fs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg);
  2375. long f2fs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  2376. int f2fs_pin_file_control(struct inode *inode, bool inc);
  2377. /*
  2378. * inode.c
  2379. */
  2380. void f2fs_set_inode_flags(struct inode *inode);
  2381. bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page);
  2382. void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page);
  2383. struct inode *f2fs_iget(struct super_block *sb, unsigned long ino);
  2384. struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino);
  2385. int f2fs_try_to_free_nats(struct f2fs_sb_info *sbi, int nr_shrink);
  2386. void f2fs_update_inode(struct inode *inode, struct page *node_page);
  2387. void f2fs_update_inode_page(struct inode *inode);
  2388. int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc);
  2389. void f2fs_evict_inode(struct inode *inode);
  2390. void f2fs_handle_failed_inode(struct inode *inode);
  2391. /*
  2392. * namei.c
  2393. */
  2394. int f2fs_update_extension_list(struct f2fs_sb_info *sbi, const char *name,
  2395. bool hot, bool set);
  2396. struct dentry *f2fs_get_parent(struct dentry *child);
  2397. /*
  2398. * dir.c
  2399. */
  2400. unsigned char f2fs_get_de_type(struct f2fs_dir_entry *de);
  2401. struct f2fs_dir_entry *f2fs_find_target_dentry(struct fscrypt_name *fname,
  2402. f2fs_hash_t namehash, int *max_slots,
  2403. struct f2fs_dentry_ptr *d);
  2404. int f2fs_fill_dentries(struct dir_context *ctx, struct f2fs_dentry_ptr *d,
  2405. unsigned int start_pos, struct fscrypt_str *fstr);
  2406. void f2fs_do_make_empty_dir(struct inode *inode, struct inode *parent,
  2407. struct f2fs_dentry_ptr *d);
  2408. struct page *f2fs_init_inode_metadata(struct inode *inode, struct inode *dir,
  2409. const struct qstr *new_name,
  2410. const struct qstr *orig_name, struct page *dpage);
  2411. void f2fs_update_parent_metadata(struct inode *dir, struct inode *inode,
  2412. unsigned int current_depth);
  2413. int f2fs_room_for_filename(const void *bitmap, int slots, int max_slots);
  2414. void f2fs_drop_nlink(struct inode *dir, struct inode *inode);
  2415. struct f2fs_dir_entry *__f2fs_find_entry(struct inode *dir,
  2416. struct fscrypt_name *fname, struct page **res_page);
  2417. struct f2fs_dir_entry *f2fs_find_entry(struct inode *dir,
  2418. const struct qstr *child, struct page **res_page);
  2419. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *dir, struct page **p);
  2420. ino_t f2fs_inode_by_name(struct inode *dir, const struct qstr *qstr,
  2421. struct page **page);
  2422. void f2fs_set_link(struct inode *dir, struct f2fs_dir_entry *de,
  2423. struct page *page, struct inode *inode);
  2424. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *d,
  2425. const struct qstr *name, f2fs_hash_t name_hash,
  2426. unsigned int bit_pos);
  2427. int f2fs_add_regular_entry(struct inode *dir, const struct qstr *new_name,
  2428. const struct qstr *orig_name,
  2429. struct inode *inode, nid_t ino, umode_t mode);
  2430. int f2fs_add_dentry(struct inode *dir, struct fscrypt_name *fname,
  2431. struct inode *inode, nid_t ino, umode_t mode);
  2432. int f2fs_do_add_link(struct inode *dir, const struct qstr *name,
  2433. struct inode *inode, nid_t ino, umode_t mode);
  2434. void f2fs_delete_entry(struct f2fs_dir_entry *dentry, struct page *page,
  2435. struct inode *dir, struct inode *inode);
  2436. int f2fs_do_tmpfile(struct inode *inode, struct inode *dir);
  2437. bool f2fs_empty_dir(struct inode *dir);
  2438. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  2439. {
  2440. return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  2441. inode, inode->i_ino, inode->i_mode);
  2442. }
  2443. /*
  2444. * super.c
  2445. */
  2446. int f2fs_inode_dirtied(struct inode *inode, bool sync);
  2447. void f2fs_inode_synced(struct inode *inode);
  2448. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
  2449. void f2fs_quota_off_umount(struct super_block *sb);
  2450. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
  2451. int f2fs_sync_fs(struct super_block *sb, int sync);
  2452. extern __printf(3, 4)
  2453. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  2454. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
  2455. /*
  2456. * hash.c
  2457. */
  2458. f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
  2459. struct fscrypt_name *fname);
  2460. /*
  2461. * node.c
  2462. */
  2463. struct dnode_of_data;
  2464. struct node_info;
  2465. int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
  2466. bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
  2467. bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
  2468. void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
  2469. void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
  2470. void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
  2471. int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
  2472. bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
  2473. bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
  2474. int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
  2475. struct node_info *ni);
  2476. pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
  2477. int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
  2478. int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
  2479. int f2fs_truncate_xattr_node(struct inode *inode);
  2480. int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
  2481. unsigned int seq_id);
  2482. int f2fs_remove_inode_page(struct inode *inode);
  2483. struct page *f2fs_new_inode_page(struct inode *inode);
  2484. struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
  2485. void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
  2486. struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
  2487. struct page *f2fs_get_node_page_ra(struct page *parent, int start);
  2488. void f2fs_move_node_page(struct page *node_page, int gc_type);
  2489. int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  2490. struct writeback_control *wbc, bool atomic,
  2491. unsigned int *seq_id);
  2492. int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
  2493. struct writeback_control *wbc,
  2494. bool do_balance, enum iostat_type io_type);
  2495. int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
  2496. bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
  2497. void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
  2498. void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
  2499. int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
  2500. void f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
  2501. int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
  2502. int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
  2503. int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
  2504. unsigned int segno, struct f2fs_summary_block *sum);
  2505. void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2506. int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
  2507. void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
  2508. int __init f2fs_create_node_manager_caches(void);
  2509. void f2fs_destroy_node_manager_caches(void);
  2510. /*
  2511. * segment.c
  2512. */
  2513. bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
  2514. void f2fs_register_inmem_page(struct inode *inode, struct page *page);
  2515. void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
  2516. void f2fs_drop_inmem_pages(struct inode *inode);
  2517. void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
  2518. int f2fs_commit_inmem_pages(struct inode *inode);
  2519. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
  2520. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
  2521. int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
  2522. int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
  2523. int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
  2524. void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
  2525. void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
  2526. bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
  2527. void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
  2528. void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
  2529. bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
  2530. void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
  2531. struct cp_control *cpc);
  2532. void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
  2533. int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
  2534. void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
  2535. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
  2536. bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
  2537. struct cp_control *cpc);
  2538. struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
  2539. void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
  2540. block_t blk_addr);
  2541. void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
  2542. enum iostat_type io_type);
  2543. void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
  2544. void f2fs_outplace_write_data(struct dnode_of_data *dn,
  2545. struct f2fs_io_info *fio);
  2546. int f2fs_inplace_write_data(struct f2fs_io_info *fio);
  2547. void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  2548. block_t old_blkaddr, block_t new_blkaddr,
  2549. bool recover_curseg, bool recover_newaddr);
  2550. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  2551. block_t old_addr, block_t new_addr,
  2552. unsigned char version, bool recover_curseg,
  2553. bool recover_newaddr);
  2554. void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  2555. block_t old_blkaddr, block_t *new_blkaddr,
  2556. struct f2fs_summary *sum, int type,
  2557. struct f2fs_io_info *fio, bool add_list);
  2558. void f2fs_wait_on_page_writeback(struct page *page,
  2559. enum page_type type, bool ordered);
  2560. void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
  2561. void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2562. void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2563. int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  2564. unsigned int val, int alloc);
  2565. void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2566. int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
  2567. void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
  2568. int __init f2fs_create_segment_manager_caches(void);
  2569. void f2fs_destroy_segment_manager_caches(void);
  2570. int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
  2571. enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
  2572. enum page_type type, enum temp_type temp);
  2573. /*
  2574. * checkpoint.c
  2575. */
  2576. void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
  2577. struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2578. struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2579. struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
  2580. struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2581. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  2582. block_t blkaddr, int type);
  2583. int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  2584. int type, bool sync);
  2585. void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
  2586. long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  2587. long nr_to_write, enum iostat_type io_type);
  2588. void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2589. void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2590. void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
  2591. bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
  2592. void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  2593. unsigned int devidx, int type);
  2594. bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  2595. unsigned int devidx, int type);
  2596. int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
  2597. int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
  2598. void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
  2599. void f2fs_add_orphan_inode(struct inode *inode);
  2600. void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
  2601. int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
  2602. int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
  2603. void f2fs_update_dirty_page(struct inode *inode, struct page *page);
  2604. void f2fs_remove_dirty_inode(struct inode *inode);
  2605. int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
  2606. void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
  2607. int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2608. void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
  2609. int __init f2fs_create_checkpoint_caches(void);
  2610. void f2fs_destroy_checkpoint_caches(void);
  2611. /*
  2612. * data.c
  2613. */
  2614. int f2fs_init_post_read_processing(void);
  2615. void f2fs_destroy_post_read_processing(void);
  2616. void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
  2617. void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
  2618. struct inode *inode, nid_t ino, pgoff_t idx,
  2619. enum page_type type);
  2620. void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
  2621. int f2fs_submit_page_bio(struct f2fs_io_info *fio);
  2622. void f2fs_submit_page_write(struct f2fs_io_info *fio);
  2623. struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
  2624. block_t blk_addr, struct bio *bio);
  2625. int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
  2626. void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
  2627. void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
  2628. int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
  2629. int f2fs_reserve_new_block(struct dnode_of_data *dn);
  2630. int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
  2631. int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
  2632. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
  2633. struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
  2634. int op_flags, bool for_write);
  2635. struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
  2636. struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
  2637. bool for_write);
  2638. struct page *f2fs_get_new_data_page(struct inode *inode,
  2639. struct page *ipage, pgoff_t index, bool new_i_size);
  2640. int f2fs_do_write_data_page(struct f2fs_io_info *fio);
  2641. int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
  2642. int create, int flag);
  2643. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2644. u64 start, u64 len);
  2645. bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
  2646. bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
  2647. void f2fs_invalidate_page(struct page *page, unsigned int offset,
  2648. unsigned int length);
  2649. int f2fs_release_page(struct page *page, gfp_t wait);
  2650. #ifdef CONFIG_MIGRATION
  2651. int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
  2652. struct page *page, enum migrate_mode mode);
  2653. #endif
  2654. bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
  2655. void f2fs_clear_radix_tree_dirty_tag(struct page *page);
  2656. /*
  2657. * gc.c
  2658. */
  2659. int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
  2660. void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
  2661. block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
  2662. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
  2663. unsigned int segno);
  2664. void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
  2665. /*
  2666. * recovery.c
  2667. */
  2668. int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
  2669. bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
  2670. /*
  2671. * debug.c
  2672. */
  2673. #ifdef CONFIG_F2FS_STAT_FS
  2674. struct f2fs_stat_info {
  2675. struct list_head stat_list;
  2676. struct f2fs_sb_info *sbi;
  2677. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  2678. int main_area_segs, main_area_sections, main_area_zones;
  2679. unsigned long long hit_largest, hit_cached, hit_rbtree;
  2680. unsigned long long hit_total, total_ext;
  2681. int ext_tree, zombie_tree, ext_node;
  2682. int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
  2683. int ndirty_data, ndirty_qdata;
  2684. int inmem_pages;
  2685. unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
  2686. int nats, dirty_nats, sits, dirty_sits;
  2687. int free_nids, avail_nids, alloc_nids;
  2688. int total_count, utilization;
  2689. int bg_gc, nr_wb_cp_data, nr_wb_data;
  2690. int nr_flushing, nr_flushed, flush_list_empty;
  2691. int nr_discarding, nr_discarded;
  2692. int nr_discard_cmd;
  2693. unsigned int undiscard_blks;
  2694. int inline_xattr, inline_inode, inline_dir, append, update, orphans;
  2695. int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
  2696. unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
  2697. unsigned int bimodal, avg_vblocks;
  2698. int util_free, util_valid, util_invalid;
  2699. int rsvd_segs, overp_segs;
  2700. int dirty_count, node_pages, meta_pages;
  2701. int prefree_count, call_count, cp_count, bg_cp_count;
  2702. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  2703. int bg_node_segs, bg_data_segs;
  2704. int tot_blks, data_blks, node_blks;
  2705. int bg_data_blks, bg_node_blks;
  2706. unsigned long long skipped_atomic_files[2];
  2707. int curseg[NR_CURSEG_TYPE];
  2708. int cursec[NR_CURSEG_TYPE];
  2709. int curzone[NR_CURSEG_TYPE];
  2710. unsigned int segment_count[2];
  2711. unsigned int block_count[2];
  2712. unsigned int inplace_count;
  2713. unsigned long long base_mem, cache_mem, page_mem;
  2714. };
  2715. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  2716. {
  2717. return (struct f2fs_stat_info *)sbi->stat_info;
  2718. }
  2719. #define stat_inc_cp_count(si) ((si)->cp_count++)
  2720. #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
  2721. #define stat_inc_call_count(si) ((si)->call_count++)
  2722. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  2723. #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
  2724. #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
  2725. #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
  2726. #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
  2727. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  2728. #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
  2729. #define stat_inc_inline_xattr(inode) \
  2730. do { \
  2731. if (f2fs_has_inline_xattr(inode)) \
  2732. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  2733. } while (0)
  2734. #define stat_dec_inline_xattr(inode) \
  2735. do { \
  2736. if (f2fs_has_inline_xattr(inode)) \
  2737. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  2738. } while (0)
  2739. #define stat_inc_inline_inode(inode) \
  2740. do { \
  2741. if (f2fs_has_inline_data(inode)) \
  2742. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  2743. } while (0)
  2744. #define stat_dec_inline_inode(inode) \
  2745. do { \
  2746. if (f2fs_has_inline_data(inode)) \
  2747. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  2748. } while (0)
  2749. #define stat_inc_inline_dir(inode) \
  2750. do { \
  2751. if (f2fs_has_inline_dentry(inode)) \
  2752. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  2753. } while (0)
  2754. #define stat_dec_inline_dir(inode) \
  2755. do { \
  2756. if (f2fs_has_inline_dentry(inode)) \
  2757. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  2758. } while (0)
  2759. #define stat_inc_seg_type(sbi, curseg) \
  2760. ((sbi)->segment_count[(curseg)->alloc_type]++)
  2761. #define stat_inc_block_count(sbi, curseg) \
  2762. ((sbi)->block_count[(curseg)->alloc_type]++)
  2763. #define stat_inc_inplace_blocks(sbi) \
  2764. (atomic_inc(&(sbi)->inplace_count))
  2765. #define stat_inc_atomic_write(inode) \
  2766. (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
  2767. #define stat_dec_atomic_write(inode) \
  2768. (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
  2769. #define stat_update_max_atomic_write(inode) \
  2770. do { \
  2771. int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
  2772. int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
  2773. if (cur > max) \
  2774. atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
  2775. } while (0)
  2776. #define stat_inc_volatile_write(inode) \
  2777. (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
  2778. #define stat_dec_volatile_write(inode) \
  2779. (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
  2780. #define stat_update_max_volatile_write(inode) \
  2781. do { \
  2782. int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
  2783. int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
  2784. if (cur > max) \
  2785. atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
  2786. } while (0)
  2787. #define stat_inc_seg_count(sbi, type, gc_type) \
  2788. do { \
  2789. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2790. si->tot_segs++; \
  2791. if ((type) == SUM_TYPE_DATA) { \
  2792. si->data_segs++; \
  2793. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  2794. } else { \
  2795. si->node_segs++; \
  2796. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  2797. } \
  2798. } while (0)
  2799. #define stat_inc_tot_blk_count(si, blks) \
  2800. ((si)->tot_blks += (blks))
  2801. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  2802. do { \
  2803. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2804. stat_inc_tot_blk_count(si, blks); \
  2805. si->data_blks += (blks); \
  2806. si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2807. } while (0)
  2808. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  2809. do { \
  2810. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2811. stat_inc_tot_blk_count(si, blks); \
  2812. si->node_blks += (blks); \
  2813. si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2814. } while (0)
  2815. int f2fs_build_stats(struct f2fs_sb_info *sbi);
  2816. void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
  2817. int __init f2fs_create_root_stats(void);
  2818. void f2fs_destroy_root_stats(void);
  2819. #else
  2820. #define stat_inc_cp_count(si) do { } while (0)
  2821. #define stat_inc_bg_cp_count(si) do { } while (0)
  2822. #define stat_inc_call_count(si) do { } while (0)
  2823. #define stat_inc_bggc_count(si) do { } while (0)
  2824. #define stat_inc_dirty_inode(sbi, type) do { } while (0)
  2825. #define stat_dec_dirty_inode(sbi, type) do { } while (0)
  2826. #define stat_inc_total_hit(sb) do { } while (0)
  2827. #define stat_inc_rbtree_node_hit(sb) do { } while (0)
  2828. #define stat_inc_largest_node_hit(sbi) do { } while (0)
  2829. #define stat_inc_cached_node_hit(sbi) do { } while (0)
  2830. #define stat_inc_inline_xattr(inode) do { } while (0)
  2831. #define stat_dec_inline_xattr(inode) do { } while (0)
  2832. #define stat_inc_inline_inode(inode) do { } while (0)
  2833. #define stat_dec_inline_inode(inode) do { } while (0)
  2834. #define stat_inc_inline_dir(inode) do { } while (0)
  2835. #define stat_dec_inline_dir(inode) do { } while (0)
  2836. #define stat_inc_atomic_write(inode) do { } while (0)
  2837. #define stat_dec_atomic_write(inode) do { } while (0)
  2838. #define stat_update_max_atomic_write(inode) do { } while (0)
  2839. #define stat_inc_volatile_write(inode) do { } while (0)
  2840. #define stat_dec_volatile_write(inode) do { } while (0)
  2841. #define stat_update_max_volatile_write(inode) do { } while (0)
  2842. #define stat_inc_seg_type(sbi, curseg) do { } while (0)
  2843. #define stat_inc_block_count(sbi, curseg) do { } while (0)
  2844. #define stat_inc_inplace_blocks(sbi) do { } while (0)
  2845. #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
  2846. #define stat_inc_tot_blk_count(si, blks) do { } while (0)
  2847. #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
  2848. #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
  2849. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  2850. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  2851. static inline int __init f2fs_create_root_stats(void) { return 0; }
  2852. static inline void f2fs_destroy_root_stats(void) { }
  2853. #endif
  2854. extern const struct file_operations f2fs_dir_operations;
  2855. extern const struct file_operations f2fs_file_operations;
  2856. extern const struct inode_operations f2fs_file_inode_operations;
  2857. extern const struct address_space_operations f2fs_dblock_aops;
  2858. extern const struct address_space_operations f2fs_node_aops;
  2859. extern const struct address_space_operations f2fs_meta_aops;
  2860. extern const struct inode_operations f2fs_dir_inode_operations;
  2861. extern const struct inode_operations f2fs_symlink_inode_operations;
  2862. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  2863. extern const struct inode_operations f2fs_special_inode_operations;
  2864. extern struct kmem_cache *f2fs_inode_entry_slab;
  2865. /*
  2866. * inline.c
  2867. */
  2868. bool f2fs_may_inline_data(struct inode *inode);
  2869. bool f2fs_may_inline_dentry(struct inode *inode);
  2870. void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
  2871. void f2fs_truncate_inline_inode(struct inode *inode,
  2872. struct page *ipage, u64 from);
  2873. int f2fs_read_inline_data(struct inode *inode, struct page *page);
  2874. int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
  2875. int f2fs_convert_inline_inode(struct inode *inode);
  2876. int f2fs_write_inline_data(struct inode *inode, struct page *page);
  2877. bool f2fs_recover_inline_data(struct inode *inode, struct page *npage);
  2878. struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
  2879. struct fscrypt_name *fname, struct page **res_page);
  2880. int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
  2881. struct page *ipage);
  2882. int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
  2883. const struct qstr *orig_name,
  2884. struct inode *inode, nid_t ino, umode_t mode);
  2885. void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
  2886. struct page *page, struct inode *dir,
  2887. struct inode *inode);
  2888. bool f2fs_empty_inline_dir(struct inode *dir);
  2889. int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
  2890. struct fscrypt_str *fstr);
  2891. int f2fs_inline_data_fiemap(struct inode *inode,
  2892. struct fiemap_extent_info *fieinfo,
  2893. __u64 start, __u64 len);
  2894. /*
  2895. * shrinker.c
  2896. */
  2897. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  2898. struct shrink_control *sc);
  2899. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  2900. struct shrink_control *sc);
  2901. void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
  2902. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
  2903. /*
  2904. * extent_cache.c
  2905. */
  2906. struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
  2907. struct rb_entry *cached_re, unsigned int ofs);
  2908. struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
  2909. struct rb_root *root, struct rb_node **parent,
  2910. unsigned int ofs);
  2911. struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
  2912. struct rb_entry *cached_re, unsigned int ofs,
  2913. struct rb_entry **prev_entry, struct rb_entry **next_entry,
  2914. struct rb_node ***insert_p, struct rb_node **insert_parent,
  2915. bool force);
  2916. bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
  2917. struct rb_root *root);
  2918. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
  2919. bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
  2920. void f2fs_drop_extent_tree(struct inode *inode);
  2921. unsigned int f2fs_destroy_extent_node(struct inode *inode);
  2922. void f2fs_destroy_extent_tree(struct inode *inode);
  2923. bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
  2924. struct extent_info *ei);
  2925. void f2fs_update_extent_cache(struct dnode_of_data *dn);
  2926. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  2927. pgoff_t fofs, block_t blkaddr, unsigned int len);
  2928. void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
  2929. int __init f2fs_create_extent_cache(void);
  2930. void f2fs_destroy_extent_cache(void);
  2931. /*
  2932. * sysfs.c
  2933. */
  2934. int __init f2fs_init_sysfs(void);
  2935. void f2fs_exit_sysfs(void);
  2936. int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
  2937. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
  2938. /*
  2939. * crypto support
  2940. */
  2941. static inline bool f2fs_encrypted_inode(struct inode *inode)
  2942. {
  2943. return file_is_encrypt(inode);
  2944. }
  2945. static inline bool f2fs_encrypted_file(struct inode *inode)
  2946. {
  2947. return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
  2948. }
  2949. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  2950. {
  2951. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2952. file_set_encrypt(inode);
  2953. f2fs_set_inode_flags(inode);
  2954. #endif
  2955. }
  2956. /*
  2957. * Returns true if the reads of the inode's data need to undergo some
  2958. * postprocessing step, like decryption or authenticity verification.
  2959. */
  2960. static inline bool f2fs_post_read_required(struct inode *inode)
  2961. {
  2962. return f2fs_encrypted_file(inode);
  2963. }
  2964. #define F2FS_FEATURE_FUNCS(name, flagname) \
  2965. static inline int f2fs_sb_has_##name(struct super_block *sb) \
  2966. { \
  2967. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
  2968. }
  2969. F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
  2970. F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
  2971. F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
  2972. F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
  2973. F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
  2974. F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
  2975. F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
  2976. F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
  2977. F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
  2978. #ifdef CONFIG_BLK_DEV_ZONED
  2979. static inline int get_blkz_type(struct f2fs_sb_info *sbi,
  2980. struct block_device *bdev, block_t blkaddr)
  2981. {
  2982. unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
  2983. int i;
  2984. for (i = 0; i < sbi->s_ndevs; i++)
  2985. if (FDEV(i).bdev == bdev)
  2986. return FDEV(i).blkz_type[zno];
  2987. return -EINVAL;
  2988. }
  2989. #endif
  2990. static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
  2991. {
  2992. return f2fs_sb_has_blkzoned(sbi->sb);
  2993. }
  2994. static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
  2995. {
  2996. return blk_queue_discard(bdev_get_queue(sbi->sb->s_bdev));
  2997. }
  2998. static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
  2999. {
  3000. return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
  3001. f2fs_hw_should_discard(sbi);
  3002. }
  3003. static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
  3004. {
  3005. clear_opt(sbi, ADAPTIVE);
  3006. clear_opt(sbi, LFS);
  3007. switch (mt) {
  3008. case F2FS_MOUNT_ADAPTIVE:
  3009. set_opt(sbi, ADAPTIVE);
  3010. break;
  3011. case F2FS_MOUNT_LFS:
  3012. set_opt(sbi, LFS);
  3013. break;
  3014. }
  3015. }
  3016. static inline bool f2fs_may_encrypt(struct inode *inode)
  3017. {
  3018. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  3019. umode_t mode = inode->i_mode;
  3020. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  3021. #else
  3022. return false;
  3023. #endif
  3024. }
  3025. static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
  3026. {
  3027. return (f2fs_post_read_required(inode) ||
  3028. (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
  3029. f2fs_is_multi_device(F2FS_I_SB(inode)));
  3030. }
  3031. #ifdef CONFIG_F2FS_FAULT_INJECTION
  3032. extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
  3033. unsigned int type);
  3034. #else
  3035. #define f2fs_build_fault_attr(sbi, rate, type) do { } while (0)
  3036. #endif
  3037. #endif
  3038. #define EFSBADCRC EBADMSG /* Bad CRC detected */
  3039. #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */