volumes.c 85 KB

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