btnode.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * btnode.c - NILFS B-tree node cache
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Originally written by Seiji Kihara.
  17. * Fully revised by Ryusuke Konishi for stabilization and simplification.
  18. *
  19. */
  20. #include <linux/types.h>
  21. #include <linux/buffer_head.h>
  22. #include <linux/mm.h>
  23. #include <linux/backing-dev.h>
  24. #include <linux/gfp.h>
  25. #include "nilfs.h"
  26. #include "mdt.h"
  27. #include "dat.h"
  28. #include "page.h"
  29. #include "btnode.h"
  30. void nilfs_btnode_cache_clear(struct address_space *btnc)
  31. {
  32. invalidate_mapping_pages(btnc, 0, -1);
  33. truncate_inode_pages(btnc, 0);
  34. }
  35. struct buffer_head *
  36. nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
  37. {
  38. struct inode *inode = NILFS_BTNC_I(btnc);
  39. struct buffer_head *bh;
  40. bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
  41. if (unlikely(!bh))
  42. return NULL;
  43. if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
  44. buffer_dirty(bh))) {
  45. brelse(bh);
  46. BUG();
  47. }
  48. memset(bh->b_data, 0, i_blocksize(inode));
  49. bh->b_bdev = inode->i_sb->s_bdev;
  50. bh->b_blocknr = blocknr;
  51. set_buffer_mapped(bh);
  52. set_buffer_uptodate(bh);
  53. unlock_page(bh->b_page);
  54. put_page(bh->b_page);
  55. return bh;
  56. }
  57. int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
  58. sector_t pblocknr, int mode, int mode_flags,
  59. struct buffer_head **pbh, sector_t *submit_ptr)
  60. {
  61. struct buffer_head *bh;
  62. struct inode *inode = NILFS_BTNC_I(btnc);
  63. struct page *page;
  64. int err;
  65. bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
  66. if (unlikely(!bh))
  67. return -ENOMEM;
  68. err = -EEXIST; /* internal code */
  69. page = bh->b_page;
  70. if (buffer_uptodate(bh) || buffer_dirty(bh))
  71. goto found;
  72. if (pblocknr == 0) {
  73. pblocknr = blocknr;
  74. if (inode->i_ino != NILFS_DAT_INO) {
  75. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  76. /* blocknr is a virtual block number */
  77. err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
  78. &pblocknr);
  79. if (unlikely(err)) {
  80. brelse(bh);
  81. goto out_locked;
  82. }
  83. }
  84. }
  85. if (mode_flags & REQ_RAHEAD) {
  86. if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
  87. err = -EBUSY; /* internal code */
  88. brelse(bh);
  89. goto out_locked;
  90. }
  91. } else { /* mode == READ */
  92. lock_buffer(bh);
  93. }
  94. if (buffer_uptodate(bh)) {
  95. unlock_buffer(bh);
  96. err = -EEXIST; /* internal code */
  97. goto found;
  98. }
  99. set_buffer_mapped(bh);
  100. bh->b_bdev = inode->i_sb->s_bdev;
  101. bh->b_blocknr = pblocknr; /* set block address for read */
  102. bh->b_end_io = end_buffer_read_sync;
  103. get_bh(bh);
  104. submit_bh(mode, mode_flags, bh);
  105. bh->b_blocknr = blocknr; /* set back to the given block address */
  106. *submit_ptr = pblocknr;
  107. err = 0;
  108. found:
  109. *pbh = bh;
  110. out_locked:
  111. unlock_page(page);
  112. put_page(page);
  113. return err;
  114. }
  115. /**
  116. * nilfs_btnode_delete - delete B-tree node buffer
  117. * @bh: buffer to be deleted
  118. *
  119. * nilfs_btnode_delete() invalidates the specified buffer and delete the page
  120. * including the buffer if the page gets unbusy.
  121. */
  122. void nilfs_btnode_delete(struct buffer_head *bh)
  123. {
  124. struct address_space *mapping;
  125. struct page *page = bh->b_page;
  126. pgoff_t index = page_index(page);
  127. int still_dirty;
  128. get_page(page);
  129. lock_page(page);
  130. wait_on_page_writeback(page);
  131. nilfs_forget_buffer(bh);
  132. still_dirty = PageDirty(page);
  133. mapping = page->mapping;
  134. unlock_page(page);
  135. put_page(page);
  136. if (!still_dirty && mapping)
  137. invalidate_inode_pages2_range(mapping, index, index);
  138. }
  139. /**
  140. * nilfs_btnode_prepare_change_key
  141. * prepare to move contents of the block for old key to one of new key.
  142. * the old buffer will not be removed, but might be reused for new buffer.
  143. * it might return -ENOMEM because of memory allocation errors,
  144. * and might return -EIO because of disk read errors.
  145. */
  146. int nilfs_btnode_prepare_change_key(struct address_space *btnc,
  147. struct nilfs_btnode_chkey_ctxt *ctxt)
  148. {
  149. struct buffer_head *obh, *nbh;
  150. struct inode *inode = NILFS_BTNC_I(btnc);
  151. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  152. int err;
  153. if (oldkey == newkey)
  154. return 0;
  155. obh = ctxt->bh;
  156. ctxt->newbh = NULL;
  157. if (inode->i_blkbits == PAGE_SHIFT) {
  158. lock_page(obh->b_page);
  159. /*
  160. * We cannot call radix_tree_preload for the kernels older
  161. * than 2.6.23, because it is not exported for modules.
  162. */
  163. retry:
  164. err = radix_tree_preload(GFP_NOFS & ~__GFP_HIGHMEM);
  165. if (err)
  166. goto failed_unlock;
  167. /* BUG_ON(oldkey != obh->b_page->index); */
  168. if (unlikely(oldkey != obh->b_page->index))
  169. NILFS_PAGE_BUG(obh->b_page,
  170. "invalid oldkey %lld (newkey=%lld)",
  171. (unsigned long long)oldkey,
  172. (unsigned long long)newkey);
  173. spin_lock_irq(&btnc->tree_lock);
  174. err = radix_tree_insert(&btnc->page_tree, newkey, obh->b_page);
  175. spin_unlock_irq(&btnc->tree_lock);
  176. /*
  177. * Note: page->index will not change to newkey until
  178. * nilfs_btnode_commit_change_key() will be called.
  179. * To protect the page in intermediate state, the page lock
  180. * is held.
  181. */
  182. radix_tree_preload_end();
  183. if (!err)
  184. return 0;
  185. else if (err != -EEXIST)
  186. goto failed_unlock;
  187. err = invalidate_inode_pages2_range(btnc, newkey, newkey);
  188. if (!err)
  189. goto retry;
  190. /* fallback to copy mode */
  191. unlock_page(obh->b_page);
  192. }
  193. nbh = nilfs_btnode_create_block(btnc, newkey);
  194. if (!nbh)
  195. return -ENOMEM;
  196. BUG_ON(nbh == obh);
  197. ctxt->newbh = nbh;
  198. return 0;
  199. failed_unlock:
  200. unlock_page(obh->b_page);
  201. return err;
  202. }
  203. /**
  204. * nilfs_btnode_commit_change_key
  205. * commit the change_key operation prepared by prepare_change_key().
  206. */
  207. void nilfs_btnode_commit_change_key(struct address_space *btnc,
  208. struct nilfs_btnode_chkey_ctxt *ctxt)
  209. {
  210. struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
  211. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  212. struct page *opage;
  213. if (oldkey == newkey)
  214. return;
  215. if (nbh == NULL) { /* blocksize == pagesize */
  216. opage = obh->b_page;
  217. if (unlikely(oldkey != opage->index))
  218. NILFS_PAGE_BUG(opage,
  219. "invalid oldkey %lld (newkey=%lld)",
  220. (unsigned long long)oldkey,
  221. (unsigned long long)newkey);
  222. mark_buffer_dirty(obh);
  223. spin_lock_irq(&btnc->tree_lock);
  224. radix_tree_delete(&btnc->page_tree, oldkey);
  225. radix_tree_tag_set(&btnc->page_tree, newkey,
  226. PAGECACHE_TAG_DIRTY);
  227. spin_unlock_irq(&btnc->tree_lock);
  228. opage->index = obh->b_blocknr = newkey;
  229. unlock_page(opage);
  230. } else {
  231. nilfs_copy_buffer(nbh, obh);
  232. mark_buffer_dirty(nbh);
  233. nbh->b_blocknr = newkey;
  234. ctxt->bh = nbh;
  235. nilfs_btnode_delete(obh); /* will decrement bh->b_count */
  236. }
  237. }
  238. /**
  239. * nilfs_btnode_abort_change_key
  240. * abort the change_key operation prepared by prepare_change_key().
  241. */
  242. void nilfs_btnode_abort_change_key(struct address_space *btnc,
  243. struct nilfs_btnode_chkey_ctxt *ctxt)
  244. {
  245. struct buffer_head *nbh = ctxt->newbh;
  246. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  247. if (oldkey == newkey)
  248. return;
  249. if (nbh == NULL) { /* blocksize == pagesize */
  250. spin_lock_irq(&btnc->tree_lock);
  251. radix_tree_delete(&btnc->page_tree, newkey);
  252. spin_unlock_irq(&btnc->tree_lock);
  253. unlock_page(ctxt->bh->b_page);
  254. } else
  255. brelse(nbh);
  256. }