f2fs.h 110 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504
  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. if (fscrypt_is_nokey_name(dentry))
  2441. return -ENOKEY;
  2442. return f2fs_do_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  2443. inode, inode->i_ino, inode->i_mode);
  2444. }
  2445. /*
  2446. * super.c
  2447. */
  2448. int f2fs_inode_dirtied(struct inode *inode, bool sync);
  2449. void f2fs_inode_synced(struct inode *inode);
  2450. int f2fs_enable_quota_files(struct f2fs_sb_info *sbi, bool rdonly);
  2451. void f2fs_quota_off_umount(struct super_block *sb);
  2452. int f2fs_commit_super(struct f2fs_sb_info *sbi, bool recover);
  2453. int f2fs_sync_fs(struct super_block *sb, int sync);
  2454. extern __printf(3, 4)
  2455. void f2fs_msg(struct super_block *sb, const char *level, const char *fmt, ...);
  2456. int f2fs_sanity_check_ckpt(struct f2fs_sb_info *sbi);
  2457. /*
  2458. * hash.c
  2459. */
  2460. f2fs_hash_t f2fs_dentry_hash(const struct qstr *name_info,
  2461. struct fscrypt_name *fname);
  2462. /*
  2463. * node.c
  2464. */
  2465. struct dnode_of_data;
  2466. struct node_info;
  2467. int f2fs_check_nid_range(struct f2fs_sb_info *sbi, nid_t nid);
  2468. bool f2fs_available_free_memory(struct f2fs_sb_info *sbi, int type);
  2469. bool f2fs_in_warm_node_list(struct f2fs_sb_info *sbi, struct page *page);
  2470. void f2fs_init_fsync_node_info(struct f2fs_sb_info *sbi);
  2471. void f2fs_del_fsync_node_entry(struct f2fs_sb_info *sbi, struct page *page);
  2472. void f2fs_reset_fsync_node_info(struct f2fs_sb_info *sbi);
  2473. int f2fs_need_dentry_mark(struct f2fs_sb_info *sbi, nid_t nid);
  2474. bool f2fs_is_checkpointed_node(struct f2fs_sb_info *sbi, nid_t nid);
  2475. bool f2fs_need_inode_block_update(struct f2fs_sb_info *sbi, nid_t ino);
  2476. int f2fs_get_node_info(struct f2fs_sb_info *sbi, nid_t nid,
  2477. struct node_info *ni);
  2478. pgoff_t f2fs_get_next_page_offset(struct dnode_of_data *dn, pgoff_t pgofs);
  2479. int f2fs_get_dnode_of_data(struct dnode_of_data *dn, pgoff_t index, int mode);
  2480. int f2fs_truncate_inode_blocks(struct inode *inode, pgoff_t from);
  2481. int f2fs_truncate_xattr_node(struct inode *inode);
  2482. int f2fs_wait_on_node_pages_writeback(struct f2fs_sb_info *sbi,
  2483. unsigned int seq_id);
  2484. int f2fs_remove_inode_page(struct inode *inode);
  2485. struct page *f2fs_new_inode_page(struct inode *inode);
  2486. struct page *f2fs_new_node_page(struct dnode_of_data *dn, unsigned int ofs);
  2487. void f2fs_ra_node_page(struct f2fs_sb_info *sbi, nid_t nid);
  2488. struct page *f2fs_get_node_page(struct f2fs_sb_info *sbi, pgoff_t nid);
  2489. struct page *f2fs_get_node_page_ra(struct page *parent, int start);
  2490. void f2fs_move_node_page(struct page *node_page, int gc_type);
  2491. int f2fs_fsync_node_pages(struct f2fs_sb_info *sbi, struct inode *inode,
  2492. struct writeback_control *wbc, bool atomic,
  2493. unsigned int *seq_id);
  2494. int f2fs_sync_node_pages(struct f2fs_sb_info *sbi,
  2495. struct writeback_control *wbc,
  2496. bool do_balance, enum iostat_type io_type);
  2497. int f2fs_build_free_nids(struct f2fs_sb_info *sbi, bool sync, bool mount);
  2498. bool f2fs_alloc_nid(struct f2fs_sb_info *sbi, nid_t *nid);
  2499. void f2fs_alloc_nid_done(struct f2fs_sb_info *sbi, nid_t nid);
  2500. void f2fs_alloc_nid_failed(struct f2fs_sb_info *sbi, nid_t nid);
  2501. int f2fs_try_to_free_nids(struct f2fs_sb_info *sbi, int nr_shrink);
  2502. int f2fs_recover_inline_xattr(struct inode *inode, struct page *page);
  2503. int f2fs_recover_xattr_data(struct inode *inode, struct page *page);
  2504. int f2fs_recover_inode_page(struct f2fs_sb_info *sbi, struct page *page);
  2505. int f2fs_restore_node_summary(struct f2fs_sb_info *sbi,
  2506. unsigned int segno, struct f2fs_summary_block *sum);
  2507. void f2fs_flush_nat_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2508. int f2fs_build_node_manager(struct f2fs_sb_info *sbi);
  2509. void f2fs_destroy_node_manager(struct f2fs_sb_info *sbi);
  2510. int __init f2fs_create_node_manager_caches(void);
  2511. void f2fs_destroy_node_manager_caches(void);
  2512. /*
  2513. * segment.c
  2514. */
  2515. bool f2fs_need_SSR(struct f2fs_sb_info *sbi);
  2516. void f2fs_register_inmem_page(struct inode *inode, struct page *page);
  2517. void f2fs_drop_inmem_pages_all(struct f2fs_sb_info *sbi, bool gc_failure);
  2518. void f2fs_drop_inmem_pages(struct inode *inode);
  2519. void f2fs_drop_inmem_page(struct inode *inode, struct page *page);
  2520. int f2fs_commit_inmem_pages(struct inode *inode);
  2521. void f2fs_balance_fs(struct f2fs_sb_info *sbi, bool need);
  2522. void f2fs_balance_fs_bg(struct f2fs_sb_info *sbi);
  2523. int f2fs_issue_flush(struct f2fs_sb_info *sbi, nid_t ino);
  2524. int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
  2525. int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
  2526. void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
  2527. void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr);
  2528. bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
  2529. void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
  2530. void f2fs_stop_discard_thread(struct f2fs_sb_info *sbi);
  2531. bool f2fs_wait_discard_bios(struct f2fs_sb_info *sbi);
  2532. void f2fs_clear_prefree_segments(struct f2fs_sb_info *sbi,
  2533. struct cp_control *cpc);
  2534. void f2fs_release_discard_addrs(struct f2fs_sb_info *sbi);
  2535. int f2fs_npages_for_summary_flush(struct f2fs_sb_info *sbi, bool for_ra);
  2536. void f2fs_allocate_new_segments(struct f2fs_sb_info *sbi);
  2537. int f2fs_trim_fs(struct f2fs_sb_info *sbi, struct fstrim_range *range);
  2538. bool f2fs_exist_trim_candidates(struct f2fs_sb_info *sbi,
  2539. struct cp_control *cpc);
  2540. struct page *f2fs_get_sum_page(struct f2fs_sb_info *sbi, unsigned int segno);
  2541. void f2fs_update_meta_page(struct f2fs_sb_info *sbi, void *src,
  2542. block_t blk_addr);
  2543. void f2fs_do_write_meta_page(struct f2fs_sb_info *sbi, struct page *page,
  2544. enum iostat_type io_type);
  2545. void f2fs_do_write_node_page(unsigned int nid, struct f2fs_io_info *fio);
  2546. void f2fs_outplace_write_data(struct dnode_of_data *dn,
  2547. struct f2fs_io_info *fio);
  2548. int f2fs_inplace_write_data(struct f2fs_io_info *fio);
  2549. void f2fs_do_replace_block(struct f2fs_sb_info *sbi, struct f2fs_summary *sum,
  2550. block_t old_blkaddr, block_t new_blkaddr,
  2551. bool recover_curseg, bool recover_newaddr);
  2552. void f2fs_replace_block(struct f2fs_sb_info *sbi, struct dnode_of_data *dn,
  2553. block_t old_addr, block_t new_addr,
  2554. unsigned char version, bool recover_curseg,
  2555. bool recover_newaddr);
  2556. void f2fs_allocate_data_block(struct f2fs_sb_info *sbi, struct page *page,
  2557. block_t old_blkaddr, block_t *new_blkaddr,
  2558. struct f2fs_summary *sum, int type,
  2559. struct f2fs_io_info *fio, bool add_list);
  2560. void f2fs_wait_on_page_writeback(struct page *page,
  2561. enum page_type type, bool ordered);
  2562. void f2fs_wait_on_block_writeback(struct inode *inode, block_t blkaddr);
  2563. void f2fs_write_data_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2564. void f2fs_write_node_summaries(struct f2fs_sb_info *sbi, block_t start_blk);
  2565. int f2fs_lookup_journal_in_cursum(struct f2fs_journal *journal, int type,
  2566. unsigned int val, int alloc);
  2567. void f2fs_flush_sit_entries(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2568. int f2fs_build_segment_manager(struct f2fs_sb_info *sbi);
  2569. void f2fs_destroy_segment_manager(struct f2fs_sb_info *sbi);
  2570. int __init f2fs_create_segment_manager_caches(void);
  2571. void f2fs_destroy_segment_manager_caches(void);
  2572. int f2fs_rw_hint_to_seg_type(enum rw_hint hint);
  2573. enum rw_hint f2fs_io_type_to_rw_hint(struct f2fs_sb_info *sbi,
  2574. enum page_type type, enum temp_type temp);
  2575. /*
  2576. * checkpoint.c
  2577. */
  2578. void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi, bool end_io);
  2579. struct page *f2fs_grab_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2580. struct page *f2fs_get_meta_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2581. struct page *f2fs_get_meta_page_nofail(struct f2fs_sb_info *sbi, pgoff_t index);
  2582. struct page *f2fs_get_tmp_page(struct f2fs_sb_info *sbi, pgoff_t index);
  2583. bool f2fs_is_valid_blkaddr(struct f2fs_sb_info *sbi,
  2584. block_t blkaddr, int type);
  2585. int f2fs_ra_meta_pages(struct f2fs_sb_info *sbi, block_t start, int nrpages,
  2586. int type, bool sync);
  2587. void f2fs_ra_meta_pages_cond(struct f2fs_sb_info *sbi, pgoff_t index);
  2588. long f2fs_sync_meta_pages(struct f2fs_sb_info *sbi, enum page_type type,
  2589. long nr_to_write, enum iostat_type io_type);
  2590. void f2fs_add_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2591. void f2fs_remove_ino_entry(struct f2fs_sb_info *sbi, nid_t ino, int type);
  2592. void f2fs_release_ino_entry(struct f2fs_sb_info *sbi, bool all);
  2593. bool f2fs_exist_written_data(struct f2fs_sb_info *sbi, nid_t ino, int mode);
  2594. void f2fs_set_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  2595. unsigned int devidx, int type);
  2596. bool f2fs_is_dirty_device(struct f2fs_sb_info *sbi, nid_t ino,
  2597. unsigned int devidx, int type);
  2598. int f2fs_sync_inode_meta(struct f2fs_sb_info *sbi);
  2599. int f2fs_acquire_orphan_inode(struct f2fs_sb_info *sbi);
  2600. void f2fs_release_orphan_inode(struct f2fs_sb_info *sbi);
  2601. void f2fs_add_orphan_inode(struct inode *inode);
  2602. void f2fs_remove_orphan_inode(struct f2fs_sb_info *sbi, nid_t ino);
  2603. int f2fs_recover_orphan_inodes(struct f2fs_sb_info *sbi);
  2604. int f2fs_get_valid_checkpoint(struct f2fs_sb_info *sbi);
  2605. void f2fs_update_dirty_page(struct inode *inode, struct page *page);
  2606. void f2fs_remove_dirty_inode(struct inode *inode);
  2607. int f2fs_sync_dirty_inodes(struct f2fs_sb_info *sbi, enum inode_type type);
  2608. void f2fs_wait_on_all_pages_writeback(struct f2fs_sb_info *sbi);
  2609. int f2fs_write_checkpoint(struct f2fs_sb_info *sbi, struct cp_control *cpc);
  2610. void f2fs_init_ino_entry_info(struct f2fs_sb_info *sbi);
  2611. int __init f2fs_create_checkpoint_caches(void);
  2612. void f2fs_destroy_checkpoint_caches(void);
  2613. /*
  2614. * data.c
  2615. */
  2616. int f2fs_init_post_read_processing(void);
  2617. void f2fs_destroy_post_read_processing(void);
  2618. void f2fs_submit_merged_write(struct f2fs_sb_info *sbi, enum page_type type);
  2619. void f2fs_submit_merged_write_cond(struct f2fs_sb_info *sbi,
  2620. struct inode *inode, nid_t ino, pgoff_t idx,
  2621. enum page_type type);
  2622. void f2fs_flush_merged_writes(struct f2fs_sb_info *sbi);
  2623. int f2fs_submit_page_bio(struct f2fs_io_info *fio);
  2624. void f2fs_submit_page_write(struct f2fs_io_info *fio);
  2625. struct block_device *f2fs_target_device(struct f2fs_sb_info *sbi,
  2626. block_t blk_addr, struct bio *bio);
  2627. int f2fs_target_device_index(struct f2fs_sb_info *sbi, block_t blkaddr);
  2628. void f2fs_set_data_blkaddr(struct dnode_of_data *dn);
  2629. void f2fs_update_data_blkaddr(struct dnode_of_data *dn, block_t blkaddr);
  2630. int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count);
  2631. int f2fs_reserve_new_block(struct dnode_of_data *dn);
  2632. int f2fs_get_block(struct dnode_of_data *dn, pgoff_t index);
  2633. int f2fs_preallocate_blocks(struct kiocb *iocb, struct iov_iter *from);
  2634. int f2fs_reserve_block(struct dnode_of_data *dn, pgoff_t index);
  2635. struct page *f2fs_get_read_data_page(struct inode *inode, pgoff_t index,
  2636. int op_flags, bool for_write);
  2637. struct page *f2fs_find_data_page(struct inode *inode, pgoff_t index);
  2638. struct page *f2fs_get_lock_data_page(struct inode *inode, pgoff_t index,
  2639. bool for_write);
  2640. struct page *f2fs_get_new_data_page(struct inode *inode,
  2641. struct page *ipage, pgoff_t index, bool new_i_size);
  2642. int f2fs_do_write_data_page(struct f2fs_io_info *fio);
  2643. int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map,
  2644. int create, int flag);
  2645. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  2646. u64 start, u64 len);
  2647. bool f2fs_should_update_inplace(struct inode *inode, struct f2fs_io_info *fio);
  2648. bool f2fs_should_update_outplace(struct inode *inode, struct f2fs_io_info *fio);
  2649. void f2fs_invalidate_page(struct page *page, unsigned int offset,
  2650. unsigned int length);
  2651. int f2fs_release_page(struct page *page, gfp_t wait);
  2652. #ifdef CONFIG_MIGRATION
  2653. int f2fs_migrate_page(struct address_space *mapping, struct page *newpage,
  2654. struct page *page, enum migrate_mode mode);
  2655. #endif
  2656. bool f2fs_overwrite_io(struct inode *inode, loff_t pos, size_t len);
  2657. void f2fs_clear_radix_tree_dirty_tag(struct page *page);
  2658. /*
  2659. * gc.c
  2660. */
  2661. int f2fs_start_gc_thread(struct f2fs_sb_info *sbi);
  2662. void f2fs_stop_gc_thread(struct f2fs_sb_info *sbi);
  2663. block_t f2fs_start_bidx_of_node(unsigned int node_ofs, struct inode *inode);
  2664. int f2fs_gc(struct f2fs_sb_info *sbi, bool sync, bool background,
  2665. unsigned int segno);
  2666. void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
  2667. /*
  2668. * recovery.c
  2669. */
  2670. int f2fs_recover_fsync_data(struct f2fs_sb_info *sbi, bool check_only);
  2671. bool f2fs_space_for_roll_forward(struct f2fs_sb_info *sbi);
  2672. /*
  2673. * debug.c
  2674. */
  2675. #ifdef CONFIG_F2FS_STAT_FS
  2676. struct f2fs_stat_info {
  2677. struct list_head stat_list;
  2678. struct f2fs_sb_info *sbi;
  2679. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  2680. int main_area_segs, main_area_sections, main_area_zones;
  2681. unsigned long long hit_largest, hit_cached, hit_rbtree;
  2682. unsigned long long hit_total, total_ext;
  2683. int ext_tree, zombie_tree, ext_node;
  2684. int ndirty_node, ndirty_dent, ndirty_meta, ndirty_imeta;
  2685. int ndirty_data, ndirty_qdata;
  2686. int inmem_pages;
  2687. unsigned int ndirty_dirs, ndirty_files, nquota_files, ndirty_all;
  2688. int nats, dirty_nats, sits, dirty_sits;
  2689. int free_nids, avail_nids, alloc_nids;
  2690. int total_count, utilization;
  2691. int bg_gc, nr_wb_cp_data, nr_wb_data;
  2692. int nr_flushing, nr_flushed, flush_list_empty;
  2693. int nr_discarding, nr_discarded;
  2694. int nr_discard_cmd;
  2695. unsigned int undiscard_blks;
  2696. int inline_xattr, inline_inode, inline_dir, append, update, orphans;
  2697. int aw_cnt, max_aw_cnt, vw_cnt, max_vw_cnt;
  2698. unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
  2699. unsigned int bimodal, avg_vblocks;
  2700. int util_free, util_valid, util_invalid;
  2701. int rsvd_segs, overp_segs;
  2702. int dirty_count, node_pages, meta_pages;
  2703. int prefree_count, call_count, cp_count, bg_cp_count;
  2704. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  2705. int bg_node_segs, bg_data_segs;
  2706. int tot_blks, data_blks, node_blks;
  2707. int bg_data_blks, bg_node_blks;
  2708. unsigned long long skipped_atomic_files[2];
  2709. int curseg[NR_CURSEG_TYPE];
  2710. int cursec[NR_CURSEG_TYPE];
  2711. int curzone[NR_CURSEG_TYPE];
  2712. unsigned int segment_count[2];
  2713. unsigned int block_count[2];
  2714. unsigned int inplace_count;
  2715. unsigned long long base_mem, cache_mem, page_mem;
  2716. };
  2717. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  2718. {
  2719. return (struct f2fs_stat_info *)sbi->stat_info;
  2720. }
  2721. #define stat_inc_cp_count(si) ((si)->cp_count++)
  2722. #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
  2723. #define stat_inc_call_count(si) ((si)->call_count++)
  2724. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  2725. #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
  2726. #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
  2727. #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
  2728. #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
  2729. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  2730. #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
  2731. #define stat_inc_inline_xattr(inode) \
  2732. do { \
  2733. if (f2fs_has_inline_xattr(inode)) \
  2734. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  2735. } while (0)
  2736. #define stat_dec_inline_xattr(inode) \
  2737. do { \
  2738. if (f2fs_has_inline_xattr(inode)) \
  2739. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  2740. } while (0)
  2741. #define stat_inc_inline_inode(inode) \
  2742. do { \
  2743. if (f2fs_has_inline_data(inode)) \
  2744. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  2745. } while (0)
  2746. #define stat_dec_inline_inode(inode) \
  2747. do { \
  2748. if (f2fs_has_inline_data(inode)) \
  2749. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  2750. } while (0)
  2751. #define stat_inc_inline_dir(inode) \
  2752. do { \
  2753. if (f2fs_has_inline_dentry(inode)) \
  2754. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  2755. } while (0)
  2756. #define stat_dec_inline_dir(inode) \
  2757. do { \
  2758. if (f2fs_has_inline_dentry(inode)) \
  2759. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  2760. } while (0)
  2761. #define stat_inc_seg_type(sbi, curseg) \
  2762. ((sbi)->segment_count[(curseg)->alloc_type]++)
  2763. #define stat_inc_block_count(sbi, curseg) \
  2764. ((sbi)->block_count[(curseg)->alloc_type]++)
  2765. #define stat_inc_inplace_blocks(sbi) \
  2766. (atomic_inc(&(sbi)->inplace_count))
  2767. #define stat_inc_atomic_write(inode) \
  2768. (atomic_inc(&F2FS_I_SB(inode)->aw_cnt))
  2769. #define stat_dec_atomic_write(inode) \
  2770. (atomic_dec(&F2FS_I_SB(inode)->aw_cnt))
  2771. #define stat_update_max_atomic_write(inode) \
  2772. do { \
  2773. int cur = atomic_read(&F2FS_I_SB(inode)->aw_cnt); \
  2774. int max = atomic_read(&F2FS_I_SB(inode)->max_aw_cnt); \
  2775. if (cur > max) \
  2776. atomic_set(&F2FS_I_SB(inode)->max_aw_cnt, cur); \
  2777. } while (0)
  2778. #define stat_inc_volatile_write(inode) \
  2779. (atomic_inc(&F2FS_I_SB(inode)->vw_cnt))
  2780. #define stat_dec_volatile_write(inode) \
  2781. (atomic_dec(&F2FS_I_SB(inode)->vw_cnt))
  2782. #define stat_update_max_volatile_write(inode) \
  2783. do { \
  2784. int cur = atomic_read(&F2FS_I_SB(inode)->vw_cnt); \
  2785. int max = atomic_read(&F2FS_I_SB(inode)->max_vw_cnt); \
  2786. if (cur > max) \
  2787. atomic_set(&F2FS_I_SB(inode)->max_vw_cnt, cur); \
  2788. } while (0)
  2789. #define stat_inc_seg_count(sbi, type, gc_type) \
  2790. do { \
  2791. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2792. si->tot_segs++; \
  2793. if ((type) == SUM_TYPE_DATA) { \
  2794. si->data_segs++; \
  2795. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  2796. } else { \
  2797. si->node_segs++; \
  2798. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  2799. } \
  2800. } while (0)
  2801. #define stat_inc_tot_blk_count(si, blks) \
  2802. ((si)->tot_blks += (blks))
  2803. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  2804. do { \
  2805. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2806. stat_inc_tot_blk_count(si, blks); \
  2807. si->data_blks += (blks); \
  2808. si->bg_data_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2809. } while (0)
  2810. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  2811. do { \
  2812. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2813. stat_inc_tot_blk_count(si, blks); \
  2814. si->node_blks += (blks); \
  2815. si->bg_node_blks += ((gc_type) == BG_GC) ? (blks) : 0; \
  2816. } while (0)
  2817. int f2fs_build_stats(struct f2fs_sb_info *sbi);
  2818. void f2fs_destroy_stats(struct f2fs_sb_info *sbi);
  2819. int __init f2fs_create_root_stats(void);
  2820. void f2fs_destroy_root_stats(void);
  2821. #else
  2822. #define stat_inc_cp_count(si) do { } while (0)
  2823. #define stat_inc_bg_cp_count(si) do { } while (0)
  2824. #define stat_inc_call_count(si) do { } while (0)
  2825. #define stat_inc_bggc_count(si) do { } while (0)
  2826. #define stat_inc_dirty_inode(sbi, type) do { } while (0)
  2827. #define stat_dec_dirty_inode(sbi, type) do { } while (0)
  2828. #define stat_inc_total_hit(sb) do { } while (0)
  2829. #define stat_inc_rbtree_node_hit(sb) do { } while (0)
  2830. #define stat_inc_largest_node_hit(sbi) do { } while (0)
  2831. #define stat_inc_cached_node_hit(sbi) do { } while (0)
  2832. #define stat_inc_inline_xattr(inode) do { } while (0)
  2833. #define stat_dec_inline_xattr(inode) do { } while (0)
  2834. #define stat_inc_inline_inode(inode) do { } while (0)
  2835. #define stat_dec_inline_inode(inode) do { } while (0)
  2836. #define stat_inc_inline_dir(inode) do { } while (0)
  2837. #define stat_dec_inline_dir(inode) do { } while (0)
  2838. #define stat_inc_atomic_write(inode) do { } while (0)
  2839. #define stat_dec_atomic_write(inode) do { } while (0)
  2840. #define stat_update_max_atomic_write(inode) do { } while (0)
  2841. #define stat_inc_volatile_write(inode) do { } while (0)
  2842. #define stat_dec_volatile_write(inode) do { } while (0)
  2843. #define stat_update_max_volatile_write(inode) do { } while (0)
  2844. #define stat_inc_seg_type(sbi, curseg) do { } while (0)
  2845. #define stat_inc_block_count(sbi, curseg) do { } while (0)
  2846. #define stat_inc_inplace_blocks(sbi) do { } while (0)
  2847. #define stat_inc_seg_count(sbi, type, gc_type) do { } while (0)
  2848. #define stat_inc_tot_blk_count(si, blks) do { } while (0)
  2849. #define stat_inc_data_blk_count(sbi, blks, gc_type) do { } while (0)
  2850. #define stat_inc_node_blk_count(sbi, blks, gc_type) do { } while (0)
  2851. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  2852. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  2853. static inline int __init f2fs_create_root_stats(void) { return 0; }
  2854. static inline void f2fs_destroy_root_stats(void) { }
  2855. #endif
  2856. extern const struct file_operations f2fs_dir_operations;
  2857. extern const struct file_operations f2fs_file_operations;
  2858. extern const struct inode_operations f2fs_file_inode_operations;
  2859. extern const struct address_space_operations f2fs_dblock_aops;
  2860. extern const struct address_space_operations f2fs_node_aops;
  2861. extern const struct address_space_operations f2fs_meta_aops;
  2862. extern const struct inode_operations f2fs_dir_inode_operations;
  2863. extern const struct inode_operations f2fs_symlink_inode_operations;
  2864. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  2865. extern const struct inode_operations f2fs_special_inode_operations;
  2866. extern struct kmem_cache *f2fs_inode_entry_slab;
  2867. /*
  2868. * inline.c
  2869. */
  2870. bool f2fs_may_inline_data(struct inode *inode);
  2871. bool f2fs_may_inline_dentry(struct inode *inode);
  2872. void f2fs_do_read_inline_data(struct page *page, struct page *ipage);
  2873. void f2fs_truncate_inline_inode(struct inode *inode,
  2874. struct page *ipage, u64 from);
  2875. int f2fs_read_inline_data(struct inode *inode, struct page *page);
  2876. int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page);
  2877. int f2fs_convert_inline_inode(struct inode *inode);
  2878. int f2fs_write_inline_data(struct inode *inode, struct page *page);
  2879. int f2fs_recover_inline_data(struct inode *inode, struct page *npage);
  2880. struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
  2881. struct fscrypt_name *fname, struct page **res_page);
  2882. int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
  2883. struct page *ipage);
  2884. int f2fs_add_inline_entry(struct inode *dir, const struct qstr *new_name,
  2885. const struct qstr *orig_name,
  2886. struct inode *inode, nid_t ino, umode_t mode);
  2887. void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry,
  2888. struct page *page, struct inode *dir,
  2889. struct inode *inode);
  2890. bool f2fs_empty_inline_dir(struct inode *dir);
  2891. int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
  2892. struct fscrypt_str *fstr);
  2893. int f2fs_inline_data_fiemap(struct inode *inode,
  2894. struct fiemap_extent_info *fieinfo,
  2895. __u64 start, __u64 len);
  2896. /*
  2897. * shrinker.c
  2898. */
  2899. unsigned long f2fs_shrink_count(struct shrinker *shrink,
  2900. struct shrink_control *sc);
  2901. unsigned long f2fs_shrink_scan(struct shrinker *shrink,
  2902. struct shrink_control *sc);
  2903. void f2fs_join_shrinker(struct f2fs_sb_info *sbi);
  2904. void f2fs_leave_shrinker(struct f2fs_sb_info *sbi);
  2905. /*
  2906. * extent_cache.c
  2907. */
  2908. struct rb_entry *f2fs_lookup_rb_tree(struct rb_root *root,
  2909. struct rb_entry *cached_re, unsigned int ofs);
  2910. struct rb_node **f2fs_lookup_rb_tree_for_insert(struct f2fs_sb_info *sbi,
  2911. struct rb_root *root, struct rb_node **parent,
  2912. unsigned int ofs);
  2913. struct rb_entry *f2fs_lookup_rb_tree_ret(struct rb_root *root,
  2914. struct rb_entry *cached_re, unsigned int ofs,
  2915. struct rb_entry **prev_entry, struct rb_entry **next_entry,
  2916. struct rb_node ***insert_p, struct rb_node **insert_parent,
  2917. bool force);
  2918. bool f2fs_check_rb_tree_consistence(struct f2fs_sb_info *sbi,
  2919. struct rb_root *root);
  2920. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *sbi, int nr_shrink);
  2921. bool f2fs_init_extent_tree(struct inode *inode, struct f2fs_extent *i_ext);
  2922. void f2fs_drop_extent_tree(struct inode *inode);
  2923. unsigned int f2fs_destroy_extent_node(struct inode *inode);
  2924. void f2fs_destroy_extent_tree(struct inode *inode);
  2925. bool f2fs_lookup_extent_cache(struct inode *inode, pgoff_t pgofs,
  2926. struct extent_info *ei);
  2927. void f2fs_update_extent_cache(struct dnode_of_data *dn);
  2928. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  2929. pgoff_t fofs, block_t blkaddr, unsigned int len);
  2930. void f2fs_init_extent_cache_info(struct f2fs_sb_info *sbi);
  2931. int __init f2fs_create_extent_cache(void);
  2932. void f2fs_destroy_extent_cache(void);
  2933. /*
  2934. * sysfs.c
  2935. */
  2936. int __init f2fs_init_sysfs(void);
  2937. void f2fs_exit_sysfs(void);
  2938. int f2fs_register_sysfs(struct f2fs_sb_info *sbi);
  2939. void f2fs_unregister_sysfs(struct f2fs_sb_info *sbi);
  2940. /*
  2941. * crypto support
  2942. */
  2943. static inline bool f2fs_encrypted_inode(struct inode *inode)
  2944. {
  2945. return file_is_encrypt(inode);
  2946. }
  2947. static inline bool f2fs_encrypted_file(struct inode *inode)
  2948. {
  2949. return f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode);
  2950. }
  2951. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  2952. {
  2953. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2954. file_set_encrypt(inode);
  2955. f2fs_set_inode_flags(inode);
  2956. #endif
  2957. }
  2958. /*
  2959. * Returns true if the reads of the inode's data need to undergo some
  2960. * postprocessing step, like decryption or authenticity verification.
  2961. */
  2962. static inline bool f2fs_post_read_required(struct inode *inode)
  2963. {
  2964. return f2fs_encrypted_file(inode);
  2965. }
  2966. #define F2FS_FEATURE_FUNCS(name, flagname) \
  2967. static inline int f2fs_sb_has_##name(struct super_block *sb) \
  2968. { \
  2969. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_##flagname); \
  2970. }
  2971. F2FS_FEATURE_FUNCS(encrypt, ENCRYPT);
  2972. F2FS_FEATURE_FUNCS(blkzoned, BLKZONED);
  2973. F2FS_FEATURE_FUNCS(extra_attr, EXTRA_ATTR);
  2974. F2FS_FEATURE_FUNCS(project_quota, PRJQUOTA);
  2975. F2FS_FEATURE_FUNCS(inode_chksum, INODE_CHKSUM);
  2976. F2FS_FEATURE_FUNCS(flexible_inline_xattr, FLEXIBLE_INLINE_XATTR);
  2977. F2FS_FEATURE_FUNCS(quota_ino, QUOTA_INO);
  2978. F2FS_FEATURE_FUNCS(inode_crtime, INODE_CRTIME);
  2979. F2FS_FEATURE_FUNCS(lost_found, LOST_FOUND);
  2980. #ifdef CONFIG_BLK_DEV_ZONED
  2981. static inline int get_blkz_type(struct f2fs_sb_info *sbi,
  2982. struct block_device *bdev, block_t blkaddr)
  2983. {
  2984. unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
  2985. int i;
  2986. for (i = 0; i < sbi->s_ndevs; i++)
  2987. if (FDEV(i).bdev == bdev)
  2988. return FDEV(i).blkz_type[zno];
  2989. return -EINVAL;
  2990. }
  2991. #endif
  2992. static inline bool f2fs_hw_should_discard(struct f2fs_sb_info *sbi)
  2993. {
  2994. return f2fs_sb_has_blkzoned(sbi->sb);
  2995. }
  2996. static inline bool f2fs_hw_support_discard(struct f2fs_sb_info *sbi)
  2997. {
  2998. return blk_queue_discard(bdev_get_queue(sbi->sb->s_bdev));
  2999. }
  3000. static inline bool f2fs_realtime_discard_enable(struct f2fs_sb_info *sbi)
  3001. {
  3002. return (test_opt(sbi, DISCARD) && f2fs_hw_support_discard(sbi)) ||
  3003. f2fs_hw_should_discard(sbi);
  3004. }
  3005. static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
  3006. {
  3007. clear_opt(sbi, ADAPTIVE);
  3008. clear_opt(sbi, LFS);
  3009. switch (mt) {
  3010. case F2FS_MOUNT_ADAPTIVE:
  3011. set_opt(sbi, ADAPTIVE);
  3012. break;
  3013. case F2FS_MOUNT_LFS:
  3014. set_opt(sbi, LFS);
  3015. break;
  3016. }
  3017. }
  3018. static inline bool f2fs_may_encrypt(struct inode *inode)
  3019. {
  3020. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  3021. umode_t mode = inode->i_mode;
  3022. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  3023. #else
  3024. return false;
  3025. #endif
  3026. }
  3027. static inline bool f2fs_force_buffered_io(struct inode *inode, int rw)
  3028. {
  3029. return (f2fs_post_read_required(inode) ||
  3030. (rw == WRITE && test_opt(F2FS_I_SB(inode), LFS)) ||
  3031. f2fs_is_multi_device(F2FS_I_SB(inode)));
  3032. }
  3033. #ifdef CONFIG_F2FS_FAULT_INJECTION
  3034. extern void f2fs_build_fault_attr(struct f2fs_sb_info *sbi, unsigned int rate,
  3035. unsigned int type);
  3036. #else
  3037. #define f2fs_build_fault_attr(sbi, rate, type) do { } while (0)
  3038. #endif
  3039. #endif
  3040. #define EFSBADCRC EBADMSG /* Bad CRC detected */
  3041. #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */