1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
8 #include "xfs_format.h"
9 #include "xfs_log_format.h"
10 #include "xfs_trans_resv.h"
12 #include "xfs_shared.h"
13 #include "xfs_mount.h"
14 #include "xfs_trans.h"
15 #include "xfs_trans_priv.h"
16 #include "xfs_buf_item.h"
17 #include "xfs_extfree_item.h"
19 #include "xfs_btree.h"
23 kmem_zone_t *xfs_efi_zone;
24 kmem_zone_t *xfs_efd_zone;
26 static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
28 return container_of(lip, struct xfs_efi_log_item, efi_item);
33 struct xfs_efi_log_item *efip)
35 kmem_free(efip->efi_item.li_lv_shadow);
36 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
39 kmem_zone_free(xfs_efi_zone, efip);
43 * Freeing the efi requires that we remove it from the AIL if it has already
44 * been placed there. However, the EFI may not yet have been placed in the AIL
45 * when called by xfs_efi_release() from EFD processing due to the ordering of
46 * committed vs unpin operations in bulk insert operations. Hence the reference
47 * count to ensure only the last caller frees the EFI.
51 struct xfs_efi_log_item *efip)
53 ASSERT(atomic_read(&efip->efi_refcount) > 0);
54 if (atomic_dec_and_test(&efip->efi_refcount)) {
55 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
56 xfs_efi_item_free(efip);
61 * This returns the number of iovecs needed to log the given efi item.
62 * We only need 1 iovec for an efi item. It just logs the efi_log_format
67 struct xfs_efi_log_item *efip)
69 return sizeof(struct xfs_efi_log_format) +
70 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
75 struct xfs_log_item *lip,
80 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
84 * This is called to fill in the vector of log iovecs for the
85 * given efi log item. We use only 1 iovec, and we point that
86 * at the efi_log_format structure embedded in the efi item.
87 * It is at this point that we assert that all of the extent
88 * slots in the efi item have been filled.
92 struct xfs_log_item *lip,
93 struct xfs_log_vec *lv)
95 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
96 struct xfs_log_iovec *vecp = NULL;
98 ASSERT(atomic_read(&efip->efi_next_extent) ==
99 efip->efi_format.efi_nextents);
101 efip->efi_format.efi_type = XFS_LI_EFI;
102 efip->efi_format.efi_size = 1;
104 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
106 xfs_efi_item_sizeof(efip));
111 * The unpin operation is the last place an EFI is manipulated in the log. It is
112 * either inserted in the AIL or aborted in the event of a log I/O error. In
113 * either case, the EFI transaction has been successfully committed to make it
114 * this far. Therefore, we expect whoever committed the EFI to either construct
115 * and commit the EFD or drop the EFD's reference in the event of error. Simply
116 * drop the log's EFI reference now that the log is done with it.
120 struct xfs_log_item *lip,
123 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
124 xfs_efi_release(efip);
128 * The EFI has been either committed or aborted if the transaction has been
129 * cancelled. If the transaction was cancelled, an EFD isn't going to be
130 * constructed and thus we free the EFI here directly.
133 xfs_efi_item_release(
134 struct xfs_log_item *lip)
136 xfs_efi_release(EFI_ITEM(lip));
140 * This is the ops vector shared by all efi log items.
142 static const struct xfs_item_ops xfs_efi_item_ops = {
143 .iop_size = xfs_efi_item_size,
144 .iop_format = xfs_efi_item_format,
145 .iop_unpin = xfs_efi_item_unpin,
146 .iop_release = xfs_efi_item_release,
151 * Allocate and initialize an efi item with the given number of extents.
153 struct xfs_efi_log_item *
155 struct xfs_mount *mp,
159 struct xfs_efi_log_item *efip;
162 ASSERT(nextents > 0);
163 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
164 size = (uint)(sizeof(xfs_efi_log_item_t) +
165 ((nextents - 1) * sizeof(xfs_extent_t)));
166 efip = kmem_zalloc(size, KM_SLEEP);
168 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
171 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
172 efip->efi_format.efi_nextents = nextents;
173 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
174 atomic_set(&efip->efi_next_extent, 0);
175 atomic_set(&efip->efi_refcount, 2);
181 * Copy an EFI format buffer from the given buf, and into the destination
182 * EFI format structure.
183 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
184 * one of which will be the native format for this kernel.
185 * It will handle the conversion of formats if necessary.
188 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
190 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
192 uint len = sizeof(xfs_efi_log_format_t) +
193 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
194 uint len32 = sizeof(xfs_efi_log_format_32_t) +
195 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
196 uint len64 = sizeof(xfs_efi_log_format_64_t) +
197 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
199 if (buf->i_len == len) {
200 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
202 } else if (buf->i_len == len32) {
203 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
205 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
206 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
207 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
208 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
209 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
210 dst_efi_fmt->efi_extents[i].ext_start =
211 src_efi_fmt_32->efi_extents[i].ext_start;
212 dst_efi_fmt->efi_extents[i].ext_len =
213 src_efi_fmt_32->efi_extents[i].ext_len;
216 } else if (buf->i_len == len64) {
217 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
219 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
220 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
221 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
222 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
223 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
224 dst_efi_fmt->efi_extents[i].ext_start =
225 src_efi_fmt_64->efi_extents[i].ext_start;
226 dst_efi_fmt->efi_extents[i].ext_len =
227 src_efi_fmt_64->efi_extents[i].ext_len;
231 return -EFSCORRUPTED;
234 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
236 return container_of(lip, struct xfs_efd_log_item, efd_item);
240 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
242 kmem_free(efdp->efd_item.li_lv_shadow);
243 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
246 kmem_zone_free(xfs_efd_zone, efdp);
250 * This returns the number of iovecs needed to log the given efd item.
251 * We only need 1 iovec for an efd item. It just logs the efd_log_format
256 struct xfs_efd_log_item *efdp)
258 return sizeof(xfs_efd_log_format_t) +
259 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
264 struct xfs_log_item *lip,
269 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
273 * This is called to fill in the vector of log iovecs for the
274 * given efd log item. We use only 1 iovec, and we point that
275 * at the efd_log_format structure embedded in the efd item.
276 * It is at this point that we assert that all of the extent
277 * slots in the efd item have been filled.
281 struct xfs_log_item *lip,
282 struct xfs_log_vec *lv)
284 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
285 struct xfs_log_iovec *vecp = NULL;
287 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
289 efdp->efd_format.efd_type = XFS_LI_EFD;
290 efdp->efd_format.efd_size = 1;
292 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
294 xfs_efd_item_sizeof(efdp));
298 * The EFD is either committed or aborted if the transaction is cancelled. If
299 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
302 xfs_efd_item_release(
303 struct xfs_log_item *lip)
305 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
307 xfs_efi_release(efdp->efd_efip);
308 xfs_efd_item_free(efdp);
312 * This is the ops vector shared by all efd log items.
314 static const struct xfs_item_ops xfs_efd_item_ops = {
315 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED,
316 .iop_size = xfs_efd_item_size,
317 .iop_format = xfs_efd_item_format,
318 .iop_release = xfs_efd_item_release,
322 * Allocate and initialize an efd item with the given number of extents.
324 struct xfs_efd_log_item *
326 struct xfs_mount *mp,
327 struct xfs_efi_log_item *efip,
331 struct xfs_efd_log_item *efdp;
334 ASSERT(nextents > 0);
335 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
336 size = (uint)(sizeof(xfs_efd_log_item_t) +
337 ((nextents - 1) * sizeof(xfs_extent_t)));
338 efdp = kmem_zalloc(size, KM_SLEEP);
340 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
343 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
344 efdp->efd_efip = efip;
345 efdp->efd_format.efd_nextents = nextents;
346 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
352 * Process an extent free intent item that was recovered from
353 * the log. We need to free the extents that it describes.
357 struct xfs_mount *mp,
358 struct xfs_efi_log_item *efip)
360 struct xfs_efd_log_item *efdp;
361 struct xfs_trans *tp;
365 xfs_fsblock_t startblock_fsb;
367 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
370 * First check the validity of the extents described by the
371 * EFI. If any are bad, then assume that all are bad and
374 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
375 extp = &efip->efi_format.efi_extents[i];
376 startblock_fsb = XFS_BB_TO_FSB(mp,
377 XFS_FSB_TO_DADDR(mp, extp->ext_start));
378 if (startblock_fsb == 0 ||
379 extp->ext_len == 0 ||
380 startblock_fsb >= mp->m_sb.sb_dblocks ||
381 extp->ext_len >= mp->m_sb.sb_agblocks) {
383 * This will pull the EFI from the AIL and
384 * free the memory associated with it.
386 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
387 xfs_efi_release(efip);
392 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
395 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
397 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
398 extp = &efip->efi_format.efi_extents[i];
399 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
401 &XFS_RMAP_OINFO_ANY_OWNER, false);
407 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
408 error = xfs_trans_commit(tp);
412 xfs_trans_cancel(tp);