volumes.c 93 KB

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