volumes.c 94 KB

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