disk-io.c 47 KB

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