volumes.c 92 KB

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