volumes.c 95 KB

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