xfs_icreate_item.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2008-2010, 2013 Dave Chinner
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_trans_priv.h"
  16. #include "xfs_icreate_item.h"
  17. #include "xfs_log.h"
  18. #include "xfs_log_priv.h"
  19. #include "xfs_log_recover.h"
  20. #include "xfs_ialloc.h"
  21. #include "xfs_trace.h"
  22. struct kmem_cache *xfs_icreate_cache; /* inode create item */
  23. static inline struct xfs_icreate_item *ICR_ITEM(struct xfs_log_item *lip)
  24. {
  25. return container_of(lip, struct xfs_icreate_item, ic_item);
  26. }
  27. /*
  28. * This returns the number of iovecs needed to log the given inode item.
  29. *
  30. * We only need one iovec for the icreate log structure.
  31. */
  32. STATIC void
  33. xfs_icreate_item_size(
  34. struct xfs_log_item *lip,
  35. int *nvecs,
  36. int *nbytes)
  37. {
  38. *nvecs += 1;
  39. *nbytes += sizeof(struct xfs_icreate_log);
  40. }
  41. /*
  42. * This is called to fill in the vector of log iovecs for the
  43. * given inode create log item.
  44. */
  45. STATIC void
  46. xfs_icreate_item_format(
  47. struct xfs_log_item *lip,
  48. struct xfs_log_vec *lv)
  49. {
  50. struct xfs_icreate_item *icp = ICR_ITEM(lip);
  51. struct xfs_log_iovec *vecp = NULL;
  52. xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_ICREATE,
  53. &icp->ic_format,
  54. sizeof(struct xfs_icreate_log));
  55. }
  56. STATIC void
  57. xfs_icreate_item_release(
  58. struct xfs_log_item *lip)
  59. {
  60. kmem_free(ICR_ITEM(lip)->ic_item.li_lv_shadow);
  61. kmem_cache_free(xfs_icreate_cache, ICR_ITEM(lip));
  62. }
  63. static const struct xfs_item_ops xfs_icreate_item_ops = {
  64. .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
  65. .iop_size = xfs_icreate_item_size,
  66. .iop_format = xfs_icreate_item_format,
  67. .iop_release = xfs_icreate_item_release,
  68. };
  69. /*
  70. * Initialize the inode log item for a newly allocated (in-core) inode.
  71. *
  72. * Inode extents can only reside within an AG. Hence specify the starting
  73. * block for the inode chunk by offset within an AG as well as the
  74. * length of the allocated extent.
  75. *
  76. * This joins the item to the transaction and marks it dirty so
  77. * that we don't need a separate call to do this, nor does the
  78. * caller need to know anything about the icreate item.
  79. */
  80. void
  81. xfs_icreate_log(
  82. struct xfs_trans *tp,
  83. xfs_agnumber_t agno,
  84. xfs_agblock_t agbno,
  85. unsigned int count,
  86. unsigned int inode_size,
  87. xfs_agblock_t length,
  88. unsigned int generation)
  89. {
  90. struct xfs_icreate_item *icp;
  91. icp = kmem_cache_zalloc(xfs_icreate_cache, GFP_KERNEL | __GFP_NOFAIL);
  92. xfs_log_item_init(tp->t_mountp, &icp->ic_item, XFS_LI_ICREATE,
  93. &xfs_icreate_item_ops);
  94. icp->ic_format.icl_type = XFS_LI_ICREATE;
  95. icp->ic_format.icl_size = 1; /* single vector */
  96. icp->ic_format.icl_ag = cpu_to_be32(agno);
  97. icp->ic_format.icl_agbno = cpu_to_be32(agbno);
  98. icp->ic_format.icl_count = cpu_to_be32(count);
  99. icp->ic_format.icl_isize = cpu_to_be32(inode_size);
  100. icp->ic_format.icl_length = cpu_to_be32(length);
  101. icp->ic_format.icl_gen = cpu_to_be32(generation);
  102. xfs_trans_add_item(tp, &icp->ic_item);
  103. tp->t_flags |= XFS_TRANS_DIRTY;
  104. set_bit(XFS_LI_DIRTY, &icp->ic_item.li_flags);
  105. }
  106. static enum xlog_recover_reorder
  107. xlog_recover_icreate_reorder(
  108. struct xlog_recover_item *item)
  109. {
  110. /*
  111. * Inode allocation buffers must be replayed before subsequent inode
  112. * items try to modify those buffers. ICREATE items are the logical
  113. * equivalent of logging a newly initialized inode buffer, so recover
  114. * these at the same time that we recover logged buffers.
  115. */
  116. return XLOG_REORDER_BUFFER_LIST;
  117. }
  118. /*
  119. * This routine is called when an inode create format structure is found in a
  120. * committed transaction in the log. It's purpose is to initialise the inodes
  121. * being allocated on disk. This requires us to get inode cluster buffers that
  122. * match the range to be initialised, stamped with inode templates and written
  123. * by delayed write so that subsequent modifications will hit the cached buffer
  124. * and only need writing out at the end of recovery.
  125. */
  126. STATIC int
  127. xlog_recover_icreate_commit_pass2(
  128. struct xlog *log,
  129. struct list_head *buffer_list,
  130. struct xlog_recover_item *item,
  131. xfs_lsn_t lsn)
  132. {
  133. struct xfs_mount *mp = log->l_mp;
  134. struct xfs_icreate_log *icl;
  135. struct xfs_ino_geometry *igeo = M_IGEO(mp);
  136. xfs_agnumber_t agno;
  137. xfs_agblock_t agbno;
  138. unsigned int count;
  139. unsigned int isize;
  140. xfs_agblock_t length;
  141. int bb_per_cluster;
  142. int cancel_count;
  143. int nbufs;
  144. int i;
  145. icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
  146. if (icl->icl_type != XFS_LI_ICREATE) {
  147. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
  148. return -EINVAL;
  149. }
  150. if (icl->icl_size != 1) {
  151. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
  152. return -EINVAL;
  153. }
  154. agno = be32_to_cpu(icl->icl_ag);
  155. if (agno >= mp->m_sb.sb_agcount) {
  156. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
  157. return -EINVAL;
  158. }
  159. agbno = be32_to_cpu(icl->icl_agbno);
  160. if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
  161. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
  162. return -EINVAL;
  163. }
  164. isize = be32_to_cpu(icl->icl_isize);
  165. if (isize != mp->m_sb.sb_inodesize) {
  166. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
  167. return -EINVAL;
  168. }
  169. count = be32_to_cpu(icl->icl_count);
  170. if (!count) {
  171. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
  172. return -EINVAL;
  173. }
  174. length = be32_to_cpu(icl->icl_length);
  175. if (!length || length >= mp->m_sb.sb_agblocks) {
  176. xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
  177. return -EINVAL;
  178. }
  179. /*
  180. * The inode chunk is either full or sparse and we only support
  181. * m_ino_geo.ialloc_min_blks sized sparse allocations at this time.
  182. */
  183. if (length != igeo->ialloc_blks &&
  184. length != igeo->ialloc_min_blks) {
  185. xfs_warn(log->l_mp,
  186. "%s: unsupported chunk length", __func__);
  187. return -EINVAL;
  188. }
  189. /* verify inode count is consistent with extent length */
  190. if ((count >> mp->m_sb.sb_inopblog) != length) {
  191. xfs_warn(log->l_mp,
  192. "%s: inconsistent inode count and chunk length",
  193. __func__);
  194. return -EINVAL;
  195. }
  196. /*
  197. * The icreate transaction can cover multiple cluster buffers and these
  198. * buffers could have been freed and reused. Check the individual
  199. * buffers for cancellation so we don't overwrite anything written after
  200. * a cancellation.
  201. */
  202. bb_per_cluster = XFS_FSB_TO_BB(mp, igeo->blocks_per_cluster);
  203. nbufs = length / igeo->blocks_per_cluster;
  204. for (i = 0, cancel_count = 0; i < nbufs; i++) {
  205. xfs_daddr_t daddr;
  206. daddr = XFS_AGB_TO_DADDR(mp, agno,
  207. agbno + i * igeo->blocks_per_cluster);
  208. if (xlog_is_buffer_cancelled(log, daddr, bb_per_cluster))
  209. cancel_count++;
  210. }
  211. /*
  212. * We currently only use icreate for a single allocation at a time. This
  213. * means we should expect either all or none of the buffers to be
  214. * cancelled. Be conservative and skip replay if at least one buffer is
  215. * cancelled, but warn the user that something is awry if the buffers
  216. * are not consistent.
  217. *
  218. * XXX: This must be refined to only skip cancelled clusters once we use
  219. * icreate for multiple chunk allocations.
  220. */
  221. ASSERT(!cancel_count || cancel_count == nbufs);
  222. if (cancel_count) {
  223. if (cancel_count != nbufs)
  224. xfs_warn(mp,
  225. "WARNING: partial inode chunk cancellation, skipped icreate.");
  226. trace_xfs_log_recover_icreate_cancel(log, icl);
  227. return 0;
  228. }
  229. trace_xfs_log_recover_icreate_recover(log, icl);
  230. return xfs_ialloc_inode_init(mp, NULL, buffer_list, count, agno, agbno,
  231. length, be32_to_cpu(icl->icl_gen));
  232. }
  233. const struct xlog_recover_item_ops xlog_icreate_item_ops = {
  234. .item_type = XFS_LI_ICREATE,
  235. .reorder = xlog_recover_icreate_reorder,
  236. .commit_pass2 = xlog_recover_icreate_commit_pass2,
  237. };