volumes.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430
  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 <asm/div64.h>
  22. #include "ctree.h"
  23. #include "extent_map.h"
  24. #include "disk-io.h"
  25. #include "transaction.h"
  26. #include "print-tree.h"
  27. #include "volumes.h"
  28. struct map_lookup {
  29. u64 type;
  30. int io_align;
  31. int io_width;
  32. int stripe_len;
  33. int sector_size;
  34. int num_stripes;
  35. int sub_stripes;
  36. struct btrfs_bio_stripe stripes[];
  37. };
  38. #define map_lookup_size(n) (sizeof(struct map_lookup) + \
  39. (sizeof(struct btrfs_bio_stripe) * (n)))
  40. static DEFINE_MUTEX(uuid_mutex);
  41. static LIST_HEAD(fs_uuids);
  42. int btrfs_cleanup_fs_uuids(void)
  43. {
  44. struct btrfs_fs_devices *fs_devices;
  45. struct list_head *uuid_cur;
  46. struct list_head *devices_cur;
  47. struct btrfs_device *dev;
  48. list_for_each(uuid_cur, &fs_uuids) {
  49. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  50. list);
  51. while(!list_empty(&fs_devices->devices)) {
  52. devices_cur = fs_devices->devices.next;
  53. dev = list_entry(devices_cur, struct btrfs_device,
  54. dev_list);
  55. if (dev->bdev) {
  56. close_bdev_excl(dev->bdev);
  57. }
  58. list_del(&dev->dev_list);
  59. kfree(dev);
  60. }
  61. }
  62. return 0;
  63. }
  64. static struct btrfs_device *__find_device(struct list_head *head, u64 devid,
  65. u8 *uuid)
  66. {
  67. struct btrfs_device *dev;
  68. struct list_head *cur;
  69. list_for_each(cur, head) {
  70. dev = list_entry(cur, struct btrfs_device, dev_list);
  71. if (dev->devid == devid &&
  72. !memcmp(dev->uuid, uuid, BTRFS_UUID_SIZE)) {
  73. return dev;
  74. }
  75. }
  76. return NULL;
  77. }
  78. static struct btrfs_fs_devices *find_fsid(u8 *fsid)
  79. {
  80. struct list_head *cur;
  81. struct btrfs_fs_devices *fs_devices;
  82. list_for_each(cur, &fs_uuids) {
  83. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  84. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  85. return fs_devices;
  86. }
  87. return NULL;
  88. }
  89. static int device_list_add(const char *path,
  90. struct btrfs_super_block *disk_super,
  91. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  92. {
  93. struct btrfs_device *device;
  94. struct btrfs_fs_devices *fs_devices;
  95. u64 found_transid = btrfs_super_generation(disk_super);
  96. fs_devices = find_fsid(disk_super->fsid);
  97. if (!fs_devices) {
  98. fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
  99. if (!fs_devices)
  100. return -ENOMEM;
  101. INIT_LIST_HEAD(&fs_devices->devices);
  102. list_add(&fs_devices->list, &fs_uuids);
  103. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  104. fs_devices->latest_devid = devid;
  105. fs_devices->latest_trans = found_transid;
  106. fs_devices->lowest_devid = (u64)-1;
  107. fs_devices->num_devices = 0;
  108. device = NULL;
  109. } else {
  110. device = __find_device(&fs_devices->devices, devid,
  111. disk_super->dev_item.uuid);
  112. }
  113. if (!device) {
  114. device = kzalloc(sizeof(*device), GFP_NOFS);
  115. if (!device) {
  116. /* we can safely leave the fs_devices entry around */
  117. return -ENOMEM;
  118. }
  119. device->devid = devid;
  120. memcpy(device->uuid, disk_super->dev_item.uuid,
  121. BTRFS_UUID_SIZE);
  122. device->barriers = 1;
  123. spin_lock_init(&device->io_lock);
  124. device->name = kstrdup(path, GFP_NOFS);
  125. if (!device->name) {
  126. kfree(device);
  127. return -ENOMEM;
  128. }
  129. list_add(&device->dev_list, &fs_devices->devices);
  130. fs_devices->num_devices++;
  131. }
  132. if (found_transid > fs_devices->latest_trans) {
  133. fs_devices->latest_devid = devid;
  134. fs_devices->latest_trans = found_transid;
  135. }
  136. if (fs_devices->lowest_devid > devid) {
  137. fs_devices->lowest_devid = devid;
  138. }
  139. *fs_devices_ret = fs_devices;
  140. return 0;
  141. }
  142. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  143. {
  144. struct list_head *head = &fs_devices->devices;
  145. struct list_head *cur;
  146. struct btrfs_device *device;
  147. mutex_lock(&uuid_mutex);
  148. list_for_each(cur, head) {
  149. device = list_entry(cur, struct btrfs_device, dev_list);
  150. if (device->bdev) {
  151. close_bdev_excl(device->bdev);
  152. }
  153. device->bdev = NULL;
  154. }
  155. mutex_unlock(&uuid_mutex);
  156. return 0;
  157. }
  158. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  159. int flags, void *holder)
  160. {
  161. struct block_device *bdev;
  162. struct list_head *head = &fs_devices->devices;
  163. struct list_head *cur;
  164. struct btrfs_device *device;
  165. int ret;
  166. mutex_lock(&uuid_mutex);
  167. list_for_each(cur, head) {
  168. device = list_entry(cur, struct btrfs_device, dev_list);
  169. bdev = open_bdev_excl(device->name, flags, holder);
  170. if (IS_ERR(bdev)) {
  171. printk("open %s failed\n", device->name);
  172. ret = PTR_ERR(bdev);
  173. goto fail;
  174. }
  175. if (device->devid == fs_devices->latest_devid)
  176. fs_devices->latest_bdev = bdev;
  177. if (device->devid == fs_devices->lowest_devid) {
  178. fs_devices->lowest_bdev = bdev;
  179. }
  180. device->bdev = bdev;
  181. }
  182. mutex_unlock(&uuid_mutex);
  183. return 0;
  184. fail:
  185. mutex_unlock(&uuid_mutex);
  186. btrfs_close_devices(fs_devices);
  187. return ret;
  188. }
  189. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  190. struct btrfs_fs_devices **fs_devices_ret)
  191. {
  192. struct btrfs_super_block *disk_super;
  193. struct block_device *bdev;
  194. struct buffer_head *bh;
  195. int ret;
  196. u64 devid;
  197. u64 transid;
  198. mutex_lock(&uuid_mutex);
  199. bdev = open_bdev_excl(path, flags, holder);
  200. if (IS_ERR(bdev)) {
  201. ret = PTR_ERR(bdev);
  202. goto error;
  203. }
  204. ret = set_blocksize(bdev, 4096);
  205. if (ret)
  206. goto error_close;
  207. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  208. if (!bh) {
  209. ret = -EIO;
  210. goto error_close;
  211. }
  212. disk_super = (struct btrfs_super_block *)bh->b_data;
  213. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  214. sizeof(disk_super->magic))) {
  215. ret = -EINVAL;
  216. goto error_brelse;
  217. }
  218. devid = le64_to_cpu(disk_super->dev_item.devid);
  219. transid = btrfs_super_generation(disk_super);
  220. if (disk_super->label[0])
  221. printk("device label %s ", disk_super->label);
  222. else {
  223. /* FIXME, make a readl uuid parser */
  224. printk("device fsid %llx-%llx ",
  225. *(unsigned long long *)disk_super->fsid,
  226. *(unsigned long long *)(disk_super->fsid + 8));
  227. }
  228. printk("devid %Lu transid %Lu %s\n", devid, transid, path);
  229. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  230. error_brelse:
  231. brelse(bh);
  232. error_close:
  233. close_bdev_excl(bdev);
  234. error:
  235. mutex_unlock(&uuid_mutex);
  236. return ret;
  237. }
  238. /*
  239. * this uses a pretty simple search, the expectation is that it is
  240. * called very infrequently and that a given device has a small number
  241. * of extents
  242. */
  243. static int find_free_dev_extent(struct btrfs_trans_handle *trans,
  244. struct btrfs_device *device,
  245. struct btrfs_path *path,
  246. u64 num_bytes, u64 *start)
  247. {
  248. struct btrfs_key key;
  249. struct btrfs_root *root = device->dev_root;
  250. struct btrfs_dev_extent *dev_extent = NULL;
  251. u64 hole_size = 0;
  252. u64 last_byte = 0;
  253. u64 search_start = 0;
  254. u64 search_end = device->total_bytes;
  255. int ret;
  256. int slot = 0;
  257. int start_found;
  258. struct extent_buffer *l;
  259. start_found = 0;
  260. path->reada = 2;
  261. /* FIXME use last free of some kind */
  262. /* we don't want to overwrite the superblock on the drive,
  263. * so we make sure to start at an offset of at least 1MB
  264. */
  265. search_start = max((u64)1024 * 1024, search_start);
  266. key.objectid = device->devid;
  267. key.offset = search_start;
  268. key.type = BTRFS_DEV_EXTENT_KEY;
  269. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  270. if (ret < 0)
  271. goto error;
  272. ret = btrfs_previous_item(root, path, 0, key.type);
  273. if (ret < 0)
  274. goto error;
  275. l = path->nodes[0];
  276. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  277. while (1) {
  278. l = path->nodes[0];
  279. slot = path->slots[0];
  280. if (slot >= btrfs_header_nritems(l)) {
  281. ret = btrfs_next_leaf(root, path);
  282. if (ret == 0)
  283. continue;
  284. if (ret < 0)
  285. goto error;
  286. no_more_items:
  287. if (!start_found) {
  288. if (search_start >= search_end) {
  289. ret = -ENOSPC;
  290. goto error;
  291. }
  292. *start = search_start;
  293. start_found = 1;
  294. goto check_pending;
  295. }
  296. *start = last_byte > search_start ?
  297. last_byte : search_start;
  298. if (search_end <= *start) {
  299. ret = -ENOSPC;
  300. goto error;
  301. }
  302. goto check_pending;
  303. }
  304. btrfs_item_key_to_cpu(l, &key, slot);
  305. if (key.objectid < device->devid)
  306. goto next;
  307. if (key.objectid > device->devid)
  308. goto no_more_items;
  309. if (key.offset >= search_start && key.offset > last_byte &&
  310. start_found) {
  311. if (last_byte < search_start)
  312. last_byte = search_start;
  313. hole_size = key.offset - last_byte;
  314. if (key.offset > last_byte &&
  315. hole_size >= num_bytes) {
  316. *start = last_byte;
  317. goto check_pending;
  318. }
  319. }
  320. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  321. goto next;
  322. }
  323. start_found = 1;
  324. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  325. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  326. next:
  327. path->slots[0]++;
  328. cond_resched();
  329. }
  330. check_pending:
  331. /* we have to make sure we didn't find an extent that has already
  332. * been allocated by the map tree or the original allocation
  333. */
  334. btrfs_release_path(root, path);
  335. BUG_ON(*start < search_start);
  336. if (*start + num_bytes > search_end) {
  337. ret = -ENOSPC;
  338. goto error;
  339. }
  340. /* check for pending inserts here */
  341. return 0;
  342. error:
  343. btrfs_release_path(root, path);
  344. return ret;
  345. }
  346. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  347. struct btrfs_device *device,
  348. u64 chunk_tree, u64 chunk_objectid,
  349. u64 chunk_offset,
  350. u64 num_bytes, u64 *start)
  351. {
  352. int ret;
  353. struct btrfs_path *path;
  354. struct btrfs_root *root = device->dev_root;
  355. struct btrfs_dev_extent *extent;
  356. struct extent_buffer *leaf;
  357. struct btrfs_key key;
  358. path = btrfs_alloc_path();
  359. if (!path)
  360. return -ENOMEM;
  361. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  362. if (ret) {
  363. goto err;
  364. }
  365. key.objectid = device->devid;
  366. key.offset = *start;
  367. key.type = BTRFS_DEV_EXTENT_KEY;
  368. ret = btrfs_insert_empty_item(trans, root, path, &key,
  369. sizeof(*extent));
  370. BUG_ON(ret);
  371. leaf = path->nodes[0];
  372. extent = btrfs_item_ptr(leaf, path->slots[0],
  373. struct btrfs_dev_extent);
  374. btrfs_set_dev_extent_chunk_tree(leaf, extent, chunk_tree);
  375. btrfs_set_dev_extent_chunk_objectid(leaf, extent, chunk_objectid);
  376. btrfs_set_dev_extent_chunk_offset(leaf, extent, chunk_offset);
  377. write_extent_buffer(leaf, root->fs_info->chunk_tree_uuid,
  378. (unsigned long)btrfs_dev_extent_chunk_tree_uuid(extent),
  379. BTRFS_UUID_SIZE);
  380. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  381. btrfs_mark_buffer_dirty(leaf);
  382. err:
  383. btrfs_free_path(path);
  384. return ret;
  385. }
  386. static int find_next_chunk(struct btrfs_root *root, u64 objectid, u64 *offset)
  387. {
  388. struct btrfs_path *path;
  389. int ret;
  390. struct btrfs_key key;
  391. struct btrfs_chunk *chunk;
  392. struct btrfs_key found_key;
  393. path = btrfs_alloc_path();
  394. BUG_ON(!path);
  395. key.objectid = objectid;
  396. key.offset = (u64)-1;
  397. key.type = BTRFS_CHUNK_ITEM_KEY;
  398. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  399. if (ret < 0)
  400. goto error;
  401. BUG_ON(ret == 0);
  402. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  403. if (ret) {
  404. *offset = 0;
  405. } else {
  406. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  407. path->slots[0]);
  408. if (found_key.objectid != objectid)
  409. *offset = 0;
  410. else {
  411. chunk = btrfs_item_ptr(path->nodes[0], path->slots[0],
  412. struct btrfs_chunk);
  413. *offset = found_key.offset +
  414. btrfs_chunk_length(path->nodes[0], chunk);
  415. }
  416. }
  417. ret = 0;
  418. error:
  419. btrfs_free_path(path);
  420. return ret;
  421. }
  422. static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
  423. u64 *objectid)
  424. {
  425. int ret;
  426. struct btrfs_key key;
  427. struct btrfs_key found_key;
  428. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  429. key.type = BTRFS_DEV_ITEM_KEY;
  430. key.offset = (u64)-1;
  431. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  432. if (ret < 0)
  433. goto error;
  434. BUG_ON(ret == 0);
  435. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  436. BTRFS_DEV_ITEM_KEY);
  437. if (ret) {
  438. *objectid = 1;
  439. } else {
  440. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  441. path->slots[0]);
  442. *objectid = found_key.offset + 1;
  443. }
  444. ret = 0;
  445. error:
  446. btrfs_release_path(root, path);
  447. return ret;
  448. }
  449. /*
  450. * the device information is stored in the chunk root
  451. * the btrfs_device struct should be fully filled in
  452. */
  453. int btrfs_add_device(struct btrfs_trans_handle *trans,
  454. struct btrfs_root *root,
  455. struct btrfs_device *device)
  456. {
  457. int ret;
  458. struct btrfs_path *path;
  459. struct btrfs_dev_item *dev_item;
  460. struct extent_buffer *leaf;
  461. struct btrfs_key key;
  462. unsigned long ptr;
  463. u64 free_devid;
  464. root = root->fs_info->chunk_root;
  465. path = btrfs_alloc_path();
  466. if (!path)
  467. return -ENOMEM;
  468. ret = find_next_devid(root, path, &free_devid);
  469. if (ret)
  470. goto out;
  471. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  472. key.type = BTRFS_DEV_ITEM_KEY;
  473. key.offset = free_devid;
  474. ret = btrfs_insert_empty_item(trans, root, path, &key,
  475. sizeof(*dev_item));
  476. if (ret)
  477. goto out;
  478. leaf = path->nodes[0];
  479. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  480. device->devid = free_devid;
  481. btrfs_set_device_id(leaf, dev_item, device->devid);
  482. btrfs_set_device_type(leaf, dev_item, device->type);
  483. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  484. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  485. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  486. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  487. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  488. btrfs_set_device_group(leaf, dev_item, 0);
  489. btrfs_set_device_seek_speed(leaf, dev_item, 0);
  490. btrfs_set_device_bandwidth(leaf, dev_item, 0);
  491. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  492. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  493. btrfs_mark_buffer_dirty(leaf);
  494. ret = 0;
  495. out:
  496. btrfs_free_path(path);
  497. return ret;
  498. }
  499. int btrfs_update_device(struct btrfs_trans_handle *trans,
  500. struct btrfs_device *device)
  501. {
  502. int ret;
  503. struct btrfs_path *path;
  504. struct btrfs_root *root;
  505. struct btrfs_dev_item *dev_item;
  506. struct extent_buffer *leaf;
  507. struct btrfs_key key;
  508. root = device->dev_root->fs_info->chunk_root;
  509. path = btrfs_alloc_path();
  510. if (!path)
  511. return -ENOMEM;
  512. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  513. key.type = BTRFS_DEV_ITEM_KEY;
  514. key.offset = device->devid;
  515. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  516. if (ret < 0)
  517. goto out;
  518. if (ret > 0) {
  519. ret = -ENOENT;
  520. goto out;
  521. }
  522. leaf = path->nodes[0];
  523. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  524. btrfs_set_device_id(leaf, dev_item, device->devid);
  525. btrfs_set_device_type(leaf, dev_item, device->type);
  526. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  527. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  528. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  529. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  530. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  531. btrfs_mark_buffer_dirty(leaf);
  532. out:
  533. btrfs_free_path(path);
  534. return ret;
  535. }
  536. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  537. struct btrfs_root *root,
  538. struct btrfs_key *key,
  539. struct btrfs_chunk *chunk, int item_size)
  540. {
  541. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  542. struct btrfs_disk_key disk_key;
  543. u32 array_size;
  544. u8 *ptr;
  545. array_size = btrfs_super_sys_array_size(super_copy);
  546. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  547. return -EFBIG;
  548. ptr = super_copy->sys_chunk_array + array_size;
  549. btrfs_cpu_key_to_disk(&disk_key, key);
  550. memcpy(ptr, &disk_key, sizeof(disk_key));
  551. ptr += sizeof(disk_key);
  552. memcpy(ptr, chunk, item_size);
  553. item_size += sizeof(disk_key);
  554. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  555. return 0;
  556. }
  557. static u64 div_factor(u64 num, int factor)
  558. {
  559. if (factor == 10)
  560. return num;
  561. num *= factor;
  562. do_div(num, 10);
  563. return num;
  564. }
  565. static u64 chunk_bytes_by_type(u64 type, u64 calc_size, int num_stripes,
  566. int sub_stripes)
  567. {
  568. if (type & (BTRFS_BLOCK_GROUP_RAID1 | BTRFS_BLOCK_GROUP_DUP))
  569. return calc_size;
  570. else if (type & BTRFS_BLOCK_GROUP_RAID10)
  571. return calc_size * (num_stripes / sub_stripes);
  572. else
  573. return calc_size * num_stripes;
  574. }
  575. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  576. struct btrfs_root *extent_root, u64 *start,
  577. u64 *num_bytes, u64 type)
  578. {
  579. u64 dev_offset;
  580. struct btrfs_fs_info *info = extent_root->fs_info;
  581. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  582. struct btrfs_stripe *stripes;
  583. struct btrfs_device *device = NULL;
  584. struct btrfs_chunk *chunk;
  585. struct list_head private_devs;
  586. struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
  587. struct list_head *cur;
  588. struct extent_map_tree *em_tree;
  589. struct map_lookup *map;
  590. struct extent_map *em;
  591. int min_chunk_size = 8 * 1024 * 1024;
  592. u64 physical;
  593. u64 calc_size = 1024 * 1024 * 1024;
  594. u64 max_chunk_size = calc_size;
  595. u64 min_free;
  596. u64 avail;
  597. u64 max_avail = 0;
  598. u64 percent_max;
  599. int num_stripes = 1;
  600. int sub_stripes = 0;
  601. int looped = 0;
  602. int ret;
  603. int index;
  604. int stripe_len = 64 * 1024;
  605. struct btrfs_key key;
  606. if (list_empty(dev_list))
  607. return -ENOSPC;
  608. if (type & (BTRFS_BLOCK_GROUP_RAID0))
  609. num_stripes = btrfs_super_num_devices(&info->super_copy);
  610. if (type & (BTRFS_BLOCK_GROUP_DUP))
  611. num_stripes = 2;
  612. if (type & (BTRFS_BLOCK_GROUP_RAID1)) {
  613. num_stripes = min_t(u64, 2,
  614. btrfs_super_num_devices(&info->super_copy));
  615. if (num_stripes < 2)
  616. return -ENOSPC;
  617. }
  618. if (type & (BTRFS_BLOCK_GROUP_RAID10)) {
  619. num_stripes = btrfs_super_num_devices(&info->super_copy);
  620. if (num_stripes < 4)
  621. return -ENOSPC;
  622. num_stripes &= ~(u32)1;
  623. sub_stripes = 2;
  624. }
  625. if (type & BTRFS_BLOCK_GROUP_DATA) {
  626. max_chunk_size = 10 * calc_size;
  627. min_chunk_size = 256 * 1024 * 1024;
  628. } else if (type & BTRFS_BLOCK_GROUP_METADATA) {
  629. max_chunk_size = 4 * calc_size;
  630. min_chunk_size = 64 * 1024 * 1024;
  631. } else {
  632. min_chunk_size = 32 * 1024 * 1024;
  633. }
  634. /* we don't want a chunk larger than 10% of the FS */
  635. percent_max = div_factor(btrfs_super_total_bytes(&info->super_copy), 1);
  636. max_chunk_size = min(percent_max, max_chunk_size);
  637. if (calc_size * num_stripes > max_chunk_size) {
  638. calc_size = max_chunk_size;
  639. do_div(calc_size, num_stripes);
  640. do_div(calc_size, stripe_len);
  641. calc_size *= stripe_len;
  642. }
  643. /* we don't want tiny stripes */
  644. *num_bytes = chunk_bytes_by_type(type, calc_size,
  645. num_stripes, sub_stripes);
  646. calc_size = max_t(u64, chunk_bytes_by_type(type, min_chunk_size,
  647. num_stripes, sub_stripes), calc_size);
  648. again:
  649. do_div(calc_size, stripe_len);
  650. calc_size *= stripe_len;
  651. INIT_LIST_HEAD(&private_devs);
  652. cur = dev_list->next;
  653. index = 0;
  654. if (type & BTRFS_BLOCK_GROUP_DUP)
  655. min_free = calc_size * 2;
  656. else
  657. min_free = calc_size;
  658. /* build a private list of devices we will allocate from */
  659. while(index < num_stripes) {
  660. device = list_entry(cur, struct btrfs_device, dev_list);
  661. avail = device->total_bytes - device->bytes_used;
  662. cur = cur->next;
  663. if (avail > max_avail)
  664. max_avail = avail;
  665. if (avail >= min_free) {
  666. list_move_tail(&device->dev_list, &private_devs);
  667. index++;
  668. if (type & BTRFS_BLOCK_GROUP_DUP)
  669. index++;
  670. }
  671. if (cur == dev_list)
  672. break;
  673. }
  674. if (index < num_stripes) {
  675. list_splice(&private_devs, dev_list);
  676. if (!looped && max_avail > 0) {
  677. looped = 1;
  678. calc_size = max_avail;
  679. goto again;
  680. }
  681. return -ENOSPC;
  682. }
  683. key.objectid = BTRFS_FIRST_CHUNK_TREE_OBJECTID;
  684. key.type = BTRFS_CHUNK_ITEM_KEY;
  685. ret = find_next_chunk(chunk_root, BTRFS_FIRST_CHUNK_TREE_OBJECTID,
  686. &key.offset);
  687. if (ret)
  688. return ret;
  689. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  690. if (!chunk)
  691. return -ENOMEM;
  692. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  693. if (!map) {
  694. kfree(chunk);
  695. return -ENOMEM;
  696. }
  697. stripes = &chunk->stripe;
  698. *num_bytes = chunk_bytes_by_type(type, calc_size,
  699. num_stripes, sub_stripes);
  700. index = 0;
  701. printk("new chunk type %Lu start %Lu size %Lu\n", type, key.offset, *num_bytes);
  702. while(index < num_stripes) {
  703. struct btrfs_stripe *stripe;
  704. BUG_ON(list_empty(&private_devs));
  705. cur = private_devs.next;
  706. device = list_entry(cur, struct btrfs_device, dev_list);
  707. /* loop over this device again if we're doing a dup group */
  708. if (!(type & BTRFS_BLOCK_GROUP_DUP) ||
  709. (index == num_stripes - 1))
  710. list_move_tail(&device->dev_list, dev_list);
  711. ret = btrfs_alloc_dev_extent(trans, device,
  712. info->chunk_root->root_key.objectid,
  713. BTRFS_FIRST_CHUNK_TREE_OBJECTID, key.offset,
  714. calc_size, &dev_offset);
  715. BUG_ON(ret);
  716. printk("alloc chunk start %Lu size %Lu from dev %Lu type %Lu\n", key.offset, calc_size, device->devid, type);
  717. device->bytes_used += calc_size;
  718. ret = btrfs_update_device(trans, device);
  719. BUG_ON(ret);
  720. map->stripes[index].dev = device;
  721. map->stripes[index].physical = dev_offset;
  722. stripe = stripes + index;
  723. btrfs_set_stack_stripe_devid(stripe, device->devid);
  724. btrfs_set_stack_stripe_offset(stripe, dev_offset);
  725. memcpy(stripe->dev_uuid, device->uuid, BTRFS_UUID_SIZE);
  726. physical = dev_offset;
  727. index++;
  728. }
  729. BUG_ON(!list_empty(&private_devs));
  730. /* key was set above */
  731. btrfs_set_stack_chunk_length(chunk, *num_bytes);
  732. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  733. btrfs_set_stack_chunk_stripe_len(chunk, stripe_len);
  734. btrfs_set_stack_chunk_type(chunk, type);
  735. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  736. btrfs_set_stack_chunk_io_align(chunk, stripe_len);
  737. btrfs_set_stack_chunk_io_width(chunk, stripe_len);
  738. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  739. btrfs_set_stack_chunk_sub_stripes(chunk, sub_stripes);
  740. map->sector_size = extent_root->sectorsize;
  741. map->stripe_len = stripe_len;
  742. map->io_align = stripe_len;
  743. map->io_width = stripe_len;
  744. map->type = type;
  745. map->num_stripes = num_stripes;
  746. map->sub_stripes = sub_stripes;
  747. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  748. btrfs_chunk_item_size(num_stripes));
  749. BUG_ON(ret);
  750. *start = key.offset;;
  751. em = alloc_extent_map(GFP_NOFS);
  752. if (!em)
  753. return -ENOMEM;
  754. em->bdev = (struct block_device *)map;
  755. em->start = key.offset;
  756. em->len = *num_bytes;
  757. em->block_start = 0;
  758. kfree(chunk);
  759. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  760. spin_lock(&em_tree->lock);
  761. ret = add_extent_mapping(em_tree, em);
  762. spin_unlock(&em_tree->lock);
  763. BUG_ON(ret);
  764. free_extent_map(em);
  765. return ret;
  766. }
  767. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  768. {
  769. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  770. }
  771. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  772. {
  773. struct extent_map *em;
  774. while(1) {
  775. spin_lock(&tree->map_tree.lock);
  776. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  777. if (em)
  778. remove_extent_mapping(&tree->map_tree, em);
  779. spin_unlock(&tree->map_tree.lock);
  780. if (!em)
  781. break;
  782. kfree(em->bdev);
  783. /* once for us */
  784. free_extent_map(em);
  785. /* once for the tree */
  786. free_extent_map(em);
  787. }
  788. }
  789. int btrfs_num_copies(struct btrfs_mapping_tree *map_tree, u64 logical, u64 len)
  790. {
  791. struct extent_map *em;
  792. struct map_lookup *map;
  793. struct extent_map_tree *em_tree = &map_tree->map_tree;
  794. int ret;
  795. spin_lock(&em_tree->lock);
  796. em = lookup_extent_mapping(em_tree, logical, len);
  797. spin_unlock(&em_tree->lock);
  798. BUG_ON(!em);
  799. BUG_ON(em->start > logical || em->start + em->len < logical);
  800. map = (struct map_lookup *)em->bdev;
  801. if (map->type & (BTRFS_BLOCK_GROUP_DUP | BTRFS_BLOCK_GROUP_RAID1))
  802. ret = map->num_stripes;
  803. else if (map->type & BTRFS_BLOCK_GROUP_RAID10)
  804. ret = map->sub_stripes;
  805. else
  806. ret = 1;
  807. free_extent_map(em);
  808. return ret;
  809. }
  810. int btrfs_map_block(struct btrfs_mapping_tree *map_tree, int rw,
  811. u64 logical, u64 *length,
  812. struct btrfs_multi_bio **multi_ret, int mirror_num)
  813. {
  814. struct extent_map *em;
  815. struct map_lookup *map;
  816. struct extent_map_tree *em_tree = &map_tree->map_tree;
  817. u64 offset;
  818. u64 stripe_offset;
  819. u64 stripe_nr;
  820. int stripes_allocated = 8;
  821. int stripes_required = 1;
  822. int stripe_index;
  823. int i;
  824. struct btrfs_multi_bio *multi = NULL;
  825. if (multi_ret && !(rw & (1 << BIO_RW))) {
  826. stripes_allocated = 1;
  827. }
  828. again:
  829. if (multi_ret) {
  830. multi = kzalloc(btrfs_multi_bio_size(stripes_allocated),
  831. GFP_NOFS);
  832. if (!multi)
  833. return -ENOMEM;
  834. }
  835. spin_lock(&em_tree->lock);
  836. em = lookup_extent_mapping(em_tree, logical, *length);
  837. spin_unlock(&em_tree->lock);
  838. if (!em) {
  839. printk("unable to find logical %Lu\n", logical);
  840. }
  841. BUG_ON(!em);
  842. BUG_ON(em->start > logical || em->start + em->len < logical);
  843. map = (struct map_lookup *)em->bdev;
  844. offset = logical - em->start;
  845. if (mirror_num > map->num_stripes)
  846. mirror_num = 0;
  847. /* if our multi bio struct is too small, back off and try again */
  848. if (rw & (1 << BIO_RW)) {
  849. if (map->type & (BTRFS_BLOCK_GROUP_RAID1 |
  850. BTRFS_BLOCK_GROUP_DUP)) {
  851. stripes_required = map->num_stripes;
  852. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  853. stripes_required = map->sub_stripes;
  854. }
  855. }
  856. if (multi_ret && rw == WRITE &&
  857. stripes_allocated < stripes_required) {
  858. stripes_allocated = map->num_stripes;
  859. free_extent_map(em);
  860. kfree(multi);
  861. goto again;
  862. }
  863. stripe_nr = offset;
  864. /*
  865. * stripe_nr counts the total number of stripes we have to stride
  866. * to get to this block
  867. */
  868. do_div(stripe_nr, map->stripe_len);
  869. stripe_offset = stripe_nr * map->stripe_len;
  870. BUG_ON(offset < stripe_offset);
  871. /* stripe_offset is the offset of this block in its stripe*/
  872. stripe_offset = offset - stripe_offset;
  873. if (map->type & (BTRFS_BLOCK_GROUP_RAID0 | BTRFS_BLOCK_GROUP_RAID1 |
  874. BTRFS_BLOCK_GROUP_RAID10 |
  875. BTRFS_BLOCK_GROUP_DUP)) {
  876. /* we limit the length of each bio to what fits in a stripe */
  877. *length = min_t(u64, em->len - offset,
  878. map->stripe_len - stripe_offset);
  879. } else {
  880. *length = em->len - offset;
  881. }
  882. if (!multi_ret)
  883. goto out;
  884. multi->num_stripes = 1;
  885. stripe_index = 0;
  886. if (map->type & BTRFS_BLOCK_GROUP_RAID1) {
  887. if (rw & (1 << BIO_RW))
  888. multi->num_stripes = map->num_stripes;
  889. else if (mirror_num) {
  890. stripe_index = mirror_num - 1;
  891. } else {
  892. int i;
  893. u64 least = (u64)-1;
  894. struct btrfs_device *cur;
  895. for (i = 0; i < map->num_stripes; i++) {
  896. cur = map->stripes[i].dev;
  897. spin_lock(&cur->io_lock);
  898. if (cur->total_ios < least) {
  899. least = cur->total_ios;
  900. stripe_index = i;
  901. }
  902. spin_unlock(&cur->io_lock);
  903. }
  904. }
  905. } else if (map->type & BTRFS_BLOCK_GROUP_DUP) {
  906. if (rw & (1 << BIO_RW))
  907. multi->num_stripes = map->num_stripes;
  908. else if (mirror_num)
  909. stripe_index = mirror_num - 1;
  910. } else if (map->type & BTRFS_BLOCK_GROUP_RAID10) {
  911. int factor = map->num_stripes / map->sub_stripes;
  912. int orig_stripe_nr = stripe_nr;
  913. stripe_index = do_div(stripe_nr, factor);
  914. stripe_index *= map->sub_stripes;
  915. if (rw & (1 << BIO_RW))
  916. multi->num_stripes = map->sub_stripes;
  917. else if (mirror_num)
  918. stripe_index += mirror_num - 1;
  919. else
  920. stripe_index += orig_stripe_nr % map->sub_stripes;
  921. } else {
  922. /*
  923. * after this do_div call, stripe_nr is the number of stripes
  924. * on this device we have to walk to find the data, and
  925. * stripe_index is the number of our device in the stripe array
  926. */
  927. stripe_index = do_div(stripe_nr, map->num_stripes);
  928. }
  929. BUG_ON(stripe_index >= map->num_stripes);
  930. for (i = 0; i < multi->num_stripes; i++) {
  931. multi->stripes[i].physical =
  932. map->stripes[stripe_index].physical + stripe_offset +
  933. stripe_nr * map->stripe_len;
  934. multi->stripes[i].dev = map->stripes[stripe_index].dev;
  935. stripe_index++;
  936. }
  937. *multi_ret = multi;
  938. out:
  939. free_extent_map(em);
  940. return 0;
  941. }
  942. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
  943. static void end_bio_multi_stripe(struct bio *bio, int err)
  944. #else
  945. static int end_bio_multi_stripe(struct bio *bio,
  946. unsigned int bytes_done, int err)
  947. #endif
  948. {
  949. struct btrfs_multi_bio *multi = bio->bi_private;
  950. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  951. if (bio->bi_size)
  952. return 1;
  953. #endif
  954. if (err)
  955. multi->error = err;
  956. if (atomic_dec_and_test(&multi->stripes_pending)) {
  957. bio->bi_private = multi->private;
  958. bio->bi_end_io = multi->end_io;
  959. if (!err && multi->error)
  960. err = multi->error;
  961. kfree(multi);
  962. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  963. bio_endio(bio, bio->bi_size, err);
  964. #else
  965. bio_endio(bio, err);
  966. #endif
  967. } else {
  968. bio_put(bio);
  969. }
  970. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,23)
  971. return 0;
  972. #endif
  973. }
  974. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio,
  975. int mirror_num)
  976. {
  977. struct btrfs_mapping_tree *map_tree;
  978. struct btrfs_device *dev;
  979. struct bio *first_bio = bio;
  980. u64 logical = bio->bi_sector << 9;
  981. u64 length = 0;
  982. u64 map_length;
  983. struct bio_vec *bvec;
  984. struct btrfs_multi_bio *multi = NULL;
  985. int i;
  986. int ret;
  987. int dev_nr = 0;
  988. int total_devs = 1;
  989. bio_for_each_segment(bvec, bio, i) {
  990. length += bvec->bv_len;
  991. }
  992. map_tree = &root->fs_info->mapping_tree;
  993. map_length = length;
  994. ret = btrfs_map_block(map_tree, rw, logical, &map_length, &multi,
  995. mirror_num);
  996. BUG_ON(ret);
  997. total_devs = multi->num_stripes;
  998. if (map_length < length) {
  999. printk("mapping failed logical %Lu bio len %Lu "
  1000. "len %Lu\n", logical, length, map_length);
  1001. BUG();
  1002. }
  1003. multi->end_io = first_bio->bi_end_io;
  1004. multi->private = first_bio->bi_private;
  1005. atomic_set(&multi->stripes_pending, multi->num_stripes);
  1006. while(dev_nr < total_devs) {
  1007. if (total_devs > 1) {
  1008. if (dev_nr < total_devs - 1) {
  1009. bio = bio_clone(first_bio, GFP_NOFS);
  1010. BUG_ON(!bio);
  1011. } else {
  1012. bio = first_bio;
  1013. }
  1014. bio->bi_private = multi;
  1015. bio->bi_end_io = end_bio_multi_stripe;
  1016. }
  1017. bio->bi_sector = multi->stripes[dev_nr].physical >> 9;
  1018. dev = multi->stripes[dev_nr].dev;
  1019. bio->bi_bdev = dev->bdev;
  1020. spin_lock(&dev->io_lock);
  1021. dev->total_ios++;
  1022. spin_unlock(&dev->io_lock);
  1023. submit_bio(rw, bio);
  1024. dev_nr++;
  1025. }
  1026. if (total_devs == 1)
  1027. kfree(multi);
  1028. return 0;
  1029. }
  1030. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid,
  1031. u8 *uuid)
  1032. {
  1033. struct list_head *head = &root->fs_info->fs_devices->devices;
  1034. return __find_device(head, devid, uuid);
  1035. }
  1036. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  1037. struct extent_buffer *leaf,
  1038. struct btrfs_chunk *chunk)
  1039. {
  1040. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  1041. struct map_lookup *map;
  1042. struct extent_map *em;
  1043. u64 logical;
  1044. u64 length;
  1045. u64 devid;
  1046. u8 uuid[BTRFS_UUID_SIZE];
  1047. int num_stripes;
  1048. int ret;
  1049. int i;
  1050. logical = key->offset;
  1051. length = btrfs_chunk_length(leaf, chunk);
  1052. spin_lock(&map_tree->map_tree.lock);
  1053. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  1054. spin_unlock(&map_tree->map_tree.lock);
  1055. /* already mapped? */
  1056. if (em && em->start <= logical && em->start + em->len > logical) {
  1057. free_extent_map(em);
  1058. return 0;
  1059. } else if (em) {
  1060. free_extent_map(em);
  1061. }
  1062. map = kzalloc(sizeof(*map), GFP_NOFS);
  1063. if (!map)
  1064. return -ENOMEM;
  1065. em = alloc_extent_map(GFP_NOFS);
  1066. if (!em)
  1067. return -ENOMEM;
  1068. num_stripes = btrfs_chunk_num_stripes(leaf, chunk);
  1069. map = kmalloc(map_lookup_size(num_stripes), GFP_NOFS);
  1070. if (!map) {
  1071. free_extent_map(em);
  1072. return -ENOMEM;
  1073. }
  1074. em->bdev = (struct block_device *)map;
  1075. em->start = logical;
  1076. em->len = length;
  1077. em->block_start = 0;
  1078. map->num_stripes = num_stripes;
  1079. map->io_width = btrfs_chunk_io_width(leaf, chunk);
  1080. map->io_align = btrfs_chunk_io_align(leaf, chunk);
  1081. map->sector_size = btrfs_chunk_sector_size(leaf, chunk);
  1082. map->stripe_len = btrfs_chunk_stripe_len(leaf, chunk);
  1083. map->type = btrfs_chunk_type(leaf, chunk);
  1084. map->sub_stripes = btrfs_chunk_sub_stripes(leaf, chunk);
  1085. for (i = 0; i < num_stripes; i++) {
  1086. map->stripes[i].physical =
  1087. btrfs_stripe_offset_nr(leaf, chunk, i);
  1088. devid = btrfs_stripe_devid_nr(leaf, chunk, i);
  1089. read_extent_buffer(leaf, uuid, (unsigned long)
  1090. btrfs_stripe_dev_uuid_nr(chunk, i),
  1091. BTRFS_UUID_SIZE);
  1092. map->stripes[i].dev = btrfs_find_device(root, devid, uuid);
  1093. if (!map->stripes[i].dev) {
  1094. kfree(map);
  1095. free_extent_map(em);
  1096. return -EIO;
  1097. }
  1098. }
  1099. spin_lock(&map_tree->map_tree.lock);
  1100. ret = add_extent_mapping(&map_tree->map_tree, em);
  1101. spin_unlock(&map_tree->map_tree.lock);
  1102. BUG_ON(ret);
  1103. free_extent_map(em);
  1104. return 0;
  1105. }
  1106. static int fill_device_from_item(struct extent_buffer *leaf,
  1107. struct btrfs_dev_item *dev_item,
  1108. struct btrfs_device *device)
  1109. {
  1110. unsigned long ptr;
  1111. device->devid = btrfs_device_id(leaf, dev_item);
  1112. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  1113. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  1114. device->type = btrfs_device_type(leaf, dev_item);
  1115. device->io_align = btrfs_device_io_align(leaf, dev_item);
  1116. device->io_width = btrfs_device_io_width(leaf, dev_item);
  1117. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  1118. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  1119. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
  1120. return 0;
  1121. }
  1122. static int read_one_dev(struct btrfs_root *root,
  1123. struct extent_buffer *leaf,
  1124. struct btrfs_dev_item *dev_item)
  1125. {
  1126. struct btrfs_device *device;
  1127. u64 devid;
  1128. int ret;
  1129. u8 dev_uuid[BTRFS_UUID_SIZE];
  1130. devid = btrfs_device_id(leaf, dev_item);
  1131. read_extent_buffer(leaf, dev_uuid,
  1132. (unsigned long)btrfs_device_uuid(dev_item),
  1133. BTRFS_UUID_SIZE);
  1134. device = btrfs_find_device(root, devid, dev_uuid);
  1135. if (!device) {
  1136. printk("warning devid %Lu not found already\n", devid);
  1137. device = kzalloc(sizeof(*device), GFP_NOFS);
  1138. if (!device)
  1139. return -ENOMEM;
  1140. list_add(&device->dev_list,
  1141. &root->fs_info->fs_devices->devices);
  1142. device->barriers = 1;
  1143. spin_lock_init(&device->io_lock);
  1144. }
  1145. fill_device_from_item(leaf, dev_item, device);
  1146. device->dev_root = root->fs_info->dev_root;
  1147. ret = 0;
  1148. #if 0
  1149. ret = btrfs_open_device(device);
  1150. if (ret) {
  1151. kfree(device);
  1152. }
  1153. #endif
  1154. return ret;
  1155. }
  1156. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  1157. {
  1158. struct btrfs_dev_item *dev_item;
  1159. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  1160. dev_item);
  1161. return read_one_dev(root, buf, dev_item);
  1162. }
  1163. int btrfs_read_sys_array(struct btrfs_root *root)
  1164. {
  1165. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  1166. struct extent_buffer *sb = root->fs_info->sb_buffer;
  1167. struct btrfs_disk_key *disk_key;
  1168. struct btrfs_chunk *chunk;
  1169. struct btrfs_key key;
  1170. u32 num_stripes;
  1171. u32 array_size;
  1172. u32 len = 0;
  1173. u8 *ptr;
  1174. unsigned long sb_ptr;
  1175. u32 cur;
  1176. int ret;
  1177. array_size = btrfs_super_sys_array_size(super_copy);
  1178. /*
  1179. * we do this loop twice, once for the device items and
  1180. * once for all of the chunks. This way there are device
  1181. * structs filled in for every chunk
  1182. */
  1183. ptr = super_copy->sys_chunk_array;
  1184. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  1185. cur = 0;
  1186. while (cur < array_size) {
  1187. disk_key = (struct btrfs_disk_key *)ptr;
  1188. btrfs_disk_key_to_cpu(&key, disk_key);
  1189. len = sizeof(*disk_key);
  1190. ptr += len;
  1191. sb_ptr += len;
  1192. cur += len;
  1193. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  1194. chunk = (struct btrfs_chunk *)sb_ptr;
  1195. ret = read_one_chunk(root, &key, sb, chunk);
  1196. BUG_ON(ret);
  1197. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  1198. len = btrfs_chunk_item_size(num_stripes);
  1199. } else {
  1200. BUG();
  1201. }
  1202. ptr += len;
  1203. sb_ptr += len;
  1204. cur += len;
  1205. }
  1206. return 0;
  1207. }
  1208. int btrfs_read_chunk_tree(struct btrfs_root *root)
  1209. {
  1210. struct btrfs_path *path;
  1211. struct extent_buffer *leaf;
  1212. struct btrfs_key key;
  1213. struct btrfs_key found_key;
  1214. int ret;
  1215. int slot;
  1216. root = root->fs_info->chunk_root;
  1217. path = btrfs_alloc_path();
  1218. if (!path)
  1219. return -ENOMEM;
  1220. /* first we search for all of the device items, and then we
  1221. * read in all of the chunk items. This way we can create chunk
  1222. * mappings that reference all of the devices that are afound
  1223. */
  1224. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  1225. key.offset = 0;
  1226. key.type = 0;
  1227. again:
  1228. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  1229. while(1) {
  1230. leaf = path->nodes[0];
  1231. slot = path->slots[0];
  1232. if (slot >= btrfs_header_nritems(leaf)) {
  1233. ret = btrfs_next_leaf(root, path);
  1234. if (ret == 0)
  1235. continue;
  1236. if (ret < 0)
  1237. goto error;
  1238. break;
  1239. }
  1240. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  1241. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1242. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  1243. break;
  1244. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  1245. struct btrfs_dev_item *dev_item;
  1246. dev_item = btrfs_item_ptr(leaf, slot,
  1247. struct btrfs_dev_item);
  1248. ret = read_one_dev(root, leaf, dev_item);
  1249. BUG_ON(ret);
  1250. }
  1251. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  1252. struct btrfs_chunk *chunk;
  1253. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  1254. ret = read_one_chunk(root, &found_key, leaf, chunk);
  1255. }
  1256. path->slots[0]++;
  1257. }
  1258. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  1259. key.objectid = 0;
  1260. btrfs_release_path(root, path);
  1261. goto again;
  1262. }
  1263. btrfs_free_path(path);
  1264. ret = 0;
  1265. error:
  1266. return ret;
  1267. }