disk-io.c 55 KB

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