volumes.c 95 KB

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