volumes.c 85 KB

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