disk-io.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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/fs.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/crc32c.h>
  21. #include <linux/scatterlist.h>
  22. #include <linux/swap.h>
  23. #include <linux/radix-tree.h>
  24. #include <linux/writeback.h>
  25. #include <linux/buffer_head.h> // for block_sync_page
  26. #include "ctree.h"
  27. #include "disk-io.h"
  28. #include "transaction.h"
  29. #include "btrfs_inode.h"
  30. #include "volumes.h"
  31. #include "print-tree.h"
  32. #if 0
  33. static int check_tree_block(struct btrfs_root *root, struct extent_buffer *buf)
  34. {
  35. if (extent_buffer_blocknr(buf) != btrfs_header_blocknr(buf)) {
  36. printk(KERN_CRIT "buf blocknr(buf) is %llu, header is %llu\n",
  37. (unsigned long long)extent_buffer_blocknr(buf),
  38. (unsigned long long)btrfs_header_blocknr(buf));
  39. return 1;
  40. }
  41. return 0;
  42. }
  43. #endif
  44. static struct extent_io_ops btree_extent_io_ops;
  45. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  46. u64 bytenr, u32 blocksize)
  47. {
  48. struct inode *btree_inode = root->fs_info->btree_inode;
  49. struct extent_buffer *eb;
  50. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  51. bytenr, blocksize, GFP_NOFS);
  52. return eb;
  53. }
  54. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  55. u64 bytenr, u32 blocksize)
  56. {
  57. struct inode *btree_inode = root->fs_info->btree_inode;
  58. struct extent_buffer *eb;
  59. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  60. bytenr, blocksize, NULL, GFP_NOFS);
  61. return eb;
  62. }
  63. struct extent_map *btree_get_extent(struct inode *inode, struct page *page,
  64. size_t page_offset, u64 start, u64 len,
  65. int create)
  66. {
  67. struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
  68. struct extent_map *em;
  69. int ret;
  70. again:
  71. spin_lock(&em_tree->lock);
  72. em = lookup_extent_mapping(em_tree, start, len);
  73. spin_unlock(&em_tree->lock);
  74. if (em) {
  75. goto out;
  76. }
  77. em = alloc_extent_map(GFP_NOFS);
  78. if (!em) {
  79. em = ERR_PTR(-ENOMEM);
  80. goto out;
  81. }
  82. em->start = 0;
  83. em->len = i_size_read(inode);
  84. em->block_start = 0;
  85. em->bdev = inode->i_sb->s_bdev;
  86. spin_lock(&em_tree->lock);
  87. ret = add_extent_mapping(em_tree, em);
  88. spin_unlock(&em_tree->lock);
  89. if (ret == -EEXIST) {
  90. free_extent_map(em);
  91. em = NULL;
  92. goto again;
  93. } else if (ret) {
  94. em = ERR_PTR(ret);
  95. }
  96. out:
  97. return em;
  98. }
  99. u32 btrfs_csum_data(struct btrfs_root *root, char *data, u32 seed, size_t len)
  100. {
  101. return crc32c(seed, data, len);
  102. }
  103. void btrfs_csum_final(u32 crc, char *result)
  104. {
  105. *(__le32 *)result = ~cpu_to_le32(crc);
  106. }
  107. static int csum_tree_block(struct btrfs_root *root, struct extent_buffer *buf,
  108. int verify)
  109. {
  110. char result[BTRFS_CRC32_SIZE];
  111. unsigned long len;
  112. unsigned long cur_len;
  113. unsigned long offset = BTRFS_CSUM_SIZE;
  114. char *map_token = NULL;
  115. char *kaddr;
  116. unsigned long map_start;
  117. unsigned long map_len;
  118. int err;
  119. u32 crc = ~(u32)0;
  120. len = buf->len - offset;
  121. while(len > 0) {
  122. err = map_private_extent_buffer(buf, offset, 32,
  123. &map_token, &kaddr,
  124. &map_start, &map_len, KM_USER0);
  125. if (err) {
  126. printk("failed to map extent buffer! %lu\n",
  127. offset);
  128. return 1;
  129. }
  130. cur_len = min(len, map_len - (offset - map_start));
  131. crc = btrfs_csum_data(root, kaddr + offset - map_start,
  132. crc, cur_len);
  133. len -= cur_len;
  134. offset += cur_len;
  135. unmap_extent_buffer(buf, map_token, KM_USER0);
  136. }
  137. btrfs_csum_final(crc, result);
  138. if (verify) {
  139. int from_this_trans = 0;
  140. if (root->fs_info->running_transaction &&
  141. btrfs_header_generation(buf) ==
  142. root->fs_info->running_transaction->transid)
  143. from_this_trans = 1;
  144. /* FIXME, this is not good */
  145. if (from_this_trans == 0 &&
  146. memcmp_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE)) {
  147. u32 val;
  148. u32 found = 0;
  149. memcpy(&found, result, BTRFS_CRC32_SIZE);
  150. read_extent_buffer(buf, &val, 0, BTRFS_CRC32_SIZE);
  151. printk("btrfs: %s checksum verify failed on %llu "
  152. "wanted %X found %X from_this_trans %d\n",
  153. root->fs_info->sb->s_id,
  154. buf->start, val, found, from_this_trans);
  155. return 1;
  156. }
  157. } else {
  158. write_extent_buffer(buf, result, 0, BTRFS_CRC32_SIZE);
  159. }
  160. return 0;
  161. }
  162. int csum_dirty_buffer(struct btrfs_root *root, struct page *page)
  163. {
  164. struct extent_io_tree *tree;
  165. u64 start = (u64)page->index << PAGE_CACHE_SHIFT;
  166. u64 found_start;
  167. int found_level;
  168. unsigned long len;
  169. struct extent_buffer *eb;
  170. tree = &BTRFS_I(page->mapping->host)->io_tree;
  171. if (page->private == EXTENT_PAGE_PRIVATE)
  172. goto out;
  173. if (!page->private)
  174. goto out;
  175. len = page->private >> 2;
  176. if (len == 0) {
  177. WARN_ON(1);
  178. }
  179. eb = alloc_extent_buffer(tree, start, len, page, GFP_NOFS);
  180. read_extent_buffer_pages(tree, eb, start + PAGE_CACHE_SIZE, 1,
  181. btree_get_extent);
  182. btrfs_clear_buffer_defrag(eb);
  183. found_start = btrfs_header_bytenr(eb);
  184. if (found_start != start) {
  185. printk("warning: eb start incorrect %Lu buffer %Lu len %lu\n",
  186. start, found_start, len);
  187. WARN_ON(1);
  188. goto err;
  189. }
  190. if (eb->first_page != page) {
  191. printk("bad first page %lu %lu\n", eb->first_page->index,
  192. page->index);
  193. WARN_ON(1);
  194. goto err;
  195. }
  196. if (!PageUptodate(page)) {
  197. printk("csum not up to date page %lu\n", page->index);
  198. WARN_ON(1);
  199. goto err;
  200. }
  201. found_level = btrfs_header_level(eb);
  202. csum_tree_block(root, eb, 0);
  203. err:
  204. free_extent_buffer(eb);
  205. out:
  206. return 0;
  207. }
  208. static int btree_writepage_io_hook(struct page *page, u64 start, u64 end)
  209. {
  210. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  211. csum_dirty_buffer(root, page);
  212. return 0;
  213. }
  214. static int btree_submit_bio_hook(struct inode *inode, int rw, struct bio *bio)
  215. {
  216. struct btrfs_root *root = BTRFS_I(inode)->root;
  217. u64 offset;
  218. offset = bio->bi_sector << 9;
  219. if (offset == BTRFS_SUPER_INFO_OFFSET) {
  220. bio->bi_bdev = root->fs_info->sb->s_bdev;
  221. submit_bio(rw, bio);
  222. return 0;
  223. }
  224. return btrfs_map_bio(BTRFS_I(inode)->root, rw, bio);
  225. }
  226. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  227. {
  228. struct extent_io_tree *tree;
  229. tree = &BTRFS_I(page->mapping->host)->io_tree;
  230. return extent_write_full_page(tree, page, btree_get_extent, wbc);
  231. }
  232. static int btree_writepages(struct address_space *mapping,
  233. struct writeback_control *wbc)
  234. {
  235. struct extent_io_tree *tree;
  236. tree = &BTRFS_I(mapping->host)->io_tree;
  237. if (wbc->sync_mode == WB_SYNC_NONE) {
  238. u64 num_dirty;
  239. u64 start = 0;
  240. unsigned long thresh = 96 * 1024 * 1024;
  241. if (wbc->for_kupdate)
  242. return 0;
  243. if (current_is_pdflush()) {
  244. thresh = 96 * 1024 * 1024;
  245. } else {
  246. thresh = 8 * 1024 * 1024;
  247. }
  248. num_dirty = count_range_bits(tree, &start, (u64)-1,
  249. thresh, EXTENT_DIRTY);
  250. if (num_dirty < thresh) {
  251. return 0;
  252. }
  253. }
  254. return extent_writepages(tree, mapping, btree_get_extent, wbc);
  255. }
  256. int btree_readpage(struct file *file, struct page *page)
  257. {
  258. struct extent_io_tree *tree;
  259. tree = &BTRFS_I(page->mapping->host)->io_tree;
  260. return extent_read_full_page(tree, page, btree_get_extent);
  261. }
  262. static int btree_releasepage(struct page *page, gfp_t gfp_flags)
  263. {
  264. struct extent_io_tree *tree;
  265. struct extent_map_tree *map;
  266. int ret;
  267. tree = &BTRFS_I(page->mapping->host)->io_tree;
  268. map = &BTRFS_I(page->mapping->host)->extent_tree;
  269. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  270. if (ret == 1) {
  271. ClearPagePrivate(page);
  272. set_page_private(page, 0);
  273. page_cache_release(page);
  274. }
  275. return ret;
  276. }
  277. static void btree_invalidatepage(struct page *page, unsigned long offset)
  278. {
  279. struct extent_io_tree *tree;
  280. tree = &BTRFS_I(page->mapping->host)->io_tree;
  281. extent_invalidatepage(tree, page, offset);
  282. btree_releasepage(page, GFP_NOFS);
  283. }
  284. #if 0
  285. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  286. {
  287. struct buffer_head *bh;
  288. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  289. struct buffer_head *head;
  290. if (!page_has_buffers(page)) {
  291. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  292. (1 << BH_Dirty)|(1 << BH_Uptodate));
  293. }
  294. head = page_buffers(page);
  295. bh = head;
  296. do {
  297. if (buffer_dirty(bh))
  298. csum_tree_block(root, bh, 0);
  299. bh = bh->b_this_page;
  300. } while (bh != head);
  301. return block_write_full_page(page, btree_get_block, wbc);
  302. }
  303. #endif
  304. static struct address_space_operations btree_aops = {
  305. .readpage = btree_readpage,
  306. .writepage = btree_writepage,
  307. .writepages = btree_writepages,
  308. .releasepage = btree_releasepage,
  309. .invalidatepage = btree_invalidatepage,
  310. .sync_page = block_sync_page,
  311. };
  312. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
  313. {
  314. struct extent_buffer *buf = NULL;
  315. struct inode *btree_inode = root->fs_info->btree_inode;
  316. int ret = 0;
  317. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  318. if (!buf)
  319. return 0;
  320. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  321. buf, 0, 0, btree_get_extent);
  322. free_extent_buffer(buf);
  323. return ret;
  324. }
  325. static int close_all_devices(struct btrfs_fs_info *fs_info)
  326. {
  327. struct list_head *list;
  328. struct list_head *next;
  329. struct btrfs_device *device;
  330. list = &fs_info->devices;
  331. while(!list_empty(list)) {
  332. next = list->next;
  333. list_del(next);
  334. device = list_entry(next, struct btrfs_device, dev_list);
  335. kfree(device->name);
  336. kfree(device);
  337. }
  338. return 0;
  339. }
  340. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  341. u32 blocksize)
  342. {
  343. struct extent_buffer *buf = NULL;
  344. struct inode *btree_inode = root->fs_info->btree_inode;
  345. struct extent_io_tree *io_tree;
  346. u64 end;
  347. int ret;
  348. io_tree = &BTRFS_I(btree_inode)->io_tree;
  349. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  350. if (!buf)
  351. return NULL;
  352. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
  353. btree_get_extent);
  354. if (buf->flags & EXTENT_CSUM)
  355. return buf;
  356. end = buf->start + PAGE_CACHE_SIZE - 1;
  357. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  358. buf->flags |= EXTENT_CSUM;
  359. return buf;
  360. }
  361. lock_extent(io_tree, buf->start, end, GFP_NOFS);
  362. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  363. buf->flags |= EXTENT_CSUM;
  364. goto out_unlock;
  365. }
  366. ret = csum_tree_block(root, buf, 1);
  367. set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
  368. buf->flags |= EXTENT_CSUM;
  369. out_unlock:
  370. unlock_extent(io_tree, buf->start, end, GFP_NOFS);
  371. return buf;
  372. }
  373. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  374. struct extent_buffer *buf)
  375. {
  376. struct inode *btree_inode = root->fs_info->btree_inode;
  377. if (btrfs_header_generation(buf) ==
  378. root->fs_info->running_transaction->transid)
  379. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  380. buf);
  381. return 0;
  382. }
  383. int wait_on_tree_block_writeback(struct btrfs_root *root,
  384. struct extent_buffer *buf)
  385. {
  386. struct inode *btree_inode = root->fs_info->btree_inode;
  387. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  388. buf);
  389. return 0;
  390. }
  391. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  392. u32 stripesize, struct btrfs_root *root,
  393. struct btrfs_fs_info *fs_info,
  394. u64 objectid)
  395. {
  396. root->node = NULL;
  397. root->inode = NULL;
  398. root->commit_root = NULL;
  399. root->sectorsize = sectorsize;
  400. root->nodesize = nodesize;
  401. root->leafsize = leafsize;
  402. root->stripesize = stripesize;
  403. root->ref_cows = 0;
  404. root->track_dirty = 0;
  405. root->fs_info = fs_info;
  406. root->objectid = objectid;
  407. root->last_trans = 0;
  408. root->highest_inode = 0;
  409. root->last_inode_alloc = 0;
  410. root->name = NULL;
  411. root->in_sysfs = 0;
  412. INIT_LIST_HEAD(&root->dirty_list);
  413. memset(&root->root_key, 0, sizeof(root->root_key));
  414. memset(&root->root_item, 0, sizeof(root->root_item));
  415. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  416. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  417. init_completion(&root->kobj_unregister);
  418. root->defrag_running = 0;
  419. root->defrag_level = 0;
  420. root->root_key.objectid = objectid;
  421. return 0;
  422. }
  423. static int find_and_setup_root(struct btrfs_root *tree_root,
  424. struct btrfs_fs_info *fs_info,
  425. u64 objectid,
  426. struct btrfs_root *root)
  427. {
  428. int ret;
  429. u32 blocksize;
  430. __setup_root(tree_root->nodesize, tree_root->leafsize,
  431. tree_root->sectorsize, tree_root->stripesize,
  432. root, fs_info, objectid);
  433. ret = btrfs_find_last_root(tree_root, objectid,
  434. &root->root_item, &root->root_key);
  435. BUG_ON(ret);
  436. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  437. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  438. blocksize);
  439. BUG_ON(!root->node);
  440. return 0;
  441. }
  442. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  443. struct btrfs_key *location)
  444. {
  445. struct btrfs_root *root;
  446. struct btrfs_root *tree_root = fs_info->tree_root;
  447. struct btrfs_path *path;
  448. struct extent_buffer *l;
  449. u64 highest_inode;
  450. u32 blocksize;
  451. int ret = 0;
  452. root = kzalloc(sizeof(*root), GFP_NOFS);
  453. if (!root)
  454. return ERR_PTR(-ENOMEM);
  455. if (location->offset == (u64)-1) {
  456. ret = find_and_setup_root(tree_root, fs_info,
  457. location->objectid, root);
  458. if (ret) {
  459. kfree(root);
  460. return ERR_PTR(ret);
  461. }
  462. goto insert;
  463. }
  464. __setup_root(tree_root->nodesize, tree_root->leafsize,
  465. tree_root->sectorsize, tree_root->stripesize,
  466. root, fs_info, location->objectid);
  467. path = btrfs_alloc_path();
  468. BUG_ON(!path);
  469. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  470. if (ret != 0) {
  471. if (ret > 0)
  472. ret = -ENOENT;
  473. goto out;
  474. }
  475. l = path->nodes[0];
  476. read_extent_buffer(l, &root->root_item,
  477. btrfs_item_ptr_offset(l, path->slots[0]),
  478. sizeof(root->root_item));
  479. memcpy(&root->root_key, location, sizeof(*location));
  480. ret = 0;
  481. out:
  482. btrfs_release_path(root, path);
  483. btrfs_free_path(path);
  484. if (ret) {
  485. kfree(root);
  486. return ERR_PTR(ret);
  487. }
  488. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  489. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  490. blocksize);
  491. BUG_ON(!root->node);
  492. insert:
  493. root->ref_cows = 1;
  494. ret = btrfs_find_highest_inode(root, &highest_inode);
  495. if (ret == 0) {
  496. root->highest_inode = highest_inode;
  497. root->last_inode_alloc = highest_inode;
  498. }
  499. return root;
  500. }
  501. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  502. u64 root_objectid)
  503. {
  504. struct btrfs_root *root;
  505. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  506. return fs_info->tree_root;
  507. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  508. return fs_info->extent_root;
  509. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  510. (unsigned long)root_objectid);
  511. return root;
  512. }
  513. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  514. struct btrfs_key *location)
  515. {
  516. struct btrfs_root *root;
  517. int ret;
  518. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  519. return fs_info->tree_root;
  520. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  521. return fs_info->extent_root;
  522. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  523. (unsigned long)location->objectid);
  524. if (root)
  525. return root;
  526. root = btrfs_read_fs_root_no_radix(fs_info, location);
  527. if (IS_ERR(root))
  528. return root;
  529. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  530. (unsigned long)root->root_key.objectid,
  531. root);
  532. if (ret) {
  533. free_extent_buffer(root->node);
  534. kfree(root);
  535. return ERR_PTR(ret);
  536. }
  537. ret = btrfs_find_dead_roots(fs_info->tree_root,
  538. root->root_key.objectid, root);
  539. BUG_ON(ret);
  540. return root;
  541. }
  542. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  543. struct btrfs_key *location,
  544. const char *name, int namelen)
  545. {
  546. struct btrfs_root *root;
  547. int ret;
  548. root = btrfs_read_fs_root_no_name(fs_info, location);
  549. if (!root)
  550. return NULL;
  551. if (root->in_sysfs)
  552. return root;
  553. ret = btrfs_set_root_name(root, name, namelen);
  554. if (ret) {
  555. free_extent_buffer(root->node);
  556. kfree(root);
  557. return ERR_PTR(ret);
  558. }
  559. ret = btrfs_sysfs_add_root(root);
  560. if (ret) {
  561. free_extent_buffer(root->node);
  562. kfree(root->name);
  563. kfree(root);
  564. return ERR_PTR(ret);
  565. }
  566. root->in_sysfs = 1;
  567. return root;
  568. }
  569. #if 0
  570. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  571. struct btrfs_hasher *hasher;
  572. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  573. if (!hasher)
  574. return -ENOMEM;
  575. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  576. if (!hasher->hash_tfm) {
  577. kfree(hasher);
  578. return -EINVAL;
  579. }
  580. spin_lock(&info->hash_lock);
  581. list_add(&hasher->list, &info->hashers);
  582. spin_unlock(&info->hash_lock);
  583. return 0;
  584. }
  585. #endif
  586. struct btrfs_root *open_ctree(struct super_block *sb)
  587. {
  588. u32 sectorsize;
  589. u32 nodesize;
  590. u32 leafsize;
  591. u32 blocksize;
  592. u32 stripesize;
  593. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  594. GFP_NOFS);
  595. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  596. GFP_NOFS);
  597. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  598. GFP_NOFS);
  599. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  600. GFP_NOFS);
  601. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  602. GFP_NOFS);
  603. int ret;
  604. int err = -EIO;
  605. struct btrfs_super_block *disk_super;
  606. if (!extent_root || !tree_root || !fs_info) {
  607. err = -ENOMEM;
  608. goto fail;
  609. }
  610. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  611. INIT_LIST_HEAD(&fs_info->trans_list);
  612. INIT_LIST_HEAD(&fs_info->dead_roots);
  613. INIT_LIST_HEAD(&fs_info->hashers);
  614. spin_lock_init(&fs_info->hash_lock);
  615. spin_lock_init(&fs_info->delalloc_lock);
  616. spin_lock_init(&fs_info->new_trans_lock);
  617. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  618. init_completion(&fs_info->kobj_unregister);
  619. sb_set_blocksize(sb, 4096);
  620. fs_info->running_transaction = NULL;
  621. fs_info->last_trans_committed = 0;
  622. fs_info->tree_root = tree_root;
  623. fs_info->extent_root = extent_root;
  624. fs_info->chunk_root = chunk_root;
  625. fs_info->dev_root = dev_root;
  626. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  627. INIT_LIST_HEAD(&fs_info->devices);
  628. INIT_LIST_HEAD(&fs_info->space_info);
  629. btrfs_mapping_init(&fs_info->mapping_tree);
  630. fs_info->sb = sb;
  631. fs_info->throttles = 0;
  632. fs_info->mount_opt = 0;
  633. fs_info->max_extent = (u64)-1;
  634. fs_info->max_inline = 8192 * 1024;
  635. fs_info->delalloc_bytes = 0;
  636. fs_info->btree_inode = new_inode(sb);
  637. fs_info->btree_inode->i_ino = 1;
  638. fs_info->btree_inode->i_nlink = 1;
  639. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  640. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  641. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  642. fs_info->btree_inode->i_mapping,
  643. GFP_NOFS);
  644. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  645. GFP_NOFS);
  646. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  647. extent_io_tree_init(&fs_info->free_space_cache,
  648. fs_info->btree_inode->i_mapping, GFP_NOFS);
  649. extent_io_tree_init(&fs_info->block_group_cache,
  650. fs_info->btree_inode->i_mapping, GFP_NOFS);
  651. extent_io_tree_init(&fs_info->pinned_extents,
  652. fs_info->btree_inode->i_mapping, GFP_NOFS);
  653. extent_io_tree_init(&fs_info->pending_del,
  654. fs_info->btree_inode->i_mapping, GFP_NOFS);
  655. extent_io_tree_init(&fs_info->extent_ins,
  656. fs_info->btree_inode->i_mapping, GFP_NOFS);
  657. fs_info->do_barriers = 1;
  658. fs_info->closing = 0;
  659. fs_info->total_pinned = 0;
  660. fs_info->last_alloc = 0;
  661. fs_info->last_data_alloc = 0;
  662. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  663. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  664. #else
  665. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  666. #endif
  667. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  668. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  669. sizeof(struct btrfs_key));
  670. insert_inode_hash(fs_info->btree_inode);
  671. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  672. mutex_init(&fs_info->trans_mutex);
  673. mutex_init(&fs_info->fs_mutex);
  674. #if 0
  675. ret = add_hasher(fs_info, "crc32c");
  676. if (ret) {
  677. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  678. err = -ENOMEM;
  679. goto fail_iput;
  680. }
  681. #endif
  682. __setup_root(4096, 4096, 4096, 4096, tree_root,
  683. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  684. fs_info->sb_buffer = read_tree_block(tree_root,
  685. BTRFS_SUPER_INFO_OFFSET,
  686. 4096);
  687. if (!fs_info->sb_buffer)
  688. goto fail_iput;
  689. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  690. sizeof(fs_info->super_copy));
  691. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  692. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  693. BTRFS_FSID_SIZE);
  694. disk_super = &fs_info->super_copy;
  695. if (!btrfs_super_root(disk_super))
  696. goto fail_sb_buffer;
  697. nodesize = btrfs_super_nodesize(disk_super);
  698. leafsize = btrfs_super_leafsize(disk_super);
  699. sectorsize = btrfs_super_sectorsize(disk_super);
  700. stripesize = btrfs_super_stripesize(disk_super);
  701. tree_root->nodesize = nodesize;
  702. tree_root->leafsize = leafsize;
  703. tree_root->sectorsize = sectorsize;
  704. tree_root->stripesize = stripesize;
  705. sb_set_blocksize(sb, sectorsize);
  706. i_size_write(fs_info->btree_inode,
  707. btrfs_super_total_bytes(disk_super));
  708. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  709. sizeof(disk_super->magic))) {
  710. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  711. goto fail_sb_buffer;
  712. }
  713. mutex_lock(&fs_info->fs_mutex);
  714. ret = btrfs_read_sys_array(tree_root);
  715. BUG_ON(ret);
  716. blocksize = btrfs_level_size(tree_root,
  717. btrfs_super_chunk_root_level(disk_super));
  718. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  719. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  720. chunk_root->node = read_tree_block(chunk_root,
  721. btrfs_super_chunk_root(disk_super),
  722. blocksize);
  723. BUG_ON(!chunk_root->node);
  724. ret = btrfs_read_chunk_tree(chunk_root);
  725. BUG_ON(ret);
  726. blocksize = btrfs_level_size(tree_root,
  727. btrfs_super_root_level(disk_super));
  728. tree_root->node = read_tree_block(tree_root,
  729. btrfs_super_root(disk_super),
  730. blocksize);
  731. if (!tree_root->node)
  732. goto fail_sb_buffer;
  733. ret = find_and_setup_root(tree_root, fs_info,
  734. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  735. if (ret)
  736. goto fail_tree_root;
  737. extent_root->track_dirty = 1;
  738. ret = find_and_setup_root(tree_root, fs_info,
  739. BTRFS_DEV_TREE_OBJECTID, dev_root);
  740. dev_root->track_dirty = 1;
  741. if (ret)
  742. goto fail_extent_root;
  743. btrfs_read_block_groups(extent_root);
  744. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  745. mutex_unlock(&fs_info->fs_mutex);
  746. return tree_root;
  747. fail_extent_root:
  748. free_extent_buffer(extent_root->node);
  749. fail_tree_root:
  750. mutex_unlock(&fs_info->fs_mutex);
  751. free_extent_buffer(tree_root->node);
  752. fail_sb_buffer:
  753. free_extent_buffer(fs_info->sb_buffer);
  754. fail_iput:
  755. iput(fs_info->btree_inode);
  756. fail:
  757. kfree(extent_root);
  758. kfree(tree_root);
  759. kfree(fs_info);
  760. return ERR_PTR(err);
  761. }
  762. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  763. *root)
  764. {
  765. int ret;
  766. struct extent_buffer *super = root->fs_info->sb_buffer;
  767. struct inode *btree_inode = root->fs_info->btree_inode;
  768. struct super_block *sb = root->fs_info->sb;
  769. if (!btrfs_test_opt(root, NOBARRIER))
  770. blkdev_issue_flush(sb->s_bdev, NULL);
  771. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  772. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  773. super->start, super->len);
  774. if (!btrfs_test_opt(root, NOBARRIER))
  775. blkdev_issue_flush(sb->s_bdev, NULL);
  776. return ret;
  777. }
  778. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  779. {
  780. radix_tree_delete(&fs_info->fs_roots_radix,
  781. (unsigned long)root->root_key.objectid);
  782. if (root->in_sysfs)
  783. btrfs_sysfs_del_root(root);
  784. if (root->inode)
  785. iput(root->inode);
  786. if (root->node)
  787. free_extent_buffer(root->node);
  788. if (root->commit_root)
  789. free_extent_buffer(root->commit_root);
  790. if (root->name)
  791. kfree(root->name);
  792. kfree(root);
  793. return 0;
  794. }
  795. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  796. {
  797. int ret;
  798. struct btrfs_root *gang[8];
  799. int i;
  800. while(1) {
  801. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  802. (void **)gang, 0,
  803. ARRAY_SIZE(gang));
  804. if (!ret)
  805. break;
  806. for (i = 0; i < ret; i++)
  807. btrfs_free_fs_root(fs_info, gang[i]);
  808. }
  809. return 0;
  810. }
  811. int close_ctree(struct btrfs_root *root)
  812. {
  813. int ret;
  814. struct btrfs_trans_handle *trans;
  815. struct btrfs_fs_info *fs_info = root->fs_info;
  816. fs_info->closing = 1;
  817. btrfs_transaction_flush_work(root);
  818. mutex_lock(&fs_info->fs_mutex);
  819. btrfs_defrag_dirty_roots(root->fs_info);
  820. trans = btrfs_start_transaction(root, 1);
  821. ret = btrfs_commit_transaction(trans, root);
  822. /* run commit again to drop the original snapshot */
  823. trans = btrfs_start_transaction(root, 1);
  824. btrfs_commit_transaction(trans, root);
  825. ret = btrfs_write_and_wait_transaction(NULL, root);
  826. BUG_ON(ret);
  827. write_ctree_super(NULL, root);
  828. mutex_unlock(&fs_info->fs_mutex);
  829. if (fs_info->delalloc_bytes) {
  830. printk("btrfs: at unmount delalloc count %Lu\n",
  831. fs_info->delalloc_bytes);
  832. }
  833. if (fs_info->extent_root->node)
  834. free_extent_buffer(fs_info->extent_root->node);
  835. if (fs_info->tree_root->node)
  836. free_extent_buffer(fs_info->tree_root->node);
  837. if (root->fs_info->chunk_root->node);
  838. free_extent_buffer(root->fs_info->chunk_root->node);
  839. if (root->fs_info->dev_root->node);
  840. free_extent_buffer(root->fs_info->dev_root->node);
  841. free_extent_buffer(fs_info->sb_buffer);
  842. btrfs_free_block_groups(root->fs_info);
  843. del_fs_roots(fs_info);
  844. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  845. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  846. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  847. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  848. extent_io_tree_empty_lru(&fs_info->pending_del);
  849. extent_io_tree_empty_lru(&fs_info->extent_ins);
  850. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  851. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  852. iput(fs_info->btree_inode);
  853. #if 0
  854. while(!list_empty(&fs_info->hashers)) {
  855. struct btrfs_hasher *hasher;
  856. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  857. hashers);
  858. list_del(&hasher->hashers);
  859. crypto_free_hash(&fs_info->hash_tfm);
  860. kfree(hasher);
  861. }
  862. #endif
  863. close_all_devices(fs_info);
  864. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  865. kfree(fs_info->extent_root);
  866. kfree(fs_info->tree_root);
  867. kfree(fs_info->chunk_root);
  868. kfree(fs_info->dev_root);
  869. return 0;
  870. }
  871. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  872. {
  873. struct inode *btree_inode = buf->first_page->mapping->host;
  874. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  875. }
  876. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  877. {
  878. struct inode *btree_inode = buf->first_page->mapping->host;
  879. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  880. buf);
  881. }
  882. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  883. {
  884. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  885. u64 transid = btrfs_header_generation(buf);
  886. struct inode *btree_inode = root->fs_info->btree_inode;
  887. if (transid != root->fs_info->generation) {
  888. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  889. (unsigned long long)buf->start,
  890. transid, root->fs_info->generation);
  891. WARN_ON(1);
  892. }
  893. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  894. }
  895. void btrfs_throttle(struct btrfs_root *root)
  896. {
  897. struct backing_dev_info *bdi;
  898. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  899. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  900. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  901. congestion_wait(WRITE, HZ/20);
  902. #else
  903. blk_congestion_wait(WRITE, HZ/20);
  904. #endif
  905. }
  906. }
  907. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  908. {
  909. balance_dirty_pages_ratelimited_nr(
  910. root->fs_info->btree_inode->i_mapping, 1);
  911. }
  912. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  913. {
  914. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  915. struct inode *btree_inode = root->fs_info->btree_inode;
  916. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  917. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  918. }
  919. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  920. {
  921. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  922. struct inode *btree_inode = root->fs_info->btree_inode;
  923. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  924. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  925. GFP_NOFS);
  926. }
  927. int btrfs_buffer_defrag(struct extent_buffer *buf)
  928. {
  929. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  930. struct inode *btree_inode = root->fs_info->btree_inode;
  931. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  932. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  933. }
  934. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  935. {
  936. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  937. struct inode *btree_inode = root->fs_info->btree_inode;
  938. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  939. buf->start, buf->start + buf->len - 1,
  940. EXTENT_DEFRAG_DONE, 0);
  941. }
  942. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  943. {
  944. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  945. struct inode *btree_inode = root->fs_info->btree_inode;
  946. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  947. buf->start, buf->start + buf->len - 1,
  948. EXTENT_DEFRAG_DONE, GFP_NOFS);
  949. }
  950. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  951. {
  952. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  953. struct inode *btree_inode = root->fs_info->btree_inode;
  954. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  955. buf->start, buf->start + buf->len - 1,
  956. EXTENT_DEFRAG, GFP_NOFS);
  957. }
  958. int btrfs_read_buffer(struct extent_buffer *buf)
  959. {
  960. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  961. struct inode *btree_inode = root->fs_info->btree_inode;
  962. return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  963. buf, 0, 1, btree_get_extent);
  964. }
  965. static struct extent_io_ops btree_extent_io_ops = {
  966. .writepage_io_hook = btree_writepage_io_hook,
  967. .submit_bio_hook = btree_submit_bio_hook,
  968. };