xfs: split iop_unlock
[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
ddf92053 120xfs_rui_item_release(
5880f2d7
DW
121 struct xfs_log_item *lip)
122{
ddf92053 123 xfs_rui_release(RUI_ITEM(lip));
5880f2d7
DW
124}
125
5880f2d7
DW
126/*
127 * This is the ops vector shared by all rui log items.
128 */
129static const struct xfs_item_ops xfs_rui_item_ops = {
130 .iop_size = xfs_rui_item_size,
131 .iop_format = xfs_rui_item_format,
5880f2d7 132 .iop_unpin = xfs_rui_item_unpin,
ddf92053 133 .iop_release = xfs_rui_item_release,
5880f2d7
DW
134};
135
136/*
137 * Allocate and initialize an rui item with the given number of extents.
138 */
139struct xfs_rui_log_item *
140xfs_rui_init(
141 struct xfs_mount *mp,
142 uint nextents)
143
144{
145 struct xfs_rui_log_item *ruip;
5880f2d7
DW
146
147 ASSERT(nextents > 0);
cd00158c
DW
148 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
149 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), KM_SLEEP);
150 else
5880f2d7 151 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
5880f2d7
DW
152
153 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
154 ruip->rui_format.rui_nextents = nextents;
155 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
156 atomic_set(&ruip->rui_next_extent, 0);
157 atomic_set(&ruip->rui_refcount, 2);
158
159 return ruip;
160}
161
162/*
163 * Copy an RUI format buffer from the given buf, and into the destination
164 * RUI format structure. The RUI/RUD items were designed not to need any
165 * special alignment handling.
166 */
167int
168xfs_rui_copy_format(
169 struct xfs_log_iovec *buf,
170 struct xfs_rui_log_format *dst_rui_fmt)
171{
172 struct xfs_rui_log_format *src_rui_fmt;
173 uint len;
174
175 src_rui_fmt = buf->i_addr;
cd00158c 176 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
5880f2d7
DW
177
178 if (buf->i_len != len)
179 return -EFSCORRUPTED;
180
cd00158c 181 memcpy(dst_rui_fmt, src_rui_fmt, len);
5880f2d7
DW
182 return 0;
183}
184
5880f2d7
DW
185static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
186{
187 return container_of(lip, struct xfs_rud_log_item, rud_item);
188}
189
5880f2d7
DW
190STATIC void
191xfs_rud_item_size(
192 struct xfs_log_item *lip,
193 int *nvecs,
194 int *nbytes)
195{
196 *nvecs += 1;
722e2517 197 *nbytes += sizeof(struct xfs_rud_log_format);
5880f2d7
DW
198}
199
200/*
201 * This is called to fill in the vector of log iovecs for the
202 * given rud log item. We use only 1 iovec, and we point that
203 * at the rud_log_format structure embedded in the rud item.
204 * It is at this point that we assert that all of the extent
205 * slots in the rud item have been filled.
206 */
207STATIC void
208xfs_rud_item_format(
209 struct xfs_log_item *lip,
210 struct xfs_log_vec *lv)
211{
212 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
213 struct xfs_log_iovec *vecp = NULL;
214
5880f2d7
DW
215 rudp->rud_format.rud_type = XFS_LI_RUD;
216 rudp->rud_format.rud_size = 1;
217
218 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
722e2517 219 sizeof(struct xfs_rud_log_format));
5880f2d7
DW
220}
221
5880f2d7
DW
222/*
223 * The RUD is either committed or aborted if the transaction is cancelled. If
224 * the transaction is cancelled, drop our reference to the RUI and free the
225 * RUD.
226 */
227STATIC void
ddf92053 228xfs_rud_item_release(
5880f2d7
DW
229 struct xfs_log_item *lip)
230{
231 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
232
ddf92053
CH
233 xfs_rui_release(rudp->rud_ruip);
234 kmem_zone_free(xfs_rud_zone, rudp);
5880f2d7
DW
235}
236
237/*
238 * When the rud item is committed to disk, all we need to do is delete our
239 * reference to our partner rui item and then free ourselves. Since we're
240 * freeing ourselves we must return -1 to keep the transaction code from
241 * further referencing this item.
242 */
243STATIC xfs_lsn_t
244xfs_rud_item_committed(
245 struct xfs_log_item *lip,
246 xfs_lsn_t lsn)
247{
248 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
249
250 /*
251 * Drop the RUI reference regardless of whether the RUD has been
252 * aborted. Once the RUD transaction is constructed, it is the sole
253 * responsibility of the RUD to release the RUI (even if the RUI is
254 * aborted due to log I/O error).
255 */
256 xfs_rui_release(rudp->rud_ruip);
722e2517 257 kmem_zone_free(xfs_rud_zone, rudp);
5880f2d7
DW
258
259 return (xfs_lsn_t)-1;
260}
261
5880f2d7
DW
262/*
263 * This is the ops vector shared by all rud log items.
264 */
265static const struct xfs_item_ops xfs_rud_item_ops = {
266 .iop_size = xfs_rud_item_size,
267 .iop_format = xfs_rud_item_format,
ddf92053 268 .iop_release = xfs_rud_item_release,
5880f2d7 269 .iop_committed = xfs_rud_item_committed,
5880f2d7
DW
270};
271
272/*
273 * Allocate and initialize an rud item with the given number of extents.
274 */
275struct xfs_rud_log_item *
276xfs_rud_init(
277 struct xfs_mount *mp,
722e2517 278 struct xfs_rui_log_item *ruip)
5880f2d7
DW
279
280{
281 struct xfs_rud_log_item *rudp;
5880f2d7 282
722e2517 283 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
5880f2d7
DW
284 xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
285 rudp->rud_ruip = ruip;
5880f2d7
DW
286 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
287
288 return rudp;
289}
9e88b5d8
DW
290
291/*
292 * Process an rmap update intent item that was recovered from the log.
293 * We need to update the rmapbt.
294 */
295int
296xfs_rui_recover(
297 struct xfs_mount *mp,
298 struct xfs_rui_log_item *ruip)
299{
300 int i;
301 int error = 0;
302 struct xfs_map_extent *rmap;
303 xfs_fsblock_t startblock_fsb;
304 bool op_ok;
9c194644
DW
305 struct xfs_rud_log_item *rudp;
306 enum xfs_rmap_intent_type type;
307 int whichfork;
308 xfs_exntst_t state;
309 struct xfs_trans *tp;
310 struct xfs_btree_cur *rcur = NULL;
9e88b5d8
DW
311
312 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
313
314 /*
315 * First check the validity of the extents described by the
316 * RUI. If any are bad, then assume that all are bad and
317 * just toss the RUI.
318 */
319 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
e127fafd 320 rmap = &ruip->rui_format.rui_extents[i];
9e88b5d8
DW
321 startblock_fsb = XFS_BB_TO_FSB(mp,
322 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
323 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
324 case XFS_RMAP_EXTENT_MAP:
0e07c039 325 case XFS_RMAP_EXTENT_MAP_SHARED:
9e88b5d8 326 case XFS_RMAP_EXTENT_UNMAP:
0e07c039 327 case XFS_RMAP_EXTENT_UNMAP_SHARED:
9e88b5d8 328 case XFS_RMAP_EXTENT_CONVERT:
0e07c039 329 case XFS_RMAP_EXTENT_CONVERT_SHARED:
9e88b5d8
DW
330 case XFS_RMAP_EXTENT_ALLOC:
331 case XFS_RMAP_EXTENT_FREE:
332 op_ok = true;
333 break;
334 default:
335 op_ok = false;
336 break;
337 }
e127fafd
DW
338 if (!op_ok || startblock_fsb == 0 ||
339 rmap->me_len == 0 ||
340 startblock_fsb >= mp->m_sb.sb_dblocks ||
341 rmap->me_len >= mp->m_sb.sb_agblocks ||
9e88b5d8
DW
342 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
343 /*
344 * This will pull the RUI from the AIL and
345 * free the memory associated with it.
346 */
347 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
348 xfs_rui_release(ruip);
349 return -EIO;
350 }
351 }
352
b31c2bdc
DW
353 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
354 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
9c194644
DW
355 if (error)
356 return error;
722e2517 357 rudp = xfs_trans_get_rud(tp, ruip);
9c194644
DW
358
359 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
e127fafd 360 rmap = &ruip->rui_format.rui_extents[i];
9c194644
DW
361 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
362 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
363 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
364 XFS_ATTR_FORK : XFS_DATA_FORK;
365 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
366 case XFS_RMAP_EXTENT_MAP:
367 type = XFS_RMAP_MAP;
368 break;
ceeb9c83
DW
369 case XFS_RMAP_EXTENT_MAP_SHARED:
370 type = XFS_RMAP_MAP_SHARED;
371 break;
9c194644
DW
372 case XFS_RMAP_EXTENT_UNMAP:
373 type = XFS_RMAP_UNMAP;
374 break;
ceeb9c83
DW
375 case XFS_RMAP_EXTENT_UNMAP_SHARED:
376 type = XFS_RMAP_UNMAP_SHARED;
377 break;
9c194644
DW
378 case XFS_RMAP_EXTENT_CONVERT:
379 type = XFS_RMAP_CONVERT;
380 break;
3f165b33
DW
381 case XFS_RMAP_EXTENT_CONVERT_SHARED:
382 type = XFS_RMAP_CONVERT_SHARED;
383 break;
9c194644
DW
384 case XFS_RMAP_EXTENT_ALLOC:
385 type = XFS_RMAP_ALLOC;
386 break;
387 case XFS_RMAP_EXTENT_FREE:
388 type = XFS_RMAP_FREE;
389 break;
390 default:
391 error = -EFSCORRUPTED;
392 goto abort_error;
393 }
394 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
395 rmap->me_owner, whichfork,
396 rmap->me_startoff, rmap->me_startblock,
397 rmap->me_len, state, &rcur);
398 if (error)
399 goto abort_error;
400
401 }
402
403 xfs_rmap_finish_one_cleanup(tp, rcur, error);
9e88b5d8 404 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
9c194644
DW
405 error = xfs_trans_commit(tp);
406 return error;
407
408abort_error:
409 xfs_rmap_finish_one_cleanup(tp, rcur, error);
410 xfs_trans_cancel(tp);
9e88b5d8
DW
411 return error;
412}