disk-io.c 46 KB

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