disk-io.c 47 KB

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