disk-io.c 37 KB

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