disk-io.c 60 KB

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