volumes.c 79 KB

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