volumes.c 86 KB

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