volumes.c 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550
  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 <asm/div64.h>
  24. #include "ctree.h"
  25. #include "extent_map.h"
  26. #include "disk-io.h"
  27. #include "transaction.h"
  28. #include "print-tree.h"
  29. #include "volumes.h"
  30. #include "async-thread.h"
  31. struct map_lookup {
  32. u64 type;
  33. int io_align;
  34. int io_width;
  35. int stripe_len;
  36. int sector_size;
  37. int num_stripes;
  38. int sub_stripes;
  39. struct btrfs_bio_stripe stripes[];
  40. };
  41. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  42. (sizeof(struct btrfs_bio_stripe) * (n)))
  43. static DEFINE_MUTEX(uuid_mutex);
  44. static LIST_HEAD(fs_uuids);
  45. void btrfs_lock_volumes(void)
  46. {
  47. mutex_lock(&uuid_mutex);
  48. }
  49. void btrfs_unlock_volumes(void)
  50. {
  51. mutex_unlock(&uuid_mutex);
  52. }
  53. static void lock_chunks(struct btrfs_root *root)
  54. {
  55. mutex_lock(&root->fs_info->chunk_mutex);
  56. }
  57. static void unlock_chunks(struct btrfs_root *root)
  58. {
  59. mutex_unlock(&root->fs_info->chunk_mutex);
  60. }
  61. int btrfs_cleanup_fs_uuids(void)
  62. {
  63. struct btrfs_fs_devices *fs_devices;
  64. struct list_head *uuid_cur;
  65. struct list_head *devices_cur;
  66. struct btrfs_device *dev;
  67. list_for_each(uuid_cur, &fs_uuids) {
  68. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  69. list);
  70. while(!list_empty(&fs_devices->devices)) {
  71. devices_cur = fs_devices->devices.next;
  72. dev = list_entry(devices_cur, struct btrfs_device,
  73. dev_list);
  74. if (dev->bdev) {
  75. close_bdev_excl(dev->bdev);
  76. fs_devices->open_devices--;
  77. }
  78. list_del(&dev->dev_list);
  79. kfree(dev->name);
  80. kfree(dev);
  81. }
  82. }
  83. return 0;
  84. }
  85. static noinline struct btrfs_device *__find_device(struct list_head *head,
  86. u64 devid, u8 *uuid)
  87. {
  88. struct btrfs_device *dev;
  89. struct list_head *cur;
  90. list_for_each(cur, head) {
  91. dev = list_entry(cur, struct btrfs_device, dev_list);
  92. if (dev->devid == devid &&
  93. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  94. return dev;
  95. }
  96. }
  97. return NULL;
  98. }
  99. static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
  100. {
  101. struct list_head *cur;
  102. struct btrfs_fs_devices *fs_devices;
  103. list_for_each(cur, &fs_uuids) {
  104. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  105. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  106. return fs_devices;
  107. }
  108. return NULL;
  109. }
  110. /*
  111. * we try to collect pending bios for a device so we don't get a large
  112. * number of procs sending bios down to the same device. This greatly
  113. * improves the schedulers ability to collect and merge the bios.
  114. *
  115. * But, it also turns into a long list of bios to process and that is sure
  116. * to eventually make the worker thread block. The solution here is to
  117. * make some progress and then put this work struct back at the end of
  118. * the list if the block device is congested. This way, multiple devices
  119. * can make progress from a single worker thread.
  120. */
  121. static int noinline run_scheduled_bios(struct btrfs_device *device)
  122. {
  123. struct bio *pending;
  124. struct backing_dev_info *bdi;
  125. struct btrfs_fs_info *fs_info;
  126. struct bio *tail;
  127. struct bio *cur;
  128. int again = 0;
  129. unsigned long num_run = 0;
  130. unsigned long limit;
  131. bdi = device->bdev->bd_inode->i_mapping->backing_dev_info;
  132. fs_info = device->dev_root->fs_info;
  133. limit = btrfs_async_submit_limit(fs_info);
  134. limit = limit * 2 / 3;
  135. loop:
  136. spin_lock(&device->io_lock);
  137. /* take all the bios off the list at once and process them
  138. * later on (without the lock held). But, remember the
  139. * tail and other pointers so the bios can be properly reinserted
  140. * into the list if we hit congestion
  141. */
  142. pending = device->pending_bios;
  143. tail = device->pending_bio_tail;
  144. WARN_ON(pending && !tail);
  145. device->pending_bios = NULL;
  146. device->pending_bio_tail = NULL;
  147. /*
  148. * if pending was null this time around, no bios need processing
  149. * at all and we can stop. Otherwise it'll loop back up again
  150. * and do an additional check so no bios are missed.
  151. *
  152. * device->running_pending is used to synchronize with the
  153. * schedule_bio code.
  154. */
  155. if (pending) {
  156. again = 1;
  157. device->running_pending = 1;
  158. } else {
  159. again = 0;
  160. device->running_pending = 0;
  161. }
  162. spin_unlock(&device->io_lock);
  163. while(pending) {
  164. cur = pending;
  165. pending = pending->bi_next;
  166. cur->bi_next = NULL;
  167. atomic_dec(&fs_info->nr_async_bios);
  168. if (atomic_read(&fs_info->nr_async_bios) < limit &&
  169. waitqueue_active(&fs_info->async_submit_wait))
  170. wake_up(&fs_info->async_submit_wait);
  171. BUG_ON(atomic_read(&cur->bi_cnt) == 0);
  172. bio_get(cur);
  173. submit_bio(cur->bi_rw, cur);
  174. bio_put(cur);
  175. num_run++;
  176. /*
  177. * we made progress, there is more work to do and the bdi
  178. * is now congested. Back off and let other work structs
  179. * run instead
  180. */
  181. if (pending && bdi_write_congested(bdi) &&
  182. fs_info->fs_devices->open_devices > 1) {
  183. struct bio *old_head;
  184. spin_lock(&device->io_lock);
  185. old_head = device->pending_bios;
  186. device->pending_bios = pending;
  187. if (device->pending_bio_tail)
  188. tail->bi_next = old_head;
  189. else
  190. device->pending_bio_tail = tail;
  191. spin_unlock(&device->io_lock);
  192. btrfs_requeue_work(&device->work);
  193. goto done;
  194. }
  195. }
  196. if (again)
  197. goto loop;
  198. done:
  199. return 0;
  200. }
  201. void pending_bios_fn(struct btrfs_work *work)
  202. {
  203. struct btrfs_device *device;
  204. device = container_of(work, struct btrfs_device, work);
  205. run_scheduled_bios(device);
  206. }
  207. static noinline int device_list_add(const char *path,
  208. struct btrfs_super_block *disk_super,
  209. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  210. {
  211. struct btrfs_device *device;
  212. struct btrfs_fs_devices *fs_devices;
  213. u64 found_transid = btrfs_super_generation(disk_super);
  214. fs_devices = find_fsid(disk_super->fsid);
  215. if (!fs_devices) {
  216. fs_devices = kzalloc(sizeof(*fs_devices), GFP_NOFS);
  217. if (!fs_devices)
  218. return -ENOMEM;
  219. INIT_LIST_HEAD(&fs_devices->devices);
  220. INIT_LIST_HEAD(&fs_devices->alloc_list);
  221. list_add(&fs_devices->list, &fs_uuids);
  222. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  223. fs_devices->latest_devid = devid;
  224. fs_devices->latest_trans = found_transid;
  225. device = NULL;
  226. } else {
  227. device = __find_device(&fs_devices->devices, devid,
  228. disk_super->dev_item.uuid);
  229. }
  230. if (!device) {
  231. device = kzalloc(sizeof(*device), GFP_NOFS);
  232. if (!device) {
  233. /* we can safely leave the fs_devices entry around */
  234. return -ENOMEM;
  235. }
  236. device->devid = devid;
  237. device->work.func = pending_bios_fn;
  238. memcpy(device->uuid, disk_super->dev_item.uuid,
  239. BTRFS_UUID_SIZE);
  240. device->barriers = 1;
  241. spin_lock_init(&device->io_lock);
  242. device->name = kstrdup(path, GFP_NOFS);
  243. if (!device->name) {
  244. kfree(device);
  245. return -ENOMEM;
  246. }
  247. list_add(&device->dev_list, &fs_devices->devices);
  248. list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
  249. fs_devices->num_devices++;
  250. }
  251. if (found_transid > fs_devices->latest_trans) {
  252. fs_devices->latest_devid = devid;
  253. fs_devices->latest_trans = found_transid;
  254. }
  255. *fs_devices_ret = fs_devices;
  256. return 0;
  257. }
  258. int btrfs_close_extra_devices(struct btrfs_fs_devices *fs_devices)
  259. {
  260. struct list_head *head = &fs_devices->devices;
  261. struct list_head *cur;
  262. struct btrfs_device *device;
  263. mutex_lock(&uuid_mutex);
  264. again:
  265. list_for_each(cur, head) {
  266. device = list_entry(cur, struct btrfs_device, dev_list);
  267. if (!device->in_fs_metadata) {
  268. struct block_device *bdev;
  269. list_del(&device->dev_list);
  270. list_del(&device->dev_alloc_list);
  271. fs_devices->num_devices--;
  272. if (device->bdev) {
  273. bdev = device->bdev;
  274. fs_devices->open_devices--;
  275. mutex_unlock(&uuid_mutex);
  276. close_bdev_excl(bdev);
  277. mutex_lock(&uuid_mutex);
  278. }
  279. kfree(device->name);
  280. kfree(device);
  281. goto again;
  282. }
  283. }
  284. mutex_unlock(&uuid_mutex);
  285. return 0;
  286. }
  287. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  288. {
  289. struct list_head *head = &fs_devices->devices;
  290. struct list_head *cur;
  291. struct btrfs_device *device;
  292. mutex_lock(&uuid_mutex);
  293. list_for_each(cur, head) {
  294. device = list_entry(cur, struct btrfs_device, dev_list);
  295. if (device->bdev) {
  296. close_bdev_excl(device->bdev);
  297. fs_devices->open_devices--;
  298. }
  299. device->bdev = NULL;
  300. device->in_fs_metadata = 0;
  301. }
  302. fs_devices->mounted = 0;
  303. mutex_unlock(&uuid_mutex);
  304. return 0;
  305. }
  306. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  307. int flags, void *holder)
  308. {
  309. struct block_device *bdev;
  310. struct list_head *head = &fs_devices->devices;
  311. struct list_head *cur;
  312. struct btrfs_device *device;
  313. struct block_device *latest_bdev = NULL;
  314. struct buffer_head *bh;
  315. struct btrfs_super_block *disk_super;
  316. u64 latest_devid = 0;
  317. u64 latest_transid = 0;
  318. u64 transid;
  319. u64 devid;
  320. int ret = 0;
  321. mutex_lock(&uuid_mutex);
  322. if (fs_devices->mounted)
  323. goto out;
  324. list_for_each(cur, head) {
  325. device = list_entry(cur, struct btrfs_device, dev_list);
  326. if (device->bdev)
  327. continue;
  328. if (!device->name)
  329. continue;
  330. bdev = open_bdev_excl(device->name, flags, holder);
  331. if (IS_ERR(bdev)) {
  332. printk("open %s failed\n", device->name);
  333. goto error;
  334. }
  335. set_blocksize(bdev, 4096);
  336. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  337. if (!bh)
  338. goto error_close;
  339. disk_super = (struct btrfs_super_block *)bh->b_data;
  340. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  341. sizeof(disk_super->magic)))
  342. goto error_brelse;
  343. devid = le64_to_cpu(disk_super->dev_item.devid);
  344. if (devid != device->devid)
  345. goto error_brelse;
  346. transid = btrfs_super_generation(disk_super);
  347. if (!latest_transid || transid > latest_transid) {
  348. latest_devid = devid;
  349. latest_transid = transid;
  350. latest_bdev = bdev;
  351. }
  352. device->bdev = bdev;
  353. device->in_fs_metadata = 0;
  354. fs_devices->open_devices++;
  355. continue;
  356. error_brelse:
  357. brelse(bh);
  358. error_close:
  359. close_bdev_excl(bdev);
  360. error:
  361. continue;
  362. }
  363. if (fs_devices->open_devices == 0) {
  364. ret = -EIO;
  365. goto out;
  366. }
  367. fs_devices->mounted = 1;
  368. fs_devices->latest_bdev = latest_bdev;
  369. fs_devices->latest_devid = latest_devid;
  370. fs_devices->latest_trans = latest_transid;
  371. out:
  372. mutex_unlock(&uuid_mutex);
  373. return ret;
  374. }
  375. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  376. struct btrfs_fs_devices **fs_devices_ret)
  377. {
  378. struct btrfs_super_block *disk_super;
  379. struct block_device *bdev;
  380. struct buffer_head *bh;
  381. int ret;
  382. u64 devid;
  383. u64 transid;
  384. mutex_lock(&uuid_mutex);
  385. bdev = open_bdev_excl(path, flags, holder);
  386. if (IS_ERR(bdev)) {
  387. ret = PTR_ERR(bdev);
  388. goto error;
  389. }
  390. ret = set_blocksize(bdev, 4096);
  391. if (ret)
  392. goto error_close;
  393. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  394. if (!bh) {
  395. ret = -EIO;
  396. goto error_close;
  397. }
  398. disk_super = (struct btrfs_super_block *)bh->b_data;
  399. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  400. sizeof(disk_super->magic))) {
  401. ret = -EINVAL;
  402. goto error_brelse;
  403. }
  404. devid = le64_to_cpu(disk_super->dev_item.devid);
  405. transid = btrfs_super_generation(disk_super);
  406. if (disk_super->label[0])
  407. printk("device label %s ", disk_super->label);
  408. else {
  409. /* FIXME, make a readl uuid parser */
  410. printk("device fsid %llx-%llx ",
  411. *(unsigned long long *)disk_super->fsid,
  412. *(unsigned long long *)(disk_super->fsid + 8));
  413. }
  414. printk("devid %Lu transid %Lu %s\n", devid, transid, path);
  415. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  416. error_brelse:
  417. brelse(bh);
  418. error_close:
  419. close_bdev_excl(bdev);
  420. error:
  421. mutex_unlock(&uuid_mutex);
  422. return ret;
  423. }
  424. /*
  425. * this uses a pretty simple search, the expectation is that it is
  426. * called very infrequently and that a given device has a small number
  427. * of extents
  428. */
  429. static noinline int find_free_dev_extent(struct btrfs_trans_handle *trans,
  430. struct btrfs_device *device,
  431. struct btrfs_path *path,
  432. u64 num_bytes, u64 *start)
  433. {
  434. struct btrfs_key key;
  435. struct btrfs_root *root = device->dev_root;
  436. struct btrfs_dev_extent *dev_extent = NULL;
  437. u64 hole_size = 0;
  438. u64 last_byte = 0;
  439. u64 search_start = 0;
  440. u64 search_end = device->total_bytes;
  441. int ret;
  442. int slot = 0;
  443. int start_found;
  444. struct extent_buffer *l;
  445. start_found = 0;
  446. path->reada = 2;
  447. /* FIXME use last free of some kind */
  448. /* we don't want to overwrite the superblock on the drive,
  449. * so we make sure to start at an offset of at least 1MB
  450. */
  451. search_start = max((u64)1024 * 1024, search_start);
  452. if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
  453. search_start = max(root->fs_info->alloc_start, search_start);
  454. key.objectid = device->devid;
  455. key.offset = search_start;
  456. key.type = BTRFS_DEV_EXTENT_KEY;
  457. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  458. if (ret < 0)
  459. goto error;
  460. ret = btrfs_previous_item(root, path, 0, key.type);
  461. if (ret < 0)
  462. goto error;
  463. l = path->nodes[0];
  464. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  465. while (1) {
  466. l = path->nodes[0];
  467. slot = path->slots[0];
  468. if (slot >= btrfs_header_nritems(l)) {
  469. ret = btrfs_next_leaf(root, path);
  470. if (ret == 0)
  471. continue;
  472. if (ret < 0)
  473. goto error;
  474. no_more_items:
  475. if (!start_found) {
  476. if (search_start >= search_end) {
  477. ret = -ENOSPC;
  478. goto error;
  479. }
  480. *start = search_start;
  481. start_found = 1;
  482. goto check_pending;
  483. }
  484. *start = last_byte > search_start ?
  485. last_byte : search_start;
  486. if (search_end <= *start) {
  487. ret = -ENOSPC;
  488. goto error;
  489. }
  490. goto check_pending;
  491. }
  492. btrfs_item_key_to_cpu(l, &key, slot);
  493. if (key.objectid < device->devid)
  494. goto next;
  495. if (key.objectid > device->devid)
  496. goto no_more_items;
  497. if (key.offset >= search_start && key.offset > last_byte &&
  498. start_found) {
  499. if (last_byte < search_start)
  500. last_byte = search_start;
  501. hole_size = key.offset - last_byte;
  502. if (key.offset > last_byte &&
  503. hole_size >= num_bytes) {
  504. *start = last_byte;
  505. goto check_pending;
  506. }
  507. }
  508. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  509. goto next;
  510. }
  511. start_found = 1;
  512. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  513. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  514. next:
  515. path->slots[0]++;
  516. cond_resched();
  517. }
  518. check_pending:
  519. /* we have to make sure we didn't find an extent that has already
  520. * been allocated by the map tree or the original allocation
  521. */
  522. btrfs_release_path(root, path);
  523. BUG_ON(*start < search_start);
  524. if (*start + num_bytes > search_end) {
  525. ret = -ENOSPC;
  526. goto error;
  527. }
  528. /* check for pending inserts here */
  529. return 0;
  530. error:
  531. btrfs_release_path(root, path);
  532. return ret;
  533. }
  534. int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  535. struct btrfs_device *device,
  536. u64 start)
  537. {
  538. int ret;
  539. struct btrfs_path *path;
  540. struct btrfs_root *root = device->dev_root;
  541. struct btrfs_key key;
  542. struct btrfs_key found_key;
  543. struct extent_buffer *leaf = NULL;
  544. struct btrfs_dev_extent *extent = NULL;
  545. path = btrfs_alloc_path();
  546. if (!path)
  547. return -ENOMEM;
  548. key.objectid = device->devid;
  549. key.offset = start;
  550. key.type = BTRFS_DEV_EXTENT_KEY;
  551. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  552. if (ret > 0) {
  553. ret = btrfs_previous_item(root, path, key.objectid,
  554. BTRFS_DEV_EXTENT_KEY);
  555. BUG_ON(ret);
  556. leaf = path->nodes[0];
  557. btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
  558. extent = btrfs_item_ptr(leaf, path->slots[0],
  559. struct btrfs_dev_extent);
  560. BUG_ON(found_key.offset > start || found_key.offset +
  561. btrfs_dev_extent_length(leaf, extent) < start);
  562. ret = 0;
  563. } else if (ret == 0) {
  564. leaf = path->nodes[0];
  565. extent = btrfs_item_ptr(leaf, path->slots[0],
  566. struct btrfs_dev_extent);
  567. }
  568. BUG_ON(ret);
  569. if (device->bytes_used > 0)
  570. device->bytes_used -= btrfs_dev_extent_length(leaf, extent);
  571. ret = btrfs_del_item(trans, root, path);
  572. BUG_ON(ret);
  573. btrfs_free_path(path);
  574. return ret;
  575. }
  576. int noinline btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  577. struct btrfs_device *device,
  578. u64 chunk_tree, u64 chunk_objectid,
  579. u64 chunk_offset,
  580. u64 num_bytes, u64 *start)
  581. {
  582. int ret;
  583. struct btrfs_path *path;
  584. struct btrfs_root *root = device->dev_root;
  585. struct btrfs_dev_extent *extent;
  586. struct extent_buffer *leaf;
  587. struct btrfs_key key;
  588. WARN_ON(!device->in_fs_metadata);
  589. path = btrfs_alloc_path();
  590. if (!path)
  591. return -ENOMEM;
  592. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  593. if (ret) {
  594. goto err;
  595. }
  596. key.objectid = device->devid;
  597. key.offset = *start;
  598. key.type = BTRFS_DEV_EXTENT_KEY;
  599. ret = btrfs_insert_empty_item(trans, root, path, &key,
  600. sizeof(*extent));
  601. BUG_ON(ret);
  602. leaf = path->nodes[0];
  603. extent = btrfs_item_ptr(leaf, path->slots[0],
  604. struct btrfs_dev_extent);
  605. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  606. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  607. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  608. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  609. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  610. BTRFS_UUID_SIZE);
  611. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  612. btrfs_mark_buffer_dirty(leaf);
  613. err:
  614. btrfs_free_path(path);
  615. return ret;
  616. }
  617. static noinline int find_next_chunk(struct btrfs_root *root,
  618. u64 objectid, u64 *offset)
  619. {
  620. struct btrfs_path *path;
  621. int ret;
  622. struct btrfs_key key;
  623. struct btrfs_chunk *chunk;
  624. struct btrfs_key found_key;
  625. path = btrfs_alloc_path();
  626. BUG_ON(!path);
  627. key.objectid = objectid;
  628. key.offset = (u64)-1;
  629. key.type = BTRFS_CHUNK_ITEM_KEY;
  630. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  631. if (ret < 0)
  632. goto error;
  633. BUG_ON(ret == 0);
  634. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  635. if (ret) {
  636. *offset = 0;
  637. } else {
  638. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  639. path->slots[0]);
  640. if (found_key.objectid != objectid)
  641. *offset = 0;
  642. else {
  643. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  644. struct btrfs_chunk);
  645. *offset = found_key.offset +
  646. btrfs_chunk_length(path->nodes[0], chunk);
  647. }
  648. }
  649. ret = 0;
  650. error:
  651. btrfs_free_path(path);
  652. return ret;
  653. }
  654. static noinline int find_next_devid(struct btrfs_root *root,
  655. struct btrfs_path *path, u64 *objectid)
  656. {
  657. int ret;
  658. struct btrfs_key key;
  659. struct btrfs_key found_key;
  660. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  661. key.type = BTRFS_DEV_ITEM_KEY;
  662. key.offset = (u64)-1;
  663. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  664. if (ret < 0)
  665. goto error;
  666. BUG_ON(ret == 0);
  667. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  668. BTRFS_DEV_ITEM_KEY);
  669. if (ret) {
  670. *objectid = 1;
  671. } else {
  672. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  673. path->slots[0]);
  674. *objectid = found_key.offset + 1;
  675. }
  676. ret = 0;
  677. error:
  678. btrfs_release_path(root, path);
  679. return ret;
  680. }
  681. /*
  682. * the device information is stored in the chunk root
  683. * the btrfs_device struct should be fully filled in
  684. */
  685. int btrfs_add_device(struct btrfs_trans_handle *trans,
  686. struct btrfs_root *root,
  687. struct btrfs_device *device)
  688. {
  689. int ret;
  690. struct btrfs_path *path;
  691. struct btrfs_dev_item *dev_item;
  692. struct extent_buffer *leaf;
  693. struct btrfs_key key;
  694. unsigned long ptr;
  695. u64 free_devid = 0;
  696. root = root->fs_info->chunk_root;
  697. path = btrfs_alloc_path();
  698. if (!path)
  699. return -ENOMEM;
  700. ret = find_next_devid(root, path, &free_devid);
  701. if (ret)
  702. goto out;
  703. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  704. key.type = BTRFS_DEV_ITEM_KEY;
  705. key.offset = free_devid;
  706. ret = btrfs_insert_empty_item(trans, root, path, &key,
  707. sizeof(*dev_item));
  708. if (ret)
  709. goto out;
  710. leaf = path->nodes[0];
  711. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  712. device->devid = free_devid;
  713. btrfs_set_device_id(leaf, dev_item, device->devid);
  714. btrfs_set_device_type(leaf, dev_item, device->type);
  715. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  716. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  717. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  718. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  719. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  720. btrfs_set_device_group(leaf, dev_item, 0);
  721. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  722. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  723. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  724. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  725. btrfs_mark_buffer_dirty(leaf);
  726. ret = 0;
  727. out:
  728. btrfs_free_path(path);
  729. return ret;
  730. }
  731. static int btrfs_rm_dev_item(struct btrfs_root *root,
  732. struct btrfs_device *device)
  733. {
  734. int ret;
  735. struct btrfs_path *path;
  736. struct block_device *bdev = device->bdev;
  737. struct btrfs_device *next_dev;
  738. struct btrfs_key key;
  739. u64 total_bytes;
  740. struct btrfs_fs_devices *fs_devices;
  741. struct btrfs_trans_handle *trans;
  742. root = root->fs_info->chunk_root;
  743. path = btrfs_alloc_path();
  744. if (!path)
  745. return -ENOMEM;
  746. trans = btrfs_start_transaction(root, 1);
  747. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  748. key.type = BTRFS_DEV_ITEM_KEY;
  749. key.offset = device->devid;
  750. lock_chunks(root);
  751. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  752. if (ret < 0)
  753. goto out;
  754. if (ret > 0) {
  755. ret = -ENOENT;
  756. goto out;
  757. }
  758. ret = btrfs_del_item(trans, root, path);
  759. if (ret)
  760. goto out;
  761. /*
  762. * at this point, the device is zero sized. We want to
  763. * remove it from the devices list and zero out the old super
  764. */
  765. list_del_init(&device->dev_list);
  766. list_del_init(&device->dev_alloc_list);
  767. fs_devices = root->fs_info->fs_devices;
  768. next_dev = list_entry(fs_devices->devices.next, struct btrfs_device,
  769. dev_list);
  770. if (bdev == root->fs_info->sb->s_bdev)
  771. root->fs_info->sb->s_bdev = next_dev->bdev;
  772. if (bdev == fs_devices->latest_bdev)
  773. fs_devices->latest_bdev = next_dev->bdev;
  774. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  775. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  776. total_bytes - 1);
  777. out:
  778. btrfs_free_path(path);
  779. unlock_chunks(root);
  780. btrfs_commit_transaction(trans, root);
  781. return ret;
  782. }
  783. int btrfs_rm_device(struct btrfs_root *root, char *device_path)
  784. {
  785. struct btrfs_device *device;
  786. struct block_device *bdev;
  787. struct buffer_head *bh = NULL;
  788. struct btrfs_super_block *disk_super;
  789. u64 all_avail;
  790. u64 devid;
  791. int ret = 0;
  792. mutex_lock(&uuid_mutex);
  793. mutex_lock(&root->fs_info->volume_mutex);
  794. all_avail = root->fs_info->avail_data_alloc_bits |
  795. root->fs_info->avail_system_alloc_bits |
  796. root->fs_info->avail_metadata_alloc_bits;
  797. if ((all_avail & BTRFS_BLOCK_GROUP_RAID10) &&
  798. btrfs_super_num_devices(&root->fs_info->super_copy) <= 4) {
  799. printk("btrfs: unable to go below four devices on raid10\n");
  800. ret = -EINVAL;
  801. goto out;
  802. }
  803. if ((all_avail & BTRFS_BLOCK_GROUP_RAID1) &&
  804. btrfs_super_num_devices(&root->fs_info->super_copy) <= 2) {
  805. printk("btrfs: unable to go below two devices on raid1\n");
  806. ret = -EINVAL;
  807. goto out;
  808. }
  809. if (strcmp(device_path, "missing") == 0) {
  810. struct list_head *cur;
  811. struct list_head *devices;
  812. struct btrfs_device *tmp;
  813. device = NULL;
  814. devices = &root->fs_info->fs_devices->devices;
  815. list_for_each(cur, devices) {
  816. tmp = list_entry(cur, struct btrfs_device, dev_list);
  817. if (tmp->in_fs_metadata && !tmp->bdev) {
  818. device = tmp;
  819. break;
  820. }
  821. }
  822. bdev = NULL;
  823. bh = NULL;
  824. disk_super = NULL;
  825. if (!device) {
  826. printk("btrfs: no missing devices found to remove\n");
  827. goto out;
  828. }
  829. } else {
  830. bdev = open_bdev_excl(device_path, 0,
  831. root->fs_info->bdev_holder);
  832. if (IS_ERR(bdev)) {
  833. ret = PTR_ERR(bdev);
  834. goto out;
  835. }
  836. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  837. if (!bh) {
  838. ret = -EIO;
  839. goto error_close;
  840. }
  841. disk_super = (struct btrfs_super_block *)bh->b_data;
  842. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  843. sizeof(disk_super->magic))) {
  844. ret = -ENOENT;
  845. goto error_brelse;
  846. }
  847. if (memcmp(disk_super->fsid, root->fs_info->fsid,
  848. BTRFS_FSID_SIZE)) {
  849. ret = -ENOENT;
  850. goto error_brelse;
  851. }
  852. devid = le64_to_cpu(disk_super->dev_item.devid);
  853. device = btrfs_find_device(root, devid, NULL);
  854. if (!device) {
  855. ret = -ENOENT;
  856. goto error_brelse;
  857. }
  858. }
  859. root->fs_info->fs_devices->num_devices--;
  860. root->fs_info->fs_devices->open_devices--;
  861. ret = btrfs_shrink_device(device, 0);
  862. if (ret)
  863. goto error_brelse;
  864. ret = btrfs_rm_dev_item(root->fs_info->chunk_root, device);
  865. if (ret)
  866. goto error_brelse;
  867. if (bh) {
  868. /* make sure this device isn't detected as part of
  869. * the FS anymore
  870. */
  871. memset(&disk_super->magic, 0, sizeof(disk_super->magic));
  872. set_buffer_dirty(bh);
  873. sync_dirty_buffer(bh);
  874. brelse(bh);
  875. }
  876. if (device->bdev) {
  877. /* one close for the device struct or super_block */
  878. close_bdev_excl(device->bdev);
  879. }
  880. if (bdev) {
  881. /* one close for us */
  882. close_bdev_excl(bdev);
  883. }
  884. kfree(device->name);
  885. kfree(device);
  886. ret = 0;
  887. goto out;
  888. error_brelse:
  889. brelse(bh);
  890. error_close:
  891. if (bdev)
  892. close_bdev_excl(bdev);
  893. out:
  894. mutex_unlock(&root->fs_info->volume_mutex);
  895. mutex_unlock(&uuid_mutex);
  896. return ret;
  897. }
  898. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  899. {
  900. struct btrfs_trans_handle *trans;
  901. struct btrfs_device *device;
  902. struct block_device *bdev;
  903. struct list_head *cur;
  904. struct list_head *devices;
  905. u64 total_bytes;
  906. int ret = 0;
  907. bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
  908. if (!bdev) {
  909. return -EIO;
  910. }
  911. filemap_write_and_wait(bdev->bd_inode->i_mapping);
  912. mutex_lock(&root->fs_info->volume_mutex);
  913. trans = btrfs_start_transaction(root, 1);
  914. lock_chunks(root);
  915. devices = &root->fs_info->fs_devices->devices;
  916. list_for_each(cur, devices) {
  917. device = list_entry(cur, struct btrfs_device, dev_list);
  918. if (device->bdev == bdev) {
  919. ret = -EEXIST;
  920. goto out;
  921. }
  922. }
  923. device = kzalloc(sizeof(*device), GFP_NOFS);
  924. if (!device) {
  925. /* we can safely leave the fs_devices entry around */
  926. ret = -ENOMEM;
  927. goto out_close_bdev;
  928. }
  929. device->barriers = 1;
  930. device->work.func = pending_bios_fn;
  931. generate_random_uuid(device->uuid);
  932. spin_lock_init(&device->io_lock);
  933. device->name = kstrdup(device_path, GFP_NOFS);
  934. if (!device->name) {
  935. kfree(device);
  936. goto out_close_bdev;
  937. }
  938. device->io_width = root->sectorsize;
  939. device->io_align = root->sectorsize;
  940. device->sector_size = root->sectorsize;
  941. device->total_bytes = i_size_read(bdev->bd_inode);
  942. device->dev_root = root->fs_info->dev_root;
  943. device->bdev = bdev;
  944. device->in_fs_metadata = 1;
  945. ret = btrfs_add_device(trans, root, device);
  946. if (ret)
  947. goto out_close_bdev;
  948. set_blocksize(device->bdev, 4096);
  949. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  950. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  951. total_bytes + device->total_bytes);
  952. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  953. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  954. total_bytes + 1);
  955. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  956. list_add(&device->dev_alloc_list,
  957. &root->fs_info->fs_devices->alloc_list);
  958. root->fs_info->fs_devices->num_devices++;
  959. root->fs_info->fs_devices->open_devices++;
  960. out:
  961. unlock_chunks(root);
  962. btrfs_end_transaction(trans, root);
  963. mutex_unlock(&root->fs_info->volume_mutex);
  964. return ret;
  965. out_close_bdev:
  966. close_bdev_excl(bdev);
  967. goto out;
  968. }
  969. int noinline btrfs_update_device(struct btrfs_trans_handle *trans,
  970. struct btrfs_device *device)
  971. {
  972. int ret;
  973. struct btrfs_path *path;
  974. struct btrfs_root *root;
  975. struct btrfs_dev_item *dev_item;
  976. struct extent_buffer *leaf;
  977. struct btrfs_key key;
  978. root = device->dev_root->fs_info->chunk_root;
  979. path = btrfs_alloc_path();
  980. if (!path)
  981. return -ENOMEM;
  982. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  983. key.type = BTRFS_DEV_ITEM_KEY;
  984. key.offset = device->devid;
  985. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  986. if (ret < 0)
  987. goto out;
  988. if (ret > 0) {
  989. ret = -ENOENT;
  990. goto out;
  991. }
  992. leaf = path->nodes[0];
  993. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  994. btrfs_set_device_id(leaf, dev_item, device->devid);
  995. btrfs_set_device_type(leaf, dev_item, device->type);
  996. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  997. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  998. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  999. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  1000. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  1001. btrfs_mark_buffer_dirty(leaf);
  1002. out:
  1003. btrfs_free_path(path);
  1004. return ret;
  1005. }
  1006. static int __btrfs_grow_device(struct btrfs_trans_handle *trans,
  1007. struct btrfs_device *device, u64 new_size)
  1008. {
  1009. struct btrfs_super_block *super_copy =
  1010. &device->dev_root->fs_info->super_copy;
  1011. u64 old_total = btrfs_super_total_bytes(super_copy);
  1012. u64 diff = new_size - device->total_bytes;
  1013. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  1014. return btrfs_update_device(trans, device);
  1015. }
  1016. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  1017. struct btrfs_device *device, u64 new_size)
  1018. {
  1019. int ret;
  1020. lock_chunks(device->dev_root);
  1021. ret = __btrfs_grow_device(trans, device, new_size);
  1022. unlock_chunks(device->dev_root);
  1023. return ret;
  1024. }
  1025. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  1026. struct btrfs_root *root,
  1027. u64 chunk_tree, u64 chunk_objectid,
  1028. u64 chunk_offset)
  1029. {
  1030. int ret;
  1031. struct btrfs_path *path;
  1032. struct btrfs_key key;
  1033. root = root->fs_info->chunk_root;
  1034. path = btrfs_alloc_path();
  1035. if (!path)
  1036. return -ENOMEM;
  1037. key.objectid = chunk_objectid;
  1038. key.offset = chunk_offset;
  1039. key.type = BTRFS_CHUNK_ITEM_KEY;
  1040. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  1041. BUG_ON(ret);
  1042. ret = btrfs_del_item(trans, root, path);
  1043. BUG_ON(ret);
  1044. btrfs_free_path(path);
  1045. return 0;
  1046. }
  1047. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  1048. chunk_offset)
  1049. {
  1050. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1051. struct btrfs_disk_key *disk_key;
  1052. struct btrfs_chunk *chunk;
  1053. u8 *ptr;
  1054. int ret = 0;
  1055. u32 num_stripes;
  1056. u32 array_size;
  1057. u32 len = 0;
  1058. u32 cur;
  1059. struct btrfs_key key;
  1060. array_size = btrfs_super_sys_array_size(super_copy);
  1061. ptr = super_copy->sys_chunk_array;
  1062. cur = 0;
  1063. while (cur < array_size) {
  1064. disk_key = (struct btrfs_disk_key *)ptr;
  1065. btrfs_disk_key_to_cpu(&key, disk_key);
  1066. len = sizeof(*disk_key);
  1067. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1068. chunk = (struct btrfs_chunk *)(ptr + len);
  1069. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  1070. len += btrfs_chunk_item_size(num_stripes);
  1071. } else {
  1072. ret = -EIO;
  1073. break;
  1074. }
  1075. if (key.objectid == chunk_objectid &&
  1076. key.offset == chunk_offset) {
  1077. memmove(ptr, ptr + len, array_size - (cur + len));
  1078. array_size -= len;
  1079. btrfs_set_super_sys_array_size(super_copy, array_size);
  1080. } else {
  1081. ptr += len;
  1082. cur += len;
  1083. }
  1084. }
  1085. return ret;
  1086. }
  1087. int btrfs_relocate_chunk(struct btrfs_root *root,
  1088. u64 chunk_tree, u64 chunk_objectid,
  1089. u64 chunk_offset)
  1090. {
  1091. struct extent_map_tree *em_tree;
  1092. struct btrfs_root *extent_root;
  1093. struct btrfs_trans_handle *trans;
  1094. struct extent_map *em;
  1095. struct map_lookup *map;
  1096. int ret;
  1097. int i;
  1098. printk("btrfs relocating chunk %llu\n",
  1099. (unsigned long long)chunk_offset);
  1100. root = root->fs_info->chunk_root;
  1101. extent_root = root->fs_info->extent_root;
  1102. em_tree = &root->fs_info->mapping_tree.map_tree;
  1103. /* step one, relocate all the extents inside this chunk */
  1104. ret = btrfs_relocate_block_group(extent_root, chunk_offset);
  1105. BUG_ON(ret);
  1106. trans = btrfs_start_transaction(root, 1);
  1107. BUG_ON(!trans);
  1108. lock_chunks(root);
  1109. /*
  1110. * step two, delete the device extents and the
  1111. * chunk tree entries
  1112. */
  1113. spin_lock(&em_tree->lock);
  1114. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  1115. spin_unlock(&em_tree->lock);
  1116. BUG_ON(em->start > chunk_offset ||
  1117. em->start + em->len < chunk_offset);
  1118. map = (struct map_lookup *)em->bdev;
  1119. for (i = 0; i < map->num_stripes; i++) {
  1120. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  1121. map->stripes[i].physical);
  1122. BUG_ON(ret);
  1123. if (map->stripes[i].dev) {
  1124. ret = btrfs_update_device(trans, map->stripes[i].dev);
  1125. BUG_ON(ret);
  1126. }
  1127. }
  1128. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  1129. chunk_offset);
  1130. BUG_ON(ret);
  1131. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1132. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  1133. BUG_ON(ret);
  1134. }
  1135. ret = btrfs_remove_block_group(trans, extent_root, chunk_offset);
  1136. BUG_ON(ret);
  1137. spin_lock(&em_tree->lock);
  1138. remove_extent_mapping(em_tree, em);
  1139. spin_unlock(&em_tree->lock);
  1140. kfree(map);
  1141. em->bdev = NULL;
  1142. /* once for the tree */
  1143. free_extent_map(em);
  1144. /* once for us */
  1145. free_extent_map(em);
  1146. unlock_chunks(root);
  1147. btrfs_end_transaction(trans, root);
  1148. return 0;
  1149. }
  1150. static u64 div_factor(u64 num, int factor)
  1151. {
  1152. if (factor == 10)
  1153. return num;
  1154. num *= factor;
  1155. do_div(num, 10);
  1156. return num;
  1157. }
  1158. int btrfs_balance(struct btrfs_root *dev_root)
  1159. {
  1160. int ret;
  1161. struct list_head *cur;
  1162. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  1163. struct btrfs_device *device;
  1164. u64 old_size;
  1165. u64 size_to_free;
  1166. struct btrfs_path *path;
  1167. struct btrfs_key key;
  1168. struct btrfs_chunk *chunk;
  1169. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  1170. struct btrfs_trans_handle *trans;
  1171. struct btrfs_key found_key;
  1172. mutex_lock(&dev_root->fs_info->volume_mutex);
  1173. dev_root = dev_root->fs_info->dev_root;
  1174. /* step one make some room on all the devices */
  1175. list_for_each(cur, devices) {
  1176. device = list_entry(cur, struct btrfs_device, dev_list);
  1177. old_size = device->total_bytes;
  1178. size_to_free = div_factor(old_size, 1);
  1179. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  1180. if (device->total_bytes - device->bytes_used > size_to_free)
  1181. continue;
  1182. ret = btrfs_shrink_device(device, old_size - size_to_free);
  1183. BUG_ON(ret);
  1184. trans = btrfs_start_transaction(dev_root, 1);
  1185. BUG_ON(!trans);
  1186. ret = btrfs_grow_device(trans, device, old_size);
  1187. BUG_ON(ret);
  1188. btrfs_end_transaction(trans, dev_root);
  1189. }
  1190. /* step two, relocate all the chunks */
  1191. path = btrfs_alloc_path();
  1192. BUG_ON(!path);
  1193. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1194. key.offset = (u64)-1;
  1195. key.type = BTRFS_CHUNK_ITEM_KEY;
  1196. while(1) {
  1197. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  1198. if (ret < 0)
  1199. goto error;
  1200. /*
  1201. * this shouldn't happen, it means the last relocate
  1202. * failed
  1203. */
  1204. if (ret == 0)
  1205. break;
  1206. ret = btrfs_previous_item(chunk_root, path, 0,
  1207. BTRFS_CHUNK_ITEM_KEY);
  1208. if (ret)
  1209. break;
  1210. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  1211. path->slots[0]);
  1212. if (found_key.objectid != key.objectid)
  1213. break;
  1214. chunk = btrfs_item_ptr(path->nodes[0],
  1215. path->slots[0],
  1216. struct btrfs_chunk);
  1217. key.offset = found_key.offset;
  1218. /* chunk zero is special */
  1219. if (key.offset == 0)
  1220. break;
  1221. btrfs_release_path(chunk_root, path);
  1222. ret = btrfs_relocate_chunk(chunk_root,
  1223. chunk_root->root_key.objectid,
  1224. found_key.objectid,
  1225. found_key.offset);
  1226. BUG_ON(ret);
  1227. }
  1228. ret = 0;
  1229. error:
  1230. btrfs_free_path(path);
  1231. mutex_unlock(&dev_root->fs_info->volume_mutex);
  1232. return ret;
  1233. }
  1234. /*
  1235. * shrinking a device means finding all of the device extents past
  1236. * the new size, and then following the back refs to the chunks.
  1237. * The chunk relocation code actually frees the device extent
  1238. */
  1239. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  1240. {
  1241. struct btrfs_trans_handle *trans;
  1242. struct btrfs_root *root = device->dev_root;
  1243. struct btrfs_dev_extent *dev_extent = NULL;
  1244. struct btrfs_path *path;
  1245. u64 length;
  1246. u64 chunk_tree;
  1247. u64 chunk_objectid;
  1248. u64 chunk_offset;
  1249. int ret;
  1250. int slot;
  1251. struct extent_buffer *l;
  1252. struct btrfs_key key;
  1253. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1254. u64 old_total = btrfs_super_total_bytes(super_copy);
  1255. u64 diff = device->total_bytes - new_size;
  1256. path = btrfs_alloc_path();
  1257. if (!path)
  1258. return -ENOMEM;
  1259. trans = btrfs_start_transaction(root, 1);
  1260. if (!trans) {
  1261. ret = -ENOMEM;
  1262. goto done;
  1263. }
  1264. path->reada = 2;
  1265. lock_chunks(root);
  1266. device->total_bytes = new_size;
  1267. ret = btrfs_update_device(trans, device);
  1268. if (ret) {
  1269. unlock_chunks(root);
  1270. btrfs_end_transaction(trans, root);
  1271. goto done;
  1272. }
  1273. WARN_ON(diff > old_total);
  1274. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  1275. unlock_chunks(root);
  1276. btrfs_end_transaction(trans, root);
  1277. key.objectid = device->devid;
  1278. key.offset = (u64)-1;
  1279. key.type = BTRFS_DEV_EXTENT_KEY;
  1280. while (1) {
  1281. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1282. if (ret < 0)
  1283. goto done;
  1284. ret = btrfs_previous_item(root, path, 0, key.type);
  1285. if (ret < 0)
  1286. goto done;
  1287. if (ret) {
  1288. ret = 0;
  1289. goto done;
  1290. }
  1291. l = path->nodes[0];
  1292. slot = path->slots[0];
  1293. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  1294. if (key.objectid != device->devid)
  1295. goto done;
  1296. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  1297. length = btrfs_dev_extent_length(l, dev_extent);
  1298. if (key.offset + length <= new_size)
  1299. goto done;
  1300. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  1301. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  1302. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  1303. btrfs_release_path(root, path);
  1304. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  1305. chunk_offset);
  1306. if (ret)
  1307. goto done;
  1308. }
  1309. done:
  1310. btrfs_free_path(path);
  1311. return ret;
  1312. }
  1313. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  1314. struct btrfs_root *root,
  1315. struct btrfs_key *key,
  1316. struct btrfs_chunk *chunk, int item_size)
  1317. {
  1318. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1319. struct btrfs_disk_key disk_key;
  1320. u32 array_size;
  1321. u8 *ptr;
  1322. array_size = btrfs_super_sys_array_size(super_copy);
  1323. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  1324. return -EFBIG;
  1325. ptr = super_copy->sys_chunk_array + array_size;
  1326. btrfs_cpu_key_to_disk(&disk_key, key);
  1327. memcpy(ptr, &disk_key, sizeof(disk_key));
  1328. ptr += sizeof(disk_key);
  1329. memcpy(ptr, chunk, item_size);
  1330. item_size += sizeof(disk_key);
  1331. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  1332. return 0;
  1333. }
  1334. static u64 noinline chunk_bytes_by_type(u64 type, u64 calc_size,
  1335. int num_stripes, int sub_stripes)
  1336. {
  1337. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  1338. return calc_size;
  1339. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  1340. return calc_size * (num_stripes / sub_stripes);
  1341. else
  1342. return calc_size * num_stripes;
  1343. }
  1344. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  1345. struct btrfs_root *extent_root, u64 *start,
  1346. u64 *num_bytes, u64 type)
  1347. {
  1348. u64 dev_offset;
  1349. struct btrfs_fs_info *info = extent_root->fs_info;
  1350. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  1351. struct btrfs_path *path;
  1352. struct btrfs_stripe *stripes;
  1353. struct btrfs_device *device = NULL;
  1354. struct btrfs_chunk *chunk;
  1355. struct list_head private_devs;
  1356. struct list_head *dev_list;
  1357. struct list_head *cur;
  1358. struct extent_map_tree *em_tree;
  1359. struct map_lookup *map;
  1360. struct extent_map *em;
  1361. int min_stripe_size = 1 * 1024 * 1024;
  1362. u64 physical;
  1363. u64 calc_size = 1024 * 1024 * 1024;
  1364. u64 max_chunk_size = calc_size;
  1365. u64 min_free;
  1366. u64 avail;
  1367. u64 max_avail = 0;
  1368. u64 percent_max;
  1369. int num_stripes = 1;
  1370. int min_stripes = 1;
  1371. int sub_stripes = 0;
  1372. int looped = 0;
  1373. int ret;
  1374. int index;
  1375. int stripe_len = 64 * 1024;
  1376. struct btrfs_key key;
  1377. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  1378. (type & BTRFS_BLOCK_GROUP_DUP)) {
  1379. WARN_ON(1);
  1380. type &= ~BTRFS_BLOCK_GROUP_DUP;
  1381. }
  1382. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  1383. if (list_empty(dev_list))
  1384. return -ENOSPC;
  1385. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  1386. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1387. min_stripes = 2;
  1388. }
  1389. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  1390. num_stripes = 2;
  1391. min_stripes = 2;
  1392. }
  1393. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  1394. num_stripes = min_t(u64, 2,
  1395. extent_root->fs_info->fs_devices->open_devices);
  1396. if (num_stripes < 2)
  1397. return -ENOSPC;
  1398. min_stripes = 2;
  1399. }
  1400. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1401. num_stripes = extent_root->fs_info->fs_devices->open_devices;
  1402. if (num_stripes < 4)
  1403. return -ENOSPC;
  1404. num_stripes &= ~(u32)1;
  1405. sub_stripes = 2;
  1406. min_stripes = 4;
  1407. }
  1408. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1409. max_chunk_size = 10 * calc_size;
  1410. min_stripe_size = 64 * 1024 * 1024;
  1411. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1412. max_chunk_size = 4 * calc_size;
  1413. min_stripe_size = 32 * 1024 * 1024;
  1414. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1415. calc_size = 8 * 1024 * 1024;
  1416. max_chunk_size = calc_size * 2;
  1417. min_stripe_size = 1 * 1024 * 1024;
  1418. }
  1419. path = btrfs_alloc_path();
  1420. if (!path)
  1421. return -ENOMEM;
  1422. /* we don't want a chunk larger than 10% of the FS */
  1423. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1424. max_chunk_size = min(percent_max, max_chunk_size);
  1425. again:
  1426. if (calc_size * num_stripes > max_chunk_size) {
  1427. calc_size = max_chunk_size;
  1428. do_div(calc_size, num_stripes);
  1429. do_div(calc_size, stripe_len);
  1430. calc_size *= stripe_len;
  1431. }
  1432. /* we don't want tiny stripes */
  1433. calc_size = max_t(u64, min_stripe_size, calc_size);
  1434. do_div(calc_size, stripe_len);
  1435. calc_size *= stripe_len;
  1436. INIT_LIST_HEAD(&private_devs);
  1437. cur = dev_list->next;
  1438. index = 0;
  1439. if (type & BTRFS_BLOCK_GROUP_DUP)
  1440. min_free = calc_size * 2;
  1441. else
  1442. min_free = calc_size;
  1443. /*
  1444. * we add 1MB because we never use the first 1MB of the device, unless
  1445. * we've looped, then we are likely allocating the maximum amount of
  1446. * space left already
  1447. */
  1448. if (!looped)
  1449. min_free += 1024 * 1024;
  1450. /* build a private list of devices we will allocate from */
  1451. while(index < num_stripes) {
  1452. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1453. if (device->total_bytes > device->bytes_used)
  1454. avail = device->total_bytes - device->bytes_used;
  1455. else
  1456. avail = 0;
  1457. cur = cur->next;
  1458. if (device->in_fs_metadata && avail >= min_free) {
  1459. u64 ignored_start = 0;
  1460. ret = find_free_dev_extent(trans, device, path,
  1461. min_free,
  1462. &ignored_start);
  1463. if (ret == 0) {
  1464. list_move_tail(&device->dev_alloc_list,
  1465. &private_devs);
  1466. index++;
  1467. if (type & BTRFS_BLOCK_GROUP_DUP)
  1468. index++;
  1469. }
  1470. } else if (device->in_fs_metadata && avail > max_avail)
  1471. max_avail = avail;
  1472. if (cur == dev_list)
  1473. break;
  1474. }
  1475. if (index < num_stripes) {
  1476. list_splice(&private_devs, dev_list);
  1477. if (index >= min_stripes) {
  1478. num_stripes = index;
  1479. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1480. num_stripes /= sub_stripes;
  1481. num_stripes *= sub_stripes;
  1482. }
  1483. looped = 1;
  1484. goto again;
  1485. }
  1486. if (!looped && max_avail > 0) {
  1487. looped = 1;
  1488. calc_size = max_avail;
  1489. goto again;
  1490. }
  1491. btrfs_free_path(path);
  1492. return -ENOSPC;
  1493. }
  1494. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1495. key.type = BTRFS_CHUNK_ITEM_KEY;
  1496. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1497. &key.offset);
  1498. if (ret) {
  1499. btrfs_free_path(path);
  1500. return ret;
  1501. }
  1502. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1503. if (!chunk) {
  1504. btrfs_free_path(path);
  1505. return -ENOMEM;
  1506. }
  1507. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1508. if (!map) {
  1509. kfree(chunk);
  1510. btrfs_free_path(path);
  1511. return -ENOMEM;
  1512. }
  1513. btrfs_free_path(path);
  1514. path = NULL;
  1515. stripes = &chunk->stripe;
  1516. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1517. num_stripes, sub_stripes);
  1518. index = 0;
  1519. while(index < num_stripes) {
  1520. struct btrfs_stripe *stripe;
  1521. BUG_ON(list_empty(&private_devs));
  1522. cur = private_devs.next;
  1523. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1524. /* loop over this device again if we're doing a dup group */
  1525. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1526. (index == num_stripes - 1))
  1527. list_move_tail(&device->dev_alloc_list, dev_list);
  1528. ret = btrfs_alloc_dev_extent(trans, device,
  1529. info->chunk_root->root_key.objectid,
  1530. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1531. calc_size, &dev_offset);
  1532. BUG_ON(ret);
  1533. device->bytes_used += calc_size;
  1534. ret = btrfs_update_device(trans, device);
  1535. BUG_ON(ret);
  1536. map->stripes[index].dev = device;
  1537. map->stripes[index].physical = dev_offset;
  1538. stripe = stripes + index;
  1539. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1540. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1541. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1542. physical = dev_offset;
  1543. index++;
  1544. }
  1545. BUG_ON(!list_empty(&private_devs));
  1546. /* key was set above */
  1547. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1548. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1549. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1550. btrfs_set_stack_chunk_type(chunk, type);
  1551. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1552. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1553. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1554. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1555. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1556. map->sector_size = extent_root->sectorsize;
  1557. map->stripe_len = stripe_len;
  1558. map->io_align = stripe_len;
  1559. map->io_width = stripe_len;
  1560. map->type = type;
  1561. map->num_stripes = num_stripes;
  1562. map->sub_stripes = sub_stripes;
  1563. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1564. btrfs_chunk_item_size(num_stripes));
  1565. BUG_ON(ret);
  1566. *start = key.offset;;
  1567. em = alloc_extent_map(GFP_NOFS);
  1568. if (!em)
  1569. return -ENOMEM;
  1570. em->bdev = (struct block_device *)map;
  1571. em->start = key.offset;
  1572. em->len = *num_bytes;
  1573. em->block_start = 0;
  1574. em->block_len = em->len;
  1575. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1576. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1577. chunk, btrfs_chunk_item_size(num_stripes));
  1578. BUG_ON(ret);
  1579. }
  1580. kfree(chunk);
  1581. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1582. spin_lock(&em_tree->lock);
  1583. ret = add_extent_mapping(em_tree, em);
  1584. spin_unlock(&em_tree->lock);
  1585. BUG_ON(ret);
  1586. free_extent_map(em);
  1587. return ret;
  1588. }
  1589. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1590. {
  1591. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1592. }
  1593. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1594. {
  1595. struct extent_map *em;
  1596. while(1) {
  1597. spin_lock(&tree->map_tree.lock);
  1598. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1599. if (em)
  1600. remove_extent_mapping(&tree->map_tree, em);
  1601. spin_unlock(&tree->map_tree.lock);
  1602. if (!em)
  1603. break;
  1604. kfree(em->bdev);
  1605. /* once for us */
  1606. free_extent_map(em);
  1607. /* once for the tree */
  1608. free_extent_map(em);
  1609. }
  1610. }
  1611. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1612. {
  1613. struct extent_map *em;
  1614. struct map_lookup *map;
  1615. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1616. int ret;
  1617. spin_lock(&em_tree->lock);
  1618. em = lookup_extent_mapping(em_tree, logical, len);
  1619. spin_unlock(&em_tree->lock);
  1620. BUG_ON(!em);
  1621. BUG_ON(em->start > logical || em->start + em->len < logical);
  1622. map = (struct map_lookup *)em->bdev;
  1623. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1624. ret = map->num_stripes;
  1625. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1626. ret = map->sub_stripes;
  1627. else
  1628. ret = 1;
  1629. free_extent_map(em);
  1630. return ret;
  1631. }
  1632. static int find_live_mirror(struct map_lookup *map, int first, int num,
  1633. int optimal)
  1634. {
  1635. int i;
  1636. if (map->stripes[optimal].dev->bdev)
  1637. return optimal;
  1638. for (i = first; i < first + num; i++) {
  1639. if (map->stripes[i].dev->bdev)
  1640. return i;
  1641. }
  1642. /* we couldn't find one that doesn't fail. Just return something
  1643. * and the io error handling code will clean up eventually
  1644. */
  1645. return optimal;
  1646. }
  1647. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1648. u64 logical, u64 *length,
  1649. struct btrfs_multi_bio **multi_ret,
  1650. int mirror_num, struct page *unplug_page)
  1651. {
  1652. struct extent_map *em;
  1653. struct map_lookup *map;
  1654. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1655. u64 offset;
  1656. u64 stripe_offset;
  1657. u64 stripe_nr;
  1658. int stripes_allocated = 8;
  1659. int stripes_required = 1;
  1660. int stripe_index;
  1661. int i;
  1662. int num_stripes;
  1663. int max_errors = 0;
  1664. struct btrfs_multi_bio *multi = NULL;
  1665. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1666. stripes_allocated = 1;
  1667. }
  1668. again:
  1669. if (multi_ret) {
  1670. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1671. GFP_NOFS);
  1672. if (!multi)
  1673. return -ENOMEM;
  1674. atomic_set(&multi->error, 0);
  1675. }
  1676. spin_lock(&em_tree->lock);
  1677. em = lookup_extent_mapping(em_tree, logical, *length);
  1678. spin_unlock(&em_tree->lock);
  1679. if (!em && unplug_page)
  1680. return 0;
  1681. if (!em) {
  1682. printk("unable to find logical %Lu len %Lu\n", logical, *length);
  1683. BUG();
  1684. }
  1685. BUG_ON(em->start > logical || em->start + em->len < logical);
  1686. map = (struct map_lookup *)em->bdev;
  1687. offset = logical - em->start;
  1688. if (mirror_num > map->num_stripes)
  1689. mirror_num = 0;
  1690. /* if our multi bio struct is too small, back off and try again */
  1691. if (rw & (1 << BIO_RW)) {
  1692. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1693. BTRFS_BLOCK_GROUP_DUP)) {
  1694. stripes_required = map->num_stripes;
  1695. max_errors = 1;
  1696. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1697. stripes_required = map->sub_stripes;
  1698. max_errors = 1;
  1699. }
  1700. }
  1701. if (multi_ret && rw == WRITE &&
  1702. stripes_allocated < stripes_required) {
  1703. stripes_allocated = map->num_stripes;
  1704. free_extent_map(em);
  1705. kfree(multi);
  1706. goto again;
  1707. }
  1708. stripe_nr = offset;
  1709. /*
  1710. * stripe_nr counts the total number of stripes we have to stride
  1711. * to get to this block
  1712. */
  1713. do_div(stripe_nr, map->stripe_len);
  1714. stripe_offset = stripe_nr * map->stripe_len;
  1715. BUG_ON(offset < stripe_offset);
  1716. /* stripe_offset is the offset of this block in its stripe*/
  1717. stripe_offset = offset - stripe_offset;
  1718. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1719. BTRFS_BLOCK_GROUP_RAID10 |
  1720. BTRFS_BLOCK_GROUP_DUP)) {
  1721. /* we limit the length of each bio to what fits in a stripe */
  1722. *length = min_t(u64, em->len - offset,
  1723. map->stripe_len - stripe_offset);
  1724. } else {
  1725. *length = em->len - offset;
  1726. }
  1727. if (!multi_ret && !unplug_page)
  1728. goto out;
  1729. num_stripes = 1;
  1730. stripe_index = 0;
  1731. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1732. if (unplug_page || (rw & (1 << BIO_RW)))
  1733. num_stripes = map->num_stripes;
  1734. else if (mirror_num)
  1735. stripe_index = mirror_num - 1;
  1736. else {
  1737. stripe_index = find_live_mirror(map, 0,
  1738. map->num_stripes,
  1739. current->pid % map->num_stripes);
  1740. }
  1741. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1742. if (rw & (1 << BIO_RW))
  1743. num_stripes = map->num_stripes;
  1744. else if (mirror_num)
  1745. stripe_index = mirror_num - 1;
  1746. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1747. int factor = map->num_stripes / map->sub_stripes;
  1748. stripe_index = do_div(stripe_nr, factor);
  1749. stripe_index *= map->sub_stripes;
  1750. if (unplug_page || (rw & (1 << BIO_RW)))
  1751. num_stripes = map->sub_stripes;
  1752. else if (mirror_num)
  1753. stripe_index += mirror_num - 1;
  1754. else {
  1755. stripe_index = find_live_mirror(map, stripe_index,
  1756. map->sub_stripes, stripe_index +
  1757. current->pid % map->sub_stripes);
  1758. }
  1759. } else {
  1760. /*
  1761. * after this do_div call, stripe_nr is the number of stripes
  1762. * on this device we have to walk to find the data, and
  1763. * stripe_index is the number of our device in the stripe array
  1764. */
  1765. stripe_index = do_div(stripe_nr, map->num_stripes);
  1766. }
  1767. BUG_ON(stripe_index >= map->num_stripes);
  1768. for (i = 0; i < num_stripes; i++) {
  1769. if (unplug_page) {
  1770. struct btrfs_device *device;
  1771. struct backing_dev_info *bdi;
  1772. device = map->stripes[stripe_index].dev;
  1773. if (device->bdev) {
  1774. bdi = blk_get_backing_dev_info(device->bdev);
  1775. if (bdi->unplug_io_fn) {
  1776. bdi->unplug_io_fn(bdi, unplug_page);
  1777. }
  1778. }
  1779. } else {
  1780. multi->stripes[i].physical =
  1781. map->stripes[stripe_index].physical +
  1782. stripe_offset + stripe_nr * map->stripe_len;
  1783. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1784. }
  1785. stripe_index++;
  1786. }
  1787. if (multi_ret) {
  1788. *multi_ret = multi;
  1789. multi->num_stripes = num_stripes;
  1790. multi->max_errors = max_errors;
  1791. }
  1792. out:
  1793. free_extent_map(em);
  1794. return 0;
  1795. }
  1796. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1797. u64 logical, u64 *length,
  1798. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1799. {
  1800. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1801. mirror_num, NULL);
  1802. }
  1803. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1804. u64 logical, struct page *page)
  1805. {
  1806. u64 length = PAGE_CACHE_SIZE;
  1807. return __btrfs_map_block(map_tree, READ, logical, &length,
  1808. NULL, 0, page);
  1809. }
  1810. static void end_bio_multi_stripe(struct bio *bio, int err)
  1811. {
  1812. struct btrfs_multi_bio *multi = bio->bi_private;
  1813. int is_orig_bio = 0;
  1814. if (err)
  1815. atomic_inc(&multi->error);
  1816. if (bio == multi->orig_bio)
  1817. is_orig_bio = 1;
  1818. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1819. if (!is_orig_bio) {
  1820. bio_put(bio);
  1821. bio = multi->orig_bio;
  1822. }
  1823. bio->bi_private = multi->private;
  1824. bio->bi_end_io = multi->end_io;
  1825. /* only send an error to the higher layers if it is
  1826. * beyond the tolerance of the multi-bio
  1827. */
  1828. if (atomic_read(&multi->error) > multi->max_errors) {
  1829. err = -EIO;
  1830. } else if (err) {
  1831. /*
  1832. * this bio is actually up to date, we didn't
  1833. * go over the max number of errors
  1834. */
  1835. set_bit(BIO_UPTODATE, &bio->bi_flags);
  1836. err = 0;
  1837. }
  1838. kfree(multi);
  1839. bio_endio(bio, err);
  1840. } else if (!is_orig_bio) {
  1841. bio_put(bio);
  1842. }
  1843. }
  1844. struct async_sched {
  1845. struct bio *bio;
  1846. int rw;
  1847. struct btrfs_fs_info *info;
  1848. struct btrfs_work work;
  1849. };
  1850. /*
  1851. * see run_scheduled_bios for a description of why bios are collected for
  1852. * async submit.
  1853. *
  1854. * This will add one bio to the pending list for a device and make sure
  1855. * the work struct is scheduled.
  1856. */
  1857. static int noinline schedule_bio(struct btrfs_root *root,
  1858. struct btrfs_device *device,
  1859. int rw, struct bio *bio)
  1860. {
  1861. int should_queue = 1;
  1862. /* don't bother with additional async steps for reads, right now */
  1863. if (!(rw & (1 << BIO_RW))) {
  1864. bio_get(bio);
  1865. submit_bio(rw, bio);
  1866. bio_put(bio);
  1867. return 0;
  1868. }
  1869. /*
  1870. * nr_async_bios allows us to reliably return congestion to the
  1871. * higher layers. Otherwise, the async bio makes it appear we have
  1872. * made progress against dirty pages when we've really just put it
  1873. * on a queue for later
  1874. */
  1875. atomic_inc(&root->fs_info->nr_async_bios);
  1876. WARN_ON(bio->bi_next);
  1877. bio->bi_next = NULL;
  1878. bio->bi_rw |= rw;
  1879. spin_lock(&device->io_lock);
  1880. if (device->pending_bio_tail)
  1881. device->pending_bio_tail->bi_next = bio;
  1882. device->pending_bio_tail = bio;
  1883. if (!device->pending_bios)
  1884. device->pending_bios = bio;
  1885. if (device->running_pending)
  1886. should_queue = 0;
  1887. spin_unlock(&device->io_lock);
  1888. if (should_queue)
  1889. btrfs_queue_worker(&root->fs_info->submit_workers,
  1890. &device->work);
  1891. return 0;
  1892. }
  1893. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1894. int mirror_num, int async_submit)
  1895. {
  1896. struct btrfs_mapping_tree *map_tree;
  1897. struct btrfs_device *dev;
  1898. struct bio *first_bio = bio;
  1899. u64 logical = (u64)bio->bi_sector << 9;
  1900. u64 length = 0;
  1901. u64 map_length;
  1902. struct btrfs_multi_bio *multi = NULL;
  1903. int ret;
  1904. int dev_nr = 0;
  1905. int total_devs = 1;
  1906. length = bio->bi_size;
  1907. map_tree = &root->fs_info->mapping_tree;
  1908. map_length = length;
  1909. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1910. mirror_num);
  1911. BUG_ON(ret);
  1912. total_devs = multi->num_stripes;
  1913. if (map_length < length) {
  1914. printk("mapping failed logical %Lu bio len %Lu "
  1915. "len %Lu\n", logical, length, map_length);
  1916. BUG();
  1917. }
  1918. multi->end_io = first_bio->bi_end_io;
  1919. multi->private = first_bio->bi_private;
  1920. multi->orig_bio = first_bio;
  1921. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1922. while(dev_nr < total_devs) {
  1923. if (total_devs > 1) {
  1924. if (dev_nr < total_devs - 1) {
  1925. bio = bio_clone(first_bio, GFP_NOFS);
  1926. BUG_ON(!bio);
  1927. } else {
  1928. bio = first_bio;
  1929. }
  1930. bio->bi_private = multi;
  1931. bio->bi_end_io = end_bio_multi_stripe;
  1932. }
  1933. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1934. dev = multi->stripes[dev_nr].dev;
  1935. if (dev && dev->bdev) {
  1936. bio->bi_bdev = dev->bdev;
  1937. if (async_submit)
  1938. schedule_bio(root, dev, rw, bio);
  1939. else
  1940. submit_bio(rw, bio);
  1941. } else {
  1942. bio->bi_bdev = root->fs_info->fs_devices->latest_bdev;
  1943. bio->bi_sector = logical >> 9;
  1944. bio_endio(bio, -EIO);
  1945. }
  1946. dev_nr++;
  1947. }
  1948. if (total_devs == 1)
  1949. kfree(multi);
  1950. return 0;
  1951. }
  1952. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1953. u8 *uuid)
  1954. {
  1955. struct list_head *head = &root->fs_info->fs_devices->devices;
  1956. return __find_device(head, devid, uuid);
  1957. }
  1958. static struct btrfs_device *add_missing_dev(struct btrfs_root *root,
  1959. u64 devid, u8 *dev_uuid)
  1960. {
  1961. struct btrfs_device *device;
  1962. struct btrfs_fs_devices *fs_devices = root->fs_info->fs_devices;
  1963. device = kzalloc(sizeof(*device), GFP_NOFS);
  1964. list_add(&device->dev_list,
  1965. &fs_devices->devices);
  1966. list_add(&device->dev_alloc_list,
  1967. &fs_devices->alloc_list);
  1968. device->barriers = 1;
  1969. device->dev_root = root->fs_info->dev_root;
  1970. device->devid = devid;
  1971. device->work.func = pending_bios_fn;
  1972. fs_devices->num_devices++;
  1973. spin_lock_init(&device->io_lock);
  1974. memcpy(device->uuid, dev_uuid, BTRFS_UUID_SIZE);
  1975. return device;
  1976. }
  1977. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1978. struct extent_buffer *leaf,
  1979. struct btrfs_chunk *chunk)
  1980. {
  1981. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1982. struct map_lookup *map;
  1983. struct extent_map *em;
  1984. u64 logical;
  1985. u64 length;
  1986. u64 devid;
  1987. u8 uuid[BTRFS_UUID_SIZE];
  1988. int num_stripes;
  1989. int ret;
  1990. int i;
  1991. logical = key->offset;
  1992. length = btrfs_chunk_length(leaf, chunk);
  1993. spin_lock(&map_tree->map_tree.lock);
  1994. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  1995. spin_unlock(&map_tree->map_tree.lock);
  1996. /* already mapped? */
  1997. if (em && em->start <= logical && em->start + em->len > logical) {
  1998. free_extent_map(em);
  1999. return 0;
  2000. } else if (em) {
  2001. free_extent_map(em);
  2002. }
  2003. map = kzalloc(sizeof(*map), GFP_NOFS);
  2004. if (!map)
  2005. return -ENOMEM;
  2006. em = alloc_extent_map(GFP_NOFS);
  2007. if (!em)
  2008. return -ENOMEM;
  2009. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  2010. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  2011. if (!map) {
  2012. free_extent_map(em);
  2013. return -ENOMEM;
  2014. }
  2015. em->bdev = (struct block_device *)map;
  2016. em->start = logical;
  2017. em->len = length;
  2018. em->block_start = 0;
  2019. em->block_len = em->len;
  2020. map->num_stripes = num_stripes;
  2021. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  2022. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  2023. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  2024. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  2025. map->type = btrfs_chunk_type(leaf, chunk);
  2026. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  2027. for (i = 0; i < num_stripes; i++) {
  2028. map->stripes[i].physical =
  2029. btrfs_stripe_offset_nr(leaf, chunk, i);
  2030. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  2031. read_extent_buffer(leaf, uuid, (unsigned long)
  2032. btrfs_stripe_dev_uuid_nr(chunk, i),
  2033. BTRFS_UUID_SIZE);
  2034. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  2035. if (!map->stripes[i].dev && !btrfs_test_opt(root, DEGRADED)) {
  2036. kfree(map);
  2037. free_extent_map(em);
  2038. return -EIO;
  2039. }
  2040. if (!map->stripes[i].dev) {
  2041. map->stripes[i].dev =
  2042. add_missing_dev(root, devid, uuid);
  2043. if (!map->stripes[i].dev) {
  2044. kfree(map);
  2045. free_extent_map(em);
  2046. return -EIO;
  2047. }
  2048. }
  2049. map->stripes[i].dev->in_fs_metadata = 1;
  2050. }
  2051. spin_lock(&map_tree->map_tree.lock);
  2052. ret = add_extent_mapping(&map_tree->map_tree, em);
  2053. spin_unlock(&map_tree->map_tree.lock);
  2054. BUG_ON(ret);
  2055. free_extent_map(em);
  2056. return 0;
  2057. }
  2058. static int fill_device_from_item(struct extent_buffer *leaf,
  2059. struct btrfs_dev_item *dev_item,
  2060. struct btrfs_device *device)
  2061. {
  2062. unsigned long ptr;
  2063. device->devid = btrfs_device_id(leaf, dev_item);
  2064. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  2065. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  2066. device->type = btrfs_device_type(leaf, dev_item);
  2067. device->io_align = btrfs_device_io_align(leaf, dev_item);
  2068. device->io_width = btrfs_device_io_width(leaf, dev_item);
  2069. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  2070. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  2071. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  2072. return 0;
  2073. }
  2074. static int read_one_dev(struct btrfs_root *root,
  2075. struct extent_buffer *leaf,
  2076. struct btrfs_dev_item *dev_item)
  2077. {
  2078. struct btrfs_device *device;
  2079. u64 devid;
  2080. int ret;
  2081. u8 dev_uuid[BTRFS_UUID_SIZE];
  2082. devid = btrfs_device_id(leaf, dev_item);
  2083. read_extent_buffer(leaf, dev_uuid,
  2084. (unsigned long)btrfs_device_uuid(dev_item),
  2085. BTRFS_UUID_SIZE);
  2086. device = btrfs_find_device(root, devid, dev_uuid);
  2087. if (!device) {
  2088. printk("warning devid %Lu missing\n", devid);
  2089. device = add_missing_dev(root, devid, dev_uuid);
  2090. if (!device)
  2091. return -ENOMEM;
  2092. }
  2093. fill_device_from_item(leaf, dev_item, device);
  2094. device->dev_root = root->fs_info->dev_root;
  2095. device->in_fs_metadata = 1;
  2096. ret = 0;
  2097. #if 0
  2098. ret = btrfs_open_device(device);
  2099. if (ret) {
  2100. kfree(device);
  2101. }
  2102. #endif
  2103. return ret;
  2104. }
  2105. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  2106. {
  2107. struct btrfs_dev_item *dev_item;
  2108. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  2109. dev_item);
  2110. return read_one_dev(root, buf, dev_item);
  2111. }
  2112. int btrfs_read_sys_array(struct btrfs_root *root)
  2113. {
  2114. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  2115. struct extent_buffer *sb;
  2116. struct btrfs_disk_key *disk_key;
  2117. struct btrfs_chunk *chunk;
  2118. u8 *ptr;
  2119. unsigned long sb_ptr;
  2120. int ret = 0;
  2121. u32 num_stripes;
  2122. u32 array_size;
  2123. u32 len = 0;
  2124. u32 cur;
  2125. struct btrfs_key key;
  2126. sb = btrfs_find_create_tree_block(root, BTRFS_SUPER_INFO_OFFSET,
  2127. BTRFS_SUPER_INFO_SIZE);
  2128. if (!sb)
  2129. return -ENOMEM;
  2130. btrfs_set_buffer_uptodate(sb);
  2131. write_extent_buffer(sb, super_copy, 0, BTRFS_SUPER_INFO_SIZE);
  2132. array_size = btrfs_super_sys_array_size(super_copy);
  2133. ptr = super_copy->sys_chunk_array;
  2134. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  2135. cur = 0;
  2136. while (cur < array_size) {
  2137. disk_key = (struct btrfs_disk_key *)ptr;
  2138. btrfs_disk_key_to_cpu(&key, disk_key);
  2139. len = sizeof(*disk_key); ptr += len;
  2140. sb_ptr += len;
  2141. cur += len;
  2142. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  2143. chunk = (struct btrfs_chunk *)sb_ptr;
  2144. ret = read_one_chunk(root, &key, sb, chunk);
  2145. if (ret)
  2146. break;
  2147. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  2148. len = btrfs_chunk_item_size(num_stripes);
  2149. } else {
  2150. ret = -EIO;
  2151. break;
  2152. }
  2153. ptr += len;
  2154. sb_ptr += len;
  2155. cur += len;
  2156. }
  2157. free_extent_buffer(sb);
  2158. return ret;
  2159. }
  2160. int btrfs_read_chunk_tree(struct btrfs_root *root)
  2161. {
  2162. struct btrfs_path *path;
  2163. struct extent_buffer *leaf;
  2164. struct btrfs_key key;
  2165. struct btrfs_key found_key;
  2166. int ret;
  2167. int slot;
  2168. root = root->fs_info->chunk_root;
  2169. path = btrfs_alloc_path();
  2170. if (!path)
  2171. return -ENOMEM;
  2172. /* first we search for all of the device items, and then we
  2173. * read in all of the chunk items. This way we can create chunk
  2174. * mappings that reference all of the devices that are afound
  2175. */
  2176. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  2177. key.offset = 0;
  2178. key.type = 0;
  2179. again:
  2180. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  2181. while(1) {
  2182. leaf = path->nodes[0];
  2183. slot = path->slots[0];
  2184. if (slot >= btrfs_header_nritems(leaf)) {
  2185. ret = btrfs_next_leaf(root, path);
  2186. if (ret == 0)
  2187. continue;
  2188. if (ret < 0)
  2189. goto error;
  2190. break;
  2191. }
  2192. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  2193. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2194. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  2195. break;
  2196. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  2197. struct btrfs_dev_item *dev_item;
  2198. dev_item = btrfs_item_ptr(leaf, slot,
  2199. struct btrfs_dev_item);
  2200. ret = read_one_dev(root, leaf, dev_item);
  2201. BUG_ON(ret);
  2202. }
  2203. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  2204. struct btrfs_chunk *chunk;
  2205. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  2206. ret = read_one_chunk(root, &found_key, leaf, chunk);
  2207. }
  2208. path->slots[0]++;
  2209. }
  2210. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  2211. key.objectid = 0;
  2212. btrfs_release_path(root, path);
  2213. goto again;
  2214. }
  2215. btrfs_free_path(path);
  2216. ret = 0;
  2217. error:
  2218. return ret;
  2219. }