disk-io.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527
  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. 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 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. tree = &BTRFS_I(page->mapping->host)->io_tree;
  393. map = &BTRFS_I(page->mapping->host)->extent_tree;
  394. ret = try_release_extent_mapping(map, tree, page, gfp_flags);
  395. if (ret == 1) {
  396. invalidate_extent_lru(tree, page_offset(page), PAGE_CACHE_SIZE);
  397. ClearPagePrivate(page);
  398. set_page_private(page, 0);
  399. page_cache_release(page);
  400. }
  401. return ret;
  402. }
  403. static void btree_invalidatepage(struct page *page, unsigned long offset)
  404. {
  405. struct extent_io_tree *tree;
  406. tree = &BTRFS_I(page->mapping->host)->io_tree;
  407. extent_invalidatepage(tree, page, offset);
  408. btree_releasepage(page, GFP_NOFS);
  409. }
  410. #if 0
  411. static int btree_writepage(struct page *page, struct writeback_control *wbc)
  412. {
  413. struct buffer_head *bh;
  414. struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
  415. struct buffer_head *head;
  416. if (!page_has_buffers(page)) {
  417. create_empty_buffers(page, root->fs_info->sb->s_blocksize,
  418. (1 << BH_Dirty)|(1 << BH_Uptodate));
  419. }
  420. head = page_buffers(page);
  421. bh = head;
  422. do {
  423. if (buffer_dirty(bh))
  424. csum_tree_block(root, bh, 0);
  425. bh = bh->b_this_page;
  426. } while (bh != head);
  427. return block_write_full_page(page, btree_get_block, wbc);
  428. }
  429. #endif
  430. static struct address_space_operations btree_aops = {
  431. .readpage = btree_readpage,
  432. .writepage = btree_writepage,
  433. .writepages = btree_writepages,
  434. .releasepage = btree_releasepage,
  435. .invalidatepage = btree_invalidatepage,
  436. .sync_page = block_sync_page,
  437. };
  438. int readahead_tree_block(struct btrfs_root *root, u64 bytenr, u32 blocksize)
  439. {
  440. struct extent_buffer *buf = NULL;
  441. struct inode *btree_inode = root->fs_info->btree_inode;
  442. int ret = 0;
  443. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  444. if (!buf)
  445. return 0;
  446. read_extent_buffer_pages(&BTRFS_I(btree_inode)->io_tree,
  447. buf, 0, 0, btree_get_extent, 0);
  448. free_extent_buffer(buf);
  449. return ret;
  450. }
  451. static int close_all_devices(struct btrfs_fs_info *fs_info)
  452. {
  453. struct list_head *list;
  454. struct list_head *next;
  455. struct btrfs_device *device;
  456. list = &fs_info->fs_devices->devices;
  457. list_for_each(next, list) {
  458. device = list_entry(next, struct btrfs_device, dev_list);
  459. if (device->bdev && device->bdev != fs_info->sb->s_bdev)
  460. close_bdev_excl(device->bdev);
  461. device->bdev = NULL;
  462. }
  463. return 0;
  464. }
  465. int btrfs_verify_block_csum(struct btrfs_root *root,
  466. struct extent_buffer *buf)
  467. {
  468. return btrfs_buffer_uptodate(buf);
  469. }
  470. struct extent_buffer *btrfs_find_tree_block(struct btrfs_root *root,
  471. u64 bytenr, u32 blocksize)
  472. {
  473. struct inode *btree_inode = root->fs_info->btree_inode;
  474. struct extent_buffer *eb;
  475. eb = find_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  476. bytenr, blocksize, GFP_NOFS);
  477. return eb;
  478. }
  479. struct extent_buffer *btrfs_find_create_tree_block(struct btrfs_root *root,
  480. u64 bytenr, u32 blocksize)
  481. {
  482. struct inode *btree_inode = root->fs_info->btree_inode;
  483. struct extent_buffer *eb;
  484. eb = alloc_extent_buffer(&BTRFS_I(btree_inode)->io_tree,
  485. bytenr, blocksize, NULL, GFP_NOFS);
  486. return eb;
  487. }
  488. struct extent_buffer *read_tree_block(struct btrfs_root *root, u64 bytenr,
  489. u32 blocksize)
  490. {
  491. struct extent_buffer *buf = NULL;
  492. struct inode *btree_inode = root->fs_info->btree_inode;
  493. struct extent_io_tree *io_tree;
  494. int ret;
  495. io_tree = &BTRFS_I(btree_inode)->io_tree;
  496. buf = btrfs_find_create_tree_block(root, bytenr, blocksize);
  497. if (!buf)
  498. return NULL;
  499. ret = btree_read_extent_buffer_pages(root, buf, 0);
  500. if (ret == 0) {
  501. buf->flags |= EXTENT_UPTODATE;
  502. }
  503. return buf;
  504. }
  505. int clean_tree_block(struct btrfs_trans_handle *trans, struct btrfs_root *root,
  506. struct extent_buffer *buf)
  507. {
  508. struct inode *btree_inode = root->fs_info->btree_inode;
  509. if (btrfs_header_generation(buf) ==
  510. root->fs_info->running_transaction->transid)
  511. clear_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree,
  512. buf);
  513. return 0;
  514. }
  515. int wait_on_tree_block_writeback(struct btrfs_root *root,
  516. struct extent_buffer *buf)
  517. {
  518. struct inode *btree_inode = root->fs_info->btree_inode;
  519. wait_on_extent_buffer_writeback(&BTRFS_I(btree_inode)->io_tree,
  520. buf);
  521. return 0;
  522. }
  523. static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
  524. u32 stripesize, struct btrfs_root *root,
  525. struct btrfs_fs_info *fs_info,
  526. u64 objectid)
  527. {
  528. root->node = NULL;
  529. root->inode = NULL;
  530. root->commit_root = NULL;
  531. root->sectorsize = sectorsize;
  532. root->nodesize = nodesize;
  533. root->leafsize = leafsize;
  534. root->stripesize = stripesize;
  535. root->ref_cows = 0;
  536. root->track_dirty = 0;
  537. root->fs_info = fs_info;
  538. root->objectid = objectid;
  539. root->last_trans = 0;
  540. root->highest_inode = 0;
  541. root->last_inode_alloc = 0;
  542. root->name = NULL;
  543. root->in_sysfs = 0;
  544. INIT_LIST_HEAD(&root->dirty_list);
  545. memset(&root->root_key, 0, sizeof(root->root_key));
  546. memset(&root->root_item, 0, sizeof(root->root_item));
  547. memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
  548. memset(&root->root_kobj, 0, sizeof(root->root_kobj));
  549. init_completion(&root->kobj_unregister);
  550. root->defrag_running = 0;
  551. root->defrag_level = 0;
  552. root->root_key.objectid = objectid;
  553. return 0;
  554. }
  555. static int find_and_setup_root(struct btrfs_root *tree_root,
  556. struct btrfs_fs_info *fs_info,
  557. u64 objectid,
  558. struct btrfs_root *root)
  559. {
  560. int ret;
  561. u32 blocksize;
  562. __setup_root(tree_root->nodesize, tree_root->leafsize,
  563. tree_root->sectorsize, tree_root->stripesize,
  564. root, fs_info, objectid);
  565. ret = btrfs_find_last_root(tree_root, objectid,
  566. &root->root_item, &root->root_key);
  567. BUG_ON(ret);
  568. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  569. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  570. blocksize);
  571. BUG_ON(!root->node);
  572. return 0;
  573. }
  574. struct btrfs_root *btrfs_read_fs_root_no_radix(struct btrfs_fs_info *fs_info,
  575. struct btrfs_key *location)
  576. {
  577. struct btrfs_root *root;
  578. struct btrfs_root *tree_root = fs_info->tree_root;
  579. struct btrfs_path *path;
  580. struct extent_buffer *l;
  581. u64 highest_inode;
  582. u32 blocksize;
  583. int ret = 0;
  584. root = kzalloc(sizeof(*root), GFP_NOFS);
  585. if (!root)
  586. return ERR_PTR(-ENOMEM);
  587. if (location->offset == (u64)-1) {
  588. ret = find_and_setup_root(tree_root, fs_info,
  589. location->objectid, root);
  590. if (ret) {
  591. kfree(root);
  592. return ERR_PTR(ret);
  593. }
  594. goto insert;
  595. }
  596. __setup_root(tree_root->nodesize, tree_root->leafsize,
  597. tree_root->sectorsize, tree_root->stripesize,
  598. root, fs_info, location->objectid);
  599. path = btrfs_alloc_path();
  600. BUG_ON(!path);
  601. ret = btrfs_search_slot(NULL, tree_root, location, path, 0, 0);
  602. if (ret != 0) {
  603. if (ret > 0)
  604. ret = -ENOENT;
  605. goto out;
  606. }
  607. l = path->nodes[0];
  608. read_extent_buffer(l, &root->root_item,
  609. btrfs_item_ptr_offset(l, path->slots[0]),
  610. sizeof(root->root_item));
  611. memcpy(&root->root_key, location, sizeof(*location));
  612. ret = 0;
  613. out:
  614. btrfs_release_path(root, path);
  615. btrfs_free_path(path);
  616. if (ret) {
  617. kfree(root);
  618. return ERR_PTR(ret);
  619. }
  620. blocksize = btrfs_level_size(root, btrfs_root_level(&root->root_item));
  621. root->node = read_tree_block(root, btrfs_root_bytenr(&root->root_item),
  622. blocksize);
  623. BUG_ON(!root->node);
  624. insert:
  625. root->ref_cows = 1;
  626. ret = btrfs_find_highest_inode(root, &highest_inode);
  627. if (ret == 0) {
  628. root->highest_inode = highest_inode;
  629. root->last_inode_alloc = highest_inode;
  630. }
  631. return root;
  632. }
  633. struct btrfs_root *btrfs_lookup_fs_root(struct btrfs_fs_info *fs_info,
  634. u64 root_objectid)
  635. {
  636. struct btrfs_root *root;
  637. if (root_objectid == BTRFS_ROOT_TREE_OBJECTID)
  638. return fs_info->tree_root;
  639. if (root_objectid == BTRFS_EXTENT_TREE_OBJECTID)
  640. return fs_info->extent_root;
  641. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  642. (unsigned long)root_objectid);
  643. return root;
  644. }
  645. struct btrfs_root *btrfs_read_fs_root_no_name(struct btrfs_fs_info *fs_info,
  646. struct btrfs_key *location)
  647. {
  648. struct btrfs_root *root;
  649. int ret;
  650. if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
  651. return fs_info->tree_root;
  652. if (location->objectid == BTRFS_EXTENT_TREE_OBJECTID)
  653. return fs_info->extent_root;
  654. root = radix_tree_lookup(&fs_info->fs_roots_radix,
  655. (unsigned long)location->objectid);
  656. if (root)
  657. return root;
  658. root = btrfs_read_fs_root_no_radix(fs_info, location);
  659. if (IS_ERR(root))
  660. return root;
  661. ret = radix_tree_insert(&fs_info->fs_roots_radix,
  662. (unsigned long)root->root_key.objectid,
  663. root);
  664. if (ret) {
  665. free_extent_buffer(root->node);
  666. kfree(root);
  667. return ERR_PTR(ret);
  668. }
  669. ret = btrfs_find_dead_roots(fs_info->tree_root,
  670. root->root_key.objectid, root);
  671. BUG_ON(ret);
  672. return root;
  673. }
  674. struct btrfs_root *btrfs_read_fs_root(struct btrfs_fs_info *fs_info,
  675. struct btrfs_key *location,
  676. const char *name, int namelen)
  677. {
  678. struct btrfs_root *root;
  679. int ret;
  680. root = btrfs_read_fs_root_no_name(fs_info, location);
  681. if (!root)
  682. return NULL;
  683. if (root->in_sysfs)
  684. return root;
  685. ret = btrfs_set_root_name(root, name, namelen);
  686. if (ret) {
  687. free_extent_buffer(root->node);
  688. kfree(root);
  689. return ERR_PTR(ret);
  690. }
  691. ret = btrfs_sysfs_add_root(root);
  692. if (ret) {
  693. free_extent_buffer(root->node);
  694. kfree(root->name);
  695. kfree(root);
  696. return ERR_PTR(ret);
  697. }
  698. root->in_sysfs = 1;
  699. return root;
  700. }
  701. #if 0
  702. static int add_hasher(struct btrfs_fs_info *info, char *type) {
  703. struct btrfs_hasher *hasher;
  704. hasher = kmalloc(sizeof(*hasher), GFP_NOFS);
  705. if (!hasher)
  706. return -ENOMEM;
  707. hasher->hash_tfm = crypto_alloc_hash(type, 0, CRYPTO_ALG_ASYNC);
  708. if (!hasher->hash_tfm) {
  709. kfree(hasher);
  710. return -EINVAL;
  711. }
  712. spin_lock(&info->hash_lock);
  713. list_add(&hasher->list, &info->hashers);
  714. spin_unlock(&info->hash_lock);
  715. return 0;
  716. }
  717. #endif
  718. static int btrfs_congested_fn(void *congested_data, int bdi_bits)
  719. {
  720. struct btrfs_fs_info *info = (struct btrfs_fs_info *)congested_data;
  721. int ret = 0;
  722. struct list_head *cur;
  723. struct btrfs_device *device;
  724. struct backing_dev_info *bdi;
  725. list_for_each(cur, &info->fs_devices->devices) {
  726. device = list_entry(cur, struct btrfs_device, dev_list);
  727. bdi = blk_get_backing_dev_info(device->bdev);
  728. if (bdi && bdi_congested(bdi, bdi_bits)) {
  729. ret = 1;
  730. break;
  731. }
  732. }
  733. return ret;
  734. }
  735. void btrfs_unplug_io_fn(struct backing_dev_info *bdi, struct page *page)
  736. {
  737. struct list_head *cur;
  738. struct btrfs_device *device;
  739. struct btrfs_fs_info *info;
  740. info = (struct btrfs_fs_info *)bdi->unplug_io_data;
  741. list_for_each(cur, &info->fs_devices->devices) {
  742. device = list_entry(cur, struct btrfs_device, dev_list);
  743. bdi = blk_get_backing_dev_info(device->bdev);
  744. if (bdi->unplug_io_fn) {
  745. bdi->unplug_io_fn(bdi, page);
  746. }
  747. }
  748. }
  749. static int setup_bdi(struct btrfs_fs_info *info, struct backing_dev_info *bdi)
  750. {
  751. bdi_init(bdi);
  752. bdi->ra_pages = default_backing_dev_info.ra_pages * 4;
  753. bdi->state = 0;
  754. bdi->capabilities = default_backing_dev_info.capabilities;
  755. bdi->unplug_io_fn = btrfs_unplug_io_fn;
  756. bdi->unplug_io_data = info;
  757. bdi->congested_fn = btrfs_congested_fn;
  758. bdi->congested_data = info;
  759. return 0;
  760. }
  761. static int bio_ready_for_csum(struct bio *bio)
  762. {
  763. u64 length = 0;
  764. u64 buf_len = 0;
  765. u64 start = 0;
  766. struct page *page;
  767. struct extent_io_tree *io_tree = NULL;
  768. struct btrfs_fs_info *info = NULL;
  769. struct bio_vec *bvec;
  770. int i;
  771. int ret;
  772. bio_for_each_segment(bvec, bio, i) {
  773. page = bvec->bv_page;
  774. if (page->private == EXTENT_PAGE_PRIVATE) {
  775. length += bvec->bv_len;
  776. continue;
  777. }
  778. if (!page->private) {
  779. length += bvec->bv_len;
  780. continue;
  781. }
  782. length = bvec->bv_len;
  783. buf_len = page->private >> 2;
  784. start = page_offset(page) + bvec->bv_offset;
  785. io_tree = &BTRFS_I(page->mapping->host)->io_tree;
  786. info = BTRFS_I(page->mapping->host)->root->fs_info;
  787. }
  788. /* are we fully contained in this bio? */
  789. if (buf_len <= length)
  790. return 1;
  791. ret = extent_range_uptodate(io_tree, start + length,
  792. start + buf_len - 1);
  793. if (ret == 1)
  794. return ret;
  795. return ret;
  796. }
  797. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  798. void btrfs_end_io_csum(void *p)
  799. #else
  800. void btrfs_end_io_csum(struct work_struct *work)
  801. #endif
  802. {
  803. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  804. struct btrfs_fs_info *fs_info = p;
  805. #else
  806. struct btrfs_fs_info *fs_info = container_of(work,
  807. struct btrfs_fs_info,
  808. end_io_work);
  809. #endif
  810. unsigned long flags;
  811. struct end_io_wq *end_io_wq;
  812. struct bio *bio;
  813. struct list_head *next;
  814. int error;
  815. int was_empty;
  816. while(1) {
  817. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  818. if (list_empty(&fs_info->end_io_work_list)) {
  819. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  820. flags);
  821. return;
  822. }
  823. next = fs_info->end_io_work_list.next;
  824. list_del(next);
  825. spin_unlock_irqrestore(&fs_info->end_io_work_lock, flags);
  826. end_io_wq = list_entry(next, struct end_io_wq, list);
  827. bio = end_io_wq->bio;
  828. if (end_io_wq->metadata && !bio_ready_for_csum(bio)) {
  829. spin_lock_irqsave(&fs_info->end_io_work_lock, flags);
  830. was_empty = list_empty(&fs_info->end_io_work_list);
  831. list_add_tail(&end_io_wq->list,
  832. &fs_info->end_io_work_list);
  833. spin_unlock_irqrestore(&fs_info->end_io_work_lock,
  834. flags);
  835. if (was_empty)
  836. return;
  837. continue;
  838. }
  839. error = end_io_wq->error;
  840. bio->bi_private = end_io_wq->private;
  841. bio->bi_end_io = end_io_wq->end_io;
  842. kfree(end_io_wq);
  843. bio_endio(bio, error);
  844. }
  845. }
  846. struct btrfs_root *open_ctree(struct super_block *sb,
  847. struct btrfs_fs_devices *fs_devices)
  848. {
  849. u32 sectorsize;
  850. u32 nodesize;
  851. u32 leafsize;
  852. u32 blocksize;
  853. u32 stripesize;
  854. struct btrfs_root *extent_root = kmalloc(sizeof(struct btrfs_root),
  855. GFP_NOFS);
  856. struct btrfs_root *tree_root = kmalloc(sizeof(struct btrfs_root),
  857. GFP_NOFS);
  858. struct btrfs_fs_info *fs_info = kzalloc(sizeof(*fs_info),
  859. GFP_NOFS);
  860. struct btrfs_root *chunk_root = kmalloc(sizeof(struct btrfs_root),
  861. GFP_NOFS);
  862. struct btrfs_root *dev_root = kmalloc(sizeof(struct btrfs_root),
  863. GFP_NOFS);
  864. int ret;
  865. int err = -EINVAL;
  866. struct btrfs_super_block *disk_super;
  867. if (!extent_root || !tree_root || !fs_info) {
  868. err = -ENOMEM;
  869. goto fail;
  870. }
  871. end_io_workqueue = create_workqueue("btrfs-end-io");
  872. BUG_ON(!end_io_workqueue);
  873. INIT_RADIX_TREE(&fs_info->fs_roots_radix, GFP_NOFS);
  874. INIT_LIST_HEAD(&fs_info->trans_list);
  875. INIT_LIST_HEAD(&fs_info->dead_roots);
  876. INIT_LIST_HEAD(&fs_info->hashers);
  877. INIT_LIST_HEAD(&fs_info->end_io_work_list);
  878. spin_lock_init(&fs_info->hash_lock);
  879. spin_lock_init(&fs_info->end_io_work_lock);
  880. spin_lock_init(&fs_info->delalloc_lock);
  881. spin_lock_init(&fs_info->new_trans_lock);
  882. init_completion(&fs_info->kobj_unregister);
  883. sb_set_blocksize(sb, BTRFS_SUPER_INFO_SIZE);
  884. fs_info->tree_root = tree_root;
  885. fs_info->extent_root = extent_root;
  886. fs_info->chunk_root = chunk_root;
  887. fs_info->dev_root = dev_root;
  888. fs_info->fs_devices = fs_devices;
  889. INIT_LIST_HEAD(&fs_info->dirty_cowonly_roots);
  890. INIT_LIST_HEAD(&fs_info->space_info);
  891. btrfs_mapping_init(&fs_info->mapping_tree);
  892. fs_info->sb = sb;
  893. fs_info->max_extent = (u64)-1;
  894. fs_info->max_inline = 8192 * 1024;
  895. setup_bdi(fs_info, &fs_info->bdi);
  896. fs_info->btree_inode = new_inode(sb);
  897. fs_info->btree_inode->i_ino = 1;
  898. fs_info->btree_inode->i_nlink = 1;
  899. fs_info->btree_inode->i_size = sb->s_bdev->bd_inode->i_size;
  900. fs_info->btree_inode->i_mapping->a_ops = &btree_aops;
  901. fs_info->btree_inode->i_mapping->backing_dev_info = &fs_info->bdi;
  902. extent_io_tree_init(&BTRFS_I(fs_info->btree_inode)->io_tree,
  903. fs_info->btree_inode->i_mapping,
  904. GFP_NOFS);
  905. extent_map_tree_init(&BTRFS_I(fs_info->btree_inode)->extent_tree,
  906. GFP_NOFS);
  907. BTRFS_I(fs_info->btree_inode)->io_tree.ops = &btree_extent_io_ops;
  908. extent_io_tree_init(&fs_info->free_space_cache,
  909. fs_info->btree_inode->i_mapping, GFP_NOFS);
  910. extent_io_tree_init(&fs_info->block_group_cache,
  911. fs_info->btree_inode->i_mapping, GFP_NOFS);
  912. extent_io_tree_init(&fs_info->pinned_extents,
  913. fs_info->btree_inode->i_mapping, GFP_NOFS);
  914. extent_io_tree_init(&fs_info->pending_del,
  915. fs_info->btree_inode->i_mapping, GFP_NOFS);
  916. extent_io_tree_init(&fs_info->extent_ins,
  917. fs_info->btree_inode->i_mapping, GFP_NOFS);
  918. fs_info->do_barriers = 1;
  919. INIT_WORK(&fs_info->end_io_work, btrfs_end_io_csum);
  920. #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
  921. INIT_WORK(&fs_info->trans_work, btrfs_transaction_cleaner, fs_info);
  922. #else
  923. INIT_DELAYED_WORK(&fs_info->trans_work, btrfs_transaction_cleaner);
  924. #endif
  925. BTRFS_I(fs_info->btree_inode)->root = tree_root;
  926. memset(&BTRFS_I(fs_info->btree_inode)->location, 0,
  927. sizeof(struct btrfs_key));
  928. insert_inode_hash(fs_info->btree_inode);
  929. mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
  930. mutex_init(&fs_info->trans_mutex);
  931. mutex_init(&fs_info->fs_mutex);
  932. #if 0
  933. ret = add_hasher(fs_info, "crc32c");
  934. if (ret) {
  935. printk("btrfs: failed hash setup, modprobe cryptomgr?\n");
  936. err = -ENOMEM;
  937. goto fail_iput;
  938. }
  939. #endif
  940. __setup_root(4096, 4096, 4096, 4096, tree_root,
  941. fs_info, BTRFS_ROOT_TREE_OBJECTID);
  942. fs_info->sb_buffer = read_tree_block(tree_root,
  943. BTRFS_SUPER_INFO_OFFSET,
  944. 4096);
  945. if (!fs_info->sb_buffer)
  946. goto fail_iput;
  947. read_extent_buffer(fs_info->sb_buffer, &fs_info->super_copy, 0,
  948. sizeof(fs_info->super_copy));
  949. read_extent_buffer(fs_info->sb_buffer, fs_info->fsid,
  950. (unsigned long)btrfs_super_fsid(fs_info->sb_buffer),
  951. BTRFS_FSID_SIZE);
  952. disk_super = &fs_info->super_copy;
  953. if (!btrfs_super_root(disk_super))
  954. goto fail_sb_buffer;
  955. if (btrfs_super_num_devices(disk_super) != fs_devices->num_devices) {
  956. printk("Btrfs: wanted %llu devices, but found %llu\n",
  957. (unsigned long long)btrfs_super_num_devices(disk_super),
  958. (unsigned long long)fs_devices->num_devices);
  959. goto fail_sb_buffer;
  960. }
  961. nodesize = btrfs_super_nodesize(disk_super);
  962. leafsize = btrfs_super_leafsize(disk_super);
  963. sectorsize = btrfs_super_sectorsize(disk_super);
  964. stripesize = btrfs_super_stripesize(disk_super);
  965. tree_root->nodesize = nodesize;
  966. tree_root->leafsize = leafsize;
  967. tree_root->sectorsize = sectorsize;
  968. tree_root->stripesize = stripesize;
  969. sb_set_blocksize(sb, sectorsize);
  970. i_size_write(fs_info->btree_inode,
  971. btrfs_super_total_bytes(disk_super));
  972. if (strncmp((char *)(&disk_super->magic), BTRFS_MAGIC,
  973. sizeof(disk_super->magic))) {
  974. printk("btrfs: valid FS not found on %s\n", sb->s_id);
  975. goto fail_sb_buffer;
  976. }
  977. mutex_lock(&fs_info->fs_mutex);
  978. ret = btrfs_read_sys_array(tree_root);
  979. BUG_ON(ret);
  980. blocksize = btrfs_level_size(tree_root,
  981. btrfs_super_chunk_root_level(disk_super));
  982. __setup_root(nodesize, leafsize, sectorsize, stripesize,
  983. chunk_root, fs_info, BTRFS_CHUNK_TREE_OBJECTID);
  984. chunk_root->node = read_tree_block(chunk_root,
  985. btrfs_super_chunk_root(disk_super),
  986. blocksize);
  987. BUG_ON(!chunk_root->node);
  988. ret = btrfs_read_chunk_tree(chunk_root);
  989. BUG_ON(ret);
  990. blocksize = btrfs_level_size(tree_root,
  991. btrfs_super_root_level(disk_super));
  992. tree_root->node = read_tree_block(tree_root,
  993. btrfs_super_root(disk_super),
  994. blocksize);
  995. if (!tree_root->node)
  996. goto fail_sb_buffer;
  997. ret = find_and_setup_root(tree_root, fs_info,
  998. BTRFS_EXTENT_TREE_OBJECTID, extent_root);
  999. if (ret)
  1000. goto fail_tree_root;
  1001. extent_root->track_dirty = 1;
  1002. ret = find_and_setup_root(tree_root, fs_info,
  1003. BTRFS_DEV_TREE_OBJECTID, dev_root);
  1004. dev_root->track_dirty = 1;
  1005. if (ret)
  1006. goto fail_extent_root;
  1007. btrfs_read_block_groups(extent_root);
  1008. fs_info->generation = btrfs_super_generation(disk_super) + 1;
  1009. fs_info->data_alloc_profile = (u64)-1;
  1010. fs_info->metadata_alloc_profile = (u64)-1;
  1011. fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
  1012. mutex_unlock(&fs_info->fs_mutex);
  1013. return tree_root;
  1014. fail_extent_root:
  1015. free_extent_buffer(extent_root->node);
  1016. fail_tree_root:
  1017. mutex_unlock(&fs_info->fs_mutex);
  1018. free_extent_buffer(tree_root->node);
  1019. fail_sb_buffer:
  1020. free_extent_buffer(fs_info->sb_buffer);
  1021. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1022. fail_iput:
  1023. iput(fs_info->btree_inode);
  1024. fail:
  1025. close_all_devices(fs_info);
  1026. kfree(extent_root);
  1027. kfree(tree_root);
  1028. bdi_destroy(&fs_info->bdi);
  1029. kfree(fs_info);
  1030. return ERR_PTR(err);
  1031. }
  1032. static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
  1033. {
  1034. char b[BDEVNAME_SIZE];
  1035. if (uptodate) {
  1036. set_buffer_uptodate(bh);
  1037. } else {
  1038. if (!buffer_eopnotsupp(bh) && printk_ratelimit()) {
  1039. printk(KERN_WARNING "lost page write due to "
  1040. "I/O error on %s\n",
  1041. bdevname(bh->b_bdev, b));
  1042. }
  1043. set_buffer_write_io_error(bh);
  1044. clear_buffer_uptodate(bh);
  1045. }
  1046. unlock_buffer(bh);
  1047. put_bh(bh);
  1048. }
  1049. int write_all_supers(struct btrfs_root *root)
  1050. {
  1051. struct list_head *cur;
  1052. struct list_head *head = &root->fs_info->fs_devices->devices;
  1053. struct btrfs_device *dev;
  1054. struct extent_buffer *sb;
  1055. struct btrfs_dev_item *dev_item;
  1056. struct buffer_head *bh;
  1057. int ret;
  1058. int do_barriers;
  1059. do_barriers = !btrfs_test_opt(root, NOBARRIER);
  1060. sb = root->fs_info->sb_buffer;
  1061. dev_item = (struct btrfs_dev_item *)offsetof(struct btrfs_super_block,
  1062. dev_item);
  1063. list_for_each(cur, head) {
  1064. dev = list_entry(cur, struct btrfs_device, dev_list);
  1065. btrfs_set_device_type(sb, dev_item, dev->type);
  1066. btrfs_set_device_id(sb, dev_item, dev->devid);
  1067. btrfs_set_device_total_bytes(sb, dev_item, dev->total_bytes);
  1068. btrfs_set_device_bytes_used(sb, dev_item, dev->bytes_used);
  1069. btrfs_set_device_io_align(sb, dev_item, dev->io_align);
  1070. btrfs_set_device_io_width(sb, dev_item, dev->io_width);
  1071. btrfs_set_device_sector_size(sb, dev_item, dev->sector_size);
  1072. write_extent_buffer(sb, dev->uuid,
  1073. (unsigned long)btrfs_device_uuid(dev_item),
  1074. BTRFS_DEV_UUID_SIZE);
  1075. btrfs_set_header_flag(sb, BTRFS_HEADER_FLAG_WRITTEN);
  1076. csum_tree_block(root, sb, 0);
  1077. bh = __getblk(dev->bdev, BTRFS_SUPER_INFO_OFFSET /
  1078. root->fs_info->sb->s_blocksize,
  1079. BTRFS_SUPER_INFO_SIZE);
  1080. read_extent_buffer(sb, bh->b_data, 0, BTRFS_SUPER_INFO_SIZE);
  1081. dev->pending_io = bh;
  1082. get_bh(bh);
  1083. set_buffer_uptodate(bh);
  1084. lock_buffer(bh);
  1085. bh->b_end_io = btrfs_end_buffer_write_sync;
  1086. if (do_barriers && dev->barriers) {
  1087. ret = submit_bh(WRITE_BARRIER, bh);
  1088. if (ret == -EOPNOTSUPP) {
  1089. printk("btrfs: disabling barriers on dev %s\n",
  1090. dev->name);
  1091. set_buffer_uptodate(bh);
  1092. dev->barriers = 0;
  1093. get_bh(bh);
  1094. lock_buffer(bh);
  1095. ret = submit_bh(WRITE, bh);
  1096. }
  1097. } else {
  1098. ret = submit_bh(WRITE, bh);
  1099. }
  1100. BUG_ON(ret);
  1101. }
  1102. list_for_each(cur, head) {
  1103. dev = list_entry(cur, struct btrfs_device, dev_list);
  1104. BUG_ON(!dev->pending_io);
  1105. bh = dev->pending_io;
  1106. wait_on_buffer(bh);
  1107. if (!buffer_uptodate(dev->pending_io)) {
  1108. if (do_barriers && dev->barriers) {
  1109. printk("btrfs: disabling barriers on dev %s\n",
  1110. dev->name);
  1111. set_buffer_uptodate(bh);
  1112. get_bh(bh);
  1113. lock_buffer(bh);
  1114. dev->barriers = 0;
  1115. ret = submit_bh(WRITE, bh);
  1116. BUG_ON(ret);
  1117. wait_on_buffer(bh);
  1118. BUG_ON(!buffer_uptodate(bh));
  1119. } else {
  1120. BUG();
  1121. }
  1122. }
  1123. dev->pending_io = NULL;
  1124. brelse(bh);
  1125. }
  1126. return 0;
  1127. }
  1128. int write_ctree_super(struct btrfs_trans_handle *trans, struct btrfs_root
  1129. *root)
  1130. {
  1131. int ret;
  1132. ret = write_all_supers(root);
  1133. #if 0
  1134. if (!btrfs_test_opt(root, NOBARRIER))
  1135. blkdev_issue_flush(sb->s_bdev, NULL);
  1136. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, super);
  1137. ret = sync_page_range_nolock(btree_inode, btree_inode->i_mapping,
  1138. super->start, super->len);
  1139. if (!btrfs_test_opt(root, NOBARRIER))
  1140. blkdev_issue_flush(sb->s_bdev, NULL);
  1141. #endif
  1142. return ret;
  1143. }
  1144. int btrfs_free_fs_root(struct btrfs_fs_info *fs_info, struct btrfs_root *root)
  1145. {
  1146. radix_tree_delete(&fs_info->fs_roots_radix,
  1147. (unsigned long)root->root_key.objectid);
  1148. if (root->in_sysfs)
  1149. btrfs_sysfs_del_root(root);
  1150. if (root->inode)
  1151. iput(root->inode);
  1152. if (root->node)
  1153. free_extent_buffer(root->node);
  1154. if (root->commit_root)
  1155. free_extent_buffer(root->commit_root);
  1156. if (root->name)
  1157. kfree(root->name);
  1158. kfree(root);
  1159. return 0;
  1160. }
  1161. static int del_fs_roots(struct btrfs_fs_info *fs_info)
  1162. {
  1163. int ret;
  1164. struct btrfs_root *gang[8];
  1165. int i;
  1166. while(1) {
  1167. ret = radix_tree_gang_lookup(&fs_info->fs_roots_radix,
  1168. (void **)gang, 0,
  1169. ARRAY_SIZE(gang));
  1170. if (!ret)
  1171. break;
  1172. for (i = 0; i < ret; i++)
  1173. btrfs_free_fs_root(fs_info, gang[i]);
  1174. }
  1175. return 0;
  1176. }
  1177. int close_ctree(struct btrfs_root *root)
  1178. {
  1179. int ret;
  1180. struct btrfs_trans_handle *trans;
  1181. struct btrfs_fs_info *fs_info = root->fs_info;
  1182. fs_info->closing = 1;
  1183. btrfs_transaction_flush_work(root);
  1184. mutex_lock(&fs_info->fs_mutex);
  1185. btrfs_defrag_dirty_roots(root->fs_info);
  1186. trans = btrfs_start_transaction(root, 1);
  1187. ret = btrfs_commit_transaction(trans, root);
  1188. /* run commit again to drop the original snapshot */
  1189. trans = btrfs_start_transaction(root, 1);
  1190. btrfs_commit_transaction(trans, root);
  1191. ret = btrfs_write_and_wait_transaction(NULL, root);
  1192. BUG_ON(ret);
  1193. write_ctree_super(NULL, root);
  1194. mutex_unlock(&fs_info->fs_mutex);
  1195. if (fs_info->delalloc_bytes) {
  1196. printk("btrfs: at unmount delalloc count %Lu\n",
  1197. fs_info->delalloc_bytes);
  1198. }
  1199. if (fs_info->extent_root->node)
  1200. free_extent_buffer(fs_info->extent_root->node);
  1201. if (fs_info->tree_root->node)
  1202. free_extent_buffer(fs_info->tree_root->node);
  1203. if (root->fs_info->chunk_root->node);
  1204. free_extent_buffer(root->fs_info->chunk_root->node);
  1205. if (root->fs_info->dev_root->node);
  1206. free_extent_buffer(root->fs_info->dev_root->node);
  1207. free_extent_buffer(fs_info->sb_buffer);
  1208. btrfs_free_block_groups(root->fs_info);
  1209. del_fs_roots(fs_info);
  1210. filemap_write_and_wait(fs_info->btree_inode->i_mapping);
  1211. extent_io_tree_empty_lru(&fs_info->free_space_cache);
  1212. extent_io_tree_empty_lru(&fs_info->block_group_cache);
  1213. extent_io_tree_empty_lru(&fs_info->pinned_extents);
  1214. extent_io_tree_empty_lru(&fs_info->pending_del);
  1215. extent_io_tree_empty_lru(&fs_info->extent_ins);
  1216. extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
  1217. truncate_inode_pages(fs_info->btree_inode->i_mapping, 0);
  1218. flush_workqueue(end_io_workqueue);
  1219. destroy_workqueue(end_io_workqueue);
  1220. iput(fs_info->btree_inode);
  1221. #if 0
  1222. while(!list_empty(&fs_info->hashers)) {
  1223. struct btrfs_hasher *hasher;
  1224. hasher = list_entry(fs_info->hashers.next, struct btrfs_hasher,
  1225. hashers);
  1226. list_del(&hasher->hashers);
  1227. crypto_free_hash(&fs_info->hash_tfm);
  1228. kfree(hasher);
  1229. }
  1230. #endif
  1231. close_all_devices(fs_info);
  1232. btrfs_mapping_tree_free(&fs_info->mapping_tree);
  1233. bdi_destroy(&fs_info->bdi);
  1234. kfree(fs_info->extent_root);
  1235. kfree(fs_info->tree_root);
  1236. kfree(fs_info->chunk_root);
  1237. kfree(fs_info->dev_root);
  1238. return 0;
  1239. }
  1240. int btrfs_buffer_uptodate(struct extent_buffer *buf)
  1241. {
  1242. struct inode *btree_inode = buf->first_page->mapping->host;
  1243. return extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree, buf);
  1244. }
  1245. int btrfs_set_buffer_uptodate(struct extent_buffer *buf)
  1246. {
  1247. struct inode *btree_inode = buf->first_page->mapping->host;
  1248. return set_extent_buffer_uptodate(&BTRFS_I(btree_inode)->io_tree,
  1249. buf);
  1250. }
  1251. void btrfs_mark_buffer_dirty(struct extent_buffer *buf)
  1252. {
  1253. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1254. u64 transid = btrfs_header_generation(buf);
  1255. struct inode *btree_inode = root->fs_info->btree_inode;
  1256. if (transid != root->fs_info->generation) {
  1257. printk(KERN_CRIT "transid mismatch buffer %llu, found %Lu running %Lu\n",
  1258. (unsigned long long)buf->start,
  1259. transid, root->fs_info->generation);
  1260. WARN_ON(1);
  1261. }
  1262. set_extent_buffer_dirty(&BTRFS_I(btree_inode)->io_tree, buf);
  1263. }
  1264. void btrfs_throttle(struct btrfs_root *root)
  1265. {
  1266. struct backing_dev_info *bdi;
  1267. bdi = root->fs_info->sb->s_bdev->bd_inode->i_mapping->backing_dev_info;
  1268. if (root->fs_info->throttles && bdi_write_congested(bdi)) {
  1269. #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
  1270. congestion_wait(WRITE, HZ/20);
  1271. #else
  1272. blk_congestion_wait(WRITE, HZ/20);
  1273. #endif
  1274. }
  1275. }
  1276. void btrfs_btree_balance_dirty(struct btrfs_root *root, unsigned long nr)
  1277. {
  1278. balance_dirty_pages_ratelimited_nr(
  1279. root->fs_info->btree_inode->i_mapping, 1);
  1280. }
  1281. void btrfs_set_buffer_defrag(struct extent_buffer *buf)
  1282. {
  1283. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1284. struct inode *btree_inode = root->fs_info->btree_inode;
  1285. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1286. buf->start + buf->len - 1, EXTENT_DEFRAG, GFP_NOFS);
  1287. }
  1288. void btrfs_set_buffer_defrag_done(struct extent_buffer *buf)
  1289. {
  1290. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1291. struct inode *btree_inode = root->fs_info->btree_inode;
  1292. set_extent_bits(&BTRFS_I(btree_inode)->io_tree, buf->start,
  1293. buf->start + buf->len - 1, EXTENT_DEFRAG_DONE,
  1294. GFP_NOFS);
  1295. }
  1296. int btrfs_buffer_defrag(struct extent_buffer *buf)
  1297. {
  1298. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1299. struct inode *btree_inode = root->fs_info->btree_inode;
  1300. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1301. buf->start, buf->start + buf->len - 1, EXTENT_DEFRAG, 0);
  1302. }
  1303. int btrfs_buffer_defrag_done(struct extent_buffer *buf)
  1304. {
  1305. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1306. struct inode *btree_inode = root->fs_info->btree_inode;
  1307. return test_range_bit(&BTRFS_I(btree_inode)->io_tree,
  1308. buf->start, buf->start + buf->len - 1,
  1309. EXTENT_DEFRAG_DONE, 0);
  1310. }
  1311. int btrfs_clear_buffer_defrag_done(struct extent_buffer *buf)
  1312. {
  1313. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1314. struct inode *btree_inode = root->fs_info->btree_inode;
  1315. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1316. buf->start, buf->start + buf->len - 1,
  1317. EXTENT_DEFRAG_DONE, GFP_NOFS);
  1318. }
  1319. int btrfs_clear_buffer_defrag(struct extent_buffer *buf)
  1320. {
  1321. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1322. struct inode *btree_inode = root->fs_info->btree_inode;
  1323. return clear_extent_bits(&BTRFS_I(btree_inode)->io_tree,
  1324. buf->start, buf->start + buf->len - 1,
  1325. EXTENT_DEFRAG, GFP_NOFS);
  1326. }
  1327. int btrfs_read_buffer(struct extent_buffer *buf)
  1328. {
  1329. struct btrfs_root *root = BTRFS_I(buf->first_page->mapping->host)->root;
  1330. int ret;
  1331. ret = btree_read_extent_buffer_pages(root, buf, 0);
  1332. if (ret == 0) {
  1333. buf->flags |= EXTENT_UPTODATE;
  1334. }
  1335. return ret;
  1336. }
  1337. static struct extent_io_ops btree_extent_io_ops = {
  1338. .writepage_io_hook = btree_writepage_io_hook,
  1339. .readpage_end_io_hook = btree_readpage_end_io_hook,
  1340. .submit_bio_hook = btree_submit_bio_hook,
  1341. /* note we're sharing with inode.c for the merge bio hook */
  1342. .merge_bio_hook = btrfs_merge_bio_hook,
  1343. };