disk-io.c 50 KB

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