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