disk-io.c 41 KB

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