ctree.h 122 KB

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