disk-io.c 59 KB

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