volumes.c 92 KB

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