disk-io.c 54 KB

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