disk-io.c 110 KB

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