volumes.c 92 KB

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