disk-io.c 44 KB

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