btnode.c 7.9 KB

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