disk-io.c 48 KB

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