disk-io.c 123 KB

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