volumes.c 95 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800
  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. * @device: the device which we search the free space in
  718. * @num_bytes: the size of the free space that we need
  719. * @start: store the start of the free space.
  720. * @len: the size of the free space. that we find, or the size of the max
  721. * free space if we don't find suitable free space
  722. *
  723. * this uses a pretty simple search, the expectation is that it is
  724. * called very infrequently and that a given device has a small number
  725. * of extents
  726. *
  727. * @start is used to store the start of the free space if we find. But if we
  728. * don't find suitable free space, it will be used to store the start position
  729. * of the max free space.
  730. *
  731. * @len is used to store the size of the free space that we find.
  732. * But if we don't find suitable free space, it is used to store the size of
  733. * the max free space.
  734. */
  735. int find_free_dev_extent(struct btrfs_device *device, u64 num_bytes,
  736. u64 *start, u64 *len)
  737. {
  738. struct btrfs_key key;
  739. struct btrfs_root *root = device->dev_root;
  740. struct btrfs_dev_extent *dev_extent;
  741. struct btrfs_path *path;
  742. u64 hole_size;
  743. u64 max_hole_start;
  744. u64 max_hole_size;
  745. u64 extent_end;
  746. u64 search_start;
  747. u64 search_end = device->total_bytes;
  748. int ret;
  749. int slot;
  750. struct extent_buffer *l;
  751. /* FIXME use last free of some kind */
  752. /* we don't want to overwrite the superblock on the drive,
  753. * so we make sure to start at an offset of at least 1MB
  754. */
  755. search_start = max(root->fs_info->alloc_start, 1024ull * 1024);
  756. max_hole_start = search_start;
  757. max_hole_size = 0;
  758. hole_size = 0;
  759. if (search_start >= search_end) {
  760. ret = -ENOSPC;
  761. goto error;
  762. }
  763. path = btrfs_alloc_path();
  764. if (!path) {
  765. ret = -ENOMEM;
  766. goto error;
  767. }
  768. path->reada = 2;
  769. key.objectid = device->devid;
  770. key.offset = search_start;
  771. key.type = BTRFS_DEV_EXTENT_KEY;
  772. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  773. if (ret < 0)
  774. goto out;
  775. if (ret > 0) {
  776. ret = btrfs_previous_item(root, path, key.objectid, key.type);
  777. if (ret < 0)
  778. goto out;
  779. }
  780. while (1) {
  781. l = path->nodes[0];
  782. slot = path->slots[0];
  783. if (slot >= btrfs_header_nritems(l)) {
  784. ret = btrfs_next_leaf(root, path);
  785. if (ret == 0)
  786. continue;
  787. if (ret < 0)
  788. goto out;
  789. break;
  790. }
  791. btrfs_item_key_to_cpu(l, &key, slot);
  792. if (key.objectid < device->devid)
  793. goto next;
  794. if (key.objectid > device->devid)
  795. break;
  796. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY)
  797. goto next;
  798. if (key.offset > search_start) {
  799. hole_size = key.offset - search_start;
  800. if (hole_size > max_hole_size) {
  801. max_hole_start = search_start;
  802. max_hole_size = hole_size;
  803. }
  804. /*
  805. * If this free space is greater than which we need,
  806. * it must be the max free space that we have found
  807. * until now, so max_hole_start must point to the start
  808. * of this free space and the length of this free space
  809. * is stored in max_hole_size. Thus, we return
  810. * max_hole_start and max_hole_size and go back to the
  811. * caller.
  812. */
  813. if (hole_size >= num_bytes) {
  814. ret = 0;
  815. goto out;
  816. }
  817. }
  818. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  819. extent_end = key.offset + btrfs_dev_extent_length(l,
  820. dev_extent);
  821. if (extent_end > search_start)
  822. search_start = extent_end;
  823. next:
  824. path->slots[0]++;
  825. cond_resched();
  826. }
  827. /*
  828. * At this point, search_start should be the end of
  829. * allocated dev extents, and when shrinking the device,
  830. * search_end may be smaller than search_start.
  831. */
  832. if (search_end > search_start)
  833. hole_size = search_end - search_start;
  834. if (hole_size > max_hole_size) {
  835. max_hole_start = search_start;
  836. max_hole_size = hole_size;
  837. }
  838. /* See above. */
  839. if (hole_size < num_bytes)
  840. ret = -ENOSPC;
  841. else
  842. ret = 0;
  843. out:
  844. btrfs_free_path(path);
  845. error:
  846. *start = max_hole_start;
  847. if (len)
  848. *len = max_hole_size;
  849. return ret;
  850. }
  851. static int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  852. struct btrfs_device *device,
  853. u64 start)
  854. {
  855. int ret;
  856. struct btrfs_path *path;
  857. struct btrfs_root *root = device->dev_root;
  858. struct btrfs_key key;
  859. struct btrfs_key found_key;
  860. struct extent_buffer *leaf = NULL;
  861. struct btrfs_dev_extent *extent = NULL;
  862. path = btrfs_alloc_path();
  863. if (!path)
  864. return -ENOMEM;
  865. key.objectid = device->devid;
  866. key.offset = start;
  867. key.type = BTRFS_DEV_EXTENT_KEY;
  868. again:
  869. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  870. if (ret > 0) {
  871. ret = btrfs_previous_item(root, path, key.objectid,
  872. BTRFS_DEV_EXTENT_KEY);
  873. if (ret)
  874. goto out;
  875. leaf = path->nodes[0];
  876. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  877. extent = btrfs_item_ptr(leaf, path->slots[0],
  878. struct btrfs_dev_extent);
  879. BUG_ON(found_key.offset > start || found_key.offset +
  880. btrfs_dev_extent_length(leaf, extent) < start);
  881. key = found_key;
  882. btrfs_release_path(path);
  883. goto again;
  884. } else if (ret == 0) {
  885. leaf = path->nodes[0];
  886. extent = btrfs_item_ptr(leaf, path->slots[0],
  887. struct btrfs_dev_extent);
  888. }
  889. BUG_ON(ret);
  890. if (device->bytes_used > 0) {
  891. u64 len = btrfs_dev_extent_length(leaf, extent);
  892. device->bytes_used -= len;
  893. spin_lock(&root->fs_info->free_chunk_lock);
  894. root->fs_info->free_chunk_space += len;
  895. spin_unlock(&root->fs_info->free_chunk_lock);
  896. }
  897. ret = btrfs_del_item(trans, root, path);
  898. out:
  899. btrfs_free_path(path);
  900. return ret;
  901. }
  902. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  903. struct btrfs_device *device,
  904. u64 chunk_tree, u64 chunk_objectid,
  905. u64 chunk_offset, u64 start, u64 num_bytes)
  906. {
  907. int ret;
  908. struct btrfs_path *path;
  909. struct btrfs_root *root = device->dev_root;
  910. struct btrfs_dev_extent *extent;
  911. struct extent_buffer *leaf;
  912. struct btrfs_key key;
  913. WARN_ON(!device->in_fs_metadata);
  914. path = btrfs_alloc_path();
  915. if (!path)
  916. return -ENOMEM;
  917. key.objectid = device->devid;
  918. key.offset = start;
  919. key.type = BTRFS_DEV_EXTENT_KEY;
  920. ret = btrfs_insert_empty_item(trans, root, path, &key,
  921. sizeof(*extent));
  922. BUG_ON(ret);
  923. leaf = path->nodes[0];
  924. extent = btrfs_item_ptr(leaf, path->slots[0],
  925. struct btrfs_dev_extent);
  926. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  927. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  928. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  929. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  930. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  931. BTRFS_UUID_SIZE);
  932. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  933. btrfs_mark_buffer_dirty(leaf);
  934. btrfs_free_path(path);
  935. return ret;
  936. }
  937. static noinline int find_next_chunk(struct btrfs_root *root,
  938. u64 objectid, u64 *offset)
  939. {
  940. struct btrfs_path *path;
  941. int ret;
  942. struct btrfs_key key;
  943. struct btrfs_chunk *chunk;
  944. struct btrfs_key found_key;
  945. path = btrfs_alloc_path();
  946. if (!path)
  947. return -ENOMEM;
  948. key.objectid = objectid;
  949. key.offset = (u64)-1;
  950. key.type = BTRFS_CHUNK_ITEM_KEY;
  951. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  952. if (ret < 0)
  953. goto error;
  954. BUG_ON(ret == 0);
  955. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  956. if (ret) {
  957. *offset = 0;
  958. } else {
  959. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  960. path->slots[0]);
  961. if (found_key.objectid != objectid)
  962. *offset = 0;
  963. else {
  964. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  965. struct btrfs_chunk);
  966. *offset = found_key.offset +
  967. btrfs_chunk_length(path->nodes[0], chunk);
  968. }
  969. }
  970. ret = 0;
  971. error:
  972. btrfs_free_path(path);
  973. return ret;
  974. }
  975. static noinline int find_next_devid(struct btrfs_root *root, u64 *objectid)
  976. {
  977. int ret;
  978. struct btrfs_key key;
  979. struct btrfs_key found_key;
  980. struct btrfs_path *path;
  981. root = root->fs_info->chunk_root;
  982. path = btrfs_alloc_path();
  983. if (!path)
  984. return -ENOMEM;
  985. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  986. key.type = BTRFS_DEV_ITEM_KEY;
  987. key.offset = (u64)-1;
  988. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  989. if (ret < 0)
  990. goto error;
  991. BUG_ON(ret == 0);
  992. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  993. BTRFS_DEV_ITEM_KEY);
  994. if (ret) {
  995. *objectid = 1;
  996. } else {
  997. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  998. path->slots[0]);
  999. *objectid = found_key.offset + 1;
  1000. }
  1001. ret = 0;
  1002. error:
  1003. btrfs_free_path(path);
  1004. return ret;
  1005. }
  1006. /*
  1007. * the device information is stored in the chunk root
  1008. * the btrfs_device struct should be fully filled in
  1009. */
  1010. int btrfs_add_device(struct btrfs_trans_handle *trans,
  1011. struct btrfs_root *root,
  1012. struct btrfs_device *device)
  1013. {
  1014. int ret;
  1015. struct btrfs_path *path;
  1016. struct btrfs_dev_item *dev_item;
  1017. struct extent_buffer *leaf;
  1018. struct btrfs_key key;
  1019. unsigned long ptr;
  1020. root = root->fs_info->chunk_root;
  1021. path = btrfs_alloc_path();
  1022. if (!path)
  1023. return -ENOMEM;
  1024. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1025. key.type = BTRFS_DEV_ITEM_KEY;
  1026. key.offset = device->devid;
  1027. ret = btrfs_insert_empty_item(trans, root, path, &key,
  1028. sizeof(*dev_item));
  1029. if (ret)
  1030. goto out;
  1031. leaf = path->nodes[0];
  1032. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1033. btrfs_set_device_id(leaf, dev_item, device->devid);
  1034. btrfs_set_device_generation(leaf, dev_item, 0);
  1035. btrfs_set_device_type(leaf, dev_item, device->type);
  1036. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1037. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1038. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1039. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1040. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1041. btrfs_set_device_group(leaf, dev_item, 0);
  1042. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  1043. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  1044. btrfs_set_device_start_offset(leaf, dev_item, 0);
  1045. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1046. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1047. ptr = (unsigned long)btrfs_device_fsid(dev_item);
  1048. write_extent_buffer(leaf, root->fs_info->fsid, ptr, BTRFS_UUID_SIZE);
  1049. btrfs_mark_buffer_dirty(leaf);
  1050. ret = 0;
  1051. out:
  1052. btrfs_free_path(path);
  1053. return ret;
  1054. }
  1055. static int btrfs_rm_dev_item(struct btrfs_root *root,
  1056. struct btrfs_device *device)
  1057. {
  1058. int ret;
  1059. struct btrfs_path *path;
  1060. struct btrfs_key key;
  1061. struct btrfs_trans_handle *trans;
  1062. root = root->fs_info->chunk_root;
  1063. path = btrfs_alloc_path();
  1064. if (!path)
  1065. return -ENOMEM;
  1066. trans = btrfs_start_transaction(root, 0);
  1067. if (IS_ERR(trans)) {
  1068. btrfs_free_path(path);
  1069. return PTR_ERR(trans);
  1070. }
  1071. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1072. key.type = BTRFS_DEV_ITEM_KEY;
  1073. key.offset = device->devid;
  1074. lock_chunks(root);
  1075. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1076. if (ret < 0)
  1077. goto out;
  1078. if (ret > 0) {
  1079. ret = -ENOENT;
  1080. goto out;
  1081. }
  1082. ret = btrfs_del_item(trans, root, path);
  1083. if (ret)
  1084. goto out;
  1085. out:
  1086. btrfs_free_path(path);
  1087. unlock_chunks(root);
  1088. btrfs_commit_transaction(trans, root);
  1089. return ret;
  1090. }
  1091. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  1092. {
  1093. struct btrfs_device *device;
  1094. struct btrfs_device *next_device;
  1095. struct block_device *bdev;
  1096. struct buffer_head *bh = NULL;
  1097. struct btrfs_super_block *disk_super;
  1098. struct btrfs_fs_devices *cur_devices;
  1099. u64 all_avail;
  1100. u64 devid;
  1101. u64 num_devices;
  1102. u8 *dev_uuid;
  1103. int ret = 0;
  1104. bool clear_super = false;
  1105. mutex_lock(&uuid_mutex);
  1106. mutex_lock(&root->fs_info->volume_mutex);
  1107. all_avail = root->fs_info->avail_data_alloc_bits |
  1108. root->fs_info->avail_system_alloc_bits |
  1109. root->fs_info->avail_metadata_alloc_bits;
  1110. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  1111. root->fs_info->fs_devices->num_devices <= 4) {
  1112. printk(KERN_ERR "btrfs: unable to go below four devices "
  1113. "on raid10\n");
  1114. ret = -EINVAL;
  1115. goto out;
  1116. }
  1117. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  1118. root->fs_info->fs_devices->num_devices <= 2) {
  1119. printk(KERN_ERR "btrfs: unable to go below two "
  1120. "devices on raid1\n");
  1121. ret = -EINVAL;
  1122. goto out;
  1123. }
  1124. if (strcmp(device_path, "missing") == 0) {
  1125. struct list_head *devices;
  1126. struct btrfs_device *tmp;
  1127. device = NULL;
  1128. devices = &root->fs_info->fs_devices->devices;
  1129. /*
  1130. * It is safe to read the devices since the volume_mutex
  1131. * is held.
  1132. */
  1133. list_for_each_entry(tmp, devices, dev_list) {
  1134. if (tmp->in_fs_metadata && !tmp->bdev) {
  1135. device = tmp;
  1136. break;
  1137. }
  1138. }
  1139. bdev = NULL;
  1140. bh = NULL;
  1141. disk_super = NULL;
  1142. if (!device) {
  1143. printk(KERN_ERR "btrfs: no missing devices found to "
  1144. "remove\n");
  1145. goto out;
  1146. }
  1147. } else {
  1148. bdev = blkdev_get_by_path(device_path, FMODE_READ | FMODE_EXCL,
  1149. root->fs_info->bdev_holder);
  1150. if (IS_ERR(bdev)) {
  1151. ret = PTR_ERR(bdev);
  1152. goto out;
  1153. }
  1154. set_blocksize(bdev, 4096);
  1155. bh = btrfs_read_dev_super(bdev);
  1156. if (!bh) {
  1157. ret = -EINVAL;
  1158. goto error_close;
  1159. }
  1160. disk_super = (struct btrfs_super_block *)bh->b_data;
  1161. devid = btrfs_stack_device_id(&disk_super->dev_item);
  1162. dev_uuid = disk_super->dev_item.uuid;
  1163. device = btrfs_find_device(root, devid, dev_uuid,
  1164. disk_super->fsid);
  1165. if (!device) {
  1166. ret = -ENOENT;
  1167. goto error_brelse;
  1168. }
  1169. }
  1170. if (device->writeable && root->fs_info->fs_devices->rw_devices == 1) {
  1171. printk(KERN_ERR "btrfs: unable to remove the only writeable "
  1172. "device\n");
  1173. ret = -EINVAL;
  1174. goto error_brelse;
  1175. }
  1176. if (device->writeable) {
  1177. lock_chunks(root);
  1178. list_del_init(&device->dev_alloc_list);
  1179. unlock_chunks(root);
  1180. root->fs_info->fs_devices->rw_devices--;
  1181. clear_super = true;
  1182. }
  1183. ret = btrfs_shrink_device(device, 0);
  1184. if (ret)
  1185. goto error_undo;
  1186. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  1187. if (ret)
  1188. goto error_undo;
  1189. spin_lock(&root->fs_info->free_chunk_lock);
  1190. root->fs_info->free_chunk_space = device->total_bytes -
  1191. device->bytes_used;
  1192. spin_unlock(&root->fs_info->free_chunk_lock);
  1193. device->in_fs_metadata = 0;
  1194. btrfs_scrub_cancel_dev(root, device);
  1195. /*
  1196. * the device list mutex makes sure that we don't change
  1197. * the device list while someone else is writing out all
  1198. * the device supers.
  1199. */
  1200. cur_devices = device->fs_devices;
  1201. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1202. list_del_rcu(&device->dev_list);
  1203. device->fs_devices->num_devices--;
  1204. if (device->missing)
  1205. root->fs_info->fs_devices->missing_devices--;
  1206. next_device = list_entry(root->fs_info->fs_devices->devices.next,
  1207. struct btrfs_device, dev_list);
  1208. if (device->bdev == root->fs_info->sb->s_bdev)
  1209. root->fs_info->sb->s_bdev = next_device->bdev;
  1210. if (device->bdev == root->fs_info->fs_devices->latest_bdev)
  1211. root->fs_info->fs_devices->latest_bdev = next_device->bdev;
  1212. if (device->bdev)
  1213. device->fs_devices->open_devices--;
  1214. call_rcu(&device->rcu, free_device);
  1215. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1216. num_devices = btrfs_super_num_devices(root->fs_info->super_copy) - 1;
  1217. btrfs_set_super_num_devices(root->fs_info->super_copy, num_devices);
  1218. if (cur_devices->open_devices == 0) {
  1219. struct btrfs_fs_devices *fs_devices;
  1220. fs_devices = root->fs_info->fs_devices;
  1221. while (fs_devices) {
  1222. if (fs_devices->seed == cur_devices)
  1223. break;
  1224. fs_devices = fs_devices->seed;
  1225. }
  1226. fs_devices->seed = cur_devices->seed;
  1227. cur_devices->seed = NULL;
  1228. lock_chunks(root);
  1229. __btrfs_close_devices(cur_devices);
  1230. unlock_chunks(root);
  1231. free_fs_devices(cur_devices);
  1232. }
  1233. /*
  1234. * at this point, the device is zero sized. We want to
  1235. * remove it from the devices list and zero out the old super
  1236. */
  1237. if (clear_super) {
  1238. /* make sure this device isn't detected as part of
  1239. * the FS anymore
  1240. */
  1241. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  1242. set_buffer_dirty(bh);
  1243. sync_dirty_buffer(bh);
  1244. }
  1245. ret = 0;
  1246. error_brelse:
  1247. brelse(bh);
  1248. error_close:
  1249. if (bdev)
  1250. blkdev_put(bdev, FMODE_READ | FMODE_EXCL);
  1251. out:
  1252. mutex_unlock(&root->fs_info->volume_mutex);
  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_root *root)
  1269. {
  1270. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1271. struct btrfs_fs_devices *old_devices;
  1272. struct btrfs_fs_devices *seed_devices;
  1273. struct btrfs_super_block *disk_super = root->fs_info->super_copy;
  1274. struct btrfs_device *device;
  1275. u64 super_flags;
  1276. BUG_ON(!mutex_is_locked(&uuid_mutex));
  1277. if (!fs_devices->seeding)
  1278. return -EINVAL;
  1279. seed_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  1280. if (!seed_devices)
  1281. return -ENOMEM;
  1282. old_devices = clone_fs_devices(fs_devices);
  1283. if (IS_ERR(old_devices)) {
  1284. kfree(seed_devices);
  1285. return PTR_ERR(old_devices);
  1286. }
  1287. list_add(&old_devices->list, &fs_uuids);
  1288. memcpy(seed_devices, fs_devices, sizeof(*seed_devices));
  1289. seed_devices->opened = 1;
  1290. INIT_LIST_HEAD(&seed_devices->devices);
  1291. INIT_LIST_HEAD(&seed_devices->alloc_list);
  1292. mutex_init(&seed_devices->device_list_mutex);
  1293. mutex_lock(&root->fs_info->fs_devices->device_list_mutex);
  1294. list_splice_init_rcu(&fs_devices->devices, &seed_devices->devices,
  1295. synchronize_rcu);
  1296. mutex_unlock(&root->fs_info->fs_devices->device_list_mutex);
  1297. list_splice_init(&fs_devices->alloc_list, &seed_devices->alloc_list);
  1298. list_for_each_entry(device, &seed_devices->devices, dev_list) {
  1299. device->fs_devices = seed_devices;
  1300. }
  1301. fs_devices->seeding = 0;
  1302. fs_devices->num_devices = 0;
  1303. fs_devices->open_devices = 0;
  1304. fs_devices->seed = seed_devices;
  1305. generate_random_uuid(fs_devices->fsid);
  1306. memcpy(root->fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1307. memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
  1308. super_flags = btrfs_super_flags(disk_super) &
  1309. ~BTRFS_SUPER_FLAG_SEEDING;
  1310. btrfs_set_super_flags(disk_super, super_flags);
  1311. return 0;
  1312. }
  1313. /*
  1314. * strore the expected generation for seed devices in device items.
  1315. */
  1316. static int btrfs_finish_sprout(struct btrfs_trans_handle *trans,
  1317. struct btrfs_root *root)
  1318. {
  1319. struct btrfs_path *path;
  1320. struct extent_buffer *leaf;
  1321. struct btrfs_dev_item *dev_item;
  1322. struct btrfs_device *device;
  1323. struct btrfs_key key;
  1324. u8 fs_uuid[BTRFS_UUID_SIZE];
  1325. u8 dev_uuid[BTRFS_UUID_SIZE];
  1326. u64 devid;
  1327. int ret;
  1328. path = btrfs_alloc_path();
  1329. if (!path)
  1330. return -ENOMEM;
  1331. root = root->fs_info->chunk_root;
  1332. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1333. key.offset = 0;
  1334. key.type = BTRFS_DEV_ITEM_KEY;
  1335. while (1) {
  1336. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1337. if (ret < 0)
  1338. goto error;
  1339. leaf = path->nodes[0];
  1340. next_slot:
  1341. if (path->slots[0] >= btrfs_header_nritems(leaf)) {
  1342. ret = btrfs_next_leaf(root, path);
  1343. if (ret > 0)
  1344. break;
  1345. if (ret < 0)
  1346. goto error;
  1347. leaf = path->nodes[0];
  1348. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1349. btrfs_release_path(path);
  1350. continue;
  1351. }
  1352. btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
  1353. if (key.objectid != BTRFS_DEV_ITEMS_OBJECTID ||
  1354. key.type != BTRFS_DEV_ITEM_KEY)
  1355. break;
  1356. dev_item = btrfs_item_ptr(leaf, path->slots[0],
  1357. struct btrfs_dev_item);
  1358. devid = btrfs_device_id(leaf, dev_item);
  1359. read_extent_buffer(leaf, dev_uuid,
  1360. (unsigned long)btrfs_device_uuid(dev_item),
  1361. BTRFS_UUID_SIZE);
  1362. read_extent_buffer(leaf, fs_uuid,
  1363. (unsigned long)btrfs_device_fsid(dev_item),
  1364. BTRFS_UUID_SIZE);
  1365. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  1366. BUG_ON(!device);
  1367. if (device->fs_devices->seeding) {
  1368. btrfs_set_device_generation(leaf, dev_item,
  1369. device->generation);
  1370. btrfs_mark_buffer_dirty(leaf);
  1371. }
  1372. path->slots[0]++;
  1373. goto next_slot;
  1374. }
  1375. ret = 0;
  1376. error:
  1377. btrfs_free_path(path);
  1378. return ret;
  1379. }
  1380. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  1381. {
  1382. struct request_queue *q;
  1383. struct btrfs_trans_handle *trans;
  1384. struct btrfs_device *device;
  1385. struct block_device *bdev;
  1386. struct list_head *devices;
  1387. struct super_block *sb = root->fs_info->sb;
  1388. u64 total_bytes;
  1389. int seeding_dev = 0;
  1390. int ret = 0;
  1391. if ((sb->s_flags & MS_RDONLY) && !root->fs_info->fs_devices->seeding)
  1392. return -EINVAL;
  1393. bdev = blkdev_get_by_path(device_path, FMODE_WRITE | FMODE_EXCL,
  1394. root->fs_info->bdev_holder);
  1395. if (IS_ERR(bdev))
  1396. return PTR_ERR(bdev);
  1397. if (root->fs_info->fs_devices->seeding) {
  1398. seeding_dev = 1;
  1399. down_write(&sb->s_umount);
  1400. mutex_lock(&uuid_mutex);
  1401. }
  1402. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  1403. mutex_lock(&root->fs_info->volume_mutex);
  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(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. out:
  1513. mutex_unlock(&root->fs_info->volume_mutex);
  1514. return ret;
  1515. error:
  1516. blkdev_put(bdev, FMODE_EXCL);
  1517. if (seeding_dev) {
  1518. mutex_unlock(&uuid_mutex);
  1519. up_write(&sb->s_umount);
  1520. }
  1521. goto out;
  1522. }
  1523. static noinline int btrfs_update_device(struct btrfs_trans_handle *trans,
  1524. struct btrfs_device *device)
  1525. {
  1526. int ret;
  1527. struct btrfs_path *path;
  1528. struct btrfs_root *root;
  1529. struct btrfs_dev_item *dev_item;
  1530. struct extent_buffer *leaf;
  1531. struct btrfs_key key;
  1532. root = device->dev_root->fs_info->chunk_root;
  1533. path = btrfs_alloc_path();
  1534. if (!path)
  1535. return -ENOMEM;
  1536. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1537. key.type = BTRFS_DEV_ITEM_KEY;
  1538. key.offset = device->devid;
  1539. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  1540. if (ret < 0)
  1541. goto out;
  1542. if (ret > 0) {
  1543. ret = -ENOENT;
  1544. goto out;
  1545. }
  1546. leaf = path->nodes[0];
  1547. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  1548. btrfs_set_device_id(leaf, dev_item, device->devid);
  1549. btrfs_set_device_type(leaf, dev_item, device->type);
  1550. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  1551. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  1552. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  1553. btrfs_set_device_total_bytes(leaf, dev_item, device->disk_total_bytes);
  1554. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1555. btrfs_mark_buffer_dirty(leaf);
  1556. out:
  1557. btrfs_free_path(path);
  1558. return ret;
  1559. }
  1560. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1561. struct btrfs_device *device, u64 new_size)
  1562. {
  1563. struct btrfs_super_block *super_copy =
  1564. device->dev_root->fs_info->super_copy;
  1565. u64 old_total = btrfs_super_total_bytes(super_copy);
  1566. u64 diff = new_size - device->total_bytes;
  1567. if (!device->writeable)
  1568. return -EACCES;
  1569. if (new_size <= device->total_bytes)
  1570. return -EINVAL;
  1571. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1572. device->fs_devices->total_rw_bytes += diff;
  1573. device->total_bytes = new_size;
  1574. device->disk_total_bytes = new_size;
  1575. btrfs_clear_space_info_full(device->dev_root->fs_info);
  1576. return btrfs_update_device(trans, device);
  1577. }
  1578. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1579. struct btrfs_device *device, u64 new_size)
  1580. {
  1581. int ret;
  1582. lock_chunks(device->dev_root);
  1583. ret = __btrfs_grow_device(trans, device, new_size);
  1584. unlock_chunks(device->dev_root);
  1585. return ret;
  1586. }
  1587. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1588. struct btrfs_root *root,
  1589. u64 chunk_tree, u64 chunk_objectid,
  1590. u64 chunk_offset)
  1591. {
  1592. int ret;
  1593. struct btrfs_path *path;
  1594. struct btrfs_key key;
  1595. root = root->fs_info->chunk_root;
  1596. path = btrfs_alloc_path();
  1597. if (!path)
  1598. return -ENOMEM;
  1599. key.objectid = chunk_objectid;
  1600. key.offset = chunk_offset;
  1601. key.type = BTRFS_CHUNK_ITEM_KEY;
  1602. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1603. BUG_ON(ret);
  1604. ret = btrfs_del_item(trans, root, path);
  1605. btrfs_free_path(path);
  1606. return ret;
  1607. }
  1608. static int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1609. chunk_offset)
  1610. {
  1611. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1612. struct btrfs_disk_key *disk_key;
  1613. struct btrfs_chunk *chunk;
  1614. u8 *ptr;
  1615. int ret = 0;
  1616. u32 num_stripes;
  1617. u32 array_size;
  1618. u32 len = 0;
  1619. u32 cur;
  1620. struct btrfs_key key;
  1621. array_size = btrfs_super_sys_array_size(super_copy);
  1622. ptr = super_copy->sys_chunk_array;
  1623. cur = 0;
  1624. while (cur < array_size) {
  1625. disk_key = (struct btrfs_disk_key *)ptr;
  1626. btrfs_disk_key_to_cpu(&key, disk_key);
  1627. len = sizeof(*disk_key);
  1628. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1629. chunk = (struct btrfs_chunk *)(ptr + len);
  1630. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1631. len += btrfs_chunk_item_size(num_stripes);
  1632. } else {
  1633. ret = -EIO;
  1634. break;
  1635. }
  1636. if (key.objectid == chunk_objectid &&
  1637. key.offset == chunk_offset) {
  1638. memmove(ptr, ptr + len, array_size - (cur + len));
  1639. array_size -= len;
  1640. btrfs_set_super_sys_array_size(super_copy, array_size);
  1641. } else {
  1642. ptr += len;
  1643. cur += len;
  1644. }
  1645. }
  1646. return ret;
  1647. }
  1648. static int btrfs_relocate_chunk(struct btrfs_root *root,
  1649. u64 chunk_tree, u64 chunk_objectid,
  1650. u64 chunk_offset)
  1651. {
  1652. struct extent_map_tree *em_tree;
  1653. struct btrfs_root *extent_root;
  1654. struct btrfs_trans_handle *trans;
  1655. struct extent_map *em;
  1656. struct map_lookup *map;
  1657. int ret;
  1658. int i;
  1659. root = root->fs_info->chunk_root;
  1660. extent_root = root->fs_info->extent_root;
  1661. em_tree = &root->fs_info->mapping_tree.map_tree;
  1662. ret = btrfs_can_relocate(extent_root, chunk_offset);
  1663. if (ret)
  1664. return -ENOSPC;
  1665. /* step one, relocate all the extents inside this chunk */
  1666. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1667. if (ret)
  1668. return ret;
  1669. trans = btrfs_start_transaction(root, 0);
  1670. BUG_ON(IS_ERR(trans));
  1671. lock_chunks(root);
  1672. /*
  1673. * step two, delete the device extents and the
  1674. * chunk tree entries
  1675. */
  1676. read_lock(&em_tree->lock);
  1677. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1678. read_unlock(&em_tree->lock);
  1679. BUG_ON(em->start > chunk_offset ||
  1680. em->start + em->len < chunk_offset);
  1681. map = (struct map_lookup *)em->bdev;
  1682. for (i = 0; i < map->num_stripes; i++) {
  1683. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1684. map->stripes[i].physical);
  1685. BUG_ON(ret);
  1686. if (map->stripes[i].dev) {
  1687. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1688. BUG_ON(ret);
  1689. }
  1690. }
  1691. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1692. chunk_offset);
  1693. BUG_ON(ret);
  1694. trace_btrfs_chunk_free(root, map, chunk_offset, em->len);
  1695. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1696. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1697. BUG_ON(ret);
  1698. }
  1699. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1700. BUG_ON(ret);
  1701. write_lock(&em_tree->lock);
  1702. remove_extent_mapping(em_tree, em);
  1703. write_unlock(&em_tree->lock);
  1704. kfree(map);
  1705. em->bdev = NULL;
  1706. /* once for the tree */
  1707. free_extent_map(em);
  1708. /* once for us */
  1709. free_extent_map(em);
  1710. unlock_chunks(root);
  1711. btrfs_end_transaction(trans, root);
  1712. return 0;
  1713. }
  1714. static int btrfs_relocate_sys_chunks(struct btrfs_root *root)
  1715. {
  1716. struct btrfs_root *chunk_root = root->fs_info->chunk_root;
  1717. struct btrfs_path *path;
  1718. struct extent_buffer *leaf;
  1719. struct btrfs_chunk *chunk;
  1720. struct btrfs_key key;
  1721. struct btrfs_key found_key;
  1722. u64 chunk_tree = chunk_root->root_key.objectid;
  1723. u64 chunk_type;
  1724. bool retried = false;
  1725. int failed = 0;
  1726. int ret;
  1727. path = btrfs_alloc_path();
  1728. if (!path)
  1729. return -ENOMEM;
  1730. again:
  1731. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1732. key.offset = (u64)-1;
  1733. key.type = BTRFS_CHUNK_ITEM_KEY;
  1734. while (1) {
  1735. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1736. if (ret < 0)
  1737. goto error;
  1738. BUG_ON(ret == 0);
  1739. ret = btrfs_previous_item(chunk_root, path, key.objectid,
  1740. key.type);
  1741. if (ret < 0)
  1742. goto error;
  1743. if (ret > 0)
  1744. break;
  1745. leaf = path->nodes[0];
  1746. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  1747. chunk = btrfs_item_ptr(leaf, path->slots[0],
  1748. struct btrfs_chunk);
  1749. chunk_type = btrfs_chunk_type(leaf, chunk);
  1750. btrfs_release_path(path);
  1751. if (chunk_type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1752. ret = btrfs_relocate_chunk(chunk_root, chunk_tree,
  1753. found_key.objectid,
  1754. found_key.offset);
  1755. if (ret == -ENOSPC)
  1756. failed++;
  1757. else if (ret)
  1758. BUG();
  1759. }
  1760. if (found_key.offset == 0)
  1761. break;
  1762. key.offset = found_key.offset - 1;
  1763. }
  1764. ret = 0;
  1765. if (failed && !retried) {
  1766. failed = 0;
  1767. retried = true;
  1768. goto again;
  1769. } else if (failed && retried) {
  1770. WARN_ON(1);
  1771. ret = -ENOSPC;
  1772. }
  1773. error:
  1774. btrfs_free_path(path);
  1775. return ret;
  1776. }
  1777. static u64 div_factor(u64 num, int factor)
  1778. {
  1779. if (factor == 10)
  1780. return num;
  1781. num *= factor;
  1782. do_div(num, 10);
  1783. return num;
  1784. }
  1785. int btrfs_balance(struct btrfs_root *dev_root)
  1786. {
  1787. int ret;
  1788. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1789. struct btrfs_device *device;
  1790. u64 old_size;
  1791. u64 size_to_free;
  1792. struct btrfs_path *path;
  1793. struct btrfs_key key;
  1794. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1795. struct btrfs_trans_handle *trans;
  1796. struct btrfs_key found_key;
  1797. if (dev_root->fs_info->sb->s_flags & MS_RDONLY)
  1798. return -EROFS;
  1799. if (!capable(CAP_SYS_ADMIN))
  1800. return -EPERM;
  1801. mutex_lock(&dev_root->fs_info->volume_mutex);
  1802. dev_root = dev_root->fs_info->dev_root;
  1803. /* step one make some room on all the devices */
  1804. list_for_each_entry(device, devices, dev_list) {
  1805. old_size = device->total_bytes;
  1806. size_to_free = div_factor(old_size, 1);
  1807. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1808. if (!device->writeable ||
  1809. device->total_bytes - device->bytes_used > size_to_free)
  1810. continue;
  1811. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1812. if (ret == -ENOSPC)
  1813. break;
  1814. BUG_ON(ret);
  1815. trans = btrfs_start_transaction(dev_root, 0);
  1816. BUG_ON(IS_ERR(trans));
  1817. ret = btrfs_grow_device(trans, device, old_size);
  1818. BUG_ON(ret);
  1819. btrfs_end_transaction(trans, dev_root);
  1820. }
  1821. /* step two, relocate all the chunks */
  1822. path = btrfs_alloc_path();
  1823. if (!path) {
  1824. ret = -ENOMEM;
  1825. goto error;
  1826. }
  1827. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1828. key.offset = (u64)-1;
  1829. key.type = BTRFS_CHUNK_ITEM_KEY;
  1830. while (1) {
  1831. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1832. if (ret < 0)
  1833. goto error;
  1834. /*
  1835. * this shouldn't happen, it means the last relocate
  1836. * failed
  1837. */
  1838. if (ret == 0)
  1839. break;
  1840. ret = btrfs_previous_item(chunk_root, path, 0,
  1841. BTRFS_CHUNK_ITEM_KEY);
  1842. if (ret)
  1843. break;
  1844. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1845. path->slots[0]);
  1846. if (found_key.objectid != key.objectid)
  1847. break;
  1848. /* chunk zero is special */
  1849. if (found_key.offset == 0)
  1850. break;
  1851. btrfs_release_path(path);
  1852. ret = btrfs_relocate_chunk(chunk_root,
  1853. chunk_root->root_key.objectid,
  1854. found_key.objectid,
  1855. found_key.offset);
  1856. if (ret && ret != -ENOSPC)
  1857. goto error;
  1858. key.offset = found_key.offset - 1;
  1859. }
  1860. ret = 0;
  1861. error:
  1862. btrfs_free_path(path);
  1863. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1864. return ret;
  1865. }
  1866. /*
  1867. * shrinking a device means finding all of the device extents past
  1868. * the new size, and then following the back refs to the chunks.
  1869. * The chunk relocation code actually frees the device extent
  1870. */
  1871. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1872. {
  1873. struct btrfs_trans_handle *trans;
  1874. struct btrfs_root *root = device->dev_root;
  1875. struct btrfs_dev_extent *dev_extent = NULL;
  1876. struct btrfs_path *path;
  1877. u64 length;
  1878. u64 chunk_tree;
  1879. u64 chunk_objectid;
  1880. u64 chunk_offset;
  1881. int ret;
  1882. int slot;
  1883. int failed = 0;
  1884. bool retried = false;
  1885. struct extent_buffer *l;
  1886. struct btrfs_key key;
  1887. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1888. u64 old_total = btrfs_super_total_bytes(super_copy);
  1889. u64 old_size = device->total_bytes;
  1890. u64 diff = device->total_bytes - new_size;
  1891. if (new_size >= device->total_bytes)
  1892. return -EINVAL;
  1893. path = btrfs_alloc_path();
  1894. if (!path)
  1895. return -ENOMEM;
  1896. path->reada = 2;
  1897. lock_chunks(root);
  1898. device->total_bytes = new_size;
  1899. if (device->writeable) {
  1900. device->fs_devices->total_rw_bytes -= diff;
  1901. spin_lock(&root->fs_info->free_chunk_lock);
  1902. root->fs_info->free_chunk_space -= diff;
  1903. spin_unlock(&root->fs_info->free_chunk_lock);
  1904. }
  1905. unlock_chunks(root);
  1906. again:
  1907. key.objectid = device->devid;
  1908. key.offset = (u64)-1;
  1909. key.type = BTRFS_DEV_EXTENT_KEY;
  1910. while (1) {
  1911. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1912. if (ret < 0)
  1913. goto done;
  1914. ret = btrfs_previous_item(root, path, 0, key.type);
  1915. if (ret < 0)
  1916. goto done;
  1917. if (ret) {
  1918. ret = 0;
  1919. btrfs_release_path(path);
  1920. break;
  1921. }
  1922. l = path->nodes[0];
  1923. slot = path->slots[0];
  1924. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1925. if (key.objectid != device->devid) {
  1926. btrfs_release_path(path);
  1927. break;
  1928. }
  1929. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1930. length = btrfs_dev_extent_length(l, dev_extent);
  1931. if (key.offset + length <= new_size) {
  1932. btrfs_release_path(path);
  1933. break;
  1934. }
  1935. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1936. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1937. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1938. btrfs_release_path(path);
  1939. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1940. chunk_offset);
  1941. if (ret && ret != -ENOSPC)
  1942. goto done;
  1943. if (ret == -ENOSPC)
  1944. failed++;
  1945. key.offset -= 1;
  1946. }
  1947. if (failed && !retried) {
  1948. failed = 0;
  1949. retried = true;
  1950. goto again;
  1951. } else if (failed && retried) {
  1952. ret = -ENOSPC;
  1953. lock_chunks(root);
  1954. device->total_bytes = old_size;
  1955. if (device->writeable)
  1956. device->fs_devices->total_rw_bytes += diff;
  1957. spin_lock(&root->fs_info->free_chunk_lock);
  1958. root->fs_info->free_chunk_space += diff;
  1959. spin_unlock(&root->fs_info->free_chunk_lock);
  1960. unlock_chunks(root);
  1961. goto done;
  1962. }
  1963. /* Shrinking succeeded, else we would be at "done". */
  1964. trans = btrfs_start_transaction(root, 0);
  1965. if (IS_ERR(trans)) {
  1966. ret = PTR_ERR(trans);
  1967. goto done;
  1968. }
  1969. lock_chunks(root);
  1970. device->disk_total_bytes = new_size;
  1971. /* Now btrfs_update_device() will change the on-disk size. */
  1972. ret = btrfs_update_device(trans, device);
  1973. if (ret) {
  1974. unlock_chunks(root);
  1975. btrfs_end_transaction(trans, root);
  1976. goto done;
  1977. }
  1978. WARN_ON(diff > old_total);
  1979. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1980. unlock_chunks(root);
  1981. btrfs_end_transaction(trans, root);
  1982. done:
  1983. btrfs_free_path(path);
  1984. return ret;
  1985. }
  1986. static int btrfs_add_system_chunk(struct btrfs_root *root,
  1987. struct btrfs_key *key,
  1988. struct btrfs_chunk *chunk, int item_size)
  1989. {
  1990. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  1991. struct btrfs_disk_key disk_key;
  1992. u32 array_size;
  1993. u8 *ptr;
  1994. array_size = btrfs_super_sys_array_size(super_copy);
  1995. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1996. return -EFBIG;
  1997. ptr = super_copy->sys_chunk_array + array_size;
  1998. btrfs_cpu_key_to_disk(&disk_key, key);
  1999. memcpy(ptr, &disk_key, sizeof(disk_key));
  2000. ptr += sizeof(disk_key);
  2001. memcpy(ptr, chunk, item_size);
  2002. item_size += sizeof(disk_key);
  2003. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  2004. return 0;
  2005. }
  2006. /*
  2007. * sort the devices in descending order by max_avail, total_avail
  2008. */
  2009. static int btrfs_cmp_device_info(const void *a, const void *b)
  2010. {
  2011. const struct btrfs_device_info *di_a = a;
  2012. const struct btrfs_device_info *di_b = b;
  2013. if (di_a->max_avail > di_b->max_avail)
  2014. return -1;
  2015. if (di_a->max_avail < di_b->max_avail)
  2016. return 1;
  2017. if (di_a->total_avail > di_b->total_avail)
  2018. return -1;
  2019. if (di_a->total_avail < di_b->total_avail)
  2020. return 1;
  2021. return 0;
  2022. }
  2023. static int __btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2024. struct btrfs_root *extent_root,
  2025. struct map_lookup **map_ret,
  2026. u64 *num_bytes_out, u64 *stripe_size_out,
  2027. u64 start, u64 type)
  2028. {
  2029. struct btrfs_fs_info *info = extent_root->fs_info;
  2030. struct btrfs_fs_devices *fs_devices = info->fs_devices;
  2031. struct list_head *cur;
  2032. struct map_lookup *map = NULL;
  2033. struct extent_map_tree *em_tree;
  2034. struct extent_map *em;
  2035. struct btrfs_device_info *devices_info = NULL;
  2036. u64 total_avail;
  2037. int num_stripes; /* total number of stripes to allocate */
  2038. int sub_stripes; /* sub_stripes info for map */
  2039. int dev_stripes; /* stripes per dev */
  2040. int devs_max; /* max devs to use */
  2041. int devs_min; /* min devs needed */
  2042. int devs_increment; /* ndevs has to be a multiple of this */
  2043. int ncopies; /* how many copies to data has */
  2044. int ret;
  2045. u64 max_stripe_size;
  2046. u64 max_chunk_size;
  2047. u64 stripe_size;
  2048. u64 num_bytes;
  2049. int ndevs;
  2050. int i;
  2051. int j;
  2052. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  2053. (type & BTRFS_BLOCK_GROUP_DUP)) {
  2054. WARN_ON(1);
  2055. type &= ~BTRFS_BLOCK_GROUP_DUP;
  2056. }
  2057. if (list_empty(&fs_devices->alloc_list))
  2058. return -ENOSPC;
  2059. sub_stripes = 1;
  2060. dev_stripes = 1;
  2061. devs_increment = 1;
  2062. ncopies = 1;
  2063. devs_max = 0; /* 0 == as many as possible */
  2064. devs_min = 1;
  2065. /*
  2066. * define the properties of each RAID type.
  2067. * FIXME: move this to a global table and use it in all RAID
  2068. * calculation code
  2069. */
  2070. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  2071. dev_stripes = 2;
  2072. ncopies = 2;
  2073. devs_max = 1;
  2074. } else if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  2075. devs_min = 2;
  2076. } else if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  2077. devs_increment = 2;
  2078. ncopies = 2;
  2079. devs_max = 2;
  2080. devs_min = 2;
  2081. } else if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  2082. sub_stripes = 2;
  2083. devs_increment = 2;
  2084. ncopies = 2;
  2085. devs_min = 4;
  2086. } else {
  2087. devs_max = 1;
  2088. }
  2089. if (type & BTRFS_BLOCK_GROUP_DATA) {
  2090. max_stripe_size = 1024 * 1024 * 1024;
  2091. max_chunk_size = 10 * max_stripe_size;
  2092. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  2093. max_stripe_size = 256 * 1024 * 1024;
  2094. max_chunk_size = max_stripe_size;
  2095. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2096. max_stripe_size = 8 * 1024 * 1024;
  2097. max_chunk_size = 2 * max_stripe_size;
  2098. } else {
  2099. printk(KERN_ERR "btrfs: invalid chunk type 0x%llx requested\n",
  2100. type);
  2101. BUG_ON(1);
  2102. }
  2103. /* we don't want a chunk larger than 10% of writeable space */
  2104. max_chunk_size = min(div_factor(fs_devices->total_rw_bytes, 1),
  2105. max_chunk_size);
  2106. devices_info = kzalloc(sizeof(*devices_info) * fs_devices->rw_devices,
  2107. GFP_NOFS);
  2108. if (!devices_info)
  2109. return -ENOMEM;
  2110. cur = fs_devices->alloc_list.next;
  2111. /*
  2112. * in the first pass through the devices list, we gather information
  2113. * about the available holes on each device.
  2114. */
  2115. ndevs = 0;
  2116. while (cur != &fs_devices->alloc_list) {
  2117. struct btrfs_device *device;
  2118. u64 max_avail;
  2119. u64 dev_offset;
  2120. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  2121. cur = cur->next;
  2122. if (!device->writeable) {
  2123. printk(KERN_ERR
  2124. "btrfs: read-only device in alloc_list\n");
  2125. WARN_ON(1);
  2126. continue;
  2127. }
  2128. if (!device->in_fs_metadata)
  2129. continue;
  2130. if (device->total_bytes > device->bytes_used)
  2131. total_avail = device->total_bytes - device->bytes_used;
  2132. else
  2133. total_avail = 0;
  2134. /* If there is no space on this device, skip it. */
  2135. if (total_avail == 0)
  2136. continue;
  2137. ret = find_free_dev_extent(device,
  2138. max_stripe_size * dev_stripes,
  2139. &dev_offset, &max_avail);
  2140. if (ret && ret != -ENOSPC)
  2141. goto error;
  2142. if (ret == 0)
  2143. max_avail = max_stripe_size * dev_stripes;
  2144. if (max_avail < BTRFS_STRIPE_LEN * dev_stripes)
  2145. continue;
  2146. devices_info[ndevs].dev_offset = dev_offset;
  2147. devices_info[ndevs].max_avail = max_avail;
  2148. devices_info[ndevs].total_avail = total_avail;
  2149. devices_info[ndevs].dev = device;
  2150. ++ndevs;
  2151. }
  2152. /*
  2153. * now sort the devices by hole size / available space
  2154. */
  2155. sort(devices_info, ndevs, sizeof(struct btrfs_device_info),
  2156. btrfs_cmp_device_info, NULL);
  2157. /* round down to number of usable stripes */
  2158. ndevs -= ndevs % devs_increment;
  2159. if (ndevs < devs_increment * sub_stripes || ndevs < devs_min) {
  2160. ret = -ENOSPC;
  2161. goto error;
  2162. }
  2163. if (devs_max && ndevs > devs_max)
  2164. ndevs = devs_max;
  2165. /*
  2166. * the primary goal is to maximize the number of stripes, so use as many
  2167. * devices as possible, even if the stripes are not maximum sized.
  2168. */
  2169. stripe_size = devices_info[ndevs-1].max_avail;
  2170. num_stripes = ndevs * dev_stripes;
  2171. if (stripe_size * num_stripes > max_chunk_size * ncopies) {
  2172. stripe_size = max_chunk_size * ncopies;
  2173. do_div(stripe_size, num_stripes);
  2174. }
  2175. do_div(stripe_size, dev_stripes);
  2176. do_div(stripe_size, BTRFS_STRIPE_LEN);
  2177. stripe_size *= BTRFS_STRIPE_LEN;
  2178. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2179. if (!map) {
  2180. ret = -ENOMEM;
  2181. goto error;
  2182. }
  2183. map->num_stripes = num_stripes;
  2184. for (i = 0; i < ndevs; ++i) {
  2185. for (j = 0; j < dev_stripes; ++j) {
  2186. int s = i * dev_stripes + j;
  2187. map->stripes[s].dev = devices_info[i].dev;
  2188. map->stripes[s].physical = devices_info[i].dev_offset +
  2189. j * stripe_size;
  2190. }
  2191. }
  2192. map->sector_size = extent_root->sectorsize;
  2193. map->stripe_len = BTRFS_STRIPE_LEN;
  2194. map->io_align = BTRFS_STRIPE_LEN;
  2195. map->io_width = BTRFS_STRIPE_LEN;
  2196. map->type = type;
  2197. map->sub_stripes = sub_stripes;
  2198. *map_ret = map;
  2199. num_bytes = stripe_size * (num_stripes / ncopies);
  2200. *stripe_size_out = stripe_size;
  2201. *num_bytes_out = num_bytes;
  2202. trace_btrfs_chunk_alloc(info->chunk_root, map, start, num_bytes);
  2203. em = alloc_extent_map();
  2204. if (!em) {
  2205. ret = -ENOMEM;
  2206. goto error;
  2207. }
  2208. em->bdev = (struct block_device *)map;
  2209. em->start = start;
  2210. em->len = num_bytes;
  2211. em->block_start = 0;
  2212. em->block_len = em->len;
  2213. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  2214. write_lock(&em_tree->lock);
  2215. ret = add_extent_mapping(em_tree, em);
  2216. write_unlock(&em_tree->lock);
  2217. BUG_ON(ret);
  2218. free_extent_map(em);
  2219. ret = btrfs_make_block_group(trans, extent_root, 0, type,
  2220. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2221. start, num_bytes);
  2222. BUG_ON(ret);
  2223. for (i = 0; i < map->num_stripes; ++i) {
  2224. struct btrfs_device *device;
  2225. u64 dev_offset;
  2226. device = map->stripes[i].dev;
  2227. dev_offset = map->stripes[i].physical;
  2228. ret = btrfs_alloc_dev_extent(trans, device,
  2229. info->chunk_root->root_key.objectid,
  2230. BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2231. start, dev_offset, stripe_size);
  2232. BUG_ON(ret);
  2233. }
  2234. kfree(devices_info);
  2235. return 0;
  2236. error:
  2237. kfree(map);
  2238. kfree(devices_info);
  2239. return ret;
  2240. }
  2241. static int __finish_chunk_alloc(struct btrfs_trans_handle *trans,
  2242. struct btrfs_root *extent_root,
  2243. struct map_lookup *map, u64 chunk_offset,
  2244. u64 chunk_size, u64 stripe_size)
  2245. {
  2246. u64 dev_offset;
  2247. struct btrfs_key key;
  2248. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2249. struct btrfs_device *device;
  2250. struct btrfs_chunk *chunk;
  2251. struct btrfs_stripe *stripe;
  2252. size_t item_size = btrfs_chunk_item_size(map->num_stripes);
  2253. int index = 0;
  2254. int ret;
  2255. chunk = kzalloc(item_size, GFP_NOFS);
  2256. if (!chunk)
  2257. return -ENOMEM;
  2258. index = 0;
  2259. while (index < map->num_stripes) {
  2260. device = map->stripes[index].dev;
  2261. device->bytes_used += stripe_size;
  2262. ret = btrfs_update_device(trans, device);
  2263. BUG_ON(ret);
  2264. index++;
  2265. }
  2266. spin_lock(&extent_root->fs_info->free_chunk_lock);
  2267. extent_root->fs_info->free_chunk_space -= (stripe_size *
  2268. map->num_stripes);
  2269. spin_unlock(&extent_root->fs_info->free_chunk_lock);
  2270. index = 0;
  2271. stripe = &chunk->stripe;
  2272. while (index < map->num_stripes) {
  2273. device = map->stripes[index].dev;
  2274. dev_offset = map->stripes[index].physical;
  2275. btrfs_set_stack_stripe_devid(stripe, device->devid);
  2276. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  2277. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  2278. stripe++;
  2279. index++;
  2280. }
  2281. btrfs_set_stack_chunk_length(chunk, chunk_size);
  2282. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  2283. btrfs_set_stack_chunk_stripe_len(chunk, map->stripe_len);
  2284. btrfs_set_stack_chunk_type(chunk, map->type);
  2285. btrfs_set_stack_chunk_num_stripes(chunk, map->num_stripes);
  2286. btrfs_set_stack_chunk_io_align(chunk, map->stripe_len);
  2287. btrfs_set_stack_chunk_io_width(chunk, map->stripe_len);
  2288. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  2289. btrfs_set_stack_chunk_sub_stripes(chunk, map->sub_stripes);
  2290. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  2291. key.type = BTRFS_CHUNK_ITEM_KEY;
  2292. key.offset = chunk_offset;
  2293. ret = btrfs_insert_item(trans, chunk_root, &key, chunk, item_size);
  2294. BUG_ON(ret);
  2295. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  2296. ret = btrfs_add_system_chunk(chunk_root, &key, chunk,
  2297. item_size);
  2298. BUG_ON(ret);
  2299. }
  2300. kfree(chunk);
  2301. return 0;
  2302. }
  2303. /*
  2304. * Chunk allocation falls into two parts. The first part does works
  2305. * that make the new allocated chunk useable, but not do any operation
  2306. * that modifies the chunk tree. The second part does the works that
  2307. * require modifying the chunk tree. This division is important for the
  2308. * bootstrap process of adding storage to a seed btrfs.
  2309. */
  2310. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  2311. struct btrfs_root *extent_root, u64 type)
  2312. {
  2313. u64 chunk_offset;
  2314. u64 chunk_size;
  2315. u64 stripe_size;
  2316. struct map_lookup *map;
  2317. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  2318. int ret;
  2319. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  2320. &chunk_offset);
  2321. if (ret)
  2322. return ret;
  2323. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2324. &stripe_size, chunk_offset, type);
  2325. if (ret)
  2326. return ret;
  2327. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2328. chunk_size, stripe_size);
  2329. BUG_ON(ret);
  2330. return 0;
  2331. }
  2332. static noinline int init_first_rw_device(struct btrfs_trans_handle *trans,
  2333. struct btrfs_root *root,
  2334. struct btrfs_device *device)
  2335. {
  2336. u64 chunk_offset;
  2337. u64 sys_chunk_offset;
  2338. u64 chunk_size;
  2339. u64 sys_chunk_size;
  2340. u64 stripe_size;
  2341. u64 sys_stripe_size;
  2342. u64 alloc_profile;
  2343. struct map_lookup *map;
  2344. struct map_lookup *sys_map;
  2345. struct btrfs_fs_info *fs_info = root->fs_info;
  2346. struct btrfs_root *extent_root = fs_info->extent_root;
  2347. int ret;
  2348. ret = find_next_chunk(fs_info->chunk_root,
  2349. BTRFS_FIRST_CHUNK_TREE_OBJECTID, &chunk_offset);
  2350. if (ret)
  2351. return ret;
  2352. alloc_profile = BTRFS_BLOCK_GROUP_METADATA |
  2353. (fs_info->metadata_alloc_profile &
  2354. fs_info->avail_metadata_alloc_bits);
  2355. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2356. ret = __btrfs_alloc_chunk(trans, extent_root, &map, &chunk_size,
  2357. &stripe_size, chunk_offset, alloc_profile);
  2358. BUG_ON(ret);
  2359. sys_chunk_offset = chunk_offset + chunk_size;
  2360. alloc_profile = BTRFS_BLOCK_GROUP_SYSTEM |
  2361. (fs_info->system_alloc_profile &
  2362. fs_info->avail_system_alloc_bits);
  2363. alloc_profile = btrfs_reduce_alloc_profile(root, alloc_profile);
  2364. ret = __btrfs_alloc_chunk(trans, extent_root, &sys_map,
  2365. &sys_chunk_size, &sys_stripe_size,
  2366. sys_chunk_offset, alloc_profile);
  2367. BUG_ON(ret);
  2368. ret = btrfs_add_device(trans, fs_info->chunk_root, device);
  2369. BUG_ON(ret);
  2370. /*
  2371. * Modifying chunk tree needs allocating new blocks from both
  2372. * system block group and metadata block group. So we only can
  2373. * do operations require modifying the chunk tree after both
  2374. * block groups were created.
  2375. */
  2376. ret = __finish_chunk_alloc(trans, extent_root, map, chunk_offset,
  2377. chunk_size, stripe_size);
  2378. BUG_ON(ret);
  2379. ret = __finish_chunk_alloc(trans, extent_root, sys_map,
  2380. sys_chunk_offset, sys_chunk_size,
  2381. sys_stripe_size);
  2382. BUG_ON(ret);
  2383. return 0;
  2384. }
  2385. int btrfs_chunk_readonly(struct btrfs_root *root, u64 chunk_offset)
  2386. {
  2387. struct extent_map *em;
  2388. struct map_lookup *map;
  2389. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2390. int readonly = 0;
  2391. int i;
  2392. read_lock(&map_tree->map_tree.lock);
  2393. em = lookup_extent_mapping(&map_tree->map_tree, chunk_offset, 1);
  2394. read_unlock(&map_tree->map_tree.lock);
  2395. if (!em)
  2396. return 1;
  2397. if (btrfs_test_opt(root, DEGRADED)) {
  2398. free_extent_map(em);
  2399. return 0;
  2400. }
  2401. map = (struct map_lookup *)em->bdev;
  2402. for (i = 0; i < map->num_stripes; i++) {
  2403. if (!map->stripes[i].dev->writeable) {
  2404. readonly = 1;
  2405. break;
  2406. }
  2407. }
  2408. free_extent_map(em);
  2409. return readonly;
  2410. }
  2411. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  2412. {
  2413. extent_map_tree_init(&tree->map_tree);
  2414. }
  2415. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  2416. {
  2417. struct extent_map *em;
  2418. while (1) {
  2419. write_lock(&tree->map_tree.lock);
  2420. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  2421. if (em)
  2422. remove_extent_mapping(&tree->map_tree, em);
  2423. write_unlock(&tree->map_tree.lock);
  2424. if (!em)
  2425. break;
  2426. kfree(em->bdev);
  2427. /* once for us */
  2428. free_extent_map(em);
  2429. /* once for the tree */
  2430. free_extent_map(em);
  2431. }
  2432. }
  2433. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  2434. {
  2435. struct extent_map *em;
  2436. struct map_lookup *map;
  2437. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2438. int ret;
  2439. read_lock(&em_tree->lock);
  2440. em = lookup_extent_mapping(em_tree, logical, len);
  2441. read_unlock(&em_tree->lock);
  2442. BUG_ON(!em);
  2443. BUG_ON(em->start > logical || em->start + em->len < logical);
  2444. map = (struct map_lookup *)em->bdev;
  2445. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  2446. ret = map->num_stripes;
  2447. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2448. ret = map->sub_stripes;
  2449. else
  2450. ret = 1;
  2451. free_extent_map(em);
  2452. return ret;
  2453. }
  2454. static int find_live_mirror(struct map_lookup *map, int first, int num,
  2455. int optimal)
  2456. {
  2457. int i;
  2458. if (map->stripes[optimal].dev->bdev)
  2459. return optimal;
  2460. for (i = first; i < first + num; i++) {
  2461. if (map->stripes[i].dev->bdev)
  2462. return i;
  2463. }
  2464. /* we couldn't find one that doesn't fail. Just return something
  2465. * and the io error handling code will clean up eventually
  2466. */
  2467. return optimal;
  2468. }
  2469. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2470. u64 logical, u64 *length,
  2471. struct btrfs_bio **bbio_ret,
  2472. int mirror_num)
  2473. {
  2474. struct extent_map *em;
  2475. struct map_lookup *map;
  2476. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2477. u64 offset;
  2478. u64 stripe_offset;
  2479. u64 stripe_end_offset;
  2480. u64 stripe_nr;
  2481. u64 stripe_nr_orig;
  2482. u64 stripe_nr_end;
  2483. int stripes_allocated = 8;
  2484. int stripes_required = 1;
  2485. int stripe_index;
  2486. int i;
  2487. int num_stripes;
  2488. int max_errors = 0;
  2489. struct btrfs_bio *bbio = NULL;
  2490. if (bbio_ret && !(rw & (REQ_WRITE | REQ_DISCARD)))
  2491. stripes_allocated = 1;
  2492. again:
  2493. if (bbio_ret) {
  2494. bbio = kzalloc(btrfs_bio_size(stripes_allocated),
  2495. GFP_NOFS);
  2496. if (!bbio)
  2497. return -ENOMEM;
  2498. atomic_set(&bbio->error, 0);
  2499. }
  2500. read_lock(&em_tree->lock);
  2501. em = lookup_extent_mapping(em_tree, logical, *length);
  2502. read_unlock(&em_tree->lock);
  2503. if (!em) {
  2504. printk(KERN_CRIT "unable to find logical %llu len %llu\n",
  2505. (unsigned long long)logical,
  2506. (unsigned long long)*length);
  2507. BUG();
  2508. }
  2509. BUG_ON(em->start > logical || em->start + em->len < logical);
  2510. map = (struct map_lookup *)em->bdev;
  2511. offset = logical - em->start;
  2512. if (mirror_num > map->num_stripes)
  2513. mirror_num = 0;
  2514. /* if our btrfs_bio struct is too small, back off and try again */
  2515. if (rw & REQ_WRITE) {
  2516. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  2517. BTRFS_BLOCK_GROUP_DUP)) {
  2518. stripes_required = map->num_stripes;
  2519. max_errors = 1;
  2520. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2521. stripes_required = map->sub_stripes;
  2522. max_errors = 1;
  2523. }
  2524. }
  2525. if (rw & REQ_DISCARD) {
  2526. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2527. BTRFS_BLOCK_GROUP_RAID1 |
  2528. BTRFS_BLOCK_GROUP_DUP |
  2529. BTRFS_BLOCK_GROUP_RAID10)) {
  2530. stripes_required = map->num_stripes;
  2531. }
  2532. }
  2533. if (bbio_ret && (rw & (REQ_WRITE | REQ_DISCARD)) &&
  2534. stripes_allocated < stripes_required) {
  2535. stripes_allocated = map->num_stripes;
  2536. free_extent_map(em);
  2537. kfree(bbio);
  2538. goto again;
  2539. }
  2540. stripe_nr = offset;
  2541. /*
  2542. * stripe_nr counts the total number of stripes we have to stride
  2543. * to get to this block
  2544. */
  2545. do_div(stripe_nr, map->stripe_len);
  2546. stripe_offset = stripe_nr * map->stripe_len;
  2547. BUG_ON(offset < stripe_offset);
  2548. /* stripe_offset is the offset of this block in its stripe*/
  2549. stripe_offset = offset - stripe_offset;
  2550. if (rw & REQ_DISCARD)
  2551. *length = min_t(u64, em->len - offset, *length);
  2552. else if (map->type & (BTRFS_BLOCK_GROUP_RAID0 |
  2553. BTRFS_BLOCK_GROUP_RAID1 |
  2554. BTRFS_BLOCK_GROUP_RAID10 |
  2555. BTRFS_BLOCK_GROUP_DUP)) {
  2556. /* we limit the length of each bio to what fits in a stripe */
  2557. *length = min_t(u64, em->len - offset,
  2558. map->stripe_len - stripe_offset);
  2559. } else {
  2560. *length = em->len - offset;
  2561. }
  2562. if (!bbio_ret)
  2563. goto out;
  2564. num_stripes = 1;
  2565. stripe_index = 0;
  2566. stripe_nr_orig = stripe_nr;
  2567. stripe_nr_end = (offset + *length + map->stripe_len - 1) &
  2568. (~(map->stripe_len - 1));
  2569. do_div(stripe_nr_end, map->stripe_len);
  2570. stripe_end_offset = stripe_nr_end * map->stripe_len -
  2571. (offset + *length);
  2572. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2573. if (rw & REQ_DISCARD)
  2574. num_stripes = min_t(u64, map->num_stripes,
  2575. stripe_nr_end - stripe_nr_orig);
  2576. stripe_index = do_div(stripe_nr, map->num_stripes);
  2577. } else if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  2578. if (rw & (REQ_WRITE | REQ_DISCARD))
  2579. num_stripes = map->num_stripes;
  2580. else if (mirror_num)
  2581. stripe_index = mirror_num - 1;
  2582. else {
  2583. stripe_index = find_live_mirror(map, 0,
  2584. map->num_stripes,
  2585. current->pid % map->num_stripes);
  2586. mirror_num = stripe_index + 1;
  2587. }
  2588. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  2589. if (rw & (REQ_WRITE | REQ_DISCARD)) {
  2590. num_stripes = map->num_stripes;
  2591. } else if (mirror_num) {
  2592. stripe_index = mirror_num - 1;
  2593. } else {
  2594. mirror_num = 1;
  2595. }
  2596. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2597. int factor = map->num_stripes / map->sub_stripes;
  2598. stripe_index = do_div(stripe_nr, factor);
  2599. stripe_index *= map->sub_stripes;
  2600. if (rw & REQ_WRITE)
  2601. num_stripes = map->sub_stripes;
  2602. else if (rw & REQ_DISCARD)
  2603. num_stripes = min_t(u64, map->sub_stripes *
  2604. (stripe_nr_end - stripe_nr_orig),
  2605. map->num_stripes);
  2606. else if (mirror_num)
  2607. stripe_index += mirror_num - 1;
  2608. else {
  2609. stripe_index = find_live_mirror(map, stripe_index,
  2610. map->sub_stripes, stripe_index +
  2611. current->pid % map->sub_stripes);
  2612. mirror_num = stripe_index + 1;
  2613. }
  2614. } else {
  2615. /*
  2616. * after this do_div call, stripe_nr is the number of stripes
  2617. * on this device we have to walk to find the data, and
  2618. * stripe_index is the number of our device in the stripe array
  2619. */
  2620. stripe_index = do_div(stripe_nr, map->num_stripes);
  2621. mirror_num = stripe_index + 1;
  2622. }
  2623. BUG_ON(stripe_index >= map->num_stripes);
  2624. if (rw & REQ_DISCARD) {
  2625. for (i = 0; i < num_stripes; i++) {
  2626. bbio->stripes[i].physical =
  2627. map->stripes[stripe_index].physical +
  2628. stripe_offset + stripe_nr * map->stripe_len;
  2629. bbio->stripes[i].dev = map->stripes[stripe_index].dev;
  2630. if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2631. u64 stripes;
  2632. u32 last_stripe = 0;
  2633. int j;
  2634. div_u64_rem(stripe_nr_end - 1,
  2635. map->num_stripes,
  2636. &last_stripe);
  2637. for (j = 0; j < map->num_stripes; j++) {
  2638. u32 test;
  2639. div_u64_rem(stripe_nr_end - 1 - j,
  2640. map->num_stripes, &test);
  2641. if (test == stripe_index)
  2642. break;
  2643. }
  2644. stripes = stripe_nr_end - 1 - j;
  2645. do_div(stripes, map->num_stripes);
  2646. bbio->stripes[i].length = map->stripe_len *
  2647. (stripes - stripe_nr + 1);
  2648. if (i == 0) {
  2649. bbio->stripes[i].length -=
  2650. stripe_offset;
  2651. stripe_offset = 0;
  2652. }
  2653. if (stripe_index == last_stripe)
  2654. bbio->stripes[i].length -=
  2655. stripe_end_offset;
  2656. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2657. u64 stripes;
  2658. int j;
  2659. int factor = map->num_stripes /
  2660. map->sub_stripes;
  2661. u32 last_stripe = 0;
  2662. div_u64_rem(stripe_nr_end - 1,
  2663. factor, &last_stripe);
  2664. last_stripe *= map->sub_stripes;
  2665. for (j = 0; j < factor; j++) {
  2666. u32 test;
  2667. div_u64_rem(stripe_nr_end - 1 - j,
  2668. factor, &test);
  2669. if (test ==
  2670. stripe_index / map->sub_stripes)
  2671. break;
  2672. }
  2673. stripes = stripe_nr_end - 1 - j;
  2674. do_div(stripes, factor);
  2675. bbio->stripes[i].length = map->stripe_len *
  2676. (stripes - stripe_nr + 1);
  2677. if (i < map->sub_stripes) {
  2678. bbio->stripes[i].length -=
  2679. stripe_offset;
  2680. if (i == map->sub_stripes - 1)
  2681. stripe_offset = 0;
  2682. }
  2683. if (stripe_index >= last_stripe &&
  2684. stripe_index <= (last_stripe +
  2685. map->sub_stripes - 1)) {
  2686. bbio->stripes[i].length -=
  2687. stripe_end_offset;
  2688. }
  2689. } else
  2690. bbio->stripes[i].length = *length;
  2691. stripe_index++;
  2692. if (stripe_index == map->num_stripes) {
  2693. /* This could only happen for RAID0/10 */
  2694. stripe_index = 0;
  2695. stripe_nr++;
  2696. }
  2697. }
  2698. } else {
  2699. for (i = 0; i < num_stripes; i++) {
  2700. bbio->stripes[i].physical =
  2701. map->stripes[stripe_index].physical +
  2702. stripe_offset +
  2703. stripe_nr * map->stripe_len;
  2704. bbio->stripes[i].dev =
  2705. map->stripes[stripe_index].dev;
  2706. stripe_index++;
  2707. }
  2708. }
  2709. if (bbio_ret) {
  2710. *bbio_ret = bbio;
  2711. bbio->num_stripes = num_stripes;
  2712. bbio->max_errors = max_errors;
  2713. bbio->mirror_num = mirror_num;
  2714. }
  2715. out:
  2716. free_extent_map(em);
  2717. return 0;
  2718. }
  2719. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  2720. u64 logical, u64 *length,
  2721. struct btrfs_bio **bbio_ret, int mirror_num)
  2722. {
  2723. return __btrfs_map_block(map_tree, rw, logical, length, bbio_ret,
  2724. mirror_num);
  2725. }
  2726. int btrfs_rmap_block(struct btrfs_mapping_tree *map_tree,
  2727. u64 chunk_start, u64 physical, u64 devid,
  2728. u64 **logical, int *naddrs, int *stripe_len)
  2729. {
  2730. struct extent_map_tree *em_tree = &map_tree->map_tree;
  2731. struct extent_map *em;
  2732. struct map_lookup *map;
  2733. u64 *buf;
  2734. u64 bytenr;
  2735. u64 length;
  2736. u64 stripe_nr;
  2737. int i, j, nr = 0;
  2738. read_lock(&em_tree->lock);
  2739. em = lookup_extent_mapping(em_tree, chunk_start, 1);
  2740. read_unlock(&em_tree->lock);
  2741. BUG_ON(!em || em->start != chunk_start);
  2742. map = (struct map_lookup *)em->bdev;
  2743. length = em->len;
  2744. if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  2745. do_div(length, map->num_stripes / map->sub_stripes);
  2746. else if (map->type & BTRFS_BLOCK_GROUP_RAID0)
  2747. do_div(length, map->num_stripes);
  2748. buf = kzalloc(sizeof(u64) * map->num_stripes, GFP_NOFS);
  2749. BUG_ON(!buf);
  2750. for (i = 0; i < map->num_stripes; i++) {
  2751. if (devid && map->stripes[i].dev->devid != devid)
  2752. continue;
  2753. if (map->stripes[i].physical > physical ||
  2754. map->stripes[i].physical + length <= physical)
  2755. continue;
  2756. stripe_nr = physical - map->stripes[i].physical;
  2757. do_div(stripe_nr, map->stripe_len);
  2758. if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  2759. stripe_nr = stripe_nr * map->num_stripes + i;
  2760. do_div(stripe_nr, map->sub_stripes);
  2761. } else if (map->type & BTRFS_BLOCK_GROUP_RAID0) {
  2762. stripe_nr = stripe_nr * map->num_stripes + i;
  2763. }
  2764. bytenr = chunk_start + stripe_nr * map->stripe_len;
  2765. WARN_ON(nr >= map->num_stripes);
  2766. for (j = 0; j < nr; j++) {
  2767. if (buf[j] == bytenr)
  2768. break;
  2769. }
  2770. if (j == nr) {
  2771. WARN_ON(nr >= map->num_stripes);
  2772. buf[nr++] = bytenr;
  2773. }
  2774. }
  2775. *logical = buf;
  2776. *naddrs = nr;
  2777. *stripe_len = map->stripe_len;
  2778. free_extent_map(em);
  2779. return 0;
  2780. }
  2781. static void btrfs_end_bio(struct bio *bio, int err)
  2782. {
  2783. struct btrfs_bio *bbio = bio->bi_private;
  2784. int is_orig_bio = 0;
  2785. if (err)
  2786. atomic_inc(&bbio->error);
  2787. if (bio == bbio->orig_bio)
  2788. is_orig_bio = 1;
  2789. if (atomic_dec_and_test(&bbio->stripes_pending)) {
  2790. if (!is_orig_bio) {
  2791. bio_put(bio);
  2792. bio = bbio->orig_bio;
  2793. }
  2794. bio->bi_private = bbio->private;
  2795. bio->bi_end_io = bbio->end_io;
  2796. bio->bi_bdev = (struct block_device *)
  2797. (unsigned long)bbio->mirror_num;
  2798. /* only send an error to the higher layers if it is
  2799. * beyond the tolerance of the multi-bio
  2800. */
  2801. if (atomic_read(&bbio->error) > bbio->max_errors) {
  2802. err = -EIO;
  2803. } else {
  2804. /*
  2805. * this bio is actually up to date, we didn't
  2806. * go over the max number of errors
  2807. */
  2808. set_bit(BIO_UPTODATE, &bio->bi_flags);
  2809. err = 0;
  2810. }
  2811. kfree(bbio);
  2812. bio_endio(bio, err);
  2813. } else if (!is_orig_bio) {
  2814. bio_put(bio);
  2815. }
  2816. }
  2817. struct async_sched {
  2818. struct bio *bio;
  2819. int rw;
  2820. struct btrfs_fs_info *info;
  2821. struct btrfs_work work;
  2822. };
  2823. /*
  2824. * see run_scheduled_bios for a description of why bios are collected for
  2825. * async submit.
  2826. *
  2827. * This will add one bio to the pending list for a device and make sure
  2828. * the work struct is scheduled.
  2829. */
  2830. static noinline int schedule_bio(struct btrfs_root *root,
  2831. struct btrfs_device *device,
  2832. int rw, struct bio *bio)
  2833. {
  2834. int should_queue = 1;
  2835. struct btrfs_pending_bios *pending_bios;
  2836. /* don't bother with additional async steps for reads, right now */
  2837. if (!(rw & REQ_WRITE)) {
  2838. bio_get(bio);
  2839. submit_bio(rw, bio);
  2840. bio_put(bio);
  2841. return 0;
  2842. }
  2843. /*
  2844. * nr_async_bios allows us to reliably return congestion to the
  2845. * higher layers. Otherwise, the async bio makes it appear we have
  2846. * made progress against dirty pages when we've really just put it
  2847. * on a queue for later
  2848. */
  2849. atomic_inc(&root->fs_info->nr_async_bios);
  2850. WARN_ON(bio->bi_next);
  2851. bio->bi_next = NULL;
  2852. bio->bi_rw |= rw;
  2853. spin_lock(&device->io_lock);
  2854. if (bio->bi_rw & REQ_SYNC)
  2855. pending_bios = &device->pending_sync_bios;
  2856. else
  2857. pending_bios = &device->pending_bios;
  2858. if (pending_bios->tail)
  2859. pending_bios->tail->bi_next = bio;
  2860. pending_bios->tail = bio;
  2861. if (!pending_bios->head)
  2862. pending_bios->head = bio;
  2863. if (device->running_pending)
  2864. should_queue = 0;
  2865. spin_unlock(&device->io_lock);
  2866. if (should_queue)
  2867. btrfs_queue_worker(&root->fs_info->submit_workers,
  2868. &device->work);
  2869. return 0;
  2870. }
  2871. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  2872. int mirror_num, int async_submit)
  2873. {
  2874. struct btrfs_mapping_tree *map_tree;
  2875. struct btrfs_device *dev;
  2876. struct bio *first_bio = bio;
  2877. u64 logical = (u64)bio->bi_sector << 9;
  2878. u64 length = 0;
  2879. u64 map_length;
  2880. int ret;
  2881. int dev_nr = 0;
  2882. int total_devs = 1;
  2883. struct btrfs_bio *bbio = NULL;
  2884. length = bio->bi_size;
  2885. map_tree = &root->fs_info->mapping_tree;
  2886. map_length = length;
  2887. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &bbio,
  2888. mirror_num);
  2889. BUG_ON(ret);
  2890. total_devs = bbio->num_stripes;
  2891. if (map_length < length) {
  2892. printk(KERN_CRIT "mapping failed logical %llu bio len %llu "
  2893. "len %llu\n", (unsigned long long)logical,
  2894. (unsigned long long)length,
  2895. (unsigned long long)map_length);
  2896. BUG();
  2897. }
  2898. bbio->orig_bio = first_bio;
  2899. bbio->private = first_bio->bi_private;
  2900. bbio->end_io = first_bio->bi_end_io;
  2901. atomic_set(&bbio->stripes_pending, bbio->num_stripes);
  2902. while (dev_nr < total_devs) {
  2903. if (dev_nr < total_devs - 1) {
  2904. bio = bio_clone(first_bio, GFP_NOFS);
  2905. BUG_ON(!bio);
  2906. } else {
  2907. bio = first_bio;
  2908. }
  2909. bio->bi_private = bbio;
  2910. bio->bi_end_io = btrfs_end_bio;
  2911. bio->bi_sector = bbio->stripes[dev_nr].physical >> 9;
  2912. dev = bbio->stripes[dev_nr].dev;
  2913. if (dev && dev->bdev && (rw != WRITE || dev->writeable)) {
  2914. pr_debug("btrfs_map_bio: rw %d, secor=%llu, dev=%lu "
  2915. "(%s id %llu), size=%u\n", rw,
  2916. (u64)bio->bi_sector, (u_long)dev->bdev->bd_dev,
  2917. dev->name, dev->devid, bio->bi_size);
  2918. bio->bi_bdev = dev->bdev;
  2919. if (async_submit)
  2920. schedule_bio(root, dev, rw, bio);
  2921. else
  2922. submit_bio(rw, bio);
  2923. } else {
  2924. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  2925. bio->bi_sector = logical >> 9;
  2926. bio_endio(bio, -EIO);
  2927. }
  2928. dev_nr++;
  2929. }
  2930. return 0;
  2931. }
  2932. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  2933. u8 *uuid, u8 *fsid)
  2934. {
  2935. struct btrfs_device *device;
  2936. struct btrfs_fs_devices *cur_devices;
  2937. cur_devices = root->fs_info->fs_devices;
  2938. while (cur_devices) {
  2939. if (!fsid ||
  2940. !memcmp(cur_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  2941. device = __find_device(&cur_devices->devices,
  2942. devid, uuid);
  2943. if (device)
  2944. return device;
  2945. }
  2946. cur_devices = cur_devices->seed;
  2947. }
  2948. return NULL;
  2949. }
  2950. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  2951. u64 devid, u8 *dev_uuid)
  2952. {
  2953. struct btrfs_device *device;
  2954. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  2955. device = kzalloc(sizeof(*device), GFP_NOFS);
  2956. if (!device)
  2957. return NULL;
  2958. list_add(&device->dev_list,
  2959. &fs_devices->devices);
  2960. device->dev_root = root->fs_info->dev_root;
  2961. device->devid = devid;
  2962. device->work.func = pending_bios_fn;
  2963. device->fs_devices = fs_devices;
  2964. device->missing = 1;
  2965. fs_devices->num_devices++;
  2966. fs_devices->missing_devices++;
  2967. spin_lock_init(&device->io_lock);
  2968. INIT_LIST_HEAD(&device->dev_alloc_list);
  2969. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  2970. return device;
  2971. }
  2972. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  2973. struct extent_buffer *leaf,
  2974. struct btrfs_chunk *chunk)
  2975. {
  2976. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  2977. struct map_lookup *map;
  2978. struct extent_map *em;
  2979. u64 logical;
  2980. u64 length;
  2981. u64 devid;
  2982. u8 uuid[BTRFS_UUID_SIZE];
  2983. int num_stripes;
  2984. int ret;
  2985. int i;
  2986. logical = key->offset;
  2987. length = btrfs_chunk_length(leaf, chunk);
  2988. read_lock(&map_tree->map_tree.lock);
  2989. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  2990. read_unlock(&map_tree->map_tree.lock);
  2991. /* already mapped? */
  2992. if (em && em->start <= logical && em->start + em->len > logical) {
  2993. free_extent_map(em);
  2994. return 0;
  2995. } else if (em) {
  2996. free_extent_map(em);
  2997. }
  2998. em = alloc_extent_map();
  2999. if (!em)
  3000. return -ENOMEM;
  3001. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  3002. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  3003. if (!map) {
  3004. free_extent_map(em);
  3005. return -ENOMEM;
  3006. }
  3007. em->bdev = (struct block_device *)map;
  3008. em->start = logical;
  3009. em->len = length;
  3010. em->block_start = 0;
  3011. em->block_len = em->len;
  3012. map->num_stripes = num_stripes;
  3013. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  3014. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  3015. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  3016. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  3017. map->type = btrfs_chunk_type(leaf, chunk);
  3018. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  3019. for (i = 0; i < num_stripes; i++) {
  3020. map->stripes[i].physical =
  3021. btrfs_stripe_offset_nr(leaf, chunk, i);
  3022. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  3023. read_extent_buffer(leaf, uuid, (unsigned long)
  3024. btrfs_stripe_dev_uuid_nr(chunk, i),
  3025. BTRFS_UUID_SIZE);
  3026. map->stripes[i].dev = btrfs_find_device(root, devid, uuid,
  3027. NULL);
  3028. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  3029. kfree(map);
  3030. free_extent_map(em);
  3031. return -EIO;
  3032. }
  3033. if (!map->stripes[i].dev) {
  3034. map->stripes[i].dev =
  3035. add_missing_dev(root, devid, uuid);
  3036. if (!map->stripes[i].dev) {
  3037. kfree(map);
  3038. free_extent_map(em);
  3039. return -EIO;
  3040. }
  3041. }
  3042. map->stripes[i].dev->in_fs_metadata = 1;
  3043. }
  3044. write_lock(&map_tree->map_tree.lock);
  3045. ret = add_extent_mapping(&map_tree->map_tree, em);
  3046. write_unlock(&map_tree->map_tree.lock);
  3047. BUG_ON(ret);
  3048. free_extent_map(em);
  3049. return 0;
  3050. }
  3051. static int fill_device_from_item(struct extent_buffer *leaf,
  3052. struct btrfs_dev_item *dev_item,
  3053. struct btrfs_device *device)
  3054. {
  3055. unsigned long ptr;
  3056. device->devid = btrfs_device_id(leaf, dev_item);
  3057. device->disk_total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  3058. device->total_bytes = device->disk_total_bytes;
  3059. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  3060. device->type = btrfs_device_type(leaf, dev_item);
  3061. device->io_align = btrfs_device_io_align(leaf, dev_item);
  3062. device->io_width = btrfs_device_io_width(leaf, dev_item);
  3063. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  3064. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  3065. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  3066. return 0;
  3067. }
  3068. static int open_seed_devices(struct btrfs_root *root, u8 *fsid)
  3069. {
  3070. struct btrfs_fs_devices *fs_devices;
  3071. int ret;
  3072. mutex_lock(&uuid_mutex);
  3073. fs_devices = root->fs_info->fs_devices->seed;
  3074. while (fs_devices) {
  3075. if (!memcmp(fs_devices->fsid, fsid, BTRFS_UUID_SIZE)) {
  3076. ret = 0;
  3077. goto out;
  3078. }
  3079. fs_devices = fs_devices->seed;
  3080. }
  3081. fs_devices = find_fsid(fsid);
  3082. if (!fs_devices) {
  3083. ret = -ENOENT;
  3084. goto out;
  3085. }
  3086. fs_devices = clone_fs_devices(fs_devices);
  3087. if (IS_ERR(fs_devices)) {
  3088. ret = PTR_ERR(fs_devices);
  3089. goto out;
  3090. }
  3091. ret = __btrfs_open_devices(fs_devices, FMODE_READ,
  3092. root->fs_info->bdev_holder);
  3093. if (ret)
  3094. goto out;
  3095. if (!fs_devices->seeding) {
  3096. __btrfs_close_devices(fs_devices);
  3097. free_fs_devices(fs_devices);
  3098. ret = -EINVAL;
  3099. goto out;
  3100. }
  3101. fs_devices->seed = root->fs_info->fs_devices->seed;
  3102. root->fs_info->fs_devices->seed = fs_devices;
  3103. out:
  3104. mutex_unlock(&uuid_mutex);
  3105. return ret;
  3106. }
  3107. static int read_one_dev(struct btrfs_root *root,
  3108. struct extent_buffer *leaf,
  3109. struct btrfs_dev_item *dev_item)
  3110. {
  3111. struct btrfs_device *device;
  3112. u64 devid;
  3113. int ret;
  3114. u8 fs_uuid[BTRFS_UUID_SIZE];
  3115. u8 dev_uuid[BTRFS_UUID_SIZE];
  3116. devid = btrfs_device_id(leaf, dev_item);
  3117. read_extent_buffer(leaf, dev_uuid,
  3118. (unsigned long)btrfs_device_uuid(dev_item),
  3119. BTRFS_UUID_SIZE);
  3120. read_extent_buffer(leaf, fs_uuid,
  3121. (unsigned long)btrfs_device_fsid(dev_item),
  3122. BTRFS_UUID_SIZE);
  3123. if (memcmp(fs_uuid, root->fs_info->fsid, BTRFS_UUID_SIZE)) {
  3124. ret = open_seed_devices(root, fs_uuid);
  3125. if (ret && !btrfs_test_opt(root, DEGRADED))
  3126. return ret;
  3127. }
  3128. device = btrfs_find_device(root, devid, dev_uuid, fs_uuid);
  3129. if (!device || !device->bdev) {
  3130. if (!btrfs_test_opt(root, DEGRADED))
  3131. return -EIO;
  3132. if (!device) {
  3133. printk(KERN_WARNING "warning devid %llu missing\n",
  3134. (unsigned long long)devid);
  3135. device = add_missing_dev(root, devid, dev_uuid);
  3136. if (!device)
  3137. return -ENOMEM;
  3138. } else if (!device->missing) {
  3139. /*
  3140. * this happens when a device that was properly setup
  3141. * in the device info lists suddenly goes bad.
  3142. * device->bdev is NULL, and so we have to set
  3143. * device->missing to one here
  3144. */
  3145. root->fs_info->fs_devices->missing_devices++;
  3146. device->missing = 1;
  3147. }
  3148. }
  3149. if (device->fs_devices != root->fs_info->fs_devices) {
  3150. BUG_ON(device->writeable);
  3151. if (device->generation !=
  3152. btrfs_device_generation(leaf, dev_item))
  3153. return -EINVAL;
  3154. }
  3155. fill_device_from_item(leaf, dev_item, device);
  3156. device->dev_root = root->fs_info->dev_root;
  3157. device->in_fs_metadata = 1;
  3158. if (device->writeable) {
  3159. device->fs_devices->total_rw_bytes += device->total_bytes;
  3160. spin_lock(&root->fs_info->free_chunk_lock);
  3161. root->fs_info->free_chunk_space += device->total_bytes -
  3162. device->bytes_used;
  3163. spin_unlock(&root->fs_info->free_chunk_lock);
  3164. }
  3165. ret = 0;
  3166. return ret;
  3167. }
  3168. int btrfs_read_sys_array(struct btrfs_root *root)
  3169. {
  3170. struct btrfs_super_block *super_copy = root->fs_info->super_copy;
  3171. struct extent_buffer *sb;
  3172. struct btrfs_disk_key *disk_key;
  3173. struct btrfs_chunk *chunk;
  3174. u8 *ptr;
  3175. unsigned long sb_ptr;
  3176. int ret = 0;
  3177. u32 num_stripes;
  3178. u32 array_size;
  3179. u32 len = 0;
  3180. u32 cur;
  3181. struct btrfs_key key;
  3182. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  3183. BTRFS_SUPER_INFO_SIZE);
  3184. if (!sb)
  3185. return -ENOMEM;
  3186. btrfs_set_buffer_uptodate(sb);
  3187. btrfs_set_buffer_lockdep_class(root->root_key.objectid, sb, 0);
  3188. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  3189. array_size = btrfs_super_sys_array_size(super_copy);
  3190. ptr = super_copy->sys_chunk_array;
  3191. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  3192. cur = 0;
  3193. while (cur < array_size) {
  3194. disk_key = (struct btrfs_disk_key *)ptr;
  3195. btrfs_disk_key_to_cpu(&key, disk_key);
  3196. len = sizeof(*disk_key); ptr += len;
  3197. sb_ptr += len;
  3198. cur += len;
  3199. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  3200. chunk = (struct btrfs_chunk *)sb_ptr;
  3201. ret = read_one_chunk(root, &key, sb, chunk);
  3202. if (ret)
  3203. break;
  3204. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  3205. len = btrfs_chunk_item_size(num_stripes);
  3206. } else {
  3207. ret = -EIO;
  3208. break;
  3209. }
  3210. ptr += len;
  3211. sb_ptr += len;
  3212. cur += len;
  3213. }
  3214. free_extent_buffer(sb);
  3215. return ret;
  3216. }
  3217. int btrfs_read_chunk_tree(struct btrfs_root *root)
  3218. {
  3219. struct btrfs_path *path;
  3220. struct extent_buffer *leaf;
  3221. struct btrfs_key key;
  3222. struct btrfs_key found_key;
  3223. int ret;
  3224. int slot;
  3225. root = root->fs_info->chunk_root;
  3226. path = btrfs_alloc_path();
  3227. if (!path)
  3228. return -ENOMEM;
  3229. /* first we search for all of the device items, and then we
  3230. * read in all of the chunk items. This way we can create chunk
  3231. * mappings that reference all of the devices that are afound
  3232. */
  3233. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  3234. key.offset = 0;
  3235. key.type = 0;
  3236. again:
  3237. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  3238. if (ret < 0)
  3239. goto error;
  3240. while (1) {
  3241. leaf = path->nodes[0];
  3242. slot = path->slots[0];
  3243. if (slot >= btrfs_header_nritems(leaf)) {
  3244. ret = btrfs_next_leaf(root, path);
  3245. if (ret == 0)
  3246. continue;
  3247. if (ret < 0)
  3248. goto error;
  3249. break;
  3250. }
  3251. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  3252. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3253. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  3254. break;
  3255. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  3256. struct btrfs_dev_item *dev_item;
  3257. dev_item = btrfs_item_ptr(leaf, slot,
  3258. struct btrfs_dev_item);
  3259. ret = read_one_dev(root, leaf, dev_item);
  3260. if (ret)
  3261. goto error;
  3262. }
  3263. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  3264. struct btrfs_chunk *chunk;
  3265. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  3266. ret = read_one_chunk(root, &found_key, leaf, chunk);
  3267. if (ret)
  3268. goto error;
  3269. }
  3270. path->slots[0]++;
  3271. }
  3272. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  3273. key.objectid = 0;
  3274. btrfs_release_path(path);
  3275. goto again;
  3276. }
  3277. ret = 0;
  3278. error:
  3279. btrfs_free_path(path);
  3280. return ret;
  3281. }