volumes.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  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 "ctree.h"
  22. #include "extent_map.h"
  23. #include "disk-io.h"
  24. #include "transaction.h"
  25. #include "print-tree.h"
  26. #include "volumes.h"
  27. struct map_lookup {
  28. struct btrfs_device *dev;
  29. u64 physical;
  30. };
  31. static DEFINE_MUTEX(uuid_mutex);
  32. static LIST_HEAD(fs_uuids);
  33. int btrfs_cleanup_fs_uuids(void)
  34. {
  35. struct btrfs_fs_devices *fs_devices;
  36. struct list_head *uuid_cur;
  37. struct list_head *devices_cur;
  38. struct btrfs_device *dev;
  39. list_for_each(uuid_cur, &fs_uuids) {
  40. fs_devices = list_entry(uuid_cur, struct btrfs_fs_devices,
  41. list);
  42. while(!list_empty(&fs_devices->devices)) {
  43. devices_cur = fs_devices->devices.next;
  44. dev = list_entry(devices_cur, struct btrfs_device,
  45. dev_list);
  46. printk("uuid cleanup finds %s\n", dev->name);
  47. if (dev->bdev) {
  48. printk("closing\n");
  49. close_bdev_excl(dev->bdev);
  50. }
  51. list_del(&dev->dev_list);
  52. kfree(dev);
  53. }
  54. }
  55. return 0;
  56. }
  57. static struct btrfs_device *__find_device(struct list_head *head, u64 devid)
  58. {
  59. struct btrfs_device *dev;
  60. struct list_head *cur;
  61. list_for_each(cur, head) {
  62. dev = list_entry(cur, struct btrfs_device, dev_list);
  63. if (dev->devid == devid)
  64. return dev;
  65. }
  66. return NULL;
  67. }
  68. static struct btrfs_fs_devices *find_fsid(u8 *fsid)
  69. {
  70. struct list_head *cur;
  71. struct btrfs_fs_devices *fs_devices;
  72. list_for_each(cur, &fs_uuids) {
  73. fs_devices = list_entry(cur, struct btrfs_fs_devices, list);
  74. if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
  75. return fs_devices;
  76. }
  77. return NULL;
  78. }
  79. static int device_list_add(const char *path,
  80. struct btrfs_super_block *disk_super,
  81. u64 devid, struct btrfs_fs_devices **fs_devices_ret)
  82. {
  83. struct btrfs_device *device;
  84. struct btrfs_fs_devices *fs_devices;
  85. u64 found_transid = btrfs_super_generation(disk_super);
  86. fs_devices = find_fsid(disk_super->fsid);
  87. if (!fs_devices) {
  88. fs_devices = kmalloc(sizeof(*fs_devices), GFP_NOFS);
  89. if (!fs_devices)
  90. return -ENOMEM;
  91. INIT_LIST_HEAD(&fs_devices->devices);
  92. list_add(&fs_devices->list, &fs_uuids);
  93. memcpy(fs_devices->fsid, disk_super->fsid, BTRFS_FSID_SIZE);
  94. fs_devices->latest_devid = devid;
  95. fs_devices->latest_trans = found_transid;
  96. fs_devices->lowest_devid = (u64)-1;
  97. fs_devices->num_devices = 0;
  98. device = NULL;
  99. } else {
  100. device = __find_device(&fs_devices->devices, devid);
  101. }
  102. if (!device) {
  103. device = kzalloc(sizeof(*device), GFP_NOFS);
  104. if (!device) {
  105. /* we can safely leave the fs_devices entry around */
  106. return -ENOMEM;
  107. }
  108. device->devid = devid;
  109. device->name = kstrdup(path, GFP_NOFS);
  110. if (!device->name) {
  111. kfree(device);
  112. return -ENOMEM;
  113. }
  114. list_add(&device->dev_list, &fs_devices->devices);
  115. fs_devices->num_devices++;
  116. }
  117. if (found_transid > fs_devices->latest_trans) {
  118. fs_devices->latest_devid = devid;
  119. fs_devices->latest_trans = found_transid;
  120. }
  121. if (fs_devices->lowest_devid > devid) {
  122. fs_devices->lowest_devid = devid;
  123. printk("lowest devid now %Lu\n", devid);
  124. }
  125. *fs_devices_ret = fs_devices;
  126. return 0;
  127. }
  128. int btrfs_close_devices(struct btrfs_fs_devices *fs_devices)
  129. {
  130. struct list_head *head = &fs_devices->devices;
  131. struct list_head *cur;
  132. struct btrfs_device *device;
  133. mutex_lock(&uuid_mutex);
  134. list_for_each(cur, head) {
  135. device = list_entry(cur, struct btrfs_device, dev_list);
  136. if (device->bdev) {
  137. close_bdev_excl(device->bdev);
  138. printk("close devices closes %s\n", device->name);
  139. }
  140. device->bdev = NULL;
  141. }
  142. mutex_unlock(&uuid_mutex);
  143. return 0;
  144. }
  145. int btrfs_open_devices(struct btrfs_fs_devices *fs_devices,
  146. int flags, void *holder)
  147. {
  148. struct block_device *bdev;
  149. struct list_head *head = &fs_devices->devices;
  150. struct list_head *cur;
  151. struct btrfs_device *device;
  152. int ret;
  153. mutex_lock(&uuid_mutex);
  154. list_for_each(cur, head) {
  155. device = list_entry(cur, struct btrfs_device, dev_list);
  156. bdev = open_bdev_excl(device->name, flags, holder);
  157. printk("opening %s devid %Lu\n", device->name, device->devid);
  158. if (IS_ERR(bdev)) {
  159. printk("open %s failed\n", device->name);
  160. ret = PTR_ERR(bdev);
  161. goto fail;
  162. }
  163. if (device->devid == fs_devices->latest_devid)
  164. fs_devices->latest_bdev = bdev;
  165. if (device->devid == fs_devices->lowest_devid) {
  166. fs_devices->lowest_bdev = bdev;
  167. printk("lowest bdev %s\n", device->name);
  168. }
  169. device->bdev = bdev;
  170. }
  171. mutex_unlock(&uuid_mutex);
  172. return 0;
  173. fail:
  174. mutex_unlock(&uuid_mutex);
  175. btrfs_close_devices(fs_devices);
  176. return ret;
  177. }
  178. int btrfs_scan_one_device(const char *path, int flags, void *holder,
  179. struct btrfs_fs_devices **fs_devices_ret)
  180. {
  181. struct btrfs_super_block *disk_super;
  182. struct block_device *bdev;
  183. struct buffer_head *bh;
  184. int ret;
  185. u64 devid;
  186. mutex_lock(&uuid_mutex);
  187. printk("scan one opens %s\n", path);
  188. bdev = open_bdev_excl(path, flags, holder);
  189. if (IS_ERR(bdev)) {
  190. printk("open failed\n");
  191. ret = PTR_ERR(bdev);
  192. goto error;
  193. }
  194. ret = set_blocksize(bdev, 4096);
  195. if (ret)
  196. goto error_close;
  197. bh = __bread(bdev, BTRFS_SUPER_INFO_OFFSET / 4096, 4096);
  198. if (!bh) {
  199. ret = -EIO;
  200. goto error_close;
  201. }
  202. disk_super = (struct btrfs_super_block *)bh->b_data;
  203. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  204. sizeof(disk_super->magic))) {
  205. printk("no btrfs found on %s\n", path);
  206. ret = -ENOENT;
  207. goto error_brelse;
  208. }
  209. devid = le64_to_cpu(disk_super->dev_item.devid);
  210. printk("found device %Lu on %s\n", devid, path);
  211. ret = device_list_add(path, disk_super, devid, fs_devices_ret);
  212. error_brelse:
  213. brelse(bh);
  214. error_close:
  215. close_bdev_excl(bdev);
  216. printk("scan one closes bdev %s\n", path);
  217. error:
  218. mutex_unlock(&uuid_mutex);
  219. return ret;
  220. }
  221. /*
  222. * this uses a pretty simple search, the expectation is that it is
  223. * called very infrequently and that a given device has a small number
  224. * of extents
  225. */
  226. static int find_free_dev_extent(struct btrfs_trans_handle *trans,
  227. struct btrfs_device *device,
  228. struct btrfs_path *path,
  229. u64 num_bytes, u64 *start)
  230. {
  231. struct btrfs_key key;
  232. struct btrfs_root *root = device->dev_root;
  233. struct btrfs_dev_extent *dev_extent = NULL;
  234. u64 hole_size = 0;
  235. u64 last_byte = 0;
  236. u64 search_start = 0;
  237. u64 search_end = device->total_bytes;
  238. int ret;
  239. int slot = 0;
  240. int start_found;
  241. struct extent_buffer *l;
  242. start_found = 0;
  243. path->reada = 2;
  244. /* FIXME use last free of some kind */
  245. /* we don't want to overwrite the superblock on the drive,
  246. * so we make sure to start at an offset of at least 1MB
  247. */
  248. search_start = max((u64)1024 * 1024, search_start);
  249. key.objectid = device->devid;
  250. key.offset = search_start;
  251. key.type = BTRFS_DEV_EXTENT_KEY;
  252. ret = btrfs_search_slot(trans, root, &key, path, 0, 0);
  253. if (ret < 0)
  254. goto error;
  255. ret = btrfs_previous_item(root, path, 0, key.type);
  256. if (ret < 0)
  257. goto error;
  258. l = path->nodes[0];
  259. btrfs_item_key_to_cpu(l, &key, path->slots[0]);
  260. while (1) {
  261. l = path->nodes[0];
  262. slot = path->slots[0];
  263. if (slot >= btrfs_header_nritems(l)) {
  264. ret = btrfs_next_leaf(root, path);
  265. if (ret == 0)
  266. continue;
  267. if (ret < 0)
  268. goto error;
  269. no_more_items:
  270. if (!start_found) {
  271. if (search_start >= search_end) {
  272. ret = -ENOSPC;
  273. goto error;
  274. }
  275. *start = search_start;
  276. start_found = 1;
  277. goto check_pending;
  278. }
  279. *start = last_byte > search_start ?
  280. last_byte : search_start;
  281. if (search_end <= *start) {
  282. ret = -ENOSPC;
  283. goto error;
  284. }
  285. goto check_pending;
  286. }
  287. btrfs_item_key_to_cpu(l, &key, slot);
  288. if (key.objectid < device->devid)
  289. goto next;
  290. if (key.objectid > device->devid)
  291. goto no_more_items;
  292. if (key.offset >= search_start && key.offset > last_byte &&
  293. start_found) {
  294. if (last_byte < search_start)
  295. last_byte = search_start;
  296. hole_size = key.offset - last_byte;
  297. if (key.offset > last_byte &&
  298. hole_size >= num_bytes) {
  299. *start = last_byte;
  300. goto check_pending;
  301. }
  302. }
  303. if (btrfs_key_type(&key) != BTRFS_DEV_EXTENT_KEY) {
  304. goto next;
  305. }
  306. start_found = 1;
  307. dev_extent = btrfs_item_ptr(l, slot, struct btrfs_dev_extent);
  308. last_byte = key.offset + btrfs_dev_extent_length(l, dev_extent);
  309. next:
  310. path->slots[0]++;
  311. cond_resched();
  312. }
  313. check_pending:
  314. /* we have to make sure we didn't find an extent that has already
  315. * been allocated by the map tree or the original allocation
  316. */
  317. btrfs_release_path(root, path);
  318. BUG_ON(*start < search_start);
  319. if (*start + num_bytes > search_end) {
  320. ret = -ENOSPC;
  321. goto error;
  322. }
  323. /* check for pending inserts here */
  324. return 0;
  325. error:
  326. btrfs_release_path(root, path);
  327. return ret;
  328. }
  329. int btrfs_alloc_dev_extent(struct btrfs_trans_handle *trans,
  330. struct btrfs_device *device,
  331. u64 owner, u64 num_bytes, u64 *start)
  332. {
  333. int ret;
  334. struct btrfs_path *path;
  335. struct btrfs_root *root = device->dev_root;
  336. struct btrfs_dev_extent *extent;
  337. struct extent_buffer *leaf;
  338. struct btrfs_key key;
  339. path = btrfs_alloc_path();
  340. if (!path)
  341. return -ENOMEM;
  342. ret = find_free_dev_extent(trans, device, path, num_bytes, start);
  343. if (ret) {
  344. goto err;
  345. }
  346. key.objectid = device->devid;
  347. key.offset = *start;
  348. key.type = BTRFS_DEV_EXTENT_KEY;
  349. ret = btrfs_insert_empty_item(trans, root, path, &key,
  350. sizeof(*extent));
  351. BUG_ON(ret);
  352. leaf = path->nodes[0];
  353. extent = btrfs_item_ptr(leaf, path->slots[0],
  354. struct btrfs_dev_extent);
  355. btrfs_set_dev_extent_owner(leaf, extent, owner);
  356. btrfs_set_dev_extent_length(leaf, extent, num_bytes);
  357. btrfs_mark_buffer_dirty(leaf);
  358. err:
  359. btrfs_free_path(path);
  360. return ret;
  361. }
  362. static int find_next_chunk(struct btrfs_root *root, u64 *objectid)
  363. {
  364. struct btrfs_path *path;
  365. int ret;
  366. struct btrfs_key key;
  367. struct btrfs_key found_key;
  368. path = btrfs_alloc_path();
  369. BUG_ON(!path);
  370. key.objectid = (u64)-1;
  371. key.offset = (u64)-1;
  372. key.type = BTRFS_CHUNK_ITEM_KEY;
  373. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  374. if (ret < 0)
  375. goto error;
  376. BUG_ON(ret == 0);
  377. ret = btrfs_previous_item(root, path, 0, BTRFS_CHUNK_ITEM_KEY);
  378. if (ret) {
  379. *objectid = 0;
  380. } else {
  381. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  382. path->slots[0]);
  383. *objectid = found_key.objectid + found_key.offset;
  384. }
  385. ret = 0;
  386. error:
  387. btrfs_free_path(path);
  388. return ret;
  389. }
  390. static int find_next_devid(struct btrfs_root *root, struct btrfs_path *path,
  391. u64 *objectid)
  392. {
  393. int ret;
  394. struct btrfs_key key;
  395. struct btrfs_key found_key;
  396. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  397. key.type = BTRFS_DEV_ITEM_KEY;
  398. key.offset = (u64)-1;
  399. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  400. if (ret < 0)
  401. goto error;
  402. BUG_ON(ret == 0);
  403. ret = btrfs_previous_item(root, path, BTRFS_DEV_ITEMS_OBJECTID,
  404. BTRFS_DEV_ITEM_KEY);
  405. if (ret) {
  406. *objectid = 1;
  407. } else {
  408. btrfs_item_key_to_cpu(path->nodes[0], &found_key,
  409. path->slots[0]);
  410. *objectid = found_key.offset + 1;
  411. }
  412. ret = 0;
  413. error:
  414. btrfs_release_path(root, path);
  415. return ret;
  416. }
  417. /*
  418. * the device information is stored in the chunk root
  419. * the btrfs_device struct should be fully filled in
  420. */
  421. int btrfs_add_device(struct btrfs_trans_handle *trans,
  422. struct btrfs_root *root,
  423. struct btrfs_device *device)
  424. {
  425. int ret;
  426. struct btrfs_path *path;
  427. struct btrfs_dev_item *dev_item;
  428. struct extent_buffer *leaf;
  429. struct btrfs_key key;
  430. unsigned long ptr;
  431. u64 free_devid;
  432. root = root->fs_info->chunk_root;
  433. path = btrfs_alloc_path();
  434. if (!path)
  435. return -ENOMEM;
  436. ret = find_next_devid(root, path, &free_devid);
  437. if (ret)
  438. goto out;
  439. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  440. key.type = BTRFS_DEV_ITEM_KEY;
  441. key.offset = free_devid;
  442. ret = btrfs_insert_empty_item(trans, root, path, &key,
  443. sizeof(*dev_item));
  444. if (ret)
  445. goto out;
  446. leaf = path->nodes[0];
  447. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  448. device->devid = free_devid;
  449. btrfs_set_device_id(leaf, dev_item, device->devid);
  450. btrfs_set_device_type(leaf, dev_item, device->type);
  451. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  452. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  453. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  454. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  455. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  456. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  457. write_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
  458. btrfs_mark_buffer_dirty(leaf);
  459. ret = 0;
  460. out:
  461. btrfs_free_path(path);
  462. return ret;
  463. }
  464. int btrfs_update_device(struct btrfs_trans_handle *trans,
  465. struct btrfs_device *device)
  466. {
  467. int ret;
  468. struct btrfs_path *path;
  469. struct btrfs_root *root;
  470. struct btrfs_dev_item *dev_item;
  471. struct extent_buffer *leaf;
  472. struct btrfs_key key;
  473. root = device->dev_root->fs_info->chunk_root;
  474. path = btrfs_alloc_path();
  475. if (!path)
  476. return -ENOMEM;
  477. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  478. key.type = BTRFS_DEV_ITEM_KEY;
  479. key.offset = device->devid;
  480. ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
  481. if (ret < 0)
  482. goto out;
  483. if (ret > 0) {
  484. ret = -ENOENT;
  485. goto out;
  486. }
  487. leaf = path->nodes[0];
  488. dev_item = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_dev_item);
  489. btrfs_set_device_id(leaf, dev_item, device->devid);
  490. btrfs_set_device_type(leaf, dev_item, device->type);
  491. btrfs_set_device_io_align(leaf, dev_item, device->io_align);
  492. btrfs_set_device_io_width(leaf, dev_item, device->io_width);
  493. btrfs_set_device_sector_size(leaf, dev_item, device->sector_size);
  494. btrfs_set_device_total_bytes(leaf, dev_item, device->total_bytes);
  495. btrfs_set_device_bytes_used(leaf, dev_item, device->bytes_used);
  496. btrfs_mark_buffer_dirty(leaf);
  497. out:
  498. btrfs_free_path(path);
  499. return ret;
  500. }
  501. int btrfs_add_system_chunk(struct btrfs_trans_handle *trans,
  502. struct btrfs_root *root,
  503. struct btrfs_key *key,
  504. struct btrfs_chunk *chunk, int item_size)
  505. {
  506. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  507. struct btrfs_disk_key disk_key;
  508. u32 array_size;
  509. u8 *ptr;
  510. array_size = btrfs_super_sys_array_size(super_copy);
  511. if (array_size + item_size > BTRFS_SYSTEM_CHUNK_ARRAY_SIZE)
  512. return -EFBIG;
  513. ptr = super_copy->sys_chunk_array + array_size;
  514. btrfs_cpu_key_to_disk(&disk_key, key);
  515. memcpy(ptr, &disk_key, sizeof(disk_key));
  516. ptr += sizeof(disk_key);
  517. memcpy(ptr, chunk, item_size);
  518. item_size += sizeof(disk_key);
  519. btrfs_set_super_sys_array_size(super_copy, array_size + item_size);
  520. return 0;
  521. }
  522. int btrfs_alloc_chunk(struct btrfs_trans_handle *trans,
  523. struct btrfs_root *extent_root, u64 *start,
  524. u64 *num_bytes, u64 type)
  525. {
  526. u64 dev_offset;
  527. struct btrfs_root *chunk_root = extent_root->fs_info->chunk_root;
  528. struct btrfs_stripe *stripes;
  529. struct btrfs_device *device = NULL;
  530. struct btrfs_chunk *chunk;
  531. struct list_head private_devs;
  532. struct list_head *dev_list = &extent_root->fs_info->fs_devices->devices;
  533. struct list_head *cur;
  534. struct extent_map_tree *em_tree;
  535. struct map_lookup *map;
  536. struct extent_map *em;
  537. u64 physical;
  538. u64 calc_size = 1024 * 1024 * 1024;
  539. u64 avail;
  540. u64 max_avail = 0;
  541. int num_stripes = 1;
  542. int looped = 0;
  543. int ret;
  544. int index;
  545. struct btrfs_key key;
  546. if (list_empty(dev_list))
  547. return -ENOSPC;
  548. again:
  549. INIT_LIST_HEAD(&private_devs);
  550. cur = dev_list->next;
  551. index = 0;
  552. /* build a private list of devices we will allocate from */
  553. while(index < num_stripes) {
  554. device = list_entry(cur, struct btrfs_device, dev_list);
  555. avail = device->total_bytes - device->bytes_used;
  556. cur = cur->next;
  557. if (avail > max_avail)
  558. max_avail = avail;
  559. if (avail >= calc_size) {
  560. list_move_tail(&device->dev_list, &private_devs);
  561. index++;
  562. }
  563. if (cur == dev_list)
  564. break;
  565. }
  566. if (index < num_stripes) {
  567. list_splice(&private_devs, dev_list);
  568. if (!looped && max_avail > 0) {
  569. looped = 1;
  570. calc_size = max_avail;
  571. goto again;
  572. }
  573. return -ENOSPC;
  574. }
  575. ret = find_next_chunk(chunk_root, &key.objectid);
  576. if (ret)
  577. return ret;
  578. chunk = kmalloc(btrfs_chunk_item_size(num_stripes), GFP_NOFS);
  579. if (!chunk)
  580. return -ENOMEM;
  581. stripes = &chunk->stripe;
  582. *num_bytes = calc_size;
  583. index = 0;
  584. while(index < num_stripes) {
  585. BUG_ON(list_empty(&private_devs));
  586. cur = private_devs.next;
  587. device = list_entry(cur, struct btrfs_device, dev_list);
  588. list_move_tail(&device->dev_list, dev_list);
  589. ret = btrfs_alloc_dev_extent(trans, device,
  590. key.objectid,
  591. calc_size, &dev_offset);
  592. BUG_ON(ret);
  593. printk("alloc chunk size %Lu from dev %Lu\n", calc_size, device->devid);
  594. device->bytes_used += calc_size;
  595. ret = btrfs_update_device(trans, device);
  596. BUG_ON(ret);
  597. btrfs_set_stack_stripe_devid(stripes + index, device->devid);
  598. btrfs_set_stack_stripe_offset(stripes + index, dev_offset);
  599. physical = dev_offset;
  600. index++;
  601. }
  602. BUG_ON(!list_empty(&private_devs));
  603. /* key.objectid was set above */
  604. key.offset = *num_bytes;
  605. key.type = BTRFS_CHUNK_ITEM_KEY;
  606. btrfs_set_stack_chunk_owner(chunk, extent_root->root_key.objectid);
  607. btrfs_set_stack_chunk_stripe_len(chunk, 64 * 1024);
  608. btrfs_set_stack_chunk_type(chunk, type);
  609. btrfs_set_stack_chunk_num_stripes(chunk, num_stripes);
  610. btrfs_set_stack_chunk_io_align(chunk, extent_root->sectorsize);
  611. btrfs_set_stack_chunk_io_width(chunk, extent_root->sectorsize);
  612. btrfs_set_stack_chunk_sector_size(chunk, extent_root->sectorsize);
  613. ret = btrfs_insert_item(trans, chunk_root, &key, chunk,
  614. btrfs_chunk_item_size(num_stripes));
  615. BUG_ON(ret);
  616. *start = key.objectid;
  617. em = alloc_extent_map(GFP_NOFS);
  618. if (!em)
  619. return -ENOMEM;
  620. map = kmalloc(sizeof(*map), GFP_NOFS);
  621. if (!map) {
  622. free_extent_map(em);
  623. return -ENOMEM;
  624. }
  625. em->bdev = (struct block_device *)map;
  626. em->start = key.objectid;
  627. em->len = key.offset;
  628. em->block_start = 0;
  629. map->physical = physical;
  630. map->dev = device;
  631. if (!map->dev) {
  632. kfree(map);
  633. free_extent_map(em);
  634. return -EIO;
  635. }
  636. kfree(chunk);
  637. em_tree = &extent_root->fs_info->mapping_tree.map_tree;
  638. spin_lock(&em_tree->lock);
  639. ret = add_extent_mapping(em_tree, em);
  640. BUG_ON(ret);
  641. spin_unlock(&em_tree->lock);
  642. free_extent_map(em);
  643. return ret;
  644. }
  645. void btrfs_mapping_init(struct btrfs_mapping_tree *tree)
  646. {
  647. extent_map_tree_init(&tree->map_tree, GFP_NOFS);
  648. }
  649. void btrfs_mapping_tree_free(struct btrfs_mapping_tree *tree)
  650. {
  651. struct extent_map *em;
  652. while(1) {
  653. spin_lock(&tree->map_tree.lock);
  654. em = lookup_extent_mapping(&tree->map_tree, 0, (u64)-1);
  655. if (em)
  656. remove_extent_mapping(&tree->map_tree, em);
  657. spin_unlock(&tree->map_tree.lock);
  658. if (!em)
  659. break;
  660. kfree(em->bdev);
  661. /* once for us */
  662. free_extent_map(em);
  663. /* once for the tree */
  664. free_extent_map(em);
  665. }
  666. }
  667. int btrfs_map_block(struct btrfs_mapping_tree *map_tree,
  668. u64 logical, u64 *phys, u64 *length,
  669. struct btrfs_device **dev)
  670. {
  671. struct extent_map *em;
  672. struct map_lookup *map;
  673. struct extent_map_tree *em_tree = &map_tree->map_tree;
  674. u64 offset;
  675. spin_lock(&em_tree->lock);
  676. em = lookup_extent_mapping(em_tree, logical, *length);
  677. BUG_ON(!em);
  678. BUG_ON(em->start > logical || em->start + em->len < logical);
  679. map = (struct map_lookup *)em->bdev;
  680. offset = logical - em->start;
  681. *phys = map->physical + offset;
  682. *length = em->len - offset;
  683. *dev = map->dev;
  684. free_extent_map(em);
  685. spin_unlock(&em_tree->lock);
  686. return 0;
  687. }
  688. int btrfs_map_bio(struct btrfs_root *root, int rw, struct bio *bio)
  689. {
  690. struct btrfs_mapping_tree *map_tree;
  691. struct btrfs_device *dev;
  692. u64 logical = bio->bi_sector << 9;
  693. u64 physical;
  694. u64 length = 0;
  695. u64 map_length;
  696. struct bio_vec *bvec;
  697. int i;
  698. int ret;
  699. bio_for_each_segment(bvec, bio, i) {
  700. length += bvec->bv_len;
  701. }
  702. map_tree = &root->fs_info->mapping_tree;
  703. map_length = length;
  704. ret = btrfs_map_block(map_tree, logical, &physical, &map_length, &dev);
  705. if (map_length < length) {
  706. printk("mapping failed logical %Lu bio len %Lu physical %Lu "
  707. "len %Lu\n", logical, length, physical, map_length);
  708. BUG();
  709. }
  710. BUG_ON(map_length < length);
  711. bio->bi_sector = physical >> 9;
  712. bio->bi_bdev = dev->bdev;
  713. submit_bio(rw, bio);
  714. return 0;
  715. }
  716. struct btrfs_device *btrfs_find_device(struct btrfs_root *root, u64 devid)
  717. {
  718. struct list_head *head = &root->fs_info->fs_devices->devices;
  719. return __find_device(head, devid);
  720. }
  721. static int read_one_chunk(struct btrfs_root *root, struct btrfs_key *key,
  722. struct extent_buffer *leaf,
  723. struct btrfs_chunk *chunk)
  724. {
  725. struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
  726. struct map_lookup *map;
  727. struct extent_map *em;
  728. u64 logical;
  729. u64 length;
  730. u64 devid;
  731. int ret;
  732. logical = key->objectid;
  733. length = key->offset;
  734. spin_lock(&map_tree->map_tree.lock);
  735. em = lookup_extent_mapping(&map_tree->map_tree, logical, 1);
  736. /* already mapped? */
  737. if (em && em->start <= logical && em->start + em->len > logical) {
  738. free_extent_map(em);
  739. spin_unlock(&map_tree->map_tree.lock);
  740. return 0;
  741. } else if (em) {
  742. free_extent_map(em);
  743. }
  744. spin_unlock(&map_tree->map_tree.lock);
  745. map = kzalloc(sizeof(*map), GFP_NOFS);
  746. if (!map)
  747. return -ENOMEM;
  748. em = alloc_extent_map(GFP_NOFS);
  749. if (!em)
  750. return -ENOMEM;
  751. map = kmalloc(sizeof(*map), GFP_NOFS);
  752. if (!map) {
  753. free_extent_map(em);
  754. return -ENOMEM;
  755. }
  756. em->bdev = (struct block_device *)map;
  757. em->start = logical;
  758. em->len = length;
  759. em->block_start = 0;
  760. map->physical = btrfs_stripe_offset_nr(leaf, chunk, 0);
  761. devid = btrfs_stripe_devid_nr(leaf, chunk, 0);
  762. map->dev = btrfs_find_device(root, devid);
  763. if (!map->dev) {
  764. kfree(map);
  765. free_extent_map(em);
  766. return -EIO;
  767. }
  768. spin_lock(&map_tree->map_tree.lock);
  769. ret = add_extent_mapping(&map_tree->map_tree, em);
  770. BUG_ON(ret);
  771. spin_unlock(&map_tree->map_tree.lock);
  772. free_extent_map(em);
  773. return 0;
  774. }
  775. static int fill_device_from_item(struct extent_buffer *leaf,
  776. struct btrfs_dev_item *dev_item,
  777. struct btrfs_device *device)
  778. {
  779. unsigned long ptr;
  780. device->devid = btrfs_device_id(leaf, dev_item);
  781. device->total_bytes = btrfs_device_total_bytes(leaf, dev_item);
  782. device->bytes_used = btrfs_device_bytes_used(leaf, dev_item);
  783. device->type = btrfs_device_type(leaf, dev_item);
  784. device->io_align = btrfs_device_io_align(leaf, dev_item);
  785. device->io_width = btrfs_device_io_width(leaf, dev_item);
  786. device->sector_size = btrfs_device_sector_size(leaf, dev_item);
  787. ptr = (unsigned long)btrfs_device_uuid(dev_item);
  788. read_extent_buffer(leaf, device->uuid, ptr, BTRFS_DEV_UUID_SIZE);
  789. return 0;
  790. }
  791. static int read_one_dev(struct btrfs_root *root,
  792. struct extent_buffer *leaf,
  793. struct btrfs_dev_item *dev_item)
  794. {
  795. struct btrfs_device *device;
  796. u64 devid;
  797. int ret;
  798. devid = btrfs_device_id(leaf, dev_item);
  799. device = btrfs_find_device(root, devid);
  800. if (!device) {
  801. printk("warning devid %Lu not found already\n", devid);
  802. device = kmalloc(sizeof(*device), GFP_NOFS);
  803. if (!device)
  804. return -ENOMEM;
  805. list_add(&device->dev_list,
  806. &root->fs_info->fs_devices->devices);
  807. }
  808. fill_device_from_item(leaf, dev_item, device);
  809. device->dev_root = root->fs_info->dev_root;
  810. ret = 0;
  811. #if 0
  812. ret = btrfs_open_device(device);
  813. if (ret) {
  814. kfree(device);
  815. }
  816. #endif
  817. return ret;
  818. }
  819. int btrfs_read_super_device(struct btrfs_root *root, struct extent_buffer *buf)
  820. {
  821. struct btrfs_dev_item *dev_item;
  822. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  823. dev_item);
  824. return read_one_dev(root, buf, dev_item);
  825. }
  826. int btrfs_read_sys_array(struct btrfs_root *root)
  827. {
  828. struct btrfs_super_block *super_copy = &root->fs_info->super_copy;
  829. struct extent_buffer *sb = root->fs_info->sb_buffer;
  830. struct btrfs_disk_key *disk_key;
  831. struct btrfs_chunk *chunk;
  832. struct btrfs_key key;
  833. u32 num_stripes;
  834. u32 array_size;
  835. u32 len = 0;
  836. u8 *ptr;
  837. unsigned long sb_ptr;
  838. u32 cur;
  839. int ret;
  840. array_size = btrfs_super_sys_array_size(super_copy);
  841. /*
  842. * we do this loop twice, once for the device items and
  843. * once for all of the chunks. This way there are device
  844. * structs filled in for every chunk
  845. */
  846. ptr = super_copy->sys_chunk_array;
  847. sb_ptr = offsetof(struct btrfs_super_block, sys_chunk_array);
  848. cur = 0;
  849. while (cur < array_size) {
  850. disk_key = (struct btrfs_disk_key *)ptr;
  851. btrfs_disk_key_to_cpu(&key, disk_key);
  852. len = sizeof(*disk_key);
  853. ptr += len;
  854. sb_ptr += len;
  855. cur += len;
  856. if (key.type == BTRFS_CHUNK_ITEM_KEY) {
  857. chunk = (struct btrfs_chunk *)sb_ptr;
  858. ret = read_one_chunk(root, &key, sb, chunk);
  859. BUG_ON(ret);
  860. num_stripes = btrfs_chunk_num_stripes(sb, chunk);
  861. len = btrfs_chunk_item_size(num_stripes);
  862. } else {
  863. BUG();
  864. }
  865. ptr += len;
  866. sb_ptr += len;
  867. cur += len;
  868. }
  869. return 0;
  870. }
  871. int btrfs_read_chunk_tree(struct btrfs_root *root)
  872. {
  873. struct btrfs_path *path;
  874. struct extent_buffer *leaf;
  875. struct btrfs_key key;
  876. struct btrfs_key found_key;
  877. int ret;
  878. int slot;
  879. root = root->fs_info->chunk_root;
  880. path = btrfs_alloc_path();
  881. if (!path)
  882. return -ENOMEM;
  883. /* first we search for all of the device items, and then we
  884. * read in all of the chunk items. This way we can create chunk
  885. * mappings that reference all of the devices that are afound
  886. */
  887. key.objectid = BTRFS_DEV_ITEMS_OBJECTID;
  888. key.offset = 0;
  889. key.type = 0;
  890. again:
  891. ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
  892. while(1) {
  893. leaf = path->nodes[0];
  894. slot = path->slots[0];
  895. if (slot >= btrfs_header_nritems(leaf)) {
  896. ret = btrfs_next_leaf(root, path);
  897. if (ret == 0)
  898. continue;
  899. if (ret < 0)
  900. goto error;
  901. break;
  902. }
  903. btrfs_item_key_to_cpu(leaf, &found_key, slot);
  904. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  905. if (found_key.objectid != BTRFS_DEV_ITEMS_OBJECTID)
  906. break;
  907. if (found_key.type == BTRFS_DEV_ITEM_KEY) {
  908. struct btrfs_dev_item *dev_item;
  909. dev_item = btrfs_item_ptr(leaf, slot,
  910. struct btrfs_dev_item);
  911. ret = read_one_dev(root, leaf, dev_item);
  912. BUG_ON(ret);
  913. }
  914. } else if (found_key.type == BTRFS_CHUNK_ITEM_KEY) {
  915. struct btrfs_chunk *chunk;
  916. chunk = btrfs_item_ptr(leaf, slot, struct btrfs_chunk);
  917. ret = read_one_chunk(root, &found_key, leaf, chunk);
  918. }
  919. path->slots[0]++;
  920. }
  921. if (key.objectid == BTRFS_DEV_ITEMS_OBJECTID) {
  922. key.objectid = 0;
  923. btrfs_release_path(root, path);
  924. goto again;
  925. }
  926. btrfs_free_path(path);
  927. ret = 0;
  928. error:
  929. return ret;
  930. }