btnode.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * btnode.c - NILFS B-tree node cache
  4. *
  5. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  6. *
  7. * Originally written by Seiji Kihara.
  8. * Fully revised by Ryusuke Konishi for stabilization and simplification.
  9. *
  10. */
  11. #include <linux/types.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/mm.h>
  14. #include <linux/backing-dev.h>
  15. #include <linux/gfp.h>
  16. #include "nilfs.h"
  17. #include "mdt.h"
  18. #include "dat.h"
  19. #include "page.h"
  20. #include "btnode.h"
  21. void nilfs_btnode_cache_clear(struct address_space *btnc)
  22. {
  23. invalidate_mapping_pages(btnc, 0, -1);
  24. truncate_inode_pages(btnc, 0);
  25. }
  26. struct buffer_head *
  27. nilfs_btnode_create_block(struct address_space *btnc, __u64 blocknr)
  28. {
  29. struct inode *inode = NILFS_BTNC_I(btnc);
  30. struct buffer_head *bh;
  31. bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
  32. if (unlikely(!bh))
  33. return NULL;
  34. if (unlikely(buffer_mapped(bh) || buffer_uptodate(bh) ||
  35. buffer_dirty(bh))) {
  36. brelse(bh);
  37. BUG();
  38. }
  39. memset(bh->b_data, 0, i_blocksize(inode));
  40. bh->b_bdev = inode->i_sb->s_bdev;
  41. bh->b_blocknr = blocknr;
  42. set_buffer_mapped(bh);
  43. set_buffer_uptodate(bh);
  44. unlock_page(bh->b_page);
  45. put_page(bh->b_page);
  46. return bh;
  47. }
  48. int nilfs_btnode_submit_block(struct address_space *btnc, __u64 blocknr,
  49. sector_t pblocknr, int mode, int mode_flags,
  50. struct buffer_head **pbh, sector_t *submit_ptr)
  51. {
  52. struct buffer_head *bh;
  53. struct inode *inode = NILFS_BTNC_I(btnc);
  54. struct page *page;
  55. int err;
  56. bh = nilfs_grab_buffer(inode, btnc, blocknr, BIT(BH_NILFS_Node));
  57. if (unlikely(!bh))
  58. return -ENOMEM;
  59. err = -EEXIST; /* internal code */
  60. page = bh->b_page;
  61. if (buffer_uptodate(bh) || buffer_dirty(bh))
  62. goto found;
  63. if (pblocknr == 0) {
  64. pblocknr = blocknr;
  65. if (inode->i_ino != NILFS_DAT_INO) {
  66. struct the_nilfs *nilfs = inode->i_sb->s_fs_info;
  67. /* blocknr is a virtual block number */
  68. err = nilfs_dat_translate(nilfs->ns_dat, blocknr,
  69. &pblocknr);
  70. if (unlikely(err)) {
  71. brelse(bh);
  72. goto out_locked;
  73. }
  74. }
  75. }
  76. if (mode_flags & REQ_RAHEAD) {
  77. if (pblocknr != *submit_ptr + 1 || !trylock_buffer(bh)) {
  78. err = -EBUSY; /* internal code */
  79. brelse(bh);
  80. goto out_locked;
  81. }
  82. } else { /* mode == READ */
  83. lock_buffer(bh);
  84. }
  85. if (buffer_uptodate(bh)) {
  86. unlock_buffer(bh);
  87. err = -EEXIST; /* internal code */
  88. goto found;
  89. }
  90. set_buffer_mapped(bh);
  91. bh->b_bdev = inode->i_sb->s_bdev;
  92. bh->b_blocknr = pblocknr; /* set block address for read */
  93. bh->b_end_io = end_buffer_read_sync;
  94. get_bh(bh);
  95. submit_bh(mode, mode_flags, bh);
  96. bh->b_blocknr = blocknr; /* set back to the given block address */
  97. *submit_ptr = pblocknr;
  98. err = 0;
  99. found:
  100. *pbh = bh;
  101. out_locked:
  102. unlock_page(page);
  103. put_page(page);
  104. return err;
  105. }
  106. /**
  107. * nilfs_btnode_delete - delete B-tree node buffer
  108. * @bh: buffer to be deleted
  109. *
  110. * nilfs_btnode_delete() invalidates the specified buffer and delete the page
  111. * including the buffer if the page gets unbusy.
  112. */
  113. void nilfs_btnode_delete(struct buffer_head *bh)
  114. {
  115. struct address_space *mapping;
  116. struct page *page = bh->b_page;
  117. pgoff_t index = page_index(page);
  118. int still_dirty;
  119. get_page(page);
  120. lock_page(page);
  121. wait_on_page_writeback(page);
  122. nilfs_forget_buffer(bh);
  123. still_dirty = PageDirty(page);
  124. mapping = page->mapping;
  125. unlock_page(page);
  126. put_page(page);
  127. if (!still_dirty && mapping)
  128. invalidate_inode_pages2_range(mapping, index, index);
  129. }
  130. /**
  131. * nilfs_btnode_prepare_change_key
  132. * prepare to move contents of the block for old key to one of new key.
  133. * the old buffer will not be removed, but might be reused for new buffer.
  134. * it might return -ENOMEM because of memory allocation errors,
  135. * and might return -EIO because of disk read errors.
  136. */
  137. int nilfs_btnode_prepare_change_key(struct address_space *btnc,
  138. struct nilfs_btnode_chkey_ctxt *ctxt)
  139. {
  140. struct buffer_head *obh, *nbh;
  141. struct inode *inode = NILFS_BTNC_I(btnc);
  142. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  143. int err;
  144. if (oldkey == newkey)
  145. return 0;
  146. obh = ctxt->bh;
  147. ctxt->newbh = NULL;
  148. if (inode->i_blkbits == PAGE_SHIFT) {
  149. struct page *opage = obh->b_page;
  150. lock_page(opage);
  151. retry:
  152. /* BUG_ON(oldkey != obh->b_page->index); */
  153. if (unlikely(oldkey != opage->index))
  154. NILFS_PAGE_BUG(opage,
  155. "invalid oldkey %lld (newkey=%lld)",
  156. (unsigned long long)oldkey,
  157. (unsigned long long)newkey);
  158. xa_lock_irq(&btnc->i_pages);
  159. err = __xa_insert(&btnc->i_pages, newkey, opage, GFP_NOFS);
  160. xa_unlock_irq(&btnc->i_pages);
  161. /*
  162. * Note: page->index will not change to newkey until
  163. * nilfs_btnode_commit_change_key() will be called.
  164. * To protect the page in intermediate state, the page lock
  165. * is held.
  166. */
  167. if (!err)
  168. return 0;
  169. else if (err != -EBUSY)
  170. goto failed_unlock;
  171. err = invalidate_inode_pages2_range(btnc, newkey, newkey);
  172. if (!err)
  173. goto retry;
  174. /* fallback to copy mode */
  175. unlock_page(opage);
  176. }
  177. nbh = nilfs_btnode_create_block(btnc, newkey);
  178. if (!nbh)
  179. return -ENOMEM;
  180. BUG_ON(nbh == obh);
  181. ctxt->newbh = nbh;
  182. return 0;
  183. failed_unlock:
  184. unlock_page(obh->b_page);
  185. return err;
  186. }
  187. /**
  188. * nilfs_btnode_commit_change_key
  189. * commit the change_key operation prepared by prepare_change_key().
  190. */
  191. void nilfs_btnode_commit_change_key(struct address_space *btnc,
  192. struct nilfs_btnode_chkey_ctxt *ctxt)
  193. {
  194. struct buffer_head *obh = ctxt->bh, *nbh = ctxt->newbh;
  195. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  196. struct page *opage;
  197. if (oldkey == newkey)
  198. return;
  199. if (nbh == NULL) { /* blocksize == pagesize */
  200. opage = obh->b_page;
  201. if (unlikely(oldkey != opage->index))
  202. NILFS_PAGE_BUG(opage,
  203. "invalid oldkey %lld (newkey=%lld)",
  204. (unsigned long long)oldkey,
  205. (unsigned long long)newkey);
  206. mark_buffer_dirty(obh);
  207. xa_lock_irq(&btnc->i_pages);
  208. __xa_erase(&btnc->i_pages, oldkey);
  209. __xa_set_mark(&btnc->i_pages, newkey, PAGECACHE_TAG_DIRTY);
  210. xa_unlock_irq(&btnc->i_pages);
  211. opage->index = obh->b_blocknr = newkey;
  212. unlock_page(opage);
  213. } else {
  214. nilfs_copy_buffer(nbh, obh);
  215. mark_buffer_dirty(nbh);
  216. nbh->b_blocknr = newkey;
  217. ctxt->bh = nbh;
  218. nilfs_btnode_delete(obh); /* will decrement bh->b_count */
  219. }
  220. }
  221. /**
  222. * nilfs_btnode_abort_change_key
  223. * abort the change_key operation prepared by prepare_change_key().
  224. */
  225. void nilfs_btnode_abort_change_key(struct address_space *btnc,
  226. struct nilfs_btnode_chkey_ctxt *ctxt)
  227. {
  228. struct buffer_head *nbh = ctxt->newbh;
  229. __u64 oldkey = ctxt->oldkey, newkey = ctxt->newkey;
  230. if (oldkey == newkey)
  231. return;
  232. if (nbh == NULL) { /* blocksize == pagesize */
  233. xa_erase_irq(&btnc->i_pages, newkey);
  234. unlock_page(ctxt->bh->b_page);
  235. } else
  236. brelse(nbh);
  237. }