disk-io.c 58 KB

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