volumes.c 106 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262
  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/sched.h>
  19. #include <linux/bio.h>
  20. #include <linux/slab.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/blkdev.h>
  23. #include <linux/random.h>
  24. #include <linux/iocontext.h>
  25. #include <linux/capability.h>
  26. #include <asm/div64.h>
  27. #include "compat.h"
  28. #include "ctree.h"
  29. #include "extent_map.h"
  30. #include "disk-io.h"
  31. #include "transaction.h"
  32. #include "print-tree.h"
  33. #include "volumes.h"
  34. #include "async-thread.h"
  35. static int init_first_rw_device(struct btrfs_trans_handle *trans,
  36. struct btrfs_root *root,
  37. struct btrfs_device *device);
  38. static int btrfs_relocate_sys_chunks(struct btrfs_root *root);
  39. static DEFINE_MUTEX(uuid_mutex);
  40. static LIST_HEAD(fs_uuids);
  41. static void lock_chunks(struct btrfs_root *root)
  42. {
  43. mutex_lock(&root->fs_info->chunk_mutex);
  44. }
  45. static void unlock_chunks(struct btrfs_root *root)
  46. {
  47. mutex_unlock(&root->fs_info->chunk_mutex);
  48. }
  49. static void free_fs_devices(struct btrfs_fs_devices *fs_devices)
  50. {
  51. struct btrfs_device *device;
  52. WARN_ON(fs_devices->opened);
  53. while (!list_empty(&fs_devices->devices)) {
  54. device = list_entry(fs_devices->devices.next,
  55. struct btrfs_device, dev_list);
  56. list_del(&device->dev_list);
  57. kfree(device->name);
  58. kfree(device);
  59. }
  60. kfree(fs_devices);
  61. }
  62. int btrfs_cleanup_fs_uuids(void)
  63. {
  64. struct btrfs_fs_devices *fs_devices;
  65. while (!list_empty(&fs_uuids)) {
  66. fs_devices = list_entry(fs_uuids.next,
  67. struct btrfs_fs_devices, list);
  68. list_del(&fs_devices->list);
  69. free_fs_devices(fs_devices);
  70. }
  71. return 0;
  72. }
  73. static noinline struct btrfs_device *__find_device(struct list_head *head,
  74. u64 devid, u8 *uuid)
  75. {
  76. struct btrfs_device *dev;
  77. list_for_each_entry(dev, head, dev_list) {
  78. if (dev->devid == devid &&
  79. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  80. return dev;
  81. }
  82. }
  83. return NULL;
  84. }
  85. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  86. {
  87. struct btrfs_fs_devices *fs_devices;
  88. list_for_each_entry(fs_devices, &fs_uuids, list) {
  89. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  90. return fs_devices;
  91. }
  92. return NULL;
  93. }
  94. static void requeue_list(struct btrfs_pending_bios *pending_bios,
  95. struct bio *head, struct bio *tail)
  96. {
  97. struct bio *old_head;
  98. old_head = pending_bios->head;
  99. pending_bios->head = head;
  100. if (pending_bios->tail)
  101. tail->bi_next = old_head;
  102. else
  103. pending_bios->tail = tail;
  104. }
  105. /*
  106. * we try to collect pending bios for a device so we don't get a large
  107. * number of procs sending bios down to the same device. This greatly
  108. * improves the schedulers ability to collect and merge the bios.
  109. *
  110. * But, it also turns into a long list of bios to process and that is sure
  111. * to eventually make the worker thread block. The solution here is to
  112. * make some progress and then put this work struct back at the end of
  113. * the list if the block device is congested. This way, multiple devices
  114. * can make progress from a single worker thread.
  115. */
  116. static noinline int run_scheduled_bios(struct btrfs_device *device)
  117. {
  118. struct bio *pending;
  119. struct backing_dev_info *bdi;
  120. struct btrfs_fs_info *fs_info;
  121. struct btrfs_pending_bios *pending_bios;
  122. struct bio *tail;
  123. struct bio *cur;
  124. int again = 0;
  125. unsigned long num_run;
  126. unsigned long batch_run = 0;
  127. unsigned long limit;
  128. unsigned long last_waited = 0;
  129. int force_reg = 0;
  130. int sync_pending = 0;
  131. struct blk_plug plug;
  132. /*
  133. * this function runs all the bios we've collected for
  134. * a particular device. We don't want to wander off to
  135. * another device without first sending all of these down.
  136. * So, setup a plug here and finish it off before we return
  137. */
  138. blk_start_plug(&plug);
  139. bdi = blk_get_backing_dev_info(device->bdev);
  140. fs_info = device->dev_root->fs_info;
  141. limit = btrfs_async_submit_limit(fs_info);
  142. limit = limit * 2 / 3;
  143. loop:
  144. spin_lock(&device->io_lock);
  145. loop_lock:
  146. num_run = 0;
  147. /* take all the bios off the list at once and process them
  148. * later on (without the lock held). But, remember the
  149. * tail and other pointers so the bios can be properly reinserted
  150. * into the list if we hit congestion
  151. */
  152. if (!force_reg && device->pending_sync_bios.head) {
  153. pending_bios = &device->pending_sync_bios;
  154. force_reg = 1;
  155. } else {
  156. pending_bios = &device->pending_bios;
  157. force_reg = 0;
  158. }
  159. pending = pending_bios->head;
  160. tail = pending_bios->tail;
  161. WARN_ON(pending && !tail);
  162. /*
  163. * if pending was null this time around, no bios need processing
  164. * at all and we can stop. Otherwise it'll loop back up again
  165. * and do an additional check so no bios are missed.
  166. *
  167. * device->running_pending is used to synchronize with the
  168. * schedule_bio code.
  169. */
  170. if (device->pending_sync_bios.head == NULL &&
  171. device->pending_bios.head == NULL) {
  172. again = 0;
  173. device->running_pending = 0;
  174. } else {
  175. again = 1;
  176. device->running_pending = 1;
  177. }
  178. pending_bios->head = NULL;
  179. pending_bios->tail = NULL;
  180. spin_unlock(&device->io_lock);
  181. while (pending) {
  182. rmb();
  183. /* we want to work on both lists, but do more bios on the
  184. * sync list than the regular list
  185. */
  186. if ((num_run > 32 &&
  187. pending_bios != &device->pending_sync_bios &&
  188. device->pending_sync_bios.head) ||
  189. (num_run > 64 && pending_bios == &device->pending_sync_bios &&
  190. device->pending_bios.head)) {
  191. spin_lock(&device->io_lock);
  192. requeue_list(pending_bios, pending, tail);
  193. goto loop_lock;
  194. }
  195. cur = pending;
  196. pending = pending->bi_next;
  197. cur->bi_next = NULL;
  198. atomic_dec(&fs_info->nr_async_bios);
  199. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  200. waitqueue_active(&fs_info->async_submit_wait))
  201. wake_up(&fs_info->async_submit_wait);
  202. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  203. /*
  204. * if we're doing the sync list, record that our
  205. * plug has some sync requests on it
  206. *
  207. * If we're doing the regular list and there are
  208. * sync requests sitting around, unplug before
  209. * we add more
  210. */
  211. if (pending_bios == &device->pending_sync_bios) {
  212. sync_pending = 1;
  213. } else if (sync_pending) {
  214. blk_finish_plug(&plug);
  215. blk_start_plug(&plug);
  216. sync_pending = 0;
  217. }
  218. submit_bio(cur->bi_rw, cur);
  219. num_run++;
  220. batch_run++;
  221. if (need_resched())
  222. cond_resched();
  223. /*
  224. * we made progress, there is more work to do and the bdi
  225. * is now congested. Back off and let other work structs
  226. * run instead
  227. */
  228. if (pending && bdi_write_congested(bdi) && batch_run > 8 &&
  229. fs_info->fs_devices->open_devices > 1) {
  230. struct io_context *ioc;
  231. ioc = current->io_context;
  232. /*
  233. * the main goal here is that we don't want to
  234. * block if we're going to be able to submit
  235. * more requests without blocking.
  236. *
  237. * This code does two great things, it pokes into
  238. * the elevator code from a filesystem _and_
  239. * it makes assumptions about how batching works.
  240. */
  241. if (ioc && ioc->nr_batch_requests > 0 &&
  242. time_before(jiffies, ioc->last_waited + HZ/50UL) &&
  243. (last_waited == 0 ||
  244. ioc->last_waited == last_waited)) {
  245. /*
  246. * we want to go through our batch of
  247. * requests and stop. So, we copy out
  248. * the ioc->last_waited time and test
  249. * against it before looping
  250. */
  251. last_waited = ioc->last_waited;
  252. if (need_resched())
  253. cond_resched();
  254. continue;
  255. }
  256. spin_lock(&device->io_lock);
  257. requeue_list(pending_bios, pending, tail);
  258. device->running_pending = 1;
  259. spin_unlock(&device->io_lock);
  260. btrfs_requeue_work(&device->work);
  261. goto done;
  262. }
  263. /* unplug every 64 requests just for good measure */
  264. if (batch_run % 64 == 0) {
  265. blk_finish_plug(&plug);
  266. blk_start_plug(&plug);
  267. sync_pending = 0;
  268. }
  269. }
  270. cond_resched();
  271. if (again)
  272. goto loop;
  273. spin_lock(&device->io_lock);
  274. if (device->pending_bios.head || device->pending_sync_bios.head)
  275. goto loop_lock;
  276. spin_unlock(&device->io_lock);
  277. done:
  278. blk_finish_plug(&plug);
  279. return 0;
  280. }
  281. static void pending_bios_fn(struct btrfs_work *work)
  282. {
  283. struct btrfs_device *device;
  284. device = container_of(work, struct btrfs_device, work);
  285. run_scheduled_bios(device);
  286. }
  287. static noinline int device_list_add(const char *path,
  288. struct btrfs_super_block *disk_super,
  289. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  290. {
  291. struct btrfs_device *device;
  292. struct btrfs_fs_devices *fs_devices;
  293. u64 found_transid = btrfs_super_generation(disk_super);
  294. char *name;
  295. fs_devices = find_fsid(disk_super->fsid);
  296. if (!fs_devices) {
  297. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  298. if (!fs_devices)
  299. return -ENOMEM;
  300. INIT_LIST_HEAD(&fs_devices->devices);
  301. INIT_LIST_HEAD(&fs_devices->alloc_list);
  302. list_add(&fs_devices->list, &fs_uuids);
  303. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  304. fs_devices->latest_devid = devid;
  305. fs_devices->latest_trans = found_transid;
  306. mutex_init(&fs_devices->device_list_mutex);
  307. device = NULL;
  308. } else {
  309. device = __find_device(&fs_devices->devices, devid,
  310. disk_super->dev_item.uuid);
  311. }
  312. if (!device) {
  313. if (fs_devices->opened)
  314. return -EBUSY;
  315. device = kzalloc(sizeof(*device), GFP_NOFS);
  316. if (!device) {
  317. /* we can safely leave the fs_devices entry around */
  318. return -ENOMEM;
  319. }
  320. device->devid = devid;
  321. device->work.func = pending_bios_fn;
  322. memcpy(device->uuid, disk_super->dev_item.uuid,
  323. BTRFS_UUID_SIZE);
  324. spin_lock_init(&device->io_lock);
  325. device->name = kstrdup(path, GFP_NOFS);
  326. if (!device->name) {
  327. kfree(device);
  328. return -ENOMEM;
  329. }
  330. INIT_LIST_HEAD(&device->dev_alloc_list);
  331. /* init readahead state */
  332. spin_lock_init(&device->reada_lock);
  333. device->reada_curr_zone = NULL;
  334. atomic_set(&device->reada_in_flight, 0);
  335. device->reada_next = 0;
  336. INIT_RADIX_TREE(&device->reada_zones, GFP_NOFS & ~__GFP_WAIT);
  337. INIT_RADIX_TREE(&device->reada_extents, GFP_NOFS & ~__GFP_WAIT);
  338. mutex_lock(&fs_devices->device_list_mutex);
  339. list_add_rcu(&device->dev_list, &fs_devices->devices);
  340. mutex_unlock(&fs_devices->device_list_mutex);
  341. device->fs_devices = fs_devices;
  342. fs_devices->num_devices++;
  343. } else if (!device->name || strcmp(device->name, path)) {
  344. name = kstrdup(path, GFP_NOFS);
  345. if (!name)
  346. return -ENOMEM;
  347. kfree(device->name);
  348. device->name = name;
  349. if (device->missing) {
  350. fs_devices->missing_devices--;
  351. device->missing = 0;
  352. }
  353. }
  354. if (found_transid > fs_devices->latest_trans) {
  355. fs_devices->latest_devid = devid;
  356. fs_devices->latest_trans = found_transid;
  357. }
  358. *fs_devices_ret = fs_devices;
  359. return 0;
  360. }
  361. static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
  362. {
  363. struct btrfs_fs_devices *fs_devices;
  364. struct btrfs_device *device;
  365. struct btrfs_device *orig_dev;
  366. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  367. if (!fs_devices)
  368. return ERR_PTR(-ENOMEM);
  369. INIT_LIST_HEAD(&fs_devices->devices);
  370. INIT_LIST_HEAD(&fs_devices->alloc_list);
  371. INIT_LIST_HEAD(&fs_devices->list);
  372. mutex_init(&fs_devices->device_list_mutex);
  373. fs_devices->latest_devid = orig->latest_devid;
  374. fs_devices->latest_trans = orig->latest_trans;
  375. memcpy(fs_devices->fsid, orig->fsid, sizeof(fs_devices->fsid));
  376. /* We have held the volume lock, it is safe to get the devices. */
  377. list_for_each_entry(orig_dev, &orig->devices, dev_list) {
  378. device = kzalloc(sizeof(*device), GFP_NOFS);
  379. if (!device)
  380. goto error;
  381. device->name = kstrdup(orig_dev->name, GFP_NOFS);
  382. if (!device->name) {
  383. kfree(device);
  384. goto error;
  385. }
  386. device->devid = orig_dev->devid;
  387. device->work.func = pending_bios_fn;
  388. memcpy(device->uuid, orig_dev->uuid, sizeof(device->uuid));
  389. spin_lock_init(&device->io_lock);
  390. INIT_LIST_HEAD(&device->dev_list);
  391. INIT_LIST_HEAD(&device->dev_alloc_list);
  392. list_add(&device->dev_list, &fs_devices->devices);
  393. device->fs_devices = fs_devices;
  394. fs_devices->num_devices++;
  395. }
  396. return fs_devices;
  397. error:
  398. free_fs_devices(fs_devices);
  399. return ERR_PTR(-ENOMEM);
  400. }
  401. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  402. {
  403. struct btrfs_device *device, *next;
  404. mutex_lock(&uuid_mutex);
  405. again:
  406. /* This is the initialized path, it is safe to release the devices. */
  407. list_for_each_entry_safe(device, next, &fs_devices->devices, dev_list) {
  408. if (device->in_fs_metadata)
  409. continue;
  410. if (device->bdev) {
  411. blkdev_put(device->bdev, device->mode);
  412. device->bdev = NULL;
  413. fs_devices->open_devices--;
  414. }
  415. if (device->writeable) {
  416. list_del_init(&device->dev_alloc_list);
  417. device->writeable = 0;
  418. fs_devices->rw_devices--;
  419. }
  420. list_del_init(&device->dev_list);
  421. fs_devices->num_devices--;
  422. kfree(device->name);
  423. kfree(device);
  424. }
  425. if (fs_devices->seed) {
  426. fs_devices = fs_devices->seed;
  427. goto again;
  428. }
  429. mutex_unlock(&uuid_mutex);
  430. return 0;
  431. }
  432. static void __free_device(struct work_struct *work)
  433. {
  434. struct btrfs_device *device;
  435. device = container_of(work, struct btrfs_device, rcu_work);
  436. if (device->bdev)
  437. blkdev_put(device->bdev, device->mode);
  438. kfree(device->name);
  439. kfree(device);
  440. }
  441. static void free_device(struct rcu_head *head)
  442. {
  443. struct btrfs_device *device;
  444. device = container_of(head, struct btrfs_device, rcu);
  445. INIT_WORK(&device->rcu_work, __free_device);
  446. schedule_work(&device->rcu_work);
  447. }
  448. static int __btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  449. {
  450. struct btrfs_device *device;
  451. if (--fs_devices->opened > 0)
  452. return 0;
  453. mutex_lock(&fs_devices->device_list_mutex);
  454. list_for_each_entry(device, &fs_devices->devices, dev_list) {
  455. struct btrfs_device *new_device;
  456. if (device->bdev)
  457. fs_devices->open_devices--;
  458. if (device->writeable) {
  459. list_del_init(&device->dev_alloc_list);
  460. fs_devices->rw_devices--;
  461. }
  462. if (device->can_discard)
  463. fs_devices->num_can_discard--;
  464. new_device = kmalloc(sizeof(*new_device), GFP_NOFS);
  465. BUG_ON(!new_device);
  466. memcpy(new_device, device, sizeof(*new_device));
  467. new_device->name = kstrdup(device->name, GFP_NOFS);
  468. BUG_ON(device->name && !new_device->name);
  469. new_device->bdev = NULL;
  470. new_device->writeable = 0;
  471. new_device->in_fs_metadata = 0;
  472. new_device->can_discard = 0;
  473. list_replace_rcu(&device->dev_list, &new_device->dev_list);
  474. call_rcu(&device->rcu, free_device);
  475. }
  476. mutex_unlock(&fs_devices->device_list_mutex);
  477. WARN_ON(fs_devices->open_devices);
  478. WARN_ON(fs_devices->rw_devices);
  479. fs_devices->opened = 0;
  480. fs_devices->seeding = 0;
  481. return 0;
  482. }
  483. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  484. {
  485. struct btrfs_fs_devices *seed_devices = NULL;
  486. int ret;
  487. mutex_lock(&uuid_mutex);
  488. ret = __btrfs_close_devices(fs_devices);
  489. if (!fs_devices->opened) {
  490. seed_devices = fs_devices->seed;
  491. fs_devices->seed = NULL;
  492. }
  493. mutex_unlock(&uuid_mutex);
  494. while (seed_devices) {
  495. fs_devices = seed_devices;
  496. seed_devices = fs_devices->seed;
  497. __btrfs_close_devices(fs_devices);
  498. free_fs_devices(fs_devices);
  499. }
  500. return ret;
  501. }
  502. static int __btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  503. fmode_t flags, void *holder)
  504. {
  505. struct request_queue *q;
  506. struct block_device *bdev;
  507. struct list_head *head = &fs_devices->devices;
  508. struct btrfs_device *device;
  509. struct block_device *latest_bdev = NULL;
  510. struct buffer_head *bh;
  511. struct btrfs_super_block *disk_super;
  512. u64 latest_devid = 0;
  513. u64 latest_transid = 0;
  514. u64 devid;
  515. int seeding = 1;
  516. int ret = 0;
  517. flags |= FMODE_EXCL;
  518. list_for_each_entry(device, head, dev_list) {
  519. if (device->bdev)
  520. continue;
  521. if (!device->name)
  522. continue;
  523. bdev = blkdev_get_by_path(device->name, flags, holder);
  524. if (IS_ERR(bdev)) {
  525. printk(KERN_INFO "open %s failed\n", device->name);
  526. goto error;
  527. }
  528. set_blocksize(bdev, 4096);
  529. bh = btrfs_read_dev_super(bdev);
  530. if (!bh)
  531. goto error_close;
  532. disk_super = (struct btrfs_super_block *)bh->b_data;
  533. devid = btrfs_stack_device_id(&disk_super->dev_item);
  534. if (devid != device->devid)
  535. goto error_brelse;
  536. if (memcmp(device->uuid, disk_super->dev_item.uuid,
  537. BTRFS_UUID_SIZE))
  538. goto error_brelse;
  539. device->generation = btrfs_super_generation(disk_super);
  540. if (!latest_transid || device->generation > latest_transid) {
  541. latest_devid = devid;
  542. latest_transid = device->generation;
  543. latest_bdev = bdev;
  544. }
  545. if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
  546. device->writeable = 0;
  547. } else {
  548. device->writeable = !bdev_read_only(bdev);
  549. seeding = 0;
  550. }
  551. q = bdev_get_queue(bdev);
  552. if (blk_queue_discard(q)) {
  553. device->can_discard = 1;
  554. fs_devices->num_can_discard++;
  555. }
  556. device->bdev = bdev;
  557. device->in_fs_metadata = 0;
  558. device->mode = flags;
  559. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  560. fs_devices->rotating = 1;
  561. fs_devices->open_devices++;
  562. if (device->writeable) {
  563. fs_devices->rw_devices++;
  564. list_add(&device->dev_alloc_list,
  565. &fs_devices->alloc_list);
  566. }
  567. brelse(bh);
  568. continue;
  569. error_brelse:
  570. brelse(bh);
  571. error_close:
  572. blkdev_put(bdev, flags);
  573. error:
  574. continue;
  575. }
  576. if (fs_devices->open_devices == 0) {
  577. ret = -EINVAL;
  578. goto out;
  579. }
  580. fs_devices->seeding = seeding;
  581. fs_devices->opened = 1;
  582. fs_devices->latest_bdev = latest_bdev;
  583. fs_devices->latest_devid = latest_devid;
  584. fs_devices->latest_trans = latest_transid;
  585. fs_devices->total_rw_bytes = 0;
  586. out:
  587. return ret;
  588. }
  589. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  590. fmode_t flags, void *holder)
  591. {
  592. int ret;
  593. mutex_lock(&uuid_mutex);
  594. if (fs_devices->opened) {
  595. fs_devices->opened++;
  596. ret = 0;
  597. } else {
  598. ret = __btrfs_open_devices(fs_devices, flags, holder);
  599. }
  600. mutex_unlock(&uuid_mutex);
  601. return ret;
  602. }
  603. int btrfs_scan_one_device(const char *path, fmode_t flags, void *holder,
  604. struct btrfs_fs_devices **fs_devices_ret)
  605. {
  606. struct btrfs_super_block *disk_super;
  607. struct block_device *bdev;
  608. struct buffer_head *bh;
  609. int ret;
  610. u64 devid;
  611. u64 transid;
  612. mutex_lock(&uuid_mutex);
  613. flags |= FMODE_EXCL;
  614. bdev = blkdev_get_by_path(path, flags, holder);
  615. if (IS_ERR(bdev)) {
  616. ret = PTR_ERR(bdev);
  617. goto error;
  618. }
  619. ret = set_blocksize(bdev, 4096);
  620. if (ret)
  621. goto error_close;
  622. bh = btrfs_read_dev_super(bdev);
  623. if (!bh) {
  624. ret = -EINVAL;
  625. goto error_close;
  626. }
  627. disk_super = (struct btrfs_super_block *)bh->b_data;
  628. devid = btrfs_stack_device_id(&disk_super->dev_item);
  629. transid = btrfs_super_generation(disk_super);
  630. if (disk_super->label[0])
  631. printk(KERN_INFO "device label %s ", disk_super->label);
  632. else
  633. printk(KERN_INFO "device fsid %pU ", disk_super->fsid);
  634. printk(KERN_CONT "devid %llu transid %llu %s\n",
  635. (unsigned long long)devid, (unsigned long long)transid, path);
  636. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  637. brelse(bh);
  638. error_close:
  639. blkdev_put(bdev, flags);
  640. error:
  641. mutex_unlock(&uuid_mutex);
  642. return ret;
  643. }
  644. /* helper to account the used device space in the range */
  645. int btrfs_account_dev_extents_size(struct btrfs_device *device, u64 start,
  646. u64 end, u64 *length)
  647. {
  648. struct btrfs_key key;
  649. struct btrfs_root *root = device->dev_root;
  650. struct btrfs_dev_extent *dev_extent;
  651. struct btrfs_path *path;
  652. u64 extent_end;
  653. int ret;
  654. int slot;
  655. struct extent_buffer *l;
  656. *length = 0;
  657. if (start >= device->total_bytes)
  658. return 0;
  659. path = btrfs_alloc_path();
  660. if (!path)
  661. return -ENOMEM;
  662. path->reada = 2;
  663. key.objectid = device->devid;
  664. key.offset = start;
  665. key.type = BTRFS_DEV_EXTENT_KEY;
  666. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  667. if (ret < 0)
  668. goto out;
  669. if (ret > 0) {
  670. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  671. if (ret < 0)
  672. goto out;
  673. }
  674. while (1) {
  675. l = path->nodes[0];
  676. slot = path->slots[0];
  677. if (slot >= btrfs_header_nritems(l)) {
  678. ret = btrfs_next_leaf(root, path);
  679. if (ret == 0)
  680. continue;
  681. if (ret < 0)
  682. goto out;
  683. break;
  684. }
  685. btrfs_item_key_to_cpu(l, &key, slot);
  686. if (key.objectid < device->devid)
  687. goto next;
  688. if (key.objectid > device->devid)
  689. break;
  690. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  691. goto next;
  692. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  693. extent_end = key.offset + btrfs_dev_extent_length(l,
  694. dev_extent);
  695. if (key.offset <= start && extent_end > end) {
  696. *length = end - start + 1;
  697. break;
  698. } else if (key.offset <= start && extent_end > start)
  699. *length += extent_end - start;
  700. else if (key.offset > start && extent_end <= end)
  701. *length += extent_end - key.offset;
  702. else if (key.offset > start && key.offset <= end) {
  703. *length += end - key.offset + 1;
  704. break;
  705. } else if (key.offset > end)
  706. break;
  707. next:
  708. path->slots[0]++;
  709. }
  710. ret = 0;
  711. out:
  712. btrfs_free_path(path);
  713. return ret;
  714. }
  715. /*
  716. * find_free_dev_extent - find free space in the specified device
  717. * @trans: transaction handler
  718. * @device: the device which we search the free space in
  719. * @num_bytes: the size of the free space that we need
  720. * @start: store the start of the free space.
  721. * @len: the size of the free space. that we find, or the size of the max
  722. * free space if we don't find suitable free space
  723. *
  724. * this uses a pretty simple search, the expectation is that it is
  725. * called very infrequently and that a given device has a small number
  726. * of extents
  727. *
  728. * @start is used to store the start of the free space if we find. But if we
  729. * don't find suitable free space, it will be used to store the start position
  730. * of the max free space.
  731. *
  732. * @len is used to store the size of the free space that we find.
  733. * But if we don't find suitable free space, it is used to store the size of
  734. * the max free space.
  735. */
  736. int find_free_dev_extent(struct btrfs_trans_handle *trans,
  737. struct btrfs_device *device, u64 num_bytes,
  738. u64 *start, u64 *len)
  739. {
  740. struct btrfs_key key;
  741. struct btrfs_root *root = device->dev_root;
  742. struct btrfs_dev_extent *dev_extent;
  743. struct btrfs_path *path;
  744. u64 hole_size;
  745. u64 max_hole_start;
  746. u64 max_hole_size;
  747. u64 extent_end;
  748. u64 search_start;
  749. u64 search_end = device->total_bytes;
  750. int ret;
  751. int slot;
  752. struct extent_buffer *l;
  753. /* FIXME use last free of some kind */
  754. /* we don't want to overwrite the superblock on the drive,
  755. * so we make sure to start at an offset of at least 1MB
  756. */
  757. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  758. max_hole_start = search_start;
  759. max_hole_size = 0;
  760. hole_size = 0;
  761. if (search_start >= search_end) {
  762. ret = -ENOSPC;
  763. goto error;
  764. }
  765. path = btrfs_alloc_path();
  766. if (!path) {
  767. ret = -ENOMEM;
  768. goto error;
  769. }
  770. path->reada = 2;
  771. key.objectid = device->devid;
  772. key.offset = search_start;
  773. key.type = BTRFS_DEV_EXTENT_KEY;
  774. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  775. if (ret < 0)
  776. goto out;
  777. if (ret > 0) {
  778. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  779. if (ret < 0)
  780. goto out;
  781. }
  782. while (1) {
  783. l = path->nodes[0];
  784. slot = path->slots[0];
  785. if (slot >= btrfs_header_nritems(l)) {
  786. ret = btrfs_next_leaf(root, path);
  787. if (ret == 0)
  788. continue;
  789. if (ret < 0)
  790. goto out;
  791. break;
  792. }
  793. btrfs_item_key_to_cpu(l, &key, slot);
  794. if (key.objectid < device->devid)
  795. goto next;
  796. if (key.objectid > device->devid)
  797. break;
  798. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  799. goto next;
  800. if (key.offset > search_start) {
  801. hole_size = key.offset - search_start;
  802. if (hole_size > max_hole_size) {
  803. max_hole_start = search_start;
  804. max_hole_size = hole_size;
  805. }
  806. /*
  807. * If this free space is greater than which we need,
  808. * it must be the max free space that we have found
  809. * until now, so max_hole_start must point to the start
  810. * of this free space and the length of this free space
  811. * is stored in max_hole_size. Thus, we return
  812. * max_hole_start and max_hole_size and go back to the
  813. * caller.
  814. */
  815. if (hole_size >= num_bytes) {
  816. ret = 0;
  817. goto out;
  818. }
  819. }
  820. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  821. extent_end = key.offset + btrfs_dev_extent_length(l,
  822. dev_extent);
  823. if (extent_end > search_start)
  824. search_start = extent_end;
  825. next:
  826. path->slots[0]++;
  827. cond_resched();
  828. }
  829. /*
  830. * At this point, search_start should be the end of
  831. * allocated dev extents, and when shrinking the device,
  832. * search_end may be smaller than search_start.
  833. */
  834. if (search_end > search_start)
  835. hole_size = search_end - search_start;
  836. if (hole_size > max_hole_size) {
  837. max_hole_start = search_start;
  838. max_hole_size = hole_size;
  839. }
  840. /* See above. */
  841. if (hole_size < num_bytes)
  842. ret = -ENOSPC;
  843. else
  844. ret = 0;
  845. out:
  846. btrfs_free_path(path);
  847. error:
  848. *start = max_hole_start;
  849. if (len)
  850. *len = max_hole_size;
  851. return ret;
  852. }
  853. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  854. struct btrfs_device *device,
  855. u64 start)
  856. {
  857. int ret;
  858. struct btrfs_path *path;
  859. struct btrfs_root *root = device->dev_root;
  860. struct btrfs_key key;
  861. struct btrfs_key found_key;
  862. struct extent_buffer *leaf = NULL;
  863. struct btrfs_dev_extent *extent = NULL;
  864. path = btrfs_alloc_path();
  865. if (!path)
  866. return -ENOMEM;
  867. key.objectid = device->devid;
  868. key.offset = start;
  869. key.type = BTRFS_DEV_EXTENT_KEY;
  870. again:
  871. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  872. if (ret > 0) {
  873. ret = btrfs_previous_item(root, path, key.objectid,
  874. BTRFS_DEV_EXTENT_KEY);
  875. if (ret)
  876. goto out;
  877. leaf = path->nodes[0];
  878. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  879. extent = btrfs_item_ptr(leaf, path->slots[0],
  880. struct btrfs_dev_extent);
  881. BUG_ON(found_key.offset > start || found_key.offset +
  882. btrfs_dev_extent_length(leaf, extent) < start);
  883. key = found_key;
  884. btrfs_release_path(path);
  885. goto again;
  886. } else if (ret == 0) {
  887. leaf = path->nodes[0];
  888. extent = btrfs_item_ptr(leaf, path->slots[0],
  889. struct btrfs_dev_extent);
  890. }
  891. BUG_ON(ret);
  892. if (device->bytes_used > 0) {
  893. u64 len = btrfs_dev_extent_length(leaf, extent);
  894. device->bytes_used -= len;
  895. spin_lock(&root->fs_info->free_chunk_lock);
  896. root->fs_info->free_chunk_space += len;
  897. spin_unlock(&root->fs_info->free_chunk_lock);
  898. }
  899. ret = btrfs_del_item(trans, root, path);
  900. out:
  901. btrfs_free_path(path);
  902. return ret;
  903. }
  904. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  905. struct btrfs_device *device,
  906. u64 chunk_tree, u64 chunk_objectid,
  907. u64 chunk_offset, u64 start, u64 num_bytes)
  908. {
  909. int ret;
  910. struct btrfs_path *path;
  911. struct btrfs_root *root = device->dev_root;
  912. struct btrfs_dev_extent *extent;
  913. struct extent_buffer *leaf;
  914. struct btrfs_key key;
  915. WARN_ON(!device->in_fs_metadata);
  916. path = btrfs_alloc_path();
  917. if (!path)
  918. return -ENOMEM;
  919. key.objectid = device->devid;
  920. key.offset = start;
  921. key.type = BTRFS_DEV_EXTENT_KEY;
  922. ret = btrfs_insert_empty_item(trans, root, path, &key,
  923. sizeof(*extent));
  924. BUG_ON(ret);
  925. leaf = path->nodes[0];
  926. extent = btrfs_item_ptr(leaf, path->slots[0],
  927. struct btrfs_dev_extent);
  928. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  929. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  930. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  931. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  932. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  933. BTRFS_UUID_SIZE);
  934. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  935. btrfs_mark_buffer_dirty(leaf);
  936. btrfs_free_path(path);
  937. return ret;
  938. }
  939. static noinline int find_next_chunk(struct btrfs_root *root,
  940. u64 objectid, u64 *offset)
  941. {
  942. struct btrfs_path *path;
  943. int ret;
  944. struct btrfs_key key;
  945. struct btrfs_chunk *chunk;
  946. struct btrfs_key found_key;
  947. path = btrfs_alloc_path();
  948. if (!path)
  949. return -ENOMEM;
  950. key.objectid = objectid;
  951. key.offset = (u64)-1;
  952. key.type = BTRFS_CHUNK_ITEM_KEY;
  953. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  954. if (ret < 0)
  955. goto error;
  956. BUG_ON(ret == 0);
  957. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  958. if (ret) {
  959. *offset = 0;
  960. } else {
  961. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  962. path->slots[0]);
  963. if (found_key.objectid != objectid)
  964. *offset = 0;
  965. else {
  966. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  967. struct btrfs_chunk);
  968. *offset = found_key.offset +
  969. btrfs_chunk_length(path->nodes[0], chunk);
  970. }
  971. }
  972. ret = 0;
  973. error:
  974. btrfs_free_path(path);
  975. return ret;
  976. }
  977. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  978. {
  979. int ret;
  980. struct btrfs_key key;
  981. struct btrfs_key found_key;
  982. struct btrfs_path *path;
  983. root = root->fs_info->chunk_root;
  984. path = btrfs_alloc_path();
  985. if (!path)
  986. return -ENOMEM;
  987. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  988. key.type = BTRFS_DEV_ITEM_KEY;
  989. key.offset = (u64)-1;
  990. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  991. if (ret < 0)
  992. goto error;
  993. BUG_ON(ret == 0);
  994. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  995. BTRFS_DEV_ITEM_KEY);
  996. if (ret) {
  997. *objectid = 1;
  998. } else {
  999. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1000. path->slots[0]);
  1001. *objectid = found_key.offset + 1;
  1002. }
  1003. ret = 0;
  1004. error:
  1005. btrfs_free_path(path);
  1006. return ret;
  1007. }
  1008. /*
  1009. * the device information is stored in the chunk root
  1010. * the btrfs_device struct should be fully filled in
  1011. */
  1012. int btrfs_add_device(struct btrfs_trans_handle *trans,
  1013. struct btrfs_root *root,
  1014. struct btrfs_device *device)
  1015. {
  1016. int ret;
  1017. struct btrfs_path *path;
  1018. struct btrfs_dev_item *dev_item;
  1019. struct extent_buffer *leaf;
  1020. struct btrfs_key key;
  1021. unsigned long ptr;
  1022. root = root->fs_info->chunk_root;
  1023. path = btrfs_alloc_path();
  1024. if (!path)
  1025. return -ENOMEM;
  1026. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1027. key.type = BTRFS_DEV_ITEM_KEY;
  1028. key.offset = device->devid;
  1029. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1030. sizeof(*dev_item));
  1031. if (ret)
  1032. goto out;
  1033. leaf = path->nodes[0];
  1034. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1035. btrfs_set_device_id(leaf, dev_item, device->devid);
  1036. btrfs_set_device_generation(leaf, dev_item, 0);
  1037. btrfs_set_device_type(leaf, dev_item, device->type);
  1038. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1039. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1040. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1041. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1042. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1043. btrfs_set_device_group(leaf, dev_item, 0);
  1044. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1045. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1046. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1047. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1048. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1049. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1050. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1051. btrfs_mark_buffer_dirty(leaf);
  1052. ret = 0;
  1053. out:
  1054. btrfs_free_path(path);
  1055. return ret;
  1056. }
  1057. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1058. struct btrfs_device *device)
  1059. {
  1060. int ret;
  1061. struct btrfs_path *path;
  1062. struct btrfs_key key;
  1063. struct btrfs_trans_handle *trans;
  1064. root = root->fs_info->chunk_root;
  1065. path = btrfs_alloc_path();
  1066. if (!path)
  1067. return -ENOMEM;
  1068. trans = btrfs_start_transaction(root, 0);
  1069. if (IS_ERR(trans)) {
  1070. btrfs_free_path(path);
  1071. return PTR_ERR(trans);
  1072. }
  1073. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1074. key.type = BTRFS_DEV_ITEM_KEY;
  1075. key.offset = device->devid;
  1076. lock_chunks(root);
  1077. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1078. if (ret < 0)
  1079. goto out;
  1080. if (ret > 0) {
  1081. ret = -ENOENT;
  1082. goto out;
  1083. }
  1084. ret = btrfs_del_item(trans, root, path);
  1085. if (ret)
  1086. goto out;
  1087. out:
  1088. btrfs_free_path(path);
  1089. unlock_chunks(root);
  1090. btrfs_commit_transaction(trans, root);
  1091. return ret;
  1092. }
  1093. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1094. {
  1095. struct btrfs_device *device;
  1096. struct btrfs_device *next_device;
  1097. struct block_device *bdev;
  1098. struct buffer_head *bh = NULL;
  1099. struct btrfs_super_block *disk_super;
  1100. struct btrfs_fs_devices *cur_devices;
  1101. u64 all_avail;
  1102. u64 devid;
  1103. u64 num_devices;
  1104. u8 *dev_uuid;
  1105. int ret = 0;
  1106. bool clear_super = false;
  1107. mutex_lock(&uuid_mutex);
  1108. all_avail = root->fs_info->avail_data_alloc_bits |
  1109. root->fs_info->avail_system_alloc_bits |
  1110. root->fs_info->avail_metadata_alloc_bits;
  1111. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  1112. root->fs_info->fs_devices->num_devices <= 4) {
  1113. printk(KERN_ERR "btrfs: unable to go below four devices "
  1114. "on raid10\n");
  1115. ret = -EINVAL;
  1116. goto out;
  1117. }
  1118. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  1119. root->fs_info->fs_devices->num_devices <= 2) {
  1120. printk(KERN_ERR "btrfs: unable to go below two "
  1121. "devices on raid1\n");
  1122. ret = -EINVAL;
  1123. goto out;
  1124. }
  1125. if (strcmp(device_path, "missing") == 0) {
  1126. struct list_head *devices;
  1127. struct btrfs_device *tmp;
  1128. device = NULL;
  1129. devices = &root->fs_info->fs_devices->devices;
  1130. /*
  1131. * It is safe to read the devices since the volume_mutex
  1132. * is held.
  1133. */
  1134. list_for_each_entry(tmp, devices, dev_list) {
  1135. if (tmp->in_fs_metadata && !tmp->bdev) {
  1136. device = tmp;
  1137. break;
  1138. }
  1139. }
  1140. bdev = NULL;
  1141. bh = NULL;
  1142. disk_super = NULL;
  1143. if (!device) {
  1144. printk(KERN_ERR "btrfs: no missing devices found to "
  1145. "remove\n");
  1146. goto out;
  1147. }
  1148. } else {
  1149. bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
  1150. root->fs_info->bdev_holder);
  1151. if (IS_ERR(bdev)) {
  1152. ret = PTR_ERR(bdev);
  1153. goto out;
  1154. }
  1155. set_blocksize(bdev, 4096);
  1156. bh = btrfs_read_dev_super(bdev);
  1157. if (!bh) {
  1158. ret = -EINVAL;
  1159. goto error_close;
  1160. }
  1161. disk_super = (struct btrfs_super_block *)bh->b_data;
  1162. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1163. dev_uuid = disk_super->dev_item.uuid;
  1164. device = btrfs_find_device(root, devid, dev_uuid,
  1165. disk_super->fsid);
  1166. if (!device) {
  1167. ret = -ENOENT;
  1168. goto error_brelse;
  1169. }
  1170. }
  1171. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1172. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1173. "device\n");
  1174. ret = -EINVAL;
  1175. goto error_brelse;
  1176. }
  1177. if (device->writeable) {
  1178. lock_chunks(root);
  1179. list_del_init(&device->dev_alloc_list);
  1180. unlock_chunks(root);
  1181. root->fs_info->fs_devices->rw_devices--;
  1182. clear_super = true;
  1183. }
  1184. ret = btrfs_shrink_device(device, 0);
  1185. if (ret)
  1186. goto error_undo;
  1187. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1188. if (ret)
  1189. goto error_undo;
  1190. spin_lock(&root->fs_info->free_chunk_lock);
  1191. root->fs_info->free_chunk_space = device->total_bytes -
  1192. device->bytes_used;
  1193. spin_unlock(&root->fs_info->free_chunk_lock);
  1194. device->in_fs_metadata = 0;
  1195. btrfs_scrub_cancel_dev(root, device);
  1196. /*
  1197. * the device list mutex makes sure that we don't change
  1198. * the device list while someone else is writing out all
  1199. * the device supers.
  1200. */
  1201. cur_devices = device->fs_devices;
  1202. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1203. list_del_rcu(&device->dev_list);
  1204. device->fs_devices->num_devices--;
  1205. if (device->missing)
  1206. root->fs_info->fs_devices->missing_devices--;
  1207. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1208. struct btrfs_device, dev_list);
  1209. if (device->bdev == root->fs_info->sb->s_bdev)
  1210. root->fs_info->sb->s_bdev = next_device->bdev;
  1211. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1212. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1213. if (device->bdev)
  1214. device->fs_devices->open_devices--;
  1215. call_rcu(&device->rcu, free_device);
  1216. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1217. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1218. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1219. if (cur_devices->open_devices == 0) {
  1220. struct btrfs_fs_devices *fs_devices;
  1221. fs_devices = root->fs_info->fs_devices;
  1222. while (fs_devices) {
  1223. if (fs_devices->seed == cur_devices)
  1224. break;
  1225. fs_devices = fs_devices->seed;
  1226. }
  1227. fs_devices->seed = cur_devices->seed;
  1228. cur_devices->seed = NULL;
  1229. lock_chunks(root);
  1230. __btrfs_close_devices(cur_devices);
  1231. unlock_chunks(root);
  1232. free_fs_devices(cur_devices);
  1233. }
  1234. /*
  1235. * at this point, the device is zero sized. We want to
  1236. * remove it from the devices list and zero out the old super
  1237. */
  1238. if (clear_super) {
  1239. /* make sure this device isn't detected as part of
  1240. * the FS anymore
  1241. */
  1242. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1243. set_buffer_dirty(bh);
  1244. sync_dirty_buffer(bh);
  1245. }
  1246. ret = 0;
  1247. error_brelse:
  1248. brelse(bh);
  1249. error_close:
  1250. if (bdev)
  1251. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1252. out:
  1253. mutex_unlock(&uuid_mutex);
  1254. return ret;
  1255. error_undo:
  1256. if (device->writeable) {
  1257. lock_chunks(root);
  1258. list_add(&device->dev_alloc_list,
  1259. &root->fs_info->fs_devices->alloc_list);
  1260. unlock_chunks(root);
  1261. root->fs_info->fs_devices->rw_devices++;
  1262. }
  1263. goto error_brelse;
  1264. }
  1265. /*
  1266. * does all the dirty work required for changing file system's UUID.
  1267. */
  1268. static int btrfs_prepare_sprout(struct btrfs_trans_handle *trans,
  1269. struct btrfs_root *root)
  1270. {
  1271. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1272. struct btrfs_fs_devices *old_devices;
  1273. struct btrfs_fs_devices *seed_devices;
  1274. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1275. struct btrfs_device *device;
  1276. u64 super_flags;
  1277. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1278. if (!fs_devices->seeding)
  1279. return -EINVAL;
  1280. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1281. if (!seed_devices)
  1282. return -ENOMEM;
  1283. old_devices = clone_fs_devices(fs_devices);
  1284. if (IS_ERR(old_devices)) {
  1285. kfree(seed_devices);
  1286. return PTR_ERR(old_devices);
  1287. }
  1288. list_add(&old_devices->list, &fs_uuids);
  1289. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1290. seed_devices->opened = 1;
  1291. INIT_LIST_HEAD(&seed_devices->devices);
  1292. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1293. mutex_init(&seed_devices->device_list_mutex);
  1294. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1295. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1296. synchronize_rcu);
  1297. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1298. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1299. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1300. device->fs_devices = seed_devices;
  1301. }
  1302. fs_devices->seeding = 0;
  1303. fs_devices->num_devices = 0;
  1304. fs_devices->open_devices = 0;
  1305. fs_devices->seed = seed_devices;
  1306. generate_random_uuid(fs_devices->fsid);
  1307. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1308. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1309. super_flags = btrfs_super_flags(disk_super) &
  1310. ~BTRFS_SUPER_FLAG_SEEDING;
  1311. btrfs_set_super_flags(disk_super, super_flags);
  1312. return 0;
  1313. }
  1314. /*
  1315. * strore the expected generation for seed devices in device items.
  1316. */
  1317. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1318. struct btrfs_root *root)
  1319. {
  1320. struct btrfs_path *path;
  1321. struct extent_buffer *leaf;
  1322. struct btrfs_dev_item *dev_item;
  1323. struct btrfs_device *device;
  1324. struct btrfs_key key;
  1325. u8 fs_uuid[BTRFS_UUID_SIZE];
  1326. u8 dev_uuid[BTRFS_UUID_SIZE];
  1327. u64 devid;
  1328. int ret;
  1329. path = btrfs_alloc_path();
  1330. if (!path)
  1331. return -ENOMEM;
  1332. root = root->fs_info->chunk_root;
  1333. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1334. key.offset = 0;
  1335. key.type = BTRFS_DEV_ITEM_KEY;
  1336. while (1) {
  1337. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1338. if (ret < 0)
  1339. goto error;
  1340. leaf = path->nodes[0];
  1341. next_slot:
  1342. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1343. ret = btrfs_next_leaf(root, path);
  1344. if (ret > 0)
  1345. break;
  1346. if (ret < 0)
  1347. goto error;
  1348. leaf = path->nodes[0];
  1349. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1350. btrfs_release_path(path);
  1351. continue;
  1352. }
  1353. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1354. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1355. key.type != BTRFS_DEV_ITEM_KEY)
  1356. break;
  1357. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1358. struct btrfs_dev_item);
  1359. devid = btrfs_device_id(leaf, dev_item);
  1360. read_extent_buffer(leaf, dev_uuid,
  1361. (unsigned long)btrfs_device_uuid(dev_item),
  1362. BTRFS_UUID_SIZE);
  1363. read_extent_buffer(leaf, fs_uuid,
  1364. (unsigned long)btrfs_device_fsid(dev_item),
  1365. BTRFS_UUID_SIZE);
  1366. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1367. BUG_ON(!device);
  1368. if (device->fs_devices->seeding) {
  1369. btrfs_set_device_generation(leaf, dev_item,
  1370. device->generation);
  1371. btrfs_mark_buffer_dirty(leaf);
  1372. }
  1373. path->slots[0]++;
  1374. goto next_slot;
  1375. }
  1376. ret = 0;
  1377. error:
  1378. btrfs_free_path(path);
  1379. return ret;
  1380. }
  1381. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1382. {
  1383. struct request_queue *q;
  1384. struct btrfs_trans_handle *trans;
  1385. struct btrfs_device *device;
  1386. struct block_device *bdev;
  1387. struct list_head *devices;
  1388. struct super_block *sb = root->fs_info->sb;
  1389. u64 total_bytes;
  1390. int seeding_dev = 0;
  1391. int ret = 0;
  1392. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1393. return -EINVAL;
  1394. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1395. root->fs_info->bdev_holder);
  1396. if (IS_ERR(bdev))
  1397. return PTR_ERR(bdev);
  1398. if (root->fs_info->fs_devices->seeding) {
  1399. seeding_dev = 1;
  1400. down_write(&sb->s_umount);
  1401. mutex_lock(&uuid_mutex);
  1402. }
  1403. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1404. devices = &root->fs_info->fs_devices->devices;
  1405. /*
  1406. * we have the volume lock, so we don't need the extra
  1407. * device list mutex while reading the list here.
  1408. */
  1409. list_for_each_entry(device, devices, dev_list) {
  1410. if (device->bdev == bdev) {
  1411. ret = -EEXIST;
  1412. goto error;
  1413. }
  1414. }
  1415. device = kzalloc(sizeof(*device), GFP_NOFS);
  1416. if (!device) {
  1417. /* we can safely leave the fs_devices entry around */
  1418. ret = -ENOMEM;
  1419. goto error;
  1420. }
  1421. device->name = kstrdup(device_path, GFP_NOFS);
  1422. if (!device->name) {
  1423. kfree(device);
  1424. ret = -ENOMEM;
  1425. goto error;
  1426. }
  1427. ret = find_next_devid(root, &device->devid);
  1428. if (ret) {
  1429. kfree(device->name);
  1430. kfree(device);
  1431. goto error;
  1432. }
  1433. trans = btrfs_start_transaction(root, 0);
  1434. if (IS_ERR(trans)) {
  1435. kfree(device->name);
  1436. kfree(device);
  1437. ret = PTR_ERR(trans);
  1438. goto error;
  1439. }
  1440. lock_chunks(root);
  1441. q = bdev_get_queue(bdev);
  1442. if (blk_queue_discard(q))
  1443. device->can_discard = 1;
  1444. device->writeable = 1;
  1445. device->work.func = pending_bios_fn;
  1446. generate_random_uuid(device->uuid);
  1447. spin_lock_init(&device->io_lock);
  1448. device->generation = trans->transid;
  1449. device->io_width = root->sectorsize;
  1450. device->io_align = root->sectorsize;
  1451. device->sector_size = root->sectorsize;
  1452. device->total_bytes = i_size_read(bdev->bd_inode);
  1453. device->disk_total_bytes = device->total_bytes;
  1454. device->dev_root = root->fs_info->dev_root;
  1455. device->bdev = bdev;
  1456. device->in_fs_metadata = 1;
  1457. device->mode = FMODE_EXCL;
  1458. set_blocksize(device->bdev, 4096);
  1459. if (seeding_dev) {
  1460. sb->s_flags &= ~MS_RDONLY;
  1461. ret = btrfs_prepare_sprout(trans, root);
  1462. BUG_ON(ret);
  1463. }
  1464. device->fs_devices = root->fs_info->fs_devices;
  1465. /*
  1466. * we don't want write_supers to jump in here with our device
  1467. * half setup
  1468. */
  1469. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1470. list_add_rcu(&device->dev_list, &root->fs_info->fs_devices->devices);
  1471. list_add(&device->dev_alloc_list,
  1472. &root->fs_info->fs_devices->alloc_list);
  1473. root->fs_info->fs_devices->num_devices++;
  1474. root->fs_info->fs_devices->open_devices++;
  1475. root->fs_info->fs_devices->rw_devices++;
  1476. if (device->can_discard)
  1477. root->fs_info->fs_devices->num_can_discard++;
  1478. root->fs_info->fs_devices->total_rw_bytes += device->total_bytes;
  1479. spin_lock(&root->fs_info->free_chunk_lock);
  1480. root->fs_info->free_chunk_space += device->total_bytes;
  1481. spin_unlock(&root->fs_info->free_chunk_lock);
  1482. if (!blk_queue_nonrot(bdev_get_queue(bdev)))
  1483. root->fs_info->fs_devices->rotating = 1;
  1484. total_bytes = btrfs_super_total_bytes(root->fs_info->super_copy);
  1485. btrfs_set_super_total_bytes(root->fs_info->super_copy,
  1486. total_bytes + device->total_bytes);
  1487. total_bytes = btrfs_super_num_devices(root->fs_info->super_copy);
  1488. btrfs_set_super_num_devices(root->fs_info->super_copy,
  1489. total_bytes + 1);
  1490. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1491. if (seeding_dev) {
  1492. ret = init_first_rw_device(trans, root, device);
  1493. BUG_ON(ret);
  1494. ret = btrfs_finish_sprout(trans, root);
  1495. BUG_ON(ret);
  1496. } else {
  1497. ret = btrfs_add_device(trans, root, device);
  1498. }
  1499. /*
  1500. * we've got more storage, clear any full flags on the space
  1501. * infos
  1502. */
  1503. btrfs_clear_space_info_full(root->fs_info);
  1504. unlock_chunks(root);
  1505. btrfs_commit_transaction(trans, root);
  1506. if (seeding_dev) {
  1507. mutex_unlock(&uuid_mutex);
  1508. up_write(&sb->s_umount);
  1509. ret = btrfs_relocate_sys_chunks(root);
  1510. BUG_ON(ret);
  1511. }
  1512. return ret;
  1513. error:
  1514. blkdev_put(bdev, FMODE_EXCL);
  1515. if (seeding_dev) {
  1516. mutex_unlock(&uuid_mutex);
  1517. up_write(&sb->s_umount);
  1518. }
  1519. return ret;
  1520. }
  1521. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1522. struct btrfs_device *device)
  1523. {
  1524. int ret;
  1525. struct btrfs_path *path;
  1526. struct btrfs_root *root;
  1527. struct btrfs_dev_item *dev_item;
  1528. struct extent_buffer *leaf;
  1529. struct btrfs_key key;
  1530. root = device->dev_root->fs_info->chunk_root;
  1531. path = btrfs_alloc_path();
  1532. if (!path)
  1533. return -ENOMEM;
  1534. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1535. key.type = BTRFS_DEV_ITEM_KEY;
  1536. key.offset = device->devid;
  1537. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1538. if (ret < 0)
  1539. goto out;
  1540. if (ret > 0) {
  1541. ret = -ENOENT;
  1542. goto out;
  1543. }
  1544. leaf = path->nodes[0];
  1545. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1546. btrfs_set_device_id(leaf, dev_item, device->devid);
  1547. btrfs_set_device_type(leaf, dev_item, device->type);
  1548. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1549. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1550. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1551. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1552. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1553. btrfs_mark_buffer_dirty(leaf);
  1554. out:
  1555. btrfs_free_path(path);
  1556. return ret;
  1557. }
  1558. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1559. struct btrfs_device *device, u64 new_size)
  1560. {
  1561. struct btrfs_super_block *super_copy =
  1562. device->dev_root->fs_info->super_copy;
  1563. u64 old_total = btrfs_super_total_bytes(super_copy);
  1564. u64 diff = new_size - device->total_bytes;
  1565. if (!device->writeable)
  1566. return -EACCES;
  1567. if (new_size <= device->total_bytes)
  1568. return -EINVAL;
  1569. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1570. device->fs_devices->total_rw_bytes += diff;
  1571. device->total_bytes = new_size;
  1572. device->disk_total_bytes = new_size;
  1573. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1574. return btrfs_update_device(trans, device);
  1575. }
  1576. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1577. struct btrfs_device *device, u64 new_size)
  1578. {
  1579. int ret;
  1580. lock_chunks(device->dev_root);
  1581. ret = __btrfs_grow_device(trans, device, new_size);
  1582. unlock_chunks(device->dev_root);
  1583. return ret;
  1584. }
  1585. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1586. struct btrfs_root *root,
  1587. u64 chunk_tree, u64 chunk_objectid,
  1588. u64 chunk_offset)
  1589. {
  1590. int ret;
  1591. struct btrfs_path *path;
  1592. struct btrfs_key key;
  1593. root = root->fs_info->chunk_root;
  1594. path = btrfs_alloc_path();
  1595. if (!path)
  1596. return -ENOMEM;
  1597. key.objectid = chunk_objectid;
  1598. key.offset = chunk_offset;
  1599. key.type = BTRFS_CHUNK_ITEM_KEY;
  1600. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1601. BUG_ON(ret);
  1602. ret = btrfs_del_item(trans, root, path);
  1603. btrfs_free_path(path);
  1604. return ret;
  1605. }
  1606. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1607. chunk_offset)
  1608. {
  1609. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1610. struct btrfs_disk_key *disk_key;
  1611. struct btrfs_chunk *chunk;
  1612. u8 *ptr;
  1613. int ret = 0;
  1614. u32 num_stripes;
  1615. u32 array_size;
  1616. u32 len = 0;
  1617. u32 cur;
  1618. struct btrfs_key key;
  1619. array_size = btrfs_super_sys_array_size(super_copy);
  1620. ptr = super_copy->sys_chunk_array;
  1621. cur = 0;
  1622. while (cur < array_size) {
  1623. disk_key = (struct btrfs_disk_key *)ptr;
  1624. btrfs_disk_key_to_cpu(&key, disk_key);
  1625. len = sizeof(*disk_key);
  1626. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1627. chunk = (struct btrfs_chunk *)(ptr + len);
  1628. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1629. len += btrfs_chunk_item_size(num_stripes);
  1630. } else {
  1631. ret = -EIO;
  1632. break;
  1633. }
  1634. if (key.objectid == chunk_objectid &&
  1635. key.offset == chunk_offset) {
  1636. memmove(ptr, ptr + len, array_size - (cur + len));
  1637. array_size -= len;
  1638. btrfs_set_super_sys_array_size(super_copy, array_size);
  1639. } else {
  1640. ptr += len;
  1641. cur += len;
  1642. }
  1643. }
  1644. return ret;
  1645. }
  1646. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1647. u64 chunk_tree, u64 chunk_objectid,
  1648. u64 chunk_offset)
  1649. {
  1650. struct extent_map_tree *em_tree;
  1651. struct btrfs_root *extent_root;
  1652. struct btrfs_trans_handle *trans;
  1653. struct extent_map *em;
  1654. struct map_lookup *map;
  1655. int ret;
  1656. int i;
  1657. root = root->fs_info->chunk_root;
  1658. extent_root = root->fs_info->extent_root;
  1659. em_tree = &root->fs_info->mapping_tree.map_tree;
  1660. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1661. if (ret)
  1662. return -ENOSPC;
  1663. /* step one, relocate all the extents inside this chunk */
  1664. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1665. if (ret)
  1666. return ret;
  1667. trans = btrfs_start_transaction(root, 0);
  1668. BUG_ON(IS_ERR(trans));
  1669. lock_chunks(root);
  1670. /*
  1671. * step two, delete the device extents and the
  1672. * chunk tree entries
  1673. */
  1674. read_lock(&em_tree->lock);
  1675. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1676. read_unlock(&em_tree->lock);
  1677. BUG_ON(em->start > chunk_offset ||
  1678. em->start + em->len < chunk_offset);
  1679. map = (struct map_lookup *)em->bdev;
  1680. for (i = 0; i < map->num_stripes; i++) {
  1681. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1682. map->stripes[i].physical);
  1683. BUG_ON(ret);
  1684. if (map->stripes[i].dev) {
  1685. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1686. BUG_ON(ret);
  1687. }
  1688. }
  1689. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1690. chunk_offset);
  1691. BUG_ON(ret);
  1692. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1693. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1694. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1695. BUG_ON(ret);
  1696. }
  1697. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1698. BUG_ON(ret);
  1699. write_lock(&em_tree->lock);
  1700. remove_extent_mapping(em_tree, em);
  1701. write_unlock(&em_tree->lock);
  1702. kfree(map);
  1703. em->bdev = NULL;
  1704. /* once for the tree */
  1705. free_extent_map(em);
  1706. /* once for us */
  1707. free_extent_map(em);
  1708. unlock_chunks(root);
  1709. btrfs_end_transaction(trans, root);
  1710. return 0;
  1711. }
  1712. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1713. {
  1714. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1715. struct btrfs_path *path;
  1716. struct extent_buffer *leaf;
  1717. struct btrfs_chunk *chunk;
  1718. struct btrfs_key key;
  1719. struct btrfs_key found_key;
  1720. u64 chunk_tree = chunk_root->root_key.objectid;
  1721. u64 chunk_type;
  1722. bool retried = false;
  1723. int failed = 0;
  1724. int ret;
  1725. path = btrfs_alloc_path();
  1726. if (!path)
  1727. return -ENOMEM;
  1728. again:
  1729. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1730. key.offset = (u64)-1;
  1731. key.type = BTRFS_CHUNK_ITEM_KEY;
  1732. while (1) {
  1733. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1734. if (ret < 0)
  1735. goto error;
  1736. BUG_ON(ret == 0);
  1737. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1738. key.type);
  1739. if (ret < 0)
  1740. goto error;
  1741. if (ret > 0)
  1742. break;
  1743. leaf = path->nodes[0];
  1744. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1745. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1746. struct btrfs_chunk);
  1747. chunk_type = btrfs_chunk_type(leaf, chunk);
  1748. btrfs_release_path(path);
  1749. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1750. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1751. found_key.objectid,
  1752. found_key.offset);
  1753. if (ret == -ENOSPC)
  1754. failed++;
  1755. else if (ret)
  1756. BUG();
  1757. }
  1758. if (found_key.offset == 0)
  1759. break;
  1760. key.offset = found_key.offset - 1;
  1761. }
  1762. ret = 0;
  1763. if (failed && !retried) {
  1764. failed = 0;
  1765. retried = true;
  1766. goto again;
  1767. } else if (failed && retried) {
  1768. WARN_ON(1);
  1769. ret = -ENOSPC;
  1770. }
  1771. error:
  1772. btrfs_free_path(path);
  1773. return ret;
  1774. }
  1775. static int insert_balance_item(struct btrfs_root *root,
  1776. struct btrfs_balance_control *bctl)
  1777. {
  1778. struct btrfs_trans_handle *trans;
  1779. struct btrfs_balance_item *item;
  1780. struct btrfs_disk_balance_args disk_bargs;
  1781. struct btrfs_path *path;
  1782. struct extent_buffer *leaf;
  1783. struct btrfs_key key;
  1784. int ret, err;
  1785. path = btrfs_alloc_path();
  1786. if (!path)
  1787. return -ENOMEM;
  1788. trans = btrfs_start_transaction(root, 0);
  1789. if (IS_ERR(trans)) {
  1790. btrfs_free_path(path);
  1791. return PTR_ERR(trans);
  1792. }
  1793. key.objectid = BTRFS_BALANCE_OBJECTID;
  1794. key.type = BTRFS_BALANCE_ITEM_KEY;
  1795. key.offset = 0;
  1796. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1797. sizeof(*item));
  1798. if (ret)
  1799. goto out;
  1800. leaf = path->nodes[0];
  1801. item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_balance_item);
  1802. memset_extent_buffer(leaf, 0, (unsigned long)item, sizeof(*item));
  1803. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->data);
  1804. btrfs_set_balance_data(leaf, item, &disk_bargs);
  1805. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->meta);
  1806. btrfs_set_balance_meta(leaf, item, &disk_bargs);
  1807. btrfs_cpu_balance_args_to_disk(&disk_bargs, &bctl->sys);
  1808. btrfs_set_balance_sys(leaf, item, &disk_bargs);
  1809. btrfs_set_balance_flags(leaf, item, bctl->flags);
  1810. btrfs_mark_buffer_dirty(leaf);
  1811. out:
  1812. btrfs_free_path(path);
  1813. err = btrfs_commit_transaction(trans, root);
  1814. if (err && !ret)
  1815. ret = err;
  1816. return ret;
  1817. }
  1818. static int del_balance_item(struct btrfs_root *root)
  1819. {
  1820. struct btrfs_trans_handle *trans;
  1821. struct btrfs_path *path;
  1822. struct btrfs_key key;
  1823. int ret, err;
  1824. path = btrfs_alloc_path();
  1825. if (!path)
  1826. return -ENOMEM;
  1827. trans = btrfs_start_transaction(root, 0);
  1828. if (IS_ERR(trans)) {
  1829. btrfs_free_path(path);
  1830. return PTR_ERR(trans);
  1831. }
  1832. key.objectid = BTRFS_BALANCE_OBJECTID;
  1833. key.type = BTRFS_BALANCE_ITEM_KEY;
  1834. key.offset = 0;
  1835. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1836. if (ret < 0)
  1837. goto out;
  1838. if (ret > 0) {
  1839. ret = -ENOENT;
  1840. goto out;
  1841. }
  1842. ret = btrfs_del_item(trans, root, path);
  1843. out:
  1844. btrfs_free_path(path);
  1845. err = btrfs_commit_transaction(trans, root);
  1846. if (err && !ret)
  1847. ret = err;
  1848. return ret;
  1849. }
  1850. /*
  1851. * Should be called with both balance and volume mutexes held to
  1852. * serialize other volume operations (add_dev/rm_dev/resize) with
  1853. * restriper. Same goes for unset_balance_control.
  1854. */
  1855. static void set_balance_control(struct btrfs_balance_control *bctl)
  1856. {
  1857. struct btrfs_fs_info *fs_info = bctl->fs_info;
  1858. BUG_ON(fs_info->balance_ctl);
  1859. spin_lock(&fs_info->balance_lock);
  1860. fs_info->balance_ctl = bctl;
  1861. spin_unlock(&fs_info->balance_lock);
  1862. }
  1863. static void unset_balance_control(struct btrfs_fs_info *fs_info)
  1864. {
  1865. struct btrfs_balance_control *bctl = fs_info->balance_ctl;
  1866. BUG_ON(!fs_info->balance_ctl);
  1867. spin_lock(&fs_info->balance_lock);
  1868. fs_info->balance_ctl = NULL;
  1869. spin_unlock(&fs_info->balance_lock);
  1870. kfree(bctl);
  1871. }
  1872. /*
  1873. * Balance filters. Return 1 if chunk should be filtered out
  1874. * (should not be balanced).
  1875. */
  1876. static int chunk_profiles_filter(u64 chunk_profile,
  1877. struct btrfs_balance_args *bargs)
  1878. {
  1879. chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
  1880. if (chunk_profile == 0)
  1881. chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  1882. if (bargs->profiles & chunk_profile)
  1883. return 0;
  1884. return 1;
  1885. }
  1886. static u64 div_factor_fine(u64 num, int factor)
  1887. {
  1888. if (factor <= 0)
  1889. return 0;
  1890. if (factor >= 100)
  1891. return num;
  1892. num *= factor;
  1893. do_div(num, 100);
  1894. return num;
  1895. }
  1896. static int chunk_usage_filter(struct btrfs_fs_info *fs_info, u64 chunk_offset,
  1897. struct btrfs_balance_args *bargs)
  1898. {
  1899. struct btrfs_block_group_cache *cache;
  1900. u64 chunk_used, user_thresh;
  1901. int ret = 1;
  1902. cache = btrfs_lookup_block_group(fs_info, chunk_offset);
  1903. chunk_used = btrfs_block_group_used(&cache->item);
  1904. user_thresh = div_factor_fine(cache->key.offset, bargs->usage);
  1905. if (chunk_used < user_thresh)
  1906. ret = 0;
  1907. btrfs_put_block_group(cache);
  1908. return ret;
  1909. }
  1910. static int chunk_devid_filter(struct extent_buffer *leaf,
  1911. struct btrfs_chunk *chunk,
  1912. struct btrfs_balance_args *bargs)
  1913. {
  1914. struct btrfs_stripe *stripe;
  1915. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1916. int i;
  1917. for (i = 0; i < num_stripes; i++) {
  1918. stripe = btrfs_stripe_nr(chunk, i);
  1919. if (btrfs_stripe_devid(leaf, stripe) == bargs->devid)
  1920. return 0;
  1921. }
  1922. return 1;
  1923. }
  1924. /* [pstart, pend) */
  1925. static int chunk_drange_filter(struct extent_buffer *leaf,
  1926. struct btrfs_chunk *chunk,
  1927. u64 chunk_offset,
  1928. struct btrfs_balance_args *bargs)
  1929. {
  1930. struct btrfs_stripe *stripe;
  1931. int num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1932. u64 stripe_offset;
  1933. u64 stripe_length;
  1934. int factor;
  1935. int i;
  1936. if (!(bargs->flags & BTRFS_BALANCE_ARGS_DEVID))
  1937. return 0;
  1938. if (btrfs_chunk_type(leaf, chunk) & (BTRFS_BLOCK_GROUP_DUP |
  1939. BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_RAID10))
  1940. factor = 2;
  1941. else
  1942. factor = 1;
  1943. factor = num_stripes / factor;
  1944. for (i = 0; i < num_stripes; i++) {
  1945. stripe = btrfs_stripe_nr(chunk, i);
  1946. if (btrfs_stripe_devid(leaf, stripe) != bargs->devid)
  1947. continue;
  1948. stripe_offset = btrfs_stripe_offset(leaf, stripe);
  1949. stripe_length = btrfs_chunk_length(leaf, chunk);
  1950. do_div(stripe_length, factor);
  1951. if (stripe_offset < bargs->pend &&
  1952. stripe_offset + stripe_length > bargs->pstart)
  1953. return 0;
  1954. }
  1955. return 1;
  1956. }
  1957. /* [vstart, vend) */
  1958. static int chunk_vrange_filter(struct extent_buffer *leaf,
  1959. struct btrfs_chunk *chunk,
  1960. u64 chunk_offset,
  1961. struct btrfs_balance_args *bargs)
  1962. {
  1963. if (chunk_offset < bargs->vend &&
  1964. chunk_offset + btrfs_chunk_length(leaf, chunk) > bargs->vstart)
  1965. /* at least part of the chunk is inside this vrange */
  1966. return 0;
  1967. return 1;
  1968. }
  1969. static int chunk_soft_convert_filter(u64 chunk_profile,
  1970. struct btrfs_balance_args *bargs)
  1971. {
  1972. if (!(bargs->flags & BTRFS_BALANCE_ARGS_CONVERT))
  1973. return 0;
  1974. chunk_profile &= BTRFS_BLOCK_GROUP_PROFILE_MASK;
  1975. if (chunk_profile == 0)
  1976. chunk_profile = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  1977. if (bargs->target & chunk_profile)
  1978. return 1;
  1979. return 0;
  1980. }
  1981. static int should_balance_chunk(struct btrfs_root *root,
  1982. struct extent_buffer *leaf,
  1983. struct btrfs_chunk *chunk, u64 chunk_offset)
  1984. {
  1985. struct btrfs_balance_control *bctl = root->fs_info->balance_ctl;
  1986. struct btrfs_balance_args *bargs = NULL;
  1987. u64 chunk_type = btrfs_chunk_type(leaf, chunk);
  1988. /* type filter */
  1989. if (!((chunk_type & BTRFS_BLOCK_GROUP_TYPE_MASK) &
  1990. (bctl->flags & BTRFS_BALANCE_TYPE_MASK))) {
  1991. return 0;
  1992. }
  1993. if (chunk_type & BTRFS_BLOCK_GROUP_DATA)
  1994. bargs = &bctl->data;
  1995. else if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM)
  1996. bargs = &bctl->sys;
  1997. else if (chunk_type & BTRFS_BLOCK_GROUP_METADATA)
  1998. bargs = &bctl->meta;
  1999. /* profiles filter */
  2000. if ((bargs->flags & BTRFS_BALANCE_ARGS_PROFILES) &&
  2001. chunk_profiles_filter(chunk_type, bargs)) {
  2002. return 0;
  2003. }
  2004. /* usage filter */
  2005. if ((bargs->flags & BTRFS_BALANCE_ARGS_USAGE) &&
  2006. chunk_usage_filter(bctl->fs_info, chunk_offset, bargs)) {
  2007. return 0;
  2008. }
  2009. /* devid filter */
  2010. if ((bargs->flags & BTRFS_BALANCE_ARGS_DEVID) &&
  2011. chunk_devid_filter(leaf, chunk, bargs)) {
  2012. return 0;
  2013. }
  2014. /* drange filter, makes sense only with devid filter */
  2015. if ((bargs->flags & BTRFS_BALANCE_ARGS_DRANGE) &&
  2016. chunk_drange_filter(leaf, chunk, chunk_offset, bargs)) {
  2017. return 0;
  2018. }
  2019. /* vrange filter */
  2020. if ((bargs->flags & BTRFS_BALANCE_ARGS_VRANGE) &&
  2021. chunk_vrange_filter(leaf, chunk, chunk_offset, bargs)) {
  2022. return 0;
  2023. }
  2024. /* soft profile changing mode */
  2025. if ((bargs->flags & BTRFS_BALANCE_ARGS_SOFT) &&
  2026. chunk_soft_convert_filter(chunk_type, bargs)) {
  2027. return 0;
  2028. }
  2029. return 1;
  2030. }
  2031. static u64 div_factor(u64 num, int factor)
  2032. {
  2033. if (factor == 10)
  2034. return num;
  2035. num *= factor;
  2036. do_div(num, 10);
  2037. return num;
  2038. }
  2039. static int __btrfs_balance(struct btrfs_fs_info *fs_info)
  2040. {
  2041. struct btrfs_root *chunk_root = fs_info->chunk_root;
  2042. struct btrfs_root *dev_root = fs_info->dev_root;
  2043. struct list_head *devices;
  2044. struct btrfs_device *device;
  2045. u64 old_size;
  2046. u64 size_to_free;
  2047. struct btrfs_chunk *chunk;
  2048. struct btrfs_path *path;
  2049. struct btrfs_key key;
  2050. struct btrfs_key found_key;
  2051. struct btrfs_trans_handle *trans;
  2052. struct extent_buffer *leaf;
  2053. int slot;
  2054. int ret;
  2055. int enospc_errors = 0;
  2056. /* step one make some room on all the devices */
  2057. devices = &fs_info->fs_devices->devices;
  2058. list_for_each_entry(device, devices, dev_list) {
  2059. old_size = device->total_bytes;
  2060. size_to_free = div_factor(old_size, 1);
  2061. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  2062. if (!device->writeable ||
  2063. device->total_bytes - device->bytes_used > size_to_free)
  2064. continue;
  2065. ret = btrfs_shrink_device(device, old_size - size_to_free);
  2066. if (ret == -ENOSPC)
  2067. break;
  2068. BUG_ON(ret);
  2069. trans = btrfs_start_transaction(dev_root, 0);
  2070. BUG_ON(IS_ERR(trans));
  2071. ret = btrfs_grow_device(trans, device, old_size);
  2072. BUG_ON(ret);
  2073. btrfs_end_transaction(trans, dev_root);
  2074. }
  2075. /* step two, relocate all the chunks */
  2076. path = btrfs_alloc_path();
  2077. if (!path) {
  2078. ret = -ENOMEM;
  2079. goto error;
  2080. }
  2081. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2082. key.offset = (u64)-1;
  2083. key.type = BTRFS_CHUNK_ITEM_KEY;
  2084. while (1) {
  2085. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  2086. if (ret < 0)
  2087. goto error;
  2088. /*
  2089. * this shouldn't happen, it means the last relocate
  2090. * failed
  2091. */
  2092. if (ret == 0)
  2093. BUG(); /* FIXME break ? */
  2094. ret = btrfs_previous_item(chunk_root, path, 0,
  2095. BTRFS_CHUNK_ITEM_KEY);
  2096. if (ret) {
  2097. ret = 0;
  2098. break;
  2099. }
  2100. leaf = path->nodes[0];
  2101. slot = path->slots[0];
  2102. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2103. if (found_key.objectid != key.objectid)
  2104. break;
  2105. /* chunk zero is special */
  2106. if (found_key.offset == 0)
  2107. break;
  2108. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2109. ret = should_balance_chunk(chunk_root, leaf, chunk,
  2110. found_key.offset);
  2111. btrfs_release_path(path);
  2112. if (!ret)
  2113. goto loop;
  2114. ret = btrfs_relocate_chunk(chunk_root,
  2115. chunk_root->root_key.objectid,
  2116. found_key.objectid,
  2117. found_key.offset);
  2118. if (ret && ret != -ENOSPC)
  2119. goto error;
  2120. if (ret == -ENOSPC)
  2121. enospc_errors++;
  2122. loop:
  2123. key.offset = found_key.offset - 1;
  2124. }
  2125. error:
  2126. btrfs_free_path(path);
  2127. if (enospc_errors) {
  2128. printk(KERN_INFO "btrfs: %d enospc errors during balance\n",
  2129. enospc_errors);
  2130. if (!ret)
  2131. ret = -ENOSPC;
  2132. }
  2133. return ret;
  2134. }
  2135. static void __cancel_balance(struct btrfs_fs_info *fs_info)
  2136. {
  2137. int ret;
  2138. unset_balance_control(fs_info);
  2139. ret = del_balance_item(fs_info->tree_root);
  2140. BUG_ON(ret);
  2141. }
  2142. void update_ioctl_balance_args(struct btrfs_fs_info *fs_info,
  2143. struct btrfs_ioctl_balance_args *bargs);
  2144. /*
  2145. * Should be called with both balance and volume mutexes held
  2146. */
  2147. int btrfs_balance(struct btrfs_balance_control *bctl,
  2148. struct btrfs_ioctl_balance_args *bargs)
  2149. {
  2150. struct btrfs_fs_info *fs_info = bctl->fs_info;
  2151. u64 allowed;
  2152. int ret;
  2153. if (btrfs_fs_closing(fs_info)) {
  2154. ret = -EINVAL;
  2155. goto out;
  2156. }
  2157. /*
  2158. * In case of mixed groups both data and meta should be picked,
  2159. * and identical options should be given for both of them.
  2160. */
  2161. allowed = btrfs_super_incompat_flags(fs_info->super_copy);
  2162. if ((allowed & BTRFS_FEATURE_INCOMPAT_MIXED_GROUPS) &&
  2163. (bctl->flags & (BTRFS_BALANCE_DATA | BTRFS_BALANCE_METADATA))) {
  2164. if (!(bctl->flags & BTRFS_BALANCE_DATA) ||
  2165. !(bctl->flags & BTRFS_BALANCE_METADATA) ||
  2166. memcmp(&bctl->data, &bctl->meta, sizeof(bctl->data))) {
  2167. printk(KERN_ERR "btrfs: with mixed groups data and "
  2168. "metadata balance options must be the same\n");
  2169. ret = -EINVAL;
  2170. goto out;
  2171. }
  2172. }
  2173. /*
  2174. * Profile changing sanity checks. Skip them if a simple
  2175. * balance is requested.
  2176. */
  2177. if (!((bctl->data.flags | bctl->sys.flags | bctl->meta.flags) &
  2178. BTRFS_BALANCE_ARGS_CONVERT))
  2179. goto do_balance;
  2180. allowed = BTRFS_AVAIL_ALLOC_BIT_SINGLE;
  2181. if (fs_info->fs_devices->num_devices == 1)
  2182. allowed |= BTRFS_BLOCK_GROUP_DUP;
  2183. else if (fs_info->fs_devices->num_devices < 4)
  2184. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1);
  2185. else
  2186. allowed |= (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  2187. BTRFS_BLOCK_GROUP_RAID10);
  2188. if (!profile_is_valid(bctl->data.target, 1) ||
  2189. bctl->data.target & ~allowed) {
  2190. printk(KERN_ERR "btrfs: unable to start balance with target "
  2191. "data profile %llu\n",
  2192. (unsigned long long)bctl->data.target);
  2193. ret = -EINVAL;
  2194. goto out;
  2195. }
  2196. if (!profile_is_valid(bctl->meta.target, 1) ||
  2197. bctl->meta.target & ~allowed) {
  2198. printk(KERN_ERR "btrfs: unable to start balance with target "
  2199. "metadata profile %llu\n",
  2200. (unsigned long long)bctl->meta.target);
  2201. ret = -EINVAL;
  2202. goto out;
  2203. }
  2204. if (!profile_is_valid(bctl->sys.target, 1) ||
  2205. bctl->sys.target & ~allowed) {
  2206. printk(KERN_ERR "btrfs: unable to start balance with target "
  2207. "system profile %llu\n",
  2208. (unsigned long long)bctl->sys.target);
  2209. ret = -EINVAL;
  2210. goto out;
  2211. }
  2212. if (bctl->data.target & BTRFS_BLOCK_GROUP_DUP) {
  2213. printk(KERN_ERR "btrfs: dup for data is not allowed\n");
  2214. ret = -EINVAL;
  2215. goto out;
  2216. }
  2217. /* allow to reduce meta or sys integrity only if force set */
  2218. allowed = BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1 |
  2219. BTRFS_BLOCK_GROUP_RAID10;
  2220. if (((bctl->sys.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2221. (fs_info->avail_system_alloc_bits & allowed) &&
  2222. !(bctl->sys.target & allowed)) ||
  2223. ((bctl->meta.flags & BTRFS_BALANCE_ARGS_CONVERT) &&
  2224. (fs_info->avail_metadata_alloc_bits & allowed) &&
  2225. !(bctl->meta.target & allowed))) {
  2226. if (bctl->flags & BTRFS_BALANCE_FORCE) {
  2227. printk(KERN_INFO "btrfs: force reducing metadata "
  2228. "integrity\n");
  2229. } else {
  2230. printk(KERN_ERR "btrfs: balance will reduce metadata "
  2231. "integrity, use force if you want this\n");
  2232. ret = -EINVAL;
  2233. goto out;
  2234. }
  2235. }
  2236. do_balance:
  2237. ret = insert_balance_item(fs_info->tree_root, bctl);
  2238. if (ret)
  2239. goto out;
  2240. set_balance_control(bctl);
  2241. mutex_unlock(&fs_info->balance_mutex);
  2242. ret = __btrfs_balance(fs_info);
  2243. mutex_lock(&fs_info->balance_mutex);
  2244. if (bargs) {
  2245. memset(bargs, 0, sizeof(*bargs));
  2246. update_ioctl_balance_args(fs_info, bargs);
  2247. }
  2248. __cancel_balance(fs_info);
  2249. return ret;
  2250. out:
  2251. kfree(bctl);
  2252. return ret;
  2253. }
  2254. /*
  2255. * shrinking a device means finding all of the device extents past
  2256. * the new size, and then following the back refs to the chunks.
  2257. * The chunk relocation code actually frees the device extent
  2258. */
  2259. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  2260. {
  2261. struct btrfs_trans_handle *trans;
  2262. struct btrfs_root *root = device->dev_root;
  2263. struct btrfs_dev_extent *dev_extent = NULL;
  2264. struct btrfs_path *path;
  2265. u64 length;
  2266. u64 chunk_tree;
  2267. u64 chunk_objectid;
  2268. u64 chunk_offset;
  2269. int ret;
  2270. int slot;
  2271. int failed = 0;
  2272. bool retried = false;
  2273. struct extent_buffer *l;
  2274. struct btrfs_key key;
  2275. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2276. u64 old_total = btrfs_super_total_bytes(super_copy);
  2277. u64 old_size = device->total_bytes;
  2278. u64 diff = device->total_bytes - new_size;
  2279. if (new_size >= device->total_bytes)
  2280. return -EINVAL;
  2281. path = btrfs_alloc_path();
  2282. if (!path)
  2283. return -ENOMEM;
  2284. path->reada = 2;
  2285. lock_chunks(root);
  2286. device->total_bytes = new_size;
  2287. if (device->writeable) {
  2288. device->fs_devices->total_rw_bytes -= diff;
  2289. spin_lock(&root->fs_info->free_chunk_lock);
  2290. root->fs_info->free_chunk_space -= diff;
  2291. spin_unlock(&root->fs_info->free_chunk_lock);
  2292. }
  2293. unlock_chunks(root);
  2294. again:
  2295. key.objectid = device->devid;
  2296. key.offset = (u64)-1;
  2297. key.type = BTRFS_DEV_EXTENT_KEY;
  2298. while (1) {
  2299. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2300. if (ret < 0)
  2301. goto done;
  2302. ret = btrfs_previous_item(root, path, 0, key.type);
  2303. if (ret < 0)
  2304. goto done;
  2305. if (ret) {
  2306. ret = 0;
  2307. btrfs_release_path(path);
  2308. break;
  2309. }
  2310. l = path->nodes[0];
  2311. slot = path->slots[0];
  2312. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  2313. if (key.objectid != device->devid) {
  2314. btrfs_release_path(path);
  2315. break;
  2316. }
  2317. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  2318. length = btrfs_dev_extent_length(l, dev_extent);
  2319. if (key.offset + length <= new_size) {
  2320. btrfs_release_path(path);
  2321. break;
  2322. }
  2323. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  2324. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  2325. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  2326. btrfs_release_path(path);
  2327. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  2328. chunk_offset);
  2329. if (ret && ret != -ENOSPC)
  2330. goto done;
  2331. if (ret == -ENOSPC)
  2332. failed++;
  2333. key.offset -= 1;
  2334. }
  2335. if (failed && !retried) {
  2336. failed = 0;
  2337. retried = true;
  2338. goto again;
  2339. } else if (failed && retried) {
  2340. ret = -ENOSPC;
  2341. lock_chunks(root);
  2342. device->total_bytes = old_size;
  2343. if (device->writeable)
  2344. device->fs_devices->total_rw_bytes += diff;
  2345. spin_lock(&root->fs_info->free_chunk_lock);
  2346. root->fs_info->free_chunk_space += diff;
  2347. spin_unlock(&root->fs_info->free_chunk_lock);
  2348. unlock_chunks(root);
  2349. goto done;
  2350. }
  2351. /* Shrinking succeeded, else we would be at "done". */
  2352. trans = btrfs_start_transaction(root, 0);
  2353. if (IS_ERR(trans)) {
  2354. ret = PTR_ERR(trans);
  2355. goto done;
  2356. }
  2357. lock_chunks(root);
  2358. device->disk_total_bytes = new_size;
  2359. /* Now btrfs_update_device() will change the on-disk size. */
  2360. ret = btrfs_update_device(trans, device);
  2361. if (ret) {
  2362. unlock_chunks(root);
  2363. btrfs_end_transaction(trans, root);
  2364. goto done;
  2365. }
  2366. WARN_ON(diff > old_total);
  2367. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  2368. unlock_chunks(root);
  2369. btrfs_end_transaction(trans, root);
  2370. done:
  2371. btrfs_free_path(path);
  2372. return ret;
  2373. }
  2374. static int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  2375. struct btrfs_root *root,
  2376. struct btrfs_key *key,
  2377. struct btrfs_chunk *chunk, int item_size)
  2378. {
  2379. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  2380. struct btrfs_disk_key disk_key;
  2381. u32 array_size;
  2382. u8 *ptr;
  2383. array_size = btrfs_super_sys_array_size(super_copy);
  2384. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  2385. return -EFBIG;
  2386. ptr = super_copy->sys_chunk_array + array_size;
  2387. btrfs_cpu_key_to_disk(&disk_key, key);
  2388. memcpy(ptr, &disk_key, sizeof(disk_key));
  2389. ptr += sizeof(disk_key);
  2390. memcpy(ptr, chunk, item_size);
  2391. item_size += sizeof(disk_key);
  2392. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2393. return 0;
  2394. }
  2395. /*
  2396. * sort the devices in descending order by max_avail, total_avail
  2397. */
  2398. static int btrfs_cmp_device_info(const void *a, const void *b)
  2399. {
  2400. const struct btrfs_device_info *di_a = a;
  2401. const struct btrfs_device_info *di_b = b;
  2402. if (di_a->max_avail > di_b->max_avail)
  2403. return -1;
  2404. if (di_a->max_avail < di_b->max_avail)
  2405. return 1;
  2406. if (di_a->total_avail > di_b->total_avail)
  2407. return -1;
  2408. if (di_a->total_avail < di_b->total_avail)
  2409. return 1;
  2410. return 0;
  2411. }
  2412. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2413. struct btrfs_root *extent_root,
  2414. struct map_lookup **map_ret,
  2415. u64 *num_bytes_out, u64 *stripe_size_out,
  2416. u64 start, u64 type)
  2417. {
  2418. struct btrfs_fs_info *info = extent_root->fs_info;
  2419. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2420. struct list_head *cur;
  2421. struct map_lookup *map = NULL;
  2422. struct extent_map_tree *em_tree;
  2423. struct extent_map *em;
  2424. struct btrfs_device_info *devices_info = NULL;
  2425. u64 total_avail;
  2426. int num_stripes; /* total number of stripes to allocate */
  2427. int sub_stripes; /* sub_stripes info for map */
  2428. int dev_stripes; /* stripes per dev */
  2429. int devs_max; /* max devs to use */
  2430. int devs_min; /* min devs needed */
  2431. int devs_increment; /* ndevs has to be a multiple of this */
  2432. int ncopies; /* how many copies to data has */
  2433. int ret;
  2434. u64 max_stripe_size;
  2435. u64 max_chunk_size;
  2436. u64 stripe_size;
  2437. u64 num_bytes;
  2438. int ndevs;
  2439. int i;
  2440. int j;
  2441. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2442. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2443. WARN_ON(1);
  2444. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2445. }
  2446. if (list_empty(&fs_devices->alloc_list))
  2447. return -ENOSPC;
  2448. sub_stripes = 1;
  2449. dev_stripes = 1;
  2450. devs_increment = 1;
  2451. ncopies = 1;
  2452. devs_max = 0; /* 0 == as many as possible */
  2453. devs_min = 1;
  2454. /*
  2455. * define the properties of each RAID type.
  2456. * FIXME: move this to a global table and use it in all RAID
  2457. * calculation code
  2458. */
  2459. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2460. dev_stripes = 2;
  2461. ncopies = 2;
  2462. devs_max = 1;
  2463. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2464. devs_min = 2;
  2465. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2466. devs_increment = 2;
  2467. ncopies = 2;
  2468. devs_max = 2;
  2469. devs_min = 2;
  2470. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2471. sub_stripes = 2;
  2472. devs_increment = 2;
  2473. ncopies = 2;
  2474. devs_min = 4;
  2475. } else {
  2476. devs_max = 1;
  2477. }
  2478. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2479. max_stripe_size = 1024 * 1024 * 1024;
  2480. max_chunk_size = 10 * max_stripe_size;
  2481. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2482. max_stripe_size = 256 * 1024 * 1024;
  2483. max_chunk_size = max_stripe_size;
  2484. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2485. max_stripe_size = 8 * 1024 * 1024;
  2486. max_chunk_size = 2 * max_stripe_size;
  2487. } else {
  2488. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2489. type);
  2490. BUG_ON(1);
  2491. }
  2492. /* we don't want a chunk larger than 10% of writeable space */
  2493. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2494. max_chunk_size);
  2495. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2496. GFP_NOFS);
  2497. if (!devices_info)
  2498. return -ENOMEM;
  2499. cur = fs_devices->alloc_list.next;
  2500. /*
  2501. * in the first pass through the devices list, we gather information
  2502. * about the available holes on each device.
  2503. */
  2504. ndevs = 0;
  2505. while (cur != &fs_devices->alloc_list) {
  2506. struct btrfs_device *device;
  2507. u64 max_avail;
  2508. u64 dev_offset;
  2509. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2510. cur = cur->next;
  2511. if (!device->writeable) {
  2512. printk(KERN_ERR
  2513. "btrfs: read-only device in alloc_list\n");
  2514. WARN_ON(1);
  2515. continue;
  2516. }
  2517. if (!device->in_fs_metadata)
  2518. continue;
  2519. if (device->total_bytes > device->bytes_used)
  2520. total_avail = device->total_bytes - device->bytes_used;
  2521. else
  2522. total_avail = 0;
  2523. /* If there is no space on this device, skip it. */
  2524. if (total_avail == 0)
  2525. continue;
  2526. ret = find_free_dev_extent(trans, device,
  2527. max_stripe_size * dev_stripes,
  2528. &dev_offset, &max_avail);
  2529. if (ret && ret != -ENOSPC)
  2530. goto error;
  2531. if (ret == 0)
  2532. max_avail = max_stripe_size * dev_stripes;
  2533. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2534. continue;
  2535. devices_info[ndevs].dev_offset = dev_offset;
  2536. devices_info[ndevs].max_avail = max_avail;
  2537. devices_info[ndevs].total_avail = total_avail;
  2538. devices_info[ndevs].dev = device;
  2539. ++ndevs;
  2540. }
  2541. /*
  2542. * now sort the devices by hole size / available space
  2543. */
  2544. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2545. btrfs_cmp_device_info, NULL);
  2546. /* round down to number of usable stripes */
  2547. ndevs -= ndevs % devs_increment;
  2548. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2549. ret = -ENOSPC;
  2550. goto error;
  2551. }
  2552. if (devs_max && ndevs > devs_max)
  2553. ndevs = devs_max;
  2554. /*
  2555. * the primary goal is to maximize the number of stripes, so use as many
  2556. * devices as possible, even if the stripes are not maximum sized.
  2557. */
  2558. stripe_size = devices_info[ndevs-1].max_avail;
  2559. num_stripes = ndevs * dev_stripes;
  2560. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2561. stripe_size = max_chunk_size * ncopies;
  2562. do_div(stripe_size, num_stripes);
  2563. }
  2564. do_div(stripe_size, dev_stripes);
  2565. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2566. stripe_size *= BTRFS_STRIPE_LEN;
  2567. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2568. if (!map) {
  2569. ret = -ENOMEM;
  2570. goto error;
  2571. }
  2572. map->num_stripes = num_stripes;
  2573. for (i = 0; i < ndevs; ++i) {
  2574. for (j = 0; j < dev_stripes; ++j) {
  2575. int s = i * dev_stripes + j;
  2576. map->stripes[s].dev = devices_info[i].dev;
  2577. map->stripes[s].physical = devices_info[i].dev_offset +
  2578. j * stripe_size;
  2579. }
  2580. }
  2581. map->sector_size = extent_root->sectorsize;
  2582. map->stripe_len = BTRFS_STRIPE_LEN;
  2583. map->io_align = BTRFS_STRIPE_LEN;
  2584. map->io_width = BTRFS_STRIPE_LEN;
  2585. map->type = type;
  2586. map->sub_stripes = sub_stripes;
  2587. *map_ret = map;
  2588. num_bytes = stripe_size * (num_stripes / ncopies);
  2589. *stripe_size_out = stripe_size;
  2590. *num_bytes_out = num_bytes;
  2591. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2592. em = alloc_extent_map();
  2593. if (!em) {
  2594. ret = -ENOMEM;
  2595. goto error;
  2596. }
  2597. em->bdev = (struct block_device *)map;
  2598. em->start = start;
  2599. em->len = num_bytes;
  2600. em->block_start = 0;
  2601. em->block_len = em->len;
  2602. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2603. write_lock(&em_tree->lock);
  2604. ret = add_extent_mapping(em_tree, em);
  2605. write_unlock(&em_tree->lock);
  2606. BUG_ON(ret);
  2607. free_extent_map(em);
  2608. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2609. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2610. start, num_bytes);
  2611. BUG_ON(ret);
  2612. for (i = 0; i < map->num_stripes; ++i) {
  2613. struct btrfs_device *device;
  2614. u64 dev_offset;
  2615. device = map->stripes[i].dev;
  2616. dev_offset = map->stripes[i].physical;
  2617. ret = btrfs_alloc_dev_extent(trans, device,
  2618. info->chunk_root->root_key.objectid,
  2619. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2620. start, dev_offset, stripe_size);
  2621. BUG_ON(ret);
  2622. }
  2623. kfree(devices_info);
  2624. return 0;
  2625. error:
  2626. kfree(map);
  2627. kfree(devices_info);
  2628. return ret;
  2629. }
  2630. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2631. struct btrfs_root *extent_root,
  2632. struct map_lookup *map, u64 chunk_offset,
  2633. u64 chunk_size, u64 stripe_size)
  2634. {
  2635. u64 dev_offset;
  2636. struct btrfs_key key;
  2637. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2638. struct btrfs_device *device;
  2639. struct btrfs_chunk *chunk;
  2640. struct btrfs_stripe *stripe;
  2641. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2642. int index = 0;
  2643. int ret;
  2644. chunk = kzalloc(item_size, GFP_NOFS);
  2645. if (!chunk)
  2646. return -ENOMEM;
  2647. index = 0;
  2648. while (index < map->num_stripes) {
  2649. device = map->stripes[index].dev;
  2650. device->bytes_used += stripe_size;
  2651. ret = btrfs_update_device(trans, device);
  2652. BUG_ON(ret);
  2653. index++;
  2654. }
  2655. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2656. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2657. map->num_stripes);
  2658. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2659. index = 0;
  2660. stripe = &chunk->stripe;
  2661. while (index < map->num_stripes) {
  2662. device = map->stripes[index].dev;
  2663. dev_offset = map->stripes[index].physical;
  2664. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2665. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2666. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2667. stripe++;
  2668. index++;
  2669. }
  2670. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2671. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2672. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2673. btrfs_set_stack_chunk_type(chunk, map->type);
  2674. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2675. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2676. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2677. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2678. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2679. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2680. key.type = BTRFS_CHUNK_ITEM_KEY;
  2681. key.offset = chunk_offset;
  2682. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2683. BUG_ON(ret);
  2684. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2685. ret = btrfs_add_system_chunk(trans, chunk_root, &key, chunk,
  2686. item_size);
  2687. BUG_ON(ret);
  2688. }
  2689. kfree(chunk);
  2690. return 0;
  2691. }
  2692. /*
  2693. * Chunk allocation falls into two parts. The first part does works
  2694. * that make the new allocated chunk useable, but not do any operation
  2695. * that modifies the chunk tree. The second part does the works that
  2696. * require modifying the chunk tree. This division is important for the
  2697. * bootstrap process of adding storage to a seed btrfs.
  2698. */
  2699. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2700. struct btrfs_root *extent_root, u64 type)
  2701. {
  2702. u64 chunk_offset;
  2703. u64 chunk_size;
  2704. u64 stripe_size;
  2705. struct map_lookup *map;
  2706. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2707. int ret;
  2708. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2709. &chunk_offset);
  2710. if (ret)
  2711. return ret;
  2712. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2713. &stripe_size, chunk_offset, type);
  2714. if (ret)
  2715. return ret;
  2716. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2717. chunk_size, stripe_size);
  2718. BUG_ON(ret);
  2719. return 0;
  2720. }
  2721. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2722. struct btrfs_root *root,
  2723. struct btrfs_device *device)
  2724. {
  2725. u64 chunk_offset;
  2726. u64 sys_chunk_offset;
  2727. u64 chunk_size;
  2728. u64 sys_chunk_size;
  2729. u64 stripe_size;
  2730. u64 sys_stripe_size;
  2731. u64 alloc_profile;
  2732. struct map_lookup *map;
  2733. struct map_lookup *sys_map;
  2734. struct btrfs_fs_info *fs_info = root->fs_info;
  2735. struct btrfs_root *extent_root = fs_info->extent_root;
  2736. int ret;
  2737. ret = find_next_chunk(fs_info->chunk_root,
  2738. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2739. if (ret)
  2740. return ret;
  2741. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2742. fs_info->avail_metadata_alloc_bits;
  2743. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2744. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2745. &stripe_size, chunk_offset, alloc_profile);
  2746. BUG_ON(ret);
  2747. sys_chunk_offset = chunk_offset + chunk_size;
  2748. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2749. fs_info->avail_system_alloc_bits;
  2750. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2751. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2752. &sys_chunk_size, &sys_stripe_size,
  2753. sys_chunk_offset, alloc_profile);
  2754. BUG_ON(ret);
  2755. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2756. BUG_ON(ret);
  2757. /*
  2758. * Modifying chunk tree needs allocating new blocks from both
  2759. * system block group and metadata block group. So we only can
  2760. * do operations require modifying the chunk tree after both
  2761. * block groups were created.
  2762. */
  2763. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2764. chunk_size, stripe_size);
  2765. BUG_ON(ret);
  2766. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2767. sys_chunk_offset, sys_chunk_size,
  2768. sys_stripe_size);
  2769. BUG_ON(ret);
  2770. return 0;
  2771. }
  2772. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2773. {
  2774. struct extent_map *em;
  2775. struct map_lookup *map;
  2776. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2777. int readonly = 0;
  2778. int i;
  2779. read_lock(&map_tree->map_tree.lock);
  2780. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2781. read_unlock(&map_tree->map_tree.lock);
  2782. if (!em)
  2783. return 1;
  2784. if (btrfs_test_opt(root, DEGRADED)) {
  2785. free_extent_map(em);
  2786. return 0;
  2787. }
  2788. map = (struct map_lookup *)em->bdev;
  2789. for (i = 0; i < map->num_stripes; i++) {
  2790. if (!map->stripes[i].dev->writeable) {
  2791. readonly = 1;
  2792. break;
  2793. }
  2794. }
  2795. free_extent_map(em);
  2796. return readonly;
  2797. }
  2798. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2799. {
  2800. extent_map_tree_init(&tree->map_tree);
  2801. }
  2802. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2803. {
  2804. struct extent_map *em;
  2805. while (1) {
  2806. write_lock(&tree->map_tree.lock);
  2807. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2808. if (em)
  2809. remove_extent_mapping(&tree->map_tree, em);
  2810. write_unlock(&tree->map_tree.lock);
  2811. if (!em)
  2812. break;
  2813. kfree(em->bdev);
  2814. /* once for us */
  2815. free_extent_map(em);
  2816. /* once for the tree */
  2817. free_extent_map(em);
  2818. }
  2819. }
  2820. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2821. {
  2822. struct extent_map *em;
  2823. struct map_lookup *map;
  2824. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2825. int ret;
  2826. read_lock(&em_tree->lock);
  2827. em = lookup_extent_mapping(em_tree, logical, len);
  2828. read_unlock(&em_tree->lock);
  2829. BUG_ON(!em);
  2830. BUG_ON(em->start > logical || em->start + em->len < logical);
  2831. map = (struct map_lookup *)em->bdev;
  2832. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2833. ret = map->num_stripes;
  2834. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2835. ret = map->sub_stripes;
  2836. else
  2837. ret = 1;
  2838. free_extent_map(em);
  2839. return ret;
  2840. }
  2841. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2842. int optimal)
  2843. {
  2844. int i;
  2845. if (map->stripes[optimal].dev->bdev)
  2846. return optimal;
  2847. for (i = first; i < first + num; i++) {
  2848. if (map->stripes[i].dev->bdev)
  2849. return i;
  2850. }
  2851. /* we couldn't find one that doesn't fail. Just return something
  2852. * and the io error handling code will clean up eventually
  2853. */
  2854. return optimal;
  2855. }
  2856. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2857. u64 logical, u64 *length,
  2858. struct btrfs_bio **bbio_ret,
  2859. int mirror_num)
  2860. {
  2861. struct extent_map *em;
  2862. struct map_lookup *map;
  2863. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2864. u64 offset;
  2865. u64 stripe_offset;
  2866. u64 stripe_end_offset;
  2867. u64 stripe_nr;
  2868. u64 stripe_nr_orig;
  2869. u64 stripe_nr_end;
  2870. int stripes_allocated = 8;
  2871. int stripes_required = 1;
  2872. int stripe_index;
  2873. int i;
  2874. int num_stripes;
  2875. int max_errors = 0;
  2876. struct btrfs_bio *bbio = NULL;
  2877. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2878. stripes_allocated = 1;
  2879. again:
  2880. if (bbio_ret) {
  2881. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2882. GFP_NOFS);
  2883. if (!bbio)
  2884. return -ENOMEM;
  2885. atomic_set(&bbio->error, 0);
  2886. }
  2887. read_lock(&em_tree->lock);
  2888. em = lookup_extent_mapping(em_tree, logical, *length);
  2889. read_unlock(&em_tree->lock);
  2890. if (!em) {
  2891. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2892. (unsigned long long)logical,
  2893. (unsigned long long)*length);
  2894. BUG();
  2895. }
  2896. BUG_ON(em->start > logical || em->start + em->len < logical);
  2897. map = (struct map_lookup *)em->bdev;
  2898. offset = logical - em->start;
  2899. if (mirror_num > map->num_stripes)
  2900. mirror_num = 0;
  2901. /* if our btrfs_bio struct is too small, back off and try again */
  2902. if (rw & REQ_WRITE) {
  2903. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2904. BTRFS_BLOCK_GROUP_DUP)) {
  2905. stripes_required = map->num_stripes;
  2906. max_errors = 1;
  2907. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2908. stripes_required = map->sub_stripes;
  2909. max_errors = 1;
  2910. }
  2911. }
  2912. if (rw & REQ_DISCARD) {
  2913. if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK)
  2914. stripes_required = map->num_stripes;
  2915. }
  2916. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2917. stripes_allocated < stripes_required) {
  2918. stripes_allocated = map->num_stripes;
  2919. free_extent_map(em);
  2920. kfree(bbio);
  2921. goto again;
  2922. }
  2923. stripe_nr = offset;
  2924. /*
  2925. * stripe_nr counts the total number of stripes we have to stride
  2926. * to get to this block
  2927. */
  2928. do_div(stripe_nr, map->stripe_len);
  2929. stripe_offset = stripe_nr * map->stripe_len;
  2930. BUG_ON(offset < stripe_offset);
  2931. /* stripe_offset is the offset of this block in its stripe*/
  2932. stripe_offset = offset - stripe_offset;
  2933. if (rw & REQ_DISCARD)
  2934. *length = min_t(u64, em->len - offset, *length);
  2935. else if (map->type & BTRFS_BLOCK_GROUP_PROFILE_MASK) {
  2936. /* we limit the length of each bio to what fits in a stripe */
  2937. *length = min_t(u64, em->len - offset,
  2938. map->stripe_len - stripe_offset);
  2939. } else {
  2940. *length = em->len - offset;
  2941. }
  2942. if (!bbio_ret)
  2943. goto out;
  2944. num_stripes = 1;
  2945. stripe_index = 0;
  2946. stripe_nr_orig = stripe_nr;
  2947. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2948. (~(map->stripe_len - 1));
  2949. do_div(stripe_nr_end, map->stripe_len);
  2950. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2951. (offset + *length);
  2952. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2953. if (rw & REQ_DISCARD)
  2954. num_stripes = min_t(u64, map->num_stripes,
  2955. stripe_nr_end - stripe_nr_orig);
  2956. stripe_index = do_div(stripe_nr, map->num_stripes);
  2957. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2958. if (rw & (REQ_WRITE | REQ_DISCARD))
  2959. num_stripes = map->num_stripes;
  2960. else if (mirror_num)
  2961. stripe_index = mirror_num - 1;
  2962. else {
  2963. stripe_index = find_live_mirror(map, 0,
  2964. map->num_stripes,
  2965. current->pid % map->num_stripes);
  2966. mirror_num = stripe_index + 1;
  2967. }
  2968. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2969. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  2970. num_stripes = map->num_stripes;
  2971. } else if (mirror_num) {
  2972. stripe_index = mirror_num - 1;
  2973. } else {
  2974. mirror_num = 1;
  2975. }
  2976. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2977. int factor = map->num_stripes / map->sub_stripes;
  2978. stripe_index = do_div(stripe_nr, factor);
  2979. stripe_index *= map->sub_stripes;
  2980. if (rw & REQ_WRITE)
  2981. num_stripes = map->sub_stripes;
  2982. else if (rw & REQ_DISCARD)
  2983. num_stripes = min_t(u64, map->sub_stripes *
  2984. (stripe_nr_end - stripe_nr_orig),
  2985. map->num_stripes);
  2986. else if (mirror_num)
  2987. stripe_index += mirror_num - 1;
  2988. else {
  2989. stripe_index = find_live_mirror(map, stripe_index,
  2990. map->sub_stripes, stripe_index +
  2991. current->pid % map->sub_stripes);
  2992. mirror_num = stripe_index + 1;
  2993. }
  2994. } else {
  2995. /*
  2996. * after this do_div call, stripe_nr is the number of stripes
  2997. * on this device we have to walk to find the data, and
  2998. * stripe_index is the number of our device in the stripe array
  2999. */
  3000. stripe_index = do_div(stripe_nr, map->num_stripes);
  3001. mirror_num = stripe_index + 1;
  3002. }
  3003. BUG_ON(stripe_index >= map->num_stripes);
  3004. if (rw & REQ_DISCARD) {
  3005. for (i = 0; i < num_stripes; i++) {
  3006. bbio->stripes[i].physical =
  3007. map->stripes[stripe_index].physical +
  3008. stripe_offset + stripe_nr * map->stripe_len;
  3009. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  3010. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3011. u64 stripes;
  3012. u32 last_stripe = 0;
  3013. int j;
  3014. div_u64_rem(stripe_nr_end - 1,
  3015. map->num_stripes,
  3016. &last_stripe);
  3017. for (j = 0; j < map->num_stripes; j++) {
  3018. u32 test;
  3019. div_u64_rem(stripe_nr_end - 1 - j,
  3020. map->num_stripes, &test);
  3021. if (test == stripe_index)
  3022. break;
  3023. }
  3024. stripes = stripe_nr_end - 1 - j;
  3025. do_div(stripes, map->num_stripes);
  3026. bbio->stripes[i].length = map->stripe_len *
  3027. (stripes - stripe_nr + 1);
  3028. if (i == 0) {
  3029. bbio->stripes[i].length -=
  3030. stripe_offset;
  3031. stripe_offset = 0;
  3032. }
  3033. if (stripe_index == last_stripe)
  3034. bbio->stripes[i].length -=
  3035. stripe_end_offset;
  3036. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3037. u64 stripes;
  3038. int j;
  3039. int factor = map->num_stripes /
  3040. map->sub_stripes;
  3041. u32 last_stripe = 0;
  3042. div_u64_rem(stripe_nr_end - 1,
  3043. factor, &last_stripe);
  3044. last_stripe *= map->sub_stripes;
  3045. for (j = 0; j < factor; j++) {
  3046. u32 test;
  3047. div_u64_rem(stripe_nr_end - 1 - j,
  3048. factor, &test);
  3049. if (test ==
  3050. stripe_index / map->sub_stripes)
  3051. break;
  3052. }
  3053. stripes = stripe_nr_end - 1 - j;
  3054. do_div(stripes, factor);
  3055. bbio->stripes[i].length = map->stripe_len *
  3056. (stripes - stripe_nr + 1);
  3057. if (i < map->sub_stripes) {
  3058. bbio->stripes[i].length -=
  3059. stripe_offset;
  3060. if (i == map->sub_stripes - 1)
  3061. stripe_offset = 0;
  3062. }
  3063. if (stripe_index >= last_stripe &&
  3064. stripe_index <= (last_stripe +
  3065. map->sub_stripes - 1)) {
  3066. bbio->stripes[i].length -=
  3067. stripe_end_offset;
  3068. }
  3069. } else
  3070. bbio->stripes[i].length = *length;
  3071. stripe_index++;
  3072. if (stripe_index == map->num_stripes) {
  3073. /* This could only happen for RAID0/10 */
  3074. stripe_index = 0;
  3075. stripe_nr++;
  3076. }
  3077. }
  3078. } else {
  3079. for (i = 0; i < num_stripes; i++) {
  3080. bbio->stripes[i].physical =
  3081. map->stripes[stripe_index].physical +
  3082. stripe_offset +
  3083. stripe_nr * map->stripe_len;
  3084. bbio->stripes[i].dev =
  3085. map->stripes[stripe_index].dev;
  3086. stripe_index++;
  3087. }
  3088. }
  3089. if (bbio_ret) {
  3090. *bbio_ret = bbio;
  3091. bbio->num_stripes = num_stripes;
  3092. bbio->max_errors = max_errors;
  3093. bbio->mirror_num = mirror_num;
  3094. }
  3095. out:
  3096. free_extent_map(em);
  3097. return 0;
  3098. }
  3099. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  3100. u64 logical, u64 *length,
  3101. struct btrfs_bio **bbio_ret, int mirror_num)
  3102. {
  3103. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  3104. mirror_num);
  3105. }
  3106. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  3107. u64 chunk_start, u64 physical, u64 devid,
  3108. u64 **logical, int *naddrs, int *stripe_len)
  3109. {
  3110. struct extent_map_tree *em_tree = &map_tree->map_tree;
  3111. struct extent_map *em;
  3112. struct map_lookup *map;
  3113. u64 *buf;
  3114. u64 bytenr;
  3115. u64 length;
  3116. u64 stripe_nr;
  3117. int i, j, nr = 0;
  3118. read_lock(&em_tree->lock);
  3119. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  3120. read_unlock(&em_tree->lock);
  3121. BUG_ON(!em || em->start != chunk_start);
  3122. map = (struct map_lookup *)em->bdev;
  3123. length = em->len;
  3124. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  3125. do_div(length, map->num_stripes / map->sub_stripes);
  3126. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  3127. do_div(length, map->num_stripes);
  3128. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  3129. BUG_ON(!buf);
  3130. for (i = 0; i < map->num_stripes; i++) {
  3131. if (devid && map->stripes[i].dev->devid != devid)
  3132. continue;
  3133. if (map->stripes[i].physical > physical ||
  3134. map->stripes[i].physical + length <= physical)
  3135. continue;
  3136. stripe_nr = physical - map->stripes[i].physical;
  3137. do_div(stripe_nr, map->stripe_len);
  3138. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  3139. stripe_nr = stripe_nr * map->num_stripes + i;
  3140. do_div(stripe_nr, map->sub_stripes);
  3141. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  3142. stripe_nr = stripe_nr * map->num_stripes + i;
  3143. }
  3144. bytenr = chunk_start + stripe_nr * map->stripe_len;
  3145. WARN_ON(nr >= map->num_stripes);
  3146. for (j = 0; j < nr; j++) {
  3147. if (buf[j] == bytenr)
  3148. break;
  3149. }
  3150. if (j == nr) {
  3151. WARN_ON(nr >= map->num_stripes);
  3152. buf[nr++] = bytenr;
  3153. }
  3154. }
  3155. *logical = buf;
  3156. *naddrs = nr;
  3157. *stripe_len = map->stripe_len;
  3158. free_extent_map(em);
  3159. return 0;
  3160. }
  3161. static void btrfs_end_bio(struct bio *bio, int err)
  3162. {
  3163. struct btrfs_bio *bbio = bio->bi_private;
  3164. int is_orig_bio = 0;
  3165. if (err)
  3166. atomic_inc(&bbio->error);
  3167. if (bio == bbio->orig_bio)
  3168. is_orig_bio = 1;
  3169. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  3170. if (!is_orig_bio) {
  3171. bio_put(bio);
  3172. bio = bbio->orig_bio;
  3173. }
  3174. bio->bi_private = bbio->private;
  3175. bio->bi_end_io = bbio->end_io;
  3176. bio->bi_bdev = (struct block_device *)
  3177. (unsigned long)bbio->mirror_num;
  3178. /* only send an error to the higher layers if it is
  3179. * beyond the tolerance of the multi-bio
  3180. */
  3181. if (atomic_read(&bbio->error) > bbio->max_errors) {
  3182. err = -EIO;
  3183. } else {
  3184. /*
  3185. * this bio is actually up to date, we didn't
  3186. * go over the max number of errors
  3187. */
  3188. set_bit(BIO_UPTODATE, &bio->bi_flags);
  3189. err = 0;
  3190. }
  3191. kfree(bbio);
  3192. bio_endio(bio, err);
  3193. } else if (!is_orig_bio) {
  3194. bio_put(bio);
  3195. }
  3196. }
  3197. struct async_sched {
  3198. struct bio *bio;
  3199. int rw;
  3200. struct btrfs_fs_info *info;
  3201. struct btrfs_work work;
  3202. };
  3203. /*
  3204. * see run_scheduled_bios for a description of why bios are collected for
  3205. * async submit.
  3206. *
  3207. * This will add one bio to the pending list for a device and make sure
  3208. * the work struct is scheduled.
  3209. */
  3210. static noinline int schedule_bio(struct btrfs_root *root,
  3211. struct btrfs_device *device,
  3212. int rw, struct bio *bio)
  3213. {
  3214. int should_queue = 1;
  3215. struct btrfs_pending_bios *pending_bios;
  3216. /* don't bother with additional async steps for reads, right now */
  3217. if (!(rw & REQ_WRITE)) {
  3218. bio_get(bio);
  3219. submit_bio(rw, bio);
  3220. bio_put(bio);
  3221. return 0;
  3222. }
  3223. /*
  3224. * nr_async_bios allows us to reliably return congestion to the
  3225. * higher layers. Otherwise, the async bio makes it appear we have
  3226. * made progress against dirty pages when we've really just put it
  3227. * on a queue for later
  3228. */
  3229. atomic_inc(&root->fs_info->nr_async_bios);
  3230. WARN_ON(bio->bi_next);
  3231. bio->bi_next = NULL;
  3232. bio->bi_rw |= rw;
  3233. spin_lock(&device->io_lock);
  3234. if (bio->bi_rw & REQ_SYNC)
  3235. pending_bios = &device->pending_sync_bios;
  3236. else
  3237. pending_bios = &device->pending_bios;
  3238. if (pending_bios->tail)
  3239. pending_bios->tail->bi_next = bio;
  3240. pending_bios->tail = bio;
  3241. if (!pending_bios->head)
  3242. pending_bios->head = bio;
  3243. if (device->running_pending)
  3244. should_queue = 0;
  3245. spin_unlock(&device->io_lock);
  3246. if (should_queue)
  3247. btrfs_queue_worker(&root->fs_info->submit_workers,
  3248. &device->work);
  3249. return 0;
  3250. }
  3251. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  3252. int mirror_num, int async_submit)
  3253. {
  3254. struct btrfs_mapping_tree *map_tree;
  3255. struct btrfs_device *dev;
  3256. struct bio *first_bio = bio;
  3257. u64 logical = (u64)bio->bi_sector << 9;
  3258. u64 length = 0;
  3259. u64 map_length;
  3260. int ret;
  3261. int dev_nr = 0;
  3262. int total_devs = 1;
  3263. struct btrfs_bio *bbio = NULL;
  3264. length = bio->bi_size;
  3265. map_tree = &root->fs_info->mapping_tree;
  3266. map_length = length;
  3267. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  3268. mirror_num);
  3269. BUG_ON(ret);
  3270. total_devs = bbio->num_stripes;
  3271. if (map_length < length) {
  3272. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  3273. "len %llu\n", (unsigned long long)logical,
  3274. (unsigned long long)length,
  3275. (unsigned long long)map_length);
  3276. BUG();
  3277. }
  3278. bbio->orig_bio = first_bio;
  3279. bbio->private = first_bio->bi_private;
  3280. bbio->end_io = first_bio->bi_end_io;
  3281. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  3282. while (dev_nr < total_devs) {
  3283. if (dev_nr < total_devs - 1) {
  3284. bio = bio_clone(first_bio, GFP_NOFS);
  3285. BUG_ON(!bio);
  3286. } else {
  3287. bio = first_bio;
  3288. }
  3289. bio->bi_private = bbio;
  3290. bio->bi_end_io = btrfs_end_bio;
  3291. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  3292. dev = bbio->stripes[dev_nr].dev;
  3293. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  3294. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  3295. "(%s id %llu), size=%u\n", rw,
  3296. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  3297. dev->name, dev->devid, bio->bi_size);
  3298. bio->bi_bdev = dev->bdev;
  3299. if (async_submit)
  3300. schedule_bio(root, dev, rw, bio);
  3301. else
  3302. submit_bio(rw, bio);
  3303. } else {
  3304. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  3305. bio->bi_sector = logical >> 9;
  3306. bio_endio(bio, -EIO);
  3307. }
  3308. dev_nr++;
  3309. }
  3310. return 0;
  3311. }
  3312. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  3313. u8 *uuid, u8 *fsid)
  3314. {
  3315. struct btrfs_device *device;
  3316. struct btrfs_fs_devices *cur_devices;
  3317. cur_devices = root->fs_info->fs_devices;
  3318. while (cur_devices) {
  3319. if (!fsid ||
  3320. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3321. device = __find_device(&cur_devices->devices,
  3322. devid, uuid);
  3323. if (device)
  3324. return device;
  3325. }
  3326. cur_devices = cur_devices->seed;
  3327. }
  3328. return NULL;
  3329. }
  3330. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  3331. u64 devid, u8 *dev_uuid)
  3332. {
  3333. struct btrfs_device *device;
  3334. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  3335. device = kzalloc(sizeof(*device), GFP_NOFS);
  3336. if (!device)
  3337. return NULL;
  3338. list_add(&device->dev_list,
  3339. &fs_devices->devices);
  3340. device->dev_root = root->fs_info->dev_root;
  3341. device->devid = devid;
  3342. device->work.func = pending_bios_fn;
  3343. device->fs_devices = fs_devices;
  3344. device->missing = 1;
  3345. fs_devices->num_devices++;
  3346. fs_devices->missing_devices++;
  3347. spin_lock_init(&device->io_lock);
  3348. INIT_LIST_HEAD(&device->dev_alloc_list);
  3349. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  3350. return device;
  3351. }
  3352. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  3353. struct extent_buffer *leaf,
  3354. struct btrfs_chunk *chunk)
  3355. {
  3356. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  3357. struct map_lookup *map;
  3358. struct extent_map *em;
  3359. u64 logical;
  3360. u64 length;
  3361. u64 devid;
  3362. u8 uuid[BTRFS_UUID_SIZE];
  3363. int num_stripes;
  3364. int ret;
  3365. int i;
  3366. logical = key->offset;
  3367. length = btrfs_chunk_length(leaf, chunk);
  3368. read_lock(&map_tree->map_tree.lock);
  3369. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  3370. read_unlock(&map_tree->map_tree.lock);
  3371. /* already mapped? */
  3372. if (em && em->start <= logical && em->start + em->len > logical) {
  3373. free_extent_map(em);
  3374. return 0;
  3375. } else if (em) {
  3376. free_extent_map(em);
  3377. }
  3378. em = alloc_extent_map();
  3379. if (!em)
  3380. return -ENOMEM;
  3381. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3382. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3383. if (!map) {
  3384. free_extent_map(em);
  3385. return -ENOMEM;
  3386. }
  3387. em->bdev = (struct block_device *)map;
  3388. em->start = logical;
  3389. em->len = length;
  3390. em->block_start = 0;
  3391. em->block_len = em->len;
  3392. map->num_stripes = num_stripes;
  3393. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3394. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3395. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3396. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3397. map->type = btrfs_chunk_type(leaf, chunk);
  3398. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3399. for (i = 0; i < num_stripes; i++) {
  3400. map->stripes[i].physical =
  3401. btrfs_stripe_offset_nr(leaf, chunk, i);
  3402. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3403. read_extent_buffer(leaf, uuid, (unsigned long)
  3404. btrfs_stripe_dev_uuid_nr(chunk, i),
  3405. BTRFS_UUID_SIZE);
  3406. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3407. NULL);
  3408. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3409. kfree(map);
  3410. free_extent_map(em);
  3411. return -EIO;
  3412. }
  3413. if (!map->stripes[i].dev) {
  3414. map->stripes[i].dev =
  3415. add_missing_dev(root, devid, uuid);
  3416. if (!map->stripes[i].dev) {
  3417. kfree(map);
  3418. free_extent_map(em);
  3419. return -EIO;
  3420. }
  3421. }
  3422. map->stripes[i].dev->in_fs_metadata = 1;
  3423. }
  3424. write_lock(&map_tree->map_tree.lock);
  3425. ret = add_extent_mapping(&map_tree->map_tree, em);
  3426. write_unlock(&map_tree->map_tree.lock);
  3427. BUG_ON(ret);
  3428. free_extent_map(em);
  3429. return 0;
  3430. }
  3431. static int fill_device_from_item(struct extent_buffer *leaf,
  3432. struct btrfs_dev_item *dev_item,
  3433. struct btrfs_device *device)
  3434. {
  3435. unsigned long ptr;
  3436. device->devid = btrfs_device_id(leaf, dev_item);
  3437. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3438. device->total_bytes = device->disk_total_bytes;
  3439. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3440. device->type = btrfs_device_type(leaf, dev_item);
  3441. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3442. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3443. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3444. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3445. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3446. return 0;
  3447. }
  3448. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3449. {
  3450. struct btrfs_fs_devices *fs_devices;
  3451. int ret;
  3452. mutex_lock(&uuid_mutex);
  3453. fs_devices = root->fs_info->fs_devices->seed;
  3454. while (fs_devices) {
  3455. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3456. ret = 0;
  3457. goto out;
  3458. }
  3459. fs_devices = fs_devices->seed;
  3460. }
  3461. fs_devices = find_fsid(fsid);
  3462. if (!fs_devices) {
  3463. ret = -ENOENT;
  3464. goto out;
  3465. }
  3466. fs_devices = clone_fs_devices(fs_devices);
  3467. if (IS_ERR(fs_devices)) {
  3468. ret = PTR_ERR(fs_devices);
  3469. goto out;
  3470. }
  3471. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3472. root->fs_info->bdev_holder);
  3473. if (ret)
  3474. goto out;
  3475. if (!fs_devices->seeding) {
  3476. __btrfs_close_devices(fs_devices);
  3477. free_fs_devices(fs_devices);
  3478. ret = -EINVAL;
  3479. goto out;
  3480. }
  3481. fs_devices->seed = root->fs_info->fs_devices->seed;
  3482. root->fs_info->fs_devices->seed = fs_devices;
  3483. out:
  3484. mutex_unlock(&uuid_mutex);
  3485. return ret;
  3486. }
  3487. static int read_one_dev(struct btrfs_root *root,
  3488. struct extent_buffer *leaf,
  3489. struct btrfs_dev_item *dev_item)
  3490. {
  3491. struct btrfs_device *device;
  3492. u64 devid;
  3493. int ret;
  3494. u8 fs_uuid[BTRFS_UUID_SIZE];
  3495. u8 dev_uuid[BTRFS_UUID_SIZE];
  3496. devid = btrfs_device_id(leaf, dev_item);
  3497. read_extent_buffer(leaf, dev_uuid,
  3498. (unsigned long)btrfs_device_uuid(dev_item),
  3499. BTRFS_UUID_SIZE);
  3500. read_extent_buffer(leaf, fs_uuid,
  3501. (unsigned long)btrfs_device_fsid(dev_item),
  3502. BTRFS_UUID_SIZE);
  3503. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3504. ret = open_seed_devices(root, fs_uuid);
  3505. if (ret && !btrfs_test_opt(root, DEGRADED))
  3506. return ret;
  3507. }
  3508. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3509. if (!device || !device->bdev) {
  3510. if (!btrfs_test_opt(root, DEGRADED))
  3511. return -EIO;
  3512. if (!device) {
  3513. printk(KERN_WARNING "warning devid %llu missing\n",
  3514. (unsigned long long)devid);
  3515. device = add_missing_dev(root, devid, dev_uuid);
  3516. if (!device)
  3517. return -ENOMEM;
  3518. } else if (!device->missing) {
  3519. /*
  3520. * this happens when a device that was properly setup
  3521. * in the device info lists suddenly goes bad.
  3522. * device->bdev is NULL, and so we have to set
  3523. * device->missing to one here
  3524. */
  3525. root->fs_info->fs_devices->missing_devices++;
  3526. device->missing = 1;
  3527. }
  3528. }
  3529. if (device->fs_devices != root->fs_info->fs_devices) {
  3530. BUG_ON(device->writeable);
  3531. if (device->generation !=
  3532. btrfs_device_generation(leaf, dev_item))
  3533. return -EINVAL;
  3534. }
  3535. fill_device_from_item(leaf, dev_item, device);
  3536. device->dev_root = root->fs_info->dev_root;
  3537. device->in_fs_metadata = 1;
  3538. if (device->writeable) {
  3539. device->fs_devices->total_rw_bytes += device->total_bytes;
  3540. spin_lock(&root->fs_info->free_chunk_lock);
  3541. root->fs_info->free_chunk_space += device->total_bytes -
  3542. device->bytes_used;
  3543. spin_unlock(&root->fs_info->free_chunk_lock);
  3544. }
  3545. ret = 0;
  3546. return ret;
  3547. }
  3548. int btrfs_read_sys_array(struct btrfs_root *root)
  3549. {
  3550. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3551. struct extent_buffer *sb;
  3552. struct btrfs_disk_key *disk_key;
  3553. struct btrfs_chunk *chunk;
  3554. u8 *ptr;
  3555. unsigned long sb_ptr;
  3556. int ret = 0;
  3557. u32 num_stripes;
  3558. u32 array_size;
  3559. u32 len = 0;
  3560. u32 cur;
  3561. struct btrfs_key key;
  3562. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3563. BTRFS_SUPER_INFO_SIZE);
  3564. if (!sb)
  3565. return -ENOMEM;
  3566. btrfs_set_buffer_uptodate(sb);
  3567. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3568. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3569. array_size = btrfs_super_sys_array_size(super_copy);
  3570. ptr = super_copy->sys_chunk_array;
  3571. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3572. cur = 0;
  3573. while (cur < array_size) {
  3574. disk_key = (struct btrfs_disk_key *)ptr;
  3575. btrfs_disk_key_to_cpu(&key, disk_key);
  3576. len = sizeof(*disk_key); ptr += len;
  3577. sb_ptr += len;
  3578. cur += len;
  3579. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3580. chunk = (struct btrfs_chunk *)sb_ptr;
  3581. ret = read_one_chunk(root, &key, sb, chunk);
  3582. if (ret)
  3583. break;
  3584. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3585. len = btrfs_chunk_item_size(num_stripes);
  3586. } else {
  3587. ret = -EIO;
  3588. break;
  3589. }
  3590. ptr += len;
  3591. sb_ptr += len;
  3592. cur += len;
  3593. }
  3594. free_extent_buffer(sb);
  3595. return ret;
  3596. }
  3597. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3598. {
  3599. struct btrfs_path *path;
  3600. struct extent_buffer *leaf;
  3601. struct btrfs_key key;
  3602. struct btrfs_key found_key;
  3603. int ret;
  3604. int slot;
  3605. root = root->fs_info->chunk_root;
  3606. path = btrfs_alloc_path();
  3607. if (!path)
  3608. return -ENOMEM;
  3609. /* first we search for all of the device items, and then we
  3610. * read in all of the chunk items. This way we can create chunk
  3611. * mappings that reference all of the devices that are afound
  3612. */
  3613. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3614. key.offset = 0;
  3615. key.type = 0;
  3616. again:
  3617. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3618. if (ret < 0)
  3619. goto error;
  3620. while (1) {
  3621. leaf = path->nodes[0];
  3622. slot = path->slots[0];
  3623. if (slot >= btrfs_header_nritems(leaf)) {
  3624. ret = btrfs_next_leaf(root, path);
  3625. if (ret == 0)
  3626. continue;
  3627. if (ret < 0)
  3628. goto error;
  3629. break;
  3630. }
  3631. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3632. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3633. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3634. break;
  3635. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3636. struct btrfs_dev_item *dev_item;
  3637. dev_item = btrfs_item_ptr(leaf, slot,
  3638. struct btrfs_dev_item);
  3639. ret = read_one_dev(root, leaf, dev_item);
  3640. if (ret)
  3641. goto error;
  3642. }
  3643. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3644. struct btrfs_chunk *chunk;
  3645. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3646. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3647. if (ret)
  3648. goto error;
  3649. }
  3650. path->slots[0]++;
  3651. }
  3652. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3653. key.objectid = 0;
  3654. btrfs_release_path(path);
  3655. goto again;
  3656. }
  3657. ret = 0;
  3658. error:
  3659. btrfs_free_path(path);
  3660. return ret;
  3661. }