btnode.c 8.5 KB

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