disk-io.c 57 KB

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