volumes.c 92 KB

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