btnode.c 7.9 KB

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