disk-io.c 46 KB

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