volumes.c 63 KB

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