disk-io.c 46 KB

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