volumes.c 84 KB

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