volumes.c 86 KB

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