disk-io.c 50 KB

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