disk-io.c 41 KB

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