volumes.c 87 KB

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