volumes.c 91 KB

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