disk-io.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169
  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->fs_devices->devices;
  331. list_for_each(next, list) {
  332. device = list_entry(next, struct btrfs_device, dev_list);
  333. if (device->bdev && device->bdev != fs_info->sb->s_bdev)
  334. close_bdev_excl(device->bdev);
  335. device->bdev = NULL;
  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. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  586. {
  587. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  588. int ret = 0;
  589. struct list_head *cur;
  590. struct btrfs_device *device;
  591. struct backing_dev_info *bdi;
  592. list_for_each(cur, &info->fs_devices->devices) {
  593. device = list_entry(cur, struct btrfs_device, dev_list);
  594. bdi = blk_get_backing_dev_info(device->bdev);
  595. if (bdi && bdi_congested(bdi, bdi_bits)) {
  596. ret = 1;
  597. break;
  598. }
  599. }
  600. return ret;
  601. }
  602. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  603. {
  604. struct list_head *cur;
  605. struct btrfs_device *device;
  606. struct btrfs_fs_info *info;
  607. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  608. list_for_each(cur, &info->fs_devices->devices) {
  609. device = list_entry(cur, struct btrfs_device, dev_list);
  610. bdi = blk_get_backing_dev_info(device->bdev);
  611. if (bdi->unplug_io_fn) {
  612. bdi->unplug_io_fn(bdi, page);
  613. }
  614. }
  615. }
  616. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  617. {
  618. bdi_init(bdi);
  619. bdi->ra_pages = default_backing_dev_info.ra_pages;
  620. bdi->state = 0;
  621. bdi->capabilities = default_backing_dev_info.capabilities;
  622. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  623. bdi->unplug_io_data = info;
  624. bdi->congested_fn = btrfs_congested_fn;
  625. bdi->congested_data = info;
  626. return 0;
  627. }
  628. struct btrfs_root *open_ctree(struct super_block *sb,
  629. struct btrfs_fs_devices *fs_devices)
  630. {
  631. u32 sectorsize;
  632. u32 nodesize;
  633. u32 leafsize;
  634. u32 blocksize;
  635. u32 stripesize;
  636. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  637. GFP_NOFS);
  638. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  639. GFP_NOFS);
  640. struct btrfs_fs_info *fs_info = kmalloc(sizeof(*fs_info),
  641. GFP_NOFS);
  642. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  643. GFP_NOFS);
  644. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  645. GFP_NOFS);
  646. int ret;
  647. int err = -EIO;
  648. struct btrfs_super_block *disk_super;
  649. if (!extent_root || !tree_root || !fs_info) {
  650. err = -ENOMEM;
  651. goto fail;
  652. }
  653. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  654. INIT_LIST_HEAD(&fs_info->trans_list);
  655. INIT_LIST_HEAD(&fs_info->dead_roots);
  656. INIT_LIST_HEAD(&fs_info->hashers);
  657. spin_lock_init(&fs_info->hash_lock);
  658. spin_lock_init(&fs_info->delalloc_lock);
  659. spin_lock_init(&fs_info->new_trans_lock);
  660. memset(&fs_info->super_kobj, 0, sizeof(fs_info->super_kobj));
  661. init_completion(&fs_info->kobj_unregister);
  662. sb_set_blocksize(sb, 4096);
  663. fs_info->running_transaction = NULL;
  664. fs_info->last_trans_committed = 0;
  665. fs_info->tree_root = tree_root;
  666. fs_info->extent_root = extent_root;
  667. fs_info->chunk_root = chunk_root;
  668. fs_info->dev_root = dev_root;
  669. fs_info->fs_devices = fs_devices;
  670. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  671. INIT_LIST_HEAD(&fs_info->space_info);
  672. btrfs_mapping_init(&fs_info->mapping_tree);
  673. fs_info->sb = sb;
  674. fs_info->throttles = 0;
  675. fs_info->mount_opt = 0;
  676. fs_info->max_extent = (u64)-1;
  677. fs_info->max_inline = 8192 * 1024;
  678. fs_info->delalloc_bytes = 0;
  679. setup_bdi(fs_info, &fs_info->bdi);
  680. fs_info->btree_inode = new_inode(sb);
  681. fs_info->btree_inode->i_ino = 1;
  682. fs_info->btree_inode->i_nlink = 1;
  683. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  684. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  685. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  686. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  687. fs_info->btree_inode->i_mapping,
  688. GFP_NOFS);
  689. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  690. GFP_NOFS);
  691. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  692. extent_io_tree_init(&fs_info->free_space_cache,
  693. fs_info->btree_inode->i_mapping, GFP_NOFS);
  694. extent_io_tree_init(&fs_info->block_group_cache,
  695. fs_info->btree_inode->i_mapping, GFP_NOFS);
  696. extent_io_tree_init(&fs_info->pinned_extents,
  697. fs_info->btree_inode->i_mapping, GFP_NOFS);
  698. extent_io_tree_init(&fs_info->pending_del,
  699. fs_info->btree_inode->i_mapping, GFP_NOFS);
  700. extent_io_tree_init(&fs_info->extent_ins,
  701. fs_info->btree_inode->i_mapping, GFP_NOFS);
  702. fs_info->do_barriers = 1;
  703. fs_info->closing = 0;
  704. fs_info->total_pinned = 0;
  705. fs_info->last_alloc = 0;
  706. fs_info->last_data_alloc = 0;
  707. fs_info->extra_alloc_bits = 0;
  708. fs_info->extra_data_alloc_bits = 0;
  709. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  710. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  711. #else
  712. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  713. #endif
  714. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  715. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  716. sizeof(struct btrfs_key));
  717. insert_inode_hash(fs_info->btree_inode);
  718. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  719. mutex_init(&fs_info->trans_mutex);
  720. mutex_init(&fs_info->fs_mutex);
  721. #if 0
  722. ret = add_hasher(fs_info, "crc32c");
  723. if (ret) {
  724. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  725. err = -ENOMEM;
  726. goto fail_iput;
  727. }
  728. #endif
  729. __setup_root(4096, 4096, 4096, 4096, tree_root,
  730. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  731. fs_info->sb_buffer = read_tree_block(tree_root,
  732. BTRFS_SUPER_INFO_OFFSET,
  733. 4096);
  734. if (!fs_info->sb_buffer)
  735. goto fail_iput;
  736. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  737. sizeof(fs_info->super_copy));
  738. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  739. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  740. BTRFS_FSID_SIZE);
  741. disk_super = &fs_info->super_copy;
  742. if (!btrfs_super_root(disk_super))
  743. goto fail_sb_buffer;
  744. if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
  745. printk("Btrfs: wanted %llu devices, but found %llu\n",
  746. (unsigned long long)btrfs_super_num_devices(disk_super),
  747. (unsigned long long)fs_devices->num_devices);
  748. goto fail_sb_buffer;
  749. }
  750. nodesize = btrfs_super_nodesize(disk_super);
  751. leafsize = btrfs_super_leafsize(disk_super);
  752. sectorsize = btrfs_super_sectorsize(disk_super);
  753. stripesize = btrfs_super_stripesize(disk_super);
  754. tree_root->nodesize = nodesize;
  755. tree_root->leafsize = leafsize;
  756. tree_root->sectorsize = sectorsize;
  757. tree_root->stripesize = stripesize;
  758. sb_set_blocksize(sb, sectorsize);
  759. i_size_write(fs_info->btree_inode,
  760. btrfs_super_total_bytes(disk_super));
  761. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  762. sizeof(disk_super->magic))) {
  763. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  764. goto fail_sb_buffer;
  765. }
  766. mutex_lock(&fs_info->fs_mutex);
  767. ret = btrfs_read_sys_array(tree_root);
  768. BUG_ON(ret);
  769. blocksize = btrfs_level_size(tree_root,
  770. btrfs_super_chunk_root_level(disk_super));
  771. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  772. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  773. chunk_root->node = read_tree_block(chunk_root,
  774. btrfs_super_chunk_root(disk_super),
  775. blocksize);
  776. BUG_ON(!chunk_root->node);
  777. ret = btrfs_read_chunk_tree(chunk_root);
  778. BUG_ON(ret);
  779. blocksize = btrfs_level_size(tree_root,
  780. btrfs_super_root_level(disk_super));
  781. tree_root->node = read_tree_block(tree_root,
  782. btrfs_super_root(disk_super),
  783. blocksize);
  784. if (!tree_root->node)
  785. goto fail_sb_buffer;
  786. ret = find_and_setup_root(tree_root, fs_info,
  787. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  788. if (ret)
  789. goto fail_tree_root;
  790. extent_root->track_dirty = 1;
  791. ret = find_and_setup_root(tree_root, fs_info,
  792. BTRFS_DEV_TREE_OBJECTID, dev_root);
  793. dev_root->track_dirty = 1;
  794. if (ret)
  795. goto fail_extent_root;
  796. btrfs_read_block_groups(extent_root);
  797. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  798. mutex_unlock(&fs_info->fs_mutex);
  799. return tree_root;
  800. fail_extent_root:
  801. free_extent_buffer(extent_root->node);
  802. fail_tree_root:
  803. mutex_unlock(&fs_info->fs_mutex);
  804. free_extent_buffer(tree_root->node);
  805. fail_sb_buffer:
  806. free_extent_buffer(fs_info->sb_buffer);
  807. fail_iput:
  808. iput(fs_info->btree_inode);
  809. fail:
  810. close_all_devices(fs_info);
  811. kfree(extent_root);
  812. kfree(tree_root);
  813. kfree(fs_info);
  814. return ERR_PTR(err);
  815. }
  816. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  817. *root)
  818. {
  819. int ret;
  820. struct extent_buffer *super = root->fs_info->sb_buffer;
  821. struct inode *btree_inode = root->fs_info->btree_inode;
  822. struct super_block *sb = root->fs_info->sb;
  823. if (!btrfs_test_opt(root, NOBARRIER))
  824. blkdev_issue_flush(sb->s_bdev, NULL);
  825. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  826. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  827. super->start, super->len);
  828. if (!btrfs_test_opt(root, NOBARRIER))
  829. blkdev_issue_flush(sb->s_bdev, NULL);
  830. return ret;
  831. }
  832. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  833. {
  834. radix_tree_delete(&fs_info->fs_roots_radix,
  835. (unsigned long)root->root_key.objectid);
  836. if (root->in_sysfs)
  837. btrfs_sysfs_del_root(root);
  838. if (root->inode)
  839. iput(root->inode);
  840. if (root->node)
  841. free_extent_buffer(root->node);
  842. if (root->commit_root)
  843. free_extent_buffer(root->commit_root);
  844. if (root->name)
  845. kfree(root->name);
  846. kfree(root);
  847. return 0;
  848. }
  849. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  850. {
  851. int ret;
  852. struct btrfs_root *gang[8];
  853. int i;
  854. while(1) {
  855. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  856. (void **)gang, 0,
  857. ARRAY_SIZE(gang));
  858. if (!ret)
  859. break;
  860. for (i = 0; i < ret; i++)
  861. btrfs_free_fs_root(fs_info, gang[i]);
  862. }
  863. return 0;
  864. }
  865. int close_ctree(struct btrfs_root *root)
  866. {
  867. int ret;
  868. struct btrfs_trans_handle *trans;
  869. struct btrfs_fs_info *fs_info = root->fs_info;
  870. fs_info->closing = 1;
  871. btrfs_transaction_flush_work(root);
  872. mutex_lock(&fs_info->fs_mutex);
  873. btrfs_defrag_dirty_roots(root->fs_info);
  874. trans = btrfs_start_transaction(root, 1);
  875. ret = btrfs_commit_transaction(trans, root);
  876. /* run commit again to drop the original snapshot */
  877. trans = btrfs_start_transaction(root, 1);
  878. btrfs_commit_transaction(trans, root);
  879. ret = btrfs_write_and_wait_transaction(NULL, root);
  880. BUG_ON(ret);
  881. write_ctree_super(NULL, root);
  882. mutex_unlock(&fs_info->fs_mutex);
  883. if (fs_info->delalloc_bytes) {
  884. printk("btrfs: at unmount delalloc count %Lu\n",
  885. fs_info->delalloc_bytes);
  886. }
  887. if (fs_info->extent_root->node)
  888. free_extent_buffer(fs_info->extent_root->node);
  889. if (fs_info->tree_root->node)
  890. free_extent_buffer(fs_info->tree_root->node);
  891. if (root->fs_info->chunk_root->node);
  892. free_extent_buffer(root->fs_info->chunk_root->node);
  893. if (root->fs_info->dev_root->node);
  894. free_extent_buffer(root->fs_info->dev_root->node);
  895. free_extent_buffer(fs_info->sb_buffer);
  896. btrfs_free_block_groups(root->fs_info);
  897. del_fs_roots(fs_info);
  898. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  899. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  900. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  901. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  902. extent_io_tree_empty_lru(&fs_info->pending_del);
  903. extent_io_tree_empty_lru(&fs_info->extent_ins);
  904. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  905. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  906. iput(fs_info->btree_inode);
  907. #if 0
  908. while(!list_empty(&fs_info->hashers)) {
  909. struct btrfs_hasher *hasher;
  910. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  911. hashers);
  912. list_del(&hasher->hashers);
  913. crypto_free_hash(&fs_info->hash_tfm);
  914. kfree(hasher);
  915. }
  916. #endif
  917. close_all_devices(fs_info);
  918. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  919. bdi_destroy(&fs_info->bdi);
  920. kfree(fs_info->extent_root);
  921. kfree(fs_info->tree_root);
  922. kfree(fs_info->chunk_root);
  923. kfree(fs_info->dev_root);
  924. return 0;
  925. }
  926. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  927. {
  928. struct inode *btree_inode = buf->first_page->mapping->host;
  929. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  930. }
  931. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  932. {
  933. struct inode *btree_inode = buf->first_page->mapping->host;
  934. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  935. buf);
  936. }
  937. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  938. {
  939. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  940. u64 transid = btrfs_header_generation(buf);
  941. struct inode *btree_inode = root->fs_info->btree_inode;
  942. if (transid != root->fs_info->generation) {
  943. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  944. (unsigned long long)buf->start,
  945. transid, root->fs_info->generation);
  946. WARN_ON(1);
  947. }
  948. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  949. }
  950. void btrfs_throttle(struct btrfs_root *root)
  951. {
  952. struct backing_dev_info *bdi;
  953. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  954. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  955. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  956. congestion_wait(WRITE, HZ/20);
  957. #else
  958. blk_congestion_wait(WRITE, HZ/20);
  959. #endif
  960. }
  961. }
  962. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  963. {
  964. balance_dirty_pages_ratelimited_nr(
  965. root->fs_info->btree_inode->i_mapping, 1);
  966. }
  967. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  968. {
  969. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  970. struct inode *btree_inode = root->fs_info->btree_inode;
  971. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  972. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  973. }
  974. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  975. {
  976. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  977. struct inode *btree_inode = root->fs_info->btree_inode;
  978. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  979. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  980. GFP_NOFS);
  981. }
  982. int btrfs_buffer_defrag(struct extent_buffer *buf)
  983. {
  984. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  985. struct inode *btree_inode = root->fs_info->btree_inode;
  986. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  987. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  988. }
  989. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  990. {
  991. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  992. struct inode *btree_inode = root->fs_info->btree_inode;
  993. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  994. buf->start, buf->start + buf->len - 1,
  995. EXTENT_DEFRAG_DONE, 0);
  996. }
  997. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  998. {
  999. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1000. struct inode *btree_inode = root->fs_info->btree_inode;
  1001. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1002. buf->start, buf->start + buf->len - 1,
  1003. EXTENT_DEFRAG_DONE, GFP_NOFS);
  1004. }
  1005. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  1006. {
  1007. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1008. struct inode *btree_inode = root->fs_info->btree_inode;
  1009. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1010. buf->start, buf->start + buf->len - 1,
  1011. EXTENT_DEFRAG, GFP_NOFS);
  1012. }
  1013. int btrfs_read_buffer(struct extent_buffer *buf)
  1014. {
  1015. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1016. struct inode *btree_inode = root->fs_info->btree_inode;
  1017. return read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  1018. buf, 0, 1, btree_get_extent);
  1019. }
  1020. static struct extent_io_ops btree_extent_io_ops = {
  1021. .writepage_io_hook = btree_writepage_io_hook,
  1022. .submit_bio_hook = btree_submit_bio_hook,
  1023. /* note we're sharing with inode.c for the merge bio hook */
  1024. .merge_bio_hook = btrfs_merge_bio_hook,
  1025. };