disk-io.c 46 KB

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