xfs: don't use xfs_trans_free_items in the commit path
[linux-2.6-block.git] / fs / xfs / xfs_rmap_item.c
CommitLineData
0b61f8a4 1// SPDX-License-Identifier: GPL-2.0+
5880f2d7
DW
2/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
5880f2d7 4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5880f2d7
DW
5 */
6#include "xfs.h"
7#include "xfs_fs.h"
8#include "xfs_format.h"
9#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
9e88b5d8 11#include "xfs_bit.h"
b31c2bdc 12#include "xfs_shared.h"
5880f2d7 13#include "xfs_mount.h"
9c194644 14#include "xfs_defer.h"
5880f2d7
DW
15#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
17#include "xfs_buf_item.h"
18#include "xfs_rmap_item.h"
19#include "xfs_log.h"
9c194644 20#include "xfs_rmap.h"
5880f2d7
DW
21
22
23kmem_zone_t *xfs_rui_zone;
24kmem_zone_t *xfs_rud_zone;
25
26static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
27{
28 return container_of(lip, struct xfs_rui_log_item, rui_item);
29}
30
31void
32xfs_rui_item_free(
33 struct xfs_rui_log_item *ruip)
34{
35 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
36 kmem_free(ruip);
37 else
38 kmem_zone_free(xfs_rui_zone, ruip);
39}
40
0612d116
DC
41/*
42 * Freeing the RUI requires that we remove it from the AIL if it has already
43 * been placed there. However, the RUI may not yet have been placed in the AIL
44 * when called by xfs_rui_release() from RUD processing due to the ordering of
45 * committed vs unpin operations in bulk insert operations. Hence the reference
46 * count to ensure only the last caller frees the RUI.
47 */
48void
49xfs_rui_release(
50 struct xfs_rui_log_item *ruip)
51{
52 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
53 if (atomic_dec_and_test(&ruip->rui_refcount)) {
54 xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
55 xfs_rui_item_free(ruip);
56 }
57}
58
5880f2d7
DW
59STATIC void
60xfs_rui_item_size(
61 struct xfs_log_item *lip,
62 int *nvecs,
63 int *nbytes)
64{
cd00158c
DW
65 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
66
5880f2d7 67 *nvecs += 1;
cd00158c 68 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
5880f2d7
DW
69}
70
71/*
72 * This is called to fill in the vector of log iovecs for the
73 * given rui log item. We use only 1 iovec, and we point that
74 * at the rui_log_format structure embedded in the rui item.
75 * It is at this point that we assert that all of the extent
76 * slots in the rui item have been filled.
77 */
78STATIC void
79xfs_rui_item_format(
80 struct xfs_log_item *lip,
81 struct xfs_log_vec *lv)
82{
83 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
84 struct xfs_log_iovec *vecp = NULL;
85
86 ASSERT(atomic_read(&ruip->rui_next_extent) ==
87 ruip->rui_format.rui_nextents);
88
89 ruip->rui_format.rui_type = XFS_LI_RUI;
90 ruip->rui_format.rui_size = 1;
91
92 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
cd00158c 93 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
5880f2d7
DW
94}
95
5880f2d7
DW
96/*
97 * The unpin operation is the last place an RUI is manipulated in the log. It is
98 * either inserted in the AIL or aborted in the event of a log I/O error. In
99 * either case, the RUI transaction has been successfully committed to make it
100 * this far. Therefore, we expect whoever committed the RUI to either construct
101 * and commit the RUD or drop the RUD's reference in the event of error. Simply
102 * drop the log's RUI reference now that the log is done with it.
103 */
104STATIC void
105xfs_rui_item_unpin(
106 struct xfs_log_item *lip,
107 int remove)
108{
109 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
110
111 xfs_rui_release(ruip);
112}
113
5880f2d7
DW
114/*
115 * The RUI has been either committed or aborted if the transaction has been
116 * cancelled. If the transaction was cancelled, an RUD isn't going to be
117 * constructed and thus we free the RUI here directly.
118 */
119STATIC void
120xfs_rui_item_unlock(
121 struct xfs_log_item *lip)
122{
22525c17 123 if (test_bit(XFS_LI_ABORTED, &lip->li_flags))
0612d116 124 xfs_rui_release(RUI_ITEM(lip));
5880f2d7
DW
125}
126
5880f2d7
DW
127/*
128 * This is the ops vector shared by all rui log items.
129 */
130static const struct xfs_item_ops xfs_rui_item_ops = {
131 .iop_size = xfs_rui_item_size,
132 .iop_format = xfs_rui_item_format,
5880f2d7
DW
133 .iop_unpin = xfs_rui_item_unpin,
134 .iop_unlock = xfs_rui_item_unlock,
5880f2d7
DW
135};
136
137/*
138 * Allocate and initialize an rui item with the given number of extents.
139 */
140struct xfs_rui_log_item *
141xfs_rui_init(
142 struct xfs_mount *mp,
143 uint nextents)
144
145{
146 struct xfs_rui_log_item *ruip;
5880f2d7
DW
147
148 ASSERT(nextents > 0);
cd00158c
DW
149 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
150 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), KM_SLEEP);
151 else
5880f2d7 152 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
5880f2d7
DW
153
154 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
155 ruip->rui_format.rui_nextents = nextents;
156 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
157 atomic_set(&ruip->rui_next_extent, 0);
158 atomic_set(&ruip->rui_refcount, 2);
159
160 return ruip;
161}
162
163/*
164 * Copy an RUI format buffer from the given buf, and into the destination
165 * RUI format structure. The RUI/RUD items were designed not to need any
166 * special alignment handling.
167 */
168int
169xfs_rui_copy_format(
170 struct xfs_log_iovec *buf,
171 struct xfs_rui_log_format *dst_rui_fmt)
172{
173 struct xfs_rui_log_format *src_rui_fmt;
174 uint len;
175
176 src_rui_fmt = buf->i_addr;
cd00158c 177 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
5880f2d7
DW
178
179 if (buf->i_len != len)
180 return -EFSCORRUPTED;
181
cd00158c 182 memcpy(dst_rui_fmt, src_rui_fmt, len);
5880f2d7
DW
183 return 0;
184}
185
5880f2d7
DW
186static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
187{
188 return container_of(lip, struct xfs_rud_log_item, rud_item);
189}
190
5880f2d7
DW
191STATIC void
192xfs_rud_item_size(
193 struct xfs_log_item *lip,
194 int *nvecs,
195 int *nbytes)
196{
197 *nvecs += 1;
722e2517 198 *nbytes += sizeof(struct xfs_rud_log_format);
5880f2d7
DW
199}
200
201/*
202 * This is called to fill in the vector of log iovecs for the
203 * given rud log item. We use only 1 iovec, and we point that
204 * at the rud_log_format structure embedded in the rud item.
205 * It is at this point that we assert that all of the extent
206 * slots in the rud item have been filled.
207 */
208STATIC void
209xfs_rud_item_format(
210 struct xfs_log_item *lip,
211 struct xfs_log_vec *lv)
212{
213 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
214 struct xfs_log_iovec *vecp = NULL;
215
5880f2d7
DW
216 rudp->rud_format.rud_type = XFS_LI_RUD;
217 rudp->rud_format.rud_size = 1;
218
219 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
722e2517 220 sizeof(struct xfs_rud_log_format));
5880f2d7
DW
221}
222
5880f2d7
DW
223/*
224 * The RUD is either committed or aborted if the transaction is cancelled. If
225 * the transaction is cancelled, drop our reference to the RUI and free the
226 * RUD.
227 */
228STATIC void
229xfs_rud_item_unlock(
230 struct xfs_log_item *lip)
231{
232 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
233
22525c17 234 if (test_bit(XFS_LI_ABORTED, &lip->li_flags)) {
5880f2d7 235 xfs_rui_release(rudp->rud_ruip);
722e2517 236 kmem_zone_free(xfs_rud_zone, rudp);
5880f2d7
DW
237 }
238}
239
240/*
241 * When the rud item is committed to disk, all we need to do is delete our
242 * reference to our partner rui item and then free ourselves. Since we're
243 * freeing ourselves we must return -1 to keep the transaction code from
244 * further referencing this item.
245 */
246STATIC xfs_lsn_t
247xfs_rud_item_committed(
248 struct xfs_log_item *lip,
249 xfs_lsn_t lsn)
250{
251 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
252
253 /*
254 * Drop the RUI reference regardless of whether the RUD has been
255 * aborted. Once the RUD transaction is constructed, it is the sole
256 * responsibility of the RUD to release the RUI (even if the RUI is
257 * aborted due to log I/O error).
258 */
259 xfs_rui_release(rudp->rud_ruip);
722e2517 260 kmem_zone_free(xfs_rud_zone, rudp);
5880f2d7
DW
261
262 return (xfs_lsn_t)-1;
263}
264
5880f2d7
DW
265/*
266 * This is the ops vector shared by all rud log items.
267 */
268static const struct xfs_item_ops xfs_rud_item_ops = {
269 .iop_size = xfs_rud_item_size,
270 .iop_format = xfs_rud_item_format,
5880f2d7
DW
271 .iop_unlock = xfs_rud_item_unlock,
272 .iop_committed = xfs_rud_item_committed,
5880f2d7
DW
273};
274
275/*
276 * Allocate and initialize an rud item with the given number of extents.
277 */
278struct xfs_rud_log_item *
279xfs_rud_init(
280 struct xfs_mount *mp,
722e2517 281 struct xfs_rui_log_item *ruip)
5880f2d7
DW
282
283{
284 struct xfs_rud_log_item *rudp;
5880f2d7 285
722e2517 286 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
5880f2d7
DW
287 xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
288 rudp->rud_ruip = ruip;
5880f2d7
DW
289 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
290
291 return rudp;
292}
9e88b5d8
DW
293
294/*
295 * Process an rmap update intent item that was recovered from the log.
296 * We need to update the rmapbt.
297 */
298int
299xfs_rui_recover(
300 struct xfs_mount *mp,
301 struct xfs_rui_log_item *ruip)
302{
303 int i;
304 int error = 0;
305 struct xfs_map_extent *rmap;
306 xfs_fsblock_t startblock_fsb;
307 bool op_ok;
9c194644
DW
308 struct xfs_rud_log_item *rudp;
309 enum xfs_rmap_intent_type type;
310 int whichfork;
311 xfs_exntst_t state;
312 struct xfs_trans *tp;
313 struct xfs_btree_cur *rcur = NULL;
9e88b5d8
DW
314
315 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
316
317 /*
318 * First check the validity of the extents described by the
319 * RUI. If any are bad, then assume that all are bad and
320 * just toss the RUI.
321 */
322 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
e127fafd 323 rmap = &ruip->rui_format.rui_extents[i];
9e88b5d8
DW
324 startblock_fsb = XFS_BB_TO_FSB(mp,
325 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
326 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
327 case XFS_RMAP_EXTENT_MAP:
0e07c039 328 case XFS_RMAP_EXTENT_MAP_SHARED:
9e88b5d8 329 case XFS_RMAP_EXTENT_UNMAP:
0e07c039 330 case XFS_RMAP_EXTENT_UNMAP_SHARED:
9e88b5d8 331 case XFS_RMAP_EXTENT_CONVERT:
0e07c039 332 case XFS_RMAP_EXTENT_CONVERT_SHARED:
9e88b5d8
DW
333 case XFS_RMAP_EXTENT_ALLOC:
334 case XFS_RMAP_EXTENT_FREE:
335 op_ok = true;
336 break;
337 default:
338 op_ok = false;
339 break;
340 }
e127fafd
DW
341 if (!op_ok || startblock_fsb == 0 ||
342 rmap->me_len == 0 ||
343 startblock_fsb >= mp->m_sb.sb_dblocks ||
344 rmap->me_len >= mp->m_sb.sb_agblocks ||
9e88b5d8
DW
345 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
346 /*
347 * This will pull the RUI from the AIL and
348 * free the memory associated with it.
349 */
350 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
351 xfs_rui_release(ruip);
352 return -EIO;
353 }
354 }
355
b31c2bdc
DW
356 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
357 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
9c194644
DW
358 if (error)
359 return error;
722e2517 360 rudp = xfs_trans_get_rud(tp, ruip);
9c194644
DW
361
362 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
e127fafd 363 rmap = &ruip->rui_format.rui_extents[i];
9c194644
DW
364 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
365 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
366 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
367 XFS_ATTR_FORK : XFS_DATA_FORK;
368 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
369 case XFS_RMAP_EXTENT_MAP:
370 type = XFS_RMAP_MAP;
371 break;
ceeb9c83
DW
372 case XFS_RMAP_EXTENT_MAP_SHARED:
373 type = XFS_RMAP_MAP_SHARED;
374 break;
9c194644
DW
375 case XFS_RMAP_EXTENT_UNMAP:
376 type = XFS_RMAP_UNMAP;
377 break;
ceeb9c83
DW
378 case XFS_RMAP_EXTENT_UNMAP_SHARED:
379 type = XFS_RMAP_UNMAP_SHARED;
380 break;
9c194644
DW
381 case XFS_RMAP_EXTENT_CONVERT:
382 type = XFS_RMAP_CONVERT;
383 break;
3f165b33
DW
384 case XFS_RMAP_EXTENT_CONVERT_SHARED:
385 type = XFS_RMAP_CONVERT_SHARED;
386 break;
9c194644
DW
387 case XFS_RMAP_EXTENT_ALLOC:
388 type = XFS_RMAP_ALLOC;
389 break;
390 case XFS_RMAP_EXTENT_FREE:
391 type = XFS_RMAP_FREE;
392 break;
393 default:
394 error = -EFSCORRUPTED;
395 goto abort_error;
396 }
397 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
398 rmap->me_owner, whichfork,
399 rmap->me_startoff, rmap->me_startblock,
400 rmap->me_len, state, &rcur);
401 if (error)
402 goto abort_error;
403
404 }
405
406 xfs_rmap_finish_one_cleanup(tp, rcur, error);
9e88b5d8 407 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
9c194644
DW
408 error = xfs_trans_commit(tp);
409 return error;
410
411abort_error:
412 xfs_rmap_finish_one_cleanup(tp, rcur, error);
413 xfs_trans_cancel(tp);
9e88b5d8
DW
414 return error;
415}