volumes.c 93 KB

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