disk-io.c 61 KB

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