disk-io.c 45 KB

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