disk-io.c 124 KB

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