btnode.c 7.8 KB

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