volumes.c 31 KB

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