disk-io.c 44 KB

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