disk-io.c 47 KB

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