volumes.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957
  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. struct map_lookup {
  31. u64 type;
  32. int io_align;
  33. int io_width;
  34. int stripe_len;
  35. int sector_size;
  36. int num_stripes;
  37. int sub_stripes;
  38. struct btrfs_bio_stripe stripes[];
  39. };
  40. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  41. (sizeof(struct btrfs_bio_stripe) * (n)))
  42. static DEFINE_MUTEX(uuid_mutex);
  43. static LIST_HEAD(fs_uuids);
  44. int btrfs_cleanup_fs_uuids(void)
  45. {
  46. struct btrfs_fs_devices *fs_devices;
  47. struct list_head *uuid_cur;
  48. struct list_head *devices_cur;
  49. struct btrfs_device *dev;
  50. list_for_each(uuid_cur, &fs_uuids) {
  51. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  52. list);
  53. while(!list_empty(&fs_devices->devices)) {
  54. devices_cur = fs_devices->devices.next;
  55. dev = list_entry(devices_cur, struct btrfs_device,
  56. dev_list);
  57. if (dev->bdev) {
  58. close_bdev_excl(dev->bdev);
  59. }
  60. list_del(&dev->dev_list);
  61. kfree(dev);
  62. }
  63. }
  64. return 0;
  65. }
  66. static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
  67. u8 *uuid)
  68. {
  69. struct btrfs_device *dev;
  70. struct list_head *cur;
  71. list_for_each(cur, head) {
  72. dev = list_entry(cur, struct btrfs_device, dev_list);
  73. if (dev->devid == devid &&
  74. (!uuid || !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE))) {
  75. return dev;
  76. }
  77. }
  78. return NULL;
  79. }
  80. static struct btrfs_fs_devices *find_fsid(u8 *fsid)
  81. {
  82. struct list_head *cur;
  83. struct btrfs_fs_devices *fs_devices;
  84. list_for_each(cur, &fs_uuids) {
  85. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  86. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  87. return fs_devices;
  88. }
  89. return NULL;
  90. }
  91. static int device_list_add(const char *path,
  92. struct btrfs_super_block *disk_super,
  93. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  94. {
  95. struct btrfs_device *device;
  96. struct btrfs_fs_devices *fs_devices;
  97. u64 found_transid = btrfs_super_generation(disk_super);
  98. fs_devices = find_fsid(disk_super->fsid);
  99. if (!fs_devices) {
  100. fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
  101. if (!fs_devices)
  102. return -ENOMEM;
  103. INIT_LIST_HEAD(&fs_devices->devices);
  104. INIT_LIST_HEAD(&fs_devices->alloc_list);
  105. list_add(&fs_devices->list, &fs_uuids);
  106. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  107. fs_devices->latest_devid = devid;
  108. fs_devices->latest_trans = found_transid;
  109. fs_devices->lowest_devid = (u64)-1;
  110. fs_devices->num_devices = 0;
  111. device = NULL;
  112. } else {
  113. device = __find_device(&fs_devices->devices, devid,
  114. disk_super->dev_item.uuid);
  115. }
  116. if (!device) {
  117. device = kzalloc(sizeof(*device), GFP_NOFS);
  118. if (!device) {
  119. /* we can safely leave the fs_devices entry around */
  120. return -ENOMEM;
  121. }
  122. device->devid = devid;
  123. memcpy(device->uuid, disk_super->dev_item.uuid,
  124. BTRFS_UUID_SIZE);
  125. device->barriers = 1;
  126. spin_lock_init(&device->io_lock);
  127. device->name = kstrdup(path, GFP_NOFS);
  128. if (!device->name) {
  129. kfree(device);
  130. return -ENOMEM;
  131. }
  132. list_add(&device->dev_list, &fs_devices->devices);
  133. list_add(&device->dev_alloc_list, &fs_devices->alloc_list);
  134. fs_devices->num_devices++;
  135. }
  136. if (found_transid > fs_devices->latest_trans) {
  137. fs_devices->latest_devid = devid;
  138. fs_devices->latest_trans = found_transid;
  139. }
  140. if (fs_devices->lowest_devid > devid) {
  141. fs_devices->lowest_devid = devid;
  142. }
  143. *fs_devices_ret = fs_devices;
  144. return 0;
  145. }
  146. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  147. {
  148. struct list_head *head = &fs_devices->devices;
  149. struct list_head *cur;
  150. struct btrfs_device *device;
  151. mutex_lock(&uuid_mutex);
  152. list_for_each(cur, head) {
  153. device = list_entry(cur, struct btrfs_device, dev_list);
  154. if (device->bdev) {
  155. close_bdev_excl(device->bdev);
  156. }
  157. device->bdev = NULL;
  158. }
  159. mutex_unlock(&uuid_mutex);
  160. return 0;
  161. }
  162. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  163. int flags, void *holder)
  164. {
  165. struct block_device *bdev;
  166. struct list_head *head = &fs_devices->devices;
  167. struct list_head *cur;
  168. struct btrfs_device *device;
  169. int ret;
  170. mutex_lock(&uuid_mutex);
  171. list_for_each(cur, head) {
  172. device = list_entry(cur, struct btrfs_device, dev_list);
  173. bdev = open_bdev_excl(device->name, flags, holder);
  174. if (IS_ERR(bdev)) {
  175. printk("open %s failed\n", device->name);
  176. ret = PTR_ERR(bdev);
  177. goto fail;
  178. }
  179. if (device->devid == fs_devices->latest_devid)
  180. fs_devices->latest_bdev = bdev;
  181. if (device->devid == fs_devices->lowest_devid) {
  182. fs_devices->lowest_bdev = bdev;
  183. }
  184. device->bdev = bdev;
  185. }
  186. mutex_unlock(&uuid_mutex);
  187. return 0;
  188. fail:
  189. mutex_unlock(&uuid_mutex);
  190. btrfs_close_devices(fs_devices);
  191. return ret;
  192. }
  193. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  194. struct btrfs_fs_devices **fs_devices_ret)
  195. {
  196. struct btrfs_super_block *disk_super;
  197. struct block_device *bdev;
  198. struct buffer_head *bh;
  199. int ret;
  200. u64 devid;
  201. u64 transid;
  202. mutex_lock(&uuid_mutex);
  203. bdev = open_bdev_excl(path, flags, holder);
  204. if (IS_ERR(bdev)) {
  205. ret = PTR_ERR(bdev);
  206. goto error;
  207. }
  208. ret = set_blocksize(bdev, 4096);
  209. if (ret)
  210. goto error_close;
  211. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  212. if (!bh) {
  213. ret = -EIO;
  214. goto error_close;
  215. }
  216. disk_super = (struct btrfs_super_block *)bh->b_data;
  217. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  218. sizeof(disk_super->magic))) {
  219. ret = -EINVAL;
  220. goto error_brelse;
  221. }
  222. devid = le64_to_cpu(disk_super->dev_item.devid);
  223. transid = btrfs_super_generation(disk_super);
  224. if (disk_super->label[0])
  225. printk("device label %s ", disk_super->label);
  226. else {
  227. /* FIXME, make a readl uuid parser */
  228. printk("device fsid %llx-%llx ",
  229. *(unsigned long long *)disk_super->fsid,
  230. *(unsigned long long *)(disk_super->fsid + 8));
  231. }
  232. printk("devid %Lu transid %Lu %s\n", devid, transid, path);
  233. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  234. error_brelse:
  235. brelse(bh);
  236. error_close:
  237. close_bdev_excl(bdev);
  238. error:
  239. mutex_unlock(&uuid_mutex);
  240. return ret;
  241. }
  242. /*
  243. * this uses a pretty simple search, the expectation is that it is
  244. * called very infrequently and that a given device has a small number
  245. * of extents
  246. */
  247. static int find_free_dev_extent(struct btrfs_trans_handle *trans,
  248. struct btrfs_device *device,
  249. struct btrfs_path *path,
  250. u64 num_bytes, u64 *start)
  251. {
  252. struct btrfs_key key;
  253. struct btrfs_root *root = device->dev_root;
  254. struct btrfs_dev_extent *dev_extent = NULL;
  255. u64 hole_size = 0;
  256. u64 last_byte = 0;
  257. u64 search_start = 0;
  258. u64 search_end = device->total_bytes;
  259. int ret;
  260. int slot = 0;
  261. int start_found;
  262. struct extent_buffer *l;
  263. start_found = 0;
  264. path->reada = 2;
  265. /* FIXME use last free of some kind */
  266. /* we don't want to overwrite the superblock on the drive,
  267. * so we make sure to start at an offset of at least 1MB
  268. */
  269. search_start = max((u64)1024 * 1024, search_start);
  270. if (root->fs_info->alloc_start + num_bytes <= device->total_bytes)
  271. search_start = max(root->fs_info->alloc_start, search_start);
  272. key.objectid = device->devid;
  273. key.offset = search_start;
  274. key.type = BTRFS_DEV_EXTENT_KEY;
  275. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  276. if (ret < 0)
  277. goto error;
  278. ret = btrfs_previous_item(root, path, 0, key.type);
  279. if (ret < 0)
  280. goto error;
  281. l = path->nodes[0];
  282. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  283. while (1) {
  284. l = path->nodes[0];
  285. slot = path->slots[0];
  286. if (slot >= btrfs_header_nritems(l)) {
  287. ret = btrfs_next_leaf(root, path);
  288. if (ret == 0)
  289. continue;
  290. if (ret < 0)
  291. goto error;
  292. no_more_items:
  293. if (!start_found) {
  294. if (search_start >= search_end) {
  295. ret = -ENOSPC;
  296. goto error;
  297. }
  298. *start = search_start;
  299. start_found = 1;
  300. goto check_pending;
  301. }
  302. *start = last_byte > search_start ?
  303. last_byte : search_start;
  304. if (search_end <= *start) {
  305. ret = -ENOSPC;
  306. goto error;
  307. }
  308. goto check_pending;
  309. }
  310. btrfs_item_key_to_cpu(l, &key, slot);
  311. if (key.objectid < device->devid)
  312. goto next;
  313. if (key.objectid > device->devid)
  314. goto no_more_items;
  315. if (key.offset >= search_start && key.offset > last_byte &&
  316. start_found) {
  317. if (last_byte < search_start)
  318. last_byte = search_start;
  319. hole_size = key.offset - last_byte;
  320. if (key.offset > last_byte &&
  321. hole_size >= num_bytes) {
  322. *start = last_byte;
  323. goto check_pending;
  324. }
  325. }
  326. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  327. goto next;
  328. }
  329. start_found = 1;
  330. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  331. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  332. next:
  333. path->slots[0]++;
  334. cond_resched();
  335. }
  336. check_pending:
  337. /* we have to make sure we didn't find an extent that has already
  338. * been allocated by the map tree or the original allocation
  339. */
  340. btrfs_release_path(root, path);
  341. BUG_ON(*start < search_start);
  342. if (*start + num_bytes > search_end) {
  343. ret = -ENOSPC;
  344. goto error;
  345. }
  346. /* check for pending inserts here */
  347. return 0;
  348. error:
  349. btrfs_release_path(root, path);
  350. return ret;
  351. }
  352. int btrfs_free_dev_extent(struct btrfs_trans_handle *trans,
  353. struct btrfs_device *device,
  354. u64 start)
  355. {
  356. int ret;
  357. struct btrfs_path *path;
  358. struct btrfs_root *root = device->dev_root;
  359. struct btrfs_key key;
  360. path = btrfs_alloc_path();
  361. if (!path)
  362. return -ENOMEM;
  363. key.objectid = device->devid;
  364. key.offset = start;
  365. key.type = BTRFS_DEV_EXTENT_KEY;
  366. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  367. BUG_ON(ret);
  368. ret = btrfs_del_item(trans, root, path);
  369. BUG_ON(ret);
  370. btrfs_free_path(path);
  371. return ret;
  372. }
  373. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  374. struct btrfs_device *device,
  375. u64 chunk_tree, u64 chunk_objectid,
  376. u64 chunk_offset,
  377. u64 num_bytes, u64 *start)
  378. {
  379. int ret;
  380. struct btrfs_path *path;
  381. struct btrfs_root *root = device->dev_root;
  382. struct btrfs_dev_extent *extent;
  383. struct extent_buffer *leaf;
  384. struct btrfs_key key;
  385. path = btrfs_alloc_path();
  386. if (!path)
  387. return -ENOMEM;
  388. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  389. if (ret) {
  390. goto err;
  391. }
  392. key.objectid = device->devid;
  393. key.offset = *start;
  394. key.type = BTRFS_DEV_EXTENT_KEY;
  395. ret = btrfs_insert_empty_item(trans, root, path, &key,
  396. sizeof(*extent));
  397. BUG_ON(ret);
  398. leaf = path->nodes[0];
  399. extent = btrfs_item_ptr(leaf, path->slots[0],
  400. struct btrfs_dev_extent);
  401. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  402. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  403. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  404. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  405. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  406. BTRFS_UUID_SIZE);
  407. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  408. btrfs_mark_buffer_dirty(leaf);
  409. err:
  410. btrfs_free_path(path);
  411. return ret;
  412. }
  413. static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
  414. {
  415. struct btrfs_path *path;
  416. int ret;
  417. struct btrfs_key key;
  418. struct btrfs_chunk *chunk;
  419. struct btrfs_key found_key;
  420. path = btrfs_alloc_path();
  421. BUG_ON(!path);
  422. key.objectid = objectid;
  423. key.offset = (u64)-1;
  424. key.type = BTRFS_CHUNK_ITEM_KEY;
  425. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  426. if (ret < 0)
  427. goto error;
  428. BUG_ON(ret == 0);
  429. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  430. if (ret) {
  431. *offset = 0;
  432. } else {
  433. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  434. path->slots[0]);
  435. if (found_key.objectid != objectid)
  436. *offset = 0;
  437. else {
  438. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  439. struct btrfs_chunk);
  440. *offset = found_key.offset +
  441. btrfs_chunk_length(path->nodes[0], chunk);
  442. }
  443. }
  444. ret = 0;
  445. error:
  446. btrfs_free_path(path);
  447. return ret;
  448. }
  449. static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
  450. u64 *objectid)
  451. {
  452. int ret;
  453. struct btrfs_key key;
  454. struct btrfs_key found_key;
  455. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  456. key.type = BTRFS_DEV_ITEM_KEY;
  457. key.offset = (u64)-1;
  458. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  459. if (ret < 0)
  460. goto error;
  461. BUG_ON(ret == 0);
  462. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  463. BTRFS_DEV_ITEM_KEY);
  464. if (ret) {
  465. *objectid = 1;
  466. } else {
  467. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  468. path->slots[0]);
  469. *objectid = found_key.offset + 1;
  470. }
  471. ret = 0;
  472. error:
  473. btrfs_release_path(root, path);
  474. return ret;
  475. }
  476. /*
  477. * the device information is stored in the chunk root
  478. * the btrfs_device struct should be fully filled in
  479. */
  480. int btrfs_add_device(struct btrfs_trans_handle *trans,
  481. struct btrfs_root *root,
  482. struct btrfs_device *device)
  483. {
  484. int ret;
  485. struct btrfs_path *path;
  486. struct btrfs_dev_item *dev_item;
  487. struct extent_buffer *leaf;
  488. struct btrfs_key key;
  489. unsigned long ptr;
  490. u64 free_devid;
  491. root = root->fs_info->chunk_root;
  492. path = btrfs_alloc_path();
  493. if (!path)
  494. return -ENOMEM;
  495. ret = find_next_devid(root, path, &free_devid);
  496. if (ret)
  497. goto out;
  498. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  499. key.type = BTRFS_DEV_ITEM_KEY;
  500. key.offset = free_devid;
  501. ret = btrfs_insert_empty_item(trans, root, path, &key,
  502. sizeof(*dev_item));
  503. if (ret)
  504. goto out;
  505. leaf = path->nodes[0];
  506. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  507. device->devid = free_devid;
  508. btrfs_set_device_id(leaf, dev_item, device->devid);
  509. btrfs_set_device_type(leaf, dev_item, device->type);
  510. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  511. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  512. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  513. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  514. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  515. btrfs_set_device_group(leaf, dev_item, 0);
  516. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  517. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  518. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  519. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  520. btrfs_mark_buffer_dirty(leaf);
  521. ret = 0;
  522. out:
  523. btrfs_free_path(path);
  524. return ret;
  525. }
  526. int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
  527. {
  528. struct btrfs_trans_handle *trans;
  529. struct btrfs_device *device;
  530. struct block_device *bdev;
  531. struct list_head *cur;
  532. struct list_head *devices;
  533. u64 total_bytes;
  534. int ret = 0;
  535. bdev = open_bdev_excl(device_path, 0, root->fs_info->bdev_holder);
  536. if (!bdev) {
  537. return -EIO;
  538. }
  539. mutex_lock(&root->fs_info->fs_mutex);
  540. trans = btrfs_start_transaction(root, 1);
  541. devices = &root->fs_info->fs_devices->devices;
  542. list_for_each(cur, devices) {
  543. device = list_entry(cur, struct btrfs_device, dev_list);
  544. if (device->bdev == bdev) {
  545. ret = -EEXIST;
  546. goto out;
  547. }
  548. }
  549. device = kzalloc(sizeof(*device), GFP_NOFS);
  550. if (!device) {
  551. /* we can safely leave the fs_devices entry around */
  552. ret = -ENOMEM;
  553. goto out_close_bdev;
  554. }
  555. device->barriers = 1;
  556. generate_random_uuid(device->uuid);
  557. spin_lock_init(&device->io_lock);
  558. device->name = kstrdup(device_path, GFP_NOFS);
  559. if (!device->name) {
  560. kfree(device);
  561. goto out_close_bdev;
  562. }
  563. device->io_width = root->sectorsize;
  564. device->io_align = root->sectorsize;
  565. device->sector_size = root->sectorsize;
  566. device->total_bytes = i_size_read(bdev->bd_inode);
  567. device->dev_root = root->fs_info->dev_root;
  568. device->bdev = bdev;
  569. ret = btrfs_add_device(trans, root, device);
  570. if (ret)
  571. goto out_close_bdev;
  572. total_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
  573. btrfs_set_super_total_bytes(&root->fs_info->super_copy,
  574. total_bytes + device->total_bytes);
  575. total_bytes = btrfs_super_num_devices(&root->fs_info->super_copy);
  576. btrfs_set_super_num_devices(&root->fs_info->super_copy,
  577. total_bytes + 1);
  578. list_add(&device->dev_list, &root->fs_info->fs_devices->devices);
  579. list_add(&device->dev_alloc_list,
  580. &root->fs_info->fs_devices->alloc_list);
  581. root->fs_info->fs_devices->num_devices++;
  582. out:
  583. btrfs_end_transaction(trans, root);
  584. mutex_unlock(&root->fs_info->fs_mutex);
  585. return ret;
  586. out_close_bdev:
  587. close_bdev_excl(bdev);
  588. goto out;
  589. }
  590. int btrfs_update_device(struct btrfs_trans_handle *trans,
  591. struct btrfs_device *device)
  592. {
  593. int ret;
  594. struct btrfs_path *path;
  595. struct btrfs_root *root;
  596. struct btrfs_dev_item *dev_item;
  597. struct extent_buffer *leaf;
  598. struct btrfs_key key;
  599. root = device->dev_root->fs_info->chunk_root;
  600. path = btrfs_alloc_path();
  601. if (!path)
  602. return -ENOMEM;
  603. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  604. key.type = BTRFS_DEV_ITEM_KEY;
  605. key.offset = device->devid;
  606. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  607. if (ret < 0)
  608. goto out;
  609. if (ret > 0) {
  610. ret = -ENOENT;
  611. goto out;
  612. }
  613. leaf = path->nodes[0];
  614. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  615. btrfs_set_device_id(leaf, dev_item, device->devid);
  616. btrfs_set_device_type(leaf, dev_item, device->type);
  617. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  618. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  619. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  620. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  621. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  622. btrfs_mark_buffer_dirty(leaf);
  623. out:
  624. btrfs_free_path(path);
  625. return ret;
  626. }
  627. int btrfs_grow_device(struct btrfs_trans_handle *trans,
  628. struct btrfs_device *device, u64 new_size)
  629. {
  630. struct btrfs_super_block *super_copy =
  631. &device->dev_root->fs_info->super_copy;
  632. u64 old_total = btrfs_super_total_bytes(super_copy);
  633. u64 diff = new_size - device->total_bytes;
  634. btrfs_set_super_total_bytes(super_copy, old_total + diff);
  635. return btrfs_update_device(trans, device);
  636. }
  637. static int btrfs_free_chunk(struct btrfs_trans_handle *trans,
  638. struct btrfs_root *root,
  639. u64 chunk_tree, u64 chunk_objectid,
  640. u64 chunk_offset)
  641. {
  642. int ret;
  643. struct btrfs_path *path;
  644. struct btrfs_key key;
  645. root = root->fs_info->chunk_root;
  646. path = btrfs_alloc_path();
  647. if (!path)
  648. return -ENOMEM;
  649. key.objectid = chunk_objectid;
  650. key.offset = chunk_offset;
  651. key.type = BTRFS_CHUNK_ITEM_KEY;
  652. ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
  653. BUG_ON(ret);
  654. ret = btrfs_del_item(trans, root, path);
  655. BUG_ON(ret);
  656. btrfs_free_path(path);
  657. return 0;
  658. }
  659. int btrfs_del_sys_chunk(struct btrfs_root *root, u64 chunk_objectid, u64
  660. chunk_offset)
  661. {
  662. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  663. struct btrfs_disk_key *disk_key;
  664. struct btrfs_chunk *chunk;
  665. u8 *ptr;
  666. int ret = 0;
  667. u32 num_stripes;
  668. u32 array_size;
  669. u32 len = 0;
  670. u32 cur;
  671. struct btrfs_key key;
  672. array_size = btrfs_super_sys_array_size(super_copy);
  673. ptr = super_copy->sys_chunk_array;
  674. cur = 0;
  675. while (cur < array_size) {
  676. disk_key = (struct btrfs_disk_key *)ptr;
  677. btrfs_disk_key_to_cpu(&key, disk_key);
  678. len = sizeof(*disk_key);
  679. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  680. chunk = (struct btrfs_chunk *)(ptr + len);
  681. num_stripes = btrfs_stack_chunk_num_stripes(chunk);
  682. len += btrfs_chunk_item_size(num_stripes);
  683. } else {
  684. ret = -EIO;
  685. break;
  686. }
  687. if (key.objectid == chunk_objectid &&
  688. key.offset == chunk_offset) {
  689. memmove(ptr, ptr + len, array_size - (cur + len));
  690. array_size -= len;
  691. btrfs_set_super_sys_array_size(super_copy, array_size);
  692. } else {
  693. ptr += len;
  694. cur += len;
  695. }
  696. }
  697. return ret;
  698. }
  699. int btrfs_relocate_chunk(struct btrfs_root *root,
  700. u64 chunk_tree, u64 chunk_objectid,
  701. u64 chunk_offset)
  702. {
  703. struct extent_map_tree *em_tree;
  704. struct btrfs_root *extent_root;
  705. struct btrfs_trans_handle *trans;
  706. struct extent_map *em;
  707. struct map_lookup *map;
  708. int ret;
  709. int i;
  710. root = root->fs_info->chunk_root;
  711. extent_root = root->fs_info->extent_root;
  712. em_tree = &root->fs_info->mapping_tree.map_tree;
  713. /* step one, relocate all the extents inside this chunk */
  714. ret = btrfs_shrink_extent_tree(extent_root, chunk_offset);
  715. BUG_ON(ret);
  716. trans = btrfs_start_transaction(root, 1);
  717. BUG_ON(!trans);
  718. /*
  719. * step two, delete the device extents and the
  720. * chunk tree entries
  721. */
  722. spin_lock(&em_tree->lock);
  723. em = lookup_extent_mapping(em_tree, chunk_offset, 1);
  724. spin_unlock(&em_tree->lock);
  725. BUG_ON(em->start > chunk_offset || em->start + em->len < chunk_offset);
  726. map = (struct map_lookup *)em->bdev;
  727. for (i = 0; i < map->num_stripes; i++) {
  728. ret = btrfs_free_dev_extent(trans, map->stripes[i].dev,
  729. map->stripes[i].physical);
  730. BUG_ON(ret);
  731. }
  732. ret = btrfs_free_chunk(trans, root, chunk_tree, chunk_objectid,
  733. chunk_offset);
  734. BUG_ON(ret);
  735. if (map->type & BTRFS_BLOCK_GROUP_SYSTEM) {
  736. ret = btrfs_del_sys_chunk(root, chunk_objectid, chunk_offset);
  737. BUG_ON(ret);
  738. goto out;
  739. }
  740. spin_lock(&em_tree->lock);
  741. remove_extent_mapping(em_tree, em);
  742. kfree(map);
  743. em->bdev = NULL;
  744. /* once for the tree */
  745. free_extent_map(em);
  746. spin_unlock(&em_tree->lock);
  747. out:
  748. /* once for us */
  749. free_extent_map(em);
  750. btrfs_end_transaction(trans, root);
  751. return 0;
  752. }
  753. static u64 div_factor(u64 num, int factor)
  754. {
  755. if (factor == 10)
  756. return num;
  757. num *= factor;
  758. do_div(num, 10);
  759. return num;
  760. }
  761. int btrfs_balance(struct btrfs_root *dev_root)
  762. {
  763. int ret;
  764. struct list_head *cur;
  765. struct list_head *devices = &dev_root->fs_info->fs_devices->devices;
  766. struct btrfs_device *device;
  767. u64 old_size;
  768. u64 size_to_free;
  769. struct btrfs_path *path;
  770. struct btrfs_key key;
  771. struct btrfs_chunk *chunk;
  772. struct btrfs_root *chunk_root = dev_root->fs_info->chunk_root;
  773. struct btrfs_trans_handle *trans;
  774. struct btrfs_key found_key;
  775. dev_root = dev_root->fs_info->dev_root;
  776. mutex_lock(&dev_root->fs_info->fs_mutex);
  777. /* step one make some room on all the devices */
  778. list_for_each(cur, devices) {
  779. device = list_entry(cur, struct btrfs_device, dev_list);
  780. old_size = device->total_bytes;
  781. size_to_free = div_factor(old_size, 1);
  782. size_to_free = min(size_to_free, (u64)1 * 1024 * 1024);
  783. if (device->total_bytes - device->bytes_used > size_to_free)
  784. continue;
  785. ret = btrfs_shrink_device(device, old_size - size_to_free);
  786. BUG_ON(ret);
  787. trans = btrfs_start_transaction(dev_root, 1);
  788. BUG_ON(!trans);
  789. ret = btrfs_grow_device(trans, device, old_size);
  790. BUG_ON(ret);
  791. btrfs_end_transaction(trans, dev_root);
  792. }
  793. /* step two, relocate all the chunks */
  794. path = btrfs_alloc_path();
  795. BUG_ON(!path);
  796. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  797. key.offset = (u64)-1;
  798. key.type = BTRFS_CHUNK_ITEM_KEY;
  799. while(1) {
  800. ret = btrfs_search_slot(NULL, chunk_root, &key, path, 0, 0);
  801. if (ret < 0)
  802. goto error;
  803. /*
  804. * this shouldn't happen, it means the last relocate
  805. * failed
  806. */
  807. if (ret == 0)
  808. break;
  809. ret = btrfs_previous_item(chunk_root, path, 0,
  810. BTRFS_CHUNK_ITEM_KEY);
  811. if (ret) {
  812. break;
  813. }
  814. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  815. path->slots[0]);
  816. if (found_key.objectid != key.objectid)
  817. break;
  818. chunk = btrfs_item_ptr(path->nodes[0],
  819. path->slots[0],
  820. struct btrfs_chunk);
  821. key.offset = found_key.offset;
  822. /* chunk zero is special */
  823. if (key.offset == 0)
  824. break;
  825. ret = btrfs_relocate_chunk(chunk_root,
  826. chunk_root->root_key.objectid,
  827. found_key.objectid,
  828. found_key.offset);
  829. BUG_ON(ret);
  830. btrfs_release_path(chunk_root, path);
  831. }
  832. ret = 0;
  833. error:
  834. btrfs_free_path(path);
  835. mutex_unlock(&dev_root->fs_info->fs_mutex);
  836. return ret;
  837. }
  838. /*
  839. * shrinking a device means finding all of the device extents past
  840. * the new size, and then following the back refs to the chunks.
  841. * The chunk relocation code actually frees the device extent
  842. */
  843. int btrfs_shrink_device(struct btrfs_device *device, u64 new_size)
  844. {
  845. struct btrfs_trans_handle *trans;
  846. struct btrfs_root *root = device->dev_root;
  847. struct btrfs_dev_extent *dev_extent = NULL;
  848. struct btrfs_path *path;
  849. u64 length;
  850. u64 chunk_tree;
  851. u64 chunk_objectid;
  852. u64 chunk_offset;
  853. int ret;
  854. int slot;
  855. struct extent_buffer *l;
  856. struct btrfs_key key;
  857. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  858. u64 old_total = btrfs_super_total_bytes(super_copy);
  859. u64 diff = device->total_bytes - new_size;
  860. path = btrfs_alloc_path();
  861. if (!path)
  862. return -ENOMEM;
  863. trans = btrfs_start_transaction(root, 1);
  864. if (!trans) {
  865. ret = -ENOMEM;
  866. goto done;
  867. }
  868. path->reada = 2;
  869. device->total_bytes = new_size;
  870. ret = btrfs_update_device(trans, device);
  871. if (ret) {
  872. btrfs_end_transaction(trans, root);
  873. goto done;
  874. }
  875. WARN_ON(diff > old_total);
  876. btrfs_set_super_total_bytes(super_copy, old_total - diff);
  877. btrfs_end_transaction(trans, root);
  878. key.objectid = device->devid;
  879. key.offset = (u64)-1;
  880. key.type = BTRFS_DEV_EXTENT_KEY;
  881. while (1) {
  882. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  883. if (ret < 0)
  884. goto done;
  885. ret = btrfs_previous_item(root, path, 0, key.type);
  886. if (ret < 0)
  887. goto done;
  888. if (ret) {
  889. ret = 0;
  890. goto done;
  891. }
  892. l = path->nodes[0];
  893. slot = path->slots[0];
  894. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  895. if (key.objectid != device->devid)
  896. goto done;
  897. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  898. length = btrfs_dev_extent_length(l, dev_extent);
  899. if (key.offset + length <= new_size)
  900. goto done;
  901. chunk_tree = btrfs_dev_extent_chunk_tree(l, dev_extent);
  902. chunk_objectid = btrfs_dev_extent_chunk_objectid(l, dev_extent);
  903. chunk_offset = btrfs_dev_extent_chunk_offset(l, dev_extent);
  904. btrfs_release_path(root, path);
  905. ret = btrfs_relocate_chunk(root, chunk_tree, chunk_objectid,
  906. chunk_offset);
  907. if (ret)
  908. goto done;
  909. }
  910. done:
  911. btrfs_free_path(path);
  912. return ret;
  913. }
  914. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  915. struct btrfs_root *root,
  916. struct btrfs_key *key,
  917. struct btrfs_chunk *chunk, int item_size)
  918. {
  919. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  920. struct btrfs_disk_key disk_key;
  921. u32 array_size;
  922. u8 *ptr;
  923. array_size = btrfs_super_sys_array_size(super_copy);
  924. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  925. return -EFBIG;
  926. ptr = super_copy->sys_chunk_array + array_size;
  927. btrfs_cpu_key_to_disk(&disk_key, key);
  928. memcpy(ptr, &disk_key, sizeof(disk_key));
  929. ptr += sizeof(disk_key);
  930. memcpy(ptr, chunk, item_size);
  931. item_size += sizeof(disk_key);
  932. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  933. return 0;
  934. }
  935. static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
  936. int sub_stripes)
  937. {
  938. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  939. return calc_size;
  940. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  941. return calc_size * (num_stripes / sub_stripes);
  942. else
  943. return calc_size * num_stripes;
  944. }
  945. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  946. struct btrfs_root *extent_root, u64 *start,
  947. u64 *num_bytes, u64 type)
  948. {
  949. u64 dev_offset;
  950. struct btrfs_fs_info *info = extent_root->fs_info;
  951. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  952. struct btrfs_path *path;
  953. struct btrfs_stripe *stripes;
  954. struct btrfs_device *device = NULL;
  955. struct btrfs_chunk *chunk;
  956. struct list_head private_devs;
  957. struct list_head *dev_list;
  958. struct list_head *cur;
  959. struct extent_map_tree *em_tree;
  960. struct map_lookup *map;
  961. struct extent_map *em;
  962. int min_stripe_size = 1 * 1024 * 1024;
  963. u64 physical;
  964. u64 calc_size = 1024 * 1024 * 1024;
  965. u64 max_chunk_size = calc_size;
  966. u64 min_free;
  967. u64 avail;
  968. u64 max_avail = 0;
  969. u64 percent_max;
  970. int num_stripes = 1;
  971. int min_stripes = 1;
  972. int sub_stripes = 0;
  973. int looped = 0;
  974. int ret;
  975. int index;
  976. int stripe_len = 64 * 1024;
  977. struct btrfs_key key;
  978. if ((type & BTRFS_BLOCK_GROUP_RAID1) &&
  979. (type & BTRFS_BLOCK_GROUP_DUP)) {
  980. WARN_ON(1);
  981. type &= ~BTRFS_BLOCK_GROUP_DUP;
  982. }
  983. dev_list = &extent_root->fs_info->fs_devices->alloc_list;
  984. if (list_empty(dev_list))
  985. return -ENOSPC;
  986. if (type & (BTRFS_BLOCK_GROUP_RAID0)) {
  987. num_stripes = btrfs_super_num_devices(&info->super_copy);
  988. min_stripes = 2;
  989. }
  990. if (type & (BTRFS_BLOCK_GROUP_DUP)) {
  991. num_stripes = 2;
  992. min_stripes = 2;
  993. }
  994. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  995. num_stripes = min_t(u64, 2,
  996. btrfs_super_num_devices(&info->super_copy));
  997. if (num_stripes < 2)
  998. return -ENOSPC;
  999. min_stripes = 2;
  1000. }
  1001. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1002. num_stripes = btrfs_super_num_devices(&info->super_copy);
  1003. if (num_stripes < 4)
  1004. return -ENOSPC;
  1005. num_stripes &= ~(u32)1;
  1006. sub_stripes = 2;
  1007. min_stripes = 4;
  1008. }
  1009. if (type & BTRFS_BLOCK_GROUP_DATA) {
  1010. max_chunk_size = 10 * calc_size;
  1011. min_stripe_size = 64 * 1024 * 1024;
  1012. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  1013. max_chunk_size = 4 * calc_size;
  1014. min_stripe_size = 32 * 1024 * 1024;
  1015. } else if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1016. calc_size = 8 * 1024 * 1024;
  1017. max_chunk_size = calc_size * 2;
  1018. min_stripe_size = 1 * 1024 * 1024;
  1019. }
  1020. path = btrfs_alloc_path();
  1021. if (!path)
  1022. return -ENOMEM;
  1023. /* we don't want a chunk larger than 10% of the FS */
  1024. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  1025. max_chunk_size = min(percent_max, max_chunk_size);
  1026. again:
  1027. if (calc_size * num_stripes > max_chunk_size) {
  1028. calc_size = max_chunk_size;
  1029. do_div(calc_size, num_stripes);
  1030. do_div(calc_size, stripe_len);
  1031. calc_size *= stripe_len;
  1032. }
  1033. /* we don't want tiny stripes */
  1034. calc_size = max_t(u64, min_stripe_size, calc_size);
  1035. do_div(calc_size, stripe_len);
  1036. calc_size *= stripe_len;
  1037. INIT_LIST_HEAD(&private_devs);
  1038. cur = dev_list->next;
  1039. index = 0;
  1040. if (type & BTRFS_BLOCK_GROUP_DUP)
  1041. min_free = calc_size * 2;
  1042. else
  1043. min_free = calc_size;
  1044. /* we add 1MB because we never use the first 1MB of the device */
  1045. min_free += 1024 * 1024;
  1046. /* build a private list of devices we will allocate from */
  1047. while(index < num_stripes) {
  1048. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1049. avail = device->total_bytes - device->bytes_used;
  1050. cur = cur->next;
  1051. if (avail >= min_free) {
  1052. u64 ignored_start = 0;
  1053. ret = find_free_dev_extent(trans, device, path,
  1054. min_free,
  1055. &ignored_start);
  1056. if (ret == 0) {
  1057. list_move_tail(&device->dev_alloc_list,
  1058. &private_devs);
  1059. index++;
  1060. if (type & BTRFS_BLOCK_GROUP_DUP)
  1061. index++;
  1062. }
  1063. } else if (avail > max_avail)
  1064. max_avail = avail;
  1065. if (cur == dev_list)
  1066. break;
  1067. }
  1068. if (index < num_stripes) {
  1069. list_splice(&private_devs, dev_list);
  1070. if (index >= min_stripes) {
  1071. num_stripes = index;
  1072. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  1073. num_stripes /= sub_stripes;
  1074. num_stripes *= sub_stripes;
  1075. }
  1076. looped = 1;
  1077. goto again;
  1078. }
  1079. if (!looped && max_avail > 0) {
  1080. looped = 1;
  1081. calc_size = max_avail;
  1082. goto again;
  1083. }
  1084. btrfs_free_path(path);
  1085. return -ENOSPC;
  1086. }
  1087. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  1088. key.type = BTRFS_CHUNK_ITEM_KEY;
  1089. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  1090. &key.offset);
  1091. if (ret) {
  1092. btrfs_free_path(path);
  1093. return ret;
  1094. }
  1095. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  1096. if (!chunk) {
  1097. btrfs_free_path(path);
  1098. return -ENOMEM;
  1099. }
  1100. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1101. if (!map) {
  1102. kfree(chunk);
  1103. btrfs_free_path(path);
  1104. return -ENOMEM;
  1105. }
  1106. btrfs_free_path(path);
  1107. path = NULL;
  1108. stripes = &chunk->stripe;
  1109. *num_bytes = chunk_bytes_by_type(type, calc_size,
  1110. num_stripes, sub_stripes);
  1111. index = 0;
  1112. while(index < num_stripes) {
  1113. struct btrfs_stripe *stripe;
  1114. BUG_ON(list_empty(&private_devs));
  1115. cur = private_devs.next;
  1116. device = list_entry(cur, struct btrfs_device, dev_alloc_list);
  1117. /* loop over this device again if we're doing a dup group */
  1118. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  1119. (index == num_stripes - 1))
  1120. list_move_tail(&device->dev_alloc_list, dev_list);
  1121. ret = btrfs_alloc_dev_extent(trans, device,
  1122. info->chunk_root->root_key.objectid,
  1123. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  1124. calc_size, &dev_offset);
  1125. BUG_ON(ret);
  1126. device->bytes_used += calc_size;
  1127. ret = btrfs_update_device(trans, device);
  1128. BUG_ON(ret);
  1129. map->stripes[index].dev = device;
  1130. map->stripes[index].physical = dev_offset;
  1131. stripe = stripes + index;
  1132. btrfs_set_stack_stripe_devid(stripe, device->devid);
  1133. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  1134. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  1135. physical = dev_offset;
  1136. index++;
  1137. }
  1138. BUG_ON(!list_empty(&private_devs));
  1139. /* key was set above */
  1140. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  1141. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  1142. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  1143. btrfs_set_stack_chunk_type(chunk, type);
  1144. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  1145. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  1146. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  1147. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  1148. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  1149. map->sector_size = extent_root->sectorsize;
  1150. map->stripe_len = stripe_len;
  1151. map->io_align = stripe_len;
  1152. map->io_width = stripe_len;
  1153. map->type = type;
  1154. map->num_stripes = num_stripes;
  1155. map->sub_stripes = sub_stripes;
  1156. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  1157. btrfs_chunk_item_size(num_stripes));
  1158. BUG_ON(ret);
  1159. *start = key.offset;;
  1160. em = alloc_extent_map(GFP_NOFS);
  1161. if (!em)
  1162. return -ENOMEM;
  1163. em->bdev = (struct block_device *)map;
  1164. em->start = key.offset;
  1165. em->len = *num_bytes;
  1166. em->block_start = 0;
  1167. if (type & BTRFS_BLOCK_GROUP_SYSTEM) {
  1168. ret = btrfs_add_system_chunk(trans, chunk_root, &key,
  1169. chunk, btrfs_chunk_item_size(num_stripes));
  1170. BUG_ON(ret);
  1171. }
  1172. kfree(chunk);
  1173. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  1174. spin_lock(&em_tree->lock);
  1175. ret = add_extent_mapping(em_tree, em);
  1176. spin_unlock(&em_tree->lock);
  1177. BUG_ON(ret);
  1178. free_extent_map(em);
  1179. return ret;
  1180. }
  1181. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  1182. {
  1183. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  1184. }
  1185. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  1186. {
  1187. struct extent_map *em;
  1188. while(1) {
  1189. spin_lock(&tree->map_tree.lock);
  1190. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  1191. if (em)
  1192. remove_extent_mapping(&tree->map_tree, em);
  1193. spin_unlock(&tree->map_tree.lock);
  1194. if (!em)
  1195. break;
  1196. kfree(em->bdev);
  1197. /* once for us */
  1198. free_extent_map(em);
  1199. /* once for the tree */
  1200. free_extent_map(em);
  1201. }
  1202. }
  1203. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  1204. {
  1205. struct extent_map *em;
  1206. struct map_lookup *map;
  1207. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1208. int ret;
  1209. spin_lock(&em_tree->lock);
  1210. em = lookup_extent_mapping(em_tree, logical, len);
  1211. spin_unlock(&em_tree->lock);
  1212. BUG_ON(!em);
  1213. BUG_ON(em->start > logical || em->start + em->len < logical);
  1214. map = (struct map_lookup *)em->bdev;
  1215. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  1216. ret = map->num_stripes;
  1217. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  1218. ret = map->sub_stripes;
  1219. else
  1220. ret = 1;
  1221. free_extent_map(em);
  1222. return ret;
  1223. }
  1224. static int __btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1225. u64 logical, u64 *length,
  1226. struct btrfs_multi_bio **multi_ret,
  1227. int mirror_num, struct page *unplug_page)
  1228. {
  1229. struct extent_map *em;
  1230. struct map_lookup *map;
  1231. struct extent_map_tree *em_tree = &map_tree->map_tree;
  1232. u64 offset;
  1233. u64 stripe_offset;
  1234. u64 stripe_nr;
  1235. int stripes_allocated = 8;
  1236. int stripes_required = 1;
  1237. int stripe_index;
  1238. int i;
  1239. int num_stripes;
  1240. int max_errors = 0;
  1241. struct btrfs_multi_bio *multi = NULL;
  1242. if (multi_ret && !(rw & (1 << BIO_RW))) {
  1243. stripes_allocated = 1;
  1244. }
  1245. again:
  1246. if (multi_ret) {
  1247. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  1248. GFP_NOFS);
  1249. if (!multi)
  1250. return -ENOMEM;
  1251. atomic_set(&multi->error, 0);
  1252. }
  1253. spin_lock(&em_tree->lock);
  1254. em = lookup_extent_mapping(em_tree, logical, *length);
  1255. spin_unlock(&em_tree->lock);
  1256. if (!em && unplug_page)
  1257. return 0;
  1258. if (!em) {
  1259. printk("unable to find logical %Lu\n", logical);
  1260. BUG();
  1261. }
  1262. BUG_ON(em->start > logical || em->start + em->len < logical);
  1263. map = (struct map_lookup *)em->bdev;
  1264. offset = logical - em->start;
  1265. if (mirror_num > map->num_stripes)
  1266. mirror_num = 0;
  1267. /* if our multi bio struct is too small, back off and try again */
  1268. if (rw & (1 << BIO_RW)) {
  1269. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  1270. BTRFS_BLOCK_GROUP_DUP)) {
  1271. stripes_required = map->num_stripes;
  1272. max_errors = 1;
  1273. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1274. stripes_required = map->sub_stripes;
  1275. max_errors = 1;
  1276. }
  1277. }
  1278. if (multi_ret && rw == WRITE &&
  1279. stripes_allocated < stripes_required) {
  1280. stripes_allocated = map->num_stripes;
  1281. free_extent_map(em);
  1282. kfree(multi);
  1283. goto again;
  1284. }
  1285. stripe_nr = offset;
  1286. /*
  1287. * stripe_nr counts the total number of stripes we have to stride
  1288. * to get to this block
  1289. */
  1290. do_div(stripe_nr, map->stripe_len);
  1291. stripe_offset = stripe_nr * map->stripe_len;
  1292. BUG_ON(offset < stripe_offset);
  1293. /* stripe_offset is the offset of this block in its stripe*/
  1294. stripe_offset = offset - stripe_offset;
  1295. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  1296. BTRFS_BLOCK_GROUP_RAID10 |
  1297. BTRFS_BLOCK_GROUP_DUP)) {
  1298. /* we limit the length of each bio to what fits in a stripe */
  1299. *length = min_t(u64, em->len - offset,
  1300. map->stripe_len - stripe_offset);
  1301. } else {
  1302. *length = em->len - offset;
  1303. }
  1304. if (!multi_ret && !unplug_page)
  1305. goto out;
  1306. num_stripes = 1;
  1307. stripe_index = 0;
  1308. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  1309. if (unplug_page || (rw & (1 << BIO_RW)))
  1310. num_stripes = map->num_stripes;
  1311. else if (mirror_num)
  1312. stripe_index = mirror_num - 1;
  1313. else
  1314. stripe_index = current->pid % map->num_stripes;
  1315. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  1316. if (rw & (1 << BIO_RW))
  1317. num_stripes = map->num_stripes;
  1318. else if (mirror_num)
  1319. stripe_index = mirror_num - 1;
  1320. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  1321. int factor = map->num_stripes / map->sub_stripes;
  1322. stripe_index = do_div(stripe_nr, factor);
  1323. stripe_index *= map->sub_stripes;
  1324. if (unplug_page || (rw & (1 << BIO_RW)))
  1325. num_stripes = map->sub_stripes;
  1326. else if (mirror_num)
  1327. stripe_index += mirror_num - 1;
  1328. else
  1329. stripe_index += current->pid % map->sub_stripes;
  1330. } else {
  1331. /*
  1332. * after this do_div call, stripe_nr is the number of stripes
  1333. * on this device we have to walk to find the data, and
  1334. * stripe_index is the number of our device in the stripe array
  1335. */
  1336. stripe_index = do_div(stripe_nr, map->num_stripes);
  1337. }
  1338. BUG_ON(stripe_index >= map->num_stripes);
  1339. for (i = 0; i < num_stripes; i++) {
  1340. if (unplug_page) {
  1341. struct btrfs_device *device;
  1342. struct backing_dev_info *bdi;
  1343. device = map->stripes[stripe_index].dev;
  1344. bdi = blk_get_backing_dev_info(device->bdev);
  1345. if (bdi->unplug_io_fn) {
  1346. bdi->unplug_io_fn(bdi, unplug_page);
  1347. }
  1348. } else {
  1349. multi->stripes[i].physical =
  1350. map->stripes[stripe_index].physical +
  1351. stripe_offset + stripe_nr * map->stripe_len;
  1352. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  1353. }
  1354. stripe_index++;
  1355. }
  1356. if (multi_ret) {
  1357. *multi_ret = multi;
  1358. multi->num_stripes = num_stripes;
  1359. multi->max_errors = max_errors;
  1360. }
  1361. out:
  1362. free_extent_map(em);
  1363. return 0;
  1364. }
  1365. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  1366. u64 logical, u64 *length,
  1367. struct btrfs_multi_bio **multi_ret, int mirror_num)
  1368. {
  1369. return __btrfs_map_block(map_tree, rw, logical, length, multi_ret,
  1370. mirror_num, NULL);
  1371. }
  1372. int btrfs_unplug_page(struct btrfs_mapping_tree *map_tree,
  1373. u64 logical, struct page *page)
  1374. {
  1375. u64 length = PAGE_CACHE_SIZE;
  1376. return __btrfs_map_block(map_tree, READ, logical, &length,
  1377. NULL, 0, page);
  1378. }
  1379. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  1380. static void end_bio_multi_stripe(struct bio *bio, int err)
  1381. #else
  1382. static int end_bio_multi_stripe(struct bio *bio,
  1383. unsigned int bytes_done, int err)
  1384. #endif
  1385. {
  1386. struct btrfs_multi_bio *multi = bio->bi_private;
  1387. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1388. if (bio->bi_size)
  1389. return 1;
  1390. #endif
  1391. if (err)
  1392. atomic_inc(&multi->error);
  1393. if (atomic_dec_and_test(&multi->stripes_pending)) {
  1394. bio->bi_private = multi->private;
  1395. bio->bi_end_io = multi->end_io;
  1396. /* only send an error to the higher layers if it is
  1397. * beyond the tolerance of the multi-bio
  1398. */
  1399. if (atomic_read(&multi->error) > multi->max_errors)
  1400. err = -EIO;
  1401. else
  1402. err = 0;
  1403. kfree(multi);
  1404. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1405. bio_endio(bio, bio->bi_size, err);
  1406. #else
  1407. bio_endio(bio, err);
  1408. #endif
  1409. } else {
  1410. bio_put(bio);
  1411. }
  1412. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  1413. return 0;
  1414. #endif
  1415. }
  1416. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  1417. int mirror_num)
  1418. {
  1419. struct btrfs_mapping_tree *map_tree;
  1420. struct btrfs_device *dev;
  1421. struct bio *first_bio = bio;
  1422. u64 logical = bio->bi_sector << 9;
  1423. u64 length = 0;
  1424. u64 map_length;
  1425. struct btrfs_multi_bio *multi = NULL;
  1426. int ret;
  1427. int dev_nr = 0;
  1428. int total_devs = 1;
  1429. length = bio->bi_size;
  1430. map_tree = &root->fs_info->mapping_tree;
  1431. map_length = length;
  1432. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  1433. mirror_num);
  1434. BUG_ON(ret);
  1435. total_devs = multi->num_stripes;
  1436. if (map_length < length) {
  1437. printk("mapping failed logical %Lu bio len %Lu "
  1438. "len %Lu\n", logical, length, map_length);
  1439. BUG();
  1440. }
  1441. multi->end_io = first_bio->bi_end_io;
  1442. multi->private = first_bio->bi_private;
  1443. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1444. while(dev_nr < total_devs) {
  1445. if (total_devs > 1) {
  1446. if (dev_nr < total_devs - 1) {
  1447. bio = bio_clone(first_bio, GFP_NOFS);
  1448. BUG_ON(!bio);
  1449. } else {
  1450. bio = first_bio;
  1451. }
  1452. bio->bi_private = multi;
  1453. bio->bi_end_io = end_bio_multi_stripe;
  1454. }
  1455. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1456. dev = multi->stripes[dev_nr].dev;
  1457. bio->bi_bdev = dev->bdev;
  1458. spin_lock(&dev->io_lock);
  1459. dev->total_ios++;
  1460. spin_unlock(&dev->io_lock);
  1461. submit_bio(rw, bio);
  1462. dev_nr++;
  1463. }
  1464. if (total_devs == 1)
  1465. kfree(multi);
  1466. return 0;
  1467. }
  1468. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1469. u8 *uuid)
  1470. {
  1471. struct list_head *head = &root->fs_info->fs_devices->devices;
  1472. return __find_device(head, devid, uuid);
  1473. }
  1474. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1475. struct extent_buffer *leaf,
  1476. struct btrfs_chunk *chunk)
  1477. {
  1478. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1479. struct map_lookup *map;
  1480. struct extent_map *em;
  1481. u64 logical;
  1482. u64 length;
  1483. u64 devid;
  1484. u8 uuid[BTRFS_UUID_SIZE];
  1485. int num_stripes;
  1486. int ret;
  1487. int i;
  1488. logical = key->offset;
  1489. length = btrfs_chunk_length(leaf, chunk);
  1490. spin_lock(&map_tree->map_tree.lock);
  1491. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  1492. spin_unlock(&map_tree->map_tree.lock);
  1493. /* already mapped? */
  1494. if (em && em->start <= logical && em->start + em->len > logical) {
  1495. free_extent_map(em);
  1496. return 0;
  1497. } else if (em) {
  1498. free_extent_map(em);
  1499. }
  1500. map = kzalloc(sizeof(*map), GFP_NOFS);
  1501. if (!map)
  1502. return -ENOMEM;
  1503. em = alloc_extent_map(GFP_NOFS);
  1504. if (!em)
  1505. return -ENOMEM;
  1506. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1507. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1508. if (!map) {
  1509. free_extent_map(em);
  1510. return -ENOMEM;
  1511. }
  1512. em->bdev = (struct block_device *)map;
  1513. em->start = logical;
  1514. em->len = length;
  1515. em->block_start = 0;
  1516. map->num_stripes = num_stripes;
  1517. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  1518. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  1519. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  1520. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  1521. map->type = btrfs_chunk_type(leaf, chunk);
  1522. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  1523. for (i = 0; i < num_stripes; i++) {
  1524. map->stripes[i].physical =
  1525. btrfs_stripe_offset_nr(leaf, chunk, i);
  1526. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  1527. read_extent_buffer(leaf, uuid, (unsigned long)
  1528. btrfs_stripe_dev_uuid_nr(chunk, i),
  1529. BTRFS_UUID_SIZE);
  1530. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  1531. if (!map->stripes[i].dev) {
  1532. kfree(map);
  1533. free_extent_map(em);
  1534. return -EIO;
  1535. }
  1536. }
  1537. spin_lock(&map_tree->map_tree.lock);
  1538. ret = add_extent_mapping(&map_tree->map_tree, em);
  1539. spin_unlock(&map_tree->map_tree.lock);
  1540. BUG_ON(ret);
  1541. free_extent_map(em);
  1542. return 0;
  1543. }
  1544. static int fill_device_from_item(struct extent_buffer *leaf,
  1545. struct btrfs_dev_item *dev_item,
  1546. struct btrfs_device *device)
  1547. {
  1548. unsigned long ptr;
  1549. device->devid = btrfs_device_id(leaf, dev_item);
  1550. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  1551. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  1552. device->type = btrfs_device_type(leaf, dev_item);
  1553. device->io_align = btrfs_device_io_align(leaf, dev_item);
  1554. device->io_width = btrfs_device_io_width(leaf, dev_item);
  1555. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  1556. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1557. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1558. return 0;
  1559. }
  1560. static int read_one_dev(struct btrfs_root *root,
  1561. struct extent_buffer *leaf,
  1562. struct btrfs_dev_item *dev_item)
  1563. {
  1564. struct btrfs_device *device;
  1565. u64 devid;
  1566. int ret;
  1567. u8 dev_uuid[BTRFS_UUID_SIZE];
  1568. devid = btrfs_device_id(leaf, dev_item);
  1569. read_extent_buffer(leaf, dev_uuid,
  1570. (unsigned long)btrfs_device_uuid(dev_item),
  1571. BTRFS_UUID_SIZE);
  1572. device = btrfs_find_device(root, devid, dev_uuid);
  1573. if (!device) {
  1574. printk("warning devid %Lu not found already\n", devid);
  1575. device = kzalloc(sizeof(*device), GFP_NOFS);
  1576. if (!device)
  1577. return -ENOMEM;
  1578. list_add(&device->dev_list,
  1579. &root->fs_info->fs_devices->devices);
  1580. list_add(&device->dev_alloc_list,
  1581. &root->fs_info->fs_devices->alloc_list);
  1582. device->barriers = 1;
  1583. spin_lock_init(&device->io_lock);
  1584. }
  1585. fill_device_from_item(leaf, dev_item, device);
  1586. device->dev_root = root->fs_info->dev_root;
  1587. ret = 0;
  1588. #if 0
  1589. ret = btrfs_open_device(device);
  1590. if (ret) {
  1591. kfree(device);
  1592. }
  1593. #endif
  1594. return ret;
  1595. }
  1596. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  1597. {
  1598. struct btrfs_dev_item *dev_item;
  1599. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  1600. dev_item);
  1601. return read_one_dev(root, buf, dev_item);
  1602. }
  1603. int btrfs_read_sys_array(struct btrfs_root *root)
  1604. {
  1605. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1606. struct extent_buffer *sb = root->fs_info->sb_buffer;
  1607. struct btrfs_disk_key *disk_key;
  1608. struct btrfs_chunk *chunk;
  1609. u8 *ptr;
  1610. unsigned long sb_ptr;
  1611. int ret = 0;
  1612. u32 num_stripes;
  1613. u32 array_size;
  1614. u32 len = 0;
  1615. u32 cur;
  1616. struct btrfs_key key;
  1617. array_size = btrfs_super_sys_array_size(super_copy);
  1618. ptr = super_copy->sys_chunk_array;
  1619. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  1620. cur = 0;
  1621. while (cur < array_size) {
  1622. disk_key = (struct btrfs_disk_key *)ptr;
  1623. btrfs_disk_key_to_cpu(&key, disk_key);
  1624. len = sizeof(*disk_key);
  1625. ptr += len;
  1626. sb_ptr += len;
  1627. cur += len;
  1628. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1629. chunk = (struct btrfs_chunk *)sb_ptr;
  1630. ret = read_one_chunk(root, &key, sb, chunk);
  1631. if (ret)
  1632. break;
  1633. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  1634. len = btrfs_chunk_item_size(num_stripes);
  1635. } else {
  1636. ret = -EIO;
  1637. break;
  1638. }
  1639. ptr += len;
  1640. sb_ptr += len;
  1641. cur += len;
  1642. }
  1643. return ret;
  1644. }
  1645. int btrfs_read_chunk_tree(struct btrfs_root *root)
  1646. {
  1647. struct btrfs_path *path;
  1648. struct extent_buffer *leaf;
  1649. struct btrfs_key key;
  1650. struct btrfs_key found_key;
  1651. int ret;
  1652. int slot;
  1653. root = root->fs_info->chunk_root;
  1654. path = btrfs_alloc_path();
  1655. if (!path)
  1656. return -ENOMEM;
  1657. /* first we search for all of the device items, and then we
  1658. * read in all of the chunk items. This way we can create chunk
  1659. * mappings that reference all of the devices that are afound
  1660. */
  1661. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1662. key.offset = 0;
  1663. key.type = 0;
  1664. again:
  1665. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1666. while(1) {
  1667. leaf = path->nodes[0];
  1668. slot = path->slots[0];
  1669. if (slot >= btrfs_header_nritems(leaf)) {
  1670. ret = btrfs_next_leaf(root, path);
  1671. if (ret == 0)
  1672. continue;
  1673. if (ret < 0)
  1674. goto error;
  1675. break;
  1676. }
  1677. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1678. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1679. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  1680. break;
  1681. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  1682. struct btrfs_dev_item *dev_item;
  1683. dev_item = btrfs_item_ptr(leaf, slot,
  1684. struct btrfs_dev_item);
  1685. ret = read_one_dev(root, leaf, dev_item);
  1686. BUG_ON(ret);
  1687. }
  1688. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  1689. struct btrfs_chunk *chunk;
  1690. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  1691. ret = read_one_chunk(root, &found_key, leaf, chunk);
  1692. }
  1693. path->slots[0]++;
  1694. }
  1695. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1696. key.objectid = 0;
  1697. btrfs_release_path(root, path);
  1698. goto again;
  1699. }
  1700. btrfs_free_path(path);
  1701. ret = 0;
  1702. error:
  1703. return ret;
  1704. }