volumes.c 86 KB

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