blob: 31d35de518d1ff70a28843f2225814cc30b2ced7 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0+
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10002/*
3 * Copyright (C) 2016 Oracle. All Rights Reserved.
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10004 * Author: Darrick J. Wong <darrick.wong@oracle.com>
Darrick J. Wong5880f2d72016-08-03 12:04:45 +10005 */
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"
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +100011#include "xfs_bit.h"
Darrick J. Wongb31c2bd2018-02-22 14:41:25 -080012#include "xfs_shared.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100013#include "xfs_mount.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100014#include "xfs_defer.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100015#include "xfs_trans.h"
16#include "xfs_trans_priv.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100017#include "xfs_rmap_item.h"
18#include "xfs_log.h"
Darrick J. Wong9c194642016-08-03 12:16:05 +100019#include "xfs_rmap.h"
Darrick J. Wonga5155b82019-11-02 09:40:53 -070020#include "xfs_error.h"
Darrick J. Wong07590a92020-05-01 16:00:49 -070021#include "xfs_log_priv.h"
Darrick J. Wong86ffa472020-05-01 16:00:45 -070022#include "xfs_log_recover.h"
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100023
24kmem_zone_t *xfs_rui_zone;
25kmem_zone_t *xfs_rud_zone;
26
Darrick J. Wongcba0cca2020-05-01 16:00:51 -070027static const struct xfs_item_ops xfs_rui_item_ops;
28
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100029static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
30{
31 return container_of(lip, struct xfs_rui_log_item, rui_item);
32}
33
Darrick J. Wong07590a92020-05-01 16:00:49 -070034STATIC void
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100035xfs_rui_item_free(
36 struct xfs_rui_log_item *ruip)
37{
38 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
39 kmem_free(ruip);
40 else
Carlos Maiolino377bcd52019-11-14 12:43:04 -080041 kmem_cache_free(xfs_rui_zone, ruip);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100042}
43
Dave Chinner0612d112018-04-02 20:08:27 -070044/*
45 * Freeing the RUI requires that we remove it from the AIL if it has already
46 * been placed there. However, the RUI may not yet have been placed in the AIL
47 * when called by xfs_rui_release() from RUD processing due to the ordering of
48 * committed vs unpin operations in bulk insert operations. Hence the reference
49 * count to ensure only the last caller frees the RUI.
50 */
Darrick J. Wongcba0cca2020-05-01 16:00:51 -070051STATIC void
Dave Chinner0612d112018-04-02 20:08:27 -070052xfs_rui_release(
53 struct xfs_rui_log_item *ruip)
54{
55 ASSERT(atomic_read(&ruip->rui_refcount) > 0);
56 if (atomic_dec_and_test(&ruip->rui_refcount)) {
Brian Foster65587922020-05-06 13:25:23 -070057 xfs_trans_ail_delete(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
Dave Chinner0612d112018-04-02 20:08:27 -070058 xfs_rui_item_free(ruip);
59 }
60}
61
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100062STATIC void
63xfs_rui_item_size(
64 struct xfs_log_item *lip,
65 int *nvecs,
66 int *nbytes)
67{
Darrick J. Wongcd001582016-09-19 10:24:27 +100068 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
69
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100070 *nvecs += 1;
Darrick J. Wongcd001582016-09-19 10:24:27 +100071 *nbytes += xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100072}
73
74/*
75 * This is called to fill in the vector of log iovecs for the
76 * given rui log item. We use only 1 iovec, and we point that
77 * at the rui_log_format structure embedded in the rui item.
78 * It is at this point that we assert that all of the extent
79 * slots in the rui item have been filled.
80 */
81STATIC void
82xfs_rui_item_format(
83 struct xfs_log_item *lip,
84 struct xfs_log_vec *lv)
85{
86 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
87 struct xfs_log_iovec *vecp = NULL;
88
89 ASSERT(atomic_read(&ruip->rui_next_extent) ==
90 ruip->rui_format.rui_nextents);
91
92 ruip->rui_format.rui_type = XFS_LI_RUI;
93 ruip->rui_format.rui_size = 1;
94
95 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
Darrick J. Wongcd001582016-09-19 10:24:27 +100096 xfs_rui_log_format_sizeof(ruip->rui_format.rui_nextents));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +100097}
98
99/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000100 * The unpin operation is the last place an RUI is manipulated in the log. It is
101 * either inserted in the AIL or aborted in the event of a log I/O error. In
102 * either case, the RUI transaction has been successfully committed to make it
103 * this far. Therefore, we expect whoever committed the RUI to either construct
104 * and commit the RUD or drop the RUD's reference in the event of error. Simply
105 * drop the log's RUI reference now that the log is done with it.
106 */
107STATIC void
108xfs_rui_item_unpin(
109 struct xfs_log_item *lip,
110 int remove)
111{
112 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
113
114 xfs_rui_release(ruip);
115}
116
117/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000118 * The RUI has been either committed or aborted if the transaction has been
119 * cancelled. If the transaction was cancelled, an RUD isn't going to be
120 * constructed and thus we free the RUI here directly.
121 */
122STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700123xfs_rui_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000124 struct xfs_log_item *lip)
125{
Christoph Hellwigddf92052019-06-28 19:27:32 -0700126 xfs_rui_release(RUI_ITEM(lip));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000127}
128
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000129/*
130 * Allocate and initialize an rui item with the given number of extents.
131 */
Darrick J. Wong07590a92020-05-01 16:00:49 -0700132STATIC struct xfs_rui_log_item *
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000133xfs_rui_init(
134 struct xfs_mount *mp,
135 uint nextents)
136
137{
138 struct xfs_rui_log_item *ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000139
140 ASSERT(nextents > 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000141 if (nextents > XFS_RUI_MAX_FAST_EXTENTS)
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700142 ruip = kmem_zalloc(xfs_rui_log_item_sizeof(nextents), 0);
Darrick J. Wongcd001582016-09-19 10:24:27 +1000143 else
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700144 ruip = kmem_zone_zalloc(xfs_rui_zone, 0);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000145
146 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
147 ruip->rui_format.rui_nextents = nextents;
148 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
149 atomic_set(&ruip->rui_next_extent, 0);
150 atomic_set(&ruip->rui_refcount, 2);
151
152 return ruip;
153}
154
155/*
156 * Copy an RUI format buffer from the given buf, and into the destination
157 * RUI format structure. The RUI/RUD items were designed not to need any
158 * special alignment handling.
159 */
Darrick J. Wong07590a92020-05-01 16:00:49 -0700160STATIC int
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000161xfs_rui_copy_format(
162 struct xfs_log_iovec *buf,
163 struct xfs_rui_log_format *dst_rui_fmt)
164{
165 struct xfs_rui_log_format *src_rui_fmt;
166 uint len;
167
168 src_rui_fmt = buf->i_addr;
Darrick J. Wongcd001582016-09-19 10:24:27 +1000169 len = xfs_rui_log_format_sizeof(src_rui_fmt->rui_nextents);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000170
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700171 if (buf->i_len != len) {
172 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000173 return -EFSCORRUPTED;
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700174 }
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000175
Darrick J. Wongcd001582016-09-19 10:24:27 +1000176 memcpy(dst_rui_fmt, src_rui_fmt, len);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000177 return 0;
178}
179
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000180static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
181{
182 return container_of(lip, struct xfs_rud_log_item, rud_item);
183}
184
185STATIC void
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000186xfs_rud_item_size(
187 struct xfs_log_item *lip,
188 int *nvecs,
189 int *nbytes)
190{
191 *nvecs += 1;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000192 *nbytes += sizeof(struct xfs_rud_log_format);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000193}
194
195/*
196 * This is called to fill in the vector of log iovecs for the
197 * given rud log item. We use only 1 iovec, and we point that
198 * at the rud_log_format structure embedded in the rud item.
199 * It is at this point that we assert that all of the extent
200 * slots in the rud item have been filled.
201 */
202STATIC void
203xfs_rud_item_format(
204 struct xfs_log_item *lip,
205 struct xfs_log_vec *lv)
206{
207 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
208 struct xfs_log_iovec *vecp = NULL;
209
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000210 rudp->rud_format.rud_type = XFS_LI_RUD;
211 rudp->rud_format.rud_size = 1;
212
213 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000214 sizeof(struct xfs_rud_log_format));
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000215}
216
217/*
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000218 * The RUD is either committed or aborted if the transaction is cancelled. If
219 * the transaction is cancelled, drop our reference to the RUI and free the
220 * RUD.
221 */
222STATIC void
Christoph Hellwigddf92052019-06-28 19:27:32 -0700223xfs_rud_item_release(
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000224 struct xfs_log_item *lip)
225{
226 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
227
Christoph Hellwigddf92052019-06-28 19:27:32 -0700228 xfs_rui_release(rudp->rud_ruip);
Carlos Maiolino377bcd52019-11-14 12:43:04 -0800229 kmem_cache_free(xfs_rud_zone, rudp);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000230}
231
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000232static const struct xfs_item_ops xfs_rud_item_ops = {
Christoph Hellwig9ce632a2019-06-28 19:27:32 -0700233 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000234 .iop_size = xfs_rud_item_size,
235 .iop_format = xfs_rud_item_format,
Christoph Hellwigddf92052019-06-28 19:27:32 -0700236 .iop_release = xfs_rud_item_release,
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000237};
238
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700239static struct xfs_rud_log_item *
Christoph Hellwig608834472019-06-28 19:27:36 -0700240xfs_trans_get_rud(
241 struct xfs_trans *tp,
Darrick J. Wong722e2512016-08-03 12:28:43 +1000242 struct xfs_rui_log_item *ruip)
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000243{
Christoph Hellwig608834472019-06-28 19:27:36 -0700244 struct xfs_rud_log_item *rudp;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000245
Tetsuo Handa707e0dd2019-08-26 12:06:22 -0700246 rudp = kmem_zone_zalloc(xfs_rud_zone, 0);
Christoph Hellwig608834472019-06-28 19:27:36 -0700247 xfs_log_item_init(tp->t_mountp, &rudp->rud_item, XFS_LI_RUD,
248 &xfs_rud_item_ops);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000249 rudp->rud_ruip = ruip;
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000250 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
251
Christoph Hellwig608834472019-06-28 19:27:36 -0700252 xfs_trans_add_item(tp, &rudp->rud_item);
Darrick J. Wong5880f2d72016-08-03 12:04:45 +1000253 return rudp;
254}
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000255
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700256/* Set the map extent flags for this reverse mapping. */
257static void
258xfs_trans_set_rmap_flags(
259 struct xfs_map_extent *rmap,
260 enum xfs_rmap_intent_type type,
261 int whichfork,
262 xfs_exntst_t state)
263{
264 rmap->me_flags = 0;
265 if (state == XFS_EXT_UNWRITTEN)
266 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
267 if (whichfork == XFS_ATTR_FORK)
268 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
269 switch (type) {
270 case XFS_RMAP_MAP:
271 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
272 break;
273 case XFS_RMAP_MAP_SHARED:
274 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
275 break;
276 case XFS_RMAP_UNMAP:
277 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
278 break;
279 case XFS_RMAP_UNMAP_SHARED:
280 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
281 break;
282 case XFS_RMAP_CONVERT:
283 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
284 break;
285 case XFS_RMAP_CONVERT_SHARED:
286 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
287 break;
288 case XFS_RMAP_ALLOC:
289 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
290 break;
291 case XFS_RMAP_FREE:
292 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
293 break;
294 default:
295 ASSERT(0);
296 }
297}
298
299/*
300 * Finish an rmap update and log it to the RUD. Note that the transaction is
301 * marked dirty regardless of whether the rmap update succeeds or fails to
302 * support the RUI/RUD lifecycle rules.
303 */
304static int
305xfs_trans_log_finish_rmap_update(
306 struct xfs_trans *tp,
307 struct xfs_rud_log_item *rudp,
308 enum xfs_rmap_intent_type type,
309 uint64_t owner,
310 int whichfork,
311 xfs_fileoff_t startoff,
312 xfs_fsblock_t startblock,
313 xfs_filblks_t blockcount,
314 xfs_exntst_t state,
315 struct xfs_btree_cur **pcur)
316{
317 int error;
318
319 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
320 startblock, blockcount, state, pcur);
321
322 /*
323 * Mark the transaction dirty, even on error. This ensures the
324 * transaction is aborted, which:
325 *
326 * 1.) releases the RUI and frees the RUD
327 * 2.) shuts down the filesystem
328 */
329 tp->t_flags |= XFS_TRANS_DIRTY;
330 set_bit(XFS_LI_DIRTY, &rudp->rud_item.li_flags);
331
332 return error;
333}
334
335/* Sort rmap intents by AG. */
336static int
337xfs_rmap_update_diff_items(
338 void *priv,
339 struct list_head *a,
340 struct list_head *b)
341{
342 struct xfs_mount *mp = priv;
343 struct xfs_rmap_intent *ra;
344 struct xfs_rmap_intent *rb;
345
346 ra = container_of(a, struct xfs_rmap_intent, ri_list);
347 rb = container_of(b, struct xfs_rmap_intent, ri_list);
348 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
349 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
350}
351
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700352/* Log rmap updates in the intent item. */
353STATIC void
354xfs_rmap_update_log_item(
355 struct xfs_trans *tp,
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700356 struct xfs_rui_log_item *ruip,
357 struct xfs_rmap_intent *rmap)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700358{
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700359 uint next_extent;
360 struct xfs_map_extent *map;
361
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700362 tp->t_flags |= XFS_TRANS_DIRTY;
363 set_bit(XFS_LI_DIRTY, &ruip->rui_item.li_flags);
364
365 /*
366 * atomic_inc_return gives us the value after the increment;
367 * we want to use it as an array index so we need to subtract 1 from
368 * it.
369 */
370 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
371 ASSERT(next_extent < ruip->rui_format.rui_nextents);
372 map = &ruip->rui_format.rui_extents[next_extent];
373 map->me_owner = rmap->ri_owner;
374 map->me_startblock = rmap->ri_bmap.br_startblock;
375 map->me_startoff = rmap->ri_bmap.br_startoff;
376 map->me_len = rmap->ri_bmap.br_blockcount;
377 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
378 rmap->ri_bmap.br_state);
379}
380
Christoph Hellwig13a83332020-04-30 12:52:21 -0700381static struct xfs_log_item *
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700382xfs_rmap_update_create_intent(
383 struct xfs_trans *tp,
384 struct list_head *items,
Christoph Hellwigd367a862020-04-30 12:52:20 -0700385 unsigned int count,
386 bool sort)
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700387{
388 struct xfs_mount *mp = tp->t_mountp;
389 struct xfs_rui_log_item *ruip = xfs_rui_init(mp, count);
390 struct xfs_rmap_intent *rmap;
391
392 ASSERT(count > 0);
393
394 xfs_trans_add_item(tp, &ruip->rui_item);
Christoph Hellwigd367a862020-04-30 12:52:20 -0700395 if (sort)
396 list_sort(mp, items, xfs_rmap_update_diff_items);
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700397 list_for_each_entry(rmap, items, ri_list)
398 xfs_rmap_update_log_item(tp, ruip, rmap);
Christoph Hellwig13a83332020-04-30 12:52:21 -0700399 return &ruip->rui_item;
Christoph Hellwigc1f09182020-04-30 12:52:20 -0700400}
401
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700402/* Get an RUD so we can process all the deferred rmap updates. */
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700403static struct xfs_log_item *
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700404xfs_rmap_update_create_done(
405 struct xfs_trans *tp,
Christoph Hellwig13a83332020-04-30 12:52:21 -0700406 struct xfs_log_item *intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700407 unsigned int count)
408{
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700409 return &xfs_trans_get_rud(tp, RUI_ITEM(intent))->rud_item;
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700410}
411
412/* Process a deferred rmap update. */
413STATIC int
414xfs_rmap_update_finish_item(
415 struct xfs_trans *tp,
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700416 struct xfs_log_item *done,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700417 struct list_head *item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700418 struct xfs_btree_cur **state)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700419{
420 struct xfs_rmap_intent *rmap;
421 int error;
422
423 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
Christoph Hellwigf09d1672020-04-30 12:52:22 -0700424 error = xfs_trans_log_finish_rmap_update(tp, RUD_ITEM(done),
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700425 rmap->ri_type, rmap->ri_owner, rmap->ri_whichfork,
426 rmap->ri_bmap.br_startoff, rmap->ri_bmap.br_startblock,
427 rmap->ri_bmap.br_blockcount, rmap->ri_bmap.br_state,
428 state);
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700429 kmem_free(rmap);
430 return error;
431}
432
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700433/* Abort all pending RUIs. */
434STATIC void
435xfs_rmap_update_abort_intent(
Christoph Hellwig13a83332020-04-30 12:52:21 -0700436 struct xfs_log_item *intent)
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700437{
Christoph Hellwig13a83332020-04-30 12:52:21 -0700438 xfs_rui_release(RUI_ITEM(intent));
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700439}
440
441/* Cancel a deferred rmap update. */
442STATIC void
443xfs_rmap_update_cancel_item(
444 struct list_head *item)
445{
446 struct xfs_rmap_intent *rmap;
447
448 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
449 kmem_free(rmap);
450}
451
452const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
453 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700454 .create_intent = xfs_rmap_update_create_intent,
455 .abort_intent = xfs_rmap_update_abort_intent,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700456 .create_done = xfs_rmap_update_create_done,
457 .finish_item = xfs_rmap_update_finish_item,
Christoph Hellwig3ec1b262020-04-30 12:52:22 -0700458 .finish_cleanup = xfs_rmap_finish_one_cleanup,
Christoph Hellwig3cfce1e2019-06-28 19:29:41 -0700459 .cancel_item = xfs_rmap_update_cancel_item,
460};
461
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000462/*
463 * Process an rmap update intent item that was recovered from the log.
464 * We need to update the rmapbt.
465 */
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700466STATIC int
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000467xfs_rui_recover(
468 struct xfs_mount *mp,
469 struct xfs_rui_log_item *ruip)
470{
471 int i;
472 int error = 0;
473 struct xfs_map_extent *rmap;
474 xfs_fsblock_t startblock_fsb;
475 bool op_ok;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000476 struct xfs_rud_log_item *rudp;
477 enum xfs_rmap_intent_type type;
478 int whichfork;
479 xfs_exntst_t state;
480 struct xfs_trans *tp;
481 struct xfs_btree_cur *rcur = NULL;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000482
483 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
484
485 /*
486 * First check the validity of the extents described by the
487 * RUI. If any are bad, then assume that all are bad and
488 * just toss the RUI.
489 */
490 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000491 rmap = &ruip->rui_format.rui_extents[i];
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000492 startblock_fsb = XFS_BB_TO_FSB(mp,
493 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
494 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
495 case XFS_RMAP_EXTENT_MAP:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700496 case XFS_RMAP_EXTENT_MAP_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000497 case XFS_RMAP_EXTENT_UNMAP:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700498 case XFS_RMAP_EXTENT_UNMAP_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000499 case XFS_RMAP_EXTENT_CONVERT:
Darrick J. Wong0e07c032016-10-03 09:11:47 -0700500 case XFS_RMAP_EXTENT_CONVERT_SHARED:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000501 case XFS_RMAP_EXTENT_ALLOC:
502 case XFS_RMAP_EXTENT_FREE:
503 op_ok = true;
504 break;
505 default:
506 op_ok = false;
507 break;
508 }
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000509 if (!op_ok || startblock_fsb == 0 ||
510 rmap->me_len == 0 ||
511 startblock_fsb >= mp->m_sb.sb_dblocks ||
512 rmap->me_len >= mp->m_sb.sb_agblocks ||
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000513 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
514 /*
515 * This will pull the RUI from the AIL and
516 * free the memory associated with it.
517 */
518 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
519 xfs_rui_release(ruip);
Darrick J. Wong895e1962019-11-06 09:17:43 -0800520 return -EFSCORRUPTED;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000521 }
522 }
523
Darrick J. Wongb31c2bd2018-02-22 14:41:25 -0800524 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate,
525 mp->m_rmap_maxlevels, 0, XFS_TRANS_RESERVE, &tp);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000526 if (error)
527 return error;
Darrick J. Wong722e2512016-08-03 12:28:43 +1000528 rudp = xfs_trans_get_rud(tp, ruip);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000529
530 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000531 rmap = &ruip->rui_format.rui_extents[i];
Darrick J. Wong9c194642016-08-03 12:16:05 +1000532 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ?
533 XFS_EXT_UNWRITTEN : XFS_EXT_NORM;
534 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ?
535 XFS_ATTR_FORK : XFS_DATA_FORK;
536 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
537 case XFS_RMAP_EXTENT_MAP:
538 type = XFS_RMAP_MAP;
539 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700540 case XFS_RMAP_EXTENT_MAP_SHARED:
541 type = XFS_RMAP_MAP_SHARED;
542 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000543 case XFS_RMAP_EXTENT_UNMAP:
544 type = XFS_RMAP_UNMAP;
545 break;
Darrick J. Wongceeb9c82016-10-03 09:11:48 -0700546 case XFS_RMAP_EXTENT_UNMAP_SHARED:
547 type = XFS_RMAP_UNMAP_SHARED;
548 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000549 case XFS_RMAP_EXTENT_CONVERT:
550 type = XFS_RMAP_CONVERT;
551 break;
Darrick J. Wong3f165b32016-10-03 09:11:48 -0700552 case XFS_RMAP_EXTENT_CONVERT_SHARED:
553 type = XFS_RMAP_CONVERT_SHARED;
554 break;
Darrick J. Wong9c194642016-08-03 12:16:05 +1000555 case XFS_RMAP_EXTENT_ALLOC:
556 type = XFS_RMAP_ALLOC;
557 break;
558 case XFS_RMAP_EXTENT_FREE:
559 type = XFS_RMAP_FREE;
560 break;
561 default:
Darrick J. Wonga5155b82019-11-02 09:40:53 -0700562 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_LOW, NULL);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000563 error = -EFSCORRUPTED;
564 goto abort_error;
565 }
566 error = xfs_trans_log_finish_rmap_update(tp, rudp, type,
567 rmap->me_owner, whichfork,
568 rmap->me_startoff, rmap->me_startblock,
569 rmap->me_len, state, &rcur);
570 if (error)
571 goto abort_error;
572
573 }
574
575 xfs_rmap_finish_one_cleanup(tp, rcur, error);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000576 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
Darrick J. Wong9c194642016-08-03 12:16:05 +1000577 error = xfs_trans_commit(tp);
578 return error;
579
580abort_error:
581 xfs_rmap_finish_one_cleanup(tp, rcur, error);
582 xfs_trans_cancel(tp);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +1000583 return error;
584}
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700585
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700586/* Recover the RUI if necessary. */
587STATIC int
588xfs_rui_item_recover(
589 struct xfs_log_item *lip,
590 struct xfs_trans *tp)
591{
592 struct xfs_ail *ailp = lip->li_ailp;
593 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
594 int error;
595
596 /*
597 * Skip RUIs that we've already processed.
598 */
599 if (test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags))
600 return 0;
601
602 spin_unlock(&ailp->ail_lock);
603 error = xfs_rui_recover(tp->t_mountp, ruip);
604 spin_lock(&ailp->ail_lock);
605
606 return error;
607}
608
Darrick J. Wong154c7332020-05-01 16:00:54 -0700609STATIC bool
610xfs_rui_item_match(
611 struct xfs_log_item *lip,
612 uint64_t intent_id)
613{
614 return RUI_ITEM(lip)->rui_format.rui_id == intent_id;
615}
616
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700617static const struct xfs_item_ops xfs_rui_item_ops = {
618 .iop_size = xfs_rui_item_size,
619 .iop_format = xfs_rui_item_format,
620 .iop_unpin = xfs_rui_item_unpin,
621 .iop_release = xfs_rui_item_release,
622 .iop_recover = xfs_rui_item_recover,
Darrick J. Wong154c7332020-05-01 16:00:54 -0700623 .iop_match = xfs_rui_item_match,
Darrick J. Wongcba0cca2020-05-01 16:00:51 -0700624};
625
Darrick J. Wong07590a92020-05-01 16:00:49 -0700626/*
627 * This routine is called to create an in-core extent rmap update
628 * item from the rui format structure which was logged on disk.
629 * It allocates an in-core rui, copies the extents from the format
630 * structure into it, and adds the rui to the AIL with the given
631 * LSN.
632 */
633STATIC int
634xlog_recover_rui_commit_pass2(
635 struct xlog *log,
636 struct list_head *buffer_list,
637 struct xlog_recover_item *item,
638 xfs_lsn_t lsn)
639{
640 int error;
641 struct xfs_mount *mp = log->l_mp;
642 struct xfs_rui_log_item *ruip;
643 struct xfs_rui_log_format *rui_formatp;
644
645 rui_formatp = item->ri_buf[0].i_addr;
646
647 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
648 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
649 if (error) {
650 xfs_rui_item_free(ruip);
651 return error;
652 }
653 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
654
655 spin_lock(&log->l_ailp->ail_lock);
656 /*
657 * The RUI has two references. One for the RUD and one for RUI to ensure
658 * it makes it into the AIL. Insert the RUI into the AIL directly and
659 * drop the RUI reference. Note that xfs_trans_ail_update() drops the
660 * AIL lock.
661 */
662 xfs_trans_ail_update(log->l_ailp, &ruip->rui_item, lsn);
663 xfs_rui_release(ruip);
664 return 0;
665}
666
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700667const struct xlog_recover_item_ops xlog_rui_item_ops = {
668 .item_type = XFS_LI_RUI,
Darrick J. Wong07590a92020-05-01 16:00:49 -0700669 .commit_pass2 = xlog_recover_rui_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700670};
671
Darrick J. Wong07590a92020-05-01 16:00:49 -0700672/*
673 * This routine is called when an RUD format structure is found in a committed
674 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
675 * was still in the log. To do this it searches the AIL for the RUI with an id
676 * equal to that in the RUD format structure. If we find it we drop the RUD
677 * reference, which removes the RUI from the AIL and frees it.
678 */
679STATIC int
680xlog_recover_rud_commit_pass2(
681 struct xlog *log,
682 struct list_head *buffer_list,
683 struct xlog_recover_item *item,
684 xfs_lsn_t lsn)
685{
686 struct xfs_rud_log_format *rud_formatp;
Darrick J. Wong07590a92020-05-01 16:00:49 -0700687
688 rud_formatp = item->ri_buf[0].i_addr;
689 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
Darrick J. Wong07590a92020-05-01 16:00:49 -0700690
Darrick J. Wong154c7332020-05-01 16:00:54 -0700691 xlog_recover_release_intent(log, XFS_LI_RUI, rud_formatp->rud_rui_id);
Darrick J. Wong07590a92020-05-01 16:00:49 -0700692 return 0;
693}
694
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700695const struct xlog_recover_item_ops xlog_rud_item_ops = {
696 .item_type = XFS_LI_RUD,
Darrick J. Wong07590a92020-05-01 16:00:49 -0700697 .commit_pass2 = xlog_recover_rud_commit_pass2,
Darrick J. Wong86ffa472020-05-01 16:00:45 -0700698};