volumes.c 83 KB

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