ctree.h 117 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #ifndef BTRFS_CTREE_H
  6. #define BTRFS_CTREE_H
  7. #include <linux/mm.h>
  8. #include <linux/sched/signal.h>
  9. #include <linux/highmem.h>
  10. #include <linux/fs.h>
  11. #include <linux/rwsem.h>
  12. #include <linux/semaphore.h>
  13. #include <linux/completion.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/wait.h>
  16. #include <linux/slab.h>
  17. #include <trace/events/btrfs.h>
  18. #include <asm/kmap_types.h>
  19. #include <asm/unaligned.h>
  20. #include <linux/pagemap.h>
  21. #include <linux/btrfs.h>
  22. #include <linux/btrfs_tree.h>
  23. #include <linux/workqueue.h>
  24. #include <linux/security.h>
  25. #include <linux/sizes.h>
  26. #include <linux/dynamic_debug.h>
  27. #include <linux/refcount.h>
  28. #include <linux/crc32c.h>
  29. #include "extent_io.h"
  30. #include "extent_map.h"
  31. #include "async-thread.h"
  32. #include "block-rsv.h"
  33. struct btrfs_trans_handle;
  34. struct btrfs_transaction;
  35. struct btrfs_pending_snapshot;
  36. struct btrfs_delayed_ref_root;
  37. struct btrfs_space_info;
  38. struct btrfs_block_group_cache;
  39. extern struct kmem_cache *btrfs_trans_handle_cachep;
  40. extern struct kmem_cache *btrfs_bit_radix_cachep;
  41. extern struct kmem_cache *btrfs_path_cachep;
  42. extern struct kmem_cache *btrfs_free_space_cachep;
  43. extern struct kmem_cache *btrfs_free_space_bitmap_cachep;
  44. struct btrfs_ordered_sum;
  45. struct btrfs_ref;
  46. #define BTRFS_MAGIC 0x4D5F53665248425FULL /* ascii _BHRfS_M, no null */
  47. /*
  48. * Maximum number of mirrors that can be available for all profiles counting
  49. * the target device of dev-replace as one. During an active device replace
  50. * procedure, the target device of the copy operation is a mirror for the
  51. * filesystem data as well that can be used to read data in order to repair
  52. * read errors on other disks.
  53. *
  54. * Current value is derived from RAID1 with 2 copies.
  55. */
  56. #define BTRFS_MAX_MIRRORS (2 + 1)
  57. #define BTRFS_MAX_LEVEL 8
  58. #define BTRFS_OLDEST_GENERATION 0ULL
  59. /*
  60. * the max metadata block size. This limit is somewhat artificial,
  61. * but the memmove costs go through the roof for larger blocks.
  62. */
  63. #define BTRFS_MAX_METADATA_BLOCKSIZE 65536
  64. /*
  65. * we can actually store much bigger names, but lets not confuse the rest
  66. * of linux
  67. */
  68. #define BTRFS_NAME_LEN 255
  69. /*
  70. * Theoretical limit is larger, but we keep this down to a sane
  71. * value. That should limit greatly the possibility of collisions on
  72. * inode ref items.
  73. */
  74. #define BTRFS_LINK_MAX 65535U
  75. #define BTRFS_EMPTY_DIR_SIZE 0
  76. /* ioprio of readahead is set to idle */
  77. #define BTRFS_IOPRIO_READA (IOPRIO_PRIO_VALUE(IOPRIO_CLASS_IDLE, 0))
  78. #define BTRFS_DIRTY_METADATA_THRESH SZ_32M
  79. /*
  80. * Use large batch size to reduce overhead of metadata updates. On the reader
  81. * side, we only read it when we are close to ENOSPC and the read overhead is
  82. * mostly related to the number of CPUs, so it is OK to use arbitrary large
  83. * value here.
  84. */
  85. #define BTRFS_TOTAL_BYTES_PINNED_BATCH SZ_128M
  86. #define BTRFS_MAX_EXTENT_SIZE SZ_128M
  87. /*
  88. * Count how many BTRFS_MAX_EXTENT_SIZE cover the @size
  89. */
  90. static inline u32 count_max_extents(u64 size)
  91. {
  92. return div_u64(size + BTRFS_MAX_EXTENT_SIZE - 1, BTRFS_MAX_EXTENT_SIZE);
  93. }
  94. static inline unsigned long btrfs_chunk_item_size(int num_stripes)
  95. {
  96. BUG_ON(num_stripes == 0);
  97. return sizeof(struct btrfs_chunk) +
  98. sizeof(struct btrfs_stripe) * (num_stripes - 1);
  99. }
  100. /*
  101. * Runtime (in-memory) states of filesystem
  102. */
  103. enum {
  104. /* Global indicator of serious filesystem errors */
  105. BTRFS_FS_STATE_ERROR,
  106. /*
  107. * Filesystem is being remounted, allow to skip some operations, like
  108. * defrag
  109. */
  110. BTRFS_FS_STATE_REMOUNTING,
  111. /* Track if a transaction abort has been reported on this filesystem */
  112. BTRFS_FS_STATE_TRANS_ABORTED,
  113. /*
  114. * Bio operations should be blocked on this filesystem because a source
  115. * or target device is being destroyed as part of a device replace
  116. */
  117. BTRFS_FS_STATE_DEV_REPLACING,
  118. /* The btrfs_fs_info created for self-tests */
  119. BTRFS_FS_STATE_DUMMY_FS_INFO,
  120. };
  121. #define BTRFS_BACKREF_REV_MAX 256
  122. #define BTRFS_BACKREF_REV_SHIFT 56
  123. #define BTRFS_BACKREF_REV_MASK (((u64)BTRFS_BACKREF_REV_MAX - 1) << \
  124. BTRFS_BACKREF_REV_SHIFT)
  125. #define BTRFS_OLD_BACKREF_REV 0
  126. #define BTRFS_MIXED_BACKREF_REV 1
  127. /*
  128. * every tree block (leaf or node) starts with this header.
  129. */
  130. struct btrfs_header {
  131. /* these first four must match the super block */
  132. u8 csum[BTRFS_CSUM_SIZE];
  133. u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
  134. __le64 bytenr; /* which block this node is supposed to live in */
  135. __le64 flags;
  136. /* allowed to be different from the super from here on down */
  137. u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
  138. __le64 generation;
  139. __le64 owner;
  140. __le32 nritems;
  141. u8 level;
  142. } __attribute__ ((__packed__));
  143. /*
  144. * this is a very generous portion of the super block, giving us
  145. * room to translate 14 chunks with 3 stripes each.
  146. */
  147. #define BTRFS_SYSTEM_CHUNK_ARRAY_SIZE 2048
  148. /*
  149. * just in case we somehow lose the roots and are not able to mount,
  150. * we store an array of the roots from previous transactions
  151. * in the super.
  152. */
  153. #define BTRFS_NUM_BACKUP_ROOTS 4
  154. struct btrfs_root_backup {
  155. __le64 tree_root;
  156. __le64 tree_root_gen;
  157. __le64 chunk_root;
  158. __le64 chunk_root_gen;
  159. __le64 extent_root;
  160. __le64 extent_root_gen;
  161. __le64 fs_root;
  162. __le64 fs_root_gen;
  163. __le64 dev_root;
  164. __le64 dev_root_gen;
  165. __le64 csum_root;
  166. __le64 csum_root_gen;
  167. __le64 total_bytes;
  168. __le64 bytes_used;
  169. __le64 num_devices;
  170. /* future */
  171. __le64 unused_64[4];
  172. u8 tree_root_level;
  173. u8 chunk_root_level;
  174. u8 extent_root_level;
  175. u8 fs_root_level;
  176. u8 dev_root_level;
  177. u8 csum_root_level;
  178. /* future and to align */
  179. u8 unused_8[10];
  180. } __attribute__ ((__packed__));
  181. /*
  182. * the super block basically lists the main trees of the FS
  183. * it currently lacks any block count etc etc
  184. */
  185. struct btrfs_super_block {
  186. /* the first 4 fields must match struct btrfs_header */
  187. u8 csum[BTRFS_CSUM_SIZE];
  188. /* FS specific UUID, visible to user */
  189. u8 fsid[BTRFS_FSID_SIZE];
  190. __le64 bytenr; /* this block number */
  191. __le64 flags;
  192. /* allowed to be different from the btrfs_header from here own down */
  193. __le64 magic;
  194. __le64 generation;
  195. __le64 root;
  196. __le64 chunk_root;
  197. __le64 log_root;
  198. /* this will help find the new super based on the log root */
  199. __le64 log_root_transid;
  200. __le64 total_bytes;
  201. __le64 bytes_used;
  202. __le64 root_dir_objectid;
  203. __le64 num_devices;
  204. __le32 sectorsize;
  205. __le32 nodesize;
  206. __le32 __unused_leafsize;
  207. __le32 stripesize;
  208. __le32 sys_chunk_array_size;
  209. __le64 chunk_root_generation;
  210. __le64 compat_flags;
  211. __le64 compat_ro_flags;
  212. __le64 incompat_flags;
  213. __le16 csum_type;
  214. u8 root_level;
  215. u8 chunk_root_level;
  216. u8 log_root_level;
  217. struct btrfs_dev_item dev_item;
  218. char label[BTRFS_LABEL_SIZE];
  219. __le64 cache_generation;
  220. __le64 uuid_tree_generation;
  221. /* the UUID written into btree blocks */
  222. u8 metadata_uuid[BTRFS_FSID_SIZE];
  223. /* future expansion */
  224. __le64 reserved[28];
  225. u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
  226. struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
  227. } __attribute__ ((__packed__));
  228. /*
  229. * Compat flags that we support. If any incompat flags are set other than the
  230. * ones specified below then we will fail to mount
  231. */
  232. #define BTRFS_FEATURE_COMPAT_SUPP 0ULL
  233. #define BTRFS_FEATURE_COMPAT_SAFE_SET 0ULL
  234. #define BTRFS_FEATURE_COMPAT_SAFE_CLEAR 0ULL
  235. #define BTRFS_FEATURE_COMPAT_RO_SUPP \
  236. (BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE | \
  237. BTRFS_FEATURE_COMPAT_RO_FREE_SPACE_TREE_VALID)
  238. #define BTRFS_FEATURE_COMPAT_RO_SAFE_SET 0ULL
  239. #define BTRFS_FEATURE_COMPAT_RO_SAFE_CLEAR 0ULL
  240. #define BTRFS_FEATURE_INCOMPAT_SUPP \
  241. (BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF | \
  242. BTRFS_FEATURE_INCOMPAT_DEFAULT_SUBVOL | \
  243. BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS | \
  244. BTRFS_FEATURE_INCOMPAT_BIG_METADATA | \
  245. BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO | \
  246. BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD | \
  247. BTRFS_FEATURE_INCOMPAT_RAID56 | \
  248. BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
  249. BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
  250. BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
  251. BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
  252. #define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
  253. (BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
  254. #define BTRFS_FEATURE_INCOMPAT_SAFE_CLEAR 0ULL
  255. /*
  256. * A leaf is full of items. offset and size tell us where to find
  257. * the item in the leaf (relative to the start of the data area)
  258. */
  259. struct btrfs_item {
  260. struct btrfs_disk_key key;
  261. __le32 offset;
  262. __le32 size;
  263. } __attribute__ ((__packed__));
  264. /*
  265. * leaves have an item area and a data area:
  266. * [item0, item1....itemN] [free space] [dataN...data1, data0]
  267. *
  268. * The data is separate from the items to get the keys closer together
  269. * during searches.
  270. */
  271. struct btrfs_leaf {
  272. struct btrfs_header header;
  273. struct btrfs_item items[];
  274. } __attribute__ ((__packed__));
  275. /*
  276. * all non-leaf blocks are nodes, they hold only keys and pointers to
  277. * other blocks
  278. */
  279. struct btrfs_key_ptr {
  280. struct btrfs_disk_key key;
  281. __le64 blockptr;
  282. __le64 generation;
  283. } __attribute__ ((__packed__));
  284. struct btrfs_node {
  285. struct btrfs_header header;
  286. struct btrfs_key_ptr ptrs[];
  287. } __attribute__ ((__packed__));
  288. /*
  289. * btrfs_paths remember the path taken from the root down to the leaf.
  290. * level 0 is always the leaf, and nodes[1...BTRFS_MAX_LEVEL] will point
  291. * to any other levels that are present.
  292. *
  293. * The slots array records the index of the item or block pointer
  294. * used while walking the tree.
  295. */
  296. enum { READA_NONE, READA_BACK, READA_FORWARD };
  297. struct btrfs_path {
  298. struct extent_buffer *nodes[BTRFS_MAX_LEVEL];
  299. int slots[BTRFS_MAX_LEVEL];
  300. /* if there is real range locking, this locks field will change */
  301. u8 locks[BTRFS_MAX_LEVEL];
  302. u8 reada;
  303. /* keep some upper locks as we walk down */
  304. u8 lowest_level;
  305. /*
  306. * set by btrfs_split_item, tells search_slot to keep all locks
  307. * and to force calls to keep space in the nodes
  308. */
  309. unsigned int search_for_split:1;
  310. unsigned int keep_locks:1;
  311. unsigned int skip_locking:1;
  312. unsigned int leave_spinning:1;
  313. unsigned int search_commit_root:1;
  314. unsigned int need_commit_sem:1;
  315. unsigned int skip_release_on_error:1;
  316. };
  317. #define BTRFS_MAX_EXTENT_ITEM_SIZE(r) ((BTRFS_LEAF_DATA_SIZE(r->fs_info) >> 4) - \
  318. sizeof(struct btrfs_item))
  319. struct btrfs_dev_replace {
  320. u64 replace_state; /* see #define above */
  321. time64_t time_started; /* seconds since 1-Jan-1970 */
  322. time64_t time_stopped; /* seconds since 1-Jan-1970 */
  323. atomic64_t num_write_errors;
  324. atomic64_t num_uncorrectable_read_errors;
  325. u64 cursor_left;
  326. u64 committed_cursor_left;
  327. u64 cursor_left_last_write_of_item;
  328. u64 cursor_right;
  329. u64 cont_reading_from_srcdev_mode; /* see #define above */
  330. int is_valid;
  331. int item_needs_writeback;
  332. struct btrfs_device *srcdev;
  333. struct btrfs_device *tgtdev;
  334. struct mutex lock_finishing_cancel_unmount;
  335. struct rw_semaphore rwsem;
  336. struct btrfs_scrub_progress scrub_progress;
  337. struct percpu_counter bio_counter;
  338. wait_queue_head_t replace_wait;
  339. };
  340. /*
  341. * free clusters are used to claim free space in relatively large chunks,
  342. * allowing us to do less seeky writes. They are used for all metadata
  343. * allocations. In ssd_spread mode they are also used for data allocations.
  344. */
  345. struct btrfs_free_cluster {
  346. spinlock_t lock;
  347. spinlock_t refill_lock;
  348. struct rb_root root;
  349. /* largest extent in this cluster */
  350. u64 max_size;
  351. /* first extent starting offset */
  352. u64 window_start;
  353. /* We did a full search and couldn't create a cluster */
  354. bool fragmented;
  355. struct btrfs_block_group_cache *block_group;
  356. /*
  357. * when a cluster is allocated from a block group, we put the
  358. * cluster onto a list in the block group so that it can
  359. * be freed before the block group is freed.
  360. */
  361. struct list_head block_group_list;
  362. };
  363. enum btrfs_caching_type {
  364. BTRFS_CACHE_NO,
  365. BTRFS_CACHE_STARTED,
  366. BTRFS_CACHE_FAST,
  367. BTRFS_CACHE_FINISHED,
  368. BTRFS_CACHE_ERROR,
  369. };
  370. /*
  371. * Tree to record all locked full stripes of a RAID5/6 block group
  372. */
  373. struct btrfs_full_stripe_locks_tree {
  374. struct rb_root root;
  375. struct mutex lock;
  376. };
  377. /* delayed seq elem */
  378. struct seq_list {
  379. struct list_head list;
  380. u64 seq;
  381. };
  382. #define SEQ_LIST_INIT(name) { .list = LIST_HEAD_INIT((name).list), .seq = 0 }
  383. #define SEQ_LAST ((u64)-1)
  384. enum btrfs_orphan_cleanup_state {
  385. ORPHAN_CLEANUP_STARTED = 1,
  386. ORPHAN_CLEANUP_DONE = 2,
  387. };
  388. void btrfs_init_async_reclaim_work(struct work_struct *work);
  389. /* fs_info */
  390. struct reloc_control;
  391. struct btrfs_device;
  392. struct btrfs_fs_devices;
  393. struct btrfs_balance_control;
  394. struct btrfs_delayed_root;
  395. /*
  396. * Block group or device which contains an active swapfile. Used for preventing
  397. * unsafe operations while a swapfile is active.
  398. *
  399. * These are sorted on (ptr, inode) (note that a block group or device can
  400. * contain more than one swapfile). We compare the pointer values because we
  401. * don't actually care what the object is, we just need a quick check whether
  402. * the object exists in the rbtree.
  403. */
  404. struct btrfs_swapfile_pin {
  405. struct rb_node node;
  406. void *ptr;
  407. struct inode *inode;
  408. /*
  409. * If true, ptr points to a struct btrfs_block_group_cache. Otherwise,
  410. * ptr points to a struct btrfs_device.
  411. */
  412. bool is_block_group;
  413. };
  414. bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
  415. enum {
  416. BTRFS_FS_BARRIER,
  417. BTRFS_FS_CLOSING_START,
  418. BTRFS_FS_CLOSING_DONE,
  419. BTRFS_FS_LOG_RECOVERING,
  420. BTRFS_FS_OPEN,
  421. BTRFS_FS_QUOTA_ENABLED,
  422. BTRFS_FS_UPDATE_UUID_TREE_GEN,
  423. BTRFS_FS_CREATING_FREE_SPACE_TREE,
  424. BTRFS_FS_BTREE_ERR,
  425. BTRFS_FS_LOG1_ERR,
  426. BTRFS_FS_LOG2_ERR,
  427. BTRFS_FS_QUOTA_OVERRIDE,
  428. /* Used to record internally whether fs has been frozen */
  429. BTRFS_FS_FROZEN,
  430. /*
  431. * Indicate that a whole-filesystem exclusive operation is running
  432. * (device replace, resize, device add/delete, balance)
  433. */
  434. BTRFS_FS_EXCL_OP,
  435. /*
  436. * Indicate that balance has been set up from the ioctl and is in the
  437. * main phase. The fs_info::balance_ctl is initialized.
  438. * Set and cleared while holding fs_info::balance_mutex.
  439. */
  440. BTRFS_FS_BALANCE_RUNNING,
  441. /* Indicate that the cleaner thread is awake and doing something. */
  442. BTRFS_FS_CLEANER_RUNNING,
  443. /*
  444. * The checksumming has an optimized version and is considered fast,
  445. * so we don't need to offload checksums to workqueues.
  446. */
  447. BTRFS_FS_CSUM_IMPL_FAST,
  448. /* Indicate that we can't trust the free space tree for caching yet */
  449. BTRFS_FS_FREE_SPACE_TREE_UNTRUSTED,
  450. };
  451. struct btrfs_fs_info {
  452. u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
  453. unsigned long flags;
  454. struct btrfs_root *extent_root;
  455. struct btrfs_root *tree_root;
  456. struct btrfs_root *chunk_root;
  457. struct btrfs_root *dev_root;
  458. struct btrfs_root *fs_root;
  459. struct btrfs_root *csum_root;
  460. struct btrfs_root *quota_root;
  461. struct btrfs_root *uuid_root;
  462. struct btrfs_root *free_space_root;
  463. /* the log root tree is a directory of all the other log roots */
  464. struct btrfs_root *log_root_tree;
  465. spinlock_t fs_roots_radix_lock;
  466. struct radix_tree_root fs_roots_radix;
  467. /* block group cache stuff */
  468. spinlock_t block_group_cache_lock;
  469. u64 first_logical_byte;
  470. struct rb_root block_group_cache_tree;
  471. /* keep track of unallocated space */
  472. atomic64_t free_chunk_space;
  473. struct extent_io_tree freed_extents[2];
  474. struct extent_io_tree *pinned_extents;
  475. /* logical->physical extent mapping */
  476. struct extent_map_tree mapping_tree;
  477. /*
  478. * block reservation for extent, checksum, root tree and
  479. * delayed dir index item
  480. */
  481. struct btrfs_block_rsv global_block_rsv;
  482. /* block reservation for metadata operations */
  483. struct btrfs_block_rsv trans_block_rsv;
  484. /* block reservation for chunk tree */
  485. struct btrfs_block_rsv chunk_block_rsv;
  486. /* block reservation for delayed operations */
  487. struct btrfs_block_rsv delayed_block_rsv;
  488. /* block reservation for delayed refs */
  489. struct btrfs_block_rsv delayed_refs_rsv;
  490. struct btrfs_block_rsv empty_block_rsv;
  491. u64 generation;
  492. u64 last_trans_committed;
  493. u64 avg_delayed_ref_runtime;
  494. /*
  495. * this is updated to the current trans every time a full commit
  496. * is required instead of the faster short fsync log commits
  497. */
  498. u64 last_trans_log_full_commit;
  499. unsigned long mount_opt;
  500. /*
  501. * Track requests for actions that need to be done during transaction
  502. * commit (like for some mount options).
  503. */
  504. unsigned long pending_changes;
  505. unsigned long compress_type:4;
  506. unsigned int compress_level;
  507. u32 commit_interval;
  508. /*
  509. * It is a suggestive number, the read side is safe even it gets a
  510. * wrong number because we will write out the data into a regular
  511. * extent. The write side(mount/remount) is under ->s_umount lock,
  512. * so it is also safe.
  513. */
  514. u64 max_inline;
  515. struct btrfs_transaction *running_transaction;
  516. wait_queue_head_t transaction_throttle;
  517. wait_queue_head_t transaction_wait;
  518. wait_queue_head_t transaction_blocked_wait;
  519. wait_queue_head_t async_submit_wait;
  520. /*
  521. * Used to protect the incompat_flags, compat_flags, compat_ro_flags
  522. * when they are updated.
  523. *
  524. * Because we do not clear the flags for ever, so we needn't use
  525. * the lock on the read side.
  526. *
  527. * We also needn't use the lock when we mount the fs, because
  528. * there is no other task which will update the flag.
  529. */
  530. spinlock_t super_lock;
  531. struct btrfs_super_block *super_copy;
  532. struct btrfs_super_block *super_for_commit;
  533. struct super_block *sb;
  534. struct inode *btree_inode;
  535. struct mutex tree_log_mutex;
  536. struct mutex transaction_kthread_mutex;
  537. struct mutex cleaner_mutex;
  538. struct mutex chunk_mutex;
  539. /*
  540. * this is taken to make sure we don't set block groups ro after
  541. * the free space cache has been allocated on them
  542. */
  543. struct mutex ro_block_group_mutex;
  544. /* this is used during read/modify/write to make sure
  545. * no two ios are trying to mod the same stripe at the same
  546. * time
  547. */
  548. struct btrfs_stripe_hash_table *stripe_hash_table;
  549. /*
  550. * this protects the ordered operations list only while we are
  551. * processing all of the entries on it. This way we make
  552. * sure the commit code doesn't find the list temporarily empty
  553. * because another function happens to be doing non-waiting preflush
  554. * before jumping into the main commit.
  555. */
  556. struct mutex ordered_operations_mutex;
  557. struct rw_semaphore commit_root_sem;
  558. struct rw_semaphore cleanup_work_sem;
  559. struct rw_semaphore subvol_sem;
  560. struct srcu_struct subvol_srcu;
  561. spinlock_t trans_lock;
  562. /*
  563. * the reloc mutex goes with the trans lock, it is taken
  564. * during commit to protect us from the relocation code
  565. */
  566. struct mutex reloc_mutex;
  567. struct list_head trans_list;
  568. struct list_head dead_roots;
  569. struct list_head caching_block_groups;
  570. spinlock_t delayed_iput_lock;
  571. struct list_head delayed_iputs;
  572. atomic_t nr_delayed_iputs;
  573. wait_queue_head_t delayed_iputs_wait;
  574. atomic64_t tree_mod_seq;
  575. /* this protects tree_mod_log and tree_mod_seq_list */
  576. rwlock_t tree_mod_log_lock;
  577. struct rb_root tree_mod_log;
  578. struct list_head tree_mod_seq_list;
  579. atomic_t async_delalloc_pages;
  580. /*
  581. * this is used to protect the following list -- ordered_roots.
  582. */
  583. spinlock_t ordered_root_lock;
  584. /*
  585. * all fs/file tree roots in which there are data=ordered extents
  586. * pending writeback are added into this list.
  587. *
  588. * these can span multiple transactions and basically include
  589. * every dirty data page that isn't from nodatacow
  590. */
  591. struct list_head ordered_roots;
  592. struct mutex delalloc_root_mutex;
  593. spinlock_t delalloc_root_lock;
  594. /* all fs/file tree roots that have delalloc inodes. */
  595. struct list_head delalloc_roots;
  596. /*
  597. * there is a pool of worker threads for checksumming during writes
  598. * and a pool for checksumming after reads. This is because readers
  599. * can run with FS locks held, and the writers may be waiting for
  600. * those locks. We don't want ordering in the pending list to cause
  601. * deadlocks, and so the two are serviced separately.
  602. *
  603. * A third pool does submit_bio to avoid deadlocking with the other
  604. * two
  605. */
  606. struct btrfs_workqueue *workers;
  607. struct btrfs_workqueue *delalloc_workers;
  608. struct btrfs_workqueue *flush_workers;
  609. struct btrfs_workqueue *endio_workers;
  610. struct btrfs_workqueue *endio_meta_workers;
  611. struct btrfs_workqueue *endio_raid56_workers;
  612. struct btrfs_workqueue *endio_repair_workers;
  613. struct btrfs_workqueue *rmw_workers;
  614. struct btrfs_workqueue *endio_meta_write_workers;
  615. struct btrfs_workqueue *endio_write_workers;
  616. struct btrfs_workqueue *endio_freespace_worker;
  617. struct btrfs_workqueue *submit_workers;
  618. struct btrfs_workqueue *caching_workers;
  619. struct btrfs_workqueue *readahead_workers;
  620. /*
  621. * fixup workers take dirty pages that didn't properly go through
  622. * the cow mechanism and make them safe to write. It happens
  623. * for the sys_munmap function call path
  624. */
  625. struct btrfs_workqueue *fixup_workers;
  626. struct btrfs_workqueue *delayed_workers;
  627. struct task_struct *transaction_kthread;
  628. struct task_struct *cleaner_kthread;
  629. u32 thread_pool_size;
  630. struct kobject *space_info_kobj;
  631. u64 total_pinned;
  632. /* used to keep from writing metadata until there is a nice batch */
  633. struct percpu_counter dirty_metadata_bytes;
  634. struct percpu_counter delalloc_bytes;
  635. struct percpu_counter dio_bytes;
  636. s32 dirty_metadata_batch;
  637. s32 delalloc_batch;
  638. struct list_head dirty_cowonly_roots;
  639. struct btrfs_fs_devices *fs_devices;
  640. /*
  641. * The space_info list is effectively read only after initial
  642. * setup. It is populated at mount time and cleaned up after
  643. * all block groups are removed. RCU is used to protect it.
  644. */
  645. struct list_head space_info;
  646. struct btrfs_space_info *data_sinfo;
  647. struct reloc_control *reloc_ctl;
  648. /* data_alloc_cluster is only used in ssd_spread mode */
  649. struct btrfs_free_cluster data_alloc_cluster;
  650. /* all metadata allocations go through this cluster */
  651. struct btrfs_free_cluster meta_alloc_cluster;
  652. /* auto defrag inodes go here */
  653. spinlock_t defrag_inodes_lock;
  654. struct rb_root defrag_inodes;
  655. atomic_t defrag_running;
  656. /* Used to protect avail_{data, metadata, system}_alloc_bits */
  657. seqlock_t profiles_lock;
  658. /*
  659. * these three are in extended format (availability of single
  660. * chunks is denoted by BTRFS_AVAIL_ALLOC_BIT_SINGLE bit, other
  661. * types are denoted by corresponding BTRFS_BLOCK_GROUP_* bits)
  662. */
  663. u64 avail_data_alloc_bits;
  664. u64 avail_metadata_alloc_bits;
  665. u64 avail_system_alloc_bits;
  666. /* restriper state */
  667. spinlock_t balance_lock;
  668. struct mutex balance_mutex;
  669. atomic_t balance_pause_req;
  670. atomic_t balance_cancel_req;
  671. struct btrfs_balance_control *balance_ctl;
  672. wait_queue_head_t balance_wait_q;
  673. u32 data_chunk_allocations;
  674. u32 metadata_ratio;
  675. void *bdev_holder;
  676. /* private scrub information */
  677. struct mutex scrub_lock;
  678. atomic_t scrubs_running;
  679. atomic_t scrub_pause_req;
  680. atomic_t scrubs_paused;
  681. atomic_t scrub_cancel_req;
  682. wait_queue_head_t scrub_pause_wait;
  683. /*
  684. * The worker pointers are NULL iff the refcount is 0, ie. scrub is not
  685. * running.
  686. */
  687. refcount_t scrub_workers_refcnt;
  688. struct btrfs_workqueue *scrub_workers;
  689. struct btrfs_workqueue *scrub_wr_completion_workers;
  690. struct btrfs_workqueue *scrub_parity_workers;
  691. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  692. u32 check_integrity_print_mask;
  693. #endif
  694. /* is qgroup tracking in a consistent state? */
  695. u64 qgroup_flags;
  696. /* holds configuration and tracking. Protected by qgroup_lock */
  697. struct rb_root qgroup_tree;
  698. spinlock_t qgroup_lock;
  699. /*
  700. * used to avoid frequently calling ulist_alloc()/ulist_free()
  701. * when doing qgroup accounting, it must be protected by qgroup_lock.
  702. */
  703. struct ulist *qgroup_ulist;
  704. /*
  705. * Protect user change for quota operations. If a transaction is needed,
  706. * it must be started before locking this lock.
  707. */
  708. struct mutex qgroup_ioctl_lock;
  709. /* list of dirty qgroups to be written at next commit */
  710. struct list_head dirty_qgroups;
  711. /* used by qgroup for an efficient tree traversal */
  712. u64 qgroup_seq;
  713. /* qgroup rescan items */
  714. struct mutex qgroup_rescan_lock; /* protects the progress item */
  715. struct btrfs_key qgroup_rescan_progress;
  716. struct btrfs_workqueue *qgroup_rescan_workers;
  717. struct completion qgroup_rescan_completion;
  718. struct btrfs_work qgroup_rescan_work;
  719. bool qgroup_rescan_running; /* protected by qgroup_rescan_lock */
  720. /* filesystem state */
  721. unsigned long fs_state;
  722. struct btrfs_delayed_root *delayed_root;
  723. /* readahead tree */
  724. spinlock_t reada_lock;
  725. struct radix_tree_root reada_tree;
  726. /* readahead works cnt */
  727. atomic_t reada_works_cnt;
  728. /* Extent buffer radix tree */
  729. spinlock_t buffer_lock;
  730. struct radix_tree_root buffer_radix;
  731. /* next backup root to be overwritten */
  732. int backup_root_index;
  733. /* device replace state */
  734. struct btrfs_dev_replace dev_replace;
  735. struct semaphore uuid_tree_rescan_sem;
  736. /* Used to reclaim the metadata space in the background. */
  737. struct work_struct async_reclaim_work;
  738. spinlock_t unused_bgs_lock;
  739. struct list_head unused_bgs;
  740. struct mutex unused_bg_unpin_mutex;
  741. struct mutex delete_unused_bgs_mutex;
  742. /* Cached block sizes */
  743. u32 nodesize;
  744. u32 sectorsize;
  745. u32 stripesize;
  746. /* Block groups and devices containing active swapfiles. */
  747. spinlock_t swapfile_pins_lock;
  748. struct rb_root swapfile_pins;
  749. struct crypto_shash *csum_shash;
  750. /*
  751. * Number of send operations in progress.
  752. * Updated while holding fs_info::balance_mutex.
  753. */
  754. int send_in_progress;
  755. #ifdef CONFIG_BTRFS_FS_REF_VERIFY
  756. spinlock_t ref_verify_lock;
  757. struct rb_root block_tree;
  758. #endif
  759. };
  760. static inline struct btrfs_fs_info *btrfs_sb(struct super_block *sb)
  761. {
  762. return sb->s_fs_info;
  763. }
  764. struct btrfs_subvolume_writers {
  765. struct percpu_counter counter;
  766. wait_queue_head_t wait;
  767. };
  768. /*
  769. * The state of btrfs root
  770. */
  771. enum {
  772. /*
  773. * btrfs_record_root_in_trans is a multi-step process, and it can race
  774. * with the balancing code. But the race is very small, and only the
  775. * first time the root is added to each transaction. So IN_TRANS_SETUP
  776. * is used to tell us when more checks are required
  777. */
  778. BTRFS_ROOT_IN_TRANS_SETUP,
  779. BTRFS_ROOT_REF_COWS,
  780. BTRFS_ROOT_TRACK_DIRTY,
  781. BTRFS_ROOT_IN_RADIX,
  782. BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
  783. BTRFS_ROOT_DEFRAG_RUNNING,
  784. BTRFS_ROOT_FORCE_COW,
  785. BTRFS_ROOT_MULTI_LOG_TASKS,
  786. BTRFS_ROOT_DIRTY,
  787. BTRFS_ROOT_DELETING,
  788. /*
  789. * Reloc tree is orphan, only kept here for qgroup delayed subtree scan
  790. *
  791. * Set for the subvolume tree owning the reloc tree.
  792. */
  793. BTRFS_ROOT_DEAD_RELOC_TREE,
  794. /* Mark dead root stored on device whose cleanup needs to be resumed */
  795. BTRFS_ROOT_DEAD_TREE,
  796. /* The root has a log tree. Used only for subvolume roots. */
  797. BTRFS_ROOT_HAS_LOG_TREE,
  798. /* Qgroup flushing is in progress */
  799. BTRFS_ROOT_QGROUP_FLUSHING,
  800. };
  801. /*
  802. * Record swapped tree blocks of a subvolume tree for delayed subtree trace
  803. * code. For detail check comment in fs/btrfs/qgroup.c.
  804. */
  805. struct btrfs_qgroup_swapped_blocks {
  806. spinlock_t lock;
  807. /* RM_EMPTY_ROOT() of above blocks[] */
  808. bool swapped;
  809. struct rb_root blocks[BTRFS_MAX_LEVEL];
  810. };
  811. /*
  812. * in ram representation of the tree. extent_root is used for all allocations
  813. * and for the extent tree extent_root root.
  814. */
  815. struct btrfs_root {
  816. struct extent_buffer *node;
  817. struct extent_buffer *commit_root;
  818. struct btrfs_root *log_root;
  819. struct btrfs_root *reloc_root;
  820. unsigned long state;
  821. struct btrfs_root_item root_item;
  822. struct btrfs_key root_key;
  823. struct btrfs_fs_info *fs_info;
  824. struct extent_io_tree dirty_log_pages;
  825. struct mutex objectid_mutex;
  826. spinlock_t accounting_lock;
  827. struct btrfs_block_rsv *block_rsv;
  828. /* free ino cache stuff */
  829. struct btrfs_free_space_ctl *free_ino_ctl;
  830. enum btrfs_caching_type ino_cache_state;
  831. spinlock_t ino_cache_lock;
  832. wait_queue_head_t ino_cache_wait;
  833. struct btrfs_free_space_ctl *free_ino_pinned;
  834. u64 ino_cache_progress;
  835. struct inode *ino_cache_inode;
  836. struct mutex log_mutex;
  837. wait_queue_head_t log_writer_wait;
  838. wait_queue_head_t log_commit_wait[2];
  839. struct list_head log_ctxs[2];
  840. /* Used only for log trees of subvolumes, not for the log root tree */
  841. atomic_t log_writers;
  842. atomic_t log_commit[2];
  843. /* Used only for log trees of subvolumes, not for the log root tree */
  844. atomic_t log_batch;
  845. int log_transid;
  846. /* No matter the commit succeeds or not*/
  847. int log_transid_committed;
  848. /* Just be updated when the commit succeeds. */
  849. int last_log_commit;
  850. pid_t log_start_pid;
  851. u64 last_trans;
  852. u32 type;
  853. u64 highest_objectid;
  854. u64 defrag_trans_start;
  855. struct btrfs_key defrag_progress;
  856. struct btrfs_key defrag_max;
  857. /* the dirty list is only used by non-reference counted roots */
  858. struct list_head dirty_list;
  859. struct list_head root_list;
  860. spinlock_t log_extents_lock[2];
  861. struct list_head logged_list[2];
  862. int orphan_cleanup_state;
  863. spinlock_t inode_lock;
  864. /* red-black tree that keeps track of in-memory inodes */
  865. struct rb_root inode_tree;
  866. /*
  867. * radix tree that keeps track of delayed nodes of every inode,
  868. * protected by inode_lock
  869. */
  870. struct radix_tree_root delayed_nodes_tree;
  871. /*
  872. * right now this just gets used so that a root has its own devid
  873. * for stat. It may be used for more later
  874. */
  875. dev_t anon_dev;
  876. spinlock_t root_item_lock;
  877. refcount_t refs;
  878. struct mutex delalloc_mutex;
  879. spinlock_t delalloc_lock;
  880. /*
  881. * all of the inodes that have delalloc bytes. It is possible for
  882. * this list to be empty even when there is still dirty data=ordered
  883. * extents waiting to finish IO.
  884. */
  885. struct list_head delalloc_inodes;
  886. struct list_head delalloc_root;
  887. u64 nr_delalloc_inodes;
  888. struct mutex ordered_extent_mutex;
  889. /*
  890. * this is used by the balancing code to wait for all the pending
  891. * ordered extents
  892. */
  893. spinlock_t ordered_extent_lock;
  894. /*
  895. * all of the data=ordered extents pending writeback
  896. * these can span multiple transactions and basically include
  897. * every dirty data page that isn't from nodatacow
  898. */
  899. struct list_head ordered_extents;
  900. struct list_head ordered_root;
  901. u64 nr_ordered_extents;
  902. /*
  903. * Not empty if this subvolume root has gone through tree block swap
  904. * (relocation)
  905. *
  906. * Will be used by reloc_control::dirty_subvol_roots.
  907. */
  908. struct list_head reloc_dirty_list;
  909. /*
  910. * Number of currently running SEND ioctls to prevent
  911. * manipulation with the read-only status via SUBVOL_SETFLAGS
  912. */
  913. int send_in_progress;
  914. /*
  915. * Number of currently running deduplication operations that have a
  916. * destination inode belonging to this root. Protected by the lock
  917. * root_item_lock.
  918. */
  919. int dedupe_in_progress;
  920. struct btrfs_subvolume_writers *subv_writers;
  921. atomic_t will_be_snapshotted;
  922. atomic_t snapshot_force_cow;
  923. /* For qgroup metadata reserved space */
  924. spinlock_t qgroup_meta_rsv_lock;
  925. u64 qgroup_meta_rsv_pertrans;
  926. u64 qgroup_meta_rsv_prealloc;
  927. wait_queue_head_t qgroup_flush_wait;
  928. /* Number of active swapfiles */
  929. atomic_t nr_swapfiles;
  930. /* Record pairs of swapped blocks for qgroup */
  931. struct btrfs_qgroup_swapped_blocks swapped_blocks;
  932. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  933. u64 alloc_bytenr;
  934. #endif
  935. };
  936. struct btrfs_clone_extent_info {
  937. u64 disk_offset;
  938. u64 disk_len;
  939. u64 data_offset;
  940. u64 data_len;
  941. u64 file_offset;
  942. char *extent_buf;
  943. u32 item_size;
  944. };
  945. struct btrfs_file_private {
  946. void *filldir_buf;
  947. };
  948. static inline u32 btrfs_inode_sectorsize(const struct inode *inode)
  949. {
  950. return btrfs_sb(inode->i_sb)->sectorsize;
  951. }
  952. static inline u32 BTRFS_LEAF_DATA_SIZE(const struct btrfs_fs_info *info)
  953. {
  954. return info->nodesize - sizeof(struct btrfs_header);
  955. }
  956. #define BTRFS_LEAF_DATA_OFFSET offsetof(struct btrfs_leaf, items)
  957. static inline u32 BTRFS_MAX_ITEM_SIZE(const struct btrfs_fs_info *info)
  958. {
  959. return BTRFS_LEAF_DATA_SIZE(info) - sizeof(struct btrfs_item);
  960. }
  961. static inline u32 BTRFS_NODEPTRS_PER_BLOCK(const struct btrfs_fs_info *info)
  962. {
  963. return BTRFS_LEAF_DATA_SIZE(info) / sizeof(struct btrfs_key_ptr);
  964. }
  965. #define BTRFS_FILE_EXTENT_INLINE_DATA_START \
  966. (offsetof(struct btrfs_file_extent_item, disk_bytenr))
  967. static inline u32 BTRFS_MAX_INLINE_DATA_SIZE(const struct btrfs_fs_info *info)
  968. {
  969. return BTRFS_MAX_ITEM_SIZE(info) -
  970. BTRFS_FILE_EXTENT_INLINE_DATA_START;
  971. }
  972. static inline u32 BTRFS_MAX_XATTR_SIZE(const struct btrfs_fs_info *info)
  973. {
  974. return BTRFS_MAX_ITEM_SIZE(info) - sizeof(struct btrfs_dir_item);
  975. }
  976. /*
  977. * Flags for mount options.
  978. *
  979. * Note: don't forget to add new options to btrfs_show_options()
  980. */
  981. #define BTRFS_MOUNT_NODATASUM (1 << 0)
  982. #define BTRFS_MOUNT_NODATACOW (1 << 1)
  983. #define BTRFS_MOUNT_NOBARRIER (1 << 2)
  984. #define BTRFS_MOUNT_SSD (1 << 3)
  985. #define BTRFS_MOUNT_DEGRADED (1 << 4)
  986. #define BTRFS_MOUNT_COMPRESS (1 << 5)
  987. #define BTRFS_MOUNT_NOTREELOG (1 << 6)
  988. #define BTRFS_MOUNT_FLUSHONCOMMIT (1 << 7)
  989. #define BTRFS_MOUNT_SSD_SPREAD (1 << 8)
  990. #define BTRFS_MOUNT_NOSSD (1 << 9)
  991. #define BTRFS_MOUNT_DISCARD (1 << 10)
  992. #define BTRFS_MOUNT_FORCE_COMPRESS (1 << 11)
  993. #define BTRFS_MOUNT_SPACE_CACHE (1 << 12)
  994. #define BTRFS_MOUNT_CLEAR_CACHE (1 << 13)
  995. #define BTRFS_MOUNT_USER_SUBVOL_RM_ALLOWED (1 << 14)
  996. #define BTRFS_MOUNT_ENOSPC_DEBUG (1 << 15)
  997. #define BTRFS_MOUNT_AUTO_DEFRAG (1 << 16)
  998. #define BTRFS_MOUNT_INODE_MAP_CACHE (1 << 17)
  999. #define BTRFS_MOUNT_USEBACKUPROOT (1 << 18)
  1000. #define BTRFS_MOUNT_SKIP_BALANCE (1 << 19)
  1001. #define BTRFS_MOUNT_CHECK_INTEGRITY (1 << 20)
  1002. #define BTRFS_MOUNT_CHECK_INTEGRITY_INCLUDING_EXTENT_DATA (1 << 21)
  1003. #define BTRFS_MOUNT_PANIC_ON_FATAL_ERROR (1 << 22)
  1004. #define BTRFS_MOUNT_RESCAN_UUID_TREE (1 << 23)
  1005. #define BTRFS_MOUNT_FRAGMENT_DATA (1 << 24)
  1006. #define BTRFS_MOUNT_FRAGMENT_METADATA (1 << 25)
  1007. #define BTRFS_MOUNT_FREE_SPACE_TREE (1 << 26)
  1008. #define BTRFS_MOUNT_NOLOGREPLAY (1 << 27)
  1009. #define BTRFS_MOUNT_REF_VERIFY (1 << 28)
  1010. #define BTRFS_DEFAULT_COMMIT_INTERVAL (30)
  1011. #define BTRFS_DEFAULT_MAX_INLINE (2048)
  1012. #define btrfs_clear_opt(o, opt) ((o) &= ~BTRFS_MOUNT_##opt)
  1013. #define btrfs_set_opt(o, opt) ((o) |= BTRFS_MOUNT_##opt)
  1014. #define btrfs_raw_test_opt(o, opt) ((o) & BTRFS_MOUNT_##opt)
  1015. #define btrfs_test_opt(fs_info, opt) ((fs_info)->mount_opt & \
  1016. BTRFS_MOUNT_##opt)
  1017. #define btrfs_set_and_info(fs_info, opt, fmt, args...) \
  1018. { \
  1019. if (!btrfs_test_opt(fs_info, opt)) \
  1020. btrfs_info(fs_info, fmt, ##args); \
  1021. btrfs_set_opt(fs_info->mount_opt, opt); \
  1022. }
  1023. #define btrfs_clear_and_info(fs_info, opt, fmt, args...) \
  1024. { \
  1025. if (btrfs_test_opt(fs_info, opt)) \
  1026. btrfs_info(fs_info, fmt, ##args); \
  1027. btrfs_clear_opt(fs_info->mount_opt, opt); \
  1028. }
  1029. /*
  1030. * Requests for changes that need to be done during transaction commit.
  1031. *
  1032. * Internal mount options that are used for special handling of the real
  1033. * mount options (eg. cannot be set during remount and have to be set during
  1034. * transaction commit)
  1035. */
  1036. #define BTRFS_PENDING_SET_INODE_MAP_CACHE (0)
  1037. #define BTRFS_PENDING_CLEAR_INODE_MAP_CACHE (1)
  1038. #define BTRFS_PENDING_COMMIT (2)
  1039. #define btrfs_test_pending(info, opt) \
  1040. test_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
  1041. #define btrfs_set_pending(info, opt) \
  1042. set_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
  1043. #define btrfs_clear_pending(info, opt) \
  1044. clear_bit(BTRFS_PENDING_##opt, &(info)->pending_changes)
  1045. /*
  1046. * Helpers for setting pending mount option changes.
  1047. *
  1048. * Expects corresponding macros
  1049. * BTRFS_PENDING_SET_ and CLEAR_ + short mount option name
  1050. */
  1051. #define btrfs_set_pending_and_info(info, opt, fmt, args...) \
  1052. do { \
  1053. if (!btrfs_raw_test_opt((info)->mount_opt, opt)) { \
  1054. btrfs_info((info), fmt, ##args); \
  1055. btrfs_set_pending((info), SET_##opt); \
  1056. btrfs_clear_pending((info), CLEAR_##opt); \
  1057. } \
  1058. } while(0)
  1059. #define btrfs_clear_pending_and_info(info, opt, fmt, args...) \
  1060. do { \
  1061. if (btrfs_raw_test_opt((info)->mount_opt, opt)) { \
  1062. btrfs_info((info), fmt, ##args); \
  1063. btrfs_set_pending((info), CLEAR_##opt); \
  1064. btrfs_clear_pending((info), SET_##opt); \
  1065. } \
  1066. } while(0)
  1067. /*
  1068. * Inode flags
  1069. */
  1070. #define BTRFS_INODE_NODATASUM (1 << 0)
  1071. #define BTRFS_INODE_NODATACOW (1 << 1)
  1072. #define BTRFS_INODE_READONLY (1 << 2)
  1073. #define BTRFS_INODE_NOCOMPRESS (1 << 3)
  1074. #define BTRFS_INODE_PREALLOC (1 << 4)
  1075. #define BTRFS_INODE_SYNC (1 << 5)
  1076. #define BTRFS_INODE_IMMUTABLE (1 << 6)
  1077. #define BTRFS_INODE_APPEND (1 << 7)
  1078. #define BTRFS_INODE_NODUMP (1 << 8)
  1079. #define BTRFS_INODE_NOATIME (1 << 9)
  1080. #define BTRFS_INODE_DIRSYNC (1 << 10)
  1081. #define BTRFS_INODE_COMPRESS (1 << 11)
  1082. #define BTRFS_INODE_ROOT_ITEM_INIT (1 << 31)
  1083. #define BTRFS_INODE_FLAG_MASK \
  1084. (BTRFS_INODE_NODATASUM | \
  1085. BTRFS_INODE_NODATACOW | \
  1086. BTRFS_INODE_READONLY | \
  1087. BTRFS_INODE_NOCOMPRESS | \
  1088. BTRFS_INODE_PREALLOC | \
  1089. BTRFS_INODE_SYNC | \
  1090. BTRFS_INODE_IMMUTABLE | \
  1091. BTRFS_INODE_APPEND | \
  1092. BTRFS_INODE_NODUMP | \
  1093. BTRFS_INODE_NOATIME | \
  1094. BTRFS_INODE_DIRSYNC | \
  1095. BTRFS_INODE_COMPRESS | \
  1096. BTRFS_INODE_ROOT_ITEM_INIT)
  1097. struct btrfs_map_token {
  1098. const struct extent_buffer *eb;
  1099. char *kaddr;
  1100. unsigned long offset;
  1101. };
  1102. #define BTRFS_BYTES_TO_BLKS(fs_info, bytes) \
  1103. ((bytes) >> (fs_info)->sb->s_blocksize_bits)
  1104. static inline void btrfs_init_map_token(struct btrfs_map_token *token,
  1105. struct extent_buffer *eb)
  1106. {
  1107. token->eb = eb;
  1108. token->kaddr = NULL;
  1109. }
  1110. /* some macros to generate set/get functions for the struct fields. This
  1111. * assumes there is a lefoo_to_cpu for every type, so lets make a simple
  1112. * one for u8:
  1113. */
  1114. #define le8_to_cpu(v) (v)
  1115. #define cpu_to_le8(v) (v)
  1116. #define __le8 u8
  1117. #define read_eb_member(eb, ptr, type, member, result) (\
  1118. read_extent_buffer(eb, (char *)(result), \
  1119. ((unsigned long)(ptr)) + \
  1120. offsetof(type, member), \
  1121. sizeof(((type *)0)->member)))
  1122. #define write_eb_member(eb, ptr, type, member, result) (\
  1123. write_extent_buffer(eb, (char *)(result), \
  1124. ((unsigned long)(ptr)) + \
  1125. offsetof(type, member), \
  1126. sizeof(((type *)0)->member)))
  1127. #define DECLARE_BTRFS_SETGET_BITS(bits) \
  1128. u##bits btrfs_get_token_##bits(const struct extent_buffer *eb, \
  1129. const void *ptr, unsigned long off, \
  1130. struct btrfs_map_token *token); \
  1131. void btrfs_set_token_##bits(struct extent_buffer *eb, const void *ptr, \
  1132. unsigned long off, u##bits val, \
  1133. struct btrfs_map_token *token); \
  1134. u##bits btrfs_get_##bits(const struct extent_buffer *eb, \
  1135. const void *ptr, unsigned long off); \
  1136. void btrfs_set_##bits(struct extent_buffer *eb, void *ptr, \
  1137. unsigned long off, u##bits val);
  1138. DECLARE_BTRFS_SETGET_BITS(8)
  1139. DECLARE_BTRFS_SETGET_BITS(16)
  1140. DECLARE_BTRFS_SETGET_BITS(32)
  1141. DECLARE_BTRFS_SETGET_BITS(64)
  1142. #define BTRFS_SETGET_FUNCS(name, type, member, bits) \
  1143. static inline u##bits btrfs_##name(const struct extent_buffer *eb, \
  1144. const type *s) \
  1145. { \
  1146. BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
  1147. return btrfs_get_##bits(eb, s, offsetof(type, member)); \
  1148. } \
  1149. static inline void btrfs_set_##name(struct extent_buffer *eb, type *s, \
  1150. u##bits val) \
  1151. { \
  1152. BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
  1153. btrfs_set_##bits(eb, s, offsetof(type, member), val); \
  1154. } \
  1155. static inline u##bits btrfs_token_##name(const struct extent_buffer *eb,\
  1156. const type *s, \
  1157. struct btrfs_map_token *token) \
  1158. { \
  1159. BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
  1160. return btrfs_get_token_##bits(eb, s, offsetof(type, member), token); \
  1161. } \
  1162. static inline void btrfs_set_token_##name(struct extent_buffer *eb, \
  1163. type *s, u##bits val, \
  1164. struct btrfs_map_token *token) \
  1165. { \
  1166. BUILD_BUG_ON(sizeof(u##bits) != sizeof(((type *)0))->member); \
  1167. btrfs_set_token_##bits(eb, s, offsetof(type, member), val, token); \
  1168. }
  1169. #define BTRFS_SETGET_HEADER_FUNCS(name, type, member, bits) \
  1170. static inline u##bits btrfs_##name(const struct extent_buffer *eb) \
  1171. { \
  1172. const type *p = page_address(eb->pages[0]); \
  1173. u##bits res = le##bits##_to_cpu(p->member); \
  1174. return res; \
  1175. } \
  1176. static inline void btrfs_set_##name(struct extent_buffer *eb, \
  1177. u##bits val) \
  1178. { \
  1179. type *p = page_address(eb->pages[0]); \
  1180. p->member = cpu_to_le##bits(val); \
  1181. }
  1182. #define BTRFS_SETGET_STACK_FUNCS(name, type, member, bits) \
  1183. static inline u##bits btrfs_##name(const type *s) \
  1184. { \
  1185. return le##bits##_to_cpu(s->member); \
  1186. } \
  1187. static inline void btrfs_set_##name(type *s, u##bits val) \
  1188. { \
  1189. s->member = cpu_to_le##bits(val); \
  1190. }
  1191. static inline u64 btrfs_device_total_bytes(struct extent_buffer *eb,
  1192. struct btrfs_dev_item *s)
  1193. {
  1194. BUILD_BUG_ON(sizeof(u64) !=
  1195. sizeof(((struct btrfs_dev_item *)0))->total_bytes);
  1196. return btrfs_get_64(eb, s, offsetof(struct btrfs_dev_item,
  1197. total_bytes));
  1198. }
  1199. static inline void btrfs_set_device_total_bytes(struct extent_buffer *eb,
  1200. struct btrfs_dev_item *s,
  1201. u64 val)
  1202. {
  1203. BUILD_BUG_ON(sizeof(u64) !=
  1204. sizeof(((struct btrfs_dev_item *)0))->total_bytes);
  1205. WARN_ON(!IS_ALIGNED(val, eb->fs_info->sectorsize));
  1206. btrfs_set_64(eb, s, offsetof(struct btrfs_dev_item, total_bytes), val);
  1207. }
  1208. BTRFS_SETGET_FUNCS(device_type, struct btrfs_dev_item, type, 64);
  1209. BTRFS_SETGET_FUNCS(device_bytes_used, struct btrfs_dev_item, bytes_used, 64);
  1210. BTRFS_SETGET_FUNCS(device_io_align, struct btrfs_dev_item, io_align, 32);
  1211. BTRFS_SETGET_FUNCS(device_io_width, struct btrfs_dev_item, io_width, 32);
  1212. BTRFS_SETGET_FUNCS(device_start_offset, struct btrfs_dev_item,
  1213. start_offset, 64);
  1214. BTRFS_SETGET_FUNCS(device_sector_size, struct btrfs_dev_item, sector_size, 32);
  1215. BTRFS_SETGET_FUNCS(device_id, struct btrfs_dev_item, devid, 64);
  1216. BTRFS_SETGET_FUNCS(device_group, struct btrfs_dev_item, dev_group, 32);
  1217. BTRFS_SETGET_FUNCS(device_seek_speed, struct btrfs_dev_item, seek_speed, 8);
  1218. BTRFS_SETGET_FUNCS(device_bandwidth, struct btrfs_dev_item, bandwidth, 8);
  1219. BTRFS_SETGET_FUNCS(device_generation, struct btrfs_dev_item, generation, 64);
  1220. BTRFS_SETGET_STACK_FUNCS(stack_device_type, struct btrfs_dev_item, type, 64);
  1221. BTRFS_SETGET_STACK_FUNCS(stack_device_total_bytes, struct btrfs_dev_item,
  1222. total_bytes, 64);
  1223. BTRFS_SETGET_STACK_FUNCS(stack_device_bytes_used, struct btrfs_dev_item,
  1224. bytes_used, 64);
  1225. BTRFS_SETGET_STACK_FUNCS(stack_device_io_align, struct btrfs_dev_item,
  1226. io_align, 32);
  1227. BTRFS_SETGET_STACK_FUNCS(stack_device_io_width, struct btrfs_dev_item,
  1228. io_width, 32);
  1229. BTRFS_SETGET_STACK_FUNCS(stack_device_sector_size, struct btrfs_dev_item,
  1230. sector_size, 32);
  1231. BTRFS_SETGET_STACK_FUNCS(stack_device_id, struct btrfs_dev_item, devid, 64);
  1232. BTRFS_SETGET_STACK_FUNCS(stack_device_group, struct btrfs_dev_item,
  1233. dev_group, 32);
  1234. BTRFS_SETGET_STACK_FUNCS(stack_device_seek_speed, struct btrfs_dev_item,
  1235. seek_speed, 8);
  1236. BTRFS_SETGET_STACK_FUNCS(stack_device_bandwidth, struct btrfs_dev_item,
  1237. bandwidth, 8);
  1238. BTRFS_SETGET_STACK_FUNCS(stack_device_generation, struct btrfs_dev_item,
  1239. generation, 64);
  1240. static inline unsigned long btrfs_device_uuid(struct btrfs_dev_item *d)
  1241. {
  1242. return (unsigned long)d + offsetof(struct btrfs_dev_item, uuid);
  1243. }
  1244. static inline unsigned long btrfs_device_fsid(struct btrfs_dev_item *d)
  1245. {
  1246. return (unsigned long)d + offsetof(struct btrfs_dev_item, fsid);
  1247. }
  1248. BTRFS_SETGET_FUNCS(chunk_length, struct btrfs_chunk, length, 64);
  1249. BTRFS_SETGET_FUNCS(chunk_owner, struct btrfs_chunk, owner, 64);
  1250. BTRFS_SETGET_FUNCS(chunk_stripe_len, struct btrfs_chunk, stripe_len, 64);
  1251. BTRFS_SETGET_FUNCS(chunk_io_align, struct btrfs_chunk, io_align, 32);
  1252. BTRFS_SETGET_FUNCS(chunk_io_width, struct btrfs_chunk, io_width, 32);
  1253. BTRFS_SETGET_FUNCS(chunk_sector_size, struct btrfs_chunk, sector_size, 32);
  1254. BTRFS_SETGET_FUNCS(chunk_type, struct btrfs_chunk, type, 64);
  1255. BTRFS_SETGET_FUNCS(chunk_num_stripes, struct btrfs_chunk, num_stripes, 16);
  1256. BTRFS_SETGET_FUNCS(chunk_sub_stripes, struct btrfs_chunk, sub_stripes, 16);
  1257. BTRFS_SETGET_FUNCS(stripe_devid, struct btrfs_stripe, devid, 64);
  1258. BTRFS_SETGET_FUNCS(stripe_offset, struct btrfs_stripe, offset, 64);
  1259. static inline char *btrfs_stripe_dev_uuid(struct btrfs_stripe *s)
  1260. {
  1261. return (char *)s + offsetof(struct btrfs_stripe, dev_uuid);
  1262. }
  1263. BTRFS_SETGET_STACK_FUNCS(stack_chunk_length, struct btrfs_chunk, length, 64);
  1264. BTRFS_SETGET_STACK_FUNCS(stack_chunk_owner, struct btrfs_chunk, owner, 64);
  1265. BTRFS_SETGET_STACK_FUNCS(stack_chunk_stripe_len, struct btrfs_chunk,
  1266. stripe_len, 64);
  1267. BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_align, struct btrfs_chunk,
  1268. io_align, 32);
  1269. BTRFS_SETGET_STACK_FUNCS(stack_chunk_io_width, struct btrfs_chunk,
  1270. io_width, 32);
  1271. BTRFS_SETGET_STACK_FUNCS(stack_chunk_sector_size, struct btrfs_chunk,
  1272. sector_size, 32);
  1273. BTRFS_SETGET_STACK_FUNCS(stack_chunk_type, struct btrfs_chunk, type, 64);
  1274. BTRFS_SETGET_STACK_FUNCS(stack_chunk_num_stripes, struct btrfs_chunk,
  1275. num_stripes, 16);
  1276. BTRFS_SETGET_STACK_FUNCS(stack_chunk_sub_stripes, struct btrfs_chunk,
  1277. sub_stripes, 16);
  1278. BTRFS_SETGET_STACK_FUNCS(stack_stripe_devid, struct btrfs_stripe, devid, 64);
  1279. BTRFS_SETGET_STACK_FUNCS(stack_stripe_offset, struct btrfs_stripe, offset, 64);
  1280. static inline struct btrfs_stripe *btrfs_stripe_nr(struct btrfs_chunk *c,
  1281. int nr)
  1282. {
  1283. unsigned long offset = (unsigned long)c;
  1284. offset += offsetof(struct btrfs_chunk, stripe);
  1285. offset += nr * sizeof(struct btrfs_stripe);
  1286. return (struct btrfs_stripe *)offset;
  1287. }
  1288. static inline char *btrfs_stripe_dev_uuid_nr(struct btrfs_chunk *c, int nr)
  1289. {
  1290. return btrfs_stripe_dev_uuid(btrfs_stripe_nr(c, nr));
  1291. }
  1292. static inline u64 btrfs_stripe_offset_nr(struct extent_buffer *eb,
  1293. struct btrfs_chunk *c, int nr)
  1294. {
  1295. return btrfs_stripe_offset(eb, btrfs_stripe_nr(c, nr));
  1296. }
  1297. static inline u64 btrfs_stripe_devid_nr(struct extent_buffer *eb,
  1298. struct btrfs_chunk *c, int nr)
  1299. {
  1300. return btrfs_stripe_devid(eb, btrfs_stripe_nr(c, nr));
  1301. }
  1302. /* struct btrfs_block_group_item */
  1303. BTRFS_SETGET_STACK_FUNCS(block_group_used, struct btrfs_block_group_item,
  1304. used, 64);
  1305. BTRFS_SETGET_FUNCS(disk_block_group_used, struct btrfs_block_group_item,
  1306. used, 64);
  1307. BTRFS_SETGET_STACK_FUNCS(block_group_chunk_objectid,
  1308. struct btrfs_block_group_item, chunk_objectid, 64);
  1309. BTRFS_SETGET_FUNCS(disk_block_group_chunk_objectid,
  1310. struct btrfs_block_group_item, chunk_objectid, 64);
  1311. BTRFS_SETGET_FUNCS(disk_block_group_flags,
  1312. struct btrfs_block_group_item, flags, 64);
  1313. BTRFS_SETGET_STACK_FUNCS(block_group_flags,
  1314. struct btrfs_block_group_item, flags, 64);
  1315. /* struct btrfs_free_space_info */
  1316. BTRFS_SETGET_FUNCS(free_space_extent_count, struct btrfs_free_space_info,
  1317. extent_count, 32);
  1318. BTRFS_SETGET_FUNCS(free_space_flags, struct btrfs_free_space_info, flags, 32);
  1319. /* struct btrfs_inode_ref */
  1320. BTRFS_SETGET_FUNCS(inode_ref_name_len, struct btrfs_inode_ref, name_len, 16);
  1321. BTRFS_SETGET_FUNCS(inode_ref_index, struct btrfs_inode_ref, index, 64);
  1322. /* struct btrfs_inode_extref */
  1323. BTRFS_SETGET_FUNCS(inode_extref_parent, struct btrfs_inode_extref,
  1324. parent_objectid, 64);
  1325. BTRFS_SETGET_FUNCS(inode_extref_name_len, struct btrfs_inode_extref,
  1326. name_len, 16);
  1327. BTRFS_SETGET_FUNCS(inode_extref_index, struct btrfs_inode_extref, index, 64);
  1328. /* struct btrfs_inode_item */
  1329. BTRFS_SETGET_FUNCS(inode_generation, struct btrfs_inode_item, generation, 64);
  1330. BTRFS_SETGET_FUNCS(inode_sequence, struct btrfs_inode_item, sequence, 64);
  1331. BTRFS_SETGET_FUNCS(inode_transid, struct btrfs_inode_item, transid, 64);
  1332. BTRFS_SETGET_FUNCS(inode_size, struct btrfs_inode_item, size, 64);
  1333. BTRFS_SETGET_FUNCS(inode_nbytes, struct btrfs_inode_item, nbytes, 64);
  1334. BTRFS_SETGET_FUNCS(inode_block_group, struct btrfs_inode_item, block_group, 64);
  1335. BTRFS_SETGET_FUNCS(inode_nlink, struct btrfs_inode_item, nlink, 32);
  1336. BTRFS_SETGET_FUNCS(inode_uid, struct btrfs_inode_item, uid, 32);
  1337. BTRFS_SETGET_FUNCS(inode_gid, struct btrfs_inode_item, gid, 32);
  1338. BTRFS_SETGET_FUNCS(inode_mode, struct btrfs_inode_item, mode, 32);
  1339. BTRFS_SETGET_FUNCS(inode_rdev, struct btrfs_inode_item, rdev, 64);
  1340. BTRFS_SETGET_FUNCS(inode_flags, struct btrfs_inode_item, flags, 64);
  1341. BTRFS_SETGET_STACK_FUNCS(stack_inode_generation, struct btrfs_inode_item,
  1342. generation, 64);
  1343. BTRFS_SETGET_STACK_FUNCS(stack_inode_sequence, struct btrfs_inode_item,
  1344. sequence, 64);
  1345. BTRFS_SETGET_STACK_FUNCS(stack_inode_transid, struct btrfs_inode_item,
  1346. transid, 64);
  1347. BTRFS_SETGET_STACK_FUNCS(stack_inode_size, struct btrfs_inode_item, size, 64);
  1348. BTRFS_SETGET_STACK_FUNCS(stack_inode_nbytes, struct btrfs_inode_item,
  1349. nbytes, 64);
  1350. BTRFS_SETGET_STACK_FUNCS(stack_inode_block_group, struct btrfs_inode_item,
  1351. block_group, 64);
  1352. BTRFS_SETGET_STACK_FUNCS(stack_inode_nlink, struct btrfs_inode_item, nlink, 32);
  1353. BTRFS_SETGET_STACK_FUNCS(stack_inode_uid, struct btrfs_inode_item, uid, 32);
  1354. BTRFS_SETGET_STACK_FUNCS(stack_inode_gid, struct btrfs_inode_item, gid, 32);
  1355. BTRFS_SETGET_STACK_FUNCS(stack_inode_mode, struct btrfs_inode_item, mode, 32);
  1356. BTRFS_SETGET_STACK_FUNCS(stack_inode_rdev, struct btrfs_inode_item, rdev, 64);
  1357. BTRFS_SETGET_STACK_FUNCS(stack_inode_flags, struct btrfs_inode_item, flags, 64);
  1358. BTRFS_SETGET_FUNCS(timespec_sec, struct btrfs_timespec, sec, 64);
  1359. BTRFS_SETGET_FUNCS(timespec_nsec, struct btrfs_timespec, nsec, 32);
  1360. BTRFS_SETGET_STACK_FUNCS(stack_timespec_sec, struct btrfs_timespec, sec, 64);
  1361. BTRFS_SETGET_STACK_FUNCS(stack_timespec_nsec, struct btrfs_timespec, nsec, 32);
  1362. /* struct btrfs_dev_extent */
  1363. BTRFS_SETGET_FUNCS(dev_extent_chunk_tree, struct btrfs_dev_extent,
  1364. chunk_tree, 64);
  1365. BTRFS_SETGET_FUNCS(dev_extent_chunk_objectid, struct btrfs_dev_extent,
  1366. chunk_objectid, 64);
  1367. BTRFS_SETGET_FUNCS(dev_extent_chunk_offset, struct btrfs_dev_extent,
  1368. chunk_offset, 64);
  1369. BTRFS_SETGET_FUNCS(dev_extent_length, struct btrfs_dev_extent, length, 64);
  1370. static inline unsigned long btrfs_dev_extent_chunk_tree_uuid(struct btrfs_dev_extent *dev)
  1371. {
  1372. unsigned long ptr = offsetof(struct btrfs_dev_extent, chunk_tree_uuid);
  1373. return (unsigned long)dev + ptr;
  1374. }
  1375. BTRFS_SETGET_FUNCS(extent_refs, struct btrfs_extent_item, refs, 64);
  1376. BTRFS_SETGET_FUNCS(extent_generation, struct btrfs_extent_item,
  1377. generation, 64);
  1378. BTRFS_SETGET_FUNCS(extent_flags, struct btrfs_extent_item, flags, 64);
  1379. BTRFS_SETGET_FUNCS(extent_refs_v0, struct btrfs_extent_item_v0, refs, 32);
  1380. BTRFS_SETGET_FUNCS(tree_block_level, struct btrfs_tree_block_info, level, 8);
  1381. static inline void btrfs_tree_block_key(struct extent_buffer *eb,
  1382. struct btrfs_tree_block_info *item,
  1383. struct btrfs_disk_key *key)
  1384. {
  1385. read_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
  1386. }
  1387. static inline void btrfs_set_tree_block_key(struct extent_buffer *eb,
  1388. struct btrfs_tree_block_info *item,
  1389. struct btrfs_disk_key *key)
  1390. {
  1391. write_eb_member(eb, item, struct btrfs_tree_block_info, key, key);
  1392. }
  1393. BTRFS_SETGET_FUNCS(extent_data_ref_root, struct btrfs_extent_data_ref,
  1394. root, 64);
  1395. BTRFS_SETGET_FUNCS(extent_data_ref_objectid, struct btrfs_extent_data_ref,
  1396. objectid, 64);
  1397. BTRFS_SETGET_FUNCS(extent_data_ref_offset, struct btrfs_extent_data_ref,
  1398. offset, 64);
  1399. BTRFS_SETGET_FUNCS(extent_data_ref_count, struct btrfs_extent_data_ref,
  1400. count, 32);
  1401. BTRFS_SETGET_FUNCS(shared_data_ref_count, struct btrfs_shared_data_ref,
  1402. count, 32);
  1403. BTRFS_SETGET_FUNCS(extent_inline_ref_type, struct btrfs_extent_inline_ref,
  1404. type, 8);
  1405. BTRFS_SETGET_FUNCS(extent_inline_ref_offset, struct btrfs_extent_inline_ref,
  1406. offset, 64);
  1407. static inline u32 btrfs_extent_inline_ref_size(int type)
  1408. {
  1409. if (type == BTRFS_TREE_BLOCK_REF_KEY ||
  1410. type == BTRFS_SHARED_BLOCK_REF_KEY)
  1411. return sizeof(struct btrfs_extent_inline_ref);
  1412. if (type == BTRFS_SHARED_DATA_REF_KEY)
  1413. return sizeof(struct btrfs_shared_data_ref) +
  1414. sizeof(struct btrfs_extent_inline_ref);
  1415. if (type == BTRFS_EXTENT_DATA_REF_KEY)
  1416. return sizeof(struct btrfs_extent_data_ref) +
  1417. offsetof(struct btrfs_extent_inline_ref, offset);
  1418. return 0;
  1419. }
  1420. BTRFS_SETGET_FUNCS(ref_root_v0, struct btrfs_extent_ref_v0, root, 64);
  1421. BTRFS_SETGET_FUNCS(ref_generation_v0, struct btrfs_extent_ref_v0,
  1422. generation, 64);
  1423. BTRFS_SETGET_FUNCS(ref_objectid_v0, struct btrfs_extent_ref_v0, objectid, 64);
  1424. BTRFS_SETGET_FUNCS(ref_count_v0, struct btrfs_extent_ref_v0, count, 32);
  1425. /* struct btrfs_node */
  1426. BTRFS_SETGET_FUNCS(key_blockptr, struct btrfs_key_ptr, blockptr, 64);
  1427. BTRFS_SETGET_FUNCS(key_generation, struct btrfs_key_ptr, generation, 64);
  1428. BTRFS_SETGET_STACK_FUNCS(stack_key_blockptr, struct btrfs_key_ptr,
  1429. blockptr, 64);
  1430. BTRFS_SETGET_STACK_FUNCS(stack_key_generation, struct btrfs_key_ptr,
  1431. generation, 64);
  1432. static inline u64 btrfs_node_blockptr(struct extent_buffer *eb, int nr)
  1433. {
  1434. unsigned long ptr;
  1435. ptr = offsetof(struct btrfs_node, ptrs) +
  1436. sizeof(struct btrfs_key_ptr) * nr;
  1437. return btrfs_key_blockptr(eb, (struct btrfs_key_ptr *)ptr);
  1438. }
  1439. static inline void btrfs_set_node_blockptr(struct extent_buffer *eb,
  1440. int nr, u64 val)
  1441. {
  1442. unsigned long ptr;
  1443. ptr = offsetof(struct btrfs_node, ptrs) +
  1444. sizeof(struct btrfs_key_ptr) * nr;
  1445. btrfs_set_key_blockptr(eb, (struct btrfs_key_ptr *)ptr, val);
  1446. }
  1447. static inline u64 btrfs_node_ptr_generation(struct extent_buffer *eb, int nr)
  1448. {
  1449. unsigned long ptr;
  1450. ptr = offsetof(struct btrfs_node, ptrs) +
  1451. sizeof(struct btrfs_key_ptr) * nr;
  1452. return btrfs_key_generation(eb, (struct btrfs_key_ptr *)ptr);
  1453. }
  1454. static inline void btrfs_set_node_ptr_generation(struct extent_buffer *eb,
  1455. int nr, u64 val)
  1456. {
  1457. unsigned long ptr;
  1458. ptr = offsetof(struct btrfs_node, ptrs) +
  1459. sizeof(struct btrfs_key_ptr) * nr;
  1460. btrfs_set_key_generation(eb, (struct btrfs_key_ptr *)ptr, val);
  1461. }
  1462. static inline unsigned long btrfs_node_key_ptr_offset(int nr)
  1463. {
  1464. return offsetof(struct btrfs_node, ptrs) +
  1465. sizeof(struct btrfs_key_ptr) * nr;
  1466. }
  1467. void btrfs_node_key(const struct extent_buffer *eb,
  1468. struct btrfs_disk_key *disk_key, int nr);
  1469. static inline void btrfs_set_node_key(struct extent_buffer *eb,
  1470. struct btrfs_disk_key *disk_key, int nr)
  1471. {
  1472. unsigned long ptr;
  1473. ptr = btrfs_node_key_ptr_offset(nr);
  1474. write_eb_member(eb, (struct btrfs_key_ptr *)ptr,
  1475. struct btrfs_key_ptr, key, disk_key);
  1476. }
  1477. /* struct btrfs_item */
  1478. BTRFS_SETGET_FUNCS(item_offset, struct btrfs_item, offset, 32);
  1479. BTRFS_SETGET_FUNCS(item_size, struct btrfs_item, size, 32);
  1480. BTRFS_SETGET_STACK_FUNCS(stack_item_offset, struct btrfs_item, offset, 32);
  1481. BTRFS_SETGET_STACK_FUNCS(stack_item_size, struct btrfs_item, size, 32);
  1482. static inline unsigned long btrfs_item_nr_offset(int nr)
  1483. {
  1484. return offsetof(struct btrfs_leaf, items) +
  1485. sizeof(struct btrfs_item) * nr;
  1486. }
  1487. static inline struct btrfs_item *btrfs_item_nr(int nr)
  1488. {
  1489. return (struct btrfs_item *)btrfs_item_nr_offset(nr);
  1490. }
  1491. static inline u32 btrfs_item_end(const struct extent_buffer *eb,
  1492. struct btrfs_item *item)
  1493. {
  1494. return btrfs_item_offset(eb, item) + btrfs_item_size(eb, item);
  1495. }
  1496. static inline u32 btrfs_item_end_nr(const struct extent_buffer *eb, int nr)
  1497. {
  1498. return btrfs_item_end(eb, btrfs_item_nr(nr));
  1499. }
  1500. static inline u32 btrfs_item_offset_nr(const struct extent_buffer *eb, int nr)
  1501. {
  1502. return btrfs_item_offset(eb, btrfs_item_nr(nr));
  1503. }
  1504. static inline u32 btrfs_item_size_nr(const struct extent_buffer *eb, int nr)
  1505. {
  1506. return btrfs_item_size(eb, btrfs_item_nr(nr));
  1507. }
  1508. static inline void btrfs_item_key(const struct extent_buffer *eb,
  1509. struct btrfs_disk_key *disk_key, int nr)
  1510. {
  1511. struct btrfs_item *item = btrfs_item_nr(nr);
  1512. read_eb_member(eb, item, struct btrfs_item, key, disk_key);
  1513. }
  1514. static inline void btrfs_set_item_key(struct extent_buffer *eb,
  1515. struct btrfs_disk_key *disk_key, int nr)
  1516. {
  1517. struct btrfs_item *item = btrfs_item_nr(nr);
  1518. write_eb_member(eb, item, struct btrfs_item, key, disk_key);
  1519. }
  1520. BTRFS_SETGET_FUNCS(dir_log_end, struct btrfs_dir_log_item, end, 64);
  1521. /*
  1522. * struct btrfs_root_ref
  1523. */
  1524. BTRFS_SETGET_FUNCS(root_ref_dirid, struct btrfs_root_ref, dirid, 64);
  1525. BTRFS_SETGET_FUNCS(root_ref_sequence, struct btrfs_root_ref, sequence, 64);
  1526. BTRFS_SETGET_FUNCS(root_ref_name_len, struct btrfs_root_ref, name_len, 16);
  1527. /* struct btrfs_dir_item */
  1528. BTRFS_SETGET_FUNCS(dir_data_len, struct btrfs_dir_item, data_len, 16);
  1529. BTRFS_SETGET_FUNCS(dir_type, struct btrfs_dir_item, type, 8);
  1530. BTRFS_SETGET_FUNCS(dir_name_len, struct btrfs_dir_item, name_len, 16);
  1531. BTRFS_SETGET_FUNCS(dir_transid, struct btrfs_dir_item, transid, 64);
  1532. BTRFS_SETGET_STACK_FUNCS(stack_dir_type, struct btrfs_dir_item, type, 8);
  1533. BTRFS_SETGET_STACK_FUNCS(stack_dir_data_len, struct btrfs_dir_item,
  1534. data_len, 16);
  1535. BTRFS_SETGET_STACK_FUNCS(stack_dir_name_len, struct btrfs_dir_item,
  1536. name_len, 16);
  1537. BTRFS_SETGET_STACK_FUNCS(stack_dir_transid, struct btrfs_dir_item,
  1538. transid, 64);
  1539. static inline void btrfs_dir_item_key(const struct extent_buffer *eb,
  1540. const struct btrfs_dir_item *item,
  1541. struct btrfs_disk_key *key)
  1542. {
  1543. read_eb_member(eb, item, struct btrfs_dir_item, location, key);
  1544. }
  1545. static inline void btrfs_set_dir_item_key(struct extent_buffer *eb,
  1546. struct btrfs_dir_item *item,
  1547. const struct btrfs_disk_key *key)
  1548. {
  1549. write_eb_member(eb, item, struct btrfs_dir_item, location, key);
  1550. }
  1551. BTRFS_SETGET_FUNCS(free_space_entries, struct btrfs_free_space_header,
  1552. num_entries, 64);
  1553. BTRFS_SETGET_FUNCS(free_space_bitmaps, struct btrfs_free_space_header,
  1554. num_bitmaps, 64);
  1555. BTRFS_SETGET_FUNCS(free_space_generation, struct btrfs_free_space_header,
  1556. generation, 64);
  1557. static inline void btrfs_free_space_key(const struct extent_buffer *eb,
  1558. const struct btrfs_free_space_header *h,
  1559. struct btrfs_disk_key *key)
  1560. {
  1561. read_eb_member(eb, h, struct btrfs_free_space_header, location, key);
  1562. }
  1563. static inline void btrfs_set_free_space_key(struct extent_buffer *eb,
  1564. struct btrfs_free_space_header *h,
  1565. const struct btrfs_disk_key *key)
  1566. {
  1567. write_eb_member(eb, h, struct btrfs_free_space_header, location, key);
  1568. }
  1569. /* struct btrfs_disk_key */
  1570. BTRFS_SETGET_STACK_FUNCS(disk_key_objectid, struct btrfs_disk_key,
  1571. objectid, 64);
  1572. BTRFS_SETGET_STACK_FUNCS(disk_key_offset, struct btrfs_disk_key, offset, 64);
  1573. BTRFS_SETGET_STACK_FUNCS(disk_key_type, struct btrfs_disk_key, type, 8);
  1574. static inline void btrfs_disk_key_to_cpu(struct btrfs_key *cpu,
  1575. const struct btrfs_disk_key *disk)
  1576. {
  1577. cpu->offset = le64_to_cpu(disk->offset);
  1578. cpu->type = disk->type;
  1579. cpu->objectid = le64_to_cpu(disk->objectid);
  1580. }
  1581. static inline void btrfs_cpu_key_to_disk(struct btrfs_disk_key *disk,
  1582. const struct btrfs_key *cpu)
  1583. {
  1584. disk->offset = cpu_to_le64(cpu->offset);
  1585. disk->type = cpu->type;
  1586. disk->objectid = cpu_to_le64(cpu->objectid);
  1587. }
  1588. static inline void btrfs_node_key_to_cpu(const struct extent_buffer *eb,
  1589. struct btrfs_key *key, int nr)
  1590. {
  1591. struct btrfs_disk_key disk_key;
  1592. btrfs_node_key(eb, &disk_key, nr);
  1593. btrfs_disk_key_to_cpu(key, &disk_key);
  1594. }
  1595. static inline void btrfs_item_key_to_cpu(const struct extent_buffer *eb,
  1596. struct btrfs_key *key, int nr)
  1597. {
  1598. struct btrfs_disk_key disk_key;
  1599. btrfs_item_key(eb, &disk_key, nr);
  1600. btrfs_disk_key_to_cpu(key, &disk_key);
  1601. }
  1602. static inline void btrfs_dir_item_key_to_cpu(const struct extent_buffer *eb,
  1603. const struct btrfs_dir_item *item,
  1604. struct btrfs_key *key)
  1605. {
  1606. struct btrfs_disk_key disk_key;
  1607. btrfs_dir_item_key(eb, item, &disk_key);
  1608. btrfs_disk_key_to_cpu(key, &disk_key);
  1609. }
  1610. /* struct btrfs_header */
  1611. BTRFS_SETGET_HEADER_FUNCS(header_bytenr, struct btrfs_header, bytenr, 64);
  1612. BTRFS_SETGET_HEADER_FUNCS(header_generation, struct btrfs_header,
  1613. generation, 64);
  1614. BTRFS_SETGET_HEADER_FUNCS(header_owner, struct btrfs_header, owner, 64);
  1615. BTRFS_SETGET_HEADER_FUNCS(header_nritems, struct btrfs_header, nritems, 32);
  1616. BTRFS_SETGET_HEADER_FUNCS(header_flags, struct btrfs_header, flags, 64);
  1617. BTRFS_SETGET_HEADER_FUNCS(header_level, struct btrfs_header, level, 8);
  1618. BTRFS_SETGET_STACK_FUNCS(stack_header_generation, struct btrfs_header,
  1619. generation, 64);
  1620. BTRFS_SETGET_STACK_FUNCS(stack_header_owner, struct btrfs_header, owner, 64);
  1621. BTRFS_SETGET_STACK_FUNCS(stack_header_nritems, struct btrfs_header,
  1622. nritems, 32);
  1623. BTRFS_SETGET_STACK_FUNCS(stack_header_bytenr, struct btrfs_header, bytenr, 64);
  1624. static inline int btrfs_header_flag(const struct extent_buffer *eb, u64 flag)
  1625. {
  1626. return (btrfs_header_flags(eb) & flag) == flag;
  1627. }
  1628. static inline void btrfs_set_header_flag(struct extent_buffer *eb, u64 flag)
  1629. {
  1630. u64 flags = btrfs_header_flags(eb);
  1631. btrfs_set_header_flags(eb, flags | flag);
  1632. }
  1633. static inline void btrfs_clear_header_flag(struct extent_buffer *eb, u64 flag)
  1634. {
  1635. u64 flags = btrfs_header_flags(eb);
  1636. btrfs_set_header_flags(eb, flags & ~flag);
  1637. }
  1638. static inline int btrfs_header_backref_rev(const struct extent_buffer *eb)
  1639. {
  1640. u64 flags = btrfs_header_flags(eb);
  1641. return flags >> BTRFS_BACKREF_REV_SHIFT;
  1642. }
  1643. static inline void btrfs_set_header_backref_rev(struct extent_buffer *eb,
  1644. int rev)
  1645. {
  1646. u64 flags = btrfs_header_flags(eb);
  1647. flags &= ~BTRFS_BACKREF_REV_MASK;
  1648. flags |= (u64)rev << BTRFS_BACKREF_REV_SHIFT;
  1649. btrfs_set_header_flags(eb, flags);
  1650. }
  1651. static inline unsigned long btrfs_header_fsid(void)
  1652. {
  1653. return offsetof(struct btrfs_header, fsid);
  1654. }
  1655. static inline unsigned long btrfs_header_chunk_tree_uuid(const struct extent_buffer *eb)
  1656. {
  1657. return offsetof(struct btrfs_header, chunk_tree_uuid);
  1658. }
  1659. static inline int btrfs_is_leaf(const struct extent_buffer *eb)
  1660. {
  1661. return btrfs_header_level(eb) == 0;
  1662. }
  1663. /* struct btrfs_root_item */
  1664. BTRFS_SETGET_FUNCS(disk_root_generation, struct btrfs_root_item,
  1665. generation, 64);
  1666. BTRFS_SETGET_FUNCS(disk_root_refs, struct btrfs_root_item, refs, 32);
  1667. BTRFS_SETGET_FUNCS(disk_root_bytenr, struct btrfs_root_item, bytenr, 64);
  1668. BTRFS_SETGET_FUNCS(disk_root_level, struct btrfs_root_item, level, 8);
  1669. BTRFS_SETGET_STACK_FUNCS(root_generation, struct btrfs_root_item,
  1670. generation, 64);
  1671. BTRFS_SETGET_STACK_FUNCS(root_bytenr, struct btrfs_root_item, bytenr, 64);
  1672. BTRFS_SETGET_STACK_FUNCS(root_level, struct btrfs_root_item, level, 8);
  1673. BTRFS_SETGET_STACK_FUNCS(root_dirid, struct btrfs_root_item, root_dirid, 64);
  1674. BTRFS_SETGET_STACK_FUNCS(root_refs, struct btrfs_root_item, refs, 32);
  1675. BTRFS_SETGET_STACK_FUNCS(root_flags, struct btrfs_root_item, flags, 64);
  1676. BTRFS_SETGET_STACK_FUNCS(root_used, struct btrfs_root_item, bytes_used, 64);
  1677. BTRFS_SETGET_STACK_FUNCS(root_limit, struct btrfs_root_item, byte_limit, 64);
  1678. BTRFS_SETGET_STACK_FUNCS(root_last_snapshot, struct btrfs_root_item,
  1679. last_snapshot, 64);
  1680. BTRFS_SETGET_STACK_FUNCS(root_generation_v2, struct btrfs_root_item,
  1681. generation_v2, 64);
  1682. BTRFS_SETGET_STACK_FUNCS(root_ctransid, struct btrfs_root_item,
  1683. ctransid, 64);
  1684. BTRFS_SETGET_STACK_FUNCS(root_otransid, struct btrfs_root_item,
  1685. otransid, 64);
  1686. BTRFS_SETGET_STACK_FUNCS(root_stransid, struct btrfs_root_item,
  1687. stransid, 64);
  1688. BTRFS_SETGET_STACK_FUNCS(root_rtransid, struct btrfs_root_item,
  1689. rtransid, 64);
  1690. static inline bool btrfs_root_readonly(const struct btrfs_root *root)
  1691. {
  1692. return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_RDONLY)) != 0;
  1693. }
  1694. static inline bool btrfs_root_dead(const struct btrfs_root *root)
  1695. {
  1696. return (root->root_item.flags & cpu_to_le64(BTRFS_ROOT_SUBVOL_DEAD)) != 0;
  1697. }
  1698. /* struct btrfs_root_backup */
  1699. BTRFS_SETGET_STACK_FUNCS(backup_tree_root, struct btrfs_root_backup,
  1700. tree_root, 64);
  1701. BTRFS_SETGET_STACK_FUNCS(backup_tree_root_gen, struct btrfs_root_backup,
  1702. tree_root_gen, 64);
  1703. BTRFS_SETGET_STACK_FUNCS(backup_tree_root_level, struct btrfs_root_backup,
  1704. tree_root_level, 8);
  1705. BTRFS_SETGET_STACK_FUNCS(backup_chunk_root, struct btrfs_root_backup,
  1706. chunk_root, 64);
  1707. BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_gen, struct btrfs_root_backup,
  1708. chunk_root_gen, 64);
  1709. BTRFS_SETGET_STACK_FUNCS(backup_chunk_root_level, struct btrfs_root_backup,
  1710. chunk_root_level, 8);
  1711. BTRFS_SETGET_STACK_FUNCS(backup_extent_root, struct btrfs_root_backup,
  1712. extent_root, 64);
  1713. BTRFS_SETGET_STACK_FUNCS(backup_extent_root_gen, struct btrfs_root_backup,
  1714. extent_root_gen, 64);
  1715. BTRFS_SETGET_STACK_FUNCS(backup_extent_root_level, struct btrfs_root_backup,
  1716. extent_root_level, 8);
  1717. BTRFS_SETGET_STACK_FUNCS(backup_fs_root, struct btrfs_root_backup,
  1718. fs_root, 64);
  1719. BTRFS_SETGET_STACK_FUNCS(backup_fs_root_gen, struct btrfs_root_backup,
  1720. fs_root_gen, 64);
  1721. BTRFS_SETGET_STACK_FUNCS(backup_fs_root_level, struct btrfs_root_backup,
  1722. fs_root_level, 8);
  1723. BTRFS_SETGET_STACK_FUNCS(backup_dev_root, struct btrfs_root_backup,
  1724. dev_root, 64);
  1725. BTRFS_SETGET_STACK_FUNCS(backup_dev_root_gen, struct btrfs_root_backup,
  1726. dev_root_gen, 64);
  1727. BTRFS_SETGET_STACK_FUNCS(backup_dev_root_level, struct btrfs_root_backup,
  1728. dev_root_level, 8);
  1729. BTRFS_SETGET_STACK_FUNCS(backup_csum_root, struct btrfs_root_backup,
  1730. csum_root, 64);
  1731. BTRFS_SETGET_STACK_FUNCS(backup_csum_root_gen, struct btrfs_root_backup,
  1732. csum_root_gen, 64);
  1733. BTRFS_SETGET_STACK_FUNCS(backup_csum_root_level, struct btrfs_root_backup,
  1734. csum_root_level, 8);
  1735. BTRFS_SETGET_STACK_FUNCS(backup_total_bytes, struct btrfs_root_backup,
  1736. total_bytes, 64);
  1737. BTRFS_SETGET_STACK_FUNCS(backup_bytes_used, struct btrfs_root_backup,
  1738. bytes_used, 64);
  1739. BTRFS_SETGET_STACK_FUNCS(backup_num_devices, struct btrfs_root_backup,
  1740. num_devices, 64);
  1741. /* struct btrfs_balance_item */
  1742. BTRFS_SETGET_FUNCS(balance_flags, struct btrfs_balance_item, flags, 64);
  1743. static inline void btrfs_balance_data(const struct extent_buffer *eb,
  1744. const struct btrfs_balance_item *bi,
  1745. struct btrfs_disk_balance_args *ba)
  1746. {
  1747. read_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
  1748. }
  1749. static inline void btrfs_set_balance_data(struct extent_buffer *eb,
  1750. struct btrfs_balance_item *bi,
  1751. const struct btrfs_disk_balance_args *ba)
  1752. {
  1753. write_eb_member(eb, bi, struct btrfs_balance_item, data, ba);
  1754. }
  1755. static inline void btrfs_balance_meta(const struct extent_buffer *eb,
  1756. const struct btrfs_balance_item *bi,
  1757. struct btrfs_disk_balance_args *ba)
  1758. {
  1759. read_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
  1760. }
  1761. static inline void btrfs_set_balance_meta(struct extent_buffer *eb,
  1762. struct btrfs_balance_item *bi,
  1763. const struct btrfs_disk_balance_args *ba)
  1764. {
  1765. write_eb_member(eb, bi, struct btrfs_balance_item, meta, ba);
  1766. }
  1767. static inline void btrfs_balance_sys(const struct extent_buffer *eb,
  1768. const struct btrfs_balance_item *bi,
  1769. struct btrfs_disk_balance_args *ba)
  1770. {
  1771. read_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
  1772. }
  1773. static inline void btrfs_set_balance_sys(struct extent_buffer *eb,
  1774. struct btrfs_balance_item *bi,
  1775. const struct btrfs_disk_balance_args *ba)
  1776. {
  1777. write_eb_member(eb, bi, struct btrfs_balance_item, sys, ba);
  1778. }
  1779. static inline void
  1780. btrfs_disk_balance_args_to_cpu(struct btrfs_balance_args *cpu,
  1781. const struct btrfs_disk_balance_args *disk)
  1782. {
  1783. memset(cpu, 0, sizeof(*cpu));
  1784. cpu->profiles = le64_to_cpu(disk->profiles);
  1785. cpu->usage = le64_to_cpu(disk->usage);
  1786. cpu->devid = le64_to_cpu(disk->devid);
  1787. cpu->pstart = le64_to_cpu(disk->pstart);
  1788. cpu->pend = le64_to_cpu(disk->pend);
  1789. cpu->vstart = le64_to_cpu(disk->vstart);
  1790. cpu->vend = le64_to_cpu(disk->vend);
  1791. cpu->target = le64_to_cpu(disk->target);
  1792. cpu->flags = le64_to_cpu(disk->flags);
  1793. cpu->limit = le64_to_cpu(disk->limit);
  1794. cpu->stripes_min = le32_to_cpu(disk->stripes_min);
  1795. cpu->stripes_max = le32_to_cpu(disk->stripes_max);
  1796. }
  1797. static inline void
  1798. btrfs_cpu_balance_args_to_disk(struct btrfs_disk_balance_args *disk,
  1799. const struct btrfs_balance_args *cpu)
  1800. {
  1801. memset(disk, 0, sizeof(*disk));
  1802. disk->profiles = cpu_to_le64(cpu->profiles);
  1803. disk->usage = cpu_to_le64(cpu->usage);
  1804. disk->devid = cpu_to_le64(cpu->devid);
  1805. disk->pstart = cpu_to_le64(cpu->pstart);
  1806. disk->pend = cpu_to_le64(cpu->pend);
  1807. disk->vstart = cpu_to_le64(cpu->vstart);
  1808. disk->vend = cpu_to_le64(cpu->vend);
  1809. disk->target = cpu_to_le64(cpu->target);
  1810. disk->flags = cpu_to_le64(cpu->flags);
  1811. disk->limit = cpu_to_le64(cpu->limit);
  1812. disk->stripes_min = cpu_to_le32(cpu->stripes_min);
  1813. disk->stripes_max = cpu_to_le32(cpu->stripes_max);
  1814. }
  1815. /* struct btrfs_super_block */
  1816. BTRFS_SETGET_STACK_FUNCS(super_bytenr, struct btrfs_super_block, bytenr, 64);
  1817. BTRFS_SETGET_STACK_FUNCS(super_flags, struct btrfs_super_block, flags, 64);
  1818. BTRFS_SETGET_STACK_FUNCS(super_generation, struct btrfs_super_block,
  1819. generation, 64);
  1820. BTRFS_SETGET_STACK_FUNCS(super_root, struct btrfs_super_block, root, 64);
  1821. BTRFS_SETGET_STACK_FUNCS(super_sys_array_size,
  1822. struct btrfs_super_block, sys_chunk_array_size, 32);
  1823. BTRFS_SETGET_STACK_FUNCS(super_chunk_root_generation,
  1824. struct btrfs_super_block, chunk_root_generation, 64);
  1825. BTRFS_SETGET_STACK_FUNCS(super_root_level, struct btrfs_super_block,
  1826. root_level, 8);
  1827. BTRFS_SETGET_STACK_FUNCS(super_chunk_root, struct btrfs_super_block,
  1828. chunk_root, 64);
  1829. BTRFS_SETGET_STACK_FUNCS(super_chunk_root_level, struct btrfs_super_block,
  1830. chunk_root_level, 8);
  1831. BTRFS_SETGET_STACK_FUNCS(super_log_root, struct btrfs_super_block,
  1832. log_root, 64);
  1833. BTRFS_SETGET_STACK_FUNCS(super_log_root_transid, struct btrfs_super_block,
  1834. log_root_transid, 64);
  1835. BTRFS_SETGET_STACK_FUNCS(super_log_root_level, struct btrfs_super_block,
  1836. log_root_level, 8);
  1837. BTRFS_SETGET_STACK_FUNCS(super_total_bytes, struct btrfs_super_block,
  1838. total_bytes, 64);
  1839. BTRFS_SETGET_STACK_FUNCS(super_bytes_used, struct btrfs_super_block,
  1840. bytes_used, 64);
  1841. BTRFS_SETGET_STACK_FUNCS(super_sectorsize, struct btrfs_super_block,
  1842. sectorsize, 32);
  1843. BTRFS_SETGET_STACK_FUNCS(super_nodesize, struct btrfs_super_block,
  1844. nodesize, 32);
  1845. BTRFS_SETGET_STACK_FUNCS(super_stripesize, struct btrfs_super_block,
  1846. stripesize, 32);
  1847. BTRFS_SETGET_STACK_FUNCS(super_root_dir, struct btrfs_super_block,
  1848. root_dir_objectid, 64);
  1849. BTRFS_SETGET_STACK_FUNCS(super_num_devices, struct btrfs_super_block,
  1850. num_devices, 64);
  1851. BTRFS_SETGET_STACK_FUNCS(super_compat_flags, struct btrfs_super_block,
  1852. compat_flags, 64);
  1853. BTRFS_SETGET_STACK_FUNCS(super_compat_ro_flags, struct btrfs_super_block,
  1854. compat_ro_flags, 64);
  1855. BTRFS_SETGET_STACK_FUNCS(super_incompat_flags, struct btrfs_super_block,
  1856. incompat_flags, 64);
  1857. BTRFS_SETGET_STACK_FUNCS(super_csum_type, struct btrfs_super_block,
  1858. csum_type, 16);
  1859. BTRFS_SETGET_STACK_FUNCS(super_cache_generation, struct btrfs_super_block,
  1860. cache_generation, 64);
  1861. BTRFS_SETGET_STACK_FUNCS(super_magic, struct btrfs_super_block, magic, 64);
  1862. BTRFS_SETGET_STACK_FUNCS(super_uuid_tree_generation, struct btrfs_super_block,
  1863. uuid_tree_generation, 64);
  1864. int btrfs_super_csum_size(const struct btrfs_super_block *s);
  1865. const char *btrfs_super_csum_name(u16 csum_type);
  1866. /*
  1867. * The leaf data grows from end-to-front in the node.
  1868. * this returns the address of the start of the last item,
  1869. * which is the stop of the leaf data stack
  1870. */
  1871. static inline unsigned int leaf_data_end(const struct extent_buffer *leaf)
  1872. {
  1873. u32 nr = btrfs_header_nritems(leaf);
  1874. if (nr == 0)
  1875. return BTRFS_LEAF_DATA_SIZE(leaf->fs_info);
  1876. return btrfs_item_offset_nr(leaf, nr - 1);
  1877. }
  1878. /* struct btrfs_file_extent_item */
  1879. BTRFS_SETGET_FUNCS(file_extent_type, struct btrfs_file_extent_item, type, 8);
  1880. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_bytenr,
  1881. struct btrfs_file_extent_item, disk_bytenr, 64);
  1882. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_offset,
  1883. struct btrfs_file_extent_item, offset, 64);
  1884. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_generation,
  1885. struct btrfs_file_extent_item, generation, 64);
  1886. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_num_bytes,
  1887. struct btrfs_file_extent_item, num_bytes, 64);
  1888. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_disk_num_bytes,
  1889. struct btrfs_file_extent_item, disk_num_bytes, 64);
  1890. BTRFS_SETGET_STACK_FUNCS(stack_file_extent_compression,
  1891. struct btrfs_file_extent_item, compression, 8);
  1892. static inline unsigned long
  1893. btrfs_file_extent_inline_start(const struct btrfs_file_extent_item *e)
  1894. {
  1895. return (unsigned long)e + BTRFS_FILE_EXTENT_INLINE_DATA_START;
  1896. }
  1897. static inline u32 btrfs_file_extent_calc_inline_size(u32 datasize)
  1898. {
  1899. return BTRFS_FILE_EXTENT_INLINE_DATA_START + datasize;
  1900. }
  1901. BTRFS_SETGET_FUNCS(file_extent_disk_bytenr, struct btrfs_file_extent_item,
  1902. disk_bytenr, 64);
  1903. BTRFS_SETGET_FUNCS(file_extent_generation, struct btrfs_file_extent_item,
  1904. generation, 64);
  1905. BTRFS_SETGET_FUNCS(file_extent_disk_num_bytes, struct btrfs_file_extent_item,
  1906. disk_num_bytes, 64);
  1907. BTRFS_SETGET_FUNCS(file_extent_offset, struct btrfs_file_extent_item,
  1908. offset, 64);
  1909. BTRFS_SETGET_FUNCS(file_extent_num_bytes, struct btrfs_file_extent_item,
  1910. num_bytes, 64);
  1911. BTRFS_SETGET_FUNCS(file_extent_ram_bytes, struct btrfs_file_extent_item,
  1912. ram_bytes, 64);
  1913. BTRFS_SETGET_FUNCS(file_extent_compression, struct btrfs_file_extent_item,
  1914. compression, 8);
  1915. BTRFS_SETGET_FUNCS(file_extent_encryption, struct btrfs_file_extent_item,
  1916. encryption, 8);
  1917. BTRFS_SETGET_FUNCS(file_extent_other_encoding, struct btrfs_file_extent_item,
  1918. other_encoding, 16);
  1919. /*
  1920. * this returns the number of bytes used by the item on disk, minus the
  1921. * size of any extent headers. If a file is compressed on disk, this is
  1922. * the compressed size
  1923. */
  1924. static inline u32 btrfs_file_extent_inline_item_len(
  1925. const struct extent_buffer *eb,
  1926. struct btrfs_item *e)
  1927. {
  1928. return btrfs_item_size(eb, e) - BTRFS_FILE_EXTENT_INLINE_DATA_START;
  1929. }
  1930. /* btrfs_qgroup_status_item */
  1931. BTRFS_SETGET_FUNCS(qgroup_status_generation, struct btrfs_qgroup_status_item,
  1932. generation, 64);
  1933. BTRFS_SETGET_FUNCS(qgroup_status_version, struct btrfs_qgroup_status_item,
  1934. version, 64);
  1935. BTRFS_SETGET_FUNCS(qgroup_status_flags, struct btrfs_qgroup_status_item,
  1936. flags, 64);
  1937. BTRFS_SETGET_FUNCS(qgroup_status_rescan, struct btrfs_qgroup_status_item,
  1938. rescan, 64);
  1939. /* btrfs_qgroup_info_item */
  1940. BTRFS_SETGET_FUNCS(qgroup_info_generation, struct btrfs_qgroup_info_item,
  1941. generation, 64);
  1942. BTRFS_SETGET_FUNCS(qgroup_info_rfer, struct btrfs_qgroup_info_item, rfer, 64);
  1943. BTRFS_SETGET_FUNCS(qgroup_info_rfer_cmpr, struct btrfs_qgroup_info_item,
  1944. rfer_cmpr, 64);
  1945. BTRFS_SETGET_FUNCS(qgroup_info_excl, struct btrfs_qgroup_info_item, excl, 64);
  1946. BTRFS_SETGET_FUNCS(qgroup_info_excl_cmpr, struct btrfs_qgroup_info_item,
  1947. excl_cmpr, 64);
  1948. BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_generation,
  1949. struct btrfs_qgroup_info_item, generation, 64);
  1950. BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer, struct btrfs_qgroup_info_item,
  1951. rfer, 64);
  1952. BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_rfer_cmpr,
  1953. struct btrfs_qgroup_info_item, rfer_cmpr, 64);
  1954. BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl, struct btrfs_qgroup_info_item,
  1955. excl, 64);
  1956. BTRFS_SETGET_STACK_FUNCS(stack_qgroup_info_excl_cmpr,
  1957. struct btrfs_qgroup_info_item, excl_cmpr, 64);
  1958. /* btrfs_qgroup_limit_item */
  1959. BTRFS_SETGET_FUNCS(qgroup_limit_flags, struct btrfs_qgroup_limit_item,
  1960. flags, 64);
  1961. BTRFS_SETGET_FUNCS(qgroup_limit_max_rfer, struct btrfs_qgroup_limit_item,
  1962. max_rfer, 64);
  1963. BTRFS_SETGET_FUNCS(qgroup_limit_max_excl, struct btrfs_qgroup_limit_item,
  1964. max_excl, 64);
  1965. BTRFS_SETGET_FUNCS(qgroup_limit_rsv_rfer, struct btrfs_qgroup_limit_item,
  1966. rsv_rfer, 64);
  1967. BTRFS_SETGET_FUNCS(qgroup_limit_rsv_excl, struct btrfs_qgroup_limit_item,
  1968. rsv_excl, 64);
  1969. /* btrfs_dev_replace_item */
  1970. BTRFS_SETGET_FUNCS(dev_replace_src_devid,
  1971. struct btrfs_dev_replace_item, src_devid, 64);
  1972. BTRFS_SETGET_FUNCS(dev_replace_cont_reading_from_srcdev_mode,
  1973. struct btrfs_dev_replace_item, cont_reading_from_srcdev_mode,
  1974. 64);
  1975. BTRFS_SETGET_FUNCS(dev_replace_replace_state, struct btrfs_dev_replace_item,
  1976. replace_state, 64);
  1977. BTRFS_SETGET_FUNCS(dev_replace_time_started, struct btrfs_dev_replace_item,
  1978. time_started, 64);
  1979. BTRFS_SETGET_FUNCS(dev_replace_time_stopped, struct btrfs_dev_replace_item,
  1980. time_stopped, 64);
  1981. BTRFS_SETGET_FUNCS(dev_replace_num_write_errors, struct btrfs_dev_replace_item,
  1982. num_write_errors, 64);
  1983. BTRFS_SETGET_FUNCS(dev_replace_num_uncorrectable_read_errors,
  1984. struct btrfs_dev_replace_item, num_uncorrectable_read_errors,
  1985. 64);
  1986. BTRFS_SETGET_FUNCS(dev_replace_cursor_left, struct btrfs_dev_replace_item,
  1987. cursor_left, 64);
  1988. BTRFS_SETGET_FUNCS(dev_replace_cursor_right, struct btrfs_dev_replace_item,
  1989. cursor_right, 64);
  1990. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_src_devid,
  1991. struct btrfs_dev_replace_item, src_devid, 64);
  1992. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cont_reading_from_srcdev_mode,
  1993. struct btrfs_dev_replace_item,
  1994. cont_reading_from_srcdev_mode, 64);
  1995. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_replace_state,
  1996. struct btrfs_dev_replace_item, replace_state, 64);
  1997. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_started,
  1998. struct btrfs_dev_replace_item, time_started, 64);
  1999. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_time_stopped,
  2000. struct btrfs_dev_replace_item, time_stopped, 64);
  2001. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_write_errors,
  2002. struct btrfs_dev_replace_item, num_write_errors, 64);
  2003. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_num_uncorrectable_read_errors,
  2004. struct btrfs_dev_replace_item,
  2005. num_uncorrectable_read_errors, 64);
  2006. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_left,
  2007. struct btrfs_dev_replace_item, cursor_left, 64);
  2008. BTRFS_SETGET_STACK_FUNCS(stack_dev_replace_cursor_right,
  2009. struct btrfs_dev_replace_item, cursor_right, 64);
  2010. /* helper function to cast into the data area of the leaf. */
  2011. #define btrfs_item_ptr(leaf, slot, type) \
  2012. ((type *)(BTRFS_LEAF_DATA_OFFSET + \
  2013. btrfs_item_offset_nr(leaf, slot)))
  2014. #define btrfs_item_ptr_offset(leaf, slot) \
  2015. ((unsigned long)(BTRFS_LEAF_DATA_OFFSET + \
  2016. btrfs_item_offset_nr(leaf, slot)))
  2017. static inline u32 btrfs_crc32c(u32 crc, const void *address, unsigned length)
  2018. {
  2019. return crc32c(crc, address, length);
  2020. }
  2021. static inline void btrfs_crc32c_final(u32 crc, u8 *result)
  2022. {
  2023. put_unaligned_le32(~crc, result);
  2024. }
  2025. static inline u64 btrfs_name_hash(const char *name, int len)
  2026. {
  2027. return crc32c((u32)~1, name, len);
  2028. }
  2029. /*
  2030. * Figure the key offset of an extended inode ref
  2031. */
  2032. static inline u64 btrfs_extref_hash(u64 parent_objectid, const char *name,
  2033. int len)
  2034. {
  2035. return (u64) crc32c(parent_objectid, name, len);
  2036. }
  2037. static inline gfp_t btrfs_alloc_write_mask(struct address_space *mapping)
  2038. {
  2039. return mapping_gfp_constraint(mapping, ~__GFP_FS);
  2040. }
  2041. /* extent-tree.c */
  2042. enum btrfs_inline_ref_type {
  2043. BTRFS_REF_TYPE_INVALID,
  2044. BTRFS_REF_TYPE_BLOCK,
  2045. BTRFS_REF_TYPE_DATA,
  2046. BTRFS_REF_TYPE_ANY,
  2047. };
  2048. int btrfs_get_extent_inline_ref_type(const struct extent_buffer *eb,
  2049. struct btrfs_extent_inline_ref *iref,
  2050. enum btrfs_inline_ref_type is_data);
  2051. u64 hash_extent_data_ref(u64 root_objectid, u64 owner, u64 offset);
  2052. u64 btrfs_csum_bytes_to_leaves(struct btrfs_fs_info *fs_info, u64 csum_bytes);
  2053. /*
  2054. * Use this if we would be adding new items, as we could split nodes as we cow
  2055. * down the tree.
  2056. */
  2057. static inline u64 btrfs_calc_insert_metadata_size(struct btrfs_fs_info *fs_info,
  2058. unsigned num_items)
  2059. {
  2060. return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * 2 * num_items;
  2061. }
  2062. /*
  2063. * Doing a truncate or a modification won't result in new nodes or leaves, just
  2064. * what we need for COW.
  2065. */
  2066. static inline u64 btrfs_calc_metadata_size(struct btrfs_fs_info *fs_info,
  2067. unsigned num_items)
  2068. {
  2069. return (u64)fs_info->nodesize * BTRFS_MAX_LEVEL * num_items;
  2070. }
  2071. int btrfs_add_excluded_extent(struct btrfs_fs_info *fs_info,
  2072. u64 start, u64 num_bytes);
  2073. void btrfs_free_excluded_extents(struct btrfs_block_group_cache *cache);
  2074. int btrfs_run_delayed_refs(struct btrfs_trans_handle *trans,
  2075. unsigned long count);
  2076. void btrfs_cleanup_ref_head_accounting(struct btrfs_fs_info *fs_info,
  2077. struct btrfs_delayed_ref_root *delayed_refs,
  2078. struct btrfs_delayed_ref_head *head);
  2079. int btrfs_lookup_data_extent(struct btrfs_fs_info *fs_info, u64 start, u64 len);
  2080. int btrfs_lookup_extent_info(struct btrfs_trans_handle *trans,
  2081. struct btrfs_fs_info *fs_info, u64 bytenr,
  2082. u64 offset, int metadata, u64 *refs, u64 *flags);
  2083. int btrfs_pin_extent(struct btrfs_fs_info *fs_info,
  2084. u64 bytenr, u64 num, int reserved);
  2085. int btrfs_pin_extent_for_log_replay(struct btrfs_fs_info *fs_info,
  2086. u64 bytenr, u64 num_bytes);
  2087. int btrfs_exclude_logged_extents(struct extent_buffer *eb);
  2088. int btrfs_cross_ref_exist(struct btrfs_root *root,
  2089. u64 objectid, u64 offset, u64 bytenr, bool strict);
  2090. struct extent_buffer *btrfs_alloc_tree_block(struct btrfs_trans_handle *trans,
  2091. struct btrfs_root *root,
  2092. u64 parent, u64 root_objectid,
  2093. const struct btrfs_disk_key *key,
  2094. int level, u64 hint,
  2095. u64 empty_size);
  2096. void btrfs_free_tree_block(struct btrfs_trans_handle *trans,
  2097. struct btrfs_root *root,
  2098. struct extent_buffer *buf,
  2099. u64 parent, int last_ref);
  2100. int btrfs_alloc_reserved_file_extent(struct btrfs_trans_handle *trans,
  2101. struct btrfs_root *root, u64 owner,
  2102. u64 offset, u64 ram_bytes,
  2103. struct btrfs_key *ins);
  2104. int btrfs_alloc_logged_file_extent(struct btrfs_trans_handle *trans,
  2105. u64 root_objectid, u64 owner, u64 offset,
  2106. struct btrfs_key *ins);
  2107. int btrfs_reserve_extent(struct btrfs_root *root, u64 ram_bytes, u64 num_bytes,
  2108. u64 min_alloc_size, u64 empty_size, u64 hint_byte,
  2109. struct btrfs_key *ins, int is_data, int delalloc);
  2110. int btrfs_inc_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2111. struct extent_buffer *buf, int full_backref);
  2112. int btrfs_dec_ref(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2113. struct extent_buffer *buf, int full_backref);
  2114. int btrfs_set_disk_extent_flags(struct btrfs_trans_handle *trans,
  2115. u64 bytenr, u64 num_bytes, u64 flags,
  2116. int level, int is_data);
  2117. int btrfs_free_extent(struct btrfs_trans_handle *trans, struct btrfs_ref *ref);
  2118. int btrfs_free_reserved_extent(struct btrfs_fs_info *fs_info,
  2119. u64 start, u64 len, int delalloc);
  2120. int btrfs_free_and_pin_reserved_extent(struct btrfs_fs_info *fs_info,
  2121. u64 start, u64 len);
  2122. void btrfs_prepare_extent_commit(struct btrfs_fs_info *fs_info);
  2123. int btrfs_finish_extent_commit(struct btrfs_trans_handle *trans);
  2124. int btrfs_inc_extent_ref(struct btrfs_trans_handle *trans,
  2125. struct btrfs_ref *generic_ref);
  2126. int btrfs_extent_readonly(struct btrfs_fs_info *fs_info, u64 bytenr);
  2127. void btrfs_get_block_group_trimming(struct btrfs_block_group_cache *cache);
  2128. void btrfs_put_block_group_trimming(struct btrfs_block_group_cache *cache);
  2129. void btrfs_clear_space_info_full(struct btrfs_fs_info *info);
  2130. enum btrfs_reserve_flush_enum {
  2131. /* If we are in the transaction, we can't flush anything.*/
  2132. BTRFS_RESERVE_NO_FLUSH,
  2133. /*
  2134. * Flushing delalloc may cause deadlock somewhere, in this
  2135. * case, use FLUSH LIMIT
  2136. */
  2137. BTRFS_RESERVE_FLUSH_LIMIT,
  2138. BTRFS_RESERVE_FLUSH_EVICT,
  2139. BTRFS_RESERVE_FLUSH_ALL,
  2140. BTRFS_RESERVE_FLUSH_ALL_STEAL,
  2141. };
  2142. enum btrfs_flush_state {
  2143. FLUSH_DELAYED_ITEMS_NR = 1,
  2144. FLUSH_DELAYED_ITEMS = 2,
  2145. FLUSH_DELAYED_REFS_NR = 3,
  2146. FLUSH_DELAYED_REFS = 4,
  2147. FLUSH_DELALLOC = 5,
  2148. FLUSH_DELALLOC_WAIT = 6,
  2149. ALLOC_CHUNK = 7,
  2150. ALLOC_CHUNK_FORCE = 8,
  2151. RUN_DELAYED_IPUTS = 9,
  2152. COMMIT_TRANS = 10,
  2153. };
  2154. int btrfs_subvolume_reserve_metadata(struct btrfs_root *root,
  2155. struct btrfs_block_rsv *rsv,
  2156. int nitems, bool use_global_rsv);
  2157. void btrfs_subvolume_release_metadata(struct btrfs_fs_info *fs_info,
  2158. struct btrfs_block_rsv *rsv);
  2159. void btrfs_delalloc_release_extents(struct btrfs_inode *inode, u64 num_bytes);
  2160. int btrfs_delalloc_reserve_metadata(struct btrfs_inode *inode, u64 num_bytes);
  2161. u64 btrfs_account_ro_block_groups_free_space(struct btrfs_space_info *sinfo);
  2162. int btrfs_error_unpin_extent_range(struct btrfs_fs_info *fs_info,
  2163. u64 start, u64 end);
  2164. int btrfs_discard_extent(struct btrfs_fs_info *fs_info, u64 bytenr,
  2165. u64 num_bytes, u64 *actual_bytes);
  2166. int btrfs_trim_fs(struct btrfs_fs_info *fs_info, struct fstrim_range *range);
  2167. int btrfs_init_space_info(struct btrfs_fs_info *fs_info);
  2168. int btrfs_delayed_refs_qgroup_accounting(struct btrfs_trans_handle *trans,
  2169. struct btrfs_fs_info *fs_info);
  2170. int btrfs_start_write_no_snapshotting(struct btrfs_root *root);
  2171. void btrfs_end_write_no_snapshotting(struct btrfs_root *root);
  2172. void btrfs_wait_for_snapshot_creation(struct btrfs_root *root);
  2173. /* ctree.c */
  2174. int btrfs_bin_search(struct extent_buffer *eb, const struct btrfs_key *key,
  2175. int level, int *slot);
  2176. int btrfs_comp_cpu_keys(const struct btrfs_key *k1, const struct btrfs_key *k2);
  2177. int btrfs_previous_item(struct btrfs_root *root,
  2178. struct btrfs_path *path, u64 min_objectid,
  2179. int type);
  2180. int btrfs_previous_extent_item(struct btrfs_root *root,
  2181. struct btrfs_path *path, u64 min_objectid);
  2182. void btrfs_set_item_key_safe(struct btrfs_fs_info *fs_info,
  2183. struct btrfs_path *path,
  2184. const struct btrfs_key *new_key);
  2185. struct extent_buffer *btrfs_root_node(struct btrfs_root *root);
  2186. struct extent_buffer *btrfs_lock_root_node(struct btrfs_root *root);
  2187. struct extent_buffer *btrfs_read_lock_root_node(struct btrfs_root *root);
  2188. int btrfs_find_next_key(struct btrfs_root *root, struct btrfs_path *path,
  2189. struct btrfs_key *key, int lowest_level,
  2190. u64 min_trans);
  2191. int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
  2192. struct btrfs_path *path,
  2193. u64 min_trans);
  2194. struct extent_buffer *btrfs_read_node_slot(struct extent_buffer *parent,
  2195. int slot);
  2196. int btrfs_cow_block(struct btrfs_trans_handle *trans,
  2197. struct btrfs_root *root, struct extent_buffer *buf,
  2198. struct extent_buffer *parent, int parent_slot,
  2199. struct extent_buffer **cow_ret);
  2200. int btrfs_copy_root(struct btrfs_trans_handle *trans,
  2201. struct btrfs_root *root,
  2202. struct extent_buffer *buf,
  2203. struct extent_buffer **cow_ret, u64 new_root_objectid);
  2204. int btrfs_block_can_be_shared(struct btrfs_root *root,
  2205. struct extent_buffer *buf);
  2206. void btrfs_extend_item(struct btrfs_path *path, u32 data_size);
  2207. void btrfs_truncate_item(struct btrfs_path *path, u32 new_size, int from_end);
  2208. int btrfs_split_item(struct btrfs_trans_handle *trans,
  2209. struct btrfs_root *root,
  2210. struct btrfs_path *path,
  2211. const struct btrfs_key *new_key,
  2212. unsigned long split_offset);
  2213. int btrfs_duplicate_item(struct btrfs_trans_handle *trans,
  2214. struct btrfs_root *root,
  2215. struct btrfs_path *path,
  2216. const struct btrfs_key *new_key);
  2217. int btrfs_find_item(struct btrfs_root *fs_root, struct btrfs_path *path,
  2218. u64 inum, u64 ioff, u8 key_type, struct btrfs_key *found_key);
  2219. int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2220. const struct btrfs_key *key, struct btrfs_path *p,
  2221. int ins_len, int cow);
  2222. int btrfs_search_old_slot(struct btrfs_root *root, const struct btrfs_key *key,
  2223. struct btrfs_path *p, u64 time_seq);
  2224. int btrfs_search_slot_for_read(struct btrfs_root *root,
  2225. const struct btrfs_key *key,
  2226. struct btrfs_path *p, int find_higher,
  2227. int return_any);
  2228. int btrfs_realloc_node(struct btrfs_trans_handle *trans,
  2229. struct btrfs_root *root, struct extent_buffer *parent,
  2230. int start_slot, u64 *last_ret,
  2231. struct btrfs_key *progress);
  2232. void btrfs_release_path(struct btrfs_path *p);
  2233. struct btrfs_path *btrfs_alloc_path(void);
  2234. void btrfs_free_path(struct btrfs_path *p);
  2235. void btrfs_set_path_blocking(struct btrfs_path *p);
  2236. void btrfs_unlock_up_safe(struct btrfs_path *p, int level);
  2237. int btrfs_del_items(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2238. struct btrfs_path *path, int slot, int nr);
  2239. static inline int btrfs_del_item(struct btrfs_trans_handle *trans,
  2240. struct btrfs_root *root,
  2241. struct btrfs_path *path)
  2242. {
  2243. return btrfs_del_items(trans, root, path, path->slots[0], 1);
  2244. }
  2245. void setup_items_for_insert(struct btrfs_root *root, struct btrfs_path *path,
  2246. const struct btrfs_key *cpu_key, u32 *data_size,
  2247. u32 total_data, u32 total_size, int nr);
  2248. int btrfs_insert_item(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2249. const struct btrfs_key *key, void *data, u32 data_size);
  2250. int btrfs_insert_empty_items(struct btrfs_trans_handle *trans,
  2251. struct btrfs_root *root,
  2252. struct btrfs_path *path,
  2253. const struct btrfs_key *cpu_key, u32 *data_size,
  2254. int nr);
  2255. static inline int btrfs_insert_empty_item(struct btrfs_trans_handle *trans,
  2256. struct btrfs_root *root,
  2257. struct btrfs_path *path,
  2258. const struct btrfs_key *key,
  2259. u32 data_size)
  2260. {
  2261. return btrfs_insert_empty_items(trans, root, path, key, &data_size, 1);
  2262. }
  2263. int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path);
  2264. int btrfs_prev_leaf(struct btrfs_root *root, struct btrfs_path *path);
  2265. int btrfs_next_old_leaf(struct btrfs_root *root, struct btrfs_path *path,
  2266. u64 time_seq);
  2267. static inline int btrfs_next_old_item(struct btrfs_root *root,
  2268. struct btrfs_path *p, u64 time_seq)
  2269. {
  2270. ++p->slots[0];
  2271. if (p->slots[0] >= btrfs_header_nritems(p->nodes[0]))
  2272. return btrfs_next_old_leaf(root, p, time_seq);
  2273. return 0;
  2274. }
  2275. static inline int btrfs_next_item(struct btrfs_root *root, struct btrfs_path *p)
  2276. {
  2277. return btrfs_next_old_item(root, p, 0);
  2278. }
  2279. int btrfs_leaf_free_space(struct extent_buffer *leaf);
  2280. int __must_check btrfs_drop_snapshot(struct btrfs_root *root,
  2281. struct btrfs_block_rsv *block_rsv,
  2282. int update_ref, int for_reloc);
  2283. int btrfs_drop_subtree(struct btrfs_trans_handle *trans,
  2284. struct btrfs_root *root,
  2285. struct extent_buffer *node,
  2286. struct extent_buffer *parent);
  2287. static inline int btrfs_fs_closing(struct btrfs_fs_info *fs_info)
  2288. {
  2289. /*
  2290. * Do it this way so we only ever do one test_bit in the normal case.
  2291. */
  2292. if (test_bit(BTRFS_FS_CLOSING_START, &fs_info->flags)) {
  2293. if (test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags))
  2294. return 2;
  2295. return 1;
  2296. }
  2297. return 0;
  2298. }
  2299. /*
  2300. * If we remount the fs to be R/O or umount the fs, the cleaner needn't do
  2301. * anything except sleeping. This function is used to check the status of
  2302. * the fs.
  2303. */
  2304. static inline int btrfs_need_cleaner_sleep(struct btrfs_fs_info *fs_info)
  2305. {
  2306. return fs_info->sb->s_flags & SB_RDONLY || btrfs_fs_closing(fs_info);
  2307. }
  2308. static inline void free_fs_info(struct btrfs_fs_info *fs_info)
  2309. {
  2310. kfree(fs_info->balance_ctl);
  2311. kfree(fs_info->delayed_root);
  2312. kfree(fs_info->extent_root);
  2313. kfree(fs_info->tree_root);
  2314. kfree(fs_info->chunk_root);
  2315. kfree(fs_info->dev_root);
  2316. kfree(fs_info->csum_root);
  2317. kfree(fs_info->quota_root);
  2318. kfree(fs_info->uuid_root);
  2319. kfree(fs_info->free_space_root);
  2320. kfree(fs_info->super_copy);
  2321. kfree(fs_info->super_for_commit);
  2322. kvfree(fs_info);
  2323. }
  2324. /* tree mod log functions from ctree.c */
  2325. u64 btrfs_get_tree_mod_seq(struct btrfs_fs_info *fs_info,
  2326. struct seq_list *elem);
  2327. void btrfs_put_tree_mod_seq(struct btrfs_fs_info *fs_info,
  2328. struct seq_list *elem);
  2329. int btrfs_old_root_level(struct btrfs_root *root, u64 time_seq);
  2330. /* root-item.c */
  2331. int btrfs_add_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
  2332. u64 ref_id, u64 dirid, u64 sequence, const char *name,
  2333. int name_len);
  2334. int btrfs_del_root_ref(struct btrfs_trans_handle *trans, u64 root_id,
  2335. u64 ref_id, u64 dirid, u64 *sequence, const char *name,
  2336. int name_len);
  2337. int btrfs_del_root(struct btrfs_trans_handle *trans,
  2338. const struct btrfs_key *key);
  2339. int btrfs_insert_root(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  2340. const struct btrfs_key *key,
  2341. struct btrfs_root_item *item);
  2342. int __must_check btrfs_update_root(struct btrfs_trans_handle *trans,
  2343. struct btrfs_root *root,
  2344. struct btrfs_key *key,
  2345. struct btrfs_root_item *item);
  2346. int btrfs_find_root(struct btrfs_root *root, const struct btrfs_key *search_key,
  2347. struct btrfs_path *path, struct btrfs_root_item *root_item,
  2348. struct btrfs_key *root_key);
  2349. int btrfs_find_orphan_roots(struct btrfs_fs_info *fs_info);
  2350. void btrfs_set_root_node(struct btrfs_root_item *item,
  2351. struct extent_buffer *node);
  2352. void btrfs_check_and_init_root_item(struct btrfs_root_item *item);
  2353. void btrfs_update_root_times(struct btrfs_trans_handle *trans,
  2354. struct btrfs_root *root);
  2355. /* uuid-tree.c */
  2356. int btrfs_uuid_tree_add(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
  2357. u64 subid);
  2358. int btrfs_uuid_tree_remove(struct btrfs_trans_handle *trans, u8 *uuid, u8 type,
  2359. u64 subid);
  2360. int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
  2361. int (*check_func)(struct btrfs_fs_info *, u8 *, u8,
  2362. u64));
  2363. /* dir-item.c */
  2364. int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
  2365. const char *name, int name_len);
  2366. int btrfs_insert_dir_item(struct btrfs_trans_handle *trans, const char *name,
  2367. int name_len, struct btrfs_inode *dir,
  2368. struct btrfs_key *location, u8 type, u64 index);
  2369. struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
  2370. struct btrfs_root *root,
  2371. struct btrfs_path *path, u64 dir,
  2372. const char *name, int name_len,
  2373. int mod);
  2374. struct btrfs_dir_item *
  2375. btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
  2376. struct btrfs_root *root,
  2377. struct btrfs_path *path, u64 dir,
  2378. u64 objectid, const char *name, int name_len,
  2379. int mod);
  2380. struct btrfs_dir_item *
  2381. btrfs_search_dir_index_item(struct btrfs_root *root,
  2382. struct btrfs_path *path, u64 dirid,
  2383. const char *name, int name_len);
  2384. int btrfs_delete_one_dir_name(struct btrfs_trans_handle *trans,
  2385. struct btrfs_root *root,
  2386. struct btrfs_path *path,
  2387. struct btrfs_dir_item *di);
  2388. int btrfs_insert_xattr_item(struct btrfs_trans_handle *trans,
  2389. struct btrfs_root *root,
  2390. struct btrfs_path *path, u64 objectid,
  2391. const char *name, u16 name_len,
  2392. const void *data, u16 data_len);
  2393. struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
  2394. struct btrfs_root *root,
  2395. struct btrfs_path *path, u64 dir,
  2396. const char *name, u16 name_len,
  2397. int mod);
  2398. struct btrfs_dir_item *btrfs_match_dir_item_name(struct btrfs_fs_info *fs_info,
  2399. struct btrfs_path *path,
  2400. const char *name,
  2401. int name_len);
  2402. /* orphan.c */
  2403. int btrfs_insert_orphan_item(struct btrfs_trans_handle *trans,
  2404. struct btrfs_root *root, u64 offset);
  2405. int btrfs_del_orphan_item(struct btrfs_trans_handle *trans,
  2406. struct btrfs_root *root, u64 offset);
  2407. int btrfs_find_orphan_item(struct btrfs_root *root, u64 offset);
  2408. /* inode-item.c */
  2409. int btrfs_insert_inode_ref(struct btrfs_trans_handle *trans,
  2410. struct btrfs_root *root,
  2411. const char *name, int name_len,
  2412. u64 inode_objectid, u64 ref_objectid, u64 index);
  2413. int btrfs_del_inode_ref(struct btrfs_trans_handle *trans,
  2414. struct btrfs_root *root,
  2415. const char *name, int name_len,
  2416. u64 inode_objectid, u64 ref_objectid, u64 *index);
  2417. int btrfs_insert_empty_inode(struct btrfs_trans_handle *trans,
  2418. struct btrfs_root *root,
  2419. struct btrfs_path *path, u64 objectid);
  2420. int btrfs_lookup_inode(struct btrfs_trans_handle *trans, struct btrfs_root
  2421. *root, struct btrfs_path *path,
  2422. struct btrfs_key *location, int mod);
  2423. struct btrfs_inode_extref *
  2424. btrfs_lookup_inode_extref(struct btrfs_trans_handle *trans,
  2425. struct btrfs_root *root,
  2426. struct btrfs_path *path,
  2427. const char *name, int name_len,
  2428. u64 inode_objectid, u64 ref_objectid, int ins_len,
  2429. int cow);
  2430. struct btrfs_inode_ref *btrfs_find_name_in_backref(struct extent_buffer *leaf,
  2431. int slot, const char *name,
  2432. int name_len);
  2433. struct btrfs_inode_extref *btrfs_find_name_in_ext_backref(
  2434. struct extent_buffer *leaf, int slot, u64 ref_objectid,
  2435. const char *name, int name_len);
  2436. /* file-item.c */
  2437. struct btrfs_dio_private;
  2438. int btrfs_del_csums(struct btrfs_trans_handle *trans,
  2439. struct btrfs_root *root, u64 bytenr, u64 len);
  2440. blk_status_t btrfs_lookup_bio_sums(struct inode *inode, struct bio *bio,
  2441. u8 *dst);
  2442. blk_status_t btrfs_lookup_bio_sums_dio(struct inode *inode, struct bio *bio,
  2443. u64 logical_offset);
  2444. int btrfs_insert_file_extent(struct btrfs_trans_handle *trans,
  2445. struct btrfs_root *root,
  2446. u64 objectid, u64 pos,
  2447. u64 disk_offset, u64 disk_num_bytes,
  2448. u64 num_bytes, u64 offset, u64 ram_bytes,
  2449. u8 compression, u8 encryption, u16 other_encoding);
  2450. int btrfs_lookup_file_extent(struct btrfs_trans_handle *trans,
  2451. struct btrfs_root *root,
  2452. struct btrfs_path *path, u64 objectid,
  2453. u64 bytenr, int mod);
  2454. int btrfs_csum_file_blocks(struct btrfs_trans_handle *trans,
  2455. struct btrfs_root *root,
  2456. struct btrfs_ordered_sum *sums);
  2457. blk_status_t btrfs_csum_one_bio(struct inode *inode, struct bio *bio,
  2458. u64 file_start, int contig);
  2459. int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end,
  2460. struct list_head *list, int search_commit);
  2461. void btrfs_extent_item_to_extent_map(struct btrfs_inode *inode,
  2462. const struct btrfs_path *path,
  2463. struct btrfs_file_extent_item *fi,
  2464. const bool new_inline,
  2465. struct extent_map *em);
  2466. /* inode.c */
  2467. struct extent_map *btrfs_get_extent_fiemap(struct btrfs_inode *inode,
  2468. u64 start, u64 len);
  2469. noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
  2470. u64 *orig_start, u64 *orig_block_len,
  2471. u64 *ram_bytes, bool strict);
  2472. void __btrfs_del_delalloc_inode(struct btrfs_root *root,
  2473. struct btrfs_inode *inode);
  2474. struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry);
  2475. int btrfs_set_inode_index(struct btrfs_inode *dir, u64 *index);
  2476. int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
  2477. struct btrfs_root *root,
  2478. struct btrfs_inode *dir, struct btrfs_inode *inode,
  2479. const char *name, int name_len);
  2480. int btrfs_add_link(struct btrfs_trans_handle *trans,
  2481. struct btrfs_inode *parent_inode, struct btrfs_inode *inode,
  2482. const char *name, int name_len, int add_backref, u64 index);
  2483. int btrfs_delete_subvolume(struct inode *dir, struct dentry *dentry);
  2484. int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
  2485. int front);
  2486. int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
  2487. struct btrfs_root *root,
  2488. struct inode *inode, u64 new_size,
  2489. u32 min_type);
  2490. int btrfs_start_delalloc_snapshot(struct btrfs_root *root);
  2491. int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int nr);
  2492. int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
  2493. unsigned int extra_bits,
  2494. struct extent_state **cached_state);
  2495. int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
  2496. struct btrfs_root *new_root,
  2497. struct btrfs_root *parent_root,
  2498. u64 new_dirid);
  2499. void btrfs_set_delalloc_extent(struct inode *inode, struct extent_state *state,
  2500. unsigned *bits);
  2501. void btrfs_clear_delalloc_extent(struct inode *inode,
  2502. struct extent_state *state, unsigned *bits);
  2503. void btrfs_merge_delalloc_extent(struct inode *inode, struct extent_state *new,
  2504. struct extent_state *other);
  2505. void btrfs_split_delalloc_extent(struct inode *inode,
  2506. struct extent_state *orig, u64 split);
  2507. int btrfs_bio_fits_in_stripe(struct page *page, size_t size, struct bio *bio,
  2508. unsigned long bio_flags);
  2509. void btrfs_set_range_writeback(struct extent_io_tree *tree, u64 start, u64 end);
  2510. vm_fault_t btrfs_page_mkwrite(struct vm_fault *vmf);
  2511. int btrfs_readpage(struct file *file, struct page *page);
  2512. void btrfs_evict_inode(struct inode *inode);
  2513. int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc);
  2514. struct inode *btrfs_alloc_inode(struct super_block *sb);
  2515. void btrfs_destroy_inode(struct inode *inode);
  2516. void btrfs_free_inode(struct inode *inode);
  2517. int btrfs_drop_inode(struct inode *inode);
  2518. int __init btrfs_init_cachep(void);
  2519. void __cold btrfs_destroy_cachep(void);
  2520. struct inode *btrfs_iget_path(struct super_block *s, struct btrfs_key *location,
  2521. struct btrfs_root *root, int *new,
  2522. struct btrfs_path *path);
  2523. struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
  2524. struct btrfs_root *root, int *was_new);
  2525. struct extent_map *btrfs_get_extent(struct btrfs_inode *inode,
  2526. struct page *page, size_t pg_offset,
  2527. u64 start, u64 end, int create);
  2528. int btrfs_update_inode(struct btrfs_trans_handle *trans,
  2529. struct btrfs_root *root,
  2530. struct inode *inode);
  2531. int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
  2532. struct btrfs_root *root, struct inode *inode);
  2533. int btrfs_orphan_add(struct btrfs_trans_handle *trans,
  2534. struct btrfs_inode *inode);
  2535. int btrfs_orphan_cleanup(struct btrfs_root *root);
  2536. int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size);
  2537. void btrfs_add_delayed_iput(struct inode *inode);
  2538. void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info);
  2539. int btrfs_wait_on_delayed_iputs(struct btrfs_fs_info *fs_info);
  2540. int btrfs_prealloc_file_range(struct inode *inode, int mode,
  2541. u64 start, u64 num_bytes, u64 min_size,
  2542. loff_t actual_len, u64 *alloc_hint);
  2543. int btrfs_prealloc_file_range_trans(struct inode *inode,
  2544. struct btrfs_trans_handle *trans, int mode,
  2545. u64 start, u64 num_bytes, u64 min_size,
  2546. loff_t actual_len, u64 *alloc_hint);
  2547. int btrfs_run_delalloc_range(struct inode *inode, struct page *locked_page,
  2548. u64 start, u64 end, int *page_started, unsigned long *nr_written,
  2549. struct writeback_control *wbc);
  2550. int btrfs_writepage_cow_fixup(struct page *page, u64 start, u64 end);
  2551. void btrfs_writepage_endio_finish_ordered(struct page *page, u64 start,
  2552. u64 end, int uptodate);
  2553. extern const struct dentry_operations btrfs_dentry_operations;
  2554. /* ioctl.c */
  2555. long btrfs_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  2556. long btrfs_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
  2557. int btrfs_ioctl_get_supported_features(void __user *arg);
  2558. void btrfs_sync_inode_flags_to_i_flags(struct inode *inode);
  2559. int btrfs_is_empty_uuid(u8 *uuid);
  2560. int btrfs_defrag_file(struct inode *inode, struct file *file,
  2561. struct btrfs_ioctl_defrag_range_args *range,
  2562. u64 newer_than, unsigned long max_pages);
  2563. void btrfs_get_block_group_info(struct list_head *groups_list,
  2564. struct btrfs_ioctl_space_info *space);
  2565. void btrfs_update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2566. struct btrfs_ioctl_balance_args *bargs);
  2567. /* file.c */
  2568. int __init btrfs_auto_defrag_init(void);
  2569. void __cold btrfs_auto_defrag_exit(void);
  2570. int btrfs_add_inode_defrag(struct btrfs_trans_handle *trans,
  2571. struct btrfs_inode *inode);
  2572. int btrfs_run_defrag_inodes(struct btrfs_fs_info *fs_info);
  2573. void btrfs_cleanup_defrag_inodes(struct btrfs_fs_info *fs_info);
  2574. int btrfs_sync_file(struct file *file, loff_t start, loff_t end, int datasync);
  2575. void btrfs_drop_extent_cache(struct btrfs_inode *inode, u64 start, u64 end,
  2576. int skip_pinned);
  2577. extern const struct file_operations btrfs_file_operations;
  2578. int __btrfs_drop_extents(struct btrfs_trans_handle *trans,
  2579. struct btrfs_root *root, struct inode *inode,
  2580. struct btrfs_path *path, u64 start, u64 end,
  2581. u64 *drop_end, int drop_cache,
  2582. int replace_extent,
  2583. u32 extent_item_size,
  2584. int *key_inserted);
  2585. int btrfs_drop_extents(struct btrfs_trans_handle *trans,
  2586. struct btrfs_root *root, struct inode *inode, u64 start,
  2587. u64 end, int drop_cache);
  2588. int btrfs_punch_hole_range(struct inode *inode, struct btrfs_path *path,
  2589. const u64 start, const u64 end,
  2590. struct btrfs_clone_extent_info *clone_info,
  2591. struct btrfs_trans_handle **trans_out);
  2592. int btrfs_mark_extent_written(struct btrfs_trans_handle *trans,
  2593. struct btrfs_inode *inode, u64 start, u64 end);
  2594. int btrfs_release_file(struct inode *inode, struct file *file);
  2595. int btrfs_dirty_pages(struct inode *inode, struct page **pages,
  2596. size_t num_pages, loff_t pos, size_t write_bytes,
  2597. struct extent_state **cached);
  2598. int btrfs_fdatawrite_range(struct inode *inode, loff_t start, loff_t end);
  2599. loff_t btrfs_remap_file_range(struct file *file_in, loff_t pos_in,
  2600. struct file *file_out, loff_t pos_out,
  2601. loff_t len, unsigned int remap_flags);
  2602. int btrfs_check_can_nocow(struct btrfs_inode *inode, loff_t pos,
  2603. size_t *write_bytes);
  2604. /* tree-defrag.c */
  2605. int btrfs_defrag_leaves(struct btrfs_trans_handle *trans,
  2606. struct btrfs_root *root);
  2607. /* super.c */
  2608. int btrfs_parse_options(struct btrfs_fs_info *info, char *options,
  2609. unsigned long new_flags);
  2610. int btrfs_sync_fs(struct super_block *sb, int wait);
  2611. char *btrfs_get_subvol_name_from_objectid(struct btrfs_fs_info *fs_info,
  2612. u64 subvol_objectid);
  2613. static inline __printf(2, 3) __cold
  2614. void btrfs_no_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...)
  2615. {
  2616. }
  2617. #ifdef CONFIG_PRINTK
  2618. __printf(2, 3)
  2619. __cold
  2620. void btrfs_printk(const struct btrfs_fs_info *fs_info, const char *fmt, ...);
  2621. #else
  2622. #define btrfs_printk(fs_info, fmt, args...) \
  2623. btrfs_no_printk(fs_info, fmt, ##args)
  2624. #endif
  2625. #define btrfs_emerg(fs_info, fmt, args...) \
  2626. btrfs_printk(fs_info, KERN_EMERG fmt, ##args)
  2627. #define btrfs_alert(fs_info, fmt, args...) \
  2628. btrfs_printk(fs_info, KERN_ALERT fmt, ##args)
  2629. #define btrfs_crit(fs_info, fmt, args...) \
  2630. btrfs_printk(fs_info, KERN_CRIT fmt, ##args)
  2631. #define btrfs_err(fs_info, fmt, args...) \
  2632. btrfs_printk(fs_info, KERN_ERR fmt, ##args)
  2633. #define btrfs_warn(fs_info, fmt, args...) \
  2634. btrfs_printk(fs_info, KERN_WARNING fmt, ##args)
  2635. #define btrfs_notice(fs_info, fmt, args...) \
  2636. btrfs_printk(fs_info, KERN_NOTICE fmt, ##args)
  2637. #define btrfs_info(fs_info, fmt, args...) \
  2638. btrfs_printk(fs_info, KERN_INFO fmt, ##args)
  2639. /*
  2640. * Wrappers that use printk_in_rcu
  2641. */
  2642. #define btrfs_emerg_in_rcu(fs_info, fmt, args...) \
  2643. btrfs_printk_in_rcu(fs_info, KERN_EMERG fmt, ##args)
  2644. #define btrfs_alert_in_rcu(fs_info, fmt, args...) \
  2645. btrfs_printk_in_rcu(fs_info, KERN_ALERT fmt, ##args)
  2646. #define btrfs_crit_in_rcu(fs_info, fmt, args...) \
  2647. btrfs_printk_in_rcu(fs_info, KERN_CRIT fmt, ##args)
  2648. #define btrfs_err_in_rcu(fs_info, fmt, args...) \
  2649. btrfs_printk_in_rcu(fs_info, KERN_ERR fmt, ##args)
  2650. #define btrfs_warn_in_rcu(fs_info, fmt, args...) \
  2651. btrfs_printk_in_rcu(fs_info, KERN_WARNING fmt, ##args)
  2652. #define btrfs_notice_in_rcu(fs_info, fmt, args...) \
  2653. btrfs_printk_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
  2654. #define btrfs_info_in_rcu(fs_info, fmt, args...) \
  2655. btrfs_printk_in_rcu(fs_info, KERN_INFO fmt, ##args)
  2656. /*
  2657. * Wrappers that use a ratelimited printk_in_rcu
  2658. */
  2659. #define btrfs_emerg_rl_in_rcu(fs_info, fmt, args...) \
  2660. btrfs_printk_rl_in_rcu(fs_info, KERN_EMERG fmt, ##args)
  2661. #define btrfs_alert_rl_in_rcu(fs_info, fmt, args...) \
  2662. btrfs_printk_rl_in_rcu(fs_info, KERN_ALERT fmt, ##args)
  2663. #define btrfs_crit_rl_in_rcu(fs_info, fmt, args...) \
  2664. btrfs_printk_rl_in_rcu(fs_info, KERN_CRIT fmt, ##args)
  2665. #define btrfs_err_rl_in_rcu(fs_info, fmt, args...) \
  2666. btrfs_printk_rl_in_rcu(fs_info, KERN_ERR fmt, ##args)
  2667. #define btrfs_warn_rl_in_rcu(fs_info, fmt, args...) \
  2668. btrfs_printk_rl_in_rcu(fs_info, KERN_WARNING fmt, ##args)
  2669. #define btrfs_notice_rl_in_rcu(fs_info, fmt, args...) \
  2670. btrfs_printk_rl_in_rcu(fs_info, KERN_NOTICE fmt, ##args)
  2671. #define btrfs_info_rl_in_rcu(fs_info, fmt, args...) \
  2672. btrfs_printk_rl_in_rcu(fs_info, KERN_INFO fmt, ##args)
  2673. /*
  2674. * Wrappers that use a ratelimited printk
  2675. */
  2676. #define btrfs_emerg_rl(fs_info, fmt, args...) \
  2677. btrfs_printk_ratelimited(fs_info, KERN_EMERG fmt, ##args)
  2678. #define btrfs_alert_rl(fs_info, fmt, args...) \
  2679. btrfs_printk_ratelimited(fs_info, KERN_ALERT fmt, ##args)
  2680. #define btrfs_crit_rl(fs_info, fmt, args...) \
  2681. btrfs_printk_ratelimited(fs_info, KERN_CRIT fmt, ##args)
  2682. #define btrfs_err_rl(fs_info, fmt, args...) \
  2683. btrfs_printk_ratelimited(fs_info, KERN_ERR fmt, ##args)
  2684. #define btrfs_warn_rl(fs_info, fmt, args...) \
  2685. btrfs_printk_ratelimited(fs_info, KERN_WARNING fmt, ##args)
  2686. #define btrfs_notice_rl(fs_info, fmt, args...) \
  2687. btrfs_printk_ratelimited(fs_info, KERN_NOTICE fmt, ##args)
  2688. #define btrfs_info_rl(fs_info, fmt, args...) \
  2689. btrfs_printk_ratelimited(fs_info, KERN_INFO fmt, ##args)
  2690. #if defined(CONFIG_DYNAMIC_DEBUG)
  2691. #define btrfs_debug(fs_info, fmt, args...) \
  2692. _dynamic_func_call_no_desc(fmt, btrfs_printk, \
  2693. fs_info, KERN_DEBUG fmt, ##args)
  2694. #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
  2695. _dynamic_func_call_no_desc(fmt, btrfs_printk_in_rcu, \
  2696. fs_info, KERN_DEBUG fmt, ##args)
  2697. #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
  2698. _dynamic_func_call_no_desc(fmt, btrfs_printk_rl_in_rcu, \
  2699. fs_info, KERN_DEBUG fmt, ##args)
  2700. #define btrfs_debug_rl(fs_info, fmt, args...) \
  2701. _dynamic_func_call_no_desc(fmt, btrfs_printk_ratelimited, \
  2702. fs_info, KERN_DEBUG fmt, ##args)
  2703. #elif defined(DEBUG)
  2704. #define btrfs_debug(fs_info, fmt, args...) \
  2705. btrfs_printk(fs_info, KERN_DEBUG fmt, ##args)
  2706. #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
  2707. btrfs_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
  2708. #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
  2709. btrfs_printk_rl_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
  2710. #define btrfs_debug_rl(fs_info, fmt, args...) \
  2711. btrfs_printk_ratelimited(fs_info, KERN_DEBUG fmt, ##args)
  2712. #else
  2713. #define btrfs_debug(fs_info, fmt, args...) \
  2714. btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
  2715. #define btrfs_debug_in_rcu(fs_info, fmt, args...) \
  2716. btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
  2717. #define btrfs_debug_rl_in_rcu(fs_info, fmt, args...) \
  2718. btrfs_no_printk_in_rcu(fs_info, KERN_DEBUG fmt, ##args)
  2719. #define btrfs_debug_rl(fs_info, fmt, args...) \
  2720. btrfs_no_printk(fs_info, KERN_DEBUG fmt, ##args)
  2721. #endif
  2722. #define btrfs_printk_in_rcu(fs_info, fmt, args...) \
  2723. do { \
  2724. rcu_read_lock(); \
  2725. btrfs_printk(fs_info, fmt, ##args); \
  2726. rcu_read_unlock(); \
  2727. } while (0)
  2728. #define btrfs_no_printk_in_rcu(fs_info, fmt, args...) \
  2729. do { \
  2730. rcu_read_lock(); \
  2731. btrfs_no_printk(fs_info, fmt, ##args); \
  2732. rcu_read_unlock(); \
  2733. } while (0)
  2734. #define btrfs_printk_ratelimited(fs_info, fmt, args...) \
  2735. do { \
  2736. static DEFINE_RATELIMIT_STATE(_rs, \
  2737. DEFAULT_RATELIMIT_INTERVAL, \
  2738. DEFAULT_RATELIMIT_BURST); \
  2739. if (__ratelimit(&_rs)) \
  2740. btrfs_printk(fs_info, fmt, ##args); \
  2741. } while (0)
  2742. #define btrfs_printk_rl_in_rcu(fs_info, fmt, args...) \
  2743. do { \
  2744. rcu_read_lock(); \
  2745. btrfs_printk_ratelimited(fs_info, fmt, ##args); \
  2746. rcu_read_unlock(); \
  2747. } while (0)
  2748. #ifdef CONFIG_BTRFS_ASSERT
  2749. __cold __noreturn
  2750. static inline void assertfail(const char *expr, const char *file, int line)
  2751. {
  2752. pr_err("assertion failed: %s, in %s:%d\n", expr, file, line);
  2753. BUG();
  2754. }
  2755. #define ASSERT(expr) \
  2756. (likely(expr) ? (void)0 : assertfail(#expr, __FILE__, __LINE__))
  2757. #else
  2758. static inline void assertfail(const char *expr, const char* file, int line) { }
  2759. #define ASSERT(expr) (void)(expr)
  2760. #endif
  2761. /*
  2762. * Use that for functions that are conditionally exported for sanity tests but
  2763. * otherwise static
  2764. */
  2765. #ifndef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  2766. #define EXPORT_FOR_TESTS static
  2767. #else
  2768. #define EXPORT_FOR_TESTS
  2769. #endif
  2770. __cold
  2771. static inline void btrfs_print_v0_err(struct btrfs_fs_info *fs_info)
  2772. {
  2773. btrfs_err(fs_info,
  2774. "Unsupported V0 extent filesystem detected. Aborting. Please re-create your filesystem with a newer kernel");
  2775. }
  2776. __printf(5, 6)
  2777. __cold
  2778. void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function,
  2779. unsigned int line, int errno, const char *fmt, ...);
  2780. const char *btrfs_decode_error(int errno);
  2781. __cold
  2782. void __btrfs_abort_transaction(struct btrfs_trans_handle *trans,
  2783. const char *function,
  2784. unsigned int line, int errno);
  2785. /*
  2786. * Call btrfs_abort_transaction as early as possible when an error condition is
  2787. * detected, that way the exact line number is reported.
  2788. */
  2789. #define btrfs_abort_transaction(trans, errno) \
  2790. do { \
  2791. /* Report first abort since mount */ \
  2792. if (!test_and_set_bit(BTRFS_FS_STATE_TRANS_ABORTED, \
  2793. &((trans)->fs_info->fs_state))) { \
  2794. if ((errno) != -EIO && (errno) != -EROFS) { \
  2795. WARN(1, KERN_DEBUG \
  2796. "BTRFS: Transaction aborted (error %d)\n", \
  2797. (errno)); \
  2798. } else { \
  2799. btrfs_debug((trans)->fs_info, \
  2800. "Transaction aborted (error %d)", \
  2801. (errno)); \
  2802. } \
  2803. } \
  2804. __btrfs_abort_transaction((trans), __func__, \
  2805. __LINE__, (errno)); \
  2806. } while (0)
  2807. #define btrfs_handle_fs_error(fs_info, errno, fmt, args...) \
  2808. do { \
  2809. __btrfs_handle_fs_error((fs_info), __func__, __LINE__, \
  2810. (errno), fmt, ##args); \
  2811. } while (0)
  2812. __printf(5, 6)
  2813. __cold
  2814. void __btrfs_panic(struct btrfs_fs_info *fs_info, const char *function,
  2815. unsigned int line, int errno, const char *fmt, ...);
  2816. /*
  2817. * If BTRFS_MOUNT_PANIC_ON_FATAL_ERROR is in mount_opt, __btrfs_panic
  2818. * will panic(). Otherwise we BUG() here.
  2819. */
  2820. #define btrfs_panic(fs_info, errno, fmt, args...) \
  2821. do { \
  2822. __btrfs_panic(fs_info, __func__, __LINE__, errno, fmt, ##args); \
  2823. BUG(); \
  2824. } while (0)
  2825. /* compatibility and incompatibility defines */
  2826. #define btrfs_set_fs_incompat(__fs_info, opt) \
  2827. __btrfs_set_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
  2828. #opt)
  2829. static inline void __btrfs_set_fs_incompat(struct btrfs_fs_info *fs_info,
  2830. u64 flag, const char* name)
  2831. {
  2832. struct btrfs_super_block *disk_super;
  2833. u64 features;
  2834. disk_super = fs_info->super_copy;
  2835. features = btrfs_super_incompat_flags(disk_super);
  2836. if (!(features & flag)) {
  2837. spin_lock(&fs_info->super_lock);
  2838. features = btrfs_super_incompat_flags(disk_super);
  2839. if (!(features & flag)) {
  2840. features |= flag;
  2841. btrfs_set_super_incompat_flags(disk_super, features);
  2842. btrfs_info(fs_info,
  2843. "setting incompat feature flag for %s (0x%llx)",
  2844. name, flag);
  2845. }
  2846. spin_unlock(&fs_info->super_lock);
  2847. }
  2848. }
  2849. #define btrfs_clear_fs_incompat(__fs_info, opt) \
  2850. __btrfs_clear_fs_incompat((__fs_info), BTRFS_FEATURE_INCOMPAT_##opt, \
  2851. #opt)
  2852. static inline void __btrfs_clear_fs_incompat(struct btrfs_fs_info *fs_info,
  2853. u64 flag, const char* name)
  2854. {
  2855. struct btrfs_super_block *disk_super;
  2856. u64 features;
  2857. disk_super = fs_info->super_copy;
  2858. features = btrfs_super_incompat_flags(disk_super);
  2859. if (features & flag) {
  2860. spin_lock(&fs_info->super_lock);
  2861. features = btrfs_super_incompat_flags(disk_super);
  2862. if (features & flag) {
  2863. features &= ~flag;
  2864. btrfs_set_super_incompat_flags(disk_super, features);
  2865. btrfs_info(fs_info,
  2866. "clearing incompat feature flag for %s (0x%llx)",
  2867. name, flag);
  2868. }
  2869. spin_unlock(&fs_info->super_lock);
  2870. }
  2871. }
  2872. #define btrfs_fs_incompat(fs_info, opt) \
  2873. __btrfs_fs_incompat((fs_info), BTRFS_FEATURE_INCOMPAT_##opt)
  2874. static inline bool __btrfs_fs_incompat(struct btrfs_fs_info *fs_info, u64 flag)
  2875. {
  2876. struct btrfs_super_block *disk_super;
  2877. disk_super = fs_info->super_copy;
  2878. return !!(btrfs_super_incompat_flags(disk_super) & flag);
  2879. }
  2880. #define btrfs_set_fs_compat_ro(__fs_info, opt) \
  2881. __btrfs_set_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
  2882. #opt)
  2883. static inline void __btrfs_set_fs_compat_ro(struct btrfs_fs_info *fs_info,
  2884. u64 flag, const char *name)
  2885. {
  2886. struct btrfs_super_block *disk_super;
  2887. u64 features;
  2888. disk_super = fs_info->super_copy;
  2889. features = btrfs_super_compat_ro_flags(disk_super);
  2890. if (!(features & flag)) {
  2891. spin_lock(&fs_info->super_lock);
  2892. features = btrfs_super_compat_ro_flags(disk_super);
  2893. if (!(features & flag)) {
  2894. features |= flag;
  2895. btrfs_set_super_compat_ro_flags(disk_super, features);
  2896. btrfs_info(fs_info,
  2897. "setting compat-ro feature flag for %s (0x%llx)",
  2898. name, flag);
  2899. }
  2900. spin_unlock(&fs_info->super_lock);
  2901. }
  2902. }
  2903. #define btrfs_clear_fs_compat_ro(__fs_info, opt) \
  2904. __btrfs_clear_fs_compat_ro((__fs_info), BTRFS_FEATURE_COMPAT_RO_##opt, \
  2905. #opt)
  2906. static inline void __btrfs_clear_fs_compat_ro(struct btrfs_fs_info *fs_info,
  2907. u64 flag, const char *name)
  2908. {
  2909. struct btrfs_super_block *disk_super;
  2910. u64 features;
  2911. disk_super = fs_info->super_copy;
  2912. features = btrfs_super_compat_ro_flags(disk_super);
  2913. if (features & flag) {
  2914. spin_lock(&fs_info->super_lock);
  2915. features = btrfs_super_compat_ro_flags(disk_super);
  2916. if (features & flag) {
  2917. features &= ~flag;
  2918. btrfs_set_super_compat_ro_flags(disk_super, features);
  2919. btrfs_info(fs_info,
  2920. "clearing compat-ro feature flag for %s (0x%llx)",
  2921. name, flag);
  2922. }
  2923. spin_unlock(&fs_info->super_lock);
  2924. }
  2925. }
  2926. #define btrfs_fs_compat_ro(fs_info, opt) \
  2927. __btrfs_fs_compat_ro((fs_info), BTRFS_FEATURE_COMPAT_RO_##opt)
  2928. static inline int __btrfs_fs_compat_ro(struct btrfs_fs_info *fs_info, u64 flag)
  2929. {
  2930. struct btrfs_super_block *disk_super;
  2931. disk_super = fs_info->super_copy;
  2932. return !!(btrfs_super_compat_ro_flags(disk_super) & flag);
  2933. }
  2934. /* acl.c */
  2935. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  2936. struct posix_acl *btrfs_get_acl(struct inode *inode, int type);
  2937. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type);
  2938. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  2939. struct inode *inode, struct inode *dir);
  2940. #else
  2941. #define btrfs_get_acl NULL
  2942. #define btrfs_set_acl NULL
  2943. static inline int btrfs_init_acl(struct btrfs_trans_handle *trans,
  2944. struct inode *inode, struct inode *dir)
  2945. {
  2946. return 0;
  2947. }
  2948. #endif
  2949. /* relocation.c */
  2950. int btrfs_relocate_block_group(struct btrfs_fs_info *fs_info, u64 group_start);
  2951. int btrfs_init_reloc_root(struct btrfs_trans_handle *trans,
  2952. struct btrfs_root *root);
  2953. int btrfs_update_reloc_root(struct btrfs_trans_handle *trans,
  2954. struct btrfs_root *root);
  2955. int btrfs_recover_relocation(struct btrfs_root *root);
  2956. int btrfs_reloc_clone_csums(struct inode *inode, u64 file_pos, u64 len);
  2957. int btrfs_reloc_cow_block(struct btrfs_trans_handle *trans,
  2958. struct btrfs_root *root, struct extent_buffer *buf,
  2959. struct extent_buffer *cow);
  2960. void btrfs_reloc_pre_snapshot(struct btrfs_pending_snapshot *pending,
  2961. u64 *bytes_to_reserve);
  2962. int btrfs_reloc_post_snapshot(struct btrfs_trans_handle *trans,
  2963. struct btrfs_pending_snapshot *pending);
  2964. /* scrub.c */
  2965. int btrfs_scrub_dev(struct btrfs_fs_info *fs_info, u64 devid, u64 start,
  2966. u64 end, struct btrfs_scrub_progress *progress,
  2967. int readonly, int is_dev_replace);
  2968. void btrfs_scrub_pause(struct btrfs_fs_info *fs_info);
  2969. void btrfs_scrub_continue(struct btrfs_fs_info *fs_info);
  2970. int btrfs_scrub_cancel(struct btrfs_fs_info *info);
  2971. int btrfs_scrub_cancel_dev(struct btrfs_device *dev);
  2972. int btrfs_scrub_progress(struct btrfs_fs_info *fs_info, u64 devid,
  2973. struct btrfs_scrub_progress *progress);
  2974. static inline void btrfs_init_full_stripe_locks_tree(
  2975. struct btrfs_full_stripe_locks_tree *locks_root)
  2976. {
  2977. locks_root->root = RB_ROOT;
  2978. mutex_init(&locks_root->lock);
  2979. }
  2980. /* dev-replace.c */
  2981. void btrfs_bio_counter_inc_blocked(struct btrfs_fs_info *fs_info);
  2982. void btrfs_bio_counter_inc_noblocked(struct btrfs_fs_info *fs_info);
  2983. void btrfs_bio_counter_sub(struct btrfs_fs_info *fs_info, s64 amount);
  2984. static inline void btrfs_bio_counter_dec(struct btrfs_fs_info *fs_info)
  2985. {
  2986. btrfs_bio_counter_sub(fs_info, 1);
  2987. }
  2988. /* reada.c */
  2989. struct reada_control {
  2990. struct btrfs_fs_info *fs_info; /* tree to prefetch */
  2991. struct btrfs_key key_start;
  2992. struct btrfs_key key_end; /* exclusive */
  2993. atomic_t elems;
  2994. struct kref refcnt;
  2995. wait_queue_head_t wait;
  2996. };
  2997. struct reada_control *btrfs_reada_add(struct btrfs_root *root,
  2998. struct btrfs_key *start, struct btrfs_key *end);
  2999. int btrfs_reada_wait(void *handle);
  3000. void btrfs_reada_detach(void *handle);
  3001. int btree_readahead_hook(struct extent_buffer *eb, int err);
  3002. void btrfs_reada_remove_dev(struct btrfs_device *dev);
  3003. void btrfs_reada_undo_remove_dev(struct btrfs_device *dev);
  3004. static inline int is_fstree(u64 rootid)
  3005. {
  3006. if (rootid == BTRFS_FS_TREE_OBJECTID ||
  3007. ((s64)rootid >= (s64)BTRFS_FIRST_FREE_OBJECTID &&
  3008. !btrfs_qgroup_level(rootid)))
  3009. return 1;
  3010. return 0;
  3011. }
  3012. static inline int btrfs_defrag_cancelled(struct btrfs_fs_info *fs_info)
  3013. {
  3014. return signal_pending(current);
  3015. }
  3016. #define in_range(b, first, len) ((b) >= (first) && (b) < (first) + (len))
  3017. /* Sanity test specific functions */
  3018. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3019. void btrfs_test_inode_set_ops(struct inode *inode);
  3020. void btrfs_test_destroy_inode(struct inode *inode);
  3021. static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
  3022. {
  3023. return test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  3024. }
  3025. #else
  3026. static inline int btrfs_is_testing(struct btrfs_fs_info *fs_info)
  3027. {
  3028. return 0;
  3029. }
  3030. #endif
  3031. #endif