btnode.c 7.7 KB

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