volumes.c 44 KB

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