volumes.c 37 KB

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