volumes.c 94 KB

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