disk-io.c 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2007 Oracle. All rights reserved.
  4. */
  5. #include <linux/fs.h>
  6. #include <linux/blkdev.h>
  7. #include <linux/radix-tree.h>
  8. #include <linux/writeback.h>
  9. #include <linux/buffer_head.h>
  10. #include <linux/workqueue.h>
  11. #include <linux/kthread.h>
  12. #include <linux/slab.h>
  13. #include <linux/migrate.h>
  14. #include <linux/ratelimit.h>
  15. #include <linux/uuid.h>
  16. #include <linux/semaphore.h>
  17. #include <linux/error-injection.h>
  18. #include <linux/crc32c.h>
  19. #include <asm/unaligned.h>
  20. #include "ctree.h"
  21. #include "disk-io.h"
  22. #include "transaction.h"
  23. #include "btrfs_inode.h"
  24. #include "volumes.h"
  25. #include "print-tree.h"
  26. #include "locking.h"
  27. #include "tree-log.h"
  28. #include "free-space-cache.h"
  29. #include "free-space-tree.h"
  30. #include "inode-map.h"
  31. #include "check-integrity.h"
  32. #include "rcu-string.h"
  33. #include "dev-replace.h"
  34. #include "raid56.h"
  35. #include "sysfs.h"
  36. #include "qgroup.h"
  37. #include "compression.h"
  38. #include "tree-checker.h"
  39. #include "ref-verify.h"
  40. #ifdef CONFIG_X86
  41. #include <asm/cpufeature.h>
  42. #endif
  43. #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
  44. BTRFS_HEADER_FLAG_RELOC |\
  45. BTRFS_SUPER_FLAG_ERROR |\
  46. BTRFS_SUPER_FLAG_SEEDING |\
  47. BTRFS_SUPER_FLAG_METADUMP |\
  48. BTRFS_SUPER_FLAG_METADUMP_V2)
  49. static const struct extent_io_ops btree_extent_io_ops;
  50. static void end_workqueue_fn(struct btrfs_work *work);
  51. static void free_fs_root(struct btrfs_root *root);
  52. static void btrfs_destroy_ordered_extents(struct btrfs_root *root);
  53. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  54. struct btrfs_fs_info *fs_info);
  55. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root);
  56. static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
  57. struct extent_io_tree *dirty_pages,
  58. int mark);
  59. static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
  60. struct extent_io_tree *pinned_extents);
  61. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
  62. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
  63. /*
  64. * btrfs_end_io_wq structs are used to do processing in task context when an IO
  65. * is complete. This is used during reads to verify checksums, and it is used
  66. * by writes to insert metadata for new file extents after IO is complete.
  67. */
  68. struct btrfs_end_io_wq {
  69. struct bio *bio;
  70. bio_end_io_t *end_io;
  71. void *private;
  72. struct btrfs_fs_info *info;
  73. blk_status_t status;
  74. enum btrfs_wq_endio_type metadata;
  75. struct btrfs_work work;
  76. };
  77. static struct kmem_cache *btrfs_end_io_wq_cache;
  78. int __init btrfs_end_io_wq_init(void)
  79. {
  80. btrfs_end_io_wq_cache = kmem_cache_create("btrfs_end_io_wq",
  81. sizeof(struct btrfs_end_io_wq),
  82. 0,
  83. SLAB_MEM_SPREAD,
  84. NULL);
  85. if (!btrfs_end_io_wq_cache)
  86. return -ENOMEM;
  87. return 0;
  88. }
  89. void __cold btrfs_end_io_wq_exit(void)
  90. {
  91. kmem_cache_destroy(btrfs_end_io_wq_cache);
  92. }
  93. /*
  94. * async submit bios are used to offload expensive checksumming
  95. * onto the worker threads. They checksum file and metadata bios
  96. * just before they are sent down the IO stack.
  97. */
  98. struct async_submit_bio {
  99. void *private_data;
  100. struct bio *bio;
  101. extent_submit_bio_start_t *submit_bio_start;
  102. int mirror_num;
  103. /*
  104. * bio_offset is optional, can be used if the pages in the bio
  105. * can't tell us where in the file the bio should go
  106. */
  107. u64 bio_offset;
  108. struct btrfs_work work;
  109. blk_status_t status;
  110. };
  111. /*
  112. * Lockdep class keys for extent_buffer->lock's in this root. For a given
  113. * eb, the lockdep key is determined by the btrfs_root it belongs to and
  114. * the level the eb occupies in the tree.
  115. *
  116. * Different roots are used for different purposes and may nest inside each
  117. * other and they require separate keysets. As lockdep keys should be
  118. * static, assign keysets according to the purpose of the root as indicated
  119. * by btrfs_root->objectid. This ensures that all special purpose roots
  120. * have separate keysets.
  121. *
  122. * Lock-nesting across peer nodes is always done with the immediate parent
  123. * node locked thus preventing deadlock. As lockdep doesn't know this, use
  124. * subclass to avoid triggering lockdep warning in such cases.
  125. *
  126. * The key is set by the readpage_end_io_hook after the buffer has passed
  127. * csum validation but before the pages are unlocked. It is also set by
  128. * btrfs_init_new_buffer on freshly allocated blocks.
  129. *
  130. * We also add a check to make sure the highest level of the tree is the
  131. * same as our lockdep setup here. If BTRFS_MAX_LEVEL changes, this code
  132. * needs update as well.
  133. */
  134. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  135. # if BTRFS_MAX_LEVEL != 8
  136. # error
  137. # endif
  138. static struct btrfs_lockdep_keyset {
  139. u64 id; /* root objectid */
  140. const char *name_stem; /* lock name stem */
  141. char names[BTRFS_MAX_LEVEL + 1][20];
  142. struct lock_class_key keys[BTRFS_MAX_LEVEL + 1];
  143. } btrfs_lockdep_keysets[] = {
  144. { .id = BTRFS_ROOT_TREE_OBJECTID, .name_stem = "root" },
  145. { .id = BTRFS_EXTENT_TREE_OBJECTID, .name_stem = "extent" },
  146. { .id = BTRFS_CHUNK_TREE_OBJECTID, .name_stem = "chunk" },
  147. { .id = BTRFS_DEV_TREE_OBJECTID, .name_stem = "dev" },
  148. { .id = BTRFS_FS_TREE_OBJECTID, .name_stem = "fs" },
  149. { .id = BTRFS_CSUM_TREE_OBJECTID, .name_stem = "csum" },
  150. { .id = BTRFS_QUOTA_TREE_OBJECTID, .name_stem = "quota" },
  151. { .id = BTRFS_TREE_LOG_OBJECTID, .name_stem = "log" },
  152. { .id = BTRFS_TREE_RELOC_OBJECTID, .name_stem = "treloc" },
  153. { .id = BTRFS_DATA_RELOC_TREE_OBJECTID, .name_stem = "dreloc" },
  154. { .id = BTRFS_UUID_TREE_OBJECTID, .name_stem = "uuid" },
  155. { .id = BTRFS_FREE_SPACE_TREE_OBJECTID, .name_stem = "free-space" },
  156. { .id = 0, .name_stem = "tree" },
  157. };
  158. void __init btrfs_init_lockdep(void)
  159. {
  160. int i, j;
  161. /* initialize lockdep class names */
  162. for (i = 0; i < ARRAY_SIZE(btrfs_lockdep_keysets); i++) {
  163. struct btrfs_lockdep_keyset *ks = &btrfs_lockdep_keysets[i];
  164. for (j = 0; j < ARRAY_SIZE(ks->names); j++)
  165. snprintf(ks->names[j], sizeof(ks->names[j]),
  166. "btrfs-%s-%02d", ks->name_stem, j);
  167. }
  168. }
  169. void btrfs_set_buffer_lockdep_class(u64 objectid, struct extent_buffer *eb,
  170. int level)
  171. {
  172. struct btrfs_lockdep_keyset *ks;
  173. BUG_ON(level >= ARRAY_SIZE(ks->keys));
  174. /* find the matching keyset, id 0 is the default entry */
  175. for (ks = btrfs_lockdep_keysets; ks->id; ks++)
  176. if (ks->id == objectid)
  177. break;
  178. lockdep_set_class_and_name(&eb->lock,
  179. &ks->keys[level], ks->names[level]);
  180. }
  181. #endif
  182. /*
  183. * extents on the btree inode are pretty simple, there's one extent
  184. * that covers the entire device
  185. */
  186. struct extent_map *btree_get_extent(struct btrfs_inode *inode,
  187. struct page *page, size_t pg_offset, u64 start, u64 len,
  188. int create)
  189. {
  190. struct btrfs_fs_info *fs_info = inode->root->fs_info;
  191. struct extent_map_tree *em_tree = &inode->extent_tree;
  192. struct extent_map *em;
  193. int ret;
  194. read_lock(&em_tree->lock);
  195. em = lookup_extent_mapping(em_tree, start, len);
  196. if (em) {
  197. em->bdev = fs_info->fs_devices->latest_bdev;
  198. read_unlock(&em_tree->lock);
  199. goto out;
  200. }
  201. read_unlock(&em_tree->lock);
  202. em = alloc_extent_map();
  203. if (!em) {
  204. em = ERR_PTR(-ENOMEM);
  205. goto out;
  206. }
  207. em->start = 0;
  208. em->len = (u64)-1;
  209. em->block_len = (u64)-1;
  210. em->block_start = 0;
  211. em->bdev = fs_info->fs_devices->latest_bdev;
  212. write_lock(&em_tree->lock);
  213. ret = add_extent_mapping(em_tree, em, 0);
  214. if (ret == -EEXIST) {
  215. free_extent_map(em);
  216. em = lookup_extent_mapping(em_tree, start, len);
  217. if (!em)
  218. em = ERR_PTR(-EIO);
  219. } else if (ret) {
  220. free_extent_map(em);
  221. em = ERR_PTR(ret);
  222. }
  223. write_unlock(&em_tree->lock);
  224. out:
  225. return em;
  226. }
  227. u32 btrfs_csum_data(const char *data, u32 seed, size_t len)
  228. {
  229. return crc32c(seed, data, len);
  230. }
  231. void btrfs_csum_final(u32 crc, u8 *result)
  232. {
  233. put_unaligned_le32(~crc, result);
  234. }
  235. /*
  236. * compute the csum for a btree block, and either verify it or write it
  237. * into the csum field of the block.
  238. */
  239. static int csum_tree_block(struct btrfs_fs_info *fs_info,
  240. struct extent_buffer *buf,
  241. int verify)
  242. {
  243. u16 csum_size = btrfs_super_csum_size(fs_info->super_copy);
  244. char result[BTRFS_CSUM_SIZE];
  245. unsigned long len;
  246. unsigned long cur_len;
  247. unsigned long offset = BTRFS_CSUM_SIZE;
  248. char *kaddr;
  249. unsigned long map_start;
  250. unsigned long map_len;
  251. int err;
  252. u32 crc = ~(u32)0;
  253. len = buf->len - offset;
  254. while (len > 0) {
  255. err = map_private_extent_buffer(buf, offset, 32,
  256. &kaddr, &map_start, &map_len);
  257. if (err)
  258. return err;
  259. cur_len = min(len, map_len - (offset - map_start));
  260. crc = btrfs_csum_data(kaddr + offset - map_start,
  261. crc, cur_len);
  262. len -= cur_len;
  263. offset += cur_len;
  264. }
  265. memset(result, 0, BTRFS_CSUM_SIZE);
  266. btrfs_csum_final(crc, result);
  267. if (verify) {
  268. if (memcmp_extent_buffer(buf, result, 0, csum_size)) {
  269. u32 val;
  270. u32 found = 0;
  271. memcpy(&found, result, csum_size);
  272. read_extent_buffer(buf, &val, 0, csum_size);
  273. btrfs_warn_rl(fs_info,
  274. "%s checksum verify failed on %llu wanted %X found %X level %d",
  275. fs_info->sb->s_id, buf->start,
  276. val, found, btrfs_header_level(buf));
  277. return -EUCLEAN;
  278. }
  279. } else {
  280. write_extent_buffer(buf, result, 0, csum_size);
  281. }
  282. return 0;
  283. }
  284. /*
  285. * we can't consider a given block up to date unless the transid of the
  286. * block matches the transid in the parent node's pointer. This is how we
  287. * detect blocks that either didn't get written at all or got written
  288. * in the wrong place.
  289. */
  290. static int verify_parent_transid(struct extent_io_tree *io_tree,
  291. struct extent_buffer *eb, u64 parent_transid,
  292. int atomic)
  293. {
  294. struct extent_state *cached_state = NULL;
  295. int ret;
  296. bool need_lock = (current->journal_info == BTRFS_SEND_TRANS_STUB);
  297. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  298. return 0;
  299. if (atomic)
  300. return -EAGAIN;
  301. if (need_lock) {
  302. btrfs_tree_read_lock(eb);
  303. btrfs_set_lock_blocking_rw(eb, BTRFS_READ_LOCK);
  304. }
  305. lock_extent_bits(io_tree, eb->start, eb->start + eb->len - 1,
  306. &cached_state);
  307. if (extent_buffer_uptodate(eb) &&
  308. btrfs_header_generation(eb) == parent_transid) {
  309. ret = 0;
  310. goto out;
  311. }
  312. btrfs_err_rl(eb->fs_info,
  313. "parent transid verify failed on %llu wanted %llu found %llu",
  314. eb->start,
  315. parent_transid, btrfs_header_generation(eb));
  316. ret = 1;
  317. /*
  318. * Things reading via commit roots that don't have normal protection,
  319. * like send, can have a really old block in cache that may point at a
  320. * block that has been freed and re-allocated. So don't clear uptodate
  321. * if we find an eb that is under IO (dirty/writeback) because we could
  322. * end up reading in the stale data and then writing it back out and
  323. * making everybody very sad.
  324. */
  325. if (!extent_buffer_under_io(eb))
  326. clear_extent_buffer_uptodate(eb);
  327. out:
  328. unlock_extent_cached(io_tree, eb->start, eb->start + eb->len - 1,
  329. &cached_state);
  330. if (need_lock)
  331. btrfs_tree_read_unlock_blocking(eb);
  332. return ret;
  333. }
  334. /*
  335. * Return 0 if the superblock checksum type matches the checksum value of that
  336. * algorithm. Pass the raw disk superblock data.
  337. */
  338. static int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
  339. char *raw_disk_sb)
  340. {
  341. struct btrfs_super_block *disk_sb =
  342. (struct btrfs_super_block *)raw_disk_sb;
  343. u16 csum_type = btrfs_super_csum_type(disk_sb);
  344. int ret = 0;
  345. if (csum_type == BTRFS_CSUM_TYPE_CRC32) {
  346. u32 crc = ~(u32)0;
  347. char result[sizeof(crc)];
  348. /*
  349. * The super_block structure does not span the whole
  350. * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space
  351. * is filled with zeros and is included in the checksum.
  352. */
  353. crc = btrfs_csum_data(raw_disk_sb + BTRFS_CSUM_SIZE,
  354. crc, BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  355. btrfs_csum_final(crc, result);
  356. if (memcmp(raw_disk_sb, result, sizeof(result)))
  357. ret = 1;
  358. }
  359. if (csum_type >= ARRAY_SIZE(btrfs_csum_sizes)) {
  360. btrfs_err(fs_info, "unsupported checksum algorithm %u",
  361. csum_type);
  362. ret = 1;
  363. }
  364. return ret;
  365. }
  366. static int verify_level_key(struct btrfs_fs_info *fs_info,
  367. struct extent_buffer *eb, int level,
  368. struct btrfs_key *first_key, u64 parent_transid)
  369. {
  370. int found_level;
  371. struct btrfs_key found_key;
  372. int ret;
  373. found_level = btrfs_header_level(eb);
  374. if (found_level != level) {
  375. #ifdef CONFIG_BTRFS_DEBUG
  376. WARN_ON(1);
  377. btrfs_err(fs_info,
  378. "tree level mismatch detected, bytenr=%llu level expected=%u has=%u",
  379. eb->start, level, found_level);
  380. #endif
  381. return -EIO;
  382. }
  383. if (!first_key)
  384. return 0;
  385. /*
  386. * For live tree block (new tree blocks in current transaction),
  387. * we need proper lock context to avoid race, which is impossible here.
  388. * So we only checks tree blocks which is read from disk, whose
  389. * generation <= fs_info->last_trans_committed.
  390. */
  391. if (btrfs_header_generation(eb) > fs_info->last_trans_committed)
  392. return 0;
  393. if (found_level)
  394. btrfs_node_key_to_cpu(eb, &found_key, 0);
  395. else
  396. btrfs_item_key_to_cpu(eb, &found_key, 0);
  397. ret = btrfs_comp_cpu_keys(first_key, &found_key);
  398. #ifdef CONFIG_BTRFS_DEBUG
  399. if (ret) {
  400. WARN_ON(1);
  401. btrfs_err(fs_info,
  402. "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
  403. eb->start, parent_transid, first_key->objectid,
  404. first_key->type, first_key->offset,
  405. found_key.objectid, found_key.type,
  406. found_key.offset);
  407. }
  408. #endif
  409. return ret;
  410. }
  411. /*
  412. * helper to read a given tree block, doing retries as required when
  413. * the checksums don't match and we have alternate mirrors to try.
  414. *
  415. * @parent_transid: expected transid, skip check if 0
  416. * @level: expected level, mandatory check
  417. * @first_key: expected key of first slot, skip check if NULL
  418. */
  419. static int btree_read_extent_buffer_pages(struct btrfs_fs_info *fs_info,
  420. struct extent_buffer *eb,
  421. u64 parent_transid, int level,
  422. struct btrfs_key *first_key)
  423. {
  424. struct extent_io_tree *io_tree;
  425. int failed = 0;
  426. int ret;
  427. int num_copies = 0;
  428. int mirror_num = 0;
  429. int failed_mirror = 0;
  430. clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  431. io_tree = &BTRFS_I(fs_info->btree_inode)->io_tree;
  432. while (1) {
  433. ret = read_extent_buffer_pages(io_tree, eb, WAIT_COMPLETE,
  434. mirror_num);
  435. if (!ret) {
  436. if (verify_parent_transid(io_tree, eb,
  437. parent_transid, 0))
  438. ret = -EIO;
  439. else if (verify_level_key(fs_info, eb, level,
  440. first_key, parent_transid))
  441. ret = -EUCLEAN;
  442. else
  443. break;
  444. }
  445. /*
  446. * This buffer's crc is fine, but its contents are corrupted, so
  447. * there is no reason to read the other copies, they won't be
  448. * any less wrong.
  449. */
  450. if (test_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags) ||
  451. ret == -EUCLEAN)
  452. break;
  453. num_copies = btrfs_num_copies(fs_info,
  454. eb->start, eb->len);
  455. if (num_copies == 1)
  456. break;
  457. if (!failed_mirror) {
  458. failed = 1;
  459. failed_mirror = eb->read_mirror;
  460. }
  461. mirror_num++;
  462. if (mirror_num == failed_mirror)
  463. mirror_num++;
  464. if (mirror_num > num_copies)
  465. break;
  466. }
  467. if (failed && !ret && failed_mirror)
  468. repair_eb_io_failure(fs_info, eb, failed_mirror);
  469. return ret;
  470. }
  471. /*
  472. * checksum a dirty tree block before IO. This has extra checks to make sure
  473. * we only fill in the checksum field in the first page of a multi-page block
  474. */
  475. static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
  476. {
  477. u64 start = page_offset(page);
  478. u64 found_start;
  479. struct extent_buffer *eb;
  480. eb = (struct extent_buffer *)page->private;
  481. if (page != eb->pages[0])
  482. return 0;
  483. found_start = btrfs_header_bytenr(eb);
  484. /*
  485. * Please do not consolidate these warnings into a single if.
  486. * It is useful to know what went wrong.
  487. */
  488. if (WARN_ON(found_start != start))
  489. return -EUCLEAN;
  490. if (WARN_ON(!PageUptodate(page)))
  491. return -EUCLEAN;
  492. ASSERT(memcmp_extent_buffer(eb, fs_info->fsid,
  493. btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
  494. return csum_tree_block(fs_info, eb, 0);
  495. }
  496. static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
  497. struct extent_buffer *eb)
  498. {
  499. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices;
  500. u8 fsid[BTRFS_FSID_SIZE];
  501. int ret = 1;
  502. read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
  503. while (fs_devices) {
  504. if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
  505. ret = 0;
  506. break;
  507. }
  508. fs_devices = fs_devices->seed;
  509. }
  510. return ret;
  511. }
  512. static int btree_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
  513. u64 phy_offset, struct page *page,
  514. u64 start, u64 end, int mirror)
  515. {
  516. u64 found_start;
  517. int found_level;
  518. struct extent_buffer *eb;
  519. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  520. struct btrfs_fs_info *fs_info = root->fs_info;
  521. int ret = 0;
  522. int reads_done;
  523. if (!page->private)
  524. goto out;
  525. eb = (struct extent_buffer *)page->private;
  526. /* the pending IO might have been the only thing that kept this buffer
  527. * in memory. Make sure we have a ref for all this other checks
  528. */
  529. extent_buffer_get(eb);
  530. reads_done = atomic_dec_and_test(&eb->io_pages);
  531. if (!reads_done)
  532. goto err;
  533. eb->read_mirror = mirror;
  534. if (test_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags)) {
  535. ret = -EIO;
  536. goto err;
  537. }
  538. found_start = btrfs_header_bytenr(eb);
  539. if (found_start != eb->start) {
  540. btrfs_err_rl(fs_info, "bad tree block start, want %llu have %llu",
  541. eb->start, found_start);
  542. ret = -EIO;
  543. goto err;
  544. }
  545. if (check_tree_block_fsid(fs_info, eb)) {
  546. btrfs_err_rl(fs_info, "bad fsid on block %llu",
  547. eb->start);
  548. ret = -EIO;
  549. goto err;
  550. }
  551. found_level = btrfs_header_level(eb);
  552. if (found_level >= BTRFS_MAX_LEVEL) {
  553. btrfs_err(fs_info, "bad tree block level %d on %llu",
  554. (int)btrfs_header_level(eb), eb->start);
  555. ret = -EIO;
  556. goto err;
  557. }
  558. btrfs_set_buffer_lockdep_class(btrfs_header_owner(eb),
  559. eb, found_level);
  560. ret = csum_tree_block(fs_info, eb, 1);
  561. if (ret)
  562. goto err;
  563. /*
  564. * If this is a leaf block and it is corrupt, set the corrupt bit so
  565. * that we don't try and read the other copies of this block, just
  566. * return -EIO.
  567. */
  568. if (found_level == 0 && btrfs_check_leaf_full(fs_info, eb)) {
  569. set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  570. ret = -EIO;
  571. }
  572. if (found_level > 0 && btrfs_check_node(fs_info, eb))
  573. ret = -EIO;
  574. if (!ret)
  575. set_extent_buffer_uptodate(eb);
  576. err:
  577. if (reads_done &&
  578. test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  579. btree_readahead_hook(eb, ret);
  580. if (ret) {
  581. /*
  582. * our io error hook is going to dec the io pages
  583. * again, we have to make sure it has something
  584. * to decrement
  585. */
  586. atomic_inc(&eb->io_pages);
  587. clear_extent_buffer_uptodate(eb);
  588. }
  589. free_extent_buffer(eb);
  590. out:
  591. return ret;
  592. }
  593. static int btree_io_failed_hook(struct page *page, int failed_mirror)
  594. {
  595. struct extent_buffer *eb;
  596. eb = (struct extent_buffer *)page->private;
  597. set_bit(EXTENT_BUFFER_READ_ERR, &eb->bflags);
  598. eb->read_mirror = failed_mirror;
  599. atomic_dec(&eb->io_pages);
  600. if (test_and_clear_bit(EXTENT_BUFFER_READAHEAD, &eb->bflags))
  601. btree_readahead_hook(eb, -EIO);
  602. return -EIO; /* we fixed nothing */
  603. }
  604. static void end_workqueue_bio(struct bio *bio)
  605. {
  606. struct btrfs_end_io_wq *end_io_wq = bio->bi_private;
  607. struct btrfs_fs_info *fs_info;
  608. struct btrfs_workqueue *wq;
  609. btrfs_work_func_t func;
  610. fs_info = end_io_wq->info;
  611. end_io_wq->status = bio->bi_status;
  612. if (bio_op(bio) == REQ_OP_WRITE) {
  613. if (end_io_wq->metadata == BTRFS_WQ_ENDIO_METADATA) {
  614. wq = fs_info->endio_meta_write_workers;
  615. func = btrfs_endio_meta_write_helper;
  616. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_FREE_SPACE) {
  617. wq = fs_info->endio_freespace_worker;
  618. func = btrfs_freespace_write_helper;
  619. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  620. wq = fs_info->endio_raid56_workers;
  621. func = btrfs_endio_raid56_helper;
  622. } else {
  623. wq = fs_info->endio_write_workers;
  624. func = btrfs_endio_write_helper;
  625. }
  626. } else {
  627. if (unlikely(end_io_wq->metadata ==
  628. BTRFS_WQ_ENDIO_DIO_REPAIR)) {
  629. wq = fs_info->endio_repair_workers;
  630. func = btrfs_endio_repair_helper;
  631. } else if (end_io_wq->metadata == BTRFS_WQ_ENDIO_RAID56) {
  632. wq = fs_info->endio_raid56_workers;
  633. func = btrfs_endio_raid56_helper;
  634. } else if (end_io_wq->metadata) {
  635. wq = fs_info->endio_meta_workers;
  636. func = btrfs_endio_meta_helper;
  637. } else {
  638. wq = fs_info->endio_workers;
  639. func = btrfs_endio_helper;
  640. }
  641. }
  642. btrfs_init_work(&end_io_wq->work, func, end_workqueue_fn, NULL, NULL);
  643. btrfs_queue_work(wq, &end_io_wq->work);
  644. }
  645. blk_status_t btrfs_bio_wq_end_io(struct btrfs_fs_info *info, struct bio *bio,
  646. enum btrfs_wq_endio_type metadata)
  647. {
  648. struct btrfs_end_io_wq *end_io_wq;
  649. end_io_wq = kmem_cache_alloc(btrfs_end_io_wq_cache, GFP_NOFS);
  650. if (!end_io_wq)
  651. return BLK_STS_RESOURCE;
  652. end_io_wq->private = bio->bi_private;
  653. end_io_wq->end_io = bio->bi_end_io;
  654. end_io_wq->info = info;
  655. end_io_wq->status = 0;
  656. end_io_wq->bio = bio;
  657. end_io_wq->metadata = metadata;
  658. bio->bi_private = end_io_wq;
  659. bio->bi_end_io = end_workqueue_bio;
  660. return 0;
  661. }
  662. static void run_one_async_start(struct btrfs_work *work)
  663. {
  664. struct async_submit_bio *async;
  665. blk_status_t ret;
  666. async = container_of(work, struct async_submit_bio, work);
  667. ret = async->submit_bio_start(async->private_data, async->bio,
  668. async->bio_offset);
  669. if (ret)
  670. async->status = ret;
  671. }
  672. static void run_one_async_done(struct btrfs_work *work)
  673. {
  674. struct async_submit_bio *async;
  675. async = container_of(work, struct async_submit_bio, work);
  676. /* If an error occurred we just want to clean up the bio and move on */
  677. if (async->status) {
  678. async->bio->bi_status = async->status;
  679. bio_endio(async->bio);
  680. return;
  681. }
  682. btrfs_submit_bio_done(async->private_data, async->bio, async->mirror_num);
  683. }
  684. static void run_one_async_free(struct btrfs_work *work)
  685. {
  686. struct async_submit_bio *async;
  687. async = container_of(work, struct async_submit_bio, work);
  688. kfree(async);
  689. }
  690. blk_status_t btrfs_wq_submit_bio(struct btrfs_fs_info *fs_info, struct bio *bio,
  691. int mirror_num, unsigned long bio_flags,
  692. u64 bio_offset, void *private_data,
  693. extent_submit_bio_start_t *submit_bio_start)
  694. {
  695. struct async_submit_bio *async;
  696. async = kmalloc(sizeof(*async), GFP_NOFS);
  697. if (!async)
  698. return BLK_STS_RESOURCE;
  699. async->private_data = private_data;
  700. async->bio = bio;
  701. async->mirror_num = mirror_num;
  702. async->submit_bio_start = submit_bio_start;
  703. btrfs_init_work(&async->work, btrfs_worker_helper, run_one_async_start,
  704. run_one_async_done, run_one_async_free);
  705. async->bio_offset = bio_offset;
  706. async->status = 0;
  707. if (op_is_sync(bio->bi_opf))
  708. btrfs_set_work_high_priority(&async->work);
  709. btrfs_queue_work(fs_info->workers, &async->work);
  710. return 0;
  711. }
  712. static blk_status_t btree_csum_one_bio(struct bio *bio)
  713. {
  714. struct bio_vec *bvec;
  715. struct btrfs_root *root;
  716. int i, ret = 0;
  717. ASSERT(!bio_flagged(bio, BIO_CLONED));
  718. bio_for_each_segment_all(bvec, bio, i) {
  719. root = BTRFS_I(bvec->bv_page->mapping->host)->root;
  720. ret = csum_dirty_buffer(root->fs_info, bvec->bv_page);
  721. if (ret)
  722. break;
  723. }
  724. return errno_to_blk_status(ret);
  725. }
  726. static blk_status_t btree_submit_bio_start(void *private_data, struct bio *bio,
  727. u64 bio_offset)
  728. {
  729. /*
  730. * when we're called for a write, we're already in the async
  731. * submission context. Just jump into btrfs_map_bio
  732. */
  733. return btree_csum_one_bio(bio);
  734. }
  735. static int check_async_write(struct btrfs_inode *bi)
  736. {
  737. if (atomic_read(&bi->sync_writers))
  738. return 0;
  739. #ifdef CONFIG_X86
  740. if (static_cpu_has(X86_FEATURE_XMM4_2))
  741. return 0;
  742. #endif
  743. return 1;
  744. }
  745. static blk_status_t btree_submit_bio_hook(void *private_data, struct bio *bio,
  746. int mirror_num, unsigned long bio_flags,
  747. u64 bio_offset)
  748. {
  749. struct inode *inode = private_data;
  750. struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
  751. int async = check_async_write(BTRFS_I(inode));
  752. blk_status_t ret;
  753. if (bio_op(bio) != REQ_OP_WRITE) {
  754. /*
  755. * called for a read, do the setup so that checksum validation
  756. * can happen in the async kernel threads
  757. */
  758. ret = btrfs_bio_wq_end_io(fs_info, bio,
  759. BTRFS_WQ_ENDIO_METADATA);
  760. if (ret)
  761. goto out_w_error;
  762. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  763. } else if (!async) {
  764. ret = btree_csum_one_bio(bio);
  765. if (ret)
  766. goto out_w_error;
  767. ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
  768. } else {
  769. /*
  770. * kthread helpers are used to submit writes so that
  771. * checksumming can happen in parallel across all CPUs
  772. */
  773. ret = btrfs_wq_submit_bio(fs_info, bio, mirror_num, 0,
  774. bio_offset, private_data,
  775. btree_submit_bio_start);
  776. }
  777. if (ret)
  778. goto out_w_error;
  779. return 0;
  780. out_w_error:
  781. bio->bi_status = ret;
  782. bio_endio(bio);
  783. return ret;
  784. }
  785. #ifdef CONFIG_MIGRATION
  786. static int btree_migratepage(struct address_space *mapping,
  787. struct page *newpage, struct page *page,
  788. enum migrate_mode mode)
  789. {
  790. /*
  791. * we can't safely write a btree page from here,
  792. * we haven't done the locking hook
  793. */
  794. if (PageDirty(page))
  795. return -EAGAIN;
  796. /*
  797. * Buffers may be managed in a filesystem specific way.
  798. * We must have no buffers or drop them.
  799. */
  800. if (page_has_private(page) &&
  801. !try_to_release_page(page, GFP_KERNEL))
  802. return -EAGAIN;
  803. return migrate_page(mapping, newpage, page, mode);
  804. }
  805. #endif
  806. static int btree_writepages(struct address_space *mapping,
  807. struct writeback_control *wbc)
  808. {
  809. struct btrfs_fs_info *fs_info;
  810. int ret;
  811. if (wbc->sync_mode == WB_SYNC_NONE) {
  812. if (wbc->for_kupdate)
  813. return 0;
  814. fs_info = BTRFS_I(mapping->host)->root->fs_info;
  815. /* this is a bit racy, but that's ok */
  816. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  817. BTRFS_DIRTY_METADATA_THRESH,
  818. fs_info->dirty_metadata_batch);
  819. if (ret < 0)
  820. return 0;
  821. }
  822. return btree_write_cache_pages(mapping, wbc);
  823. }
  824. static int btree_readpage(struct file *file, struct page *page)
  825. {
  826. struct extent_io_tree *tree;
  827. tree = &BTRFS_I(page->mapping->host)->io_tree;
  828. return extent_read_full_page(tree, page, btree_get_extent, 0);
  829. }
  830. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  831. {
  832. if (PageWriteback(page) || PageDirty(page))
  833. return 0;
  834. return try_release_extent_buffer(page);
  835. }
  836. static void btree_invalidatepage(struct page *page, unsigned int offset,
  837. unsigned int length)
  838. {
  839. struct extent_io_tree *tree;
  840. tree = &BTRFS_I(page->mapping->host)->io_tree;
  841. extent_invalidatepage(tree, page, offset);
  842. btree_releasepage(page, GFP_NOFS);
  843. if (PagePrivate(page)) {
  844. btrfs_warn(BTRFS_I(page->mapping->host)->root->fs_info,
  845. "page private not zero on page %llu",
  846. (unsigned long long)page_offset(page));
  847. ClearPagePrivate(page);
  848. set_page_private(page, 0);
  849. put_page(page);
  850. }
  851. }
  852. static int btree_set_page_dirty(struct page *page)
  853. {
  854. #ifdef DEBUG
  855. struct extent_buffer *eb;
  856. BUG_ON(!PagePrivate(page));
  857. eb = (struct extent_buffer *)page->private;
  858. BUG_ON(!eb);
  859. BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  860. BUG_ON(!atomic_read(&eb->refs));
  861. btrfs_assert_tree_locked(eb);
  862. #endif
  863. return __set_page_dirty_nobuffers(page);
  864. }
  865. static const struct address_space_operations btree_aops = {
  866. .readpage = btree_readpage,
  867. .writepages = btree_writepages,
  868. .releasepage = btree_releasepage,
  869. .invalidatepage = btree_invalidatepage,
  870. #ifdef CONFIG_MIGRATION
  871. .migratepage = btree_migratepage,
  872. #endif
  873. .set_page_dirty = btree_set_page_dirty,
  874. };
  875. void readahead_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr)
  876. {
  877. struct extent_buffer *buf = NULL;
  878. struct inode *btree_inode = fs_info->btree_inode;
  879. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  880. if (IS_ERR(buf))
  881. return;
  882. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  883. buf, WAIT_NONE, 0);
  884. free_extent_buffer(buf);
  885. }
  886. int reada_tree_block_flagged(struct btrfs_fs_info *fs_info, u64 bytenr,
  887. int mirror_num, struct extent_buffer **eb)
  888. {
  889. struct extent_buffer *buf = NULL;
  890. struct inode *btree_inode = fs_info->btree_inode;
  891. struct extent_io_tree *io_tree = &BTRFS_I(btree_inode)->io_tree;
  892. int ret;
  893. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  894. if (IS_ERR(buf))
  895. return 0;
  896. set_bit(EXTENT_BUFFER_READAHEAD, &buf->bflags);
  897. ret = read_extent_buffer_pages(io_tree, buf, WAIT_PAGE_LOCK,
  898. mirror_num);
  899. if (ret) {
  900. free_extent_buffer(buf);
  901. return ret;
  902. }
  903. if (test_bit(EXTENT_BUFFER_CORRUPT, &buf->bflags)) {
  904. free_extent_buffer(buf);
  905. return -EIO;
  906. } else if (extent_buffer_uptodate(buf)) {
  907. *eb = buf;
  908. } else {
  909. free_extent_buffer(buf);
  910. }
  911. return 0;
  912. }
  913. struct extent_buffer *btrfs_find_create_tree_block(
  914. struct btrfs_fs_info *fs_info,
  915. u64 bytenr)
  916. {
  917. if (btrfs_is_testing(fs_info))
  918. return alloc_test_extent_buffer(fs_info, bytenr);
  919. return alloc_extent_buffer(fs_info, bytenr);
  920. }
  921. int btrfs_write_tree_block(struct extent_buffer *buf)
  922. {
  923. return filemap_fdatawrite_range(buf->pages[0]->mapping, buf->start,
  924. buf->start + buf->len - 1);
  925. }
  926. void btrfs_wait_tree_block_writeback(struct extent_buffer *buf)
  927. {
  928. filemap_fdatawait_range(buf->pages[0]->mapping,
  929. buf->start, buf->start + buf->len - 1);
  930. }
  931. /*
  932. * Read tree block at logical address @bytenr and do variant basic but critical
  933. * verification.
  934. *
  935. * @parent_transid: expected transid of this tree block, skip check if 0
  936. * @level: expected level, mandatory check
  937. * @first_key: expected key in slot 0, skip check if NULL
  938. */
  939. struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
  940. u64 parent_transid, int level,
  941. struct btrfs_key *first_key)
  942. {
  943. struct extent_buffer *buf = NULL;
  944. int ret;
  945. buf = btrfs_find_create_tree_block(fs_info, bytenr);
  946. if (IS_ERR(buf))
  947. return buf;
  948. ret = btree_read_extent_buffer_pages(fs_info, buf, parent_transid,
  949. level, first_key);
  950. if (ret) {
  951. free_extent_buffer(buf);
  952. return ERR_PTR(ret);
  953. }
  954. return buf;
  955. }
  956. void clean_tree_block(struct btrfs_fs_info *fs_info,
  957. struct extent_buffer *buf)
  958. {
  959. if (btrfs_header_generation(buf) ==
  960. fs_info->running_transaction->transid) {
  961. btrfs_assert_tree_locked(buf);
  962. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY, &buf->bflags)) {
  963. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  964. -buf->len,
  965. fs_info->dirty_metadata_batch);
  966. /* ugh, clear_extent_buffer_dirty needs to lock the page */
  967. btrfs_set_lock_blocking(buf);
  968. clear_extent_buffer_dirty(buf);
  969. }
  970. }
  971. }
  972. static struct btrfs_subvolume_writers *btrfs_alloc_subvolume_writers(void)
  973. {
  974. struct btrfs_subvolume_writers *writers;
  975. int ret;
  976. writers = kmalloc(sizeof(*writers), GFP_NOFS);
  977. if (!writers)
  978. return ERR_PTR(-ENOMEM);
  979. ret = percpu_counter_init(&writers->counter, 0, GFP_NOFS);
  980. if (ret < 0) {
  981. kfree(writers);
  982. return ERR_PTR(ret);
  983. }
  984. init_waitqueue_head(&writers->wait);
  985. return writers;
  986. }
  987. static void
  988. btrfs_free_subvolume_writers(struct btrfs_subvolume_writers *writers)
  989. {
  990. percpu_counter_destroy(&writers->counter);
  991. kfree(writers);
  992. }
  993. static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
  994. u64 objectid)
  995. {
  996. bool dummy = test_bit(BTRFS_FS_STATE_DUMMY_FS_INFO, &fs_info->fs_state);
  997. root->node = NULL;
  998. root->commit_root = NULL;
  999. root->state = 0;
  1000. root->orphan_cleanup_state = 0;
  1001. root->objectid = objectid;
  1002. root->last_trans = 0;
  1003. root->highest_objectid = 0;
  1004. root->nr_delalloc_inodes = 0;
  1005. root->nr_ordered_extents = 0;
  1006. root->name = NULL;
  1007. root->inode_tree = RB_ROOT;
  1008. INIT_RADIX_TREE(&root->delayed_nodes_tree, GFP_ATOMIC);
  1009. root->block_rsv = NULL;
  1010. INIT_LIST_HEAD(&root->dirty_list);
  1011. INIT_LIST_HEAD(&root->root_list);
  1012. INIT_LIST_HEAD(&root->delalloc_inodes);
  1013. INIT_LIST_HEAD(&root->delalloc_root);
  1014. INIT_LIST_HEAD(&root->ordered_extents);
  1015. INIT_LIST_HEAD(&root->ordered_root);
  1016. INIT_LIST_HEAD(&root->logged_list[0]);
  1017. INIT_LIST_HEAD(&root->logged_list[1]);
  1018. spin_lock_init(&root->inode_lock);
  1019. spin_lock_init(&root->delalloc_lock);
  1020. spin_lock_init(&root->ordered_extent_lock);
  1021. spin_lock_init(&root->accounting_lock);
  1022. spin_lock_init(&root->log_extents_lock[0]);
  1023. spin_lock_init(&root->log_extents_lock[1]);
  1024. spin_lock_init(&root->qgroup_meta_rsv_lock);
  1025. mutex_init(&root->objectid_mutex);
  1026. mutex_init(&root->log_mutex);
  1027. mutex_init(&root->ordered_extent_mutex);
  1028. mutex_init(&root->delalloc_mutex);
  1029. init_waitqueue_head(&root->log_writer_wait);
  1030. init_waitqueue_head(&root->log_commit_wait[0]);
  1031. init_waitqueue_head(&root->log_commit_wait[1]);
  1032. INIT_LIST_HEAD(&root->log_ctxs[0]);
  1033. INIT_LIST_HEAD(&root->log_ctxs[1]);
  1034. atomic_set(&root->log_commit[0], 0);
  1035. atomic_set(&root->log_commit[1], 0);
  1036. atomic_set(&root->log_writers, 0);
  1037. atomic_set(&root->log_batch, 0);
  1038. refcount_set(&root->refs, 1);
  1039. atomic_set(&root->will_be_snapshotted, 0);
  1040. root->log_transid = 0;
  1041. root->log_transid_committed = -1;
  1042. root->last_log_commit = 0;
  1043. if (!dummy)
  1044. extent_io_tree_init(&root->dirty_log_pages, NULL);
  1045. memset(&root->root_key, 0, sizeof(root->root_key));
  1046. memset(&root->root_item, 0, sizeof(root->root_item));
  1047. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  1048. if (!dummy)
  1049. root->defrag_trans_start = fs_info->generation;
  1050. else
  1051. root->defrag_trans_start = 0;
  1052. root->root_key.objectid = objectid;
  1053. root->anon_dev = 0;
  1054. spin_lock_init(&root->root_item_lock);
  1055. }
  1056. static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
  1057. gfp_t flags)
  1058. {
  1059. struct btrfs_root *root = kzalloc(sizeof(*root), flags);
  1060. if (root)
  1061. root->fs_info = fs_info;
  1062. return root;
  1063. }
  1064. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  1065. /* Should only be used by the testing infrastructure */
  1066. struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
  1067. {
  1068. struct btrfs_root *root;
  1069. if (!fs_info)
  1070. return ERR_PTR(-EINVAL);
  1071. root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1072. if (!root)
  1073. return ERR_PTR(-ENOMEM);
  1074. /* We don't use the stripesize in selftest, set it as sectorsize */
  1075. __setup_root(root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  1076. root->alloc_bytenr = 0;
  1077. return root;
  1078. }
  1079. #endif
  1080. struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
  1081. struct btrfs_fs_info *fs_info,
  1082. u64 objectid)
  1083. {
  1084. struct extent_buffer *leaf;
  1085. struct btrfs_root *tree_root = fs_info->tree_root;
  1086. struct btrfs_root *root;
  1087. struct btrfs_key key;
  1088. int ret = 0;
  1089. uuid_le uuid = NULL_UUID_LE;
  1090. root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1091. if (!root)
  1092. return ERR_PTR(-ENOMEM);
  1093. __setup_root(root, fs_info, objectid);
  1094. root->root_key.objectid = objectid;
  1095. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1096. root->root_key.offset = 0;
  1097. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0);
  1098. if (IS_ERR(leaf)) {
  1099. ret = PTR_ERR(leaf);
  1100. leaf = NULL;
  1101. goto fail;
  1102. }
  1103. root->node = leaf;
  1104. btrfs_mark_buffer_dirty(leaf);
  1105. root->commit_root = btrfs_root_node(root);
  1106. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1107. root->root_item.flags = 0;
  1108. root->root_item.byte_limit = 0;
  1109. btrfs_set_root_bytenr(&root->root_item, leaf->start);
  1110. btrfs_set_root_generation(&root->root_item, trans->transid);
  1111. btrfs_set_root_level(&root->root_item, 0);
  1112. btrfs_set_root_refs(&root->root_item, 1);
  1113. btrfs_set_root_used(&root->root_item, leaf->len);
  1114. btrfs_set_root_last_snapshot(&root->root_item, 0);
  1115. btrfs_set_root_dirid(&root->root_item, 0);
  1116. if (is_fstree(objectid))
  1117. uuid_le_gen(&uuid);
  1118. memcpy(root->root_item.uuid, uuid.b, BTRFS_UUID_SIZE);
  1119. root->root_item.drop_level = 0;
  1120. key.objectid = objectid;
  1121. key.type = BTRFS_ROOT_ITEM_KEY;
  1122. key.offset = 0;
  1123. ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
  1124. if (ret)
  1125. goto fail;
  1126. btrfs_tree_unlock(leaf);
  1127. return root;
  1128. fail:
  1129. if (leaf) {
  1130. btrfs_tree_unlock(leaf);
  1131. free_extent_buffer(root->commit_root);
  1132. free_extent_buffer(leaf);
  1133. }
  1134. kfree(root);
  1135. return ERR_PTR(ret);
  1136. }
  1137. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  1138. struct btrfs_fs_info *fs_info)
  1139. {
  1140. struct btrfs_root *root;
  1141. struct extent_buffer *leaf;
  1142. root = btrfs_alloc_root(fs_info, GFP_NOFS);
  1143. if (!root)
  1144. return ERR_PTR(-ENOMEM);
  1145. __setup_root(root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1146. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  1147. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  1148. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  1149. /*
  1150. * DON'T set REF_COWS for log trees
  1151. *
  1152. * log trees do not get reference counted because they go away
  1153. * before a real commit is actually done. They do store pointers
  1154. * to file data extents, and those reference counts still get
  1155. * updated (along with back refs to the log tree).
  1156. */
  1157. leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
  1158. NULL, 0, 0, 0);
  1159. if (IS_ERR(leaf)) {
  1160. kfree(root);
  1161. return ERR_CAST(leaf);
  1162. }
  1163. root->node = leaf;
  1164. btrfs_mark_buffer_dirty(root->node);
  1165. btrfs_tree_unlock(root->node);
  1166. return root;
  1167. }
  1168. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  1169. struct btrfs_fs_info *fs_info)
  1170. {
  1171. struct btrfs_root *log_root;
  1172. log_root = alloc_log_tree(trans, fs_info);
  1173. if (IS_ERR(log_root))
  1174. return PTR_ERR(log_root);
  1175. WARN_ON(fs_info->log_root_tree);
  1176. fs_info->log_root_tree = log_root;
  1177. return 0;
  1178. }
  1179. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  1180. struct btrfs_root *root)
  1181. {
  1182. struct btrfs_fs_info *fs_info = root->fs_info;
  1183. struct btrfs_root *log_root;
  1184. struct btrfs_inode_item *inode_item;
  1185. log_root = alloc_log_tree(trans, fs_info);
  1186. if (IS_ERR(log_root))
  1187. return PTR_ERR(log_root);
  1188. log_root->last_trans = trans->transid;
  1189. log_root->root_key.offset = root->root_key.objectid;
  1190. inode_item = &log_root->root_item.inode;
  1191. btrfs_set_stack_inode_generation(inode_item, 1);
  1192. btrfs_set_stack_inode_size(inode_item, 3);
  1193. btrfs_set_stack_inode_nlink(inode_item, 1);
  1194. btrfs_set_stack_inode_nbytes(inode_item,
  1195. fs_info->nodesize);
  1196. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  1197. btrfs_set_root_node(&log_root->root_item, log_root->node);
  1198. WARN_ON(root->log_root);
  1199. root->log_root = log_root;
  1200. root->log_transid = 0;
  1201. root->log_transid_committed = -1;
  1202. root->last_log_commit = 0;
  1203. return 0;
  1204. }
  1205. static struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
  1206. struct btrfs_key *key)
  1207. {
  1208. struct btrfs_root *root;
  1209. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  1210. struct btrfs_path *path;
  1211. u64 generation;
  1212. int ret;
  1213. int level;
  1214. path = btrfs_alloc_path();
  1215. if (!path)
  1216. return ERR_PTR(-ENOMEM);
  1217. root = btrfs_alloc_root(fs_info, GFP_NOFS);
  1218. if (!root) {
  1219. ret = -ENOMEM;
  1220. goto alloc_fail;
  1221. }
  1222. __setup_root(root, fs_info, key->objectid);
  1223. ret = btrfs_find_root(tree_root, key, path,
  1224. &root->root_item, &root->root_key);
  1225. if (ret) {
  1226. if (ret > 0)
  1227. ret = -ENOENT;
  1228. goto find_fail;
  1229. }
  1230. generation = btrfs_root_generation(&root->root_item);
  1231. level = btrfs_root_level(&root->root_item);
  1232. root->node = read_tree_block(fs_info,
  1233. btrfs_root_bytenr(&root->root_item),
  1234. generation, level, NULL);
  1235. if (IS_ERR(root->node)) {
  1236. ret = PTR_ERR(root->node);
  1237. goto find_fail;
  1238. } else if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
  1239. ret = -EIO;
  1240. free_extent_buffer(root->node);
  1241. goto find_fail;
  1242. }
  1243. root->commit_root = btrfs_root_node(root);
  1244. out:
  1245. btrfs_free_path(path);
  1246. return root;
  1247. find_fail:
  1248. kfree(root);
  1249. alloc_fail:
  1250. root = ERR_PTR(ret);
  1251. goto out;
  1252. }
  1253. struct btrfs_root *btrfs_read_fs_root(struct btrfs_root *tree_root,
  1254. struct btrfs_key *location)
  1255. {
  1256. struct btrfs_root *root;
  1257. root = btrfs_read_tree_root(tree_root, location);
  1258. if (IS_ERR(root))
  1259. return root;
  1260. if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID) {
  1261. set_bit(BTRFS_ROOT_REF_COWS, &root->state);
  1262. btrfs_check_and_init_root_item(&root->root_item);
  1263. }
  1264. return root;
  1265. }
  1266. int btrfs_init_fs_root(struct btrfs_root *root)
  1267. {
  1268. int ret;
  1269. struct btrfs_subvolume_writers *writers;
  1270. root->free_ino_ctl = kzalloc(sizeof(*root->free_ino_ctl), GFP_NOFS);
  1271. root->free_ino_pinned = kzalloc(sizeof(*root->free_ino_pinned),
  1272. GFP_NOFS);
  1273. if (!root->free_ino_pinned || !root->free_ino_ctl) {
  1274. ret = -ENOMEM;
  1275. goto fail;
  1276. }
  1277. writers = btrfs_alloc_subvolume_writers();
  1278. if (IS_ERR(writers)) {
  1279. ret = PTR_ERR(writers);
  1280. goto fail;
  1281. }
  1282. root->subv_writers = writers;
  1283. btrfs_init_free_ino_ctl(root);
  1284. spin_lock_init(&root->ino_cache_lock);
  1285. init_waitqueue_head(&root->ino_cache_wait);
  1286. ret = get_anon_bdev(&root->anon_dev);
  1287. if (ret)
  1288. goto fail;
  1289. mutex_lock(&root->objectid_mutex);
  1290. ret = btrfs_find_highest_objectid(root,
  1291. &root->highest_objectid);
  1292. if (ret) {
  1293. mutex_unlock(&root->objectid_mutex);
  1294. goto fail;
  1295. }
  1296. ASSERT(root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
  1297. mutex_unlock(&root->objectid_mutex);
  1298. return 0;
  1299. fail:
  1300. /* the caller is responsible to call free_fs_root */
  1301. return ret;
  1302. }
  1303. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  1304. u64 root_id)
  1305. {
  1306. struct btrfs_root *root;
  1307. spin_lock(&fs_info->fs_roots_radix_lock);
  1308. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  1309. (unsigned long)root_id);
  1310. spin_unlock(&fs_info->fs_roots_radix_lock);
  1311. return root;
  1312. }
  1313. int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
  1314. struct btrfs_root *root)
  1315. {
  1316. int ret;
  1317. ret = radix_tree_preload(GFP_NOFS);
  1318. if (ret)
  1319. return ret;
  1320. spin_lock(&fs_info->fs_roots_radix_lock);
  1321. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1322. (unsigned long)root->root_key.objectid,
  1323. root);
  1324. if (ret == 0)
  1325. set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
  1326. spin_unlock(&fs_info->fs_roots_radix_lock);
  1327. radix_tree_preload_end();
  1328. return ret;
  1329. }
  1330. struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
  1331. struct btrfs_key *location,
  1332. bool check_ref)
  1333. {
  1334. struct btrfs_root *root;
  1335. struct btrfs_path *path;
  1336. struct btrfs_key key;
  1337. int ret;
  1338. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  1339. return fs_info->tree_root;
  1340. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1341. return fs_info->extent_root;
  1342. if (location->objectid == BTRFS_CHUNK_TREE_OBJECTID)
  1343. return fs_info->chunk_root;
  1344. if (location->objectid == BTRFS_DEV_TREE_OBJECTID)
  1345. return fs_info->dev_root;
  1346. if (location->objectid == BTRFS_CSUM_TREE_OBJECTID)
  1347. return fs_info->csum_root;
  1348. if (location->objectid == BTRFS_QUOTA_TREE_OBJECTID)
  1349. return fs_info->quota_root ? fs_info->quota_root :
  1350. ERR_PTR(-ENOENT);
  1351. if (location->objectid == BTRFS_UUID_TREE_OBJECTID)
  1352. return fs_info->uuid_root ? fs_info->uuid_root :
  1353. ERR_PTR(-ENOENT);
  1354. if (location->objectid == BTRFS_FREE_SPACE_TREE_OBJECTID)
  1355. return fs_info->free_space_root ? fs_info->free_space_root :
  1356. ERR_PTR(-ENOENT);
  1357. again:
  1358. root = btrfs_lookup_fs_root(fs_info, location->objectid);
  1359. if (root) {
  1360. if (check_ref && btrfs_root_refs(&root->root_item) == 0)
  1361. return ERR_PTR(-ENOENT);
  1362. return root;
  1363. }
  1364. root = btrfs_read_fs_root(fs_info->tree_root, location);
  1365. if (IS_ERR(root))
  1366. return root;
  1367. if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
  1368. ret = -ENOENT;
  1369. goto fail;
  1370. }
  1371. ret = btrfs_init_fs_root(root);
  1372. if (ret)
  1373. goto fail;
  1374. path = btrfs_alloc_path();
  1375. if (!path) {
  1376. ret = -ENOMEM;
  1377. goto fail;
  1378. }
  1379. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1380. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1381. key.offset = location->objectid;
  1382. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  1383. btrfs_free_path(path);
  1384. if (ret < 0)
  1385. goto fail;
  1386. if (ret == 0)
  1387. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  1388. ret = btrfs_insert_fs_root(fs_info, root);
  1389. if (ret) {
  1390. if (ret == -EEXIST) {
  1391. free_fs_root(root);
  1392. goto again;
  1393. }
  1394. goto fail;
  1395. }
  1396. return root;
  1397. fail:
  1398. free_fs_root(root);
  1399. return ERR_PTR(ret);
  1400. }
  1401. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  1402. {
  1403. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  1404. int ret = 0;
  1405. struct btrfs_device *device;
  1406. struct backing_dev_info *bdi;
  1407. rcu_read_lock();
  1408. list_for_each_entry_rcu(device, &info->fs_devices->devices, dev_list) {
  1409. if (!device->bdev)
  1410. continue;
  1411. bdi = device->bdev->bd_bdi;
  1412. if (bdi_congested(bdi, bdi_bits)) {
  1413. ret = 1;
  1414. break;
  1415. }
  1416. }
  1417. rcu_read_unlock();
  1418. return ret;
  1419. }
  1420. /*
  1421. * called by the kthread helper functions to finally call the bio end_io
  1422. * functions. This is where read checksum verification actually happens
  1423. */
  1424. static void end_workqueue_fn(struct btrfs_work *work)
  1425. {
  1426. struct bio *bio;
  1427. struct btrfs_end_io_wq *end_io_wq;
  1428. end_io_wq = container_of(work, struct btrfs_end_io_wq, work);
  1429. bio = end_io_wq->bio;
  1430. bio->bi_status = end_io_wq->status;
  1431. bio->bi_private = end_io_wq->private;
  1432. bio->bi_end_io = end_io_wq->end_io;
  1433. kmem_cache_free(btrfs_end_io_wq_cache, end_io_wq);
  1434. bio_endio(bio);
  1435. }
  1436. static int cleaner_kthread(void *arg)
  1437. {
  1438. struct btrfs_root *root = arg;
  1439. struct btrfs_fs_info *fs_info = root->fs_info;
  1440. int again;
  1441. struct btrfs_trans_handle *trans;
  1442. do {
  1443. again = 0;
  1444. /* Make the cleaner go to sleep early. */
  1445. if (btrfs_need_cleaner_sleep(fs_info))
  1446. goto sleep;
  1447. /*
  1448. * Do not do anything if we might cause open_ctree() to block
  1449. * before we have finished mounting the filesystem.
  1450. */
  1451. if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
  1452. goto sleep;
  1453. if (!mutex_trylock(&fs_info->cleaner_mutex))
  1454. goto sleep;
  1455. /*
  1456. * Avoid the problem that we change the status of the fs
  1457. * during the above check and trylock.
  1458. */
  1459. if (btrfs_need_cleaner_sleep(fs_info)) {
  1460. mutex_unlock(&fs_info->cleaner_mutex);
  1461. goto sleep;
  1462. }
  1463. mutex_lock(&fs_info->cleaner_delayed_iput_mutex);
  1464. btrfs_run_delayed_iputs(fs_info);
  1465. mutex_unlock(&fs_info->cleaner_delayed_iput_mutex);
  1466. again = btrfs_clean_one_deleted_snapshot(root);
  1467. mutex_unlock(&fs_info->cleaner_mutex);
  1468. /*
  1469. * The defragger has dealt with the R/O remount and umount,
  1470. * needn't do anything special here.
  1471. */
  1472. btrfs_run_defrag_inodes(fs_info);
  1473. /*
  1474. * Acquires fs_info->delete_unused_bgs_mutex to avoid racing
  1475. * with relocation (btrfs_relocate_chunk) and relocation
  1476. * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
  1477. * after acquiring fs_info->delete_unused_bgs_mutex. So we
  1478. * can't hold, nor need to, fs_info->cleaner_mutex when deleting
  1479. * unused block groups.
  1480. */
  1481. btrfs_delete_unused_bgs(fs_info);
  1482. sleep:
  1483. if (!again) {
  1484. set_current_state(TASK_INTERRUPTIBLE);
  1485. if (!kthread_should_stop())
  1486. schedule();
  1487. __set_current_state(TASK_RUNNING);
  1488. }
  1489. } while (!kthread_should_stop());
  1490. /*
  1491. * Transaction kthread is stopped before us and wakes us up.
  1492. * However we might have started a new transaction and COWed some
  1493. * tree blocks when deleting unused block groups for example. So
  1494. * make sure we commit the transaction we started to have a clean
  1495. * shutdown when evicting the btree inode - if it has dirty pages
  1496. * when we do the final iput() on it, eviction will trigger a
  1497. * writeback for it which will fail with null pointer dereferences
  1498. * since work queues and other resources were already released and
  1499. * destroyed by the time the iput/eviction/writeback is made.
  1500. */
  1501. trans = btrfs_attach_transaction(root);
  1502. if (IS_ERR(trans)) {
  1503. if (PTR_ERR(trans) != -ENOENT)
  1504. btrfs_err(fs_info,
  1505. "cleaner transaction attach returned %ld",
  1506. PTR_ERR(trans));
  1507. } else {
  1508. int ret;
  1509. ret = btrfs_commit_transaction(trans);
  1510. if (ret)
  1511. btrfs_err(fs_info,
  1512. "cleaner open transaction commit returned %d",
  1513. ret);
  1514. }
  1515. return 0;
  1516. }
  1517. static int transaction_kthread(void *arg)
  1518. {
  1519. struct btrfs_root *root = arg;
  1520. struct btrfs_fs_info *fs_info = root->fs_info;
  1521. struct btrfs_trans_handle *trans;
  1522. struct btrfs_transaction *cur;
  1523. u64 transid;
  1524. time64_t now;
  1525. unsigned long delay;
  1526. bool cannot_commit;
  1527. do {
  1528. cannot_commit = false;
  1529. delay = HZ * fs_info->commit_interval;
  1530. mutex_lock(&fs_info->transaction_kthread_mutex);
  1531. spin_lock(&fs_info->trans_lock);
  1532. cur = fs_info->running_transaction;
  1533. if (!cur) {
  1534. spin_unlock(&fs_info->trans_lock);
  1535. goto sleep;
  1536. }
  1537. now = ktime_get_seconds();
  1538. if (cur->state < TRANS_STATE_BLOCKED &&
  1539. !test_bit(BTRFS_FS_NEED_ASYNC_COMMIT, &fs_info->flags) &&
  1540. (now < cur->start_time ||
  1541. now - cur->start_time < fs_info->commit_interval)) {
  1542. spin_unlock(&fs_info->trans_lock);
  1543. delay = HZ * 5;
  1544. goto sleep;
  1545. }
  1546. transid = cur->transid;
  1547. spin_unlock(&fs_info->trans_lock);
  1548. /* If the file system is aborted, this will always fail. */
  1549. trans = btrfs_attach_transaction(root);
  1550. if (IS_ERR(trans)) {
  1551. if (PTR_ERR(trans) != -ENOENT)
  1552. cannot_commit = true;
  1553. goto sleep;
  1554. }
  1555. if (transid == trans->transid) {
  1556. btrfs_commit_transaction(trans);
  1557. } else {
  1558. btrfs_end_transaction(trans);
  1559. }
  1560. sleep:
  1561. wake_up_process(fs_info->cleaner_kthread);
  1562. mutex_unlock(&fs_info->transaction_kthread_mutex);
  1563. if (unlikely(test_bit(BTRFS_FS_STATE_ERROR,
  1564. &fs_info->fs_state)))
  1565. btrfs_cleanup_transaction(fs_info);
  1566. if (!kthread_should_stop() &&
  1567. (!btrfs_transaction_blocked(fs_info) ||
  1568. cannot_commit))
  1569. schedule_timeout_interruptible(delay);
  1570. } while (!kthread_should_stop());
  1571. return 0;
  1572. }
  1573. /*
  1574. * this will find the highest generation in the array of
  1575. * root backups. The index of the highest array is returned,
  1576. * or -1 if we can't find anything.
  1577. *
  1578. * We check to make sure the array is valid by comparing the
  1579. * generation of the latest root in the array with the generation
  1580. * in the super block. If they don't match we pitch it.
  1581. */
  1582. static int find_newest_super_backup(struct btrfs_fs_info *info, u64 newest_gen)
  1583. {
  1584. u64 cur;
  1585. int newest_index = -1;
  1586. struct btrfs_root_backup *root_backup;
  1587. int i;
  1588. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
  1589. root_backup = info->super_copy->super_roots + i;
  1590. cur = btrfs_backup_tree_root_gen(root_backup);
  1591. if (cur == newest_gen)
  1592. newest_index = i;
  1593. }
  1594. /* check to see if we actually wrapped around */
  1595. if (newest_index == BTRFS_NUM_BACKUP_ROOTS - 1) {
  1596. root_backup = info->super_copy->super_roots;
  1597. cur = btrfs_backup_tree_root_gen(root_backup);
  1598. if (cur == newest_gen)
  1599. newest_index = 0;
  1600. }
  1601. return newest_index;
  1602. }
  1603. /*
  1604. * find the oldest backup so we know where to store new entries
  1605. * in the backup array. This will set the backup_root_index
  1606. * field in the fs_info struct
  1607. */
  1608. static void find_oldest_super_backup(struct btrfs_fs_info *info,
  1609. u64 newest_gen)
  1610. {
  1611. int newest_index = -1;
  1612. newest_index = find_newest_super_backup(info, newest_gen);
  1613. /* if there was garbage in there, just move along */
  1614. if (newest_index == -1) {
  1615. info->backup_root_index = 0;
  1616. } else {
  1617. info->backup_root_index = (newest_index + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1618. }
  1619. }
  1620. /*
  1621. * copy all the root pointers into the super backup array.
  1622. * this will bump the backup pointer by one when it is
  1623. * done
  1624. */
  1625. static void backup_super_roots(struct btrfs_fs_info *info)
  1626. {
  1627. int next_backup;
  1628. struct btrfs_root_backup *root_backup;
  1629. int last_backup;
  1630. next_backup = info->backup_root_index;
  1631. last_backup = (next_backup + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1632. BTRFS_NUM_BACKUP_ROOTS;
  1633. /*
  1634. * just overwrite the last backup if we're at the same generation
  1635. * this happens only at umount
  1636. */
  1637. root_backup = info->super_for_commit->super_roots + last_backup;
  1638. if (btrfs_backup_tree_root_gen(root_backup) ==
  1639. btrfs_header_generation(info->tree_root->node))
  1640. next_backup = last_backup;
  1641. root_backup = info->super_for_commit->super_roots + next_backup;
  1642. /*
  1643. * make sure all of our padding and empty slots get zero filled
  1644. * regardless of which ones we use today
  1645. */
  1646. memset(root_backup, 0, sizeof(*root_backup));
  1647. info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1648. btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
  1649. btrfs_set_backup_tree_root_gen(root_backup,
  1650. btrfs_header_generation(info->tree_root->node));
  1651. btrfs_set_backup_tree_root_level(root_backup,
  1652. btrfs_header_level(info->tree_root->node));
  1653. btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
  1654. btrfs_set_backup_chunk_root_gen(root_backup,
  1655. btrfs_header_generation(info->chunk_root->node));
  1656. btrfs_set_backup_chunk_root_level(root_backup,
  1657. btrfs_header_level(info->chunk_root->node));
  1658. btrfs_set_backup_extent_root(root_backup, info->extent_root->node->start);
  1659. btrfs_set_backup_extent_root_gen(root_backup,
  1660. btrfs_header_generation(info->extent_root->node));
  1661. btrfs_set_backup_extent_root_level(root_backup,
  1662. btrfs_header_level(info->extent_root->node));
  1663. /*
  1664. * we might commit during log recovery, which happens before we set
  1665. * the fs_root. Make sure it is valid before we fill it in.
  1666. */
  1667. if (info->fs_root && info->fs_root->node) {
  1668. btrfs_set_backup_fs_root(root_backup,
  1669. info->fs_root->node->start);
  1670. btrfs_set_backup_fs_root_gen(root_backup,
  1671. btrfs_header_generation(info->fs_root->node));
  1672. btrfs_set_backup_fs_root_level(root_backup,
  1673. btrfs_header_level(info->fs_root->node));
  1674. }
  1675. btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
  1676. btrfs_set_backup_dev_root_gen(root_backup,
  1677. btrfs_header_generation(info->dev_root->node));
  1678. btrfs_set_backup_dev_root_level(root_backup,
  1679. btrfs_header_level(info->dev_root->node));
  1680. btrfs_set_backup_csum_root(root_backup, info->csum_root->node->start);
  1681. btrfs_set_backup_csum_root_gen(root_backup,
  1682. btrfs_header_generation(info->csum_root->node));
  1683. btrfs_set_backup_csum_root_level(root_backup,
  1684. btrfs_header_level(info->csum_root->node));
  1685. btrfs_set_backup_total_bytes(root_backup,
  1686. btrfs_super_total_bytes(info->super_copy));
  1687. btrfs_set_backup_bytes_used(root_backup,
  1688. btrfs_super_bytes_used(info->super_copy));
  1689. btrfs_set_backup_num_devices(root_backup,
  1690. btrfs_super_num_devices(info->super_copy));
  1691. /*
  1692. * if we don't copy this out to the super_copy, it won't get remembered
  1693. * for the next commit
  1694. */
  1695. memcpy(&info->super_copy->super_roots,
  1696. &info->super_for_commit->super_roots,
  1697. sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
  1698. }
  1699. /*
  1700. * this copies info out of the root backup array and back into
  1701. * the in-memory super block. It is meant to help iterate through
  1702. * the array, so you send it the number of backups you've already
  1703. * tried and the last backup index you used.
  1704. *
  1705. * this returns -1 when it has tried all the backups
  1706. */
  1707. static noinline int next_root_backup(struct btrfs_fs_info *info,
  1708. struct btrfs_super_block *super,
  1709. int *num_backups_tried, int *backup_index)
  1710. {
  1711. struct btrfs_root_backup *root_backup;
  1712. int newest = *backup_index;
  1713. if (*num_backups_tried == 0) {
  1714. u64 gen = btrfs_super_generation(super);
  1715. newest = find_newest_super_backup(info, gen);
  1716. if (newest == -1)
  1717. return -1;
  1718. *backup_index = newest;
  1719. *num_backups_tried = 1;
  1720. } else if (*num_backups_tried == BTRFS_NUM_BACKUP_ROOTS) {
  1721. /* we've tried all the backups, all done */
  1722. return -1;
  1723. } else {
  1724. /* jump to the next oldest backup */
  1725. newest = (*backup_index + BTRFS_NUM_BACKUP_ROOTS - 1) %
  1726. BTRFS_NUM_BACKUP_ROOTS;
  1727. *backup_index = newest;
  1728. *num_backups_tried += 1;
  1729. }
  1730. root_backup = super->super_roots + newest;
  1731. btrfs_set_super_generation(super,
  1732. btrfs_backup_tree_root_gen(root_backup));
  1733. btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
  1734. btrfs_set_super_root_level(super,
  1735. btrfs_backup_tree_root_level(root_backup));
  1736. btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
  1737. /*
  1738. * fixme: the total bytes and num_devices need to match or we should
  1739. * need a fsck
  1740. */
  1741. btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
  1742. btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
  1743. return 0;
  1744. }
  1745. /* helper to cleanup workers */
  1746. static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
  1747. {
  1748. btrfs_destroy_workqueue(fs_info->fixup_workers);
  1749. btrfs_destroy_workqueue(fs_info->delalloc_workers);
  1750. btrfs_destroy_workqueue(fs_info->workers);
  1751. btrfs_destroy_workqueue(fs_info->endio_workers);
  1752. btrfs_destroy_workqueue(fs_info->endio_raid56_workers);
  1753. btrfs_destroy_workqueue(fs_info->endio_repair_workers);
  1754. btrfs_destroy_workqueue(fs_info->rmw_workers);
  1755. btrfs_destroy_workqueue(fs_info->endio_write_workers);
  1756. btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
  1757. btrfs_destroy_workqueue(fs_info->submit_workers);
  1758. btrfs_destroy_workqueue(fs_info->delayed_workers);
  1759. btrfs_destroy_workqueue(fs_info->caching_workers);
  1760. btrfs_destroy_workqueue(fs_info->readahead_workers);
  1761. btrfs_destroy_workqueue(fs_info->flush_workers);
  1762. btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
  1763. btrfs_destroy_workqueue(fs_info->extent_workers);
  1764. /*
  1765. * Now that all other work queues are destroyed, we can safely destroy
  1766. * the queues used for metadata I/O, since tasks from those other work
  1767. * queues can do metadata I/O operations.
  1768. */
  1769. btrfs_destroy_workqueue(fs_info->endio_meta_workers);
  1770. btrfs_destroy_workqueue(fs_info->endio_meta_write_workers);
  1771. }
  1772. static void free_root_extent_buffers(struct btrfs_root *root)
  1773. {
  1774. if (root) {
  1775. free_extent_buffer(root->node);
  1776. free_extent_buffer(root->commit_root);
  1777. root->node = NULL;
  1778. root->commit_root = NULL;
  1779. }
  1780. }
  1781. /* helper to cleanup tree roots */
  1782. static void free_root_pointers(struct btrfs_fs_info *info, int chunk_root)
  1783. {
  1784. free_root_extent_buffers(info->tree_root);
  1785. free_root_extent_buffers(info->dev_root);
  1786. free_root_extent_buffers(info->extent_root);
  1787. free_root_extent_buffers(info->csum_root);
  1788. free_root_extent_buffers(info->quota_root);
  1789. free_root_extent_buffers(info->uuid_root);
  1790. if (chunk_root)
  1791. free_root_extent_buffers(info->chunk_root);
  1792. free_root_extent_buffers(info->free_space_root);
  1793. }
  1794. void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
  1795. {
  1796. int ret;
  1797. struct btrfs_root *gang[8];
  1798. int i;
  1799. while (!list_empty(&fs_info->dead_roots)) {
  1800. gang[0] = list_entry(fs_info->dead_roots.next,
  1801. struct btrfs_root, root_list);
  1802. list_del(&gang[0]->root_list);
  1803. if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state)) {
  1804. btrfs_drop_and_free_fs_root(fs_info, gang[0]);
  1805. } else {
  1806. free_extent_buffer(gang[0]->node);
  1807. free_extent_buffer(gang[0]->commit_root);
  1808. btrfs_put_fs_root(gang[0]);
  1809. }
  1810. }
  1811. while (1) {
  1812. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1813. (void **)gang, 0,
  1814. ARRAY_SIZE(gang));
  1815. if (!ret)
  1816. break;
  1817. for (i = 0; i < ret; i++)
  1818. btrfs_drop_and_free_fs_root(fs_info, gang[i]);
  1819. }
  1820. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  1821. btrfs_free_log_root_tree(NULL, fs_info);
  1822. btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
  1823. }
  1824. }
  1825. static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
  1826. {
  1827. mutex_init(&fs_info->scrub_lock);
  1828. atomic_set(&fs_info->scrubs_running, 0);
  1829. atomic_set(&fs_info->scrub_pause_req, 0);
  1830. atomic_set(&fs_info->scrubs_paused, 0);
  1831. atomic_set(&fs_info->scrub_cancel_req, 0);
  1832. init_waitqueue_head(&fs_info->scrub_pause_wait);
  1833. fs_info->scrub_workers_refcnt = 0;
  1834. }
  1835. static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
  1836. {
  1837. spin_lock_init(&fs_info->balance_lock);
  1838. mutex_init(&fs_info->balance_mutex);
  1839. atomic_set(&fs_info->balance_pause_req, 0);
  1840. atomic_set(&fs_info->balance_cancel_req, 0);
  1841. fs_info->balance_ctl = NULL;
  1842. init_waitqueue_head(&fs_info->balance_wait_q);
  1843. }
  1844. static void btrfs_init_btree_inode(struct btrfs_fs_info *fs_info)
  1845. {
  1846. struct inode *inode = fs_info->btree_inode;
  1847. inode->i_ino = BTRFS_BTREE_INODE_OBJECTID;
  1848. set_nlink(inode, 1);
  1849. /*
  1850. * we set the i_size on the btree inode to the max possible int.
  1851. * the real end of the address space is determined by all of
  1852. * the devices in the system
  1853. */
  1854. inode->i_size = OFFSET_MAX;
  1855. inode->i_mapping->a_ops = &btree_aops;
  1856. RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
  1857. extent_io_tree_init(&BTRFS_I(inode)->io_tree, inode);
  1858. BTRFS_I(inode)->io_tree.track_uptodate = 0;
  1859. extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
  1860. BTRFS_I(inode)->io_tree.ops = &btree_extent_io_ops;
  1861. BTRFS_I(inode)->root = fs_info->tree_root;
  1862. memset(&BTRFS_I(inode)->location, 0, sizeof(struct btrfs_key));
  1863. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  1864. btrfs_insert_inode_hash(inode);
  1865. }
  1866. static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
  1867. {
  1868. fs_info->dev_replace.lock_owner = 0;
  1869. atomic_set(&fs_info->dev_replace.nesting_level, 0);
  1870. mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
  1871. rwlock_init(&fs_info->dev_replace.lock);
  1872. atomic_set(&fs_info->dev_replace.read_locks, 0);
  1873. atomic_set(&fs_info->dev_replace.blocking_readers, 0);
  1874. init_waitqueue_head(&fs_info->replace_wait);
  1875. init_waitqueue_head(&fs_info->dev_replace.read_lock_wq);
  1876. }
  1877. static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
  1878. {
  1879. spin_lock_init(&fs_info->qgroup_lock);
  1880. mutex_init(&fs_info->qgroup_ioctl_lock);
  1881. fs_info->qgroup_tree = RB_ROOT;
  1882. fs_info->qgroup_op_tree = RB_ROOT;
  1883. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  1884. fs_info->qgroup_seq = 1;
  1885. fs_info->qgroup_ulist = NULL;
  1886. fs_info->qgroup_rescan_running = false;
  1887. mutex_init(&fs_info->qgroup_rescan_lock);
  1888. }
  1889. static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info,
  1890. struct btrfs_fs_devices *fs_devices)
  1891. {
  1892. u32 max_active = fs_info->thread_pool_size;
  1893. unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
  1894. fs_info->workers =
  1895. btrfs_alloc_workqueue(fs_info, "worker",
  1896. flags | WQ_HIGHPRI, max_active, 16);
  1897. fs_info->delalloc_workers =
  1898. btrfs_alloc_workqueue(fs_info, "delalloc",
  1899. flags, max_active, 2);
  1900. fs_info->flush_workers =
  1901. btrfs_alloc_workqueue(fs_info, "flush_delalloc",
  1902. flags, max_active, 0);
  1903. fs_info->caching_workers =
  1904. btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
  1905. /*
  1906. * a higher idle thresh on the submit workers makes it much more
  1907. * likely that bios will be send down in a sane order to the
  1908. * devices
  1909. */
  1910. fs_info->submit_workers =
  1911. btrfs_alloc_workqueue(fs_info, "submit", flags,
  1912. min_t(u64, fs_devices->num_devices,
  1913. max_active), 64);
  1914. fs_info->fixup_workers =
  1915. btrfs_alloc_workqueue(fs_info, "fixup", flags, 1, 0);
  1916. /*
  1917. * endios are largely parallel and should have a very
  1918. * low idle thresh
  1919. */
  1920. fs_info->endio_workers =
  1921. btrfs_alloc_workqueue(fs_info, "endio", flags, max_active, 4);
  1922. fs_info->endio_meta_workers =
  1923. btrfs_alloc_workqueue(fs_info, "endio-meta", flags,
  1924. max_active, 4);
  1925. fs_info->endio_meta_write_workers =
  1926. btrfs_alloc_workqueue(fs_info, "endio-meta-write", flags,
  1927. max_active, 2);
  1928. fs_info->endio_raid56_workers =
  1929. btrfs_alloc_workqueue(fs_info, "endio-raid56", flags,
  1930. max_active, 4);
  1931. fs_info->endio_repair_workers =
  1932. btrfs_alloc_workqueue(fs_info, "endio-repair", flags, 1, 0);
  1933. fs_info->rmw_workers =
  1934. btrfs_alloc_workqueue(fs_info, "rmw", flags, max_active, 2);
  1935. fs_info->endio_write_workers =
  1936. btrfs_alloc_workqueue(fs_info, "endio-write", flags,
  1937. max_active, 2);
  1938. fs_info->endio_freespace_worker =
  1939. btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
  1940. max_active, 0);
  1941. fs_info->delayed_workers =
  1942. btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
  1943. max_active, 0);
  1944. fs_info->readahead_workers =
  1945. btrfs_alloc_workqueue(fs_info, "readahead", flags,
  1946. max_active, 2);
  1947. fs_info->qgroup_rescan_workers =
  1948. btrfs_alloc_workqueue(fs_info, "qgroup-rescan", flags, 1, 0);
  1949. fs_info->extent_workers =
  1950. btrfs_alloc_workqueue(fs_info, "extent-refs", flags,
  1951. min_t(u64, fs_devices->num_devices,
  1952. max_active), 8);
  1953. if (!(fs_info->workers && fs_info->delalloc_workers &&
  1954. fs_info->submit_workers && fs_info->flush_workers &&
  1955. fs_info->endio_workers && fs_info->endio_meta_workers &&
  1956. fs_info->endio_meta_write_workers &&
  1957. fs_info->endio_repair_workers &&
  1958. fs_info->endio_write_workers && fs_info->endio_raid56_workers &&
  1959. fs_info->endio_freespace_worker && fs_info->rmw_workers &&
  1960. fs_info->caching_workers && fs_info->readahead_workers &&
  1961. fs_info->fixup_workers && fs_info->delayed_workers &&
  1962. fs_info->extent_workers &&
  1963. fs_info->qgroup_rescan_workers)) {
  1964. return -ENOMEM;
  1965. }
  1966. return 0;
  1967. }
  1968. static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
  1969. struct btrfs_fs_devices *fs_devices)
  1970. {
  1971. int ret;
  1972. struct btrfs_root *log_tree_root;
  1973. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1974. u64 bytenr = btrfs_super_log_root(disk_super);
  1975. int level = btrfs_super_log_root_level(disk_super);
  1976. if (fs_devices->rw_devices == 0) {
  1977. btrfs_warn(fs_info, "log replay required on RO media");
  1978. return -EIO;
  1979. }
  1980. log_tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  1981. if (!log_tree_root)
  1982. return -ENOMEM;
  1983. __setup_root(log_tree_root, fs_info, BTRFS_TREE_LOG_OBJECTID);
  1984. log_tree_root->node = read_tree_block(fs_info, bytenr,
  1985. fs_info->generation + 1,
  1986. level, NULL);
  1987. if (IS_ERR(log_tree_root->node)) {
  1988. btrfs_warn(fs_info, "failed to read log tree");
  1989. ret = PTR_ERR(log_tree_root->node);
  1990. kfree(log_tree_root);
  1991. return ret;
  1992. } else if (!extent_buffer_uptodate(log_tree_root->node)) {
  1993. btrfs_err(fs_info, "failed to read log tree");
  1994. free_extent_buffer(log_tree_root->node);
  1995. kfree(log_tree_root);
  1996. return -EIO;
  1997. }
  1998. /* returns with log_tree_root freed on success */
  1999. ret = btrfs_recover_log_trees(log_tree_root);
  2000. if (ret) {
  2001. btrfs_handle_fs_error(fs_info, ret,
  2002. "Failed to recover log tree");
  2003. free_extent_buffer(log_tree_root->node);
  2004. kfree(log_tree_root);
  2005. return ret;
  2006. }
  2007. if (sb_rdonly(fs_info->sb)) {
  2008. ret = btrfs_commit_super(fs_info);
  2009. if (ret)
  2010. return ret;
  2011. }
  2012. return 0;
  2013. }
  2014. static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
  2015. {
  2016. struct btrfs_root *tree_root = fs_info->tree_root;
  2017. struct btrfs_root *root;
  2018. struct btrfs_key location;
  2019. int ret;
  2020. BUG_ON(!fs_info->tree_root);
  2021. location.objectid = BTRFS_EXTENT_TREE_OBJECTID;
  2022. location.type = BTRFS_ROOT_ITEM_KEY;
  2023. location.offset = 0;
  2024. root = btrfs_read_tree_root(tree_root, &location);
  2025. if (IS_ERR(root)) {
  2026. ret = PTR_ERR(root);
  2027. goto out;
  2028. }
  2029. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2030. fs_info->extent_root = root;
  2031. location.objectid = BTRFS_DEV_TREE_OBJECTID;
  2032. root = btrfs_read_tree_root(tree_root, &location);
  2033. if (IS_ERR(root)) {
  2034. ret = PTR_ERR(root);
  2035. goto out;
  2036. }
  2037. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2038. fs_info->dev_root = root;
  2039. btrfs_init_devices_late(fs_info);
  2040. location.objectid = BTRFS_CSUM_TREE_OBJECTID;
  2041. root = btrfs_read_tree_root(tree_root, &location);
  2042. if (IS_ERR(root)) {
  2043. ret = PTR_ERR(root);
  2044. goto out;
  2045. }
  2046. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2047. fs_info->csum_root = root;
  2048. location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
  2049. root = btrfs_read_tree_root(tree_root, &location);
  2050. if (!IS_ERR(root)) {
  2051. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2052. set_bit(BTRFS_FS_QUOTA_ENABLED, &fs_info->flags);
  2053. fs_info->quota_root = root;
  2054. }
  2055. location.objectid = BTRFS_UUID_TREE_OBJECTID;
  2056. root = btrfs_read_tree_root(tree_root, &location);
  2057. if (IS_ERR(root)) {
  2058. ret = PTR_ERR(root);
  2059. if (ret != -ENOENT)
  2060. goto out;
  2061. } else {
  2062. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2063. fs_info->uuid_root = root;
  2064. }
  2065. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2066. location.objectid = BTRFS_FREE_SPACE_TREE_OBJECTID;
  2067. root = btrfs_read_tree_root(tree_root, &location);
  2068. if (IS_ERR(root)) {
  2069. ret = PTR_ERR(root);
  2070. goto out;
  2071. }
  2072. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2073. fs_info->free_space_root = root;
  2074. }
  2075. return 0;
  2076. out:
  2077. btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
  2078. location.objectid, ret);
  2079. return ret;
  2080. }
  2081. /*
  2082. * Real super block validation
  2083. * NOTE: super csum type and incompat features will not be checked here.
  2084. *
  2085. * @sb: super block to check
  2086. * @mirror_num: the super block number to check its bytenr:
  2087. * 0 the primary (1st) sb
  2088. * 1, 2 2nd and 3rd backup copy
  2089. * -1 skip bytenr check
  2090. */
  2091. static int validate_super(struct btrfs_fs_info *fs_info,
  2092. struct btrfs_super_block *sb, int mirror_num)
  2093. {
  2094. u64 nodesize = btrfs_super_nodesize(sb);
  2095. u64 sectorsize = btrfs_super_sectorsize(sb);
  2096. int ret = 0;
  2097. if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
  2098. btrfs_err(fs_info, "no valid FS found");
  2099. ret = -EINVAL;
  2100. }
  2101. if (btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP) {
  2102. btrfs_err(fs_info, "unrecognized or unsupported super flag: %llu",
  2103. btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
  2104. ret = -EINVAL;
  2105. }
  2106. if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2107. btrfs_err(fs_info, "tree_root level too big: %d >= %d",
  2108. btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
  2109. ret = -EINVAL;
  2110. }
  2111. if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2112. btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
  2113. btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
  2114. ret = -EINVAL;
  2115. }
  2116. if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2117. btrfs_err(fs_info, "log_root level too big: %d >= %d",
  2118. btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
  2119. ret = -EINVAL;
  2120. }
  2121. /*
  2122. * Check sectorsize and nodesize first, other check will need it.
  2123. * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
  2124. */
  2125. if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
  2126. sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2127. btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
  2128. ret = -EINVAL;
  2129. }
  2130. /* Only PAGE SIZE is supported yet */
  2131. if (sectorsize != PAGE_SIZE) {
  2132. btrfs_err(fs_info,
  2133. "sectorsize %llu not supported yet, only support %lu",
  2134. sectorsize, PAGE_SIZE);
  2135. ret = -EINVAL;
  2136. }
  2137. if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
  2138. nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2139. btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
  2140. ret = -EINVAL;
  2141. }
  2142. if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
  2143. btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
  2144. le32_to_cpu(sb->__unused_leafsize), nodesize);
  2145. ret = -EINVAL;
  2146. }
  2147. /* Root alignment check */
  2148. if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
  2149. btrfs_warn(fs_info, "tree_root block unaligned: %llu",
  2150. btrfs_super_root(sb));
  2151. ret = -EINVAL;
  2152. }
  2153. if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
  2154. btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
  2155. btrfs_super_chunk_root(sb));
  2156. ret = -EINVAL;
  2157. }
  2158. if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
  2159. btrfs_warn(fs_info, "log_root block unaligned: %llu",
  2160. btrfs_super_log_root(sb));
  2161. ret = -EINVAL;
  2162. }
  2163. if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
  2164. btrfs_err(fs_info,
  2165. "dev_item UUID does not match fsid: %pU != %pU",
  2166. fs_info->fsid, sb->dev_item.fsid);
  2167. ret = -EINVAL;
  2168. }
  2169. /*
  2170. * Hint to catch really bogus numbers, bitflips or so, more exact checks are
  2171. * done later
  2172. */
  2173. if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
  2174. btrfs_err(fs_info, "bytes_used is too small %llu",
  2175. btrfs_super_bytes_used(sb));
  2176. ret = -EINVAL;
  2177. }
  2178. if (!is_power_of_2(btrfs_super_stripesize(sb))) {
  2179. btrfs_err(fs_info, "invalid stripesize %u",
  2180. btrfs_super_stripesize(sb));
  2181. ret = -EINVAL;
  2182. }
  2183. if (btrfs_super_num_devices(sb) > (1UL << 31))
  2184. btrfs_warn(fs_info, "suspicious number of devices: %llu",
  2185. btrfs_super_num_devices(sb));
  2186. if (btrfs_super_num_devices(sb) == 0) {
  2187. btrfs_err(fs_info, "number of devices is 0");
  2188. ret = -EINVAL;
  2189. }
  2190. if (mirror_num >= 0 &&
  2191. btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
  2192. btrfs_err(fs_info, "super offset mismatch %llu != %u",
  2193. btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
  2194. ret = -EINVAL;
  2195. }
  2196. /*
  2197. * Obvious sys_chunk_array corruptions, it must hold at least one key
  2198. * and one chunk
  2199. */
  2200. if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  2201. btrfs_err(fs_info, "system chunk array too big %u > %u",
  2202. btrfs_super_sys_array_size(sb),
  2203. BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
  2204. ret = -EINVAL;
  2205. }
  2206. if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
  2207. + sizeof(struct btrfs_chunk)) {
  2208. btrfs_err(fs_info, "system chunk array too small %u < %zu",
  2209. btrfs_super_sys_array_size(sb),
  2210. sizeof(struct btrfs_disk_key)
  2211. + sizeof(struct btrfs_chunk));
  2212. ret = -EINVAL;
  2213. }
  2214. /*
  2215. * The generation is a global counter, we'll trust it more than the others
  2216. * but it's still possible that it's the one that's wrong.
  2217. */
  2218. if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
  2219. btrfs_warn(fs_info,
  2220. "suspicious: generation < chunk_root_generation: %llu < %llu",
  2221. btrfs_super_generation(sb),
  2222. btrfs_super_chunk_root_generation(sb));
  2223. if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
  2224. && btrfs_super_cache_generation(sb) != (u64)-1)
  2225. btrfs_warn(fs_info,
  2226. "suspicious: generation < cache_generation: %llu < %llu",
  2227. btrfs_super_generation(sb),
  2228. btrfs_super_cache_generation(sb));
  2229. return ret;
  2230. }
  2231. /*
  2232. * Validation of super block at mount time.
  2233. * Some checks already done early at mount time, like csum type and incompat
  2234. * flags will be skipped.
  2235. */
  2236. static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
  2237. {
  2238. return validate_super(fs_info, fs_info->super_copy, 0);
  2239. }
  2240. /*
  2241. * Validation of super block at write time.
  2242. * Some checks like bytenr check will be skipped as their values will be
  2243. * overwritten soon.
  2244. * Extra checks like csum type and incompat flags will be done here.
  2245. */
  2246. static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
  2247. struct btrfs_super_block *sb)
  2248. {
  2249. int ret;
  2250. ret = validate_super(fs_info, sb, -1);
  2251. if (ret < 0)
  2252. goto out;
  2253. if (btrfs_super_csum_type(sb) != BTRFS_CSUM_TYPE_CRC32) {
  2254. ret = -EUCLEAN;
  2255. btrfs_err(fs_info, "invalid csum type, has %u want %u",
  2256. btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
  2257. goto out;
  2258. }
  2259. if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
  2260. ret = -EUCLEAN;
  2261. btrfs_err(fs_info,
  2262. "invalid incompat flags, has 0x%llx valid mask 0x%llx",
  2263. btrfs_super_incompat_flags(sb),
  2264. (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
  2265. goto out;
  2266. }
  2267. out:
  2268. if (ret < 0)
  2269. btrfs_err(fs_info,
  2270. "super block corruption detected before writing it to disk");
  2271. return ret;
  2272. }
  2273. int open_ctree(struct super_block *sb,
  2274. struct btrfs_fs_devices *fs_devices,
  2275. char *options)
  2276. {
  2277. u32 sectorsize;
  2278. u32 nodesize;
  2279. u32 stripesize;
  2280. u64 generation;
  2281. u64 features;
  2282. struct btrfs_key location;
  2283. struct buffer_head *bh;
  2284. struct btrfs_super_block *disk_super;
  2285. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2286. struct btrfs_root *tree_root;
  2287. struct btrfs_root *chunk_root;
  2288. int ret;
  2289. int err = -EINVAL;
  2290. int num_backups_tried = 0;
  2291. int backup_index = 0;
  2292. int clear_free_space_tree = 0;
  2293. int level;
  2294. tree_root = fs_info->tree_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  2295. chunk_root = fs_info->chunk_root = btrfs_alloc_root(fs_info, GFP_KERNEL);
  2296. if (!tree_root || !chunk_root) {
  2297. err = -ENOMEM;
  2298. goto fail;
  2299. }
  2300. ret = init_srcu_struct(&fs_info->subvol_srcu);
  2301. if (ret) {
  2302. err = ret;
  2303. goto fail;
  2304. }
  2305. ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
  2306. if (ret) {
  2307. err = ret;
  2308. goto fail_srcu;
  2309. }
  2310. fs_info->dirty_metadata_batch = PAGE_SIZE *
  2311. (1 + ilog2(nr_cpu_ids));
  2312. ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
  2313. if (ret) {
  2314. err = ret;
  2315. goto fail_dirty_metadata_bytes;
  2316. }
  2317. ret = percpu_counter_init(&fs_info->bio_counter, 0, GFP_KERNEL);
  2318. if (ret) {
  2319. err = ret;
  2320. goto fail_delalloc_bytes;
  2321. }
  2322. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  2323. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  2324. INIT_LIST_HEAD(&fs_info->trans_list);
  2325. INIT_LIST_HEAD(&fs_info->dead_roots);
  2326. INIT_LIST_HEAD(&fs_info->delayed_iputs);
  2327. INIT_LIST_HEAD(&fs_info->delalloc_roots);
  2328. INIT_LIST_HEAD(&fs_info->caching_block_groups);
  2329. INIT_LIST_HEAD(&fs_info->pending_raid_kobjs);
  2330. spin_lock_init(&fs_info->pending_raid_kobjs_lock);
  2331. spin_lock_init(&fs_info->delalloc_root_lock);
  2332. spin_lock_init(&fs_info->trans_lock);
  2333. spin_lock_init(&fs_info->fs_roots_radix_lock);
  2334. spin_lock_init(&fs_info->delayed_iput_lock);
  2335. spin_lock_init(&fs_info->defrag_inodes_lock);
  2336. spin_lock_init(&fs_info->tree_mod_seq_lock);
  2337. spin_lock_init(&fs_info->super_lock);
  2338. spin_lock_init(&fs_info->qgroup_op_lock);
  2339. spin_lock_init(&fs_info->buffer_lock);
  2340. spin_lock_init(&fs_info->unused_bgs_lock);
  2341. rwlock_init(&fs_info->tree_mod_log_lock);
  2342. mutex_init(&fs_info->unused_bg_unpin_mutex);
  2343. mutex_init(&fs_info->delete_unused_bgs_mutex);
  2344. mutex_init(&fs_info->reloc_mutex);
  2345. mutex_init(&fs_info->delalloc_root_mutex);
  2346. mutex_init(&fs_info->cleaner_delayed_iput_mutex);
  2347. seqlock_init(&fs_info->profiles_lock);
  2348. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  2349. INIT_LIST_HEAD(&fs_info->space_info);
  2350. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  2351. INIT_LIST_HEAD(&fs_info->unused_bgs);
  2352. btrfs_mapping_init(&fs_info->mapping_tree);
  2353. btrfs_init_block_rsv(&fs_info->global_block_rsv,
  2354. BTRFS_BLOCK_RSV_GLOBAL);
  2355. btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
  2356. btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
  2357. btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
  2358. btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
  2359. BTRFS_BLOCK_RSV_DELOPS);
  2360. atomic_set(&fs_info->async_delalloc_pages, 0);
  2361. atomic_set(&fs_info->defrag_running, 0);
  2362. atomic_set(&fs_info->qgroup_op_seq, 0);
  2363. atomic_set(&fs_info->reada_works_cnt, 0);
  2364. atomic64_set(&fs_info->tree_mod_seq, 0);
  2365. fs_info->sb = sb;
  2366. fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
  2367. fs_info->metadata_ratio = 0;
  2368. fs_info->defrag_inodes = RB_ROOT;
  2369. atomic64_set(&fs_info->free_chunk_space, 0);
  2370. fs_info->tree_mod_log = RB_ROOT;
  2371. fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  2372. fs_info->avg_delayed_ref_runtime = NSEC_PER_SEC >> 6; /* div by 64 */
  2373. /* readahead state */
  2374. INIT_RADIX_TREE(&fs_info->reada_tree, GFP_NOFS & ~__GFP_DIRECT_RECLAIM);
  2375. spin_lock_init(&fs_info->reada_lock);
  2376. btrfs_init_ref_verify(fs_info);
  2377. fs_info->thread_pool_size = min_t(unsigned long,
  2378. num_online_cpus() + 2, 8);
  2379. INIT_LIST_HEAD(&fs_info->ordered_roots);
  2380. spin_lock_init(&fs_info->ordered_root_lock);
  2381. fs_info->btree_inode = new_inode(sb);
  2382. if (!fs_info->btree_inode) {
  2383. err = -ENOMEM;
  2384. goto fail_bio_counter;
  2385. }
  2386. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  2387. fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
  2388. GFP_KERNEL);
  2389. if (!fs_info->delayed_root) {
  2390. err = -ENOMEM;
  2391. goto fail_iput;
  2392. }
  2393. btrfs_init_delayed_root(fs_info->delayed_root);
  2394. btrfs_init_scrub(fs_info);
  2395. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2396. fs_info->check_integrity_print_mask = 0;
  2397. #endif
  2398. btrfs_init_balance(fs_info);
  2399. btrfs_init_async_reclaim_work(&fs_info->async_reclaim_work);
  2400. sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
  2401. sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
  2402. btrfs_init_btree_inode(fs_info);
  2403. spin_lock_init(&fs_info->block_group_cache_lock);
  2404. fs_info->block_group_cache_tree = RB_ROOT;
  2405. fs_info->first_logical_byte = (u64)-1;
  2406. extent_io_tree_init(&fs_info->freed_extents[0], NULL);
  2407. extent_io_tree_init(&fs_info->freed_extents[1], NULL);
  2408. fs_info->pinned_extents = &fs_info->freed_extents[0];
  2409. set_bit(BTRFS_FS_BARRIER, &fs_info->flags);
  2410. mutex_init(&fs_info->ordered_operations_mutex);
  2411. mutex_init(&fs_info->tree_log_mutex);
  2412. mutex_init(&fs_info->chunk_mutex);
  2413. mutex_init(&fs_info->transaction_kthread_mutex);
  2414. mutex_init(&fs_info->cleaner_mutex);
  2415. mutex_init(&fs_info->ro_block_group_mutex);
  2416. init_rwsem(&fs_info->commit_root_sem);
  2417. init_rwsem(&fs_info->cleanup_work_sem);
  2418. init_rwsem(&fs_info->subvol_sem);
  2419. sema_init(&fs_info->uuid_tree_rescan_sem, 1);
  2420. btrfs_init_dev_replace_locks(fs_info);
  2421. btrfs_init_qgroup(fs_info);
  2422. btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
  2423. btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
  2424. init_waitqueue_head(&fs_info->transaction_throttle);
  2425. init_waitqueue_head(&fs_info->transaction_wait);
  2426. init_waitqueue_head(&fs_info->transaction_blocked_wait);
  2427. init_waitqueue_head(&fs_info->async_submit_wait);
  2428. INIT_LIST_HEAD(&fs_info->pinned_chunks);
  2429. /* Usable values until the real ones are cached from the superblock */
  2430. fs_info->nodesize = 4096;
  2431. fs_info->sectorsize = 4096;
  2432. fs_info->stripesize = 4096;
  2433. ret = btrfs_alloc_stripe_hash_table(fs_info);
  2434. if (ret) {
  2435. err = ret;
  2436. goto fail_alloc;
  2437. }
  2438. __setup_root(tree_root, fs_info, BTRFS_ROOT_TREE_OBJECTID);
  2439. invalidate_bdev(fs_devices->latest_bdev);
  2440. /*
  2441. * Read super block and check the signature bytes only
  2442. */
  2443. bh = btrfs_read_dev_super(fs_devices->latest_bdev);
  2444. if (IS_ERR(bh)) {
  2445. err = PTR_ERR(bh);
  2446. goto fail_alloc;
  2447. }
  2448. /*
  2449. * We want to check superblock checksum, the type is stored inside.
  2450. * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
  2451. */
  2452. if (btrfs_check_super_csum(fs_info, bh->b_data)) {
  2453. btrfs_err(fs_info, "superblock checksum mismatch");
  2454. err = -EINVAL;
  2455. brelse(bh);
  2456. goto fail_alloc;
  2457. }
  2458. /*
  2459. * super_copy is zeroed at allocation time and we never touch the
  2460. * following bytes up to INFO_SIZE, the checksum is calculated from
  2461. * the whole block of INFO_SIZE
  2462. */
  2463. memcpy(fs_info->super_copy, bh->b_data, sizeof(*fs_info->super_copy));
  2464. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2465. sizeof(*fs_info->super_for_commit));
  2466. brelse(bh);
  2467. memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
  2468. ret = btrfs_validate_mount_super(fs_info);
  2469. if (ret) {
  2470. btrfs_err(fs_info, "superblock contains fatal errors");
  2471. err = -EINVAL;
  2472. goto fail_alloc;
  2473. }
  2474. disk_super = fs_info->super_copy;
  2475. if (!btrfs_super_root(disk_super))
  2476. goto fail_alloc;
  2477. /* check FS state, whether FS is broken. */
  2478. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
  2479. set_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state);
  2480. /*
  2481. * run through our array of backup supers and setup
  2482. * our ring pointer to the oldest one
  2483. */
  2484. generation = btrfs_super_generation(disk_super);
  2485. find_oldest_super_backup(fs_info, generation);
  2486. /*
  2487. * In the long term, we'll store the compression type in the super
  2488. * block, and it'll be used for per file compression control.
  2489. */
  2490. fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
  2491. ret = btrfs_parse_options(fs_info, options, sb->s_flags);
  2492. if (ret) {
  2493. err = ret;
  2494. goto fail_alloc;
  2495. }
  2496. features = btrfs_super_incompat_flags(disk_super) &
  2497. ~BTRFS_FEATURE_INCOMPAT_SUPP;
  2498. if (features) {
  2499. btrfs_err(fs_info,
  2500. "cannot mount because of unsupported optional features (%llx)",
  2501. features);
  2502. err = -EINVAL;
  2503. goto fail_alloc;
  2504. }
  2505. features = btrfs_super_incompat_flags(disk_super);
  2506. features |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
  2507. if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
  2508. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  2509. else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
  2510. features |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
  2511. if (features & BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA)
  2512. btrfs_info(fs_info, "has skinny extents");
  2513. /*
  2514. * flag our filesystem as having big metadata blocks if
  2515. * they are bigger than the page size
  2516. */
  2517. if (btrfs_super_nodesize(disk_super) > PAGE_SIZE) {
  2518. if (!(features & BTRFS_FEATURE_INCOMPAT_BIG_METADATA))
  2519. btrfs_info(fs_info,
  2520. "flagging fs with big metadata feature");
  2521. features |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
  2522. }
  2523. nodesize = btrfs_super_nodesize(disk_super);
  2524. sectorsize = btrfs_super_sectorsize(disk_super);
  2525. stripesize = sectorsize;
  2526. fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
  2527. fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
  2528. /* Cache block sizes */
  2529. fs_info->nodesize = nodesize;
  2530. fs_info->sectorsize = sectorsize;
  2531. fs_info->stripesize = stripesize;
  2532. /*
  2533. * mixed block groups end up with duplicate but slightly offset
  2534. * extent buffers for the same range. It leads to corruptions
  2535. */
  2536. if ((features & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2537. (sectorsize != nodesize)) {
  2538. btrfs_err(fs_info,
  2539. "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
  2540. nodesize, sectorsize);
  2541. goto fail_alloc;
  2542. }
  2543. /*
  2544. * Needn't use the lock because there is no other task which will
  2545. * update the flag.
  2546. */
  2547. btrfs_set_super_incompat_flags(disk_super, features);
  2548. features = btrfs_super_compat_ro_flags(disk_super) &
  2549. ~BTRFS_FEATURE_COMPAT_RO_SUPP;
  2550. if (!sb_rdonly(sb) && features) {
  2551. btrfs_err(fs_info,
  2552. "cannot mount read-write because of unsupported optional features (%llx)",
  2553. features);
  2554. err = -EINVAL;
  2555. goto fail_alloc;
  2556. }
  2557. ret = btrfs_init_workqueues(fs_info, fs_devices);
  2558. if (ret) {
  2559. err = ret;
  2560. goto fail_sb_buffer;
  2561. }
  2562. sb->s_bdi->congested_fn = btrfs_congested_fn;
  2563. sb->s_bdi->congested_data = fs_info;
  2564. sb->s_bdi->capabilities |= BDI_CAP_CGROUP_WRITEBACK;
  2565. sb->s_bdi->ra_pages = VM_MAX_READAHEAD * SZ_1K / PAGE_SIZE;
  2566. sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
  2567. sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
  2568. sb->s_blocksize = sectorsize;
  2569. sb->s_blocksize_bits = blksize_bits(sectorsize);
  2570. memcpy(&sb->s_uuid, fs_info->fsid, BTRFS_FSID_SIZE);
  2571. mutex_lock(&fs_info->chunk_mutex);
  2572. ret = btrfs_read_sys_array(fs_info);
  2573. mutex_unlock(&fs_info->chunk_mutex);
  2574. if (ret) {
  2575. btrfs_err(fs_info, "failed to read the system array: %d", ret);
  2576. goto fail_sb_buffer;
  2577. }
  2578. generation = btrfs_super_chunk_root_generation(disk_super);
  2579. level = btrfs_super_chunk_root_level(disk_super);
  2580. __setup_root(chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  2581. chunk_root->node = read_tree_block(fs_info,
  2582. btrfs_super_chunk_root(disk_super),
  2583. generation, level, NULL);
  2584. if (IS_ERR(chunk_root->node) ||
  2585. !extent_buffer_uptodate(chunk_root->node)) {
  2586. btrfs_err(fs_info, "failed to read chunk root");
  2587. if (!IS_ERR(chunk_root->node))
  2588. free_extent_buffer(chunk_root->node);
  2589. chunk_root->node = NULL;
  2590. goto fail_tree_roots;
  2591. }
  2592. btrfs_set_root_node(&chunk_root->root_item, chunk_root->node);
  2593. chunk_root->commit_root = btrfs_root_node(chunk_root);
  2594. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  2595. btrfs_header_chunk_tree_uuid(chunk_root->node), BTRFS_UUID_SIZE);
  2596. ret = btrfs_read_chunk_tree(fs_info);
  2597. if (ret) {
  2598. btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
  2599. goto fail_tree_roots;
  2600. }
  2601. /*
  2602. * Keep the devid that is marked to be the target device for the
  2603. * device replace procedure
  2604. */
  2605. btrfs_free_extra_devids(fs_devices, 0);
  2606. if (!fs_devices->latest_bdev) {
  2607. btrfs_err(fs_info, "failed to read devices");
  2608. goto fail_tree_roots;
  2609. }
  2610. retry_root_backup:
  2611. generation = btrfs_super_generation(disk_super);
  2612. level = btrfs_super_root_level(disk_super);
  2613. tree_root->node = read_tree_block(fs_info,
  2614. btrfs_super_root(disk_super),
  2615. generation, level, NULL);
  2616. if (IS_ERR(tree_root->node) ||
  2617. !extent_buffer_uptodate(tree_root->node)) {
  2618. btrfs_warn(fs_info, "failed to read tree root");
  2619. if (!IS_ERR(tree_root->node))
  2620. free_extent_buffer(tree_root->node);
  2621. tree_root->node = NULL;
  2622. goto recovery_tree_root;
  2623. }
  2624. btrfs_set_root_node(&tree_root->root_item, tree_root->node);
  2625. tree_root->commit_root = btrfs_root_node(tree_root);
  2626. btrfs_set_root_refs(&tree_root->root_item, 1);
  2627. mutex_lock(&tree_root->objectid_mutex);
  2628. ret = btrfs_find_highest_objectid(tree_root,
  2629. &tree_root->highest_objectid);
  2630. if (ret) {
  2631. mutex_unlock(&tree_root->objectid_mutex);
  2632. goto recovery_tree_root;
  2633. }
  2634. ASSERT(tree_root->highest_objectid <= BTRFS_LAST_FREE_OBJECTID);
  2635. mutex_unlock(&tree_root->objectid_mutex);
  2636. ret = btrfs_read_roots(fs_info);
  2637. if (ret)
  2638. goto recovery_tree_root;
  2639. fs_info->generation = generation;
  2640. fs_info->last_trans_committed = generation;
  2641. ret = btrfs_recover_balance(fs_info);
  2642. if (ret) {
  2643. btrfs_err(fs_info, "failed to recover balance: %d", ret);
  2644. goto fail_block_groups;
  2645. }
  2646. ret = btrfs_init_dev_stats(fs_info);
  2647. if (ret) {
  2648. btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
  2649. goto fail_block_groups;
  2650. }
  2651. ret = btrfs_init_dev_replace(fs_info);
  2652. if (ret) {
  2653. btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
  2654. goto fail_block_groups;
  2655. }
  2656. btrfs_free_extra_devids(fs_devices, 1);
  2657. ret = btrfs_sysfs_add_fsid(fs_devices, NULL);
  2658. if (ret) {
  2659. btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
  2660. ret);
  2661. goto fail_block_groups;
  2662. }
  2663. ret = btrfs_sysfs_add_device(fs_devices);
  2664. if (ret) {
  2665. btrfs_err(fs_info, "failed to init sysfs device interface: %d",
  2666. ret);
  2667. goto fail_fsdev_sysfs;
  2668. }
  2669. ret = btrfs_sysfs_add_mounted(fs_info);
  2670. if (ret) {
  2671. btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
  2672. goto fail_fsdev_sysfs;
  2673. }
  2674. ret = btrfs_init_space_info(fs_info);
  2675. if (ret) {
  2676. btrfs_err(fs_info, "failed to initialize space info: %d", ret);
  2677. goto fail_sysfs;
  2678. }
  2679. ret = btrfs_read_block_groups(fs_info);
  2680. if (ret) {
  2681. btrfs_err(fs_info, "failed to read block groups: %d", ret);
  2682. goto fail_sysfs;
  2683. }
  2684. if (!sb_rdonly(sb) && !btrfs_check_rw_degradable(fs_info, NULL)) {
  2685. btrfs_warn(fs_info,
  2686. "writeable mount is not allowed due to too many missing devices");
  2687. goto fail_sysfs;
  2688. }
  2689. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, tree_root,
  2690. "btrfs-cleaner");
  2691. if (IS_ERR(fs_info->cleaner_kthread))
  2692. goto fail_sysfs;
  2693. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  2694. tree_root,
  2695. "btrfs-transaction");
  2696. if (IS_ERR(fs_info->transaction_kthread))
  2697. goto fail_cleaner;
  2698. if (!btrfs_test_opt(fs_info, NOSSD) &&
  2699. !fs_info->fs_devices->rotating) {
  2700. btrfs_set_and_info(fs_info, SSD, "enabling ssd optimizations");
  2701. }
  2702. /*
  2703. * Mount does not set all options immediately, we can do it now and do
  2704. * not have to wait for transaction commit
  2705. */
  2706. btrfs_apply_pending_changes(fs_info);
  2707. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  2708. if (btrfs_test_opt(fs_info, CHECK_INTEGRITY)) {
  2709. ret = btrfsic_mount(fs_info, fs_devices,
  2710. btrfs_test_opt(fs_info,
  2711. CHECK_INTEGRITY_INCLUDING_EXTENT_DATA) ?
  2712. 1 : 0,
  2713. fs_info->check_integrity_print_mask);
  2714. if (ret)
  2715. btrfs_warn(fs_info,
  2716. "failed to initialize integrity check module: %d",
  2717. ret);
  2718. }
  2719. #endif
  2720. ret = btrfs_read_qgroup_config(fs_info);
  2721. if (ret)
  2722. goto fail_trans_kthread;
  2723. if (btrfs_build_ref_tree(fs_info))
  2724. btrfs_err(fs_info, "couldn't build ref tree");
  2725. /* do not make disk changes in broken FS or nologreplay is given */
  2726. if (btrfs_super_log_root(disk_super) != 0 &&
  2727. !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
  2728. ret = btrfs_replay_log(fs_info, fs_devices);
  2729. if (ret) {
  2730. err = ret;
  2731. goto fail_qgroup;
  2732. }
  2733. }
  2734. ret = btrfs_find_orphan_roots(fs_info);
  2735. if (ret)
  2736. goto fail_qgroup;
  2737. if (!sb_rdonly(sb)) {
  2738. ret = btrfs_cleanup_fs_roots(fs_info);
  2739. if (ret)
  2740. goto fail_qgroup;
  2741. mutex_lock(&fs_info->cleaner_mutex);
  2742. ret = btrfs_recover_relocation(tree_root);
  2743. mutex_unlock(&fs_info->cleaner_mutex);
  2744. if (ret < 0) {
  2745. btrfs_warn(fs_info, "failed to recover relocation: %d",
  2746. ret);
  2747. err = -EINVAL;
  2748. goto fail_qgroup;
  2749. }
  2750. }
  2751. location.objectid = BTRFS_FS_TREE_OBJECTID;
  2752. location.type = BTRFS_ROOT_ITEM_KEY;
  2753. location.offset = 0;
  2754. fs_info->fs_root = btrfs_read_fs_root_no_name(fs_info, &location);
  2755. if (IS_ERR(fs_info->fs_root)) {
  2756. err = PTR_ERR(fs_info->fs_root);
  2757. btrfs_warn(fs_info, "failed to read fs tree: %d", err);
  2758. goto fail_qgroup;
  2759. }
  2760. if (sb_rdonly(sb))
  2761. return 0;
  2762. if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
  2763. btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2764. clear_free_space_tree = 1;
  2765. } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
  2766. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
  2767. btrfs_warn(fs_info, "free space tree is invalid");
  2768. clear_free_space_tree = 1;
  2769. }
  2770. if (clear_free_space_tree) {
  2771. btrfs_info(fs_info, "clearing free space tree");
  2772. ret = btrfs_clear_free_space_tree(fs_info);
  2773. if (ret) {
  2774. btrfs_warn(fs_info,
  2775. "failed to clear free space tree: %d", ret);
  2776. close_ctree(fs_info);
  2777. return ret;
  2778. }
  2779. }
  2780. if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
  2781. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2782. btrfs_info(fs_info, "creating free space tree");
  2783. ret = btrfs_create_free_space_tree(fs_info);
  2784. if (ret) {
  2785. btrfs_warn(fs_info,
  2786. "failed to create free space tree: %d", ret);
  2787. close_ctree(fs_info);
  2788. return ret;
  2789. }
  2790. }
  2791. down_read(&fs_info->cleanup_work_sem);
  2792. if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
  2793. (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
  2794. up_read(&fs_info->cleanup_work_sem);
  2795. close_ctree(fs_info);
  2796. return ret;
  2797. }
  2798. up_read(&fs_info->cleanup_work_sem);
  2799. ret = btrfs_resume_balance_async(fs_info);
  2800. if (ret) {
  2801. btrfs_warn(fs_info, "failed to resume balance: %d", ret);
  2802. close_ctree(fs_info);
  2803. return ret;
  2804. }
  2805. ret = btrfs_resume_dev_replace_async(fs_info);
  2806. if (ret) {
  2807. btrfs_warn(fs_info, "failed to resume device replace: %d", ret);
  2808. close_ctree(fs_info);
  2809. return ret;
  2810. }
  2811. btrfs_qgroup_rescan_resume(fs_info);
  2812. if (!fs_info->uuid_root) {
  2813. btrfs_info(fs_info, "creating UUID tree");
  2814. ret = btrfs_create_uuid_tree(fs_info);
  2815. if (ret) {
  2816. btrfs_warn(fs_info,
  2817. "failed to create the UUID tree: %d", ret);
  2818. close_ctree(fs_info);
  2819. return ret;
  2820. }
  2821. } else if (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
  2822. fs_info->generation !=
  2823. btrfs_super_uuid_tree_generation(disk_super)) {
  2824. btrfs_info(fs_info, "checking UUID tree");
  2825. ret = btrfs_check_uuid_tree(fs_info);
  2826. if (ret) {
  2827. btrfs_warn(fs_info,
  2828. "failed to check the UUID tree: %d", ret);
  2829. close_ctree(fs_info);
  2830. return ret;
  2831. }
  2832. } else {
  2833. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  2834. }
  2835. set_bit(BTRFS_FS_OPEN, &fs_info->flags);
  2836. /*
  2837. * backuproot only affect mount behavior, and if open_ctree succeeded,
  2838. * no need to keep the flag
  2839. */
  2840. btrfs_clear_opt(fs_info->mount_opt, USEBACKUPROOT);
  2841. return 0;
  2842. fail_qgroup:
  2843. btrfs_free_qgroup_config(fs_info);
  2844. fail_trans_kthread:
  2845. kthread_stop(fs_info->transaction_kthread);
  2846. btrfs_cleanup_transaction(fs_info);
  2847. btrfs_free_fs_roots(fs_info);
  2848. fail_cleaner:
  2849. kthread_stop(fs_info->cleaner_kthread);
  2850. /*
  2851. * make sure we're done with the btree inode before we stop our
  2852. * kthreads
  2853. */
  2854. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  2855. fail_sysfs:
  2856. btrfs_sysfs_remove_mounted(fs_info);
  2857. fail_fsdev_sysfs:
  2858. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  2859. fail_block_groups:
  2860. btrfs_put_block_group_cache(fs_info);
  2861. fail_tree_roots:
  2862. free_root_pointers(fs_info, 1);
  2863. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  2864. fail_sb_buffer:
  2865. btrfs_stop_all_workers(fs_info);
  2866. btrfs_free_block_groups(fs_info);
  2867. fail_alloc:
  2868. fail_iput:
  2869. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  2870. iput(fs_info->btree_inode);
  2871. fail_bio_counter:
  2872. percpu_counter_destroy(&fs_info->bio_counter);
  2873. fail_delalloc_bytes:
  2874. percpu_counter_destroy(&fs_info->delalloc_bytes);
  2875. fail_dirty_metadata_bytes:
  2876. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  2877. fail_srcu:
  2878. cleanup_srcu_struct(&fs_info->subvol_srcu);
  2879. fail:
  2880. btrfs_free_stripe_hash_table(fs_info);
  2881. btrfs_close_devices(fs_info->fs_devices);
  2882. return err;
  2883. recovery_tree_root:
  2884. if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
  2885. goto fail_tree_roots;
  2886. free_root_pointers(fs_info, 0);
  2887. /* don't use the log in recovery mode, it won't be valid */
  2888. btrfs_set_super_log_root(disk_super, 0);
  2889. /* we can't trust the free space cache either */
  2890. btrfs_set_opt(fs_info->mount_opt, CLEAR_CACHE);
  2891. ret = next_root_backup(fs_info, fs_info->super_copy,
  2892. &num_backups_tried, &backup_index);
  2893. if (ret == -1)
  2894. goto fail_block_groups;
  2895. goto retry_root_backup;
  2896. }
  2897. ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
  2898. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  2899. {
  2900. if (uptodate) {
  2901. set_buffer_uptodate(bh);
  2902. } else {
  2903. struct btrfs_device *device = (struct btrfs_device *)
  2904. bh->b_private;
  2905. btrfs_warn_rl_in_rcu(device->fs_info,
  2906. "lost page write due to IO error on %s",
  2907. rcu_str_deref(device->name));
  2908. /* note, we don't set_buffer_write_io_error because we have
  2909. * our own ways of dealing with the IO errors
  2910. */
  2911. clear_buffer_uptodate(bh);
  2912. btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_WRITE_ERRS);
  2913. }
  2914. unlock_buffer(bh);
  2915. put_bh(bh);
  2916. }
  2917. int btrfs_read_dev_one_super(struct block_device *bdev, int copy_num,
  2918. struct buffer_head **bh_ret)
  2919. {
  2920. struct buffer_head *bh;
  2921. struct btrfs_super_block *super;
  2922. u64 bytenr;
  2923. bytenr = btrfs_sb_offset(copy_num);
  2924. if (bytenr + BTRFS_SUPER_INFO_SIZE >= i_size_read(bdev->bd_inode))
  2925. return -EINVAL;
  2926. bh = __bread(bdev, bytenr / BTRFS_BDEV_BLOCKSIZE, BTRFS_SUPER_INFO_SIZE);
  2927. /*
  2928. * If we fail to read from the underlying devices, as of now
  2929. * the best option we have is to mark it EIO.
  2930. */
  2931. if (!bh)
  2932. return -EIO;
  2933. super = (struct btrfs_super_block *)bh->b_data;
  2934. if (btrfs_super_bytenr(super) != bytenr ||
  2935. btrfs_super_magic(super) != BTRFS_MAGIC) {
  2936. brelse(bh);
  2937. return -EINVAL;
  2938. }
  2939. *bh_ret = bh;
  2940. return 0;
  2941. }
  2942. struct buffer_head *btrfs_read_dev_super(struct block_device *bdev)
  2943. {
  2944. struct buffer_head *bh;
  2945. struct buffer_head *latest = NULL;
  2946. struct btrfs_super_block *super;
  2947. int i;
  2948. u64 transid = 0;
  2949. int ret = -EINVAL;
  2950. /* we would like to check all the supers, but that would make
  2951. * a btrfs mount succeed after a mkfs from a different FS.
  2952. * So, we need to add a special mount option to scan for
  2953. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  2954. */
  2955. for (i = 0; i < 1; i++) {
  2956. ret = btrfs_read_dev_one_super(bdev, i, &bh);
  2957. if (ret)
  2958. continue;
  2959. super = (struct btrfs_super_block *)bh->b_data;
  2960. if (!latest || btrfs_super_generation(super) > transid) {
  2961. brelse(latest);
  2962. latest = bh;
  2963. transid = btrfs_super_generation(super);
  2964. } else {
  2965. brelse(bh);
  2966. }
  2967. }
  2968. if (!latest)
  2969. return ERR_PTR(ret);
  2970. return latest;
  2971. }
  2972. /*
  2973. * Write superblock @sb to the @device. Do not wait for completion, all the
  2974. * buffer heads we write are pinned.
  2975. *
  2976. * Write @max_mirrors copies of the superblock, where 0 means default that fit
  2977. * the expected device size at commit time. Note that max_mirrors must be
  2978. * same for write and wait phases.
  2979. *
  2980. * Return number of errors when buffer head is not found or submission fails.
  2981. */
  2982. static int write_dev_supers(struct btrfs_device *device,
  2983. struct btrfs_super_block *sb, int max_mirrors)
  2984. {
  2985. struct buffer_head *bh;
  2986. int i;
  2987. int ret;
  2988. int errors = 0;
  2989. u32 crc;
  2990. u64 bytenr;
  2991. int op_flags;
  2992. if (max_mirrors == 0)
  2993. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  2994. for (i = 0; i < max_mirrors; i++) {
  2995. bytenr = btrfs_sb_offset(i);
  2996. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  2997. device->commit_total_bytes)
  2998. break;
  2999. btrfs_set_super_bytenr(sb, bytenr);
  3000. crc = ~(u32)0;
  3001. crc = btrfs_csum_data((const char *)sb + BTRFS_CSUM_SIZE, crc,
  3002. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE);
  3003. btrfs_csum_final(crc, sb->csum);
  3004. /* One reference for us, and we leave it for the caller */
  3005. bh = __getblk(device->bdev, bytenr / BTRFS_BDEV_BLOCKSIZE,
  3006. BTRFS_SUPER_INFO_SIZE);
  3007. if (!bh) {
  3008. btrfs_err(device->fs_info,
  3009. "couldn't get super buffer head for bytenr %llu",
  3010. bytenr);
  3011. errors++;
  3012. continue;
  3013. }
  3014. memcpy(bh->b_data, sb, BTRFS_SUPER_INFO_SIZE);
  3015. /* one reference for submit_bh */
  3016. get_bh(bh);
  3017. set_buffer_uptodate(bh);
  3018. lock_buffer(bh);
  3019. bh->b_end_io = btrfs_end_buffer_write_sync;
  3020. bh->b_private = device;
  3021. /*
  3022. * we fua the first super. The others we allow
  3023. * to go down lazy.
  3024. */
  3025. op_flags = REQ_SYNC | REQ_META | REQ_PRIO;
  3026. if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
  3027. op_flags |= REQ_FUA;
  3028. ret = btrfsic_submit_bh(REQ_OP_WRITE, op_flags, bh);
  3029. if (ret)
  3030. errors++;
  3031. }
  3032. return errors < i ? 0 : -1;
  3033. }
  3034. /*
  3035. * Wait for write completion of superblocks done by write_dev_supers,
  3036. * @max_mirrors same for write and wait phases.
  3037. *
  3038. * Return number of errors when buffer head is not found or not marked up to
  3039. * date.
  3040. */
  3041. static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
  3042. {
  3043. struct buffer_head *bh;
  3044. int i;
  3045. int errors = 0;
  3046. bool primary_failed = false;
  3047. u64 bytenr;
  3048. if (max_mirrors == 0)
  3049. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  3050. for (i = 0; i < max_mirrors; i++) {
  3051. bytenr = btrfs_sb_offset(i);
  3052. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  3053. device->commit_total_bytes)
  3054. break;
  3055. bh = __find_get_block(device->bdev,
  3056. bytenr / BTRFS_BDEV_BLOCKSIZE,
  3057. BTRFS_SUPER_INFO_SIZE);
  3058. if (!bh) {
  3059. errors++;
  3060. if (i == 0)
  3061. primary_failed = true;
  3062. continue;
  3063. }
  3064. wait_on_buffer(bh);
  3065. if (!buffer_uptodate(bh)) {
  3066. errors++;
  3067. if (i == 0)
  3068. primary_failed = true;
  3069. }
  3070. /* drop our reference */
  3071. brelse(bh);
  3072. /* drop the reference from the writing run */
  3073. brelse(bh);
  3074. }
  3075. /* log error, force error return */
  3076. if (primary_failed) {
  3077. btrfs_err(device->fs_info, "error writing primary super block to device %llu",
  3078. device->devid);
  3079. return -1;
  3080. }
  3081. return errors < i ? 0 : -1;
  3082. }
  3083. /*
  3084. * endio for the write_dev_flush, this will wake anyone waiting
  3085. * for the barrier when it is done
  3086. */
  3087. static void btrfs_end_empty_barrier(struct bio *bio)
  3088. {
  3089. complete(bio->bi_private);
  3090. }
  3091. /*
  3092. * Submit a flush request to the device if it supports it. Error handling is
  3093. * done in the waiting counterpart.
  3094. */
  3095. static void write_dev_flush(struct btrfs_device *device)
  3096. {
  3097. struct request_queue *q = bdev_get_queue(device->bdev);
  3098. struct bio *bio = device->flush_bio;
  3099. if (!test_bit(QUEUE_FLAG_WC, &q->queue_flags))
  3100. return;
  3101. bio_reset(bio);
  3102. bio->bi_end_io = btrfs_end_empty_barrier;
  3103. bio_set_dev(bio, device->bdev);
  3104. bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH;
  3105. init_completion(&device->flush_wait);
  3106. bio->bi_private = &device->flush_wait;
  3107. btrfsic_submit_bio(bio);
  3108. set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
  3109. }
  3110. /*
  3111. * If the flush bio has been submitted by write_dev_flush, wait for it.
  3112. */
  3113. static blk_status_t wait_dev_flush(struct btrfs_device *device)
  3114. {
  3115. struct bio *bio = device->flush_bio;
  3116. if (!test_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
  3117. return BLK_STS_OK;
  3118. clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
  3119. wait_for_completion_io(&device->flush_wait);
  3120. return bio->bi_status;
  3121. }
  3122. static int check_barrier_error(struct btrfs_fs_info *fs_info)
  3123. {
  3124. if (!btrfs_check_rw_degradable(fs_info, NULL))
  3125. return -EIO;
  3126. return 0;
  3127. }
  3128. /*
  3129. * send an empty flush down to each device in parallel,
  3130. * then wait for them
  3131. */
  3132. static int barrier_all_devices(struct btrfs_fs_info *info)
  3133. {
  3134. struct list_head *head;
  3135. struct btrfs_device *dev;
  3136. int errors_wait = 0;
  3137. blk_status_t ret;
  3138. lockdep_assert_held(&info->fs_devices->device_list_mutex);
  3139. /* send down all the barriers */
  3140. head = &info->fs_devices->devices;
  3141. list_for_each_entry(dev, head, dev_list) {
  3142. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3143. continue;
  3144. if (!dev->bdev)
  3145. continue;
  3146. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3147. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3148. continue;
  3149. write_dev_flush(dev);
  3150. dev->last_flush_error = BLK_STS_OK;
  3151. }
  3152. /* wait for all the barriers */
  3153. list_for_each_entry(dev, head, dev_list) {
  3154. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3155. continue;
  3156. if (!dev->bdev) {
  3157. errors_wait++;
  3158. continue;
  3159. }
  3160. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3161. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3162. continue;
  3163. ret = wait_dev_flush(dev);
  3164. if (ret) {
  3165. dev->last_flush_error = ret;
  3166. btrfs_dev_stat_inc_and_print(dev,
  3167. BTRFS_DEV_STAT_FLUSH_ERRS);
  3168. errors_wait++;
  3169. }
  3170. }
  3171. if (errors_wait) {
  3172. /*
  3173. * At some point we need the status of all disks
  3174. * to arrive at the volume status. So error checking
  3175. * is being pushed to a separate loop.
  3176. */
  3177. return check_barrier_error(info);
  3178. }
  3179. return 0;
  3180. }
  3181. int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
  3182. {
  3183. int raid_type;
  3184. int min_tolerated = INT_MAX;
  3185. if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
  3186. (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
  3187. min_tolerated = min(min_tolerated,
  3188. btrfs_raid_array[BTRFS_RAID_SINGLE].
  3189. tolerated_failures);
  3190. for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
  3191. if (raid_type == BTRFS_RAID_SINGLE)
  3192. continue;
  3193. if (!(flags & btrfs_raid_array[raid_type].bg_flag))
  3194. continue;
  3195. min_tolerated = min(min_tolerated,
  3196. btrfs_raid_array[raid_type].
  3197. tolerated_failures);
  3198. }
  3199. if (min_tolerated == INT_MAX) {
  3200. pr_warn("BTRFS: unknown raid flag: %llu", flags);
  3201. min_tolerated = 0;
  3202. }
  3203. return min_tolerated;
  3204. }
  3205. int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  3206. {
  3207. struct list_head *head;
  3208. struct btrfs_device *dev;
  3209. struct btrfs_super_block *sb;
  3210. struct btrfs_dev_item *dev_item;
  3211. int ret;
  3212. int do_barriers;
  3213. int max_errors;
  3214. int total_errors = 0;
  3215. u64 flags;
  3216. do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
  3217. /*
  3218. * max_mirrors == 0 indicates we're from commit_transaction,
  3219. * not from fsync where the tree roots in fs_info have not
  3220. * been consistent on disk.
  3221. */
  3222. if (max_mirrors == 0)
  3223. backup_super_roots(fs_info);
  3224. sb = fs_info->super_for_commit;
  3225. dev_item = &sb->dev_item;
  3226. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3227. head = &fs_info->fs_devices->devices;
  3228. max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
  3229. if (do_barriers) {
  3230. ret = barrier_all_devices(fs_info);
  3231. if (ret) {
  3232. mutex_unlock(
  3233. &fs_info->fs_devices->device_list_mutex);
  3234. btrfs_handle_fs_error(fs_info, ret,
  3235. "errors while submitting device barriers.");
  3236. return ret;
  3237. }
  3238. }
  3239. list_for_each_entry(dev, head, dev_list) {
  3240. if (!dev->bdev) {
  3241. total_errors++;
  3242. continue;
  3243. }
  3244. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3245. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3246. continue;
  3247. btrfs_set_stack_device_generation(dev_item, 0);
  3248. btrfs_set_stack_device_type(dev_item, dev->type);
  3249. btrfs_set_stack_device_id(dev_item, dev->devid);
  3250. btrfs_set_stack_device_total_bytes(dev_item,
  3251. dev->commit_total_bytes);
  3252. btrfs_set_stack_device_bytes_used(dev_item,
  3253. dev->commit_bytes_used);
  3254. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  3255. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  3256. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  3257. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  3258. memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE);
  3259. flags = btrfs_super_flags(sb);
  3260. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  3261. ret = btrfs_validate_write_super(fs_info, sb);
  3262. if (ret < 0) {
  3263. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3264. btrfs_handle_fs_error(fs_info, -EUCLEAN,
  3265. "unexpected superblock corruption detected");
  3266. return -EUCLEAN;
  3267. }
  3268. ret = write_dev_supers(dev, sb, max_mirrors);
  3269. if (ret)
  3270. total_errors++;
  3271. }
  3272. if (total_errors > max_errors) {
  3273. btrfs_err(fs_info, "%d errors while writing supers",
  3274. total_errors);
  3275. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3276. /* FUA is masked off if unsupported and can't be the reason */
  3277. btrfs_handle_fs_error(fs_info, -EIO,
  3278. "%d errors while writing supers",
  3279. total_errors);
  3280. return -EIO;
  3281. }
  3282. total_errors = 0;
  3283. list_for_each_entry(dev, head, dev_list) {
  3284. if (!dev->bdev)
  3285. continue;
  3286. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3287. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3288. continue;
  3289. ret = wait_dev_supers(dev, max_mirrors);
  3290. if (ret)
  3291. total_errors++;
  3292. }
  3293. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3294. if (total_errors > max_errors) {
  3295. btrfs_handle_fs_error(fs_info, -EIO,
  3296. "%d errors while writing supers",
  3297. total_errors);
  3298. return -EIO;
  3299. }
  3300. return 0;
  3301. }
  3302. /* Drop a fs root from the radix tree and free it. */
  3303. void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
  3304. struct btrfs_root *root)
  3305. {
  3306. spin_lock(&fs_info->fs_roots_radix_lock);
  3307. radix_tree_delete(&fs_info->fs_roots_radix,
  3308. (unsigned long)root->root_key.objectid);
  3309. spin_unlock(&fs_info->fs_roots_radix_lock);
  3310. if (btrfs_root_refs(&root->root_item) == 0)
  3311. synchronize_srcu(&fs_info->subvol_srcu);
  3312. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state)) {
  3313. btrfs_free_log(NULL, root);
  3314. if (root->reloc_root) {
  3315. free_extent_buffer(root->reloc_root->node);
  3316. free_extent_buffer(root->reloc_root->commit_root);
  3317. btrfs_put_fs_root(root->reloc_root);
  3318. root->reloc_root = NULL;
  3319. }
  3320. }
  3321. if (root->free_ino_pinned)
  3322. __btrfs_remove_free_space_cache(root->free_ino_pinned);
  3323. if (root->free_ino_ctl)
  3324. __btrfs_remove_free_space_cache(root->free_ino_ctl);
  3325. free_fs_root(root);
  3326. }
  3327. static void free_fs_root(struct btrfs_root *root)
  3328. {
  3329. iput(root->ino_cache_inode);
  3330. WARN_ON(!RB_EMPTY_ROOT(&root->inode_tree));
  3331. if (root->anon_dev)
  3332. free_anon_bdev(root->anon_dev);
  3333. if (root->subv_writers)
  3334. btrfs_free_subvolume_writers(root->subv_writers);
  3335. free_extent_buffer(root->node);
  3336. free_extent_buffer(root->commit_root);
  3337. kfree(root->free_ino_ctl);
  3338. kfree(root->free_ino_pinned);
  3339. kfree(root->name);
  3340. btrfs_put_fs_root(root);
  3341. }
  3342. void btrfs_free_fs_root(struct btrfs_root *root)
  3343. {
  3344. free_fs_root(root);
  3345. }
  3346. int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  3347. {
  3348. u64 root_objectid = 0;
  3349. struct btrfs_root *gang[8];
  3350. int i = 0;
  3351. int err = 0;
  3352. unsigned int ret = 0;
  3353. int index;
  3354. while (1) {
  3355. index = srcu_read_lock(&fs_info->subvol_srcu);
  3356. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  3357. (void **)gang, root_objectid,
  3358. ARRAY_SIZE(gang));
  3359. if (!ret) {
  3360. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3361. break;
  3362. }
  3363. root_objectid = gang[ret - 1]->root_key.objectid + 1;
  3364. for (i = 0; i < ret; i++) {
  3365. /* Avoid to grab roots in dead_roots */
  3366. if (btrfs_root_refs(&gang[i]->root_item) == 0) {
  3367. gang[i] = NULL;
  3368. continue;
  3369. }
  3370. /* grab all the search result for later use */
  3371. gang[i] = btrfs_grab_fs_root(gang[i]);
  3372. }
  3373. srcu_read_unlock(&fs_info->subvol_srcu, index);
  3374. for (i = 0; i < ret; i++) {
  3375. if (!gang[i])
  3376. continue;
  3377. root_objectid = gang[i]->root_key.objectid;
  3378. err = btrfs_orphan_cleanup(gang[i]);
  3379. if (err)
  3380. break;
  3381. btrfs_put_fs_root(gang[i]);
  3382. }
  3383. root_objectid++;
  3384. }
  3385. /* release the uncleaned roots due to error */
  3386. for (; i < ret; i++) {
  3387. if (gang[i])
  3388. btrfs_put_fs_root(gang[i]);
  3389. }
  3390. return err;
  3391. }
  3392. int btrfs_commit_super(struct btrfs_fs_info *fs_info)
  3393. {
  3394. struct btrfs_root *root = fs_info->tree_root;
  3395. struct btrfs_trans_handle *trans;
  3396. mutex_lock(&fs_info->cleaner_mutex);
  3397. btrfs_run_delayed_iputs(fs_info);
  3398. mutex_unlock(&fs_info->cleaner_mutex);
  3399. wake_up_process(fs_info->cleaner_kthread);
  3400. /* wait until ongoing cleanup work done */
  3401. down_write(&fs_info->cleanup_work_sem);
  3402. up_write(&fs_info->cleanup_work_sem);
  3403. trans = btrfs_join_transaction(root);
  3404. if (IS_ERR(trans))
  3405. return PTR_ERR(trans);
  3406. return btrfs_commit_transaction(trans);
  3407. }
  3408. void close_ctree(struct btrfs_fs_info *fs_info)
  3409. {
  3410. int ret;
  3411. set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
  3412. /* wait for the qgroup rescan worker to stop */
  3413. btrfs_qgroup_wait_for_completion(fs_info, false);
  3414. /* wait for the uuid_scan task to finish */
  3415. down(&fs_info->uuid_tree_rescan_sem);
  3416. /* avoid complains from lockdep et al., set sem back to initial state */
  3417. up(&fs_info->uuid_tree_rescan_sem);
  3418. /* pause restriper - we want to resume on mount */
  3419. btrfs_pause_balance(fs_info);
  3420. btrfs_dev_replace_suspend_for_unmount(fs_info);
  3421. btrfs_scrub_cancel(fs_info);
  3422. /* wait for any defraggers to finish */
  3423. wait_event(fs_info->transaction_wait,
  3424. (atomic_read(&fs_info->defrag_running) == 0));
  3425. /* clear out the rbtree of defraggable inodes */
  3426. btrfs_cleanup_defrag_inodes(fs_info);
  3427. cancel_work_sync(&fs_info->async_reclaim_work);
  3428. if (!sb_rdonly(fs_info->sb)) {
  3429. /*
  3430. * If the cleaner thread is stopped and there are
  3431. * block groups queued for removal, the deletion will be
  3432. * skipped when we quit the cleaner thread.
  3433. */
  3434. btrfs_delete_unused_bgs(fs_info);
  3435. ret = btrfs_commit_super(fs_info);
  3436. if (ret)
  3437. btrfs_err(fs_info, "commit super ret %d", ret);
  3438. }
  3439. if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state) ||
  3440. test_bit(BTRFS_FS_STATE_TRANS_ABORTED, &fs_info->fs_state))
  3441. btrfs_error_commit_super(fs_info);
  3442. kthread_stop(fs_info->transaction_kthread);
  3443. kthread_stop(fs_info->cleaner_kthread);
  3444. set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
  3445. btrfs_free_qgroup_config(fs_info);
  3446. ASSERT(list_empty(&fs_info->delalloc_roots));
  3447. if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
  3448. btrfs_info(fs_info, "at unmount delalloc count %lld",
  3449. percpu_counter_sum(&fs_info->delalloc_bytes));
  3450. }
  3451. btrfs_sysfs_remove_mounted(fs_info);
  3452. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  3453. btrfs_free_fs_roots(fs_info);
  3454. btrfs_put_block_group_cache(fs_info);
  3455. /*
  3456. * we must make sure there is not any read request to
  3457. * submit after we stopping all workers.
  3458. */
  3459. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  3460. btrfs_stop_all_workers(fs_info);
  3461. btrfs_free_block_groups(fs_info);
  3462. clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
  3463. free_root_pointers(fs_info, 1);
  3464. iput(fs_info->btree_inode);
  3465. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3466. if (btrfs_test_opt(fs_info, CHECK_INTEGRITY))
  3467. btrfsic_unmount(fs_info->fs_devices);
  3468. #endif
  3469. btrfs_close_devices(fs_info->fs_devices);
  3470. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  3471. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  3472. percpu_counter_destroy(&fs_info->delalloc_bytes);
  3473. percpu_counter_destroy(&fs_info->bio_counter);
  3474. cleanup_srcu_struct(&fs_info->subvol_srcu);
  3475. btrfs_free_stripe_hash_table(fs_info);
  3476. btrfs_free_ref_cache(fs_info);
  3477. while (!list_empty(&fs_info->pinned_chunks)) {
  3478. struct extent_map *em;
  3479. em = list_first_entry(&fs_info->pinned_chunks,
  3480. struct extent_map, list);
  3481. list_del_init(&em->list);
  3482. free_extent_map(em);
  3483. }
  3484. }
  3485. int btrfs_buffer_uptodate(struct extent_buffer *buf, u64 parent_transid,
  3486. int atomic)
  3487. {
  3488. int ret;
  3489. struct inode *btree_inode = buf->pages[0]->mapping->host;
  3490. ret = extent_buffer_uptodate(buf);
  3491. if (!ret)
  3492. return ret;
  3493. ret = verify_parent_transid(&BTRFS_I(btree_inode)->io_tree, buf,
  3494. parent_transid, atomic);
  3495. if (ret == -EAGAIN)
  3496. return ret;
  3497. return !ret;
  3498. }
  3499. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  3500. {
  3501. struct btrfs_fs_info *fs_info;
  3502. struct btrfs_root *root;
  3503. u64 transid = btrfs_header_generation(buf);
  3504. int was_dirty;
  3505. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3506. /*
  3507. * This is a fast path so only do this check if we have sanity tests
  3508. * enabled. Normal people shouldn't be using umapped buffers as dirty
  3509. * outside of the sanity tests.
  3510. */
  3511. if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
  3512. return;
  3513. #endif
  3514. root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3515. fs_info = root->fs_info;
  3516. btrfs_assert_tree_locked(buf);
  3517. if (transid != fs_info->generation)
  3518. WARN(1, KERN_CRIT "btrfs transid mismatch buffer %llu, found %llu running %llu\n",
  3519. buf->start, transid, fs_info->generation);
  3520. was_dirty = set_extent_buffer_dirty(buf);
  3521. if (!was_dirty)
  3522. percpu_counter_add_batch(&fs_info->dirty_metadata_bytes,
  3523. buf->len,
  3524. fs_info->dirty_metadata_batch);
  3525. #ifdef CONFIG_BTRFS_FS_CHECK_INTEGRITY
  3526. /*
  3527. * Since btrfs_mark_buffer_dirty() can be called with item pointer set
  3528. * but item data not updated.
  3529. * So here we should only check item pointers, not item data.
  3530. */
  3531. if (btrfs_header_level(buf) == 0 &&
  3532. btrfs_check_leaf_relaxed(fs_info, buf)) {
  3533. btrfs_print_leaf(buf);
  3534. ASSERT(0);
  3535. }
  3536. #endif
  3537. }
  3538. static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
  3539. int flush_delayed)
  3540. {
  3541. /*
  3542. * looks as though older kernels can get into trouble with
  3543. * this code, they end up stuck in balance_dirty_pages forever
  3544. */
  3545. int ret;
  3546. if (current->flags & PF_MEMALLOC)
  3547. return;
  3548. if (flush_delayed)
  3549. btrfs_balance_delayed_items(fs_info);
  3550. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  3551. BTRFS_DIRTY_METADATA_THRESH,
  3552. fs_info->dirty_metadata_batch);
  3553. if (ret > 0) {
  3554. balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
  3555. }
  3556. }
  3557. void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
  3558. {
  3559. __btrfs_btree_balance_dirty(fs_info, 1);
  3560. }
  3561. void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
  3562. {
  3563. __btrfs_btree_balance_dirty(fs_info, 0);
  3564. }
  3565. int btrfs_read_buffer(struct extent_buffer *buf, u64 parent_transid, int level,
  3566. struct btrfs_key *first_key)
  3567. {
  3568. struct btrfs_root *root = BTRFS_I(buf->pages[0]->mapping->host)->root;
  3569. struct btrfs_fs_info *fs_info = root->fs_info;
  3570. return btree_read_extent_buffer_pages(fs_info, buf, parent_transid,
  3571. level, first_key);
  3572. }
  3573. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
  3574. {
  3575. /* cleanup FS via transaction */
  3576. btrfs_cleanup_transaction(fs_info);
  3577. mutex_lock(&fs_info->cleaner_mutex);
  3578. btrfs_run_delayed_iputs(fs_info);
  3579. mutex_unlock(&fs_info->cleaner_mutex);
  3580. down_write(&fs_info->cleanup_work_sem);
  3581. up_write(&fs_info->cleanup_work_sem);
  3582. }
  3583. static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
  3584. {
  3585. struct btrfs_ordered_extent *ordered;
  3586. spin_lock(&root->ordered_extent_lock);
  3587. /*
  3588. * This will just short circuit the ordered completion stuff which will
  3589. * make sure the ordered extent gets properly cleaned up.
  3590. */
  3591. list_for_each_entry(ordered, &root->ordered_extents,
  3592. root_extent_list)
  3593. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  3594. spin_unlock(&root->ordered_extent_lock);
  3595. }
  3596. static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
  3597. {
  3598. struct btrfs_root *root;
  3599. struct list_head splice;
  3600. INIT_LIST_HEAD(&splice);
  3601. spin_lock(&fs_info->ordered_root_lock);
  3602. list_splice_init(&fs_info->ordered_roots, &splice);
  3603. while (!list_empty(&splice)) {
  3604. root = list_first_entry(&splice, struct btrfs_root,
  3605. ordered_root);
  3606. list_move_tail(&root->ordered_root,
  3607. &fs_info->ordered_roots);
  3608. spin_unlock(&fs_info->ordered_root_lock);
  3609. btrfs_destroy_ordered_extents(root);
  3610. cond_resched();
  3611. spin_lock(&fs_info->ordered_root_lock);
  3612. }
  3613. spin_unlock(&fs_info->ordered_root_lock);
  3614. }
  3615. static int btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  3616. struct btrfs_fs_info *fs_info)
  3617. {
  3618. struct rb_node *node;
  3619. struct btrfs_delayed_ref_root *delayed_refs;
  3620. struct btrfs_delayed_ref_node *ref;
  3621. int ret = 0;
  3622. delayed_refs = &trans->delayed_refs;
  3623. spin_lock(&delayed_refs->lock);
  3624. if (atomic_read(&delayed_refs->num_entries) == 0) {
  3625. spin_unlock(&delayed_refs->lock);
  3626. btrfs_info(fs_info, "delayed_refs has NO entry");
  3627. return ret;
  3628. }
  3629. while ((node = rb_first(&delayed_refs->href_root)) != NULL) {
  3630. struct btrfs_delayed_ref_head *head;
  3631. struct rb_node *n;
  3632. bool pin_bytes = false;
  3633. head = rb_entry(node, struct btrfs_delayed_ref_head,
  3634. href_node);
  3635. if (!mutex_trylock(&head->mutex)) {
  3636. refcount_inc(&head->refs);
  3637. spin_unlock(&delayed_refs->lock);
  3638. mutex_lock(&head->mutex);
  3639. mutex_unlock(&head->mutex);
  3640. btrfs_put_delayed_ref_head(head);
  3641. spin_lock(&delayed_refs->lock);
  3642. continue;
  3643. }
  3644. spin_lock(&head->lock);
  3645. while ((n = rb_first(&head->ref_tree)) != NULL) {
  3646. ref = rb_entry(n, struct btrfs_delayed_ref_node,
  3647. ref_node);
  3648. ref->in_tree = 0;
  3649. rb_erase(&ref->ref_node, &head->ref_tree);
  3650. RB_CLEAR_NODE(&ref->ref_node);
  3651. if (!list_empty(&ref->add_list))
  3652. list_del(&ref->add_list);
  3653. atomic_dec(&delayed_refs->num_entries);
  3654. btrfs_put_delayed_ref(ref);
  3655. }
  3656. if (head->must_insert_reserved)
  3657. pin_bytes = true;
  3658. btrfs_free_delayed_extent_op(head->extent_op);
  3659. delayed_refs->num_heads--;
  3660. if (head->processing == 0)
  3661. delayed_refs->num_heads_ready--;
  3662. atomic_dec(&delayed_refs->num_entries);
  3663. rb_erase(&head->href_node, &delayed_refs->href_root);
  3664. RB_CLEAR_NODE(&head->href_node);
  3665. spin_unlock(&head->lock);
  3666. spin_unlock(&delayed_refs->lock);
  3667. mutex_unlock(&head->mutex);
  3668. if (pin_bytes)
  3669. btrfs_pin_extent(fs_info, head->bytenr,
  3670. head->num_bytes, 1);
  3671. btrfs_put_delayed_ref_head(head);
  3672. cond_resched();
  3673. spin_lock(&delayed_refs->lock);
  3674. }
  3675. spin_unlock(&delayed_refs->lock);
  3676. return ret;
  3677. }
  3678. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
  3679. {
  3680. struct btrfs_inode *btrfs_inode;
  3681. struct list_head splice;
  3682. INIT_LIST_HEAD(&splice);
  3683. spin_lock(&root->delalloc_lock);
  3684. list_splice_init(&root->delalloc_inodes, &splice);
  3685. while (!list_empty(&splice)) {
  3686. struct inode *inode = NULL;
  3687. btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
  3688. delalloc_inodes);
  3689. __btrfs_del_delalloc_inode(root, btrfs_inode);
  3690. spin_unlock(&root->delalloc_lock);
  3691. /*
  3692. * Make sure we get a live inode and that it'll not disappear
  3693. * meanwhile.
  3694. */
  3695. inode = igrab(&btrfs_inode->vfs_inode);
  3696. if (inode) {
  3697. invalidate_inode_pages2(inode->i_mapping);
  3698. iput(inode);
  3699. }
  3700. spin_lock(&root->delalloc_lock);
  3701. }
  3702. spin_unlock(&root->delalloc_lock);
  3703. }
  3704. static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
  3705. {
  3706. struct btrfs_root *root;
  3707. struct list_head splice;
  3708. INIT_LIST_HEAD(&splice);
  3709. spin_lock(&fs_info->delalloc_root_lock);
  3710. list_splice_init(&fs_info->delalloc_roots, &splice);
  3711. while (!list_empty(&splice)) {
  3712. root = list_first_entry(&splice, struct btrfs_root,
  3713. delalloc_root);
  3714. root = btrfs_grab_fs_root(root);
  3715. BUG_ON(!root);
  3716. spin_unlock(&fs_info->delalloc_root_lock);
  3717. btrfs_destroy_delalloc_inodes(root);
  3718. btrfs_put_fs_root(root);
  3719. spin_lock(&fs_info->delalloc_root_lock);
  3720. }
  3721. spin_unlock(&fs_info->delalloc_root_lock);
  3722. }
  3723. static int btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
  3724. struct extent_io_tree *dirty_pages,
  3725. int mark)
  3726. {
  3727. int ret;
  3728. struct extent_buffer *eb;
  3729. u64 start = 0;
  3730. u64 end;
  3731. while (1) {
  3732. ret = find_first_extent_bit(dirty_pages, start, &start, &end,
  3733. mark, NULL);
  3734. if (ret)
  3735. break;
  3736. clear_extent_bits(dirty_pages, start, end, mark);
  3737. while (start <= end) {
  3738. eb = find_extent_buffer(fs_info, start);
  3739. start += fs_info->nodesize;
  3740. if (!eb)
  3741. continue;
  3742. wait_on_extent_buffer_writeback(eb);
  3743. if (test_and_clear_bit(EXTENT_BUFFER_DIRTY,
  3744. &eb->bflags))
  3745. clear_extent_buffer_dirty(eb);
  3746. free_extent_buffer_stale(eb);
  3747. }
  3748. }
  3749. return ret;
  3750. }
  3751. static int btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
  3752. struct extent_io_tree *pinned_extents)
  3753. {
  3754. struct extent_io_tree *unpin;
  3755. u64 start;
  3756. u64 end;
  3757. int ret;
  3758. bool loop = true;
  3759. unpin = pinned_extents;
  3760. again:
  3761. while (1) {
  3762. ret = find_first_extent_bit(unpin, 0, &start, &end,
  3763. EXTENT_DIRTY, NULL);
  3764. if (ret)
  3765. break;
  3766. clear_extent_dirty(unpin, start, end);
  3767. btrfs_error_unpin_extent_range(fs_info, start, end);
  3768. cond_resched();
  3769. }
  3770. if (loop) {
  3771. if (unpin == &fs_info->freed_extents[0])
  3772. unpin = &fs_info->freed_extents[1];
  3773. else
  3774. unpin = &fs_info->freed_extents[0];
  3775. loop = false;
  3776. goto again;
  3777. }
  3778. return 0;
  3779. }
  3780. static void btrfs_cleanup_bg_io(struct btrfs_block_group_cache *cache)
  3781. {
  3782. struct inode *inode;
  3783. inode = cache->io_ctl.inode;
  3784. if (inode) {
  3785. invalidate_inode_pages2(inode->i_mapping);
  3786. BTRFS_I(inode)->generation = 0;
  3787. cache->io_ctl.inode = NULL;
  3788. iput(inode);
  3789. }
  3790. btrfs_put_block_group(cache);
  3791. }
  3792. void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
  3793. struct btrfs_fs_info *fs_info)
  3794. {
  3795. struct btrfs_block_group_cache *cache;
  3796. spin_lock(&cur_trans->dirty_bgs_lock);
  3797. while (!list_empty(&cur_trans->dirty_bgs)) {
  3798. cache = list_first_entry(&cur_trans->dirty_bgs,
  3799. struct btrfs_block_group_cache,
  3800. dirty_list);
  3801. if (!list_empty(&cache->io_list)) {
  3802. spin_unlock(&cur_trans->dirty_bgs_lock);
  3803. list_del_init(&cache->io_list);
  3804. btrfs_cleanup_bg_io(cache);
  3805. spin_lock(&cur_trans->dirty_bgs_lock);
  3806. }
  3807. list_del_init(&cache->dirty_list);
  3808. spin_lock(&cache->lock);
  3809. cache->disk_cache_state = BTRFS_DC_ERROR;
  3810. spin_unlock(&cache->lock);
  3811. spin_unlock(&cur_trans->dirty_bgs_lock);
  3812. btrfs_put_block_group(cache);
  3813. spin_lock(&cur_trans->dirty_bgs_lock);
  3814. }
  3815. spin_unlock(&cur_trans->dirty_bgs_lock);
  3816. /*
  3817. * Refer to the definition of io_bgs member for details why it's safe
  3818. * to use it without any locking
  3819. */
  3820. while (!list_empty(&cur_trans->io_bgs)) {
  3821. cache = list_first_entry(&cur_trans->io_bgs,
  3822. struct btrfs_block_group_cache,
  3823. io_list);
  3824. list_del_init(&cache->io_list);
  3825. spin_lock(&cache->lock);
  3826. cache->disk_cache_state = BTRFS_DC_ERROR;
  3827. spin_unlock(&cache->lock);
  3828. btrfs_cleanup_bg_io(cache);
  3829. }
  3830. }
  3831. void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
  3832. struct btrfs_fs_info *fs_info)
  3833. {
  3834. btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
  3835. ASSERT(list_empty(&cur_trans->dirty_bgs));
  3836. ASSERT(list_empty(&cur_trans->io_bgs));
  3837. btrfs_destroy_delayed_refs(cur_trans, fs_info);
  3838. cur_trans->state = TRANS_STATE_COMMIT_START;
  3839. wake_up(&fs_info->transaction_blocked_wait);
  3840. cur_trans->state = TRANS_STATE_UNBLOCKED;
  3841. wake_up(&fs_info->transaction_wait);
  3842. btrfs_destroy_delayed_inodes(fs_info);
  3843. btrfs_assert_delayed_root_empty(fs_info);
  3844. btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
  3845. EXTENT_DIRTY);
  3846. btrfs_destroy_pinned_extent(fs_info,
  3847. fs_info->pinned_extents);
  3848. cur_trans->state =TRANS_STATE_COMPLETED;
  3849. wake_up(&cur_trans->commit_wait);
  3850. }
  3851. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
  3852. {
  3853. struct btrfs_transaction *t;
  3854. mutex_lock(&fs_info->transaction_kthread_mutex);
  3855. spin_lock(&fs_info->trans_lock);
  3856. while (!list_empty(&fs_info->trans_list)) {
  3857. t = list_first_entry(&fs_info->trans_list,
  3858. struct btrfs_transaction, list);
  3859. if (t->state >= TRANS_STATE_COMMIT_START) {
  3860. refcount_inc(&t->use_count);
  3861. spin_unlock(&fs_info->trans_lock);
  3862. btrfs_wait_for_commit(fs_info, t->transid);
  3863. btrfs_put_transaction(t);
  3864. spin_lock(&fs_info->trans_lock);
  3865. continue;
  3866. }
  3867. if (t == fs_info->running_transaction) {
  3868. t->state = TRANS_STATE_COMMIT_DOING;
  3869. spin_unlock(&fs_info->trans_lock);
  3870. /*
  3871. * We wait for 0 num_writers since we don't hold a trans
  3872. * handle open currently for this transaction.
  3873. */
  3874. wait_event(t->writer_wait,
  3875. atomic_read(&t->num_writers) == 0);
  3876. } else {
  3877. spin_unlock(&fs_info->trans_lock);
  3878. }
  3879. btrfs_cleanup_one_transaction(t, fs_info);
  3880. spin_lock(&fs_info->trans_lock);
  3881. if (t == fs_info->running_transaction)
  3882. fs_info->running_transaction = NULL;
  3883. list_del_init(&t->list);
  3884. spin_unlock(&fs_info->trans_lock);
  3885. btrfs_put_transaction(t);
  3886. trace_btrfs_transaction_commit(fs_info->tree_root);
  3887. spin_lock(&fs_info->trans_lock);
  3888. }
  3889. spin_unlock(&fs_info->trans_lock);
  3890. btrfs_destroy_all_ordered_extents(fs_info);
  3891. btrfs_destroy_delayed_inodes(fs_info);
  3892. btrfs_assert_delayed_root_empty(fs_info);
  3893. btrfs_destroy_pinned_extent(fs_info, fs_info->pinned_extents);
  3894. btrfs_destroy_all_delalloc_inodes(fs_info);
  3895. mutex_unlock(&fs_info->transaction_kthread_mutex);
  3896. return 0;
  3897. }
  3898. static const struct extent_io_ops btree_extent_io_ops = {
  3899. /* mandatory callbacks */
  3900. .submit_bio_hook = btree_submit_bio_hook,
  3901. .readpage_end_io_hook = btree_readpage_end_io_hook,
  3902. .readpage_io_failed_hook = btree_io_failed_hook,
  3903. .set_range_writeback = btrfs_set_range_writeback,
  3904. /* optional callbacks */
  3905. };