disk-io.c 44 KB

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