volumes.c 97 KB

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