btnode.c 8.6 KB

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