disk-io.c 125 KB

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