disk-io.c 141 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987
  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/workqueue.h>
  10. #include <linux/kthread.h>
  11. #include <linux/slab.h>
  12. #include <linux/migrate.h>
  13. #include <linux/ratelimit.h>
  14. #include <linux/uuid.h>
  15. #include <linux/semaphore.h>
  16. #include <linux/error-injection.h>
  17. #include <linux/crc32c.h>
  18. #include <linux/sched/mm.h>
  19. #include <linux/unaligned.h>
  20. #include <crypto/hash.h>
  21. #include "ctree.h"
  22. #include "disk-io.h"
  23. #include "transaction.h"
  24. #include "btrfs_inode.h"
  25. #include "bio.h"
  26. #include "print-tree.h"
  27. #include "locking.h"
  28. #include "tree-log.h"
  29. #include "free-space-cache.h"
  30. #include "free-space-tree.h"
  31. #include "dev-replace.h"
  32. #include "raid56.h"
  33. #include "sysfs.h"
  34. #include "qgroup.h"
  35. #include "compression.h"
  36. #include "tree-checker.h"
  37. #include "ref-verify.h"
  38. #include "block-group.h"
  39. #include "discard.h"
  40. #include "space-info.h"
  41. #include "zoned.h"
  42. #include "subpage.h"
  43. #include "fs.h"
  44. #include "accessors.h"
  45. #include "extent-tree.h"
  46. #include "root-tree.h"
  47. #include "defrag.h"
  48. #include "uuid-tree.h"
  49. #include "relocation.h"
  50. #include "scrub.h"
  51. #include "super.h"
  52. #define BTRFS_SUPER_FLAG_SUPP (BTRFS_HEADER_FLAG_WRITTEN |\
  53. BTRFS_HEADER_FLAG_RELOC |\
  54. BTRFS_SUPER_FLAG_ERROR |\
  55. BTRFS_SUPER_FLAG_SEEDING |\
  56. BTRFS_SUPER_FLAG_METADUMP |\
  57. BTRFS_SUPER_FLAG_METADUMP_V2)
  58. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info);
  59. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info);
  60. static void btrfs_free_csum_hash(struct btrfs_fs_info *fs_info)
  61. {
  62. if (fs_info->csum_shash)
  63. crypto_free_shash(fs_info->csum_shash);
  64. }
  65. /*
  66. * Compute the csum of a btree block and store the result to provided buffer.
  67. */
  68. static void csum_tree_block(struct extent_buffer *buf, u8 *result)
  69. {
  70. struct btrfs_fs_info *fs_info = buf->fs_info;
  71. int num_pages;
  72. u32 first_page_part;
  73. SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
  74. char *kaddr;
  75. int i;
  76. shash->tfm = fs_info->csum_shash;
  77. crypto_shash_init(shash);
  78. if (buf->addr) {
  79. /* Pages are contiguous, handle them as a big one. */
  80. kaddr = buf->addr;
  81. first_page_part = fs_info->nodesize;
  82. num_pages = 1;
  83. } else {
  84. kaddr = folio_address(buf->folios[0]);
  85. first_page_part = min_t(u32, PAGE_SIZE, fs_info->nodesize);
  86. num_pages = num_extent_pages(buf);
  87. }
  88. crypto_shash_update(shash, kaddr + BTRFS_CSUM_SIZE,
  89. first_page_part - BTRFS_CSUM_SIZE);
  90. /*
  91. * Multiple single-page folios case would reach here.
  92. *
  93. * nodesize <= PAGE_SIZE and large folio all handled by above
  94. * crypto_shash_update() already.
  95. */
  96. for (i = 1; i < num_pages && INLINE_EXTENT_BUFFER_PAGES > 1; i++) {
  97. kaddr = folio_address(buf->folios[i]);
  98. crypto_shash_update(shash, kaddr, PAGE_SIZE);
  99. }
  100. memset(result, 0, BTRFS_CSUM_SIZE);
  101. crypto_shash_final(shash, result);
  102. }
  103. /*
  104. * we can't consider a given block up to date unless the transid of the
  105. * block matches the transid in the parent node's pointer. This is how we
  106. * detect blocks that either didn't get written at all or got written
  107. * in the wrong place.
  108. */
  109. int btrfs_buffer_uptodate(struct extent_buffer *eb, u64 parent_transid, int atomic)
  110. {
  111. if (!extent_buffer_uptodate(eb))
  112. return 0;
  113. if (!parent_transid || btrfs_header_generation(eb) == parent_transid)
  114. return 1;
  115. if (atomic)
  116. return -EAGAIN;
  117. if (!extent_buffer_uptodate(eb) ||
  118. btrfs_header_generation(eb) != parent_transid) {
  119. btrfs_err_rl(eb->fs_info,
  120. "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
  121. eb->start, eb->read_mirror,
  122. parent_transid, btrfs_header_generation(eb));
  123. clear_extent_buffer_uptodate(eb);
  124. return 0;
  125. }
  126. return 1;
  127. }
  128. static bool btrfs_supported_super_csum(u16 csum_type)
  129. {
  130. switch (csum_type) {
  131. case BTRFS_CSUM_TYPE_CRC32:
  132. case BTRFS_CSUM_TYPE_XXHASH:
  133. case BTRFS_CSUM_TYPE_SHA256:
  134. case BTRFS_CSUM_TYPE_BLAKE2:
  135. return true;
  136. default:
  137. return false;
  138. }
  139. }
  140. /*
  141. * Return 0 if the superblock checksum type matches the checksum value of that
  142. * algorithm. Pass the raw disk superblock data.
  143. */
  144. int btrfs_check_super_csum(struct btrfs_fs_info *fs_info,
  145. const struct btrfs_super_block *disk_sb)
  146. {
  147. char result[BTRFS_CSUM_SIZE];
  148. SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
  149. shash->tfm = fs_info->csum_shash;
  150. /*
  151. * The super_block structure does not span the whole
  152. * BTRFS_SUPER_INFO_SIZE range, we expect that the unused space is
  153. * filled with zeros and is included in the checksum.
  154. */
  155. crypto_shash_digest(shash, (const u8 *)disk_sb + BTRFS_CSUM_SIZE,
  156. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE, result);
  157. if (memcmp(disk_sb->csum, result, fs_info->csum_size))
  158. return 1;
  159. return 0;
  160. }
  161. static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,
  162. int mirror_num)
  163. {
  164. struct btrfs_fs_info *fs_info = eb->fs_info;
  165. int num_folios = num_extent_folios(eb);
  166. int ret = 0;
  167. if (sb_rdonly(fs_info->sb))
  168. return -EROFS;
  169. for (int i = 0; i < num_folios; i++) {
  170. struct folio *folio = eb->folios[i];
  171. u64 start = max_t(u64, eb->start, folio_pos(folio));
  172. u64 end = min_t(u64, eb->start + eb->len,
  173. folio_pos(folio) + eb->folio_size);
  174. u32 len = end - start;
  175. ret = btrfs_repair_io_failure(fs_info, 0, start, len,
  176. start, folio, offset_in_folio(folio, start),
  177. mirror_num);
  178. if (ret)
  179. break;
  180. }
  181. return ret;
  182. }
  183. /*
  184. * helper to read a given tree block, doing retries as required when
  185. * the checksums don't match and we have alternate mirrors to try.
  186. *
  187. * @check: expected tree parentness check, see the comments of the
  188. * structure for details.
  189. */
  190. int btrfs_read_extent_buffer(struct extent_buffer *eb,
  191. const struct btrfs_tree_parent_check *check)
  192. {
  193. struct btrfs_fs_info *fs_info = eb->fs_info;
  194. int failed = 0;
  195. int ret;
  196. int num_copies = 0;
  197. int mirror_num = 0;
  198. int failed_mirror = 0;
  199. ASSERT(check);
  200. while (1) {
  201. clear_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  202. ret = read_extent_buffer_pages(eb, WAIT_COMPLETE, mirror_num, check);
  203. if (!ret)
  204. break;
  205. num_copies = btrfs_num_copies(fs_info,
  206. eb->start, eb->len);
  207. if (num_copies == 1)
  208. break;
  209. if (!failed_mirror) {
  210. failed = 1;
  211. failed_mirror = eb->read_mirror;
  212. }
  213. mirror_num++;
  214. if (mirror_num == failed_mirror)
  215. mirror_num++;
  216. if (mirror_num > num_copies)
  217. break;
  218. }
  219. if (failed && !ret && failed_mirror)
  220. btrfs_repair_eb_io_failure(eb, failed_mirror);
  221. return ret;
  222. }
  223. /*
  224. * Checksum a dirty tree block before IO.
  225. */
  226. blk_status_t btree_csum_one_bio(struct btrfs_bio *bbio)
  227. {
  228. struct extent_buffer *eb = bbio->private;
  229. struct btrfs_fs_info *fs_info = eb->fs_info;
  230. u64 found_start = btrfs_header_bytenr(eb);
  231. u64 last_trans;
  232. u8 result[BTRFS_CSUM_SIZE];
  233. int ret;
  234. /* Btree blocks are always contiguous on disk. */
  235. if (WARN_ON_ONCE(bbio->file_offset != eb->start))
  236. return BLK_STS_IOERR;
  237. if (WARN_ON_ONCE(bbio->bio.bi_iter.bi_size != eb->len))
  238. return BLK_STS_IOERR;
  239. /*
  240. * If an extent_buffer is marked as EXTENT_BUFFER_ZONED_ZEROOUT, don't
  241. * checksum it but zero-out its content. This is done to preserve
  242. * ordering of I/O without unnecessarily writing out data.
  243. */
  244. if (test_bit(EXTENT_BUFFER_ZONED_ZEROOUT, &eb->bflags)) {
  245. memzero_extent_buffer(eb, 0, eb->len);
  246. return BLK_STS_OK;
  247. }
  248. if (WARN_ON_ONCE(found_start != eb->start))
  249. return BLK_STS_IOERR;
  250. if (WARN_ON(!btrfs_folio_test_uptodate(fs_info, eb->folios[0],
  251. eb->start, eb->len)))
  252. return BLK_STS_IOERR;
  253. ASSERT(memcmp_extent_buffer(eb, fs_info->fs_devices->metadata_uuid,
  254. offsetof(struct btrfs_header, fsid),
  255. BTRFS_FSID_SIZE) == 0);
  256. csum_tree_block(eb, result);
  257. if (btrfs_header_level(eb))
  258. ret = btrfs_check_node(eb);
  259. else
  260. ret = btrfs_check_leaf(eb);
  261. if (ret < 0)
  262. goto error;
  263. /*
  264. * Also check the generation, the eb reached here must be newer than
  265. * last committed. Or something seriously wrong happened.
  266. */
  267. last_trans = btrfs_get_last_trans_committed(fs_info);
  268. if (unlikely(btrfs_header_generation(eb) <= last_trans)) {
  269. ret = -EUCLEAN;
  270. btrfs_err(fs_info,
  271. "block=%llu bad generation, have %llu expect > %llu",
  272. eb->start, btrfs_header_generation(eb), last_trans);
  273. goto error;
  274. }
  275. write_extent_buffer(eb, result, 0, fs_info->csum_size);
  276. return BLK_STS_OK;
  277. error:
  278. btrfs_print_tree(eb, 0);
  279. btrfs_err(fs_info, "block=%llu write time tree block corruption detected",
  280. eb->start);
  281. /*
  282. * Be noisy if this is an extent buffer from a log tree. We don't abort
  283. * a transaction in case there's a bad log tree extent buffer, we just
  284. * fallback to a transaction commit. Still we want to know when there is
  285. * a bad log tree extent buffer, as that may signal a bug somewhere.
  286. */
  287. WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG) ||
  288. btrfs_header_owner(eb) == BTRFS_TREE_LOG_OBJECTID);
  289. return errno_to_blk_status(ret);
  290. }
  291. static bool check_tree_block_fsid(struct extent_buffer *eb)
  292. {
  293. struct btrfs_fs_info *fs_info = eb->fs_info;
  294. struct btrfs_fs_devices *fs_devices = fs_info->fs_devices, *seed_devs;
  295. u8 fsid[BTRFS_FSID_SIZE];
  296. read_extent_buffer(eb, fsid, offsetof(struct btrfs_header, fsid),
  297. BTRFS_FSID_SIZE);
  298. /*
  299. * alloc_fsid_devices() copies the fsid into fs_devices::metadata_uuid.
  300. * This is then overwritten by metadata_uuid if it is present in the
  301. * device_list_add(). The same true for a seed device as well. So use of
  302. * fs_devices::metadata_uuid is appropriate here.
  303. */
  304. if (memcmp(fsid, fs_info->fs_devices->metadata_uuid, BTRFS_FSID_SIZE) == 0)
  305. return false;
  306. list_for_each_entry(seed_devs, &fs_devices->seed_list, seed_list)
  307. if (!memcmp(fsid, seed_devs->fsid, BTRFS_FSID_SIZE))
  308. return false;
  309. return true;
  310. }
  311. /* Do basic extent buffer checks at read time */
  312. int btrfs_validate_extent_buffer(struct extent_buffer *eb,
  313. const struct btrfs_tree_parent_check *check)
  314. {
  315. struct btrfs_fs_info *fs_info = eb->fs_info;
  316. u64 found_start;
  317. const u32 csum_size = fs_info->csum_size;
  318. u8 found_level;
  319. u8 result[BTRFS_CSUM_SIZE];
  320. const u8 *header_csum;
  321. int ret = 0;
  322. const bool ignore_csum = btrfs_test_opt(fs_info, IGNOREMETACSUMS);
  323. ASSERT(check);
  324. found_start = btrfs_header_bytenr(eb);
  325. if (found_start != eb->start) {
  326. btrfs_err_rl(fs_info,
  327. "bad tree block start, mirror %u want %llu have %llu",
  328. eb->read_mirror, eb->start, found_start);
  329. ret = -EIO;
  330. goto out;
  331. }
  332. if (check_tree_block_fsid(eb)) {
  333. btrfs_err_rl(fs_info, "bad fsid on logical %llu mirror %u",
  334. eb->start, eb->read_mirror);
  335. ret = -EIO;
  336. goto out;
  337. }
  338. found_level = btrfs_header_level(eb);
  339. if (found_level >= BTRFS_MAX_LEVEL) {
  340. btrfs_err(fs_info,
  341. "bad tree block level, mirror %u level %d on logical %llu",
  342. eb->read_mirror, btrfs_header_level(eb), eb->start);
  343. ret = -EIO;
  344. goto out;
  345. }
  346. csum_tree_block(eb, result);
  347. header_csum = folio_address(eb->folios[0]) +
  348. get_eb_offset_in_folio(eb, offsetof(struct btrfs_header, csum));
  349. if (memcmp(result, header_csum, csum_size) != 0) {
  350. btrfs_warn_rl(fs_info,
  351. "checksum verify failed on logical %llu mirror %u wanted " CSUM_FMT " found " CSUM_FMT " level %d%s",
  352. eb->start, eb->read_mirror,
  353. CSUM_FMT_VALUE(csum_size, header_csum),
  354. CSUM_FMT_VALUE(csum_size, result),
  355. btrfs_header_level(eb),
  356. ignore_csum ? ", ignored" : "");
  357. if (!ignore_csum) {
  358. ret = -EUCLEAN;
  359. goto out;
  360. }
  361. }
  362. if (found_level != check->level) {
  363. btrfs_err(fs_info,
  364. "level verify failed on logical %llu mirror %u wanted %u found %u",
  365. eb->start, eb->read_mirror, check->level, found_level);
  366. ret = -EIO;
  367. goto out;
  368. }
  369. if (unlikely(check->transid &&
  370. btrfs_header_generation(eb) != check->transid)) {
  371. btrfs_err_rl(eb->fs_info,
  372. "parent transid verify failed on logical %llu mirror %u wanted %llu found %llu",
  373. eb->start, eb->read_mirror, check->transid,
  374. btrfs_header_generation(eb));
  375. ret = -EIO;
  376. goto out;
  377. }
  378. if (check->has_first_key) {
  379. const struct btrfs_key *expect_key = &check->first_key;
  380. struct btrfs_key found_key;
  381. if (found_level)
  382. btrfs_node_key_to_cpu(eb, &found_key, 0);
  383. else
  384. btrfs_item_key_to_cpu(eb, &found_key, 0);
  385. if (unlikely(btrfs_comp_cpu_keys(expect_key, &found_key))) {
  386. btrfs_err(fs_info,
  387. "tree first key mismatch detected, bytenr=%llu parent_transid=%llu key expected=(%llu,%u,%llu) has=(%llu,%u,%llu)",
  388. eb->start, check->transid,
  389. expect_key->objectid,
  390. expect_key->type, expect_key->offset,
  391. found_key.objectid, found_key.type,
  392. found_key.offset);
  393. ret = -EUCLEAN;
  394. goto out;
  395. }
  396. }
  397. if (check->owner_root) {
  398. ret = btrfs_check_eb_owner(eb, check->owner_root);
  399. if (ret < 0)
  400. goto out;
  401. }
  402. /*
  403. * If this is a leaf block and it is corrupt, set the corrupt bit so
  404. * that we don't try and read the other copies of this block, just
  405. * return -EIO.
  406. */
  407. if (found_level == 0 && btrfs_check_leaf(eb)) {
  408. set_bit(EXTENT_BUFFER_CORRUPT, &eb->bflags);
  409. ret = -EIO;
  410. }
  411. if (found_level > 0 && btrfs_check_node(eb))
  412. ret = -EIO;
  413. if (ret)
  414. btrfs_err(fs_info,
  415. "read time tree block corruption detected on logical %llu mirror %u",
  416. eb->start, eb->read_mirror);
  417. out:
  418. return ret;
  419. }
  420. #ifdef CONFIG_MIGRATION
  421. static int btree_migrate_folio(struct address_space *mapping,
  422. struct folio *dst, struct folio *src, enum migrate_mode mode)
  423. {
  424. /*
  425. * we can't safely write a btree page from here,
  426. * we haven't done the locking hook
  427. */
  428. if (folio_test_dirty(src))
  429. return -EAGAIN;
  430. /*
  431. * Buffers may be managed in a filesystem specific way.
  432. * We must have no buffers or drop them.
  433. */
  434. if (folio_get_private(src) &&
  435. !filemap_release_folio(src, GFP_KERNEL))
  436. return -EAGAIN;
  437. return migrate_folio(mapping, dst, src, mode);
  438. }
  439. #else
  440. #define btree_migrate_folio NULL
  441. #endif
  442. static int btree_writepages(struct address_space *mapping,
  443. struct writeback_control *wbc)
  444. {
  445. int ret;
  446. if (wbc->sync_mode == WB_SYNC_NONE) {
  447. struct btrfs_fs_info *fs_info;
  448. if (wbc->for_kupdate)
  449. return 0;
  450. fs_info = inode_to_fs_info(mapping->host);
  451. /* this is a bit racy, but that's ok */
  452. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  453. BTRFS_DIRTY_METADATA_THRESH,
  454. fs_info->dirty_metadata_batch);
  455. if (ret < 0)
  456. return 0;
  457. }
  458. return btree_write_cache_pages(mapping, wbc);
  459. }
  460. static bool btree_release_folio(struct folio *folio, gfp_t gfp_flags)
  461. {
  462. if (folio_test_writeback(folio) || folio_test_dirty(folio))
  463. return false;
  464. return try_release_extent_buffer(folio);
  465. }
  466. static void btree_invalidate_folio(struct folio *folio, size_t offset,
  467. size_t length)
  468. {
  469. struct extent_io_tree *tree;
  470. tree = &folio_to_inode(folio)->io_tree;
  471. extent_invalidate_folio(tree, folio, offset);
  472. btree_release_folio(folio, GFP_NOFS);
  473. if (folio_get_private(folio)) {
  474. btrfs_warn(folio_to_fs_info(folio),
  475. "folio private not zero on folio %llu",
  476. (unsigned long long)folio_pos(folio));
  477. folio_detach_private(folio);
  478. }
  479. }
  480. #ifdef DEBUG
  481. static bool btree_dirty_folio(struct address_space *mapping,
  482. struct folio *folio)
  483. {
  484. struct btrfs_fs_info *fs_info = inode_to_fs_info(mapping->host);
  485. struct btrfs_subpage_info *spi = fs_info->subpage_info;
  486. struct btrfs_subpage *subpage;
  487. struct extent_buffer *eb;
  488. int cur_bit = 0;
  489. u64 page_start = folio_pos(folio);
  490. if (fs_info->sectorsize == PAGE_SIZE) {
  491. eb = folio_get_private(folio);
  492. BUG_ON(!eb);
  493. BUG_ON(!test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  494. BUG_ON(!atomic_read(&eb->refs));
  495. btrfs_assert_tree_write_locked(eb);
  496. return filemap_dirty_folio(mapping, folio);
  497. }
  498. ASSERT(spi);
  499. subpage = folio_get_private(folio);
  500. for (cur_bit = spi->dirty_offset;
  501. cur_bit < spi->dirty_offset + spi->bitmap_nr_bits;
  502. cur_bit++) {
  503. unsigned long flags;
  504. u64 cur;
  505. spin_lock_irqsave(&subpage->lock, flags);
  506. if (!test_bit(cur_bit, subpage->bitmaps)) {
  507. spin_unlock_irqrestore(&subpage->lock, flags);
  508. continue;
  509. }
  510. spin_unlock_irqrestore(&subpage->lock, flags);
  511. cur = page_start + cur_bit * fs_info->sectorsize;
  512. eb = find_extent_buffer(fs_info, cur);
  513. ASSERT(eb);
  514. ASSERT(test_bit(EXTENT_BUFFER_DIRTY, &eb->bflags));
  515. ASSERT(atomic_read(&eb->refs));
  516. btrfs_assert_tree_write_locked(eb);
  517. free_extent_buffer(eb);
  518. cur_bit += (fs_info->nodesize >> fs_info->sectorsize_bits) - 1;
  519. }
  520. return filemap_dirty_folio(mapping, folio);
  521. }
  522. #else
  523. #define btree_dirty_folio filemap_dirty_folio
  524. #endif
  525. static const struct address_space_operations btree_aops = {
  526. .writepages = btree_writepages,
  527. .release_folio = btree_release_folio,
  528. .invalidate_folio = btree_invalidate_folio,
  529. .migrate_folio = btree_migrate_folio,
  530. .dirty_folio = btree_dirty_folio,
  531. };
  532. struct extent_buffer *btrfs_find_create_tree_block(
  533. struct btrfs_fs_info *fs_info,
  534. u64 bytenr, u64 owner_root,
  535. int level)
  536. {
  537. if (btrfs_is_testing(fs_info))
  538. return alloc_test_extent_buffer(fs_info, bytenr);
  539. return alloc_extent_buffer(fs_info, bytenr, owner_root, level);
  540. }
  541. /*
  542. * Read tree block at logical address @bytenr and do variant basic but critical
  543. * verification.
  544. *
  545. * @check: expected tree parentness check, see comments of the
  546. * structure for details.
  547. */
  548. struct extent_buffer *read_tree_block(struct btrfs_fs_info *fs_info, u64 bytenr,
  549. struct btrfs_tree_parent_check *check)
  550. {
  551. struct extent_buffer *buf = NULL;
  552. int ret;
  553. ASSERT(check);
  554. buf = btrfs_find_create_tree_block(fs_info, bytenr, check->owner_root,
  555. check->level);
  556. if (IS_ERR(buf))
  557. return buf;
  558. ret = btrfs_read_extent_buffer(buf, check);
  559. if (ret) {
  560. free_extent_buffer_stale(buf);
  561. return ERR_PTR(ret);
  562. }
  563. return buf;
  564. }
  565. static void __setup_root(struct btrfs_root *root, struct btrfs_fs_info *fs_info,
  566. u64 objectid)
  567. {
  568. bool dummy = btrfs_is_testing(fs_info);
  569. memset(&root->root_key, 0, sizeof(root->root_key));
  570. memset(&root->root_item, 0, sizeof(root->root_item));
  571. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  572. root->fs_info = fs_info;
  573. root->root_key.objectid = objectid;
  574. root->node = NULL;
  575. root->commit_root = NULL;
  576. root->state = 0;
  577. RB_CLEAR_NODE(&root->rb_node);
  578. btrfs_set_root_last_trans(root, 0);
  579. root->free_objectid = 0;
  580. root->nr_delalloc_inodes = 0;
  581. root->nr_ordered_extents = 0;
  582. xa_init(&root->inodes);
  583. xa_init(&root->delayed_nodes);
  584. btrfs_init_root_block_rsv(root);
  585. INIT_LIST_HEAD(&root->dirty_list);
  586. INIT_LIST_HEAD(&root->root_list);
  587. INIT_LIST_HEAD(&root->delalloc_inodes);
  588. INIT_LIST_HEAD(&root->delalloc_root);
  589. INIT_LIST_HEAD(&root->ordered_extents);
  590. INIT_LIST_HEAD(&root->ordered_root);
  591. INIT_LIST_HEAD(&root->reloc_dirty_list);
  592. spin_lock_init(&root->delalloc_lock);
  593. spin_lock_init(&root->ordered_extent_lock);
  594. spin_lock_init(&root->accounting_lock);
  595. spin_lock_init(&root->qgroup_meta_rsv_lock);
  596. mutex_init(&root->objectid_mutex);
  597. mutex_init(&root->log_mutex);
  598. mutex_init(&root->ordered_extent_mutex);
  599. mutex_init(&root->delalloc_mutex);
  600. init_waitqueue_head(&root->qgroup_flush_wait);
  601. init_waitqueue_head(&root->log_writer_wait);
  602. init_waitqueue_head(&root->log_commit_wait[0]);
  603. init_waitqueue_head(&root->log_commit_wait[1]);
  604. INIT_LIST_HEAD(&root->log_ctxs[0]);
  605. INIT_LIST_HEAD(&root->log_ctxs[1]);
  606. atomic_set(&root->log_commit[0], 0);
  607. atomic_set(&root->log_commit[1], 0);
  608. atomic_set(&root->log_writers, 0);
  609. atomic_set(&root->log_batch, 0);
  610. refcount_set(&root->refs, 1);
  611. atomic_set(&root->snapshot_force_cow, 0);
  612. atomic_set(&root->nr_swapfiles, 0);
  613. btrfs_set_root_log_transid(root, 0);
  614. root->log_transid_committed = -1;
  615. btrfs_set_root_last_log_commit(root, 0);
  616. root->anon_dev = 0;
  617. if (!dummy) {
  618. extent_io_tree_init(fs_info, &root->dirty_log_pages,
  619. IO_TREE_ROOT_DIRTY_LOG_PAGES);
  620. extent_io_tree_init(fs_info, &root->log_csum_range,
  621. IO_TREE_LOG_CSUM_RANGE);
  622. }
  623. spin_lock_init(&root->root_item_lock);
  624. btrfs_qgroup_init_swapped_blocks(&root->swapped_blocks);
  625. #ifdef CONFIG_BTRFS_DEBUG
  626. INIT_LIST_HEAD(&root->leak_list);
  627. spin_lock(&fs_info->fs_roots_radix_lock);
  628. list_add_tail(&root->leak_list, &fs_info->allocated_roots);
  629. spin_unlock(&fs_info->fs_roots_radix_lock);
  630. #endif
  631. }
  632. static struct btrfs_root *btrfs_alloc_root(struct btrfs_fs_info *fs_info,
  633. u64 objectid, gfp_t flags)
  634. {
  635. struct btrfs_root *root = kzalloc(sizeof(*root), flags);
  636. if (root)
  637. __setup_root(root, fs_info, objectid);
  638. return root;
  639. }
  640. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  641. /* Should only be used by the testing infrastructure */
  642. struct btrfs_root *btrfs_alloc_dummy_root(struct btrfs_fs_info *fs_info)
  643. {
  644. struct btrfs_root *root;
  645. if (!fs_info)
  646. return ERR_PTR(-EINVAL);
  647. root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID, GFP_KERNEL);
  648. if (!root)
  649. return ERR_PTR(-ENOMEM);
  650. /* We don't use the stripesize in selftest, set it as sectorsize */
  651. root->alloc_bytenr = 0;
  652. return root;
  653. }
  654. #endif
  655. static int global_root_cmp(struct rb_node *a_node, const struct rb_node *b_node)
  656. {
  657. const struct btrfs_root *a = rb_entry(a_node, struct btrfs_root, rb_node);
  658. const struct btrfs_root *b = rb_entry(b_node, struct btrfs_root, rb_node);
  659. return btrfs_comp_cpu_keys(&a->root_key, &b->root_key);
  660. }
  661. static int global_root_key_cmp(const void *k, const struct rb_node *node)
  662. {
  663. const struct btrfs_key *key = k;
  664. const struct btrfs_root *root = rb_entry(node, struct btrfs_root, rb_node);
  665. return btrfs_comp_cpu_keys(key, &root->root_key);
  666. }
  667. int btrfs_global_root_insert(struct btrfs_root *root)
  668. {
  669. struct btrfs_fs_info *fs_info = root->fs_info;
  670. struct rb_node *tmp;
  671. int ret = 0;
  672. write_lock(&fs_info->global_root_lock);
  673. tmp = rb_find_add(&root->rb_node, &fs_info->global_root_tree, global_root_cmp);
  674. write_unlock(&fs_info->global_root_lock);
  675. if (tmp) {
  676. ret = -EEXIST;
  677. btrfs_warn(fs_info, "global root %llu %llu already exists",
  678. btrfs_root_id(root), root->root_key.offset);
  679. }
  680. return ret;
  681. }
  682. void btrfs_global_root_delete(struct btrfs_root *root)
  683. {
  684. struct btrfs_fs_info *fs_info = root->fs_info;
  685. write_lock(&fs_info->global_root_lock);
  686. rb_erase(&root->rb_node, &fs_info->global_root_tree);
  687. write_unlock(&fs_info->global_root_lock);
  688. }
  689. struct btrfs_root *btrfs_global_root(struct btrfs_fs_info *fs_info,
  690. struct btrfs_key *key)
  691. {
  692. struct rb_node *node;
  693. struct btrfs_root *root = NULL;
  694. read_lock(&fs_info->global_root_lock);
  695. node = rb_find(key, &fs_info->global_root_tree, global_root_key_cmp);
  696. if (node)
  697. root = container_of(node, struct btrfs_root, rb_node);
  698. read_unlock(&fs_info->global_root_lock);
  699. return root;
  700. }
  701. static u64 btrfs_global_root_id(struct btrfs_fs_info *fs_info, u64 bytenr)
  702. {
  703. struct btrfs_block_group *block_group;
  704. u64 ret;
  705. if (!btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
  706. return 0;
  707. if (bytenr)
  708. block_group = btrfs_lookup_block_group(fs_info, bytenr);
  709. else
  710. block_group = btrfs_lookup_first_block_group(fs_info, bytenr);
  711. ASSERT(block_group);
  712. if (!block_group)
  713. return 0;
  714. ret = block_group->global_root_id;
  715. btrfs_put_block_group(block_group);
  716. return ret;
  717. }
  718. struct btrfs_root *btrfs_csum_root(struct btrfs_fs_info *fs_info, u64 bytenr)
  719. {
  720. struct btrfs_key key = {
  721. .objectid = BTRFS_CSUM_TREE_OBJECTID,
  722. .type = BTRFS_ROOT_ITEM_KEY,
  723. .offset = btrfs_global_root_id(fs_info, bytenr),
  724. };
  725. return btrfs_global_root(fs_info, &key);
  726. }
  727. struct btrfs_root *btrfs_extent_root(struct btrfs_fs_info *fs_info, u64 bytenr)
  728. {
  729. struct btrfs_key key = {
  730. .objectid = BTRFS_EXTENT_TREE_OBJECTID,
  731. .type = BTRFS_ROOT_ITEM_KEY,
  732. .offset = btrfs_global_root_id(fs_info, bytenr),
  733. };
  734. return btrfs_global_root(fs_info, &key);
  735. }
  736. struct btrfs_root *btrfs_create_tree(struct btrfs_trans_handle *trans,
  737. u64 objectid)
  738. {
  739. struct btrfs_fs_info *fs_info = trans->fs_info;
  740. struct extent_buffer *leaf;
  741. struct btrfs_root *tree_root = fs_info->tree_root;
  742. struct btrfs_root *root;
  743. struct btrfs_key key;
  744. unsigned int nofs_flag;
  745. int ret = 0;
  746. /*
  747. * We're holding a transaction handle, so use a NOFS memory allocation
  748. * context to avoid deadlock if reclaim happens.
  749. */
  750. nofs_flag = memalloc_nofs_save();
  751. root = btrfs_alloc_root(fs_info, objectid, GFP_KERNEL);
  752. memalloc_nofs_restore(nofs_flag);
  753. if (!root)
  754. return ERR_PTR(-ENOMEM);
  755. root->root_key.objectid = objectid;
  756. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  757. root->root_key.offset = 0;
  758. leaf = btrfs_alloc_tree_block(trans, root, 0, objectid, NULL, 0, 0, 0,
  759. 0, BTRFS_NESTING_NORMAL);
  760. if (IS_ERR(leaf)) {
  761. ret = PTR_ERR(leaf);
  762. leaf = NULL;
  763. goto fail;
  764. }
  765. root->node = leaf;
  766. btrfs_mark_buffer_dirty(trans, leaf);
  767. root->commit_root = btrfs_root_node(root);
  768. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  769. btrfs_set_root_flags(&root->root_item, 0);
  770. btrfs_set_root_limit(&root->root_item, 0);
  771. btrfs_set_root_bytenr(&root->root_item, leaf->start);
  772. btrfs_set_root_generation(&root->root_item, trans->transid);
  773. btrfs_set_root_level(&root->root_item, 0);
  774. btrfs_set_root_refs(&root->root_item, 1);
  775. btrfs_set_root_used(&root->root_item, leaf->len);
  776. btrfs_set_root_last_snapshot(&root->root_item, 0);
  777. btrfs_set_root_dirid(&root->root_item, 0);
  778. if (is_fstree(objectid))
  779. generate_random_guid(root->root_item.uuid);
  780. else
  781. export_guid(root->root_item.uuid, &guid_null);
  782. btrfs_set_root_drop_level(&root->root_item, 0);
  783. btrfs_tree_unlock(leaf);
  784. key.objectid = objectid;
  785. key.type = BTRFS_ROOT_ITEM_KEY;
  786. key.offset = 0;
  787. ret = btrfs_insert_root(trans, tree_root, &key, &root->root_item);
  788. if (ret)
  789. goto fail;
  790. return root;
  791. fail:
  792. btrfs_put_root(root);
  793. return ERR_PTR(ret);
  794. }
  795. static struct btrfs_root *alloc_log_tree(struct btrfs_trans_handle *trans,
  796. struct btrfs_fs_info *fs_info)
  797. {
  798. struct btrfs_root *root;
  799. root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID, GFP_NOFS);
  800. if (!root)
  801. return ERR_PTR(-ENOMEM);
  802. root->root_key.objectid = BTRFS_TREE_LOG_OBJECTID;
  803. root->root_key.type = BTRFS_ROOT_ITEM_KEY;
  804. root->root_key.offset = BTRFS_TREE_LOG_OBJECTID;
  805. return root;
  806. }
  807. int btrfs_alloc_log_tree_node(struct btrfs_trans_handle *trans,
  808. struct btrfs_root *root)
  809. {
  810. struct extent_buffer *leaf;
  811. /*
  812. * DON'T set SHAREABLE bit for log trees.
  813. *
  814. * Log trees are not exposed to user space thus can't be snapshotted,
  815. * and they go away before a real commit is actually done.
  816. *
  817. * They do store pointers to file data extents, and those reference
  818. * counts still get updated (along with back refs to the log tree).
  819. */
  820. leaf = btrfs_alloc_tree_block(trans, root, 0, BTRFS_TREE_LOG_OBJECTID,
  821. NULL, 0, 0, 0, 0, BTRFS_NESTING_NORMAL);
  822. if (IS_ERR(leaf))
  823. return PTR_ERR(leaf);
  824. root->node = leaf;
  825. btrfs_mark_buffer_dirty(trans, root->node);
  826. btrfs_tree_unlock(root->node);
  827. return 0;
  828. }
  829. int btrfs_init_log_root_tree(struct btrfs_trans_handle *trans,
  830. struct btrfs_fs_info *fs_info)
  831. {
  832. struct btrfs_root *log_root;
  833. log_root = alloc_log_tree(trans, fs_info);
  834. if (IS_ERR(log_root))
  835. return PTR_ERR(log_root);
  836. if (!btrfs_is_zoned(fs_info)) {
  837. int ret = btrfs_alloc_log_tree_node(trans, log_root);
  838. if (ret) {
  839. btrfs_put_root(log_root);
  840. return ret;
  841. }
  842. }
  843. WARN_ON(fs_info->log_root_tree);
  844. fs_info->log_root_tree = log_root;
  845. return 0;
  846. }
  847. int btrfs_add_log_tree(struct btrfs_trans_handle *trans,
  848. struct btrfs_root *root)
  849. {
  850. struct btrfs_fs_info *fs_info = root->fs_info;
  851. struct btrfs_root *log_root;
  852. struct btrfs_inode_item *inode_item;
  853. int ret;
  854. log_root = alloc_log_tree(trans, fs_info);
  855. if (IS_ERR(log_root))
  856. return PTR_ERR(log_root);
  857. ret = btrfs_alloc_log_tree_node(trans, log_root);
  858. if (ret) {
  859. btrfs_put_root(log_root);
  860. return ret;
  861. }
  862. btrfs_set_root_last_trans(log_root, trans->transid);
  863. log_root->root_key.offset = btrfs_root_id(root);
  864. inode_item = &log_root->root_item.inode;
  865. btrfs_set_stack_inode_generation(inode_item, 1);
  866. btrfs_set_stack_inode_size(inode_item, 3);
  867. btrfs_set_stack_inode_nlink(inode_item, 1);
  868. btrfs_set_stack_inode_nbytes(inode_item,
  869. fs_info->nodesize);
  870. btrfs_set_stack_inode_mode(inode_item, S_IFDIR | 0755);
  871. btrfs_set_root_node(&log_root->root_item, log_root->node);
  872. WARN_ON(root->log_root);
  873. root->log_root = log_root;
  874. btrfs_set_root_log_transid(root, 0);
  875. root->log_transid_committed = -1;
  876. btrfs_set_root_last_log_commit(root, 0);
  877. return 0;
  878. }
  879. static struct btrfs_root *read_tree_root_path(struct btrfs_root *tree_root,
  880. struct btrfs_path *path,
  881. const struct btrfs_key *key)
  882. {
  883. struct btrfs_root *root;
  884. struct btrfs_tree_parent_check check = { 0 };
  885. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  886. u64 generation;
  887. int ret;
  888. int level;
  889. root = btrfs_alloc_root(fs_info, key->objectid, GFP_NOFS);
  890. if (!root)
  891. return ERR_PTR(-ENOMEM);
  892. ret = btrfs_find_root(tree_root, key, path,
  893. &root->root_item, &root->root_key);
  894. if (ret) {
  895. if (ret > 0)
  896. ret = -ENOENT;
  897. goto fail;
  898. }
  899. generation = btrfs_root_generation(&root->root_item);
  900. level = btrfs_root_level(&root->root_item);
  901. check.level = level;
  902. check.transid = generation;
  903. check.owner_root = key->objectid;
  904. root->node = read_tree_block(fs_info, btrfs_root_bytenr(&root->root_item),
  905. &check);
  906. if (IS_ERR(root->node)) {
  907. ret = PTR_ERR(root->node);
  908. root->node = NULL;
  909. goto fail;
  910. }
  911. if (!btrfs_buffer_uptodate(root->node, generation, 0)) {
  912. ret = -EIO;
  913. goto fail;
  914. }
  915. /*
  916. * For real fs, and not log/reloc trees, root owner must
  917. * match its root node owner
  918. */
  919. if (!btrfs_is_testing(fs_info) &&
  920. btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
  921. btrfs_root_id(root) != BTRFS_TREE_RELOC_OBJECTID &&
  922. btrfs_root_id(root) != btrfs_header_owner(root->node)) {
  923. btrfs_crit(fs_info,
  924. "root=%llu block=%llu, tree root owner mismatch, have %llu expect %llu",
  925. btrfs_root_id(root), root->node->start,
  926. btrfs_header_owner(root->node),
  927. btrfs_root_id(root));
  928. ret = -EUCLEAN;
  929. goto fail;
  930. }
  931. root->commit_root = btrfs_root_node(root);
  932. return root;
  933. fail:
  934. btrfs_put_root(root);
  935. return ERR_PTR(ret);
  936. }
  937. struct btrfs_root *btrfs_read_tree_root(struct btrfs_root *tree_root,
  938. const struct btrfs_key *key)
  939. {
  940. struct btrfs_root *root;
  941. struct btrfs_path *path;
  942. path = btrfs_alloc_path();
  943. if (!path)
  944. return ERR_PTR(-ENOMEM);
  945. root = read_tree_root_path(tree_root, path, key);
  946. btrfs_free_path(path);
  947. return root;
  948. }
  949. /*
  950. * Initialize subvolume root in-memory structure
  951. *
  952. * @anon_dev: anonymous device to attach to the root, if zero, allocate new
  953. */
  954. static int btrfs_init_fs_root(struct btrfs_root *root, dev_t anon_dev)
  955. {
  956. int ret;
  957. btrfs_drew_lock_init(&root->snapshot_lock);
  958. if (btrfs_root_id(root) != BTRFS_TREE_LOG_OBJECTID &&
  959. !btrfs_is_data_reloc_root(root) &&
  960. is_fstree(btrfs_root_id(root))) {
  961. set_bit(BTRFS_ROOT_SHAREABLE, &root->state);
  962. btrfs_check_and_init_root_item(&root->root_item);
  963. }
  964. /*
  965. * Don't assign anonymous block device to roots that are not exposed to
  966. * userspace, the id pool is limited to 1M
  967. */
  968. if (is_fstree(btrfs_root_id(root)) &&
  969. btrfs_root_refs(&root->root_item) > 0) {
  970. if (!anon_dev) {
  971. ret = get_anon_bdev(&root->anon_dev);
  972. if (ret)
  973. goto fail;
  974. } else {
  975. root->anon_dev = anon_dev;
  976. }
  977. }
  978. mutex_lock(&root->objectid_mutex);
  979. ret = btrfs_init_root_free_objectid(root);
  980. if (ret) {
  981. mutex_unlock(&root->objectid_mutex);
  982. goto fail;
  983. }
  984. ASSERT(root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
  985. mutex_unlock(&root->objectid_mutex);
  986. return 0;
  987. fail:
  988. /* The caller is responsible to call btrfs_free_fs_root */
  989. return ret;
  990. }
  991. static struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  992. u64 root_id)
  993. {
  994. struct btrfs_root *root;
  995. spin_lock(&fs_info->fs_roots_radix_lock);
  996. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  997. (unsigned long)root_id);
  998. root = btrfs_grab_root(root);
  999. spin_unlock(&fs_info->fs_roots_radix_lock);
  1000. return root;
  1001. }
  1002. static struct btrfs_root *btrfs_get_global_root(struct btrfs_fs_info *fs_info,
  1003. u64 objectid)
  1004. {
  1005. struct btrfs_key key = {
  1006. .objectid = objectid,
  1007. .type = BTRFS_ROOT_ITEM_KEY,
  1008. .offset = 0,
  1009. };
  1010. switch (objectid) {
  1011. case BTRFS_ROOT_TREE_OBJECTID:
  1012. return btrfs_grab_root(fs_info->tree_root);
  1013. case BTRFS_EXTENT_TREE_OBJECTID:
  1014. return btrfs_grab_root(btrfs_global_root(fs_info, &key));
  1015. case BTRFS_CHUNK_TREE_OBJECTID:
  1016. return btrfs_grab_root(fs_info->chunk_root);
  1017. case BTRFS_DEV_TREE_OBJECTID:
  1018. return btrfs_grab_root(fs_info->dev_root);
  1019. case BTRFS_CSUM_TREE_OBJECTID:
  1020. return btrfs_grab_root(btrfs_global_root(fs_info, &key));
  1021. case BTRFS_QUOTA_TREE_OBJECTID:
  1022. return btrfs_grab_root(fs_info->quota_root);
  1023. case BTRFS_UUID_TREE_OBJECTID:
  1024. return btrfs_grab_root(fs_info->uuid_root);
  1025. case BTRFS_BLOCK_GROUP_TREE_OBJECTID:
  1026. return btrfs_grab_root(fs_info->block_group_root);
  1027. case BTRFS_FREE_SPACE_TREE_OBJECTID:
  1028. return btrfs_grab_root(btrfs_global_root(fs_info, &key));
  1029. case BTRFS_RAID_STRIPE_TREE_OBJECTID:
  1030. return btrfs_grab_root(fs_info->stripe_root);
  1031. default:
  1032. return NULL;
  1033. }
  1034. }
  1035. int btrfs_insert_fs_root(struct btrfs_fs_info *fs_info,
  1036. struct btrfs_root *root)
  1037. {
  1038. int ret;
  1039. ret = radix_tree_preload(GFP_NOFS);
  1040. if (ret)
  1041. return ret;
  1042. spin_lock(&fs_info->fs_roots_radix_lock);
  1043. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  1044. (unsigned long)btrfs_root_id(root),
  1045. root);
  1046. if (ret == 0) {
  1047. btrfs_grab_root(root);
  1048. set_bit(BTRFS_ROOT_IN_RADIX, &root->state);
  1049. }
  1050. spin_unlock(&fs_info->fs_roots_radix_lock);
  1051. radix_tree_preload_end();
  1052. return ret;
  1053. }
  1054. void btrfs_check_leaked_roots(const struct btrfs_fs_info *fs_info)
  1055. {
  1056. #ifdef CONFIG_BTRFS_DEBUG
  1057. struct btrfs_root *root;
  1058. while (!list_empty(&fs_info->allocated_roots)) {
  1059. char buf[BTRFS_ROOT_NAME_BUF_LEN];
  1060. root = list_first_entry(&fs_info->allocated_roots,
  1061. struct btrfs_root, leak_list);
  1062. btrfs_err(fs_info, "leaked root %s refcount %d",
  1063. btrfs_root_name(&root->root_key, buf),
  1064. refcount_read(&root->refs));
  1065. WARN_ON_ONCE(1);
  1066. while (refcount_read(&root->refs) > 1)
  1067. btrfs_put_root(root);
  1068. btrfs_put_root(root);
  1069. }
  1070. #endif
  1071. }
  1072. static void free_global_roots(struct btrfs_fs_info *fs_info)
  1073. {
  1074. struct btrfs_root *root;
  1075. struct rb_node *node;
  1076. while ((node = rb_first_postorder(&fs_info->global_root_tree)) != NULL) {
  1077. root = rb_entry(node, struct btrfs_root, rb_node);
  1078. rb_erase(&root->rb_node, &fs_info->global_root_tree);
  1079. btrfs_put_root(root);
  1080. }
  1081. }
  1082. void btrfs_free_fs_info(struct btrfs_fs_info *fs_info)
  1083. {
  1084. struct percpu_counter *em_counter = &fs_info->evictable_extent_maps;
  1085. percpu_counter_destroy(&fs_info->dirty_metadata_bytes);
  1086. percpu_counter_destroy(&fs_info->delalloc_bytes);
  1087. percpu_counter_destroy(&fs_info->ordered_bytes);
  1088. if (percpu_counter_initialized(em_counter))
  1089. ASSERT(percpu_counter_sum_positive(em_counter) == 0);
  1090. percpu_counter_destroy(em_counter);
  1091. percpu_counter_destroy(&fs_info->dev_replace.bio_counter);
  1092. btrfs_free_csum_hash(fs_info);
  1093. btrfs_free_stripe_hash_table(fs_info);
  1094. btrfs_free_ref_cache(fs_info);
  1095. kfree(fs_info->balance_ctl);
  1096. kfree(fs_info->delayed_root);
  1097. free_global_roots(fs_info);
  1098. btrfs_put_root(fs_info->tree_root);
  1099. btrfs_put_root(fs_info->chunk_root);
  1100. btrfs_put_root(fs_info->dev_root);
  1101. btrfs_put_root(fs_info->quota_root);
  1102. btrfs_put_root(fs_info->uuid_root);
  1103. btrfs_put_root(fs_info->fs_root);
  1104. btrfs_put_root(fs_info->data_reloc_root);
  1105. btrfs_put_root(fs_info->block_group_root);
  1106. btrfs_put_root(fs_info->stripe_root);
  1107. btrfs_check_leaked_roots(fs_info);
  1108. btrfs_extent_buffer_leak_debug_check(fs_info);
  1109. kfree(fs_info->super_copy);
  1110. kfree(fs_info->super_for_commit);
  1111. kvfree(fs_info);
  1112. }
  1113. /*
  1114. * Get an in-memory reference of a root structure.
  1115. *
  1116. * For essential trees like root/extent tree, we grab it from fs_info directly.
  1117. * For subvolume trees, we check the cached filesystem roots first. If not
  1118. * found, then read it from disk and add it to cached fs roots.
  1119. *
  1120. * Caller should release the root by calling btrfs_put_root() after the usage.
  1121. *
  1122. * NOTE: Reloc and log trees can't be read by this function as they share the
  1123. * same root objectid.
  1124. *
  1125. * @objectid: root id
  1126. * @anon_dev: preallocated anonymous block device number for new roots,
  1127. * pass NULL for a new allocation.
  1128. * @check_ref: whether to check root item references, If true, return -ENOENT
  1129. * for orphan roots
  1130. */
  1131. static struct btrfs_root *btrfs_get_root_ref(struct btrfs_fs_info *fs_info,
  1132. u64 objectid, dev_t *anon_dev,
  1133. bool check_ref)
  1134. {
  1135. struct btrfs_root *root;
  1136. struct btrfs_path *path;
  1137. struct btrfs_key key;
  1138. int ret;
  1139. root = btrfs_get_global_root(fs_info, objectid);
  1140. if (root)
  1141. return root;
  1142. /*
  1143. * If we're called for non-subvolume trees, and above function didn't
  1144. * find one, do not try to read it from disk.
  1145. *
  1146. * This is namely for free-space-tree and quota tree, which can change
  1147. * at runtime and should only be grabbed from fs_info.
  1148. */
  1149. if (!is_fstree(objectid) && objectid != BTRFS_DATA_RELOC_TREE_OBJECTID)
  1150. return ERR_PTR(-ENOENT);
  1151. again:
  1152. root = btrfs_lookup_fs_root(fs_info, objectid);
  1153. if (root) {
  1154. /*
  1155. * Some other caller may have read out the newly inserted
  1156. * subvolume already (for things like backref walk etc). Not
  1157. * that common but still possible. In that case, we just need
  1158. * to free the anon_dev.
  1159. */
  1160. if (unlikely(anon_dev && *anon_dev)) {
  1161. free_anon_bdev(*anon_dev);
  1162. *anon_dev = 0;
  1163. }
  1164. if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
  1165. btrfs_put_root(root);
  1166. return ERR_PTR(-ENOENT);
  1167. }
  1168. return root;
  1169. }
  1170. key.objectid = objectid;
  1171. key.type = BTRFS_ROOT_ITEM_KEY;
  1172. key.offset = (u64)-1;
  1173. root = btrfs_read_tree_root(fs_info->tree_root, &key);
  1174. if (IS_ERR(root))
  1175. return root;
  1176. if (check_ref && btrfs_root_refs(&root->root_item) == 0) {
  1177. ret = -ENOENT;
  1178. goto fail;
  1179. }
  1180. ret = btrfs_init_fs_root(root, anon_dev ? *anon_dev : 0);
  1181. if (ret)
  1182. goto fail;
  1183. path = btrfs_alloc_path();
  1184. if (!path) {
  1185. ret = -ENOMEM;
  1186. goto fail;
  1187. }
  1188. key.objectid = BTRFS_ORPHAN_OBJECTID;
  1189. key.type = BTRFS_ORPHAN_ITEM_KEY;
  1190. key.offset = objectid;
  1191. ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
  1192. btrfs_free_path(path);
  1193. if (ret < 0)
  1194. goto fail;
  1195. if (ret == 0)
  1196. set_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state);
  1197. ret = btrfs_insert_fs_root(fs_info, root);
  1198. if (ret) {
  1199. if (ret == -EEXIST) {
  1200. btrfs_put_root(root);
  1201. goto again;
  1202. }
  1203. goto fail;
  1204. }
  1205. return root;
  1206. fail:
  1207. /*
  1208. * If our caller provided us an anonymous device, then it's his
  1209. * responsibility to free it in case we fail. So we have to set our
  1210. * root's anon_dev to 0 to avoid a double free, once by btrfs_put_root()
  1211. * and once again by our caller.
  1212. */
  1213. if (anon_dev && *anon_dev)
  1214. root->anon_dev = 0;
  1215. btrfs_put_root(root);
  1216. return ERR_PTR(ret);
  1217. }
  1218. /*
  1219. * Get in-memory reference of a root structure
  1220. *
  1221. * @objectid: tree objectid
  1222. * @check_ref: if set, verify that the tree exists and the item has at least
  1223. * one reference
  1224. */
  1225. struct btrfs_root *btrfs_get_fs_root(struct btrfs_fs_info *fs_info,
  1226. u64 objectid, bool check_ref)
  1227. {
  1228. return btrfs_get_root_ref(fs_info, objectid, NULL, check_ref);
  1229. }
  1230. /*
  1231. * Get in-memory reference of a root structure, created as new, optionally pass
  1232. * the anonymous block device id
  1233. *
  1234. * @objectid: tree objectid
  1235. * @anon_dev: if NULL, allocate a new anonymous block device or use the
  1236. * parameter value if not NULL
  1237. */
  1238. struct btrfs_root *btrfs_get_new_fs_root(struct btrfs_fs_info *fs_info,
  1239. u64 objectid, dev_t *anon_dev)
  1240. {
  1241. return btrfs_get_root_ref(fs_info, objectid, anon_dev, true);
  1242. }
  1243. /*
  1244. * Return a root for the given objectid.
  1245. *
  1246. * @fs_info: the fs_info
  1247. * @objectid: the objectid we need to lookup
  1248. *
  1249. * This is exclusively used for backref walking, and exists specifically because
  1250. * of how qgroups does lookups. Qgroups will do a backref lookup at delayed ref
  1251. * creation time, which means we may have to read the tree_root in order to look
  1252. * up a fs root that is not in memory. If the root is not in memory we will
  1253. * read the tree root commit root and look up the fs root from there. This is a
  1254. * temporary root, it will not be inserted into the radix tree as it doesn't
  1255. * have the most uptodate information, it'll simply be discarded once the
  1256. * backref code is finished using the root.
  1257. */
  1258. struct btrfs_root *btrfs_get_fs_root_commit_root(struct btrfs_fs_info *fs_info,
  1259. struct btrfs_path *path,
  1260. u64 objectid)
  1261. {
  1262. struct btrfs_root *root;
  1263. struct btrfs_key key;
  1264. ASSERT(path->search_commit_root && path->skip_locking);
  1265. /*
  1266. * This can return -ENOENT if we ask for a root that doesn't exist, but
  1267. * since this is called via the backref walking code we won't be looking
  1268. * up a root that doesn't exist, unless there's corruption. So if root
  1269. * != NULL just return it.
  1270. */
  1271. root = btrfs_get_global_root(fs_info, objectid);
  1272. if (root)
  1273. return root;
  1274. root = btrfs_lookup_fs_root(fs_info, objectid);
  1275. if (root)
  1276. return root;
  1277. key.objectid = objectid;
  1278. key.type = BTRFS_ROOT_ITEM_KEY;
  1279. key.offset = (u64)-1;
  1280. root = read_tree_root_path(fs_info->tree_root, path, &key);
  1281. btrfs_release_path(path);
  1282. return root;
  1283. }
  1284. static int cleaner_kthread(void *arg)
  1285. {
  1286. struct btrfs_fs_info *fs_info = arg;
  1287. int again;
  1288. while (1) {
  1289. again = 0;
  1290. set_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
  1291. /* Make the cleaner go to sleep early. */
  1292. if (btrfs_need_cleaner_sleep(fs_info))
  1293. goto sleep;
  1294. /*
  1295. * Do not do anything if we might cause open_ctree() to block
  1296. * before we have finished mounting the filesystem.
  1297. */
  1298. if (!test_bit(BTRFS_FS_OPEN, &fs_info->flags))
  1299. goto sleep;
  1300. if (!mutex_trylock(&fs_info->cleaner_mutex))
  1301. goto sleep;
  1302. /*
  1303. * Avoid the problem that we change the status of the fs
  1304. * during the above check and trylock.
  1305. */
  1306. if (btrfs_need_cleaner_sleep(fs_info)) {
  1307. mutex_unlock(&fs_info->cleaner_mutex);
  1308. goto sleep;
  1309. }
  1310. if (test_and_clear_bit(BTRFS_FS_FEATURE_CHANGED, &fs_info->flags))
  1311. btrfs_sysfs_feature_update(fs_info);
  1312. btrfs_run_delayed_iputs(fs_info);
  1313. again = btrfs_clean_one_deleted_snapshot(fs_info);
  1314. mutex_unlock(&fs_info->cleaner_mutex);
  1315. /*
  1316. * The defragger has dealt with the R/O remount and umount,
  1317. * needn't do anything special here.
  1318. */
  1319. btrfs_run_defrag_inodes(fs_info);
  1320. /*
  1321. * Acquires fs_info->reclaim_bgs_lock to avoid racing
  1322. * with relocation (btrfs_relocate_chunk) and relocation
  1323. * acquires fs_info->cleaner_mutex (btrfs_relocate_block_group)
  1324. * after acquiring fs_info->reclaim_bgs_lock. So we
  1325. * can't hold, nor need to, fs_info->cleaner_mutex when deleting
  1326. * unused block groups.
  1327. */
  1328. btrfs_delete_unused_bgs(fs_info);
  1329. /*
  1330. * Reclaim block groups in the reclaim_bgs list after we deleted
  1331. * all unused block_groups. This possibly gives us some more free
  1332. * space.
  1333. */
  1334. btrfs_reclaim_bgs(fs_info);
  1335. sleep:
  1336. clear_and_wake_up_bit(BTRFS_FS_CLEANER_RUNNING, &fs_info->flags);
  1337. if (kthread_should_park())
  1338. kthread_parkme();
  1339. if (kthread_should_stop())
  1340. return 0;
  1341. if (!again) {
  1342. set_current_state(TASK_INTERRUPTIBLE);
  1343. schedule();
  1344. __set_current_state(TASK_RUNNING);
  1345. }
  1346. }
  1347. }
  1348. static int transaction_kthread(void *arg)
  1349. {
  1350. struct btrfs_root *root = arg;
  1351. struct btrfs_fs_info *fs_info = root->fs_info;
  1352. struct btrfs_trans_handle *trans;
  1353. struct btrfs_transaction *cur;
  1354. u64 transid;
  1355. time64_t delta;
  1356. unsigned long delay;
  1357. bool cannot_commit;
  1358. do {
  1359. cannot_commit = false;
  1360. delay = msecs_to_jiffies(fs_info->commit_interval * 1000);
  1361. mutex_lock(&fs_info->transaction_kthread_mutex);
  1362. spin_lock(&fs_info->trans_lock);
  1363. cur = fs_info->running_transaction;
  1364. if (!cur) {
  1365. spin_unlock(&fs_info->trans_lock);
  1366. goto sleep;
  1367. }
  1368. delta = ktime_get_seconds() - cur->start_time;
  1369. if (!test_and_clear_bit(BTRFS_FS_COMMIT_TRANS, &fs_info->flags) &&
  1370. cur->state < TRANS_STATE_COMMIT_PREP &&
  1371. delta < fs_info->commit_interval) {
  1372. spin_unlock(&fs_info->trans_lock);
  1373. delay -= msecs_to_jiffies((delta - 1) * 1000);
  1374. delay = min(delay,
  1375. msecs_to_jiffies(fs_info->commit_interval * 1000));
  1376. goto sleep;
  1377. }
  1378. transid = cur->transid;
  1379. spin_unlock(&fs_info->trans_lock);
  1380. /* If the file system is aborted, this will always fail. */
  1381. trans = btrfs_attach_transaction(root);
  1382. if (IS_ERR(trans)) {
  1383. if (PTR_ERR(trans) != -ENOENT)
  1384. cannot_commit = true;
  1385. goto sleep;
  1386. }
  1387. if (transid == trans->transid) {
  1388. btrfs_commit_transaction(trans);
  1389. } else {
  1390. btrfs_end_transaction(trans);
  1391. }
  1392. sleep:
  1393. wake_up_process(fs_info->cleaner_kthread);
  1394. mutex_unlock(&fs_info->transaction_kthread_mutex);
  1395. if (BTRFS_FS_ERROR(fs_info))
  1396. btrfs_cleanup_transaction(fs_info);
  1397. if (!kthread_should_stop() &&
  1398. (!btrfs_transaction_blocked(fs_info) ||
  1399. cannot_commit))
  1400. schedule_timeout_interruptible(delay);
  1401. } while (!kthread_should_stop());
  1402. return 0;
  1403. }
  1404. /*
  1405. * This will find the highest generation in the array of root backups. The
  1406. * index of the highest array is returned, or -EINVAL if we can't find
  1407. * anything.
  1408. *
  1409. * We check to make sure the array is valid by comparing the
  1410. * generation of the latest root in the array with the generation
  1411. * in the super block. If they don't match we pitch it.
  1412. */
  1413. static int find_newest_super_backup(struct btrfs_fs_info *info)
  1414. {
  1415. const u64 newest_gen = btrfs_super_generation(info->super_copy);
  1416. u64 cur;
  1417. struct btrfs_root_backup *root_backup;
  1418. int i;
  1419. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
  1420. root_backup = info->super_copy->super_roots + i;
  1421. cur = btrfs_backup_tree_root_gen(root_backup);
  1422. if (cur == newest_gen)
  1423. return i;
  1424. }
  1425. return -EINVAL;
  1426. }
  1427. /*
  1428. * copy all the root pointers into the super backup array.
  1429. * this will bump the backup pointer by one when it is
  1430. * done
  1431. */
  1432. static void backup_super_roots(struct btrfs_fs_info *info)
  1433. {
  1434. const int next_backup = info->backup_root_index;
  1435. struct btrfs_root_backup *root_backup;
  1436. root_backup = info->super_for_commit->super_roots + next_backup;
  1437. /*
  1438. * make sure all of our padding and empty slots get zero filled
  1439. * regardless of which ones we use today
  1440. */
  1441. memset(root_backup, 0, sizeof(*root_backup));
  1442. info->backup_root_index = (next_backup + 1) % BTRFS_NUM_BACKUP_ROOTS;
  1443. btrfs_set_backup_tree_root(root_backup, info->tree_root->node->start);
  1444. btrfs_set_backup_tree_root_gen(root_backup,
  1445. btrfs_header_generation(info->tree_root->node));
  1446. btrfs_set_backup_tree_root_level(root_backup,
  1447. btrfs_header_level(info->tree_root->node));
  1448. btrfs_set_backup_chunk_root(root_backup, info->chunk_root->node->start);
  1449. btrfs_set_backup_chunk_root_gen(root_backup,
  1450. btrfs_header_generation(info->chunk_root->node));
  1451. btrfs_set_backup_chunk_root_level(root_backup,
  1452. btrfs_header_level(info->chunk_root->node));
  1453. if (!btrfs_fs_compat_ro(info, BLOCK_GROUP_TREE)) {
  1454. struct btrfs_root *extent_root = btrfs_extent_root(info, 0);
  1455. struct btrfs_root *csum_root = btrfs_csum_root(info, 0);
  1456. btrfs_set_backup_extent_root(root_backup,
  1457. extent_root->node->start);
  1458. btrfs_set_backup_extent_root_gen(root_backup,
  1459. btrfs_header_generation(extent_root->node));
  1460. btrfs_set_backup_extent_root_level(root_backup,
  1461. btrfs_header_level(extent_root->node));
  1462. btrfs_set_backup_csum_root(root_backup, csum_root->node->start);
  1463. btrfs_set_backup_csum_root_gen(root_backup,
  1464. btrfs_header_generation(csum_root->node));
  1465. btrfs_set_backup_csum_root_level(root_backup,
  1466. btrfs_header_level(csum_root->node));
  1467. }
  1468. /*
  1469. * we might commit during log recovery, which happens before we set
  1470. * the fs_root. Make sure it is valid before we fill it in.
  1471. */
  1472. if (info->fs_root && info->fs_root->node) {
  1473. btrfs_set_backup_fs_root(root_backup,
  1474. info->fs_root->node->start);
  1475. btrfs_set_backup_fs_root_gen(root_backup,
  1476. btrfs_header_generation(info->fs_root->node));
  1477. btrfs_set_backup_fs_root_level(root_backup,
  1478. btrfs_header_level(info->fs_root->node));
  1479. }
  1480. btrfs_set_backup_dev_root(root_backup, info->dev_root->node->start);
  1481. btrfs_set_backup_dev_root_gen(root_backup,
  1482. btrfs_header_generation(info->dev_root->node));
  1483. btrfs_set_backup_dev_root_level(root_backup,
  1484. btrfs_header_level(info->dev_root->node));
  1485. btrfs_set_backup_total_bytes(root_backup,
  1486. btrfs_super_total_bytes(info->super_copy));
  1487. btrfs_set_backup_bytes_used(root_backup,
  1488. btrfs_super_bytes_used(info->super_copy));
  1489. btrfs_set_backup_num_devices(root_backup,
  1490. btrfs_super_num_devices(info->super_copy));
  1491. /*
  1492. * if we don't copy this out to the super_copy, it won't get remembered
  1493. * for the next commit
  1494. */
  1495. memcpy(&info->super_copy->super_roots,
  1496. &info->super_for_commit->super_roots,
  1497. sizeof(*root_backup) * BTRFS_NUM_BACKUP_ROOTS);
  1498. }
  1499. /*
  1500. * Reads a backup root based on the passed priority. Prio 0 is the newest, prio
  1501. * 1/2/3 are 2nd newest/3rd newest/4th (oldest) backup roots
  1502. *
  1503. * @fs_info: filesystem whose backup roots need to be read
  1504. * @priority: priority of backup root required
  1505. *
  1506. * Returns backup root index on success and -EINVAL otherwise.
  1507. */
  1508. static int read_backup_root(struct btrfs_fs_info *fs_info, u8 priority)
  1509. {
  1510. int backup_index = find_newest_super_backup(fs_info);
  1511. struct btrfs_super_block *super = fs_info->super_copy;
  1512. struct btrfs_root_backup *root_backup;
  1513. if (priority < BTRFS_NUM_BACKUP_ROOTS && backup_index >= 0) {
  1514. if (priority == 0)
  1515. return backup_index;
  1516. backup_index = backup_index + BTRFS_NUM_BACKUP_ROOTS - priority;
  1517. backup_index %= BTRFS_NUM_BACKUP_ROOTS;
  1518. } else {
  1519. return -EINVAL;
  1520. }
  1521. root_backup = super->super_roots + backup_index;
  1522. btrfs_set_super_generation(super,
  1523. btrfs_backup_tree_root_gen(root_backup));
  1524. btrfs_set_super_root(super, btrfs_backup_tree_root(root_backup));
  1525. btrfs_set_super_root_level(super,
  1526. btrfs_backup_tree_root_level(root_backup));
  1527. btrfs_set_super_bytes_used(super, btrfs_backup_bytes_used(root_backup));
  1528. /*
  1529. * Fixme: the total bytes and num_devices need to match or we should
  1530. * need a fsck
  1531. */
  1532. btrfs_set_super_total_bytes(super, btrfs_backup_total_bytes(root_backup));
  1533. btrfs_set_super_num_devices(super, btrfs_backup_num_devices(root_backup));
  1534. return backup_index;
  1535. }
  1536. /* helper to cleanup workers */
  1537. static void btrfs_stop_all_workers(struct btrfs_fs_info *fs_info)
  1538. {
  1539. btrfs_destroy_workqueue(fs_info->fixup_workers);
  1540. btrfs_destroy_workqueue(fs_info->delalloc_workers);
  1541. btrfs_destroy_workqueue(fs_info->workers);
  1542. if (fs_info->endio_workers)
  1543. destroy_workqueue(fs_info->endio_workers);
  1544. if (fs_info->rmw_workers)
  1545. destroy_workqueue(fs_info->rmw_workers);
  1546. if (fs_info->compressed_write_workers)
  1547. destroy_workqueue(fs_info->compressed_write_workers);
  1548. btrfs_destroy_workqueue(fs_info->endio_write_workers);
  1549. btrfs_destroy_workqueue(fs_info->endio_freespace_worker);
  1550. btrfs_destroy_workqueue(fs_info->delayed_workers);
  1551. btrfs_destroy_workqueue(fs_info->caching_workers);
  1552. btrfs_destroy_workqueue(fs_info->flush_workers);
  1553. btrfs_destroy_workqueue(fs_info->qgroup_rescan_workers);
  1554. if (fs_info->discard_ctl.discard_workers)
  1555. destroy_workqueue(fs_info->discard_ctl.discard_workers);
  1556. /*
  1557. * Now that all other work queues are destroyed, we can safely destroy
  1558. * the queues used for metadata I/O, since tasks from those other work
  1559. * queues can do metadata I/O operations.
  1560. */
  1561. if (fs_info->endio_meta_workers)
  1562. destroy_workqueue(fs_info->endio_meta_workers);
  1563. }
  1564. static void free_root_extent_buffers(struct btrfs_root *root)
  1565. {
  1566. if (root) {
  1567. free_extent_buffer(root->node);
  1568. free_extent_buffer(root->commit_root);
  1569. root->node = NULL;
  1570. root->commit_root = NULL;
  1571. }
  1572. }
  1573. static void free_global_root_pointers(struct btrfs_fs_info *fs_info)
  1574. {
  1575. struct btrfs_root *root, *tmp;
  1576. rbtree_postorder_for_each_entry_safe(root, tmp,
  1577. &fs_info->global_root_tree,
  1578. rb_node)
  1579. free_root_extent_buffers(root);
  1580. }
  1581. /* helper to cleanup tree roots */
  1582. static void free_root_pointers(struct btrfs_fs_info *info, bool free_chunk_root)
  1583. {
  1584. free_root_extent_buffers(info->tree_root);
  1585. free_global_root_pointers(info);
  1586. free_root_extent_buffers(info->dev_root);
  1587. free_root_extent_buffers(info->quota_root);
  1588. free_root_extent_buffers(info->uuid_root);
  1589. free_root_extent_buffers(info->fs_root);
  1590. free_root_extent_buffers(info->data_reloc_root);
  1591. free_root_extent_buffers(info->block_group_root);
  1592. free_root_extent_buffers(info->stripe_root);
  1593. if (free_chunk_root)
  1594. free_root_extent_buffers(info->chunk_root);
  1595. }
  1596. void btrfs_put_root(struct btrfs_root *root)
  1597. {
  1598. if (!root)
  1599. return;
  1600. if (refcount_dec_and_test(&root->refs)) {
  1601. if (WARN_ON(!xa_empty(&root->inodes)))
  1602. xa_destroy(&root->inodes);
  1603. WARN_ON(test_bit(BTRFS_ROOT_DEAD_RELOC_TREE, &root->state));
  1604. if (root->anon_dev)
  1605. free_anon_bdev(root->anon_dev);
  1606. free_root_extent_buffers(root);
  1607. #ifdef CONFIG_BTRFS_DEBUG
  1608. spin_lock(&root->fs_info->fs_roots_radix_lock);
  1609. list_del_init(&root->leak_list);
  1610. spin_unlock(&root->fs_info->fs_roots_radix_lock);
  1611. #endif
  1612. kfree(root);
  1613. }
  1614. }
  1615. void btrfs_free_fs_roots(struct btrfs_fs_info *fs_info)
  1616. {
  1617. int ret;
  1618. struct btrfs_root *gang[8];
  1619. int i;
  1620. while (!list_empty(&fs_info->dead_roots)) {
  1621. gang[0] = list_entry(fs_info->dead_roots.next,
  1622. struct btrfs_root, root_list);
  1623. list_del(&gang[0]->root_list);
  1624. if (test_bit(BTRFS_ROOT_IN_RADIX, &gang[0]->state))
  1625. btrfs_drop_and_free_fs_root(fs_info, gang[0]);
  1626. btrfs_put_root(gang[0]);
  1627. }
  1628. while (1) {
  1629. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1630. (void **)gang, 0,
  1631. ARRAY_SIZE(gang));
  1632. if (!ret)
  1633. break;
  1634. for (i = 0; i < ret; i++)
  1635. btrfs_drop_and_free_fs_root(fs_info, gang[i]);
  1636. }
  1637. }
  1638. static void btrfs_init_scrub(struct btrfs_fs_info *fs_info)
  1639. {
  1640. mutex_init(&fs_info->scrub_lock);
  1641. atomic_set(&fs_info->scrubs_running, 0);
  1642. atomic_set(&fs_info->scrub_pause_req, 0);
  1643. atomic_set(&fs_info->scrubs_paused, 0);
  1644. atomic_set(&fs_info->scrub_cancel_req, 0);
  1645. init_waitqueue_head(&fs_info->scrub_pause_wait);
  1646. refcount_set(&fs_info->scrub_workers_refcnt, 0);
  1647. }
  1648. static void btrfs_init_balance(struct btrfs_fs_info *fs_info)
  1649. {
  1650. spin_lock_init(&fs_info->balance_lock);
  1651. mutex_init(&fs_info->balance_mutex);
  1652. atomic_set(&fs_info->balance_pause_req, 0);
  1653. atomic_set(&fs_info->balance_cancel_req, 0);
  1654. fs_info->balance_ctl = NULL;
  1655. init_waitqueue_head(&fs_info->balance_wait_q);
  1656. atomic_set(&fs_info->reloc_cancel_req, 0);
  1657. }
  1658. static int btrfs_init_btree_inode(struct super_block *sb)
  1659. {
  1660. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  1661. unsigned long hash = btrfs_inode_hash(BTRFS_BTREE_INODE_OBJECTID,
  1662. fs_info->tree_root);
  1663. struct inode *inode;
  1664. inode = new_inode(sb);
  1665. if (!inode)
  1666. return -ENOMEM;
  1667. btrfs_set_inode_number(BTRFS_I(inode), BTRFS_BTREE_INODE_OBJECTID);
  1668. set_nlink(inode, 1);
  1669. /*
  1670. * we set the i_size on the btree inode to the max possible int.
  1671. * the real end of the address space is determined by all of
  1672. * the devices in the system
  1673. */
  1674. inode->i_size = OFFSET_MAX;
  1675. inode->i_mapping->a_ops = &btree_aops;
  1676. mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
  1677. extent_io_tree_init(fs_info, &BTRFS_I(inode)->io_tree,
  1678. IO_TREE_BTREE_INODE_IO);
  1679. extent_map_tree_init(&BTRFS_I(inode)->extent_tree);
  1680. BTRFS_I(inode)->root = btrfs_grab_root(fs_info->tree_root);
  1681. set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
  1682. __insert_inode_hash(inode, hash);
  1683. fs_info->btree_inode = inode;
  1684. return 0;
  1685. }
  1686. static void btrfs_init_dev_replace_locks(struct btrfs_fs_info *fs_info)
  1687. {
  1688. mutex_init(&fs_info->dev_replace.lock_finishing_cancel_unmount);
  1689. init_rwsem(&fs_info->dev_replace.rwsem);
  1690. init_waitqueue_head(&fs_info->dev_replace.replace_wait);
  1691. }
  1692. static void btrfs_init_qgroup(struct btrfs_fs_info *fs_info)
  1693. {
  1694. spin_lock_init(&fs_info->qgroup_lock);
  1695. mutex_init(&fs_info->qgroup_ioctl_lock);
  1696. fs_info->qgroup_tree = RB_ROOT;
  1697. INIT_LIST_HEAD(&fs_info->dirty_qgroups);
  1698. fs_info->qgroup_seq = 1;
  1699. fs_info->qgroup_ulist = NULL;
  1700. fs_info->qgroup_rescan_running = false;
  1701. fs_info->qgroup_drop_subtree_thres = BTRFS_QGROUP_DROP_SUBTREE_THRES_DEFAULT;
  1702. mutex_init(&fs_info->qgroup_rescan_lock);
  1703. }
  1704. static int btrfs_init_workqueues(struct btrfs_fs_info *fs_info)
  1705. {
  1706. u32 max_active = fs_info->thread_pool_size;
  1707. unsigned int flags = WQ_MEM_RECLAIM | WQ_FREEZABLE | WQ_UNBOUND;
  1708. unsigned int ordered_flags = WQ_MEM_RECLAIM | WQ_FREEZABLE;
  1709. fs_info->workers =
  1710. btrfs_alloc_workqueue(fs_info, "worker", flags, max_active, 16);
  1711. fs_info->delalloc_workers =
  1712. btrfs_alloc_workqueue(fs_info, "delalloc",
  1713. flags, max_active, 2);
  1714. fs_info->flush_workers =
  1715. btrfs_alloc_workqueue(fs_info, "flush_delalloc",
  1716. flags, max_active, 0);
  1717. fs_info->caching_workers =
  1718. btrfs_alloc_workqueue(fs_info, "cache", flags, max_active, 0);
  1719. fs_info->fixup_workers =
  1720. btrfs_alloc_ordered_workqueue(fs_info, "fixup", ordered_flags);
  1721. fs_info->endio_workers =
  1722. alloc_workqueue("btrfs-endio", flags, max_active);
  1723. fs_info->endio_meta_workers =
  1724. alloc_workqueue("btrfs-endio-meta", flags, max_active);
  1725. fs_info->rmw_workers = alloc_workqueue("btrfs-rmw", flags, max_active);
  1726. fs_info->endio_write_workers =
  1727. btrfs_alloc_workqueue(fs_info, "endio-write", flags,
  1728. max_active, 2);
  1729. fs_info->compressed_write_workers =
  1730. alloc_workqueue("btrfs-compressed-write", flags, max_active);
  1731. fs_info->endio_freespace_worker =
  1732. btrfs_alloc_workqueue(fs_info, "freespace-write", flags,
  1733. max_active, 0);
  1734. fs_info->delayed_workers =
  1735. btrfs_alloc_workqueue(fs_info, "delayed-meta", flags,
  1736. max_active, 0);
  1737. fs_info->qgroup_rescan_workers =
  1738. btrfs_alloc_ordered_workqueue(fs_info, "qgroup-rescan",
  1739. ordered_flags);
  1740. fs_info->discard_ctl.discard_workers =
  1741. alloc_ordered_workqueue("btrfs_discard", WQ_FREEZABLE);
  1742. if (!(fs_info->workers &&
  1743. fs_info->delalloc_workers && fs_info->flush_workers &&
  1744. fs_info->endio_workers && fs_info->endio_meta_workers &&
  1745. fs_info->compressed_write_workers &&
  1746. fs_info->endio_write_workers &&
  1747. fs_info->endio_freespace_worker && fs_info->rmw_workers &&
  1748. fs_info->caching_workers && fs_info->fixup_workers &&
  1749. fs_info->delayed_workers && fs_info->qgroup_rescan_workers &&
  1750. fs_info->discard_ctl.discard_workers)) {
  1751. return -ENOMEM;
  1752. }
  1753. return 0;
  1754. }
  1755. static int btrfs_init_csum_hash(struct btrfs_fs_info *fs_info, u16 csum_type)
  1756. {
  1757. struct crypto_shash *csum_shash;
  1758. const char *csum_driver = btrfs_super_csum_driver(csum_type);
  1759. csum_shash = crypto_alloc_shash(csum_driver, 0, 0);
  1760. if (IS_ERR(csum_shash)) {
  1761. btrfs_err(fs_info, "error allocating %s hash for checksum",
  1762. csum_driver);
  1763. return PTR_ERR(csum_shash);
  1764. }
  1765. fs_info->csum_shash = csum_shash;
  1766. /*
  1767. * Check if the checksum implementation is a fast accelerated one.
  1768. * As-is this is a bit of a hack and should be replaced once the csum
  1769. * implementations provide that information themselves.
  1770. */
  1771. switch (csum_type) {
  1772. case BTRFS_CSUM_TYPE_CRC32:
  1773. if (!strstr(crypto_shash_driver_name(csum_shash), "generic"))
  1774. set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
  1775. break;
  1776. case BTRFS_CSUM_TYPE_XXHASH:
  1777. set_bit(BTRFS_FS_CSUM_IMPL_FAST, &fs_info->flags);
  1778. break;
  1779. default:
  1780. break;
  1781. }
  1782. btrfs_info(fs_info, "using %s (%s) checksum algorithm",
  1783. btrfs_super_csum_name(csum_type),
  1784. crypto_shash_driver_name(csum_shash));
  1785. return 0;
  1786. }
  1787. static int btrfs_replay_log(struct btrfs_fs_info *fs_info,
  1788. struct btrfs_fs_devices *fs_devices)
  1789. {
  1790. int ret;
  1791. struct btrfs_tree_parent_check check = { 0 };
  1792. struct btrfs_root *log_tree_root;
  1793. struct btrfs_super_block *disk_super = fs_info->super_copy;
  1794. u64 bytenr = btrfs_super_log_root(disk_super);
  1795. int level = btrfs_super_log_root_level(disk_super);
  1796. if (fs_devices->rw_devices == 0) {
  1797. btrfs_warn(fs_info, "log replay required on RO media");
  1798. return -EIO;
  1799. }
  1800. log_tree_root = btrfs_alloc_root(fs_info, BTRFS_TREE_LOG_OBJECTID,
  1801. GFP_KERNEL);
  1802. if (!log_tree_root)
  1803. return -ENOMEM;
  1804. check.level = level;
  1805. check.transid = fs_info->generation + 1;
  1806. check.owner_root = BTRFS_TREE_LOG_OBJECTID;
  1807. log_tree_root->node = read_tree_block(fs_info, bytenr, &check);
  1808. if (IS_ERR(log_tree_root->node)) {
  1809. btrfs_warn(fs_info, "failed to read log tree");
  1810. ret = PTR_ERR(log_tree_root->node);
  1811. log_tree_root->node = NULL;
  1812. btrfs_put_root(log_tree_root);
  1813. return ret;
  1814. }
  1815. if (!extent_buffer_uptodate(log_tree_root->node)) {
  1816. btrfs_err(fs_info, "failed to read log tree");
  1817. btrfs_put_root(log_tree_root);
  1818. return -EIO;
  1819. }
  1820. /* returns with log_tree_root freed on success */
  1821. ret = btrfs_recover_log_trees(log_tree_root);
  1822. if (ret) {
  1823. btrfs_handle_fs_error(fs_info, ret,
  1824. "Failed to recover log tree");
  1825. btrfs_put_root(log_tree_root);
  1826. return ret;
  1827. }
  1828. if (sb_rdonly(fs_info->sb)) {
  1829. ret = btrfs_commit_super(fs_info);
  1830. if (ret)
  1831. return ret;
  1832. }
  1833. return 0;
  1834. }
  1835. static int load_global_roots_objectid(struct btrfs_root *tree_root,
  1836. struct btrfs_path *path, u64 objectid,
  1837. const char *name)
  1838. {
  1839. struct btrfs_fs_info *fs_info = tree_root->fs_info;
  1840. struct btrfs_root *root;
  1841. u64 max_global_id = 0;
  1842. int ret;
  1843. struct btrfs_key key = {
  1844. .objectid = objectid,
  1845. .type = BTRFS_ROOT_ITEM_KEY,
  1846. .offset = 0,
  1847. };
  1848. bool found = false;
  1849. /* If we have IGNOREDATACSUMS skip loading these roots. */
  1850. if (objectid == BTRFS_CSUM_TREE_OBJECTID &&
  1851. btrfs_test_opt(fs_info, IGNOREDATACSUMS)) {
  1852. set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
  1853. return 0;
  1854. }
  1855. while (1) {
  1856. ret = btrfs_search_slot(NULL, tree_root, &key, path, 0, 0);
  1857. if (ret < 0)
  1858. break;
  1859. if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
  1860. ret = btrfs_next_leaf(tree_root, path);
  1861. if (ret) {
  1862. if (ret > 0)
  1863. ret = 0;
  1864. break;
  1865. }
  1866. }
  1867. ret = 0;
  1868. btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
  1869. if (key.objectid != objectid)
  1870. break;
  1871. btrfs_release_path(path);
  1872. /*
  1873. * Just worry about this for extent tree, it'll be the same for
  1874. * everybody.
  1875. */
  1876. if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1877. max_global_id = max(max_global_id, key.offset);
  1878. found = true;
  1879. root = read_tree_root_path(tree_root, path, &key);
  1880. if (IS_ERR(root)) {
  1881. ret = PTR_ERR(root);
  1882. break;
  1883. }
  1884. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1885. ret = btrfs_global_root_insert(root);
  1886. if (ret) {
  1887. btrfs_put_root(root);
  1888. break;
  1889. }
  1890. key.offset++;
  1891. }
  1892. btrfs_release_path(path);
  1893. if (objectid == BTRFS_EXTENT_TREE_OBJECTID)
  1894. fs_info->nr_global_roots = max_global_id + 1;
  1895. if (!found || ret) {
  1896. if (objectid == BTRFS_CSUM_TREE_OBJECTID)
  1897. set_bit(BTRFS_FS_STATE_NO_DATA_CSUMS, &fs_info->fs_state);
  1898. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS))
  1899. ret = ret ? ret : -ENOENT;
  1900. else
  1901. ret = 0;
  1902. btrfs_err(fs_info, "failed to load root %s", name);
  1903. }
  1904. return ret;
  1905. }
  1906. static int load_global_roots(struct btrfs_root *tree_root)
  1907. {
  1908. struct btrfs_path *path;
  1909. int ret = 0;
  1910. path = btrfs_alloc_path();
  1911. if (!path)
  1912. return -ENOMEM;
  1913. ret = load_global_roots_objectid(tree_root, path,
  1914. BTRFS_EXTENT_TREE_OBJECTID, "extent");
  1915. if (ret)
  1916. goto out;
  1917. ret = load_global_roots_objectid(tree_root, path,
  1918. BTRFS_CSUM_TREE_OBJECTID, "csum");
  1919. if (ret)
  1920. goto out;
  1921. if (!btrfs_fs_compat_ro(tree_root->fs_info, FREE_SPACE_TREE))
  1922. goto out;
  1923. ret = load_global_roots_objectid(tree_root, path,
  1924. BTRFS_FREE_SPACE_TREE_OBJECTID,
  1925. "free space");
  1926. out:
  1927. btrfs_free_path(path);
  1928. return ret;
  1929. }
  1930. static int btrfs_read_roots(struct btrfs_fs_info *fs_info)
  1931. {
  1932. struct btrfs_root *tree_root = fs_info->tree_root;
  1933. struct btrfs_root *root;
  1934. struct btrfs_key location;
  1935. int ret;
  1936. ASSERT(fs_info->tree_root);
  1937. ret = load_global_roots(tree_root);
  1938. if (ret)
  1939. return ret;
  1940. location.type = BTRFS_ROOT_ITEM_KEY;
  1941. location.offset = 0;
  1942. if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE)) {
  1943. location.objectid = BTRFS_BLOCK_GROUP_TREE_OBJECTID;
  1944. root = btrfs_read_tree_root(tree_root, &location);
  1945. if (IS_ERR(root)) {
  1946. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
  1947. ret = PTR_ERR(root);
  1948. goto out;
  1949. }
  1950. } else {
  1951. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1952. fs_info->block_group_root = root;
  1953. }
  1954. }
  1955. location.objectid = BTRFS_DEV_TREE_OBJECTID;
  1956. root = btrfs_read_tree_root(tree_root, &location);
  1957. if (IS_ERR(root)) {
  1958. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
  1959. ret = PTR_ERR(root);
  1960. goto out;
  1961. }
  1962. } else {
  1963. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1964. fs_info->dev_root = root;
  1965. }
  1966. /* Initialize fs_info for all devices in any case */
  1967. ret = btrfs_init_devices_late(fs_info);
  1968. if (ret)
  1969. goto out;
  1970. /*
  1971. * This tree can share blocks with some other fs tree during relocation
  1972. * and we need a proper setup by btrfs_get_fs_root
  1973. */
  1974. root = btrfs_get_fs_root(tree_root->fs_info,
  1975. BTRFS_DATA_RELOC_TREE_OBJECTID, true);
  1976. if (IS_ERR(root)) {
  1977. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
  1978. ret = PTR_ERR(root);
  1979. goto out;
  1980. }
  1981. } else {
  1982. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1983. fs_info->data_reloc_root = root;
  1984. }
  1985. location.objectid = BTRFS_QUOTA_TREE_OBJECTID;
  1986. root = btrfs_read_tree_root(tree_root, &location);
  1987. if (!IS_ERR(root)) {
  1988. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  1989. fs_info->quota_root = root;
  1990. }
  1991. location.objectid = BTRFS_UUID_TREE_OBJECTID;
  1992. root = btrfs_read_tree_root(tree_root, &location);
  1993. if (IS_ERR(root)) {
  1994. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
  1995. ret = PTR_ERR(root);
  1996. if (ret != -ENOENT)
  1997. goto out;
  1998. }
  1999. } else {
  2000. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2001. fs_info->uuid_root = root;
  2002. }
  2003. if (btrfs_fs_incompat(fs_info, RAID_STRIPE_TREE)) {
  2004. location.objectid = BTRFS_RAID_STRIPE_TREE_OBJECTID;
  2005. root = btrfs_read_tree_root(tree_root, &location);
  2006. if (IS_ERR(root)) {
  2007. if (!btrfs_test_opt(fs_info, IGNOREBADROOTS)) {
  2008. ret = PTR_ERR(root);
  2009. goto out;
  2010. }
  2011. } else {
  2012. set_bit(BTRFS_ROOT_TRACK_DIRTY, &root->state);
  2013. fs_info->stripe_root = root;
  2014. }
  2015. }
  2016. return 0;
  2017. out:
  2018. btrfs_warn(fs_info, "failed to read root (objectid=%llu): %d",
  2019. location.objectid, ret);
  2020. return ret;
  2021. }
  2022. /*
  2023. * Real super block validation
  2024. * NOTE: super csum type and incompat features will not be checked here.
  2025. *
  2026. * @sb: super block to check
  2027. * @mirror_num: the super block number to check its bytenr:
  2028. * 0 the primary (1st) sb
  2029. * 1, 2 2nd and 3rd backup copy
  2030. * -1 skip bytenr check
  2031. */
  2032. int btrfs_validate_super(const struct btrfs_fs_info *fs_info,
  2033. const struct btrfs_super_block *sb, int mirror_num)
  2034. {
  2035. u64 nodesize = btrfs_super_nodesize(sb);
  2036. u64 sectorsize = btrfs_super_sectorsize(sb);
  2037. int ret = 0;
  2038. const bool ignore_flags = btrfs_test_opt(fs_info, IGNORESUPERFLAGS);
  2039. if (btrfs_super_magic(sb) != BTRFS_MAGIC) {
  2040. btrfs_err(fs_info, "no valid FS found");
  2041. ret = -EINVAL;
  2042. }
  2043. if ((btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP)) {
  2044. if (!ignore_flags) {
  2045. btrfs_err(fs_info,
  2046. "unrecognized or unsupported super flag 0x%llx",
  2047. btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
  2048. ret = -EINVAL;
  2049. } else {
  2050. btrfs_info(fs_info,
  2051. "unrecognized or unsupported super flags: 0x%llx, ignored",
  2052. btrfs_super_flags(sb) & ~BTRFS_SUPER_FLAG_SUPP);
  2053. }
  2054. }
  2055. if (btrfs_super_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2056. btrfs_err(fs_info, "tree_root level too big: %d >= %d",
  2057. btrfs_super_root_level(sb), BTRFS_MAX_LEVEL);
  2058. ret = -EINVAL;
  2059. }
  2060. if (btrfs_super_chunk_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2061. btrfs_err(fs_info, "chunk_root level too big: %d >= %d",
  2062. btrfs_super_chunk_root_level(sb), BTRFS_MAX_LEVEL);
  2063. ret = -EINVAL;
  2064. }
  2065. if (btrfs_super_log_root_level(sb) >= BTRFS_MAX_LEVEL) {
  2066. btrfs_err(fs_info, "log_root level too big: %d >= %d",
  2067. btrfs_super_log_root_level(sb), BTRFS_MAX_LEVEL);
  2068. ret = -EINVAL;
  2069. }
  2070. /*
  2071. * Check sectorsize and nodesize first, other check will need it.
  2072. * Check all possible sectorsize(4K, 8K, 16K, 32K, 64K) here.
  2073. */
  2074. if (!is_power_of_2(sectorsize) || sectorsize < 4096 ||
  2075. sectorsize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2076. btrfs_err(fs_info, "invalid sectorsize %llu", sectorsize);
  2077. ret = -EINVAL;
  2078. }
  2079. /*
  2080. * We only support at most two sectorsizes: 4K and PAGE_SIZE.
  2081. *
  2082. * We can support 16K sectorsize with 64K page size without problem,
  2083. * but such sectorsize/pagesize combination doesn't make much sense.
  2084. * 4K will be our future standard, PAGE_SIZE is supported from the very
  2085. * beginning.
  2086. */
  2087. if (sectorsize > PAGE_SIZE || (sectorsize != SZ_4K && sectorsize != PAGE_SIZE)) {
  2088. btrfs_err(fs_info,
  2089. "sectorsize %llu not yet supported for page size %lu",
  2090. sectorsize, PAGE_SIZE);
  2091. ret = -EINVAL;
  2092. }
  2093. if (!is_power_of_2(nodesize) || nodesize < sectorsize ||
  2094. nodesize > BTRFS_MAX_METADATA_BLOCKSIZE) {
  2095. btrfs_err(fs_info, "invalid nodesize %llu", nodesize);
  2096. ret = -EINVAL;
  2097. }
  2098. if (nodesize != le32_to_cpu(sb->__unused_leafsize)) {
  2099. btrfs_err(fs_info, "invalid leafsize %u, should be %llu",
  2100. le32_to_cpu(sb->__unused_leafsize), nodesize);
  2101. ret = -EINVAL;
  2102. }
  2103. /* Root alignment check */
  2104. if (!IS_ALIGNED(btrfs_super_root(sb), sectorsize)) {
  2105. btrfs_warn(fs_info, "tree_root block unaligned: %llu",
  2106. btrfs_super_root(sb));
  2107. ret = -EINVAL;
  2108. }
  2109. if (!IS_ALIGNED(btrfs_super_chunk_root(sb), sectorsize)) {
  2110. btrfs_warn(fs_info, "chunk_root block unaligned: %llu",
  2111. btrfs_super_chunk_root(sb));
  2112. ret = -EINVAL;
  2113. }
  2114. if (!IS_ALIGNED(btrfs_super_log_root(sb), sectorsize)) {
  2115. btrfs_warn(fs_info, "log_root block unaligned: %llu",
  2116. btrfs_super_log_root(sb));
  2117. ret = -EINVAL;
  2118. }
  2119. if (!fs_info->fs_devices->temp_fsid &&
  2120. memcmp(fs_info->fs_devices->fsid, sb->fsid, BTRFS_FSID_SIZE) != 0) {
  2121. btrfs_err(fs_info,
  2122. "superblock fsid doesn't match fsid of fs_devices: %pU != %pU",
  2123. sb->fsid, fs_info->fs_devices->fsid);
  2124. ret = -EINVAL;
  2125. }
  2126. if (memcmp(fs_info->fs_devices->metadata_uuid, btrfs_sb_fsid_ptr(sb),
  2127. BTRFS_FSID_SIZE) != 0) {
  2128. btrfs_err(fs_info,
  2129. "superblock metadata_uuid doesn't match metadata uuid of fs_devices: %pU != %pU",
  2130. btrfs_sb_fsid_ptr(sb), fs_info->fs_devices->metadata_uuid);
  2131. ret = -EINVAL;
  2132. }
  2133. if (memcmp(fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid,
  2134. BTRFS_FSID_SIZE) != 0) {
  2135. btrfs_err(fs_info,
  2136. "dev_item UUID does not match metadata fsid: %pU != %pU",
  2137. fs_info->fs_devices->metadata_uuid, sb->dev_item.fsid);
  2138. ret = -EINVAL;
  2139. }
  2140. /*
  2141. * Artificial requirement for block-group-tree to force newer features
  2142. * (free-space-tree, no-holes) so the test matrix is smaller.
  2143. */
  2144. if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
  2145. (!btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID) ||
  2146. !btrfs_fs_incompat(fs_info, NO_HOLES))) {
  2147. btrfs_err(fs_info,
  2148. "block-group-tree feature requires free-space-tree and no-holes");
  2149. ret = -EINVAL;
  2150. }
  2151. /*
  2152. * Hint to catch really bogus numbers, bitflips or so, more exact checks are
  2153. * done later
  2154. */
  2155. if (btrfs_super_bytes_used(sb) < 6 * btrfs_super_nodesize(sb)) {
  2156. btrfs_err(fs_info, "bytes_used is too small %llu",
  2157. btrfs_super_bytes_used(sb));
  2158. ret = -EINVAL;
  2159. }
  2160. if (!is_power_of_2(btrfs_super_stripesize(sb))) {
  2161. btrfs_err(fs_info, "invalid stripesize %u",
  2162. btrfs_super_stripesize(sb));
  2163. ret = -EINVAL;
  2164. }
  2165. if (btrfs_super_num_devices(sb) > (1UL << 31))
  2166. btrfs_warn(fs_info, "suspicious number of devices: %llu",
  2167. btrfs_super_num_devices(sb));
  2168. if (btrfs_super_num_devices(sb) == 0) {
  2169. btrfs_err(fs_info, "number of devices is 0");
  2170. ret = -EINVAL;
  2171. }
  2172. if (mirror_num >= 0 &&
  2173. btrfs_super_bytenr(sb) != btrfs_sb_offset(mirror_num)) {
  2174. btrfs_err(fs_info, "super offset mismatch %llu != %u",
  2175. btrfs_super_bytenr(sb), BTRFS_SUPER_INFO_OFFSET);
  2176. ret = -EINVAL;
  2177. }
  2178. /*
  2179. * Obvious sys_chunk_array corruptions, it must hold at least one key
  2180. * and one chunk
  2181. */
  2182. if (btrfs_super_sys_array_size(sb) > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE) {
  2183. btrfs_err(fs_info, "system chunk array too big %u > %u",
  2184. btrfs_super_sys_array_size(sb),
  2185. BTRFS_SYSTEM_CHUNK_ARRAY_SIZE);
  2186. ret = -EINVAL;
  2187. }
  2188. if (btrfs_super_sys_array_size(sb) < sizeof(struct btrfs_disk_key)
  2189. + sizeof(struct btrfs_chunk)) {
  2190. btrfs_err(fs_info, "system chunk array too small %u < %zu",
  2191. btrfs_super_sys_array_size(sb),
  2192. sizeof(struct btrfs_disk_key)
  2193. + sizeof(struct btrfs_chunk));
  2194. ret = -EINVAL;
  2195. }
  2196. /*
  2197. * The generation is a global counter, we'll trust it more than the others
  2198. * but it's still possible that it's the one that's wrong.
  2199. */
  2200. if (btrfs_super_generation(sb) < btrfs_super_chunk_root_generation(sb))
  2201. btrfs_warn(fs_info,
  2202. "suspicious: generation < chunk_root_generation: %llu < %llu",
  2203. btrfs_super_generation(sb),
  2204. btrfs_super_chunk_root_generation(sb));
  2205. if (btrfs_super_generation(sb) < btrfs_super_cache_generation(sb)
  2206. && btrfs_super_cache_generation(sb) != (u64)-1)
  2207. btrfs_warn(fs_info,
  2208. "suspicious: generation < cache_generation: %llu < %llu",
  2209. btrfs_super_generation(sb),
  2210. btrfs_super_cache_generation(sb));
  2211. return ret;
  2212. }
  2213. /*
  2214. * Validation of super block at mount time.
  2215. * Some checks already done early at mount time, like csum type and incompat
  2216. * flags will be skipped.
  2217. */
  2218. static int btrfs_validate_mount_super(struct btrfs_fs_info *fs_info)
  2219. {
  2220. return btrfs_validate_super(fs_info, fs_info->super_copy, 0);
  2221. }
  2222. /*
  2223. * Validation of super block at write time.
  2224. * Some checks like bytenr check will be skipped as their values will be
  2225. * overwritten soon.
  2226. * Extra checks like csum type and incompat flags will be done here.
  2227. */
  2228. static int btrfs_validate_write_super(struct btrfs_fs_info *fs_info,
  2229. struct btrfs_super_block *sb)
  2230. {
  2231. int ret;
  2232. ret = btrfs_validate_super(fs_info, sb, -1);
  2233. if (ret < 0)
  2234. goto out;
  2235. if (!btrfs_supported_super_csum(btrfs_super_csum_type(sb))) {
  2236. ret = -EUCLEAN;
  2237. btrfs_err(fs_info, "invalid csum type, has %u want %u",
  2238. btrfs_super_csum_type(sb), BTRFS_CSUM_TYPE_CRC32);
  2239. goto out;
  2240. }
  2241. if (btrfs_super_incompat_flags(sb) & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
  2242. ret = -EUCLEAN;
  2243. btrfs_err(fs_info,
  2244. "invalid incompat flags, has 0x%llx valid mask 0x%llx",
  2245. btrfs_super_incompat_flags(sb),
  2246. (unsigned long long)BTRFS_FEATURE_INCOMPAT_SUPP);
  2247. goto out;
  2248. }
  2249. out:
  2250. if (ret < 0)
  2251. btrfs_err(fs_info,
  2252. "super block corruption detected before writing it to disk");
  2253. return ret;
  2254. }
  2255. static int load_super_root(struct btrfs_root *root, u64 bytenr, u64 gen, int level)
  2256. {
  2257. struct btrfs_tree_parent_check check = {
  2258. .level = level,
  2259. .transid = gen,
  2260. .owner_root = btrfs_root_id(root)
  2261. };
  2262. int ret = 0;
  2263. root->node = read_tree_block(root->fs_info, bytenr, &check);
  2264. if (IS_ERR(root->node)) {
  2265. ret = PTR_ERR(root->node);
  2266. root->node = NULL;
  2267. return ret;
  2268. }
  2269. if (!extent_buffer_uptodate(root->node)) {
  2270. free_extent_buffer(root->node);
  2271. root->node = NULL;
  2272. return -EIO;
  2273. }
  2274. btrfs_set_root_node(&root->root_item, root->node);
  2275. root->commit_root = btrfs_root_node(root);
  2276. btrfs_set_root_refs(&root->root_item, 1);
  2277. return ret;
  2278. }
  2279. static int load_important_roots(struct btrfs_fs_info *fs_info)
  2280. {
  2281. struct btrfs_super_block *sb = fs_info->super_copy;
  2282. u64 gen, bytenr;
  2283. int level, ret;
  2284. bytenr = btrfs_super_root(sb);
  2285. gen = btrfs_super_generation(sb);
  2286. level = btrfs_super_root_level(sb);
  2287. ret = load_super_root(fs_info->tree_root, bytenr, gen, level);
  2288. if (ret) {
  2289. btrfs_warn(fs_info, "couldn't read tree root");
  2290. return ret;
  2291. }
  2292. return 0;
  2293. }
  2294. static int __cold init_tree_roots(struct btrfs_fs_info *fs_info)
  2295. {
  2296. int backup_index = find_newest_super_backup(fs_info);
  2297. struct btrfs_super_block *sb = fs_info->super_copy;
  2298. struct btrfs_root *tree_root = fs_info->tree_root;
  2299. bool handle_error = false;
  2300. int ret = 0;
  2301. int i;
  2302. for (i = 0; i < BTRFS_NUM_BACKUP_ROOTS; i++) {
  2303. if (handle_error) {
  2304. if (!IS_ERR(tree_root->node))
  2305. free_extent_buffer(tree_root->node);
  2306. tree_root->node = NULL;
  2307. if (!btrfs_test_opt(fs_info, USEBACKUPROOT))
  2308. break;
  2309. free_root_pointers(fs_info, 0);
  2310. /*
  2311. * Don't use the log in recovery mode, it won't be
  2312. * valid
  2313. */
  2314. btrfs_set_super_log_root(sb, 0);
  2315. btrfs_warn(fs_info, "try to load backup roots slot %d", i);
  2316. ret = read_backup_root(fs_info, i);
  2317. backup_index = ret;
  2318. if (ret < 0)
  2319. return ret;
  2320. }
  2321. ret = load_important_roots(fs_info);
  2322. if (ret) {
  2323. handle_error = true;
  2324. continue;
  2325. }
  2326. /*
  2327. * No need to hold btrfs_root::objectid_mutex since the fs
  2328. * hasn't been fully initialised and we are the only user
  2329. */
  2330. ret = btrfs_init_root_free_objectid(tree_root);
  2331. if (ret < 0) {
  2332. handle_error = true;
  2333. continue;
  2334. }
  2335. ASSERT(tree_root->free_objectid <= BTRFS_LAST_FREE_OBJECTID);
  2336. ret = btrfs_read_roots(fs_info);
  2337. if (ret < 0) {
  2338. handle_error = true;
  2339. continue;
  2340. }
  2341. /* All successful */
  2342. fs_info->generation = btrfs_header_generation(tree_root->node);
  2343. btrfs_set_last_trans_committed(fs_info, fs_info->generation);
  2344. fs_info->last_reloc_trans = 0;
  2345. /* Always begin writing backup roots after the one being used */
  2346. if (backup_index < 0) {
  2347. fs_info->backup_root_index = 0;
  2348. } else {
  2349. fs_info->backup_root_index = backup_index + 1;
  2350. fs_info->backup_root_index %= BTRFS_NUM_BACKUP_ROOTS;
  2351. }
  2352. break;
  2353. }
  2354. return ret;
  2355. }
  2356. void btrfs_init_fs_info(struct btrfs_fs_info *fs_info)
  2357. {
  2358. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_ATOMIC);
  2359. INIT_RADIX_TREE(&fs_info->buffer_radix, GFP_ATOMIC);
  2360. INIT_LIST_HEAD(&fs_info->trans_list);
  2361. INIT_LIST_HEAD(&fs_info->dead_roots);
  2362. INIT_LIST_HEAD(&fs_info->delayed_iputs);
  2363. INIT_LIST_HEAD(&fs_info->delalloc_roots);
  2364. INIT_LIST_HEAD(&fs_info->caching_block_groups);
  2365. spin_lock_init(&fs_info->delalloc_root_lock);
  2366. spin_lock_init(&fs_info->trans_lock);
  2367. spin_lock_init(&fs_info->fs_roots_radix_lock);
  2368. spin_lock_init(&fs_info->delayed_iput_lock);
  2369. spin_lock_init(&fs_info->defrag_inodes_lock);
  2370. spin_lock_init(&fs_info->super_lock);
  2371. spin_lock_init(&fs_info->buffer_lock);
  2372. spin_lock_init(&fs_info->unused_bgs_lock);
  2373. spin_lock_init(&fs_info->treelog_bg_lock);
  2374. spin_lock_init(&fs_info->zone_active_bgs_lock);
  2375. spin_lock_init(&fs_info->relocation_bg_lock);
  2376. rwlock_init(&fs_info->tree_mod_log_lock);
  2377. rwlock_init(&fs_info->global_root_lock);
  2378. mutex_init(&fs_info->unused_bg_unpin_mutex);
  2379. mutex_init(&fs_info->reclaim_bgs_lock);
  2380. mutex_init(&fs_info->reloc_mutex);
  2381. mutex_init(&fs_info->delalloc_root_mutex);
  2382. mutex_init(&fs_info->zoned_meta_io_lock);
  2383. mutex_init(&fs_info->zoned_data_reloc_io_lock);
  2384. seqlock_init(&fs_info->profiles_lock);
  2385. btrfs_lockdep_init_map(fs_info, btrfs_trans_num_writers);
  2386. btrfs_lockdep_init_map(fs_info, btrfs_trans_num_extwriters);
  2387. btrfs_lockdep_init_map(fs_info, btrfs_trans_pending_ordered);
  2388. btrfs_lockdep_init_map(fs_info, btrfs_ordered_extent);
  2389. btrfs_state_lockdep_init_map(fs_info, btrfs_trans_commit_prep,
  2390. BTRFS_LOCKDEP_TRANS_COMMIT_PREP);
  2391. btrfs_state_lockdep_init_map(fs_info, btrfs_trans_unblocked,
  2392. BTRFS_LOCKDEP_TRANS_UNBLOCKED);
  2393. btrfs_state_lockdep_init_map(fs_info, btrfs_trans_super_committed,
  2394. BTRFS_LOCKDEP_TRANS_SUPER_COMMITTED);
  2395. btrfs_state_lockdep_init_map(fs_info, btrfs_trans_completed,
  2396. BTRFS_LOCKDEP_TRANS_COMPLETED);
  2397. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  2398. INIT_LIST_HEAD(&fs_info->space_info);
  2399. INIT_LIST_HEAD(&fs_info->tree_mod_seq_list);
  2400. INIT_LIST_HEAD(&fs_info->unused_bgs);
  2401. INIT_LIST_HEAD(&fs_info->reclaim_bgs);
  2402. INIT_LIST_HEAD(&fs_info->zone_active_bgs);
  2403. #ifdef CONFIG_BTRFS_DEBUG
  2404. INIT_LIST_HEAD(&fs_info->allocated_roots);
  2405. INIT_LIST_HEAD(&fs_info->allocated_ebs);
  2406. spin_lock_init(&fs_info->eb_leak_lock);
  2407. #endif
  2408. fs_info->mapping_tree = RB_ROOT_CACHED;
  2409. rwlock_init(&fs_info->mapping_tree_lock);
  2410. btrfs_init_block_rsv(&fs_info->global_block_rsv,
  2411. BTRFS_BLOCK_RSV_GLOBAL);
  2412. btrfs_init_block_rsv(&fs_info->trans_block_rsv, BTRFS_BLOCK_RSV_TRANS);
  2413. btrfs_init_block_rsv(&fs_info->chunk_block_rsv, BTRFS_BLOCK_RSV_CHUNK);
  2414. btrfs_init_block_rsv(&fs_info->empty_block_rsv, BTRFS_BLOCK_RSV_EMPTY);
  2415. btrfs_init_block_rsv(&fs_info->delayed_block_rsv,
  2416. BTRFS_BLOCK_RSV_DELOPS);
  2417. btrfs_init_block_rsv(&fs_info->delayed_refs_rsv,
  2418. BTRFS_BLOCK_RSV_DELREFS);
  2419. atomic_set(&fs_info->async_delalloc_pages, 0);
  2420. atomic_set(&fs_info->defrag_running, 0);
  2421. atomic_set(&fs_info->nr_delayed_iputs, 0);
  2422. atomic64_set(&fs_info->tree_mod_seq, 0);
  2423. fs_info->global_root_tree = RB_ROOT;
  2424. fs_info->max_inline = BTRFS_DEFAULT_MAX_INLINE;
  2425. fs_info->metadata_ratio = 0;
  2426. fs_info->defrag_inodes = RB_ROOT;
  2427. atomic64_set(&fs_info->free_chunk_space, 0);
  2428. fs_info->tree_mod_log = RB_ROOT;
  2429. fs_info->commit_interval = BTRFS_DEFAULT_COMMIT_INTERVAL;
  2430. btrfs_init_ref_verify(fs_info);
  2431. fs_info->thread_pool_size = min_t(unsigned long,
  2432. num_online_cpus() + 2, 8);
  2433. INIT_LIST_HEAD(&fs_info->ordered_roots);
  2434. spin_lock_init(&fs_info->ordered_root_lock);
  2435. btrfs_init_scrub(fs_info);
  2436. btrfs_init_balance(fs_info);
  2437. btrfs_init_async_reclaim_work(fs_info);
  2438. btrfs_init_extent_map_shrinker_work(fs_info);
  2439. rwlock_init(&fs_info->block_group_cache_lock);
  2440. fs_info->block_group_cache_tree = RB_ROOT_CACHED;
  2441. extent_io_tree_init(fs_info, &fs_info->excluded_extents,
  2442. IO_TREE_FS_EXCLUDED_EXTENTS);
  2443. mutex_init(&fs_info->ordered_operations_mutex);
  2444. mutex_init(&fs_info->tree_log_mutex);
  2445. mutex_init(&fs_info->chunk_mutex);
  2446. mutex_init(&fs_info->transaction_kthread_mutex);
  2447. mutex_init(&fs_info->cleaner_mutex);
  2448. mutex_init(&fs_info->ro_block_group_mutex);
  2449. init_rwsem(&fs_info->commit_root_sem);
  2450. init_rwsem(&fs_info->cleanup_work_sem);
  2451. init_rwsem(&fs_info->subvol_sem);
  2452. sema_init(&fs_info->uuid_tree_rescan_sem, 1);
  2453. btrfs_init_dev_replace_locks(fs_info);
  2454. btrfs_init_qgroup(fs_info);
  2455. btrfs_discard_init(fs_info);
  2456. btrfs_init_free_cluster(&fs_info->meta_alloc_cluster);
  2457. btrfs_init_free_cluster(&fs_info->data_alloc_cluster);
  2458. init_waitqueue_head(&fs_info->transaction_throttle);
  2459. init_waitqueue_head(&fs_info->transaction_wait);
  2460. init_waitqueue_head(&fs_info->transaction_blocked_wait);
  2461. init_waitqueue_head(&fs_info->async_submit_wait);
  2462. init_waitqueue_head(&fs_info->delayed_iputs_wait);
  2463. /* Usable values until the real ones are cached from the superblock */
  2464. fs_info->nodesize = 4096;
  2465. fs_info->sectorsize = 4096;
  2466. fs_info->sectorsize_bits = ilog2(4096);
  2467. fs_info->stripesize = 4096;
  2468. /* Default compress algorithm when user does -o compress */
  2469. fs_info->compress_type = BTRFS_COMPRESS_ZLIB;
  2470. fs_info->max_extent_size = BTRFS_MAX_EXTENT_SIZE;
  2471. spin_lock_init(&fs_info->swapfile_pins_lock);
  2472. fs_info->swapfile_pins = RB_ROOT;
  2473. fs_info->bg_reclaim_threshold = BTRFS_DEFAULT_RECLAIM_THRESH;
  2474. INIT_WORK(&fs_info->reclaim_bgs_work, btrfs_reclaim_bgs_work);
  2475. }
  2476. static int init_mount_fs_info(struct btrfs_fs_info *fs_info, struct super_block *sb)
  2477. {
  2478. int ret;
  2479. fs_info->sb = sb;
  2480. /* Temporary fixed values for block size until we read the superblock. */
  2481. sb->s_blocksize = BTRFS_BDEV_BLOCKSIZE;
  2482. sb->s_blocksize_bits = blksize_bits(BTRFS_BDEV_BLOCKSIZE);
  2483. ret = percpu_counter_init(&fs_info->ordered_bytes, 0, GFP_KERNEL);
  2484. if (ret)
  2485. return ret;
  2486. ret = percpu_counter_init(&fs_info->evictable_extent_maps, 0, GFP_KERNEL);
  2487. if (ret)
  2488. return ret;
  2489. spin_lock_init(&fs_info->extent_map_shrinker_lock);
  2490. ret = percpu_counter_init(&fs_info->dirty_metadata_bytes, 0, GFP_KERNEL);
  2491. if (ret)
  2492. return ret;
  2493. fs_info->dirty_metadata_batch = PAGE_SIZE *
  2494. (1 + ilog2(nr_cpu_ids));
  2495. ret = percpu_counter_init(&fs_info->delalloc_bytes, 0, GFP_KERNEL);
  2496. if (ret)
  2497. return ret;
  2498. ret = percpu_counter_init(&fs_info->dev_replace.bio_counter, 0,
  2499. GFP_KERNEL);
  2500. if (ret)
  2501. return ret;
  2502. fs_info->delayed_root = kmalloc(sizeof(struct btrfs_delayed_root),
  2503. GFP_KERNEL);
  2504. if (!fs_info->delayed_root)
  2505. return -ENOMEM;
  2506. btrfs_init_delayed_root(fs_info->delayed_root);
  2507. if (sb_rdonly(sb))
  2508. set_bit(BTRFS_FS_STATE_RO, &fs_info->fs_state);
  2509. if (btrfs_test_opt(fs_info, IGNOREMETACSUMS))
  2510. set_bit(BTRFS_FS_STATE_SKIP_META_CSUMS, &fs_info->fs_state);
  2511. return btrfs_alloc_stripe_hash_table(fs_info);
  2512. }
  2513. static int btrfs_uuid_rescan_kthread(void *data)
  2514. {
  2515. struct btrfs_fs_info *fs_info = data;
  2516. int ret;
  2517. /*
  2518. * 1st step is to iterate through the existing UUID tree and
  2519. * to delete all entries that contain outdated data.
  2520. * 2nd step is to add all missing entries to the UUID tree.
  2521. */
  2522. ret = btrfs_uuid_tree_iterate(fs_info);
  2523. if (ret < 0) {
  2524. if (ret != -EINTR)
  2525. btrfs_warn(fs_info, "iterating uuid_tree failed %d",
  2526. ret);
  2527. up(&fs_info->uuid_tree_rescan_sem);
  2528. return ret;
  2529. }
  2530. return btrfs_uuid_scan_kthread(data);
  2531. }
  2532. static int btrfs_check_uuid_tree(struct btrfs_fs_info *fs_info)
  2533. {
  2534. struct task_struct *task;
  2535. down(&fs_info->uuid_tree_rescan_sem);
  2536. task = kthread_run(btrfs_uuid_rescan_kthread, fs_info, "btrfs-uuid");
  2537. if (IS_ERR(task)) {
  2538. /* fs_info->update_uuid_tree_gen remains 0 in all error case */
  2539. btrfs_warn(fs_info, "failed to start uuid_rescan task");
  2540. up(&fs_info->uuid_tree_rescan_sem);
  2541. return PTR_ERR(task);
  2542. }
  2543. return 0;
  2544. }
  2545. static int btrfs_cleanup_fs_roots(struct btrfs_fs_info *fs_info)
  2546. {
  2547. u64 root_objectid = 0;
  2548. struct btrfs_root *gang[8];
  2549. int ret = 0;
  2550. while (1) {
  2551. unsigned int found;
  2552. spin_lock(&fs_info->fs_roots_radix_lock);
  2553. found = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  2554. (void **)gang, root_objectid,
  2555. ARRAY_SIZE(gang));
  2556. if (!found) {
  2557. spin_unlock(&fs_info->fs_roots_radix_lock);
  2558. break;
  2559. }
  2560. root_objectid = btrfs_root_id(gang[found - 1]) + 1;
  2561. for (int i = 0; i < found; i++) {
  2562. /* Avoid to grab roots in dead_roots. */
  2563. if (btrfs_root_refs(&gang[i]->root_item) == 0) {
  2564. gang[i] = NULL;
  2565. continue;
  2566. }
  2567. /* Grab all the search result for later use. */
  2568. gang[i] = btrfs_grab_root(gang[i]);
  2569. }
  2570. spin_unlock(&fs_info->fs_roots_radix_lock);
  2571. for (int i = 0; i < found; i++) {
  2572. if (!gang[i])
  2573. continue;
  2574. root_objectid = btrfs_root_id(gang[i]);
  2575. /*
  2576. * Continue to release the remaining roots after the first
  2577. * error without cleanup and preserve the first error
  2578. * for the return.
  2579. */
  2580. if (!ret)
  2581. ret = btrfs_orphan_cleanup(gang[i]);
  2582. btrfs_put_root(gang[i]);
  2583. }
  2584. if (ret)
  2585. break;
  2586. root_objectid++;
  2587. }
  2588. return ret;
  2589. }
  2590. /*
  2591. * Mounting logic specific to read-write file systems. Shared by open_ctree
  2592. * and btrfs_remount when remounting from read-only to read-write.
  2593. */
  2594. int btrfs_start_pre_rw_mount(struct btrfs_fs_info *fs_info)
  2595. {
  2596. int ret;
  2597. const bool cache_opt = btrfs_test_opt(fs_info, SPACE_CACHE);
  2598. bool rebuild_free_space_tree = false;
  2599. if (btrfs_test_opt(fs_info, CLEAR_CACHE) &&
  2600. btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2601. if (btrfs_fs_incompat(fs_info, EXTENT_TREE_V2))
  2602. btrfs_warn(fs_info,
  2603. "'clear_cache' option is ignored with extent tree v2");
  2604. else
  2605. rebuild_free_space_tree = true;
  2606. } else if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
  2607. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE_VALID)) {
  2608. btrfs_warn(fs_info, "free space tree is invalid");
  2609. rebuild_free_space_tree = true;
  2610. }
  2611. if (rebuild_free_space_tree) {
  2612. btrfs_info(fs_info, "rebuilding free space tree");
  2613. ret = btrfs_rebuild_free_space_tree(fs_info);
  2614. if (ret) {
  2615. btrfs_warn(fs_info,
  2616. "failed to rebuild free space tree: %d", ret);
  2617. goto out;
  2618. }
  2619. }
  2620. if (btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE) &&
  2621. !btrfs_test_opt(fs_info, FREE_SPACE_TREE)) {
  2622. btrfs_info(fs_info, "disabling free space tree");
  2623. ret = btrfs_delete_free_space_tree(fs_info);
  2624. if (ret) {
  2625. btrfs_warn(fs_info,
  2626. "failed to disable free space tree: %d", ret);
  2627. goto out;
  2628. }
  2629. }
  2630. /*
  2631. * btrfs_find_orphan_roots() is responsible for finding all the dead
  2632. * roots (with 0 refs), flag them with BTRFS_ROOT_DEAD_TREE and load
  2633. * them into the fs_info->fs_roots_radix tree. This must be done before
  2634. * calling btrfs_orphan_cleanup() on the tree root. If we don't do it
  2635. * first, then btrfs_orphan_cleanup() will delete a dead root's orphan
  2636. * item before the root's tree is deleted - this means that if we unmount
  2637. * or crash before the deletion completes, on the next mount we will not
  2638. * delete what remains of the tree because the orphan item does not
  2639. * exists anymore, which is what tells us we have a pending deletion.
  2640. */
  2641. ret = btrfs_find_orphan_roots(fs_info);
  2642. if (ret)
  2643. goto out;
  2644. ret = btrfs_cleanup_fs_roots(fs_info);
  2645. if (ret)
  2646. goto out;
  2647. down_read(&fs_info->cleanup_work_sem);
  2648. if ((ret = btrfs_orphan_cleanup(fs_info->fs_root)) ||
  2649. (ret = btrfs_orphan_cleanup(fs_info->tree_root))) {
  2650. up_read(&fs_info->cleanup_work_sem);
  2651. goto out;
  2652. }
  2653. up_read(&fs_info->cleanup_work_sem);
  2654. mutex_lock(&fs_info->cleaner_mutex);
  2655. ret = btrfs_recover_relocation(fs_info);
  2656. mutex_unlock(&fs_info->cleaner_mutex);
  2657. if (ret < 0) {
  2658. btrfs_warn(fs_info, "failed to recover relocation: %d", ret);
  2659. goto out;
  2660. }
  2661. if (btrfs_test_opt(fs_info, FREE_SPACE_TREE) &&
  2662. !btrfs_fs_compat_ro(fs_info, FREE_SPACE_TREE)) {
  2663. btrfs_info(fs_info, "creating free space tree");
  2664. ret = btrfs_create_free_space_tree(fs_info);
  2665. if (ret) {
  2666. btrfs_warn(fs_info,
  2667. "failed to create free space tree: %d", ret);
  2668. goto out;
  2669. }
  2670. }
  2671. if (cache_opt != btrfs_free_space_cache_v1_active(fs_info)) {
  2672. ret = btrfs_set_free_space_cache_v1_active(fs_info, cache_opt);
  2673. if (ret)
  2674. goto out;
  2675. }
  2676. ret = btrfs_resume_balance_async(fs_info);
  2677. if (ret)
  2678. goto out;
  2679. ret = btrfs_resume_dev_replace_async(fs_info);
  2680. if (ret) {
  2681. btrfs_warn(fs_info, "failed to resume dev_replace");
  2682. goto out;
  2683. }
  2684. btrfs_qgroup_rescan_resume(fs_info);
  2685. if (!fs_info->uuid_root) {
  2686. btrfs_info(fs_info, "creating UUID tree");
  2687. ret = btrfs_create_uuid_tree(fs_info);
  2688. if (ret) {
  2689. btrfs_warn(fs_info,
  2690. "failed to create the UUID tree %d", ret);
  2691. goto out;
  2692. }
  2693. }
  2694. out:
  2695. return ret;
  2696. }
  2697. /*
  2698. * Do various sanity and dependency checks of different features.
  2699. *
  2700. * @is_rw_mount: If the mount is read-write.
  2701. *
  2702. * This is the place for less strict checks (like for subpage or artificial
  2703. * feature dependencies).
  2704. *
  2705. * For strict checks or possible corruption detection, see
  2706. * btrfs_validate_super().
  2707. *
  2708. * This should be called after btrfs_parse_options(), as some mount options
  2709. * (space cache related) can modify on-disk format like free space tree and
  2710. * screw up certain feature dependencies.
  2711. */
  2712. int btrfs_check_features(struct btrfs_fs_info *fs_info, bool is_rw_mount)
  2713. {
  2714. struct btrfs_super_block *disk_super = fs_info->super_copy;
  2715. u64 incompat = btrfs_super_incompat_flags(disk_super);
  2716. const u64 compat_ro = btrfs_super_compat_ro_flags(disk_super);
  2717. const u64 compat_ro_unsupp = (compat_ro & ~BTRFS_FEATURE_COMPAT_RO_SUPP);
  2718. if (incompat & ~BTRFS_FEATURE_INCOMPAT_SUPP) {
  2719. btrfs_err(fs_info,
  2720. "cannot mount because of unknown incompat features (0x%llx)",
  2721. incompat);
  2722. return -EINVAL;
  2723. }
  2724. /* Runtime limitation for mixed block groups. */
  2725. if ((incompat & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2726. (fs_info->sectorsize != fs_info->nodesize)) {
  2727. btrfs_err(fs_info,
  2728. "unequal nodesize/sectorsize (%u != %u) are not allowed for mixed block groups",
  2729. fs_info->nodesize, fs_info->sectorsize);
  2730. return -EINVAL;
  2731. }
  2732. /* Mixed backref is an always-enabled feature. */
  2733. incompat |= BTRFS_FEATURE_INCOMPAT_MIXED_BACKREF;
  2734. /* Set compression related flags just in case. */
  2735. if (fs_info->compress_type == BTRFS_COMPRESS_LZO)
  2736. incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_LZO;
  2737. else if (fs_info->compress_type == BTRFS_COMPRESS_ZSTD)
  2738. incompat |= BTRFS_FEATURE_INCOMPAT_COMPRESS_ZSTD;
  2739. /*
  2740. * An ancient flag, which should really be marked deprecated.
  2741. * Such runtime limitation doesn't really need a incompat flag.
  2742. */
  2743. if (btrfs_super_nodesize(disk_super) > PAGE_SIZE)
  2744. incompat |= BTRFS_FEATURE_INCOMPAT_BIG_METADATA;
  2745. if (compat_ro_unsupp && is_rw_mount) {
  2746. btrfs_err(fs_info,
  2747. "cannot mount read-write because of unknown compat_ro features (0x%llx)",
  2748. compat_ro);
  2749. return -EINVAL;
  2750. }
  2751. /*
  2752. * We have unsupported RO compat features, although RO mounted, we
  2753. * should not cause any metadata writes, including log replay.
  2754. * Or we could screw up whatever the new feature requires.
  2755. */
  2756. if (compat_ro_unsupp && btrfs_super_log_root(disk_super) &&
  2757. !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
  2758. btrfs_err(fs_info,
  2759. "cannot replay dirty log with unsupported compat_ro features (0x%llx), try rescue=nologreplay",
  2760. compat_ro);
  2761. return -EINVAL;
  2762. }
  2763. /*
  2764. * Artificial limitations for block group tree, to force
  2765. * block-group-tree to rely on no-holes and free-space-tree.
  2766. */
  2767. if (btrfs_fs_compat_ro(fs_info, BLOCK_GROUP_TREE) &&
  2768. (!btrfs_fs_incompat(fs_info, NO_HOLES) ||
  2769. !btrfs_test_opt(fs_info, FREE_SPACE_TREE))) {
  2770. btrfs_err(fs_info,
  2771. "block-group-tree feature requires no-holes and free-space-tree features");
  2772. return -EINVAL;
  2773. }
  2774. /*
  2775. * Subpage runtime limitation on v1 cache.
  2776. *
  2777. * V1 space cache still has some hard codeed PAGE_SIZE usage, while
  2778. * we're already defaulting to v2 cache, no need to bother v1 as it's
  2779. * going to be deprecated anyway.
  2780. */
  2781. if (fs_info->sectorsize < PAGE_SIZE && btrfs_test_opt(fs_info, SPACE_CACHE)) {
  2782. btrfs_warn(fs_info,
  2783. "v1 space cache is not supported for page size %lu with sectorsize %u",
  2784. PAGE_SIZE, fs_info->sectorsize);
  2785. return -EINVAL;
  2786. }
  2787. /* This can be called by remount, we need to protect the super block. */
  2788. spin_lock(&fs_info->super_lock);
  2789. btrfs_set_super_incompat_flags(disk_super, incompat);
  2790. spin_unlock(&fs_info->super_lock);
  2791. return 0;
  2792. }
  2793. int __cold open_ctree(struct super_block *sb, struct btrfs_fs_devices *fs_devices)
  2794. {
  2795. u32 sectorsize;
  2796. u32 nodesize;
  2797. u32 stripesize;
  2798. u64 generation;
  2799. u16 csum_type;
  2800. struct btrfs_super_block *disk_super;
  2801. struct btrfs_fs_info *fs_info = btrfs_sb(sb);
  2802. struct btrfs_root *tree_root;
  2803. struct btrfs_root *chunk_root;
  2804. int ret;
  2805. int level;
  2806. ret = init_mount_fs_info(fs_info, sb);
  2807. if (ret)
  2808. goto fail;
  2809. /* These need to be init'ed before we start creating inodes and such. */
  2810. tree_root = btrfs_alloc_root(fs_info, BTRFS_ROOT_TREE_OBJECTID,
  2811. GFP_KERNEL);
  2812. fs_info->tree_root = tree_root;
  2813. chunk_root = btrfs_alloc_root(fs_info, BTRFS_CHUNK_TREE_OBJECTID,
  2814. GFP_KERNEL);
  2815. fs_info->chunk_root = chunk_root;
  2816. if (!tree_root || !chunk_root) {
  2817. ret = -ENOMEM;
  2818. goto fail;
  2819. }
  2820. ret = btrfs_init_btree_inode(sb);
  2821. if (ret)
  2822. goto fail;
  2823. invalidate_bdev(fs_devices->latest_dev->bdev);
  2824. /*
  2825. * Read super block and check the signature bytes only
  2826. */
  2827. disk_super = btrfs_read_dev_super(fs_devices->latest_dev->bdev);
  2828. if (IS_ERR(disk_super)) {
  2829. ret = PTR_ERR(disk_super);
  2830. goto fail_alloc;
  2831. }
  2832. btrfs_info(fs_info, "first mount of filesystem %pU", disk_super->fsid);
  2833. /*
  2834. * Verify the type first, if that or the checksum value are
  2835. * corrupted, we'll find out
  2836. */
  2837. csum_type = btrfs_super_csum_type(disk_super);
  2838. if (!btrfs_supported_super_csum(csum_type)) {
  2839. btrfs_err(fs_info, "unsupported checksum algorithm: %u",
  2840. csum_type);
  2841. ret = -EINVAL;
  2842. btrfs_release_disk_super(disk_super);
  2843. goto fail_alloc;
  2844. }
  2845. fs_info->csum_size = btrfs_super_csum_size(disk_super);
  2846. ret = btrfs_init_csum_hash(fs_info, csum_type);
  2847. if (ret) {
  2848. btrfs_release_disk_super(disk_super);
  2849. goto fail_alloc;
  2850. }
  2851. /*
  2852. * We want to check superblock checksum, the type is stored inside.
  2853. * Pass the whole disk block of size BTRFS_SUPER_INFO_SIZE (4k).
  2854. */
  2855. if (btrfs_check_super_csum(fs_info, disk_super)) {
  2856. btrfs_err(fs_info, "superblock checksum mismatch");
  2857. ret = -EINVAL;
  2858. btrfs_release_disk_super(disk_super);
  2859. goto fail_alloc;
  2860. }
  2861. /*
  2862. * super_copy is zeroed at allocation time and we never touch the
  2863. * following bytes up to INFO_SIZE, the checksum is calculated from
  2864. * the whole block of INFO_SIZE
  2865. */
  2866. memcpy(fs_info->super_copy, disk_super, sizeof(*fs_info->super_copy));
  2867. btrfs_release_disk_super(disk_super);
  2868. disk_super = fs_info->super_copy;
  2869. memcpy(fs_info->super_for_commit, fs_info->super_copy,
  2870. sizeof(*fs_info->super_for_commit));
  2871. ret = btrfs_validate_mount_super(fs_info);
  2872. if (ret) {
  2873. btrfs_err(fs_info, "superblock contains fatal errors");
  2874. ret = -EINVAL;
  2875. goto fail_alloc;
  2876. }
  2877. if (!btrfs_super_root(disk_super)) {
  2878. btrfs_err(fs_info, "invalid superblock tree root bytenr");
  2879. ret = -EINVAL;
  2880. goto fail_alloc;
  2881. }
  2882. /* check FS state, whether FS is broken. */
  2883. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_ERROR)
  2884. WRITE_ONCE(fs_info->fs_error, -EUCLEAN);
  2885. /* Set up fs_info before parsing mount options */
  2886. nodesize = btrfs_super_nodesize(disk_super);
  2887. sectorsize = btrfs_super_sectorsize(disk_super);
  2888. stripesize = sectorsize;
  2889. fs_info->dirty_metadata_batch = nodesize * (1 + ilog2(nr_cpu_ids));
  2890. fs_info->delalloc_batch = sectorsize * 512 * (1 + ilog2(nr_cpu_ids));
  2891. fs_info->nodesize = nodesize;
  2892. fs_info->sectorsize = sectorsize;
  2893. fs_info->sectorsize_bits = ilog2(sectorsize);
  2894. fs_info->sectors_per_page = (PAGE_SIZE >> fs_info->sectorsize_bits);
  2895. fs_info->csums_per_leaf = BTRFS_MAX_ITEM_SIZE(fs_info) / fs_info->csum_size;
  2896. fs_info->stripesize = stripesize;
  2897. /*
  2898. * Handle the space caching options appropriately now that we have the
  2899. * super block loaded and validated.
  2900. */
  2901. btrfs_set_free_space_cache_settings(fs_info);
  2902. if (!btrfs_check_options(fs_info, &fs_info->mount_opt, sb->s_flags)) {
  2903. ret = -EINVAL;
  2904. goto fail_alloc;
  2905. }
  2906. ret = btrfs_check_features(fs_info, !sb_rdonly(sb));
  2907. if (ret < 0)
  2908. goto fail_alloc;
  2909. /*
  2910. * At this point our mount options are validated, if we set ->max_inline
  2911. * to something non-standard make sure we truncate it to sectorsize.
  2912. */
  2913. fs_info->max_inline = min_t(u64, fs_info->max_inline, fs_info->sectorsize);
  2914. if (sectorsize < PAGE_SIZE)
  2915. btrfs_warn(fs_info,
  2916. "read-write for sector size %u with page size %lu is experimental",
  2917. sectorsize, PAGE_SIZE);
  2918. ret = btrfs_init_workqueues(fs_info);
  2919. if (ret)
  2920. goto fail_sb_buffer;
  2921. sb->s_bdi->ra_pages *= btrfs_super_num_devices(disk_super);
  2922. sb->s_bdi->ra_pages = max(sb->s_bdi->ra_pages, SZ_4M / PAGE_SIZE);
  2923. /* Update the values for the current filesystem. */
  2924. sb->s_blocksize = sectorsize;
  2925. sb->s_blocksize_bits = blksize_bits(sectorsize);
  2926. memcpy(&sb->s_uuid, fs_info->fs_devices->fsid, BTRFS_FSID_SIZE);
  2927. mutex_lock(&fs_info->chunk_mutex);
  2928. ret = btrfs_read_sys_array(fs_info);
  2929. mutex_unlock(&fs_info->chunk_mutex);
  2930. if (ret) {
  2931. btrfs_err(fs_info, "failed to read the system array: %d", ret);
  2932. goto fail_sb_buffer;
  2933. }
  2934. generation = btrfs_super_chunk_root_generation(disk_super);
  2935. level = btrfs_super_chunk_root_level(disk_super);
  2936. ret = load_super_root(chunk_root, btrfs_super_chunk_root(disk_super),
  2937. generation, level);
  2938. if (ret) {
  2939. btrfs_err(fs_info, "failed to read chunk root");
  2940. goto fail_tree_roots;
  2941. }
  2942. read_extent_buffer(chunk_root->node, fs_info->chunk_tree_uuid,
  2943. offsetof(struct btrfs_header, chunk_tree_uuid),
  2944. BTRFS_UUID_SIZE);
  2945. ret = btrfs_read_chunk_tree(fs_info);
  2946. if (ret) {
  2947. btrfs_err(fs_info, "failed to read chunk tree: %d", ret);
  2948. goto fail_tree_roots;
  2949. }
  2950. /*
  2951. * At this point we know all the devices that make this filesystem,
  2952. * including the seed devices but we don't know yet if the replace
  2953. * target is required. So free devices that are not part of this
  2954. * filesystem but skip the replace target device which is checked
  2955. * below in btrfs_init_dev_replace().
  2956. */
  2957. btrfs_free_extra_devids(fs_devices);
  2958. if (!fs_devices->latest_dev->bdev) {
  2959. btrfs_err(fs_info, "failed to read devices");
  2960. ret = -EIO;
  2961. goto fail_tree_roots;
  2962. }
  2963. ret = init_tree_roots(fs_info);
  2964. if (ret)
  2965. goto fail_tree_roots;
  2966. /*
  2967. * Get zone type information of zoned block devices. This will also
  2968. * handle emulation of a zoned filesystem if a regular device has the
  2969. * zoned incompat feature flag set.
  2970. */
  2971. ret = btrfs_get_dev_zone_info_all_devices(fs_info);
  2972. if (ret) {
  2973. btrfs_err(fs_info,
  2974. "zoned: failed to read device zone info: %d", ret);
  2975. goto fail_block_groups;
  2976. }
  2977. /*
  2978. * If we have a uuid root and we're not being told to rescan we need to
  2979. * check the generation here so we can set the
  2980. * BTRFS_FS_UPDATE_UUID_TREE_GEN bit. Otherwise we could commit the
  2981. * transaction during a balance or the log replay without updating the
  2982. * uuid generation, and then if we crash we would rescan the uuid tree,
  2983. * even though it was perfectly fine.
  2984. */
  2985. if (fs_info->uuid_root && !btrfs_test_opt(fs_info, RESCAN_UUID_TREE) &&
  2986. fs_info->generation == btrfs_super_uuid_tree_generation(disk_super))
  2987. set_bit(BTRFS_FS_UPDATE_UUID_TREE_GEN, &fs_info->flags);
  2988. ret = btrfs_verify_dev_extents(fs_info);
  2989. if (ret) {
  2990. btrfs_err(fs_info,
  2991. "failed to verify dev extents against chunks: %d",
  2992. ret);
  2993. goto fail_block_groups;
  2994. }
  2995. ret = btrfs_recover_balance(fs_info);
  2996. if (ret) {
  2997. btrfs_err(fs_info, "failed to recover balance: %d", ret);
  2998. goto fail_block_groups;
  2999. }
  3000. ret = btrfs_init_dev_stats(fs_info);
  3001. if (ret) {
  3002. btrfs_err(fs_info, "failed to init dev_stats: %d", ret);
  3003. goto fail_block_groups;
  3004. }
  3005. ret = btrfs_init_dev_replace(fs_info);
  3006. if (ret) {
  3007. btrfs_err(fs_info, "failed to init dev_replace: %d", ret);
  3008. goto fail_block_groups;
  3009. }
  3010. ret = btrfs_check_zoned_mode(fs_info);
  3011. if (ret) {
  3012. btrfs_err(fs_info, "failed to initialize zoned mode: %d",
  3013. ret);
  3014. goto fail_block_groups;
  3015. }
  3016. ret = btrfs_sysfs_add_fsid(fs_devices);
  3017. if (ret) {
  3018. btrfs_err(fs_info, "failed to init sysfs fsid interface: %d",
  3019. ret);
  3020. goto fail_block_groups;
  3021. }
  3022. ret = btrfs_sysfs_add_mounted(fs_info);
  3023. if (ret) {
  3024. btrfs_err(fs_info, "failed to init sysfs interface: %d", ret);
  3025. goto fail_fsdev_sysfs;
  3026. }
  3027. ret = btrfs_init_space_info(fs_info);
  3028. if (ret) {
  3029. btrfs_err(fs_info, "failed to initialize space info: %d", ret);
  3030. goto fail_sysfs;
  3031. }
  3032. ret = btrfs_read_block_groups(fs_info);
  3033. if (ret) {
  3034. btrfs_err(fs_info, "failed to read block groups: %d", ret);
  3035. goto fail_sysfs;
  3036. }
  3037. btrfs_free_zone_cache(fs_info);
  3038. btrfs_check_active_zone_reservation(fs_info);
  3039. if (!sb_rdonly(sb) && fs_info->fs_devices->missing_devices &&
  3040. !btrfs_check_rw_degradable(fs_info, NULL)) {
  3041. btrfs_warn(fs_info,
  3042. "writable mount is not allowed due to too many missing devices");
  3043. ret = -EINVAL;
  3044. goto fail_sysfs;
  3045. }
  3046. fs_info->cleaner_kthread = kthread_run(cleaner_kthread, fs_info,
  3047. "btrfs-cleaner");
  3048. if (IS_ERR(fs_info->cleaner_kthread)) {
  3049. ret = PTR_ERR(fs_info->cleaner_kthread);
  3050. goto fail_sysfs;
  3051. }
  3052. fs_info->transaction_kthread = kthread_run(transaction_kthread,
  3053. tree_root,
  3054. "btrfs-transaction");
  3055. if (IS_ERR(fs_info->transaction_kthread)) {
  3056. ret = PTR_ERR(fs_info->transaction_kthread);
  3057. goto fail_cleaner;
  3058. }
  3059. ret = btrfs_read_qgroup_config(fs_info);
  3060. if (ret)
  3061. goto fail_trans_kthread;
  3062. if (btrfs_build_ref_tree(fs_info))
  3063. btrfs_err(fs_info, "couldn't build ref tree");
  3064. /* do not make disk changes in broken FS or nologreplay is given */
  3065. if (btrfs_super_log_root(disk_super) != 0 &&
  3066. !btrfs_test_opt(fs_info, NOLOGREPLAY)) {
  3067. btrfs_info(fs_info, "start tree-log replay");
  3068. ret = btrfs_replay_log(fs_info, fs_devices);
  3069. if (ret)
  3070. goto fail_qgroup;
  3071. }
  3072. fs_info->fs_root = btrfs_get_fs_root(fs_info, BTRFS_FS_TREE_OBJECTID, true);
  3073. if (IS_ERR(fs_info->fs_root)) {
  3074. ret = PTR_ERR(fs_info->fs_root);
  3075. btrfs_warn(fs_info, "failed to read fs tree: %d", ret);
  3076. fs_info->fs_root = NULL;
  3077. goto fail_qgroup;
  3078. }
  3079. if (sb_rdonly(sb))
  3080. return 0;
  3081. ret = btrfs_start_pre_rw_mount(fs_info);
  3082. if (ret) {
  3083. close_ctree(fs_info);
  3084. return ret;
  3085. }
  3086. btrfs_discard_resume(fs_info);
  3087. if (fs_info->uuid_root &&
  3088. (btrfs_test_opt(fs_info, RESCAN_UUID_TREE) ||
  3089. fs_info->generation != btrfs_super_uuid_tree_generation(disk_super))) {
  3090. btrfs_info(fs_info, "checking UUID tree");
  3091. ret = btrfs_check_uuid_tree(fs_info);
  3092. if (ret) {
  3093. btrfs_warn(fs_info,
  3094. "failed to check the UUID tree: %d", ret);
  3095. close_ctree(fs_info);
  3096. return ret;
  3097. }
  3098. }
  3099. set_bit(BTRFS_FS_OPEN, &fs_info->flags);
  3100. /* Kick the cleaner thread so it'll start deleting snapshots. */
  3101. if (test_bit(BTRFS_FS_UNFINISHED_DROPS, &fs_info->flags))
  3102. wake_up_process(fs_info->cleaner_kthread);
  3103. return 0;
  3104. fail_qgroup:
  3105. btrfs_free_qgroup_config(fs_info);
  3106. fail_trans_kthread:
  3107. kthread_stop(fs_info->transaction_kthread);
  3108. btrfs_cleanup_transaction(fs_info);
  3109. btrfs_free_fs_roots(fs_info);
  3110. fail_cleaner:
  3111. kthread_stop(fs_info->cleaner_kthread);
  3112. /*
  3113. * make sure we're done with the btree inode before we stop our
  3114. * kthreads
  3115. */
  3116. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  3117. fail_sysfs:
  3118. btrfs_sysfs_remove_mounted(fs_info);
  3119. fail_fsdev_sysfs:
  3120. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  3121. fail_block_groups:
  3122. btrfs_put_block_group_cache(fs_info);
  3123. fail_tree_roots:
  3124. if (fs_info->data_reloc_root)
  3125. btrfs_drop_and_free_fs_root(fs_info, fs_info->data_reloc_root);
  3126. free_root_pointers(fs_info, true);
  3127. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  3128. fail_sb_buffer:
  3129. btrfs_stop_all_workers(fs_info);
  3130. btrfs_free_block_groups(fs_info);
  3131. fail_alloc:
  3132. btrfs_mapping_tree_free(fs_info);
  3133. iput(fs_info->btree_inode);
  3134. fail:
  3135. btrfs_close_devices(fs_info->fs_devices);
  3136. ASSERT(ret < 0);
  3137. return ret;
  3138. }
  3139. ALLOW_ERROR_INJECTION(open_ctree, ERRNO);
  3140. static void btrfs_end_super_write(struct bio *bio)
  3141. {
  3142. struct btrfs_device *device = bio->bi_private;
  3143. struct folio_iter fi;
  3144. bio_for_each_folio_all(fi, bio) {
  3145. if (bio->bi_status) {
  3146. btrfs_warn_rl_in_rcu(device->fs_info,
  3147. "lost super block write due to IO error on %s (%d)",
  3148. btrfs_dev_name(device),
  3149. blk_status_to_errno(bio->bi_status));
  3150. btrfs_dev_stat_inc_and_print(device,
  3151. BTRFS_DEV_STAT_WRITE_ERRS);
  3152. /* Ensure failure if the primary sb fails. */
  3153. if (bio->bi_opf & REQ_FUA)
  3154. atomic_add(BTRFS_SUPER_PRIMARY_WRITE_ERROR,
  3155. &device->sb_write_errors);
  3156. else
  3157. atomic_inc(&device->sb_write_errors);
  3158. }
  3159. folio_unlock(fi.folio);
  3160. folio_put(fi.folio);
  3161. }
  3162. bio_put(bio);
  3163. }
  3164. struct btrfs_super_block *btrfs_read_dev_one_super(struct block_device *bdev,
  3165. int copy_num, bool drop_cache)
  3166. {
  3167. struct btrfs_super_block *super;
  3168. struct page *page;
  3169. u64 bytenr, bytenr_orig;
  3170. struct address_space *mapping = bdev->bd_mapping;
  3171. int ret;
  3172. bytenr_orig = btrfs_sb_offset(copy_num);
  3173. ret = btrfs_sb_log_location_bdev(bdev, copy_num, READ, &bytenr);
  3174. if (ret == -ENOENT)
  3175. return ERR_PTR(-EINVAL);
  3176. else if (ret)
  3177. return ERR_PTR(ret);
  3178. if (bytenr + BTRFS_SUPER_INFO_SIZE >= bdev_nr_bytes(bdev))
  3179. return ERR_PTR(-EINVAL);
  3180. if (drop_cache) {
  3181. /* This should only be called with the primary sb. */
  3182. ASSERT(copy_num == 0);
  3183. /*
  3184. * Drop the page of the primary superblock, so later read will
  3185. * always read from the device.
  3186. */
  3187. invalidate_inode_pages2_range(mapping,
  3188. bytenr >> PAGE_SHIFT,
  3189. (bytenr + BTRFS_SUPER_INFO_SIZE) >> PAGE_SHIFT);
  3190. }
  3191. page = read_cache_page_gfp(mapping, bytenr >> PAGE_SHIFT, GFP_NOFS);
  3192. if (IS_ERR(page))
  3193. return ERR_CAST(page);
  3194. super = page_address(page);
  3195. if (btrfs_super_magic(super) != BTRFS_MAGIC) {
  3196. btrfs_release_disk_super(super);
  3197. return ERR_PTR(-ENODATA);
  3198. }
  3199. if (btrfs_super_bytenr(super) != bytenr_orig) {
  3200. btrfs_release_disk_super(super);
  3201. return ERR_PTR(-EINVAL);
  3202. }
  3203. return super;
  3204. }
  3205. struct btrfs_super_block *btrfs_read_dev_super(struct block_device *bdev)
  3206. {
  3207. struct btrfs_super_block *super, *latest = NULL;
  3208. int i;
  3209. u64 transid = 0;
  3210. /* we would like to check all the supers, but that would make
  3211. * a btrfs mount succeed after a mkfs from a different FS.
  3212. * So, we need to add a special mount option to scan for
  3213. * later supers, using BTRFS_SUPER_MIRROR_MAX instead
  3214. */
  3215. for (i = 0; i < 1; i++) {
  3216. super = btrfs_read_dev_one_super(bdev, i, false);
  3217. if (IS_ERR(super))
  3218. continue;
  3219. if (!latest || btrfs_super_generation(super) > transid) {
  3220. if (latest)
  3221. btrfs_release_disk_super(super);
  3222. latest = super;
  3223. transid = btrfs_super_generation(super);
  3224. }
  3225. }
  3226. return super;
  3227. }
  3228. /*
  3229. * Write superblock @sb to the @device. Do not wait for completion, all the
  3230. * folios we use for writing are locked.
  3231. *
  3232. * Write @max_mirrors copies of the superblock, where 0 means default that fit
  3233. * the expected device size at commit time. Note that max_mirrors must be
  3234. * same for write and wait phases.
  3235. *
  3236. * Return number of errors when folio is not found or submission fails.
  3237. */
  3238. static int write_dev_supers(struct btrfs_device *device,
  3239. struct btrfs_super_block *sb, int max_mirrors)
  3240. {
  3241. struct btrfs_fs_info *fs_info = device->fs_info;
  3242. struct address_space *mapping = device->bdev->bd_mapping;
  3243. SHASH_DESC_ON_STACK(shash, fs_info->csum_shash);
  3244. int i;
  3245. int ret;
  3246. u64 bytenr, bytenr_orig;
  3247. atomic_set(&device->sb_write_errors, 0);
  3248. if (max_mirrors == 0)
  3249. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  3250. shash->tfm = fs_info->csum_shash;
  3251. for (i = 0; i < max_mirrors; i++) {
  3252. struct folio *folio;
  3253. struct bio *bio;
  3254. struct btrfs_super_block *disk_super;
  3255. size_t offset;
  3256. bytenr_orig = btrfs_sb_offset(i);
  3257. ret = btrfs_sb_log_location(device, i, WRITE, &bytenr);
  3258. if (ret == -ENOENT) {
  3259. continue;
  3260. } else if (ret < 0) {
  3261. btrfs_err(device->fs_info,
  3262. "couldn't get super block location for mirror %d",
  3263. i);
  3264. atomic_inc(&device->sb_write_errors);
  3265. continue;
  3266. }
  3267. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  3268. device->commit_total_bytes)
  3269. break;
  3270. btrfs_set_super_bytenr(sb, bytenr_orig);
  3271. crypto_shash_digest(shash, (const char *)sb + BTRFS_CSUM_SIZE,
  3272. BTRFS_SUPER_INFO_SIZE - BTRFS_CSUM_SIZE,
  3273. sb->csum);
  3274. folio = __filemap_get_folio(mapping, bytenr >> PAGE_SHIFT,
  3275. FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  3276. GFP_NOFS);
  3277. if (IS_ERR(folio)) {
  3278. btrfs_err(device->fs_info,
  3279. "couldn't get super block page for bytenr %llu",
  3280. bytenr);
  3281. atomic_inc(&device->sb_write_errors);
  3282. continue;
  3283. }
  3284. ASSERT(folio_order(folio) == 0);
  3285. offset = offset_in_folio(folio, bytenr);
  3286. disk_super = folio_address(folio) + offset;
  3287. memcpy(disk_super, sb, BTRFS_SUPER_INFO_SIZE);
  3288. /*
  3289. * Directly use bios here instead of relying on the page cache
  3290. * to do I/O, so we don't lose the ability to do integrity
  3291. * checking.
  3292. */
  3293. bio = bio_alloc(device->bdev, 1,
  3294. REQ_OP_WRITE | REQ_SYNC | REQ_META | REQ_PRIO,
  3295. GFP_NOFS);
  3296. bio->bi_iter.bi_sector = bytenr >> SECTOR_SHIFT;
  3297. bio->bi_private = device;
  3298. bio->bi_end_io = btrfs_end_super_write;
  3299. bio_add_folio_nofail(bio, folio, BTRFS_SUPER_INFO_SIZE, offset);
  3300. /*
  3301. * We FUA only the first super block. The others we allow to
  3302. * go down lazy and there's a short window where the on-disk
  3303. * copies might still contain the older version.
  3304. */
  3305. if (i == 0 && !btrfs_test_opt(device->fs_info, NOBARRIER))
  3306. bio->bi_opf |= REQ_FUA;
  3307. submit_bio(bio);
  3308. if (btrfs_advance_sb_log(device, i))
  3309. atomic_inc(&device->sb_write_errors);
  3310. }
  3311. return atomic_read(&device->sb_write_errors) < i ? 0 : -1;
  3312. }
  3313. /*
  3314. * Wait for write completion of superblocks done by write_dev_supers,
  3315. * @max_mirrors same for write and wait phases.
  3316. *
  3317. * Return -1 if primary super block write failed or when there were no super block
  3318. * copies written. Otherwise 0.
  3319. */
  3320. static int wait_dev_supers(struct btrfs_device *device, int max_mirrors)
  3321. {
  3322. int i;
  3323. int errors = 0;
  3324. bool primary_failed = false;
  3325. int ret;
  3326. u64 bytenr;
  3327. if (max_mirrors == 0)
  3328. max_mirrors = BTRFS_SUPER_MIRROR_MAX;
  3329. for (i = 0; i < max_mirrors; i++) {
  3330. struct folio *folio;
  3331. ret = btrfs_sb_log_location(device, i, READ, &bytenr);
  3332. if (ret == -ENOENT) {
  3333. break;
  3334. } else if (ret < 0) {
  3335. errors++;
  3336. if (i == 0)
  3337. primary_failed = true;
  3338. continue;
  3339. }
  3340. if (bytenr + BTRFS_SUPER_INFO_SIZE >=
  3341. device->commit_total_bytes)
  3342. break;
  3343. folio = filemap_get_folio(device->bdev->bd_mapping,
  3344. bytenr >> PAGE_SHIFT);
  3345. /* If the folio has been removed, then we know it completed. */
  3346. if (IS_ERR(folio))
  3347. continue;
  3348. ASSERT(folio_order(folio) == 0);
  3349. /* Folio will be unlocked once the write completes. */
  3350. folio_wait_locked(folio);
  3351. folio_put(folio);
  3352. }
  3353. errors += atomic_read(&device->sb_write_errors);
  3354. if (errors >= BTRFS_SUPER_PRIMARY_WRITE_ERROR)
  3355. primary_failed = true;
  3356. if (primary_failed) {
  3357. btrfs_err(device->fs_info, "error writing primary super block to device %llu",
  3358. device->devid);
  3359. return -1;
  3360. }
  3361. return errors < i ? 0 : -1;
  3362. }
  3363. /*
  3364. * endio for the write_dev_flush, this will wake anyone waiting
  3365. * for the barrier when it is done
  3366. */
  3367. static void btrfs_end_empty_barrier(struct bio *bio)
  3368. {
  3369. bio_uninit(bio);
  3370. complete(bio->bi_private);
  3371. }
  3372. /*
  3373. * Submit a flush request to the device if it supports it. Error handling is
  3374. * done in the waiting counterpart.
  3375. */
  3376. static void write_dev_flush(struct btrfs_device *device)
  3377. {
  3378. struct bio *bio = &device->flush_bio;
  3379. device->last_flush_error = BLK_STS_OK;
  3380. bio_init(bio, device->bdev, NULL, 0,
  3381. REQ_OP_WRITE | REQ_SYNC | REQ_PREFLUSH);
  3382. bio->bi_end_io = btrfs_end_empty_barrier;
  3383. init_completion(&device->flush_wait);
  3384. bio->bi_private = &device->flush_wait;
  3385. submit_bio(bio);
  3386. set_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state);
  3387. }
  3388. /*
  3389. * If the flush bio has been submitted by write_dev_flush, wait for it.
  3390. * Return true for any error, and false otherwise.
  3391. */
  3392. static bool wait_dev_flush(struct btrfs_device *device)
  3393. {
  3394. struct bio *bio = &device->flush_bio;
  3395. if (!test_and_clear_bit(BTRFS_DEV_STATE_FLUSH_SENT, &device->dev_state))
  3396. return false;
  3397. wait_for_completion_io(&device->flush_wait);
  3398. if (bio->bi_status) {
  3399. device->last_flush_error = bio->bi_status;
  3400. btrfs_dev_stat_inc_and_print(device, BTRFS_DEV_STAT_FLUSH_ERRS);
  3401. return true;
  3402. }
  3403. return false;
  3404. }
  3405. /*
  3406. * send an empty flush down to each device in parallel,
  3407. * then wait for them
  3408. */
  3409. static int barrier_all_devices(struct btrfs_fs_info *info)
  3410. {
  3411. struct list_head *head;
  3412. struct btrfs_device *dev;
  3413. int errors_wait = 0;
  3414. lockdep_assert_held(&info->fs_devices->device_list_mutex);
  3415. /* send down all the barriers */
  3416. head = &info->fs_devices->devices;
  3417. list_for_each_entry(dev, head, dev_list) {
  3418. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3419. continue;
  3420. if (!dev->bdev)
  3421. continue;
  3422. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3423. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3424. continue;
  3425. write_dev_flush(dev);
  3426. }
  3427. /* wait for all the barriers */
  3428. list_for_each_entry(dev, head, dev_list) {
  3429. if (test_bit(BTRFS_DEV_STATE_MISSING, &dev->dev_state))
  3430. continue;
  3431. if (!dev->bdev) {
  3432. errors_wait++;
  3433. continue;
  3434. }
  3435. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3436. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3437. continue;
  3438. if (wait_dev_flush(dev))
  3439. errors_wait++;
  3440. }
  3441. /*
  3442. * Checks last_flush_error of disks in order to determine the device
  3443. * state.
  3444. */
  3445. if (errors_wait && !btrfs_check_rw_degradable(info, NULL))
  3446. return -EIO;
  3447. return 0;
  3448. }
  3449. int btrfs_get_num_tolerated_disk_barrier_failures(u64 flags)
  3450. {
  3451. int raid_type;
  3452. int min_tolerated = INT_MAX;
  3453. if ((flags & BTRFS_BLOCK_GROUP_PROFILE_MASK) == 0 ||
  3454. (flags & BTRFS_AVAIL_ALLOC_BIT_SINGLE))
  3455. min_tolerated = min_t(int, min_tolerated,
  3456. btrfs_raid_array[BTRFS_RAID_SINGLE].
  3457. tolerated_failures);
  3458. for (raid_type = 0; raid_type < BTRFS_NR_RAID_TYPES; raid_type++) {
  3459. if (raid_type == BTRFS_RAID_SINGLE)
  3460. continue;
  3461. if (!(flags & btrfs_raid_array[raid_type].bg_flag))
  3462. continue;
  3463. min_tolerated = min_t(int, min_tolerated,
  3464. btrfs_raid_array[raid_type].
  3465. tolerated_failures);
  3466. }
  3467. if (min_tolerated == INT_MAX) {
  3468. pr_warn("BTRFS: unknown raid flag: %llu", flags);
  3469. min_tolerated = 0;
  3470. }
  3471. return min_tolerated;
  3472. }
  3473. int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
  3474. {
  3475. struct list_head *head;
  3476. struct btrfs_device *dev;
  3477. struct btrfs_super_block *sb;
  3478. struct btrfs_dev_item *dev_item;
  3479. int ret;
  3480. int do_barriers;
  3481. int max_errors;
  3482. int total_errors = 0;
  3483. u64 flags;
  3484. do_barriers = !btrfs_test_opt(fs_info, NOBARRIER);
  3485. /*
  3486. * max_mirrors == 0 indicates we're from commit_transaction,
  3487. * not from fsync where the tree roots in fs_info have not
  3488. * been consistent on disk.
  3489. */
  3490. if (max_mirrors == 0)
  3491. backup_super_roots(fs_info);
  3492. sb = fs_info->super_for_commit;
  3493. dev_item = &sb->dev_item;
  3494. mutex_lock(&fs_info->fs_devices->device_list_mutex);
  3495. head = &fs_info->fs_devices->devices;
  3496. max_errors = btrfs_super_num_devices(fs_info->super_copy) - 1;
  3497. if (do_barriers) {
  3498. ret = barrier_all_devices(fs_info);
  3499. if (ret) {
  3500. mutex_unlock(
  3501. &fs_info->fs_devices->device_list_mutex);
  3502. btrfs_handle_fs_error(fs_info, ret,
  3503. "errors while submitting device barriers.");
  3504. return ret;
  3505. }
  3506. }
  3507. list_for_each_entry(dev, head, dev_list) {
  3508. if (!dev->bdev) {
  3509. total_errors++;
  3510. continue;
  3511. }
  3512. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3513. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3514. continue;
  3515. btrfs_set_stack_device_generation(dev_item, 0);
  3516. btrfs_set_stack_device_type(dev_item, dev->type);
  3517. btrfs_set_stack_device_id(dev_item, dev->devid);
  3518. btrfs_set_stack_device_total_bytes(dev_item,
  3519. dev->commit_total_bytes);
  3520. btrfs_set_stack_device_bytes_used(dev_item,
  3521. dev->commit_bytes_used);
  3522. btrfs_set_stack_device_io_align(dev_item, dev->io_align);
  3523. btrfs_set_stack_device_io_width(dev_item, dev->io_width);
  3524. btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
  3525. memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
  3526. memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
  3527. BTRFS_FSID_SIZE);
  3528. flags = btrfs_super_flags(sb);
  3529. btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);
  3530. ret = btrfs_validate_write_super(fs_info, sb);
  3531. if (ret < 0) {
  3532. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3533. btrfs_handle_fs_error(fs_info, -EUCLEAN,
  3534. "unexpected superblock corruption detected");
  3535. return -EUCLEAN;
  3536. }
  3537. ret = write_dev_supers(dev, sb, max_mirrors);
  3538. if (ret)
  3539. total_errors++;
  3540. }
  3541. if (total_errors > max_errors) {
  3542. btrfs_err(fs_info, "%d errors while writing supers",
  3543. total_errors);
  3544. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3545. /* FUA is masked off if unsupported and can't be the reason */
  3546. btrfs_handle_fs_error(fs_info, -EIO,
  3547. "%d errors while writing supers",
  3548. total_errors);
  3549. return -EIO;
  3550. }
  3551. total_errors = 0;
  3552. list_for_each_entry(dev, head, dev_list) {
  3553. if (!dev->bdev)
  3554. continue;
  3555. if (!test_bit(BTRFS_DEV_STATE_IN_FS_METADATA, &dev->dev_state) ||
  3556. !test_bit(BTRFS_DEV_STATE_WRITEABLE, &dev->dev_state))
  3557. continue;
  3558. ret = wait_dev_supers(dev, max_mirrors);
  3559. if (ret)
  3560. total_errors++;
  3561. }
  3562. mutex_unlock(&fs_info->fs_devices->device_list_mutex);
  3563. if (total_errors > max_errors) {
  3564. btrfs_handle_fs_error(fs_info, -EIO,
  3565. "%d errors while writing supers",
  3566. total_errors);
  3567. return -EIO;
  3568. }
  3569. return 0;
  3570. }
  3571. /* Drop a fs root from the radix tree and free it. */
  3572. void btrfs_drop_and_free_fs_root(struct btrfs_fs_info *fs_info,
  3573. struct btrfs_root *root)
  3574. {
  3575. bool drop_ref = false;
  3576. spin_lock(&fs_info->fs_roots_radix_lock);
  3577. radix_tree_delete(&fs_info->fs_roots_radix,
  3578. (unsigned long)btrfs_root_id(root));
  3579. if (test_and_clear_bit(BTRFS_ROOT_IN_RADIX, &root->state))
  3580. drop_ref = true;
  3581. spin_unlock(&fs_info->fs_roots_radix_lock);
  3582. if (BTRFS_FS_ERROR(fs_info)) {
  3583. ASSERT(root->log_root == NULL);
  3584. if (root->reloc_root) {
  3585. btrfs_put_root(root->reloc_root);
  3586. root->reloc_root = NULL;
  3587. }
  3588. }
  3589. if (drop_ref)
  3590. btrfs_put_root(root);
  3591. }
  3592. int btrfs_commit_super(struct btrfs_fs_info *fs_info)
  3593. {
  3594. mutex_lock(&fs_info->cleaner_mutex);
  3595. btrfs_run_delayed_iputs(fs_info);
  3596. mutex_unlock(&fs_info->cleaner_mutex);
  3597. wake_up_process(fs_info->cleaner_kthread);
  3598. /* wait until ongoing cleanup work done */
  3599. down_write(&fs_info->cleanup_work_sem);
  3600. up_write(&fs_info->cleanup_work_sem);
  3601. return btrfs_commit_current_transaction(fs_info->tree_root);
  3602. }
  3603. static void warn_about_uncommitted_trans(struct btrfs_fs_info *fs_info)
  3604. {
  3605. struct btrfs_transaction *trans;
  3606. struct btrfs_transaction *tmp;
  3607. bool found = false;
  3608. /*
  3609. * This function is only called at the very end of close_ctree(),
  3610. * thus no other running transaction, no need to take trans_lock.
  3611. */
  3612. ASSERT(test_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags));
  3613. list_for_each_entry_safe(trans, tmp, &fs_info->trans_list, list) {
  3614. struct extent_state *cached = NULL;
  3615. u64 dirty_bytes = 0;
  3616. u64 cur = 0;
  3617. u64 found_start;
  3618. u64 found_end;
  3619. found = true;
  3620. while (find_first_extent_bit(&trans->dirty_pages, cur,
  3621. &found_start, &found_end, EXTENT_DIRTY, &cached)) {
  3622. dirty_bytes += found_end + 1 - found_start;
  3623. cur = found_end + 1;
  3624. }
  3625. btrfs_warn(fs_info,
  3626. "transaction %llu (with %llu dirty metadata bytes) is not committed",
  3627. trans->transid, dirty_bytes);
  3628. btrfs_cleanup_one_transaction(trans, fs_info);
  3629. if (trans == fs_info->running_transaction)
  3630. fs_info->running_transaction = NULL;
  3631. list_del_init(&trans->list);
  3632. btrfs_put_transaction(trans);
  3633. trace_btrfs_transaction_commit(fs_info);
  3634. }
  3635. ASSERT(!found);
  3636. }
  3637. void __cold close_ctree(struct btrfs_fs_info *fs_info)
  3638. {
  3639. int ret;
  3640. set_bit(BTRFS_FS_CLOSING_START, &fs_info->flags);
  3641. /*
  3642. * If we had UNFINISHED_DROPS we could still be processing them, so
  3643. * clear that bit and wake up relocation so it can stop.
  3644. * We must do this before stopping the block group reclaim task, because
  3645. * at btrfs_relocate_block_group() we wait for this bit, and after the
  3646. * wait we stop with -EINTR if btrfs_fs_closing() returns non-zero - we
  3647. * have just set BTRFS_FS_CLOSING_START, so btrfs_fs_closing() will
  3648. * return 1.
  3649. */
  3650. btrfs_wake_unfinished_drop(fs_info);
  3651. /*
  3652. * We may have the reclaim task running and relocating a data block group,
  3653. * in which case it may create delayed iputs. So stop it before we park
  3654. * the cleaner kthread otherwise we can get new delayed iputs after
  3655. * parking the cleaner, and that can make the async reclaim task to hang
  3656. * if it's waiting for delayed iputs to complete, since the cleaner is
  3657. * parked and can not run delayed iputs - this will make us hang when
  3658. * trying to stop the async reclaim task.
  3659. */
  3660. cancel_work_sync(&fs_info->reclaim_bgs_work);
  3661. /*
  3662. * We don't want the cleaner to start new transactions, add more delayed
  3663. * iputs, etc. while we're closing. We can't use kthread_stop() yet
  3664. * because that frees the task_struct, and the transaction kthread might
  3665. * still try to wake up the cleaner.
  3666. */
  3667. kthread_park(fs_info->cleaner_kthread);
  3668. /* wait for the qgroup rescan worker to stop */
  3669. btrfs_qgroup_wait_for_completion(fs_info, false);
  3670. /* wait for the uuid_scan task to finish */
  3671. down(&fs_info->uuid_tree_rescan_sem);
  3672. /* avoid complains from lockdep et al., set sem back to initial state */
  3673. up(&fs_info->uuid_tree_rescan_sem);
  3674. /* pause restriper - we want to resume on mount */
  3675. btrfs_pause_balance(fs_info);
  3676. btrfs_dev_replace_suspend_for_unmount(fs_info);
  3677. btrfs_scrub_cancel(fs_info);
  3678. /* wait for any defraggers to finish */
  3679. wait_event(fs_info->transaction_wait,
  3680. (atomic_read(&fs_info->defrag_running) == 0));
  3681. /* clear out the rbtree of defraggable inodes */
  3682. btrfs_cleanup_defrag_inodes(fs_info);
  3683. /*
  3684. * Handle the error fs first, as it will flush and wait for all ordered
  3685. * extents. This will generate delayed iputs, thus we want to handle
  3686. * it first.
  3687. */
  3688. if (unlikely(BTRFS_FS_ERROR(fs_info)))
  3689. btrfs_error_commit_super(fs_info);
  3690. /*
  3691. * Wait for any fixup workers to complete.
  3692. * If we don't wait for them here and they are still running by the time
  3693. * we call kthread_stop() against the cleaner kthread further below, we
  3694. * get an use-after-free on the cleaner because the fixup worker adds an
  3695. * inode to the list of delayed iputs and then attempts to wakeup the
  3696. * cleaner kthread, which was already stopped and destroyed. We parked
  3697. * already the cleaner, but below we run all pending delayed iputs.
  3698. */
  3699. btrfs_flush_workqueue(fs_info->fixup_workers);
  3700. /*
  3701. * Similar case here, we have to wait for delalloc workers before we
  3702. * proceed below and stop the cleaner kthread, otherwise we trigger a
  3703. * use-after-tree on the cleaner kthread task_struct when a delalloc
  3704. * worker running submit_compressed_extents() adds a delayed iput, which
  3705. * does a wake up on the cleaner kthread, which was already freed below
  3706. * when we call kthread_stop().
  3707. */
  3708. btrfs_flush_workqueue(fs_info->delalloc_workers);
  3709. /*
  3710. * We can have ordered extents getting their last reference dropped from
  3711. * the fs_info->workers queue because for async writes for data bios we
  3712. * queue a work for that queue, at btrfs_wq_submit_bio(), that runs
  3713. * run_one_async_done() which calls btrfs_bio_end_io() in case the bio
  3714. * has an error, and that later function can do the final
  3715. * btrfs_put_ordered_extent() on the ordered extent attached to the bio,
  3716. * which adds a delayed iput for the inode. So we must flush the queue
  3717. * so that we don't have delayed iputs after committing the current
  3718. * transaction below and stopping the cleaner and transaction kthreads.
  3719. */
  3720. btrfs_flush_workqueue(fs_info->workers);
  3721. /*
  3722. * When finishing a compressed write bio we schedule a work queue item
  3723. * to finish an ordered extent - btrfs_finish_compressed_write_work()
  3724. * calls btrfs_finish_ordered_extent() which in turns does a call to
  3725. * btrfs_queue_ordered_fn(), and that queues the ordered extent
  3726. * completion either in the endio_write_workers work queue or in the
  3727. * fs_info->endio_freespace_worker work queue. We flush those queues
  3728. * below, so before we flush them we must flush this queue for the
  3729. * workers of compressed writes.
  3730. */
  3731. flush_workqueue(fs_info->compressed_write_workers);
  3732. /*
  3733. * After we parked the cleaner kthread, ordered extents may have
  3734. * completed and created new delayed iputs. If one of the async reclaim
  3735. * tasks is running and in the RUN_DELAYED_IPUTS flush state, then we
  3736. * can hang forever trying to stop it, because if a delayed iput is
  3737. * added after it ran btrfs_run_delayed_iputs() and before it called
  3738. * btrfs_wait_on_delayed_iputs(), it will hang forever since there is
  3739. * no one else to run iputs.
  3740. *
  3741. * So wait for all ongoing ordered extents to complete and then run
  3742. * delayed iputs. This works because once we reach this point no one
  3743. * can either create new ordered extents nor create delayed iputs
  3744. * through some other means.
  3745. *
  3746. * Also note that btrfs_wait_ordered_roots() is not safe here, because
  3747. * it waits for BTRFS_ORDERED_COMPLETE to be set on an ordered extent,
  3748. * but the delayed iput for the respective inode is made only when doing
  3749. * the final btrfs_put_ordered_extent() (which must happen at
  3750. * btrfs_finish_ordered_io() when we are unmounting).
  3751. */
  3752. btrfs_flush_workqueue(fs_info->endio_write_workers);
  3753. /* Ordered extents for free space inodes. */
  3754. btrfs_flush_workqueue(fs_info->endio_freespace_worker);
  3755. btrfs_run_delayed_iputs(fs_info);
  3756. cancel_work_sync(&fs_info->async_reclaim_work);
  3757. cancel_work_sync(&fs_info->async_data_reclaim_work);
  3758. cancel_work_sync(&fs_info->preempt_reclaim_work);
  3759. cancel_work_sync(&fs_info->extent_map_shrinker_work);
  3760. /* Cancel or finish ongoing discard work */
  3761. btrfs_discard_cleanup(fs_info);
  3762. if (!sb_rdonly(fs_info->sb)) {
  3763. /*
  3764. * The cleaner kthread is stopped, so do one final pass over
  3765. * unused block groups.
  3766. */
  3767. btrfs_delete_unused_bgs(fs_info);
  3768. /*
  3769. * There might be existing delayed inode workers still running
  3770. * and holding an empty delayed inode item. We must wait for
  3771. * them to complete first because they can create a transaction.
  3772. * This happens when someone calls btrfs_balance_delayed_items()
  3773. * and then a transaction commit runs the same delayed nodes
  3774. * before any delayed worker has done something with the nodes.
  3775. * We must wait for any worker here and not at transaction
  3776. * commit time since that could cause a deadlock.
  3777. * This is a very rare case.
  3778. */
  3779. btrfs_flush_workqueue(fs_info->delayed_workers);
  3780. ret = btrfs_commit_super(fs_info);
  3781. if (ret)
  3782. btrfs_err(fs_info, "commit super ret %d", ret);
  3783. }
  3784. kthread_stop(fs_info->transaction_kthread);
  3785. kthread_stop(fs_info->cleaner_kthread);
  3786. ASSERT(list_empty(&fs_info->delayed_iputs));
  3787. set_bit(BTRFS_FS_CLOSING_DONE, &fs_info->flags);
  3788. if (btrfs_check_quota_leak(fs_info)) {
  3789. WARN_ON(IS_ENABLED(CONFIG_BTRFS_DEBUG));
  3790. btrfs_err(fs_info, "qgroup reserved space leaked");
  3791. }
  3792. btrfs_free_qgroup_config(fs_info);
  3793. ASSERT(list_empty(&fs_info->delalloc_roots));
  3794. if (percpu_counter_sum(&fs_info->delalloc_bytes)) {
  3795. btrfs_info(fs_info, "at unmount delalloc count %lld",
  3796. percpu_counter_sum(&fs_info->delalloc_bytes));
  3797. }
  3798. if (percpu_counter_sum(&fs_info->ordered_bytes))
  3799. btrfs_info(fs_info, "at unmount dio bytes count %lld",
  3800. percpu_counter_sum(&fs_info->ordered_bytes));
  3801. btrfs_sysfs_remove_mounted(fs_info);
  3802. btrfs_sysfs_remove_fsid(fs_info->fs_devices);
  3803. btrfs_put_block_group_cache(fs_info);
  3804. /*
  3805. * we must make sure there is not any read request to
  3806. * submit after we stopping all workers.
  3807. */
  3808. invalidate_inode_pages2(fs_info->btree_inode->i_mapping);
  3809. btrfs_stop_all_workers(fs_info);
  3810. /* We shouldn't have any transaction open at this point */
  3811. warn_about_uncommitted_trans(fs_info);
  3812. clear_bit(BTRFS_FS_OPEN, &fs_info->flags);
  3813. free_root_pointers(fs_info, true);
  3814. btrfs_free_fs_roots(fs_info);
  3815. /*
  3816. * We must free the block groups after dropping the fs_roots as we could
  3817. * have had an IO error and have left over tree log blocks that aren't
  3818. * cleaned up until the fs roots are freed. This makes the block group
  3819. * accounting appear to be wrong because there's pending reserved bytes,
  3820. * so make sure we do the block group cleanup afterwards.
  3821. */
  3822. btrfs_free_block_groups(fs_info);
  3823. iput(fs_info->btree_inode);
  3824. btrfs_mapping_tree_free(fs_info);
  3825. btrfs_close_devices(fs_info->fs_devices);
  3826. }
  3827. void btrfs_mark_buffer_dirty(struct btrfs_trans_handle *trans,
  3828. struct extent_buffer *buf)
  3829. {
  3830. struct btrfs_fs_info *fs_info = buf->fs_info;
  3831. u64 transid = btrfs_header_generation(buf);
  3832. #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
  3833. /*
  3834. * This is a fast path so only do this check if we have sanity tests
  3835. * enabled. Normal people shouldn't be using unmapped buffers as dirty
  3836. * outside of the sanity tests.
  3837. */
  3838. if (unlikely(test_bit(EXTENT_BUFFER_UNMAPPED, &buf->bflags)))
  3839. return;
  3840. #endif
  3841. /* This is an active transaction (its state < TRANS_STATE_UNBLOCKED). */
  3842. ASSERT(trans->transid == fs_info->generation);
  3843. btrfs_assert_tree_write_locked(buf);
  3844. if (unlikely(transid != fs_info->generation)) {
  3845. btrfs_abort_transaction(trans, -EUCLEAN);
  3846. btrfs_crit(fs_info,
  3847. "dirty buffer transid mismatch, logical %llu found transid %llu running transid %llu",
  3848. buf->start, transid, fs_info->generation);
  3849. }
  3850. set_extent_buffer_dirty(buf);
  3851. }
  3852. static void __btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info,
  3853. int flush_delayed)
  3854. {
  3855. /*
  3856. * looks as though older kernels can get into trouble with
  3857. * this code, they end up stuck in balance_dirty_pages forever
  3858. */
  3859. int ret;
  3860. if (current->flags & PF_MEMALLOC)
  3861. return;
  3862. if (flush_delayed)
  3863. btrfs_balance_delayed_items(fs_info);
  3864. ret = __percpu_counter_compare(&fs_info->dirty_metadata_bytes,
  3865. BTRFS_DIRTY_METADATA_THRESH,
  3866. fs_info->dirty_metadata_batch);
  3867. if (ret > 0) {
  3868. balance_dirty_pages_ratelimited(fs_info->btree_inode->i_mapping);
  3869. }
  3870. }
  3871. void btrfs_btree_balance_dirty(struct btrfs_fs_info *fs_info)
  3872. {
  3873. __btrfs_btree_balance_dirty(fs_info, 1);
  3874. }
  3875. void btrfs_btree_balance_dirty_nodelay(struct btrfs_fs_info *fs_info)
  3876. {
  3877. __btrfs_btree_balance_dirty(fs_info, 0);
  3878. }
  3879. static void btrfs_error_commit_super(struct btrfs_fs_info *fs_info)
  3880. {
  3881. /* cleanup FS via transaction */
  3882. btrfs_cleanup_transaction(fs_info);
  3883. down_write(&fs_info->cleanup_work_sem);
  3884. up_write(&fs_info->cleanup_work_sem);
  3885. }
  3886. static void btrfs_drop_all_logs(struct btrfs_fs_info *fs_info)
  3887. {
  3888. struct btrfs_root *gang[8];
  3889. u64 root_objectid = 0;
  3890. int ret;
  3891. spin_lock(&fs_info->fs_roots_radix_lock);
  3892. while ((ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  3893. (void **)gang, root_objectid,
  3894. ARRAY_SIZE(gang))) != 0) {
  3895. int i;
  3896. for (i = 0; i < ret; i++)
  3897. gang[i] = btrfs_grab_root(gang[i]);
  3898. spin_unlock(&fs_info->fs_roots_radix_lock);
  3899. for (i = 0; i < ret; i++) {
  3900. if (!gang[i])
  3901. continue;
  3902. root_objectid = btrfs_root_id(gang[i]);
  3903. btrfs_free_log(NULL, gang[i]);
  3904. btrfs_put_root(gang[i]);
  3905. }
  3906. root_objectid++;
  3907. spin_lock(&fs_info->fs_roots_radix_lock);
  3908. }
  3909. spin_unlock(&fs_info->fs_roots_radix_lock);
  3910. btrfs_free_log_root_tree(NULL, fs_info);
  3911. }
  3912. static void btrfs_destroy_ordered_extents(struct btrfs_root *root)
  3913. {
  3914. struct btrfs_ordered_extent *ordered;
  3915. spin_lock(&root->ordered_extent_lock);
  3916. /*
  3917. * This will just short circuit the ordered completion stuff which will
  3918. * make sure the ordered extent gets properly cleaned up.
  3919. */
  3920. list_for_each_entry(ordered, &root->ordered_extents,
  3921. root_extent_list)
  3922. set_bit(BTRFS_ORDERED_IOERR, &ordered->flags);
  3923. spin_unlock(&root->ordered_extent_lock);
  3924. }
  3925. static void btrfs_destroy_all_ordered_extents(struct btrfs_fs_info *fs_info)
  3926. {
  3927. struct btrfs_root *root;
  3928. LIST_HEAD(splice);
  3929. spin_lock(&fs_info->ordered_root_lock);
  3930. list_splice_init(&fs_info->ordered_roots, &splice);
  3931. while (!list_empty(&splice)) {
  3932. root = list_first_entry(&splice, struct btrfs_root,
  3933. ordered_root);
  3934. list_move_tail(&root->ordered_root,
  3935. &fs_info->ordered_roots);
  3936. spin_unlock(&fs_info->ordered_root_lock);
  3937. btrfs_destroy_ordered_extents(root);
  3938. cond_resched();
  3939. spin_lock(&fs_info->ordered_root_lock);
  3940. }
  3941. spin_unlock(&fs_info->ordered_root_lock);
  3942. /*
  3943. * We need this here because if we've been flipped read-only we won't
  3944. * get sync() from the umount, so we need to make sure any ordered
  3945. * extents that haven't had their dirty pages IO start writeout yet
  3946. * actually get run and error out properly.
  3947. */
  3948. btrfs_wait_ordered_roots(fs_info, U64_MAX, NULL);
  3949. }
  3950. static void btrfs_destroy_delayed_refs(struct btrfs_transaction *trans,
  3951. struct btrfs_fs_info *fs_info)
  3952. {
  3953. struct rb_node *node;
  3954. struct btrfs_delayed_ref_root *delayed_refs = &trans->delayed_refs;
  3955. struct btrfs_delayed_ref_node *ref;
  3956. spin_lock(&delayed_refs->lock);
  3957. while ((node = rb_first_cached(&delayed_refs->href_root)) != NULL) {
  3958. struct btrfs_delayed_ref_head *head;
  3959. struct rb_node *n;
  3960. bool pin_bytes = false;
  3961. head = rb_entry(node, struct btrfs_delayed_ref_head,
  3962. href_node);
  3963. if (btrfs_delayed_ref_lock(delayed_refs, head))
  3964. continue;
  3965. spin_lock(&head->lock);
  3966. while ((n = rb_first_cached(&head->ref_tree)) != NULL) {
  3967. ref = rb_entry(n, struct btrfs_delayed_ref_node,
  3968. ref_node);
  3969. rb_erase_cached(&ref->ref_node, &head->ref_tree);
  3970. RB_CLEAR_NODE(&ref->ref_node);
  3971. if (!list_empty(&ref->add_list))
  3972. list_del(&ref->add_list);
  3973. atomic_dec(&delayed_refs->num_entries);
  3974. btrfs_put_delayed_ref(ref);
  3975. btrfs_delayed_refs_rsv_release(fs_info, 1, 0);
  3976. }
  3977. if (head->must_insert_reserved)
  3978. pin_bytes = true;
  3979. btrfs_free_delayed_extent_op(head->extent_op);
  3980. btrfs_delete_ref_head(delayed_refs, head);
  3981. spin_unlock(&head->lock);
  3982. spin_unlock(&delayed_refs->lock);
  3983. mutex_unlock(&head->mutex);
  3984. if (pin_bytes) {
  3985. struct btrfs_block_group *cache;
  3986. cache = btrfs_lookup_block_group(fs_info, head->bytenr);
  3987. BUG_ON(!cache);
  3988. spin_lock(&cache->space_info->lock);
  3989. spin_lock(&cache->lock);
  3990. cache->pinned += head->num_bytes;
  3991. btrfs_space_info_update_bytes_pinned(fs_info,
  3992. cache->space_info, head->num_bytes);
  3993. cache->reserved -= head->num_bytes;
  3994. cache->space_info->bytes_reserved -= head->num_bytes;
  3995. spin_unlock(&cache->lock);
  3996. spin_unlock(&cache->space_info->lock);
  3997. btrfs_put_block_group(cache);
  3998. btrfs_error_unpin_extent_range(fs_info, head->bytenr,
  3999. head->bytenr + head->num_bytes - 1);
  4000. }
  4001. btrfs_cleanup_ref_head_accounting(fs_info, delayed_refs, head);
  4002. btrfs_put_delayed_ref_head(head);
  4003. cond_resched();
  4004. spin_lock(&delayed_refs->lock);
  4005. }
  4006. btrfs_qgroup_destroy_extent_records(trans);
  4007. spin_unlock(&delayed_refs->lock);
  4008. }
  4009. static void btrfs_destroy_delalloc_inodes(struct btrfs_root *root)
  4010. {
  4011. struct btrfs_inode *btrfs_inode;
  4012. LIST_HEAD(splice);
  4013. spin_lock(&root->delalloc_lock);
  4014. list_splice_init(&root->delalloc_inodes, &splice);
  4015. while (!list_empty(&splice)) {
  4016. struct inode *inode = NULL;
  4017. btrfs_inode = list_first_entry(&splice, struct btrfs_inode,
  4018. delalloc_inodes);
  4019. btrfs_del_delalloc_inode(btrfs_inode);
  4020. spin_unlock(&root->delalloc_lock);
  4021. /*
  4022. * Make sure we get a live inode and that it'll not disappear
  4023. * meanwhile.
  4024. */
  4025. inode = igrab(&btrfs_inode->vfs_inode);
  4026. if (inode) {
  4027. unsigned int nofs_flag;
  4028. nofs_flag = memalloc_nofs_save();
  4029. invalidate_inode_pages2(inode->i_mapping);
  4030. memalloc_nofs_restore(nofs_flag);
  4031. iput(inode);
  4032. }
  4033. spin_lock(&root->delalloc_lock);
  4034. }
  4035. spin_unlock(&root->delalloc_lock);
  4036. }
  4037. static void btrfs_destroy_all_delalloc_inodes(struct btrfs_fs_info *fs_info)
  4038. {
  4039. struct btrfs_root *root;
  4040. LIST_HEAD(splice);
  4041. spin_lock(&fs_info->delalloc_root_lock);
  4042. list_splice_init(&fs_info->delalloc_roots, &splice);
  4043. while (!list_empty(&splice)) {
  4044. root = list_first_entry(&splice, struct btrfs_root,
  4045. delalloc_root);
  4046. root = btrfs_grab_root(root);
  4047. BUG_ON(!root);
  4048. spin_unlock(&fs_info->delalloc_root_lock);
  4049. btrfs_destroy_delalloc_inodes(root);
  4050. btrfs_put_root(root);
  4051. spin_lock(&fs_info->delalloc_root_lock);
  4052. }
  4053. spin_unlock(&fs_info->delalloc_root_lock);
  4054. }
  4055. static void btrfs_destroy_marked_extents(struct btrfs_fs_info *fs_info,
  4056. struct extent_io_tree *dirty_pages,
  4057. int mark)
  4058. {
  4059. struct extent_buffer *eb;
  4060. u64 start = 0;
  4061. u64 end;
  4062. while (find_first_extent_bit(dirty_pages, start, &start, &end,
  4063. mark, NULL)) {
  4064. clear_extent_bits(dirty_pages, start, end, mark);
  4065. while (start <= end) {
  4066. eb = find_extent_buffer(fs_info, start);
  4067. start += fs_info->nodesize;
  4068. if (!eb)
  4069. continue;
  4070. btrfs_tree_lock(eb);
  4071. wait_on_extent_buffer_writeback(eb);
  4072. btrfs_clear_buffer_dirty(NULL, eb);
  4073. btrfs_tree_unlock(eb);
  4074. free_extent_buffer_stale(eb);
  4075. }
  4076. }
  4077. }
  4078. static void btrfs_destroy_pinned_extent(struct btrfs_fs_info *fs_info,
  4079. struct extent_io_tree *unpin)
  4080. {
  4081. u64 start;
  4082. u64 end;
  4083. while (1) {
  4084. struct extent_state *cached_state = NULL;
  4085. /*
  4086. * The btrfs_finish_extent_commit() may get the same range as
  4087. * ours between find_first_extent_bit and clear_extent_dirty.
  4088. * Hence, hold the unused_bg_unpin_mutex to avoid double unpin
  4089. * the same extent range.
  4090. */
  4091. mutex_lock(&fs_info->unused_bg_unpin_mutex);
  4092. if (!find_first_extent_bit(unpin, 0, &start, &end,
  4093. EXTENT_DIRTY, &cached_state)) {
  4094. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  4095. break;
  4096. }
  4097. clear_extent_dirty(unpin, start, end, &cached_state);
  4098. free_extent_state(cached_state);
  4099. btrfs_error_unpin_extent_range(fs_info, start, end);
  4100. mutex_unlock(&fs_info->unused_bg_unpin_mutex);
  4101. cond_resched();
  4102. }
  4103. }
  4104. static void btrfs_cleanup_bg_io(struct btrfs_block_group *cache)
  4105. {
  4106. struct inode *inode;
  4107. inode = cache->io_ctl.inode;
  4108. if (inode) {
  4109. unsigned int nofs_flag;
  4110. nofs_flag = memalloc_nofs_save();
  4111. invalidate_inode_pages2(inode->i_mapping);
  4112. memalloc_nofs_restore(nofs_flag);
  4113. BTRFS_I(inode)->generation = 0;
  4114. cache->io_ctl.inode = NULL;
  4115. iput(inode);
  4116. }
  4117. ASSERT(cache->io_ctl.pages == NULL);
  4118. btrfs_put_block_group(cache);
  4119. }
  4120. void btrfs_cleanup_dirty_bgs(struct btrfs_transaction *cur_trans,
  4121. struct btrfs_fs_info *fs_info)
  4122. {
  4123. struct btrfs_block_group *cache;
  4124. spin_lock(&cur_trans->dirty_bgs_lock);
  4125. while (!list_empty(&cur_trans->dirty_bgs)) {
  4126. cache = list_first_entry(&cur_trans->dirty_bgs,
  4127. struct btrfs_block_group,
  4128. dirty_list);
  4129. if (!list_empty(&cache->io_list)) {
  4130. spin_unlock(&cur_trans->dirty_bgs_lock);
  4131. list_del_init(&cache->io_list);
  4132. btrfs_cleanup_bg_io(cache);
  4133. spin_lock(&cur_trans->dirty_bgs_lock);
  4134. }
  4135. list_del_init(&cache->dirty_list);
  4136. spin_lock(&cache->lock);
  4137. cache->disk_cache_state = BTRFS_DC_ERROR;
  4138. spin_unlock(&cache->lock);
  4139. spin_unlock(&cur_trans->dirty_bgs_lock);
  4140. btrfs_put_block_group(cache);
  4141. btrfs_dec_delayed_refs_rsv_bg_updates(fs_info);
  4142. spin_lock(&cur_trans->dirty_bgs_lock);
  4143. }
  4144. spin_unlock(&cur_trans->dirty_bgs_lock);
  4145. /*
  4146. * Refer to the definition of io_bgs member for details why it's safe
  4147. * to use it without any locking
  4148. */
  4149. while (!list_empty(&cur_trans->io_bgs)) {
  4150. cache = list_first_entry(&cur_trans->io_bgs,
  4151. struct btrfs_block_group,
  4152. io_list);
  4153. list_del_init(&cache->io_list);
  4154. spin_lock(&cache->lock);
  4155. cache->disk_cache_state = BTRFS_DC_ERROR;
  4156. spin_unlock(&cache->lock);
  4157. btrfs_cleanup_bg_io(cache);
  4158. }
  4159. }
  4160. static void btrfs_free_all_qgroup_pertrans(struct btrfs_fs_info *fs_info)
  4161. {
  4162. struct btrfs_root *gang[8];
  4163. int i;
  4164. int ret;
  4165. spin_lock(&fs_info->fs_roots_radix_lock);
  4166. while (1) {
  4167. ret = radix_tree_gang_lookup_tag(&fs_info->fs_roots_radix,
  4168. (void **)gang, 0,
  4169. ARRAY_SIZE(gang),
  4170. BTRFS_ROOT_TRANS_TAG);
  4171. if (ret == 0)
  4172. break;
  4173. for (i = 0; i < ret; i++) {
  4174. struct btrfs_root *root = gang[i];
  4175. btrfs_qgroup_free_meta_all_pertrans(root);
  4176. radix_tree_tag_clear(&fs_info->fs_roots_radix,
  4177. (unsigned long)btrfs_root_id(root),
  4178. BTRFS_ROOT_TRANS_TAG);
  4179. }
  4180. }
  4181. spin_unlock(&fs_info->fs_roots_radix_lock);
  4182. }
  4183. void btrfs_cleanup_one_transaction(struct btrfs_transaction *cur_trans,
  4184. struct btrfs_fs_info *fs_info)
  4185. {
  4186. struct btrfs_device *dev, *tmp;
  4187. btrfs_cleanup_dirty_bgs(cur_trans, fs_info);
  4188. ASSERT(list_empty(&cur_trans->dirty_bgs));
  4189. ASSERT(list_empty(&cur_trans->io_bgs));
  4190. list_for_each_entry_safe(dev, tmp, &cur_trans->dev_update_list,
  4191. post_commit_list) {
  4192. list_del_init(&dev->post_commit_list);
  4193. }
  4194. btrfs_destroy_delayed_refs(cur_trans, fs_info);
  4195. cur_trans->state = TRANS_STATE_COMMIT_START;
  4196. wake_up(&fs_info->transaction_blocked_wait);
  4197. cur_trans->state = TRANS_STATE_UNBLOCKED;
  4198. wake_up(&fs_info->transaction_wait);
  4199. btrfs_destroy_marked_extents(fs_info, &cur_trans->dirty_pages,
  4200. EXTENT_DIRTY);
  4201. btrfs_destroy_pinned_extent(fs_info, &cur_trans->pinned_extents);
  4202. cur_trans->state =TRANS_STATE_COMPLETED;
  4203. wake_up(&cur_trans->commit_wait);
  4204. }
  4205. static int btrfs_cleanup_transaction(struct btrfs_fs_info *fs_info)
  4206. {
  4207. struct btrfs_transaction *t;
  4208. mutex_lock(&fs_info->transaction_kthread_mutex);
  4209. spin_lock(&fs_info->trans_lock);
  4210. while (!list_empty(&fs_info->trans_list)) {
  4211. t = list_first_entry(&fs_info->trans_list,
  4212. struct btrfs_transaction, list);
  4213. if (t->state >= TRANS_STATE_COMMIT_PREP) {
  4214. refcount_inc(&t->use_count);
  4215. spin_unlock(&fs_info->trans_lock);
  4216. btrfs_wait_for_commit(fs_info, t->transid);
  4217. btrfs_put_transaction(t);
  4218. spin_lock(&fs_info->trans_lock);
  4219. continue;
  4220. }
  4221. if (t == fs_info->running_transaction) {
  4222. t->state = TRANS_STATE_COMMIT_DOING;
  4223. spin_unlock(&fs_info->trans_lock);
  4224. /*
  4225. * We wait for 0 num_writers since we don't hold a trans
  4226. * handle open currently for this transaction.
  4227. */
  4228. wait_event(t->writer_wait,
  4229. atomic_read(&t->num_writers) == 0);
  4230. } else {
  4231. spin_unlock(&fs_info->trans_lock);
  4232. }
  4233. btrfs_cleanup_one_transaction(t, fs_info);
  4234. spin_lock(&fs_info->trans_lock);
  4235. if (t == fs_info->running_transaction)
  4236. fs_info->running_transaction = NULL;
  4237. list_del_init(&t->list);
  4238. spin_unlock(&fs_info->trans_lock);
  4239. btrfs_put_transaction(t);
  4240. trace_btrfs_transaction_commit(fs_info);
  4241. spin_lock(&fs_info->trans_lock);
  4242. }
  4243. spin_unlock(&fs_info->trans_lock);
  4244. btrfs_destroy_all_ordered_extents(fs_info);
  4245. btrfs_destroy_delayed_inodes(fs_info);
  4246. btrfs_assert_delayed_root_empty(fs_info);
  4247. btrfs_destroy_all_delalloc_inodes(fs_info);
  4248. btrfs_drop_all_logs(fs_info);
  4249. btrfs_free_all_qgroup_pertrans(fs_info);
  4250. mutex_unlock(&fs_info->transaction_kthread_mutex);
  4251. return 0;
  4252. }
  4253. int btrfs_init_root_free_objectid(struct btrfs_root *root)
  4254. {
  4255. struct btrfs_path *path;
  4256. int ret;
  4257. struct extent_buffer *l;
  4258. struct btrfs_key search_key;
  4259. struct btrfs_key found_key;
  4260. int slot;
  4261. path = btrfs_alloc_path();
  4262. if (!path)
  4263. return -ENOMEM;
  4264. search_key.objectid = BTRFS_LAST_FREE_OBJECTID;
  4265. search_key.type = -1;
  4266. search_key.offset = (u64)-1;
  4267. ret = btrfs_search_slot(NULL, root, &search_key, path, 0, 0);
  4268. if (ret < 0)
  4269. goto error;
  4270. if (ret == 0) {
  4271. /*
  4272. * Key with offset -1 found, there would have to exist a root
  4273. * with such id, but this is out of valid range.
  4274. */
  4275. ret = -EUCLEAN;
  4276. goto error;
  4277. }
  4278. if (path->slots[0] > 0) {
  4279. slot = path->slots[0] - 1;
  4280. l = path->nodes[0];
  4281. btrfs_item_key_to_cpu(l, &found_key, slot);
  4282. root->free_objectid = max_t(u64, found_key.objectid + 1,
  4283. BTRFS_FIRST_FREE_OBJECTID);
  4284. } else {
  4285. root->free_objectid = BTRFS_FIRST_FREE_OBJECTID;
  4286. }
  4287. ret = 0;
  4288. error:
  4289. btrfs_free_path(path);
  4290. return ret;
  4291. }
  4292. int btrfs_get_free_objectid(struct btrfs_root *root, u64 *objectid)
  4293. {
  4294. int ret;
  4295. mutex_lock(&root->objectid_mutex);
  4296. if (unlikely(root->free_objectid >= BTRFS_LAST_FREE_OBJECTID)) {
  4297. btrfs_warn(root->fs_info,
  4298. "the objectid of root %llu reaches its highest value",
  4299. btrfs_root_id(root));
  4300. ret = -ENOSPC;
  4301. goto out;
  4302. }
  4303. *objectid = root->free_objectid++;
  4304. ret = 0;
  4305. out:
  4306. mutex_unlock(&root->objectid_mutex);
  4307. return ret;
  4308. }