disk-io.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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);
  336. }
  337. return 0;
  338. }
  339. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  340. u32 blocksize)
  341. {
  342. struct extent_buffer *buf = NULL;
  343. struct inode *btree_inode = root->fs_info->btree_inode;
  344. struct extent_io_tree *io_tree;
  345. u64 end;
  346. int ret;
  347. io_tree = &BTRFS_I(btree_inode)->io_tree;
  348. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  349. if (!buf)
  350. return NULL;
  351. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree, buf, 0, 1,
  352. btree_get_extent);
  353. if (buf->flags & EXTENT_CSUM)
  354. return buf;
  355. end = buf->start + PAGE_CACHE_SIZE - 1;
  356. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  357. buf->flags |= EXTENT_CSUM;
  358. return buf;
  359. }
  360. lock_extent(io_tree, buf->start, end, GFP_NOFS);
  361. if (test_range_bit(io_tree, buf->start, end, EXTENT_CSUM, 1)) {
  362. buf->flags |= EXTENT_CSUM;
  363. goto out_unlock;
  364. }
  365. ret = csum_tree_block(root, buf, 1);
  366. set_extent_bits(io_tree, buf->start, end, EXTENT_CSUM, GFP_NOFS);
  367. buf->flags |= EXTENT_CSUM;
  368. out_unlock:
  369. unlock_extent(io_tree, buf->start, end, GFP_NOFS);
  370. return buf;
  371. }
  372. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  373. struct extent_buffer *buf)
  374. {
  375. struct inode *btree_inode = root->fs_info->btree_inode;
  376. if (btrfs_header_generation(buf) ==
  377. root->fs_info->running_transaction->transid)
  378. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  379. buf);
  380. return 0;
  381. }
  382. int wait_on_tree_block_writeback(struct btrfs_root *root,
  383. struct extent_buffer *buf)
  384. {
  385. struct inode *btree_inode = root->fs_info->btree_inode;
  386. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  387. buf);
  388. return 0;
  389. }
  390. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  391. u32 stripesize, struct btrfs_root *root,
  392. struct btrfs_fs_info *fs_info,
  393. u64 objectid)
  394. {
  395. root->node = NULL;
  396. root->inode = NULL;
  397. root->commit_root = NULL;
  398. root->sectorsize = sectorsize;
  399. root->nodesize = nodesize;
  400. root->leafsize = leafsize;
  401. root->stripesize = stripesize;
  402. root->ref_cows = 0;
  403. root->track_dirty = 0;
  404. root->fs_info = fs_info;
  405. root->objectid = objectid;
  406. root->last_trans = 0;
  407. root->highest_inode = 0;
  408. root->last_inode_alloc = 0;
  409. root->name = NULL;
  410. root->in_sysfs = 0;
  411. INIT_LIST_HEAD(&root->dirty_list);
  412. memset(&root->root_key, 0, sizeof(root->root_key));
  413. memset(&root->root_item, 0, sizeof(root->root_item));
  414. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  415. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  416. init_completion(&root->kobj_unregister);
  417. root->defrag_running = 0;
  418. root->defrag_level = 0;
  419. root->root_key.objectid = objectid;
  420. return 0;
  421. }
  422. static int find_and_setup_root(struct btrfs_root *tree_root,
  423. struct btrfs_fs_info *fs_info,
  424. u64 objectid,
  425. struct btrfs_root *root)
  426. {
  427. int ret;
  428. u32 blocksize;
  429. __setup_root(tree_root->nodesize, tree_root->leafsize,
  430. tree_root->sectorsize, tree_root->stripesize,
  431. root, fs_info, objectid);
  432. ret = btrfs_find_last_root(tree_root, objectid,
  433. &root->root_item, &root->root_key);
  434. BUG_ON(ret);
  435. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  436. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  437. blocksize);
  438. BUG_ON(!root->node);
  439. return 0;
  440. }
  441. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  442. struct btrfs_key *location)
  443. {
  444. struct btrfs_root *root;
  445. struct btrfs_root *tree_root = fs_info->tree_root;
  446. struct btrfs_path *path;
  447. struct extent_buffer *l;
  448. u64 highest_inode;
  449. u32 blocksize;
  450. int ret = 0;
  451. root = kzalloc(sizeof(*root), GFP_NOFS);
  452. if (!root)
  453. return ERR_PTR(-ENOMEM);
  454. if (location->offset == (u64)-1) {
  455. ret = find_and_setup_root(tree_root, fs_info,
  456. location->objectid, root);
  457. if (ret) {
  458. kfree(root);
  459. return ERR_PTR(ret);
  460. }
  461. goto insert;
  462. }
  463. __setup_root(tree_root->nodesize, tree_root->leafsize,
  464. tree_root->sectorsize, tree_root->stripesize,
  465. root, fs_info, location->objectid);
  466. path = btrfs_alloc_path();
  467. BUG_ON(!path);
  468. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  469. if (ret != 0) {
  470. if (ret > 0)
  471. ret = -ENOENT;
  472. goto out;
  473. }
  474. l = path->nodes[0];
  475. read_extent_buffer(l, &root->root_item,
  476. btrfs_item_ptr_offset(l, path->slots[0]),
  477. sizeof(root->root_item));
  478. memcpy(&root->root_key, location, sizeof(*location));
  479. ret = 0;
  480. out:
  481. btrfs_release_path(root, path);
  482. btrfs_free_path(path);
  483. if (ret) {
  484. kfree(root);
  485. return ERR_PTR(ret);
  486. }
  487. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  488. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  489. blocksize);
  490. BUG_ON(!root->node);
  491. insert:
  492. root->ref_cows = 1;
  493. ret = btrfs_find_highest_inode(root, &highest_inode);
  494. if (ret == 0) {
  495. root->highest_inode = highest_inode;
  496. root->last_inode_alloc = highest_inode;
  497. }
  498. return root;
  499. }
  500. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  501. u64 root_objectid)
  502. {
  503. struct btrfs_root *root;
  504. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  505. return fs_info->tree_root;
  506. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  507. return fs_info->extent_root;
  508. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  509. (unsigned long)root_objectid);
  510. return root;
  511. }
  512. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  513. struct btrfs_key *location)
  514. {
  515. struct btrfs_root *root;
  516. int ret;
  517. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  518. return fs_info->tree_root;
  519. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  520. return fs_info->extent_root;
  521. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  522. (unsigned long)location->objectid);
  523. if (root)
  524. return root;
  525. root = btrfs_read_fs_root_no_radix(fs_info, location);
  526. if (IS_ERR(root))
  527. return root;
  528. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  529. (unsigned long)root->root_key.objectid,
  530. root);
  531. if (ret) {
  532. free_extent_buffer(root->node);
  533. kfree(root);
  534. return ERR_PTR(ret);
  535. }
  536. ret = btrfs_find_dead_roots(fs_info->tree_root,
  537. root->root_key.objectid, root);
  538. BUG_ON(ret);
  539. return root;
  540. }
  541. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  542. struct btrfs_key *location,
  543. const char *name, int namelen)
  544. {
  545. struct btrfs_root *root;
  546. int ret;
  547. root = btrfs_read_fs_root_no_name(fs_info, location);
  548. if (!root)
  549. return NULL;
  550. if (root->in_sysfs)
  551. return root;
  552. ret = btrfs_set_root_name(root, name, namelen);
  553. if (ret) {
  554. free_extent_buffer(root->node);
  555. kfree(root);
  556. return ERR_PTR(ret);
  557. }
  558. ret = btrfs_sysfs_add_root(root);
  559. if (ret) {
  560. free_extent_buffer(root->node);
  561. kfree(root->name);
  562. kfree(root);
  563. return ERR_PTR(ret);
  564. }
  565. root->in_sysfs = 1;
  566. return root;
  567. }
  568. #if 0
  569. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  570. struct btrfs_hasher *hasher;
  571. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  572. if (!hasher)
  573. return -ENOMEM;
  574. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  575. if (!hasher->hash_tfm) {
  576. kfree(hasher);
  577. return -EINVAL;
  578. }
  579. spin_lock(&info->hash_lock);
  580. list_add(&hasher->list, &info->hashers);
  581. spin_unlock(&info->hash_lock);
  582. return 0;
  583. }
  584. #endif
  585. struct btrfs_root *open_ctree(struct super_block *sb)
  586. {
  587. u32 sectorsize;
  588. u32 nodesize;
  589. u32 leafsize;
  590. u32 blocksize;
  591. u32 stripesize;
  592. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  593. GFP_NOFS);
  594. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  595. GFP_NOFS);
  596. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  597. GFP_NOFS);
  598. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  599. GFP_NOFS);
  600. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  601. GFP_NOFS);
  602. int ret;
  603. int err = -EIO;
  604. struct btrfs_super_block *disk_super;
  605. if (!extent_root || !tree_root || !fs_info) {
  606. err = -ENOMEM;
  607. goto fail;
  608. }
  609. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  610. INIT_LIST_HEAD(&fs_info->trans_list);
  611. INIT_LIST_HEAD(&fs_info->dead_roots);
  612. INIT_LIST_HEAD(&fs_info->hashers);
  613. spin_lock_init(&fs_info->hash_lock);
  614. spin_lock_init(&fs_info->delalloc_lock);
  615. spin_lock_init(&fs_info->new_trans_lock);
  616. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  617. init_completion(&fs_info->kobj_unregister);
  618. sb_set_blocksize(sb, 4096);
  619. fs_info->running_transaction = NULL;
  620. fs_info->last_trans_committed = 0;
  621. fs_info->tree_root = tree_root;
  622. fs_info->extent_root = extent_root;
  623. fs_info->chunk_root = chunk_root;
  624. fs_info->dev_root = dev_root;
  625. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  626. INIT_LIST_HEAD(&fs_info->devices);
  627. INIT_LIST_HEAD(&fs_info->space_info);
  628. btrfs_mapping_init(&fs_info->mapping_tree);
  629. fs_info->sb = sb;
  630. fs_info->throttles = 0;
  631. fs_info->mount_opt = 0;
  632. fs_info->max_extent = (u64)-1;
  633. fs_info->max_inline = 8192 * 1024;
  634. fs_info->delalloc_bytes = 0;
  635. fs_info->btree_inode = new_inode(sb);
  636. fs_info->btree_inode->i_ino = 1;
  637. fs_info->btree_inode->i_nlink = 1;
  638. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  639. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  640. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  641. fs_info->btree_inode->i_mapping,
  642. GFP_NOFS);
  643. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  644. GFP_NOFS);
  645. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  646. extent_io_tree_init(&fs_info->free_space_cache,
  647. fs_info->btree_inode->i_mapping, GFP_NOFS);
  648. extent_io_tree_init(&fs_info->block_group_cache,
  649. fs_info->btree_inode->i_mapping, GFP_NOFS);
  650. extent_io_tree_init(&fs_info->pinned_extents,
  651. fs_info->btree_inode->i_mapping, GFP_NOFS);
  652. extent_io_tree_init(&fs_info->pending_del,
  653. fs_info->btree_inode->i_mapping, GFP_NOFS);
  654. extent_io_tree_init(&fs_info->extent_ins,
  655. fs_info->btree_inode->i_mapping, GFP_NOFS);
  656. fs_info->do_barriers = 1;
  657. fs_info->closing = 0;
  658. fs_info->total_pinned = 0;
  659. fs_info->last_alloc = 0;
  660. fs_info->last_data_alloc = 0;
  661. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  662. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  663. #else
  664. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  665. #endif
  666. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  667. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  668. sizeof(struct btrfs_key));
  669. insert_inode_hash(fs_info->btree_inode);
  670. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  671. mutex_init(&fs_info->trans_mutex);
  672. mutex_init(&fs_info->fs_mutex);
  673. #if 0
  674. ret = add_hasher(fs_info, "crc32c");
  675. if (ret) {
  676. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  677. err = -ENOMEM;
  678. goto fail_iput;
  679. }
  680. #endif
  681. __setup_root(4096, 4096, 4096, 4096, tree_root,
  682. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  683. fs_info->sb_buffer = read_tree_block(tree_root,
  684. BTRFS_SUPER_INFO_OFFSET,
  685. 4096);
  686. if (!fs_info->sb_buffer)
  687. goto fail_iput;
  688. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  689. sizeof(fs_info->super_copy));
  690. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  691. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  692. BTRFS_FSID_SIZE);
  693. disk_super = &fs_info->super_copy;
  694. if (!btrfs_super_root(disk_super))
  695. goto fail_sb_buffer;
  696. nodesize = btrfs_super_nodesize(disk_super);
  697. leafsize = btrfs_super_leafsize(disk_super);
  698. sectorsize = btrfs_super_sectorsize(disk_super);
  699. stripesize = btrfs_super_stripesize(disk_super);
  700. tree_root->nodesize = nodesize;
  701. tree_root->leafsize = leafsize;
  702. tree_root->sectorsize = sectorsize;
  703. tree_root->stripesize = stripesize;
  704. sb_set_blocksize(sb, sectorsize);
  705. i_size_write(fs_info->btree_inode,
  706. btrfs_super_total_bytes(disk_super));
  707. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  708. sizeof(disk_super->magic))) {
  709. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  710. goto fail_sb_buffer;
  711. }
  712. mutex_lock(&fs_info->fs_mutex);
  713. ret = btrfs_read_super_device(tree_root, fs_info->sb_buffer);
  714. BUG_ON(ret);
  715. ret = btrfs_read_sys_array(tree_root);
  716. BUG_ON(ret);
  717. blocksize = btrfs_level_size(tree_root,
  718. btrfs_super_chunk_root_level(disk_super));
  719. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  720. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  721. chunk_root->node = read_tree_block(chunk_root,
  722. btrfs_super_chunk_root(disk_super),
  723. blocksize);
  724. BUG_ON(!chunk_root->node);
  725. ret = btrfs_read_chunk_tree(chunk_root);
  726. BUG_ON(ret);
  727. blocksize = btrfs_level_size(tree_root,
  728. btrfs_super_root_level(disk_super));
  729. tree_root->node = read_tree_block(tree_root,
  730. btrfs_super_root(disk_super),
  731. blocksize);
  732. if (!tree_root->node)
  733. goto fail_sb_buffer;
  734. ret = find_and_setup_root(tree_root, fs_info,
  735. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  736. if (ret)
  737. goto fail_tree_root;
  738. extent_root->track_dirty = 1;
  739. ret = find_and_setup_root(tree_root, fs_info,
  740. BTRFS_DEV_TREE_OBJECTID, dev_root);
  741. dev_root->track_dirty = 1;
  742. if (ret)
  743. goto fail_extent_root;
  744. btrfs_read_block_groups(extent_root);
  745. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  746. mutex_unlock(&fs_info->fs_mutex);
  747. return tree_root;
  748. fail_extent_root:
  749. free_extent_buffer(extent_root->node);
  750. fail_tree_root:
  751. mutex_unlock(&fs_info->fs_mutex);
  752. free_extent_buffer(tree_root->node);
  753. fail_sb_buffer:
  754. free_extent_buffer(fs_info->sb_buffer);
  755. fail_iput:
  756. iput(fs_info->btree_inode);
  757. fail:
  758. kfree(extent_root);
  759. kfree(tree_root);
  760. kfree(fs_info);
  761. return ERR_PTR(err);
  762. }
  763. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  764. *root)
  765. {
  766. int ret;
  767. struct extent_buffer *super = root->fs_info->sb_buffer;
  768. struct inode *btree_inode = root->fs_info->btree_inode;
  769. struct super_block *sb = root->fs_info->sb;
  770. if (!btrfs_test_opt(root, NOBARRIER))
  771. blkdev_issue_flush(sb->s_bdev, NULL);
  772. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  773. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  774. super->start, super->len);
  775. if (!btrfs_test_opt(root, NOBARRIER))
  776. blkdev_issue_flush(sb->s_bdev, NULL);
  777. return ret;
  778. }
  779. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  780. {
  781. radix_tree_delete(&fs_info->fs_roots_radix,
  782. (unsigned long)root->root_key.objectid);
  783. if (root->in_sysfs)
  784. btrfs_sysfs_del_root(root);
  785. if (root->inode)
  786. iput(root->inode);
  787. if (root->node)
  788. free_extent_buffer(root->node);
  789. if (root->commit_root)
  790. free_extent_buffer(root->commit_root);
  791. if (root->name)
  792. kfree(root->name);
  793. kfree(root);
  794. return 0;
  795. }
  796. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  797. {
  798. int ret;
  799. struct btrfs_root *gang[8];
  800. int i;
  801. while(1) {
  802. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  803. (void **)gang, 0,
  804. ARRAY_SIZE(gang));
  805. if (!ret)
  806. break;
  807. for (i = 0; i < ret; i++)
  808. btrfs_free_fs_root(fs_info, gang[i]);
  809. }
  810. return 0;
  811. }
  812. int close_ctree(struct btrfs_root *root)
  813. {
  814. int ret;
  815. struct btrfs_trans_handle *trans;
  816. struct btrfs_fs_info *fs_info = root->fs_info;
  817. fs_info->closing = 1;
  818. btrfs_transaction_flush_work(root);
  819. mutex_lock(&fs_info->fs_mutex);
  820. btrfs_defrag_dirty_roots(root->fs_info);
  821. trans = btrfs_start_transaction(root, 1);
  822. ret = btrfs_commit_transaction(trans, root);
  823. /* run commit again to drop the original snapshot */
  824. trans = btrfs_start_transaction(root, 1);
  825. btrfs_commit_transaction(trans, root);
  826. ret = btrfs_write_and_wait_transaction(NULL, root);
  827. BUG_ON(ret);
  828. write_ctree_super(NULL, root);
  829. mutex_unlock(&fs_info->fs_mutex);
  830. if (fs_info->delalloc_bytes) {
  831. printk("btrfs: at unmount delalloc count %Lu\n",
  832. fs_info->delalloc_bytes);
  833. }
  834. if (fs_info->extent_root->node)
  835. free_extent_buffer(fs_info->extent_root->node);
  836. if (fs_info->tree_root->node)
  837. free_extent_buffer(fs_info->tree_root->node);
  838. if (root->fs_info->chunk_root->node);
  839. free_extent_buffer(root->fs_info->chunk_root->node);
  840. if (root->fs_info->dev_root->node);
  841. free_extent_buffer(root->fs_info->dev_root->node);
  842. free_extent_buffer(fs_info->sb_buffer);
  843. btrfs_free_block_groups(root->fs_info);
  844. del_fs_roots(fs_info);
  845. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  846. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  847. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  848. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  849. extent_io_tree_empty_lru(&fs_info->pending_del);
  850. extent_io_tree_empty_lru(&fs_info->extent_ins);
  851. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  852. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  853. iput(fs_info->btree_inode);
  854. #if 0
  855. while(!list_empty(&fs_info->hashers)) {
  856. struct btrfs_hasher *hasher;
  857. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  858. hashers);
  859. list_del(&hasher->hashers);
  860. crypto_free_hash(&fs_info->hash_tfm);
  861. kfree(hasher);
  862. }
  863. #endif
  864. close_all_devices(fs_info);
  865. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  866. kfree(fs_info->extent_root);
  867. kfree(fs_info->tree_root);
  868. kfree(fs_info->chunk_root);
  869. kfree(fs_info->dev_root);
  870. return 0;
  871. }
  872. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  873. {
  874. struct inode *btree_inode = buf->first_page->mapping->host;
  875. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  876. }
  877. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  878. {
  879. struct inode *btree_inode = buf->first_page->mapping->host;
  880. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  881. buf);
  882. }
  883. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  884. {
  885. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  886. u64 transid = btrfs_header_generation(buf);
  887. struct inode *btree_inode = root->fs_info->btree_inode;
  888. if (transid != root->fs_info->generation) {
  889. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  890. (unsigned long long)buf->start,
  891. transid, root->fs_info->generation);
  892. WARN_ON(1);
  893. }
  894. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  895. }
  896. void btrfs_throttle(struct btrfs_root *root)
  897. {
  898. struct backing_dev_info *bdi;
  899. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  900. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  901. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  902. congestion_wait(WRITE, HZ/20);
  903. #else
  904. blk_congestion_wait(WRITE, HZ/20);
  905. #endif
  906. }
  907. }
  908. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  909. {
  910. balance_dirty_pages_ratelimited_nr(
  911. root->fs_info->btree_inode->i_mapping, 1);
  912. }
  913. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  914. {
  915. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  916. struct inode *btree_inode = root->fs_info->btree_inode;
  917. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  918. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  919. }
  920. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  921. {
  922. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  923. struct inode *btree_inode = root->fs_info->btree_inode;
  924. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  925. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  926. GFP_NOFS);
  927. }
  928. int btrfs_buffer_defrag(struct extent_buffer *buf)
  929. {
  930. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  931. struct inode *btree_inode = root->fs_info->btree_inode;
  932. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  933. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  934. }
  935. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  936. {
  937. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  938. struct inode *btree_inode = root->fs_info->btree_inode;
  939. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  940. buf->start, buf->start + buf->len - 1,
  941. EXTENT_DEFRAG_DONE, 0);
  942. }
  943. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  944. {
  945. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  946. struct inode *btree_inode = root->fs_info->btree_inode;
  947. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  948. buf->start, buf->start + buf->len - 1,
  949. EXTENT_DEFRAG_DONE, GFP_NOFS);
  950. }
  951. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  952. {
  953. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  954. struct inode *btree_inode = root->fs_info->btree_inode;
  955. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  956. buf->start, buf->start + buf->len - 1,
  957. EXTENT_DEFRAG, GFP_NOFS);
  958. }
  959. int btrfs_read_buffer(struct extent_buffer *buf)
  960. {
  961. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  962. struct inode *btree_inode = root->fs_info->btree_inode;
  963. return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  964. buf, 0, 1, btree_get_extent);
  965. }
  966. static struct extent_io_ops btree_extent_io_ops = {
  967. .writepage_io_hook = btree_writepage_io_hook,
  968. .submit_bio_hook = btree_submit_bio_hook,
  969. };