disk-io.c 53 KB

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