Loading...
1/*
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18#include "xfs.h"
19#include "xfs_fs.h"
20#include "xfs_log_format.h"
21#include "xfs_trans_resv.h"
22#include "xfs_sb.h"
23#include "xfs_ag.h"
24#include "xfs_mount.h"
25#include "xfs_trans.h"
26#include "xfs_trans_priv.h"
27#include "xfs_buf_item.h"
28#include "xfs_extfree_item.h"
29#include "xfs_log.h"
30
31
32kmem_zone_t *xfs_efi_zone;
33kmem_zone_t *xfs_efd_zone;
34
35static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
36{
37 return container_of(lip, struct xfs_efi_log_item, efi_item);
38}
39
40void
41xfs_efi_item_free(
42 struct xfs_efi_log_item *efip)
43{
44 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
45 kmem_free(efip);
46 else
47 kmem_zone_free(xfs_efi_zone, efip);
48}
49
50/*
51 * Freeing the efi requires that we remove it from the AIL if it has already
52 * been placed there. However, the EFI may not yet have been placed in the AIL
53 * when called by xfs_efi_release() from EFD processing due to the ordering of
54 * committed vs unpin operations in bulk insert operations. Hence the reference
55 * count to ensure only the last caller frees the EFI.
56 */
57STATIC void
58__xfs_efi_release(
59 struct xfs_efi_log_item *efip)
60{
61 struct xfs_ail *ailp = efip->efi_item.li_ailp;
62
63 if (atomic_dec_and_test(&efip->efi_refcount)) {
64 spin_lock(&ailp->xa_lock);
65 /* xfs_trans_ail_delete() drops the AIL lock. */
66 xfs_trans_ail_delete(ailp, &efip->efi_item,
67 SHUTDOWN_LOG_IO_ERROR);
68 xfs_efi_item_free(efip);
69 }
70}
71
72/*
73 * This returns the number of iovecs needed to log the given efi item.
74 * We only need 1 iovec for an efi item. It just logs the efi_log_format
75 * structure.
76 */
77static inline int
78xfs_efi_item_sizeof(
79 struct xfs_efi_log_item *efip)
80{
81 return sizeof(struct xfs_efi_log_format) +
82 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
83}
84
85STATIC void
86xfs_efi_item_size(
87 struct xfs_log_item *lip,
88 int *nvecs,
89 int *nbytes)
90{
91 *nvecs += 1;
92 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
93}
94
95/*
96 * This is called to fill in the vector of log iovecs for the
97 * given efi log item. We use only 1 iovec, and we point that
98 * at the efi_log_format structure embedded in the efi item.
99 * It is at this point that we assert that all of the extent
100 * slots in the efi item have been filled.
101 */
102STATIC void
103xfs_efi_item_format(
104 struct xfs_log_item *lip,
105 struct xfs_log_vec *lv)
106{
107 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
108 struct xfs_log_iovec *vecp = NULL;
109
110 ASSERT(atomic_read(&efip->efi_next_extent) ==
111 efip->efi_format.efi_nextents);
112
113 efip->efi_format.efi_type = XFS_LI_EFI;
114 efip->efi_format.efi_size = 1;
115
116 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
117 &efip->efi_format,
118 xfs_efi_item_sizeof(efip));
119}
120
121
122/*
123 * Pinning has no meaning for an efi item, so just return.
124 */
125STATIC void
126xfs_efi_item_pin(
127 struct xfs_log_item *lip)
128{
129}
130
131/*
132 * While EFIs cannot really be pinned, the unpin operation is the last place at
133 * which the EFI is manipulated during a transaction. If we are being asked to
134 * remove the EFI it's because the transaction has been cancelled and by
135 * definition that means the EFI cannot be in the AIL so remove it from the
136 * transaction and free it. Otherwise coordinate with xfs_efi_release()
137 * to determine who gets to free the EFI.
138 */
139STATIC void
140xfs_efi_item_unpin(
141 struct xfs_log_item *lip,
142 int remove)
143{
144 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
145
146 if (remove) {
147 ASSERT(!(lip->li_flags & XFS_LI_IN_AIL));
148 if (lip->li_desc)
149 xfs_trans_del_item(lip);
150 xfs_efi_item_free(efip);
151 return;
152 }
153 __xfs_efi_release(efip);
154}
155
156/*
157 * Efi items have no locking or pushing. However, since EFIs are pulled from
158 * the AIL when their corresponding EFDs are committed to disk, their situation
159 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
160 * will eventually flush the log. This should help in getting the EFI out of
161 * the AIL.
162 */
163STATIC uint
164xfs_efi_item_push(
165 struct xfs_log_item *lip,
166 struct list_head *buffer_list)
167{
168 return XFS_ITEM_PINNED;
169}
170
171STATIC void
172xfs_efi_item_unlock(
173 struct xfs_log_item *lip)
174{
175 if (lip->li_flags & XFS_LI_ABORTED)
176 xfs_efi_item_free(EFI_ITEM(lip));
177}
178
179/*
180 * The EFI is logged only once and cannot be moved in the log, so simply return
181 * the lsn at which it's been logged.
182 */
183STATIC xfs_lsn_t
184xfs_efi_item_committed(
185 struct xfs_log_item *lip,
186 xfs_lsn_t lsn)
187{
188 return lsn;
189}
190
191/*
192 * The EFI dependency tracking op doesn't do squat. It can't because
193 * it doesn't know where the free extent is coming from. The dependency
194 * tracking has to be handled by the "enclosing" metadata object. For
195 * example, for inodes, the inode is locked throughout the extent freeing
196 * so the dependency should be recorded there.
197 */
198STATIC void
199xfs_efi_item_committing(
200 struct xfs_log_item *lip,
201 xfs_lsn_t lsn)
202{
203}
204
205/*
206 * This is the ops vector shared by all efi log items.
207 */
208static const struct xfs_item_ops xfs_efi_item_ops = {
209 .iop_size = xfs_efi_item_size,
210 .iop_format = xfs_efi_item_format,
211 .iop_pin = xfs_efi_item_pin,
212 .iop_unpin = xfs_efi_item_unpin,
213 .iop_unlock = xfs_efi_item_unlock,
214 .iop_committed = xfs_efi_item_committed,
215 .iop_push = xfs_efi_item_push,
216 .iop_committing = xfs_efi_item_committing
217};
218
219
220/*
221 * Allocate and initialize an efi item with the given number of extents.
222 */
223struct xfs_efi_log_item *
224xfs_efi_init(
225 struct xfs_mount *mp,
226 uint nextents)
227
228{
229 struct xfs_efi_log_item *efip;
230 uint size;
231
232 ASSERT(nextents > 0);
233 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
234 size = (uint)(sizeof(xfs_efi_log_item_t) +
235 ((nextents - 1) * sizeof(xfs_extent_t)));
236 efip = kmem_zalloc(size, KM_SLEEP);
237 } else {
238 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
239 }
240
241 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
242 efip->efi_format.efi_nextents = nextents;
243 efip->efi_format.efi_id = (__psint_t)(void*)efip;
244 atomic_set(&efip->efi_next_extent, 0);
245 atomic_set(&efip->efi_refcount, 2);
246
247 return efip;
248}
249
250/*
251 * Copy an EFI format buffer from the given buf, and into the destination
252 * EFI format structure.
253 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
254 * one of which will be the native format for this kernel.
255 * It will handle the conversion of formats if necessary.
256 */
257int
258xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
259{
260 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
261 uint i;
262 uint len = sizeof(xfs_efi_log_format_t) +
263 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
264 uint len32 = sizeof(xfs_efi_log_format_32_t) +
265 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
266 uint len64 = sizeof(xfs_efi_log_format_64_t) +
267 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
268
269 if (buf->i_len == len) {
270 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
271 return 0;
272 } else if (buf->i_len == len32) {
273 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
274
275 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
276 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
277 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
278 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
279 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
280 dst_efi_fmt->efi_extents[i].ext_start =
281 src_efi_fmt_32->efi_extents[i].ext_start;
282 dst_efi_fmt->efi_extents[i].ext_len =
283 src_efi_fmt_32->efi_extents[i].ext_len;
284 }
285 return 0;
286 } else if (buf->i_len == len64) {
287 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
288
289 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
290 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
291 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
292 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
293 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
294 dst_efi_fmt->efi_extents[i].ext_start =
295 src_efi_fmt_64->efi_extents[i].ext_start;
296 dst_efi_fmt->efi_extents[i].ext_len =
297 src_efi_fmt_64->efi_extents[i].ext_len;
298 }
299 return 0;
300 }
301 return EFSCORRUPTED;
302}
303
304/*
305 * This is called by the efd item code below to release references to the given
306 * efi item. Each efd calls this with the number of extents that it has
307 * logged, and when the sum of these reaches the total number of extents logged
308 * by this efi item we can free the efi item.
309 */
310void
311xfs_efi_release(xfs_efi_log_item_t *efip,
312 uint nextents)
313{
314 ASSERT(atomic_read(&efip->efi_next_extent) >= nextents);
315 if (atomic_sub_and_test(nextents, &efip->efi_next_extent)) {
316 /* recovery needs us to drop the EFI reference, too */
317 if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags))
318 __xfs_efi_release(efip);
319
320 __xfs_efi_release(efip);
321 /* efip may now have been freed, do not reference it again. */
322 }
323}
324
325static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
326{
327 return container_of(lip, struct xfs_efd_log_item, efd_item);
328}
329
330STATIC void
331xfs_efd_item_free(struct xfs_efd_log_item *efdp)
332{
333 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
334 kmem_free(efdp);
335 else
336 kmem_zone_free(xfs_efd_zone, efdp);
337}
338
339/*
340 * This returns the number of iovecs needed to log the given efd item.
341 * We only need 1 iovec for an efd item. It just logs the efd_log_format
342 * structure.
343 */
344static inline int
345xfs_efd_item_sizeof(
346 struct xfs_efd_log_item *efdp)
347{
348 return sizeof(xfs_efd_log_format_t) +
349 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
350}
351
352STATIC void
353xfs_efd_item_size(
354 struct xfs_log_item *lip,
355 int *nvecs,
356 int *nbytes)
357{
358 *nvecs += 1;
359 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
360}
361
362/*
363 * This is called to fill in the vector of log iovecs for the
364 * given efd log item. We use only 1 iovec, and we point that
365 * at the efd_log_format structure embedded in the efd item.
366 * It is at this point that we assert that all of the extent
367 * slots in the efd item have been filled.
368 */
369STATIC void
370xfs_efd_item_format(
371 struct xfs_log_item *lip,
372 struct xfs_log_vec *lv)
373{
374 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
375 struct xfs_log_iovec *vecp = NULL;
376
377 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
378
379 efdp->efd_format.efd_type = XFS_LI_EFD;
380 efdp->efd_format.efd_size = 1;
381
382 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
383 &efdp->efd_format,
384 xfs_efd_item_sizeof(efdp));
385}
386
387/*
388 * Pinning has no meaning for an efd item, so just return.
389 */
390STATIC void
391xfs_efd_item_pin(
392 struct xfs_log_item *lip)
393{
394}
395
396/*
397 * Since pinning has no meaning for an efd item, unpinning does
398 * not either.
399 */
400STATIC void
401xfs_efd_item_unpin(
402 struct xfs_log_item *lip,
403 int remove)
404{
405}
406
407/*
408 * There isn't much you can do to push on an efd item. It is simply stuck
409 * waiting for the log to be flushed to disk.
410 */
411STATIC uint
412xfs_efd_item_push(
413 struct xfs_log_item *lip,
414 struct list_head *buffer_list)
415{
416 return XFS_ITEM_PINNED;
417}
418
419STATIC void
420xfs_efd_item_unlock(
421 struct xfs_log_item *lip)
422{
423 if (lip->li_flags & XFS_LI_ABORTED)
424 xfs_efd_item_free(EFD_ITEM(lip));
425}
426
427/*
428 * When the efd item is committed to disk, all we need to do
429 * is delete our reference to our partner efi item and then
430 * free ourselves. Since we're freeing ourselves we must
431 * return -1 to keep the transaction code from further referencing
432 * this item.
433 */
434STATIC xfs_lsn_t
435xfs_efd_item_committed(
436 struct xfs_log_item *lip,
437 xfs_lsn_t lsn)
438{
439 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
440
441 /*
442 * If we got a log I/O error, it's always the case that the LR with the
443 * EFI got unpinned and freed before the EFD got aborted.
444 */
445 if (!(lip->li_flags & XFS_LI_ABORTED))
446 xfs_efi_release(efdp->efd_efip, efdp->efd_format.efd_nextents);
447
448 xfs_efd_item_free(efdp);
449 return (xfs_lsn_t)-1;
450}
451
452/*
453 * The EFD dependency tracking op doesn't do squat. It can't because
454 * it doesn't know where the free extent is coming from. The dependency
455 * tracking has to be handled by the "enclosing" metadata object. For
456 * example, for inodes, the inode is locked throughout the extent freeing
457 * so the dependency should be recorded there.
458 */
459STATIC void
460xfs_efd_item_committing(
461 struct xfs_log_item *lip,
462 xfs_lsn_t lsn)
463{
464}
465
466/*
467 * This is the ops vector shared by all efd log items.
468 */
469static const struct xfs_item_ops xfs_efd_item_ops = {
470 .iop_size = xfs_efd_item_size,
471 .iop_format = xfs_efd_item_format,
472 .iop_pin = xfs_efd_item_pin,
473 .iop_unpin = xfs_efd_item_unpin,
474 .iop_unlock = xfs_efd_item_unlock,
475 .iop_committed = xfs_efd_item_committed,
476 .iop_push = xfs_efd_item_push,
477 .iop_committing = xfs_efd_item_committing
478};
479
480/*
481 * Allocate and initialize an efd item with the given number of extents.
482 */
483struct xfs_efd_log_item *
484xfs_efd_init(
485 struct xfs_mount *mp,
486 struct xfs_efi_log_item *efip,
487 uint nextents)
488
489{
490 struct xfs_efd_log_item *efdp;
491 uint size;
492
493 ASSERT(nextents > 0);
494 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
495 size = (uint)(sizeof(xfs_efd_log_item_t) +
496 ((nextents - 1) * sizeof(xfs_extent_t)));
497 efdp = kmem_zalloc(size, KM_SLEEP);
498 } else {
499 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
500 }
501
502 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
503 efdp->efd_efip = efip;
504 efdp->efd_format.efd_nextents = nextents;
505 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
506
507 return efdp;
508}
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
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"
11#include "xfs_bit.h"
12#include "xfs_shared.h"
13#include "xfs_mount.h"
14#include "xfs_ag.h"
15#include "xfs_defer.h"
16#include "xfs_trans.h"
17#include "xfs_trans_priv.h"
18#include "xfs_extfree_item.h"
19#include "xfs_log.h"
20#include "xfs_btree.h"
21#include "xfs_rmap.h"
22#include "xfs_alloc.h"
23#include "xfs_bmap.h"
24#include "xfs_trace.h"
25#include "xfs_error.h"
26#include "xfs_log_priv.h"
27#include "xfs_log_recover.h"
28#include "xfs_rtalloc.h"
29#include "xfs_inode.h"
30#include "xfs_rtbitmap.h"
31#include "xfs_rtgroup.h"
32
33struct kmem_cache *xfs_efi_cache;
34struct kmem_cache *xfs_efd_cache;
35
36static const struct xfs_item_ops xfs_efi_item_ops;
37
38static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
39{
40 return container_of(lip, struct xfs_efi_log_item, efi_item);
41}
42
43STATIC void
44xfs_efi_item_free(
45 struct xfs_efi_log_item *efip)
46{
47 kvfree(efip->efi_item.li_lv_shadow);
48 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
49 kfree(efip);
50 else
51 kmem_cache_free(xfs_efi_cache, efip);
52}
53
54/*
55 * Freeing the efi requires that we remove it from the AIL if it has already
56 * been placed there. However, the EFI may not yet have been placed in the AIL
57 * when called by xfs_efi_release() from EFD processing due to the ordering of
58 * committed vs unpin operations in bulk insert operations. Hence the reference
59 * count to ensure only the last caller frees the EFI.
60 */
61STATIC void
62xfs_efi_release(
63 struct xfs_efi_log_item *efip)
64{
65 ASSERT(atomic_read(&efip->efi_refcount) > 0);
66 if (!atomic_dec_and_test(&efip->efi_refcount))
67 return;
68
69 xfs_trans_ail_delete(&efip->efi_item, 0);
70 xfs_efi_item_free(efip);
71}
72
73STATIC void
74xfs_efi_item_size(
75 struct xfs_log_item *lip,
76 int *nvecs,
77 int *nbytes)
78{
79 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
80
81 *nvecs += 1;
82 *nbytes += xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents);
83}
84
85/*
86 * This is called to fill in the vector of log iovecs for the
87 * given efi log item. We use only 1 iovec, and we point that
88 * at the efi_log_format structure embedded in the efi item.
89 * It is at this point that we assert that all of the extent
90 * slots in the efi item have been filled.
91 */
92STATIC void
93xfs_efi_item_format(
94 struct xfs_log_item *lip,
95 struct xfs_log_vec *lv)
96{
97 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
98 struct xfs_log_iovec *vecp = NULL;
99
100 ASSERT(atomic_read(&efip->efi_next_extent) ==
101 efip->efi_format.efi_nextents);
102 ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT);
103
104 efip->efi_format.efi_type = lip->li_type;
105 efip->efi_format.efi_size = 1;
106
107 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT, &efip->efi_format,
108 xfs_efi_log_format_sizeof(efip->efi_format.efi_nextents));
109}
110
111/*
112 * The unpin operation is the last place an EFI is manipulated in the log. It is
113 * either inserted in the AIL or aborted in the event of a log I/O error. In
114 * either case, the EFI transaction has been successfully committed to make it
115 * this far. Therefore, we expect whoever committed the EFI to either construct
116 * and commit the EFD or drop the EFD's reference in the event of error. Simply
117 * drop the log's EFI reference now that the log is done with it.
118 */
119STATIC void
120xfs_efi_item_unpin(
121 struct xfs_log_item *lip,
122 int remove)
123{
124 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
125 xfs_efi_release(efip);
126}
127
128/*
129 * The EFI has been either committed or aborted if the transaction has been
130 * cancelled. If the transaction was cancelled, an EFD isn't going to be
131 * constructed and thus we free the EFI here directly.
132 */
133STATIC void
134xfs_efi_item_release(
135 struct xfs_log_item *lip)
136{
137 xfs_efi_release(EFI_ITEM(lip));
138}
139
140/*
141 * Allocate and initialize an efi item with the given number of extents.
142 */
143STATIC struct xfs_efi_log_item *
144xfs_efi_init(
145 struct xfs_mount *mp,
146 unsigned short item_type,
147 uint nextents)
148{
149 struct xfs_efi_log_item *efip;
150
151 ASSERT(item_type == XFS_LI_EFI || item_type == XFS_LI_EFI_RT);
152 ASSERT(nextents > 0);
153
154 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
155 efip = kzalloc(xfs_efi_log_item_sizeof(nextents),
156 GFP_KERNEL | __GFP_NOFAIL);
157 } else {
158 efip = kmem_cache_zalloc(xfs_efi_cache,
159 GFP_KERNEL | __GFP_NOFAIL);
160 }
161
162 xfs_log_item_init(mp, &efip->efi_item, item_type, &xfs_efi_item_ops);
163 efip->efi_format.efi_nextents = nextents;
164 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
165 atomic_set(&efip->efi_next_extent, 0);
166 atomic_set(&efip->efi_refcount, 2);
167
168 return efip;
169}
170
171/*
172 * Copy an EFI format buffer from the given buf, and into the destination
173 * EFI format structure.
174 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
175 * one of which will be the native format for this kernel.
176 * It will handle the conversion of formats if necessary.
177 */
178STATIC int
179xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
180{
181 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
182 uint i;
183 uint len = xfs_efi_log_format_sizeof(src_efi_fmt->efi_nextents);
184 uint len32 = xfs_efi_log_format32_sizeof(src_efi_fmt->efi_nextents);
185 uint len64 = xfs_efi_log_format64_sizeof(src_efi_fmt->efi_nextents);
186
187 if (buf->i_len == len) {
188 memcpy(dst_efi_fmt, src_efi_fmt,
189 offsetof(struct xfs_efi_log_format, efi_extents));
190 for (i = 0; i < src_efi_fmt->efi_nextents; i++)
191 memcpy(&dst_efi_fmt->efi_extents[i],
192 &src_efi_fmt->efi_extents[i],
193 sizeof(struct xfs_extent));
194 return 0;
195 } else if (buf->i_len == len32) {
196 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
197
198 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
199 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
200 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
201 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
202 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
203 dst_efi_fmt->efi_extents[i].ext_start =
204 src_efi_fmt_32->efi_extents[i].ext_start;
205 dst_efi_fmt->efi_extents[i].ext_len =
206 src_efi_fmt_32->efi_extents[i].ext_len;
207 }
208 return 0;
209 } else if (buf->i_len == len64) {
210 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
211
212 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
213 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
214 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
215 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
216 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
217 dst_efi_fmt->efi_extents[i].ext_start =
218 src_efi_fmt_64->efi_extents[i].ext_start;
219 dst_efi_fmt->efi_extents[i].ext_len =
220 src_efi_fmt_64->efi_extents[i].ext_len;
221 }
222 return 0;
223 }
224 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, NULL, buf->i_addr,
225 buf->i_len);
226 return -EFSCORRUPTED;
227}
228
229static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
230{
231 return container_of(lip, struct xfs_efd_log_item, efd_item);
232}
233
234STATIC void
235xfs_efd_item_free(struct xfs_efd_log_item *efdp)
236{
237 kvfree(efdp->efd_item.li_lv_shadow);
238 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
239 kfree(efdp);
240 else
241 kmem_cache_free(xfs_efd_cache, efdp);
242}
243
244STATIC void
245xfs_efd_item_size(
246 struct xfs_log_item *lip,
247 int *nvecs,
248 int *nbytes)
249{
250 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
251
252 *nvecs += 1;
253 *nbytes += xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents);
254}
255
256/*
257 * This is called to fill in the vector of log iovecs for the
258 * given efd log item. We use only 1 iovec, and we point that
259 * at the efd_log_format structure embedded in the efd item.
260 * It is at this point that we assert that all of the extent
261 * slots in the efd item have been filled.
262 */
263STATIC void
264xfs_efd_item_format(
265 struct xfs_log_item *lip,
266 struct xfs_log_vec *lv)
267{
268 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
269 struct xfs_log_iovec *vecp = NULL;
270
271 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
272 ASSERT(lip->li_type == XFS_LI_EFD || lip->li_type == XFS_LI_EFD_RT);
273
274 efdp->efd_format.efd_type = lip->li_type;
275 efdp->efd_format.efd_size = 1;
276
277 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT, &efdp->efd_format,
278 xfs_efd_log_format_sizeof(efdp->efd_format.efd_nextents));
279}
280
281/*
282 * The EFD is either committed or aborted if the transaction is cancelled. If
283 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
284 */
285STATIC void
286xfs_efd_item_release(
287 struct xfs_log_item *lip)
288{
289 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
290
291 xfs_efi_release(efdp->efd_efip);
292 xfs_efd_item_free(efdp);
293}
294
295static struct xfs_log_item *
296xfs_efd_item_intent(
297 struct xfs_log_item *lip)
298{
299 return &EFD_ITEM(lip)->efd_efip->efi_item;
300}
301
302static const struct xfs_item_ops xfs_efd_item_ops = {
303 .flags = XFS_ITEM_RELEASE_WHEN_COMMITTED |
304 XFS_ITEM_INTENT_DONE,
305 .iop_size = xfs_efd_item_size,
306 .iop_format = xfs_efd_item_format,
307 .iop_release = xfs_efd_item_release,
308 .iop_intent = xfs_efd_item_intent,
309};
310
311static inline struct xfs_extent_free_item *xefi_entry(const struct list_head *e)
312{
313 return list_entry(e, struct xfs_extent_free_item, xefi_list);
314}
315
316static inline bool
317xfs_efi_item_isrt(const struct xfs_log_item *lip)
318{
319 ASSERT(lip->li_type == XFS_LI_EFI || lip->li_type == XFS_LI_EFI_RT);
320
321 return lip->li_type == XFS_LI_EFI_RT;
322}
323
324/*
325 * Fill the EFD with all extents from the EFI when we need to roll the
326 * transaction and continue with a new EFI.
327 *
328 * This simply copies all the extents in the EFI to the EFD rather than make
329 * assumptions about which extents in the EFI have already been processed. We
330 * currently keep the xefi list in the same order as the EFI extent list, but
331 * that may not always be the case. Copying everything avoids leaving a landmine
332 * were we fail to cancel all the extents in an EFI if the xefi list is
333 * processed in a different order to the extents in the EFI.
334 */
335static void
336xfs_efd_from_efi(
337 struct xfs_efd_log_item *efdp)
338{
339 struct xfs_efi_log_item *efip = efdp->efd_efip;
340 uint i;
341
342 ASSERT(efip->efi_format.efi_nextents > 0);
343 ASSERT(efdp->efd_next_extent < efip->efi_format.efi_nextents);
344
345 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
346 efdp->efd_format.efd_extents[i] =
347 efip->efi_format.efi_extents[i];
348 }
349 efdp->efd_next_extent = efip->efi_format.efi_nextents;
350}
351
352static void
353xfs_efd_add_extent(
354 struct xfs_efd_log_item *efdp,
355 struct xfs_extent_free_item *xefi)
356{
357 struct xfs_extent *extp;
358
359 ASSERT(efdp->efd_next_extent < efdp->efd_format.efd_nextents);
360
361 extp = &efdp->efd_format.efd_extents[efdp->efd_next_extent];
362 extp->ext_start = xefi->xefi_startblock;
363 extp->ext_len = xefi->xefi_blockcount;
364
365 efdp->efd_next_extent++;
366}
367
368/* Sort bmap items by AG. */
369static int
370xfs_extent_free_diff_items(
371 void *priv,
372 const struct list_head *a,
373 const struct list_head *b)
374{
375 struct xfs_extent_free_item *ra = xefi_entry(a);
376 struct xfs_extent_free_item *rb = xefi_entry(b);
377
378 return ra->xefi_group->xg_gno - rb->xefi_group->xg_gno;
379}
380
381/* Log a free extent to the intent item. */
382STATIC void
383xfs_extent_free_log_item(
384 struct xfs_trans *tp,
385 struct xfs_efi_log_item *efip,
386 struct xfs_extent_free_item *xefi)
387{
388 uint next_extent;
389 struct xfs_extent *extp;
390
391 /*
392 * atomic_inc_return gives us the value after the increment;
393 * we want to use it as an array index so we need to subtract 1 from
394 * it.
395 */
396 next_extent = atomic_inc_return(&efip->efi_next_extent) - 1;
397 ASSERT(next_extent < efip->efi_format.efi_nextents);
398 extp = &efip->efi_format.efi_extents[next_extent];
399 extp->ext_start = xefi->xefi_startblock;
400 extp->ext_len = xefi->xefi_blockcount;
401}
402
403static struct xfs_log_item *
404__xfs_extent_free_create_intent(
405 struct xfs_trans *tp,
406 struct list_head *items,
407 unsigned int count,
408 bool sort,
409 unsigned short item_type)
410{
411 struct xfs_mount *mp = tp->t_mountp;
412 struct xfs_efi_log_item *efip;
413 struct xfs_extent_free_item *xefi;
414
415 ASSERT(count > 0);
416
417 efip = xfs_efi_init(mp, item_type, count);
418 if (sort)
419 list_sort(mp, items, xfs_extent_free_diff_items);
420 list_for_each_entry(xefi, items, xefi_list)
421 xfs_extent_free_log_item(tp, efip, xefi);
422 return &efip->efi_item;
423}
424
425static struct xfs_log_item *
426xfs_extent_free_create_intent(
427 struct xfs_trans *tp,
428 struct list_head *items,
429 unsigned int count,
430 bool sort)
431{
432 return __xfs_extent_free_create_intent(tp, items, count, sort,
433 XFS_LI_EFI);
434}
435
436static inline unsigned short
437xfs_efd_type_from_efi(const struct xfs_efi_log_item *efip)
438{
439 return xfs_efi_item_isrt(&efip->efi_item) ? XFS_LI_EFD_RT : XFS_LI_EFD;
440}
441
442/* Get an EFD so we can process all the free extents. */
443static struct xfs_log_item *
444xfs_extent_free_create_done(
445 struct xfs_trans *tp,
446 struct xfs_log_item *intent,
447 unsigned int count)
448{
449 struct xfs_efi_log_item *efip = EFI_ITEM(intent);
450 struct xfs_efd_log_item *efdp;
451
452 ASSERT(count > 0);
453
454 if (count > XFS_EFD_MAX_FAST_EXTENTS) {
455 efdp = kzalloc(xfs_efd_log_item_sizeof(count),
456 GFP_KERNEL | __GFP_NOFAIL);
457 } else {
458 efdp = kmem_cache_zalloc(xfs_efd_cache,
459 GFP_KERNEL | __GFP_NOFAIL);
460 }
461
462 xfs_log_item_init(tp->t_mountp, &efdp->efd_item,
463 xfs_efd_type_from_efi(efip), &xfs_efd_item_ops);
464 efdp->efd_efip = efip;
465 efdp->efd_format.efd_nextents = count;
466 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
467
468 return &efdp->efd_item;
469}
470
471static inline const struct xfs_defer_op_type *
472xefi_ops(
473 struct xfs_extent_free_item *xefi)
474{
475 if (xfs_efi_is_realtime(xefi))
476 return &xfs_rtextent_free_defer_type;
477 if (xefi->xefi_agresv == XFS_AG_RESV_AGFL)
478 return &xfs_agfl_free_defer_type;
479 return &xfs_extent_free_defer_type;
480}
481
482/* Add this deferred EFI to the transaction. */
483void
484xfs_extent_free_defer_add(
485 struct xfs_trans *tp,
486 struct xfs_extent_free_item *xefi,
487 struct xfs_defer_pending **dfpp)
488{
489 struct xfs_mount *mp = tp->t_mountp;
490
491 xefi->xefi_group = xfs_group_intent_get(mp, xefi->xefi_startblock,
492 xfs_efi_is_realtime(xefi) ? XG_TYPE_RTG : XG_TYPE_AG);
493
494 trace_xfs_extent_free_defer(mp, xefi);
495 *dfpp = xfs_defer_add(tp, &xefi->xefi_list, xefi_ops(xefi));
496}
497
498/* Cancel a free extent. */
499STATIC void
500xfs_extent_free_cancel_item(
501 struct list_head *item)
502{
503 struct xfs_extent_free_item *xefi = xefi_entry(item);
504
505 xfs_group_intent_put(xefi->xefi_group);
506 kmem_cache_free(xfs_extfree_item_cache, xefi);
507}
508
509/* Process a free extent. */
510STATIC int
511xfs_extent_free_finish_item(
512 struct xfs_trans *tp,
513 struct xfs_log_item *done,
514 struct list_head *item,
515 struct xfs_btree_cur **state)
516{
517 struct xfs_owner_info oinfo = { };
518 struct xfs_extent_free_item *xefi = xefi_entry(item);
519 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
520 struct xfs_mount *mp = tp->t_mountp;
521 xfs_agblock_t agbno;
522 int error = 0;
523
524 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
525
526 oinfo.oi_owner = xefi->xefi_owner;
527 if (xefi->xefi_flags & XFS_EFI_ATTR_FORK)
528 oinfo.oi_flags |= XFS_OWNER_INFO_ATTR_FORK;
529 if (xefi->xefi_flags & XFS_EFI_BMBT_BLOCK)
530 oinfo.oi_flags |= XFS_OWNER_INFO_BMBT_BLOCK;
531
532 trace_xfs_extent_free_deferred(mp, xefi);
533
534 /*
535 * If we need a new transaction to make progress, the caller will log a
536 * new EFI with the current contents. It will also log an EFD to cancel
537 * the existing EFI, and so we need to copy all the unprocessed extents
538 * in this EFI to the EFD so this works correctly.
539 */
540 if (!(xefi->xefi_flags & XFS_EFI_CANCELLED))
541 error = __xfs_free_extent(tp, to_perag(xefi->xefi_group), agbno,
542 xefi->xefi_blockcount, &oinfo, xefi->xefi_agresv,
543 xefi->xefi_flags & XFS_EFI_SKIP_DISCARD);
544 if (error == -EAGAIN) {
545 xfs_efd_from_efi(efdp);
546 return error;
547 }
548
549 xfs_efd_add_extent(efdp, xefi);
550 xfs_extent_free_cancel_item(item);
551 return error;
552}
553
554/* Abort all pending EFIs. */
555STATIC void
556xfs_extent_free_abort_intent(
557 struct xfs_log_item *intent)
558{
559 xfs_efi_release(EFI_ITEM(intent));
560}
561
562/*
563 * AGFL blocks are accounted differently in the reserve pools and are not
564 * inserted into the busy extent list.
565 */
566STATIC int
567xfs_agfl_free_finish_item(
568 struct xfs_trans *tp,
569 struct xfs_log_item *done,
570 struct list_head *item,
571 struct xfs_btree_cur **state)
572{
573 struct xfs_owner_info oinfo = { };
574 struct xfs_mount *mp = tp->t_mountp;
575 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
576 struct xfs_extent_free_item *xefi = xefi_entry(item);
577 struct xfs_buf *agbp;
578 int error;
579 xfs_agblock_t agbno;
580
581 ASSERT(xefi->xefi_blockcount == 1);
582 agbno = XFS_FSB_TO_AGBNO(mp, xefi->xefi_startblock);
583 oinfo.oi_owner = xefi->xefi_owner;
584
585 trace_xfs_agfl_free_deferred(mp, xefi);
586
587 error = xfs_alloc_read_agf(to_perag(xefi->xefi_group), tp, 0, &agbp);
588 if (!error)
589 error = xfs_free_ag_extent(tp, agbp, agbno, 1, &oinfo,
590 XFS_AG_RESV_AGFL);
591
592 xfs_efd_add_extent(efdp, xefi);
593 xfs_extent_free_cancel_item(&xefi->xefi_list);
594 return error;
595}
596
597/* Is this recovered EFI ok? */
598static inline bool
599xfs_efi_validate_ext(
600 struct xfs_mount *mp,
601 bool isrt,
602 struct xfs_extent *extp)
603{
604 if (isrt)
605 return xfs_verify_rtbext(mp, extp->ext_start, extp->ext_len);
606
607 return xfs_verify_fsbext(mp, extp->ext_start, extp->ext_len);
608}
609
610static inline void
611xfs_efi_recover_work(
612 struct xfs_mount *mp,
613 struct xfs_defer_pending *dfp,
614 bool isrt,
615 struct xfs_extent *extp)
616{
617 struct xfs_extent_free_item *xefi;
618
619 xefi = kmem_cache_zalloc(xfs_extfree_item_cache,
620 GFP_KERNEL | __GFP_NOFAIL);
621 xefi->xefi_startblock = extp->ext_start;
622 xefi->xefi_blockcount = extp->ext_len;
623 xefi->xefi_agresv = XFS_AG_RESV_NONE;
624 xefi->xefi_owner = XFS_RMAP_OWN_UNKNOWN;
625 xefi->xefi_group = xfs_group_intent_get(mp, extp->ext_start,
626 isrt ? XG_TYPE_RTG : XG_TYPE_AG);
627 if (isrt)
628 xefi->xefi_flags |= XFS_EFI_REALTIME;
629
630 xfs_defer_add_item(dfp, &xefi->xefi_list);
631}
632
633/*
634 * Process an extent free intent item that was recovered from
635 * the log. We need to free the extents that it describes.
636 */
637STATIC int
638xfs_extent_free_recover_work(
639 struct xfs_defer_pending *dfp,
640 struct list_head *capture_list)
641{
642 struct xfs_trans_res resv;
643 struct xfs_log_item *lip = dfp->dfp_intent;
644 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
645 struct xfs_mount *mp = lip->li_log->l_mp;
646 struct xfs_trans *tp;
647 int i;
648 int error = 0;
649 bool isrt = xfs_efi_item_isrt(lip);
650
651 /*
652 * First check the validity of the extents described by the EFI. If
653 * any are bad, then assume that all are bad and just toss the EFI.
654 * Mixing RT and non-RT extents in the same EFI item is not allowed.
655 */
656 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
657 if (!xfs_efi_validate_ext(mp, isrt,
658 &efip->efi_format.efi_extents[i])) {
659 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
660 &efip->efi_format,
661 sizeof(efip->efi_format));
662 return -EFSCORRUPTED;
663 }
664
665 xfs_efi_recover_work(mp, dfp, isrt,
666 &efip->efi_format.efi_extents[i]);
667 }
668
669 resv = xlog_recover_resv(&M_RES(mp)->tr_itruncate);
670 error = xfs_trans_alloc(mp, &resv, 0, 0, 0, &tp);
671 if (error)
672 return error;
673
674 error = xlog_recover_finish_intent(tp, dfp);
675 if (error == -EFSCORRUPTED)
676 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
677 &efip->efi_format,
678 sizeof(efip->efi_format));
679 if (error)
680 goto abort_error;
681
682 return xfs_defer_ops_capture_and_commit(tp, capture_list);
683
684abort_error:
685 xfs_trans_cancel(tp);
686 return error;
687}
688
689/* Relog an intent item to push the log tail forward. */
690static struct xfs_log_item *
691xfs_extent_free_relog_intent(
692 struct xfs_trans *tp,
693 struct xfs_log_item *intent,
694 struct xfs_log_item *done_item)
695{
696 struct xfs_efd_log_item *efdp = EFD_ITEM(done_item);
697 struct xfs_efi_log_item *efip;
698 struct xfs_extent *extp;
699 unsigned int count;
700
701 count = EFI_ITEM(intent)->efi_format.efi_nextents;
702 extp = EFI_ITEM(intent)->efi_format.efi_extents;
703
704 ASSERT(intent->li_type == XFS_LI_EFI || intent->li_type == XFS_LI_EFI_RT);
705
706 efdp->efd_next_extent = count;
707 memcpy(efdp->efd_format.efd_extents, extp, count * sizeof(*extp));
708
709 efip = xfs_efi_init(tp->t_mountp, intent->li_type, count);
710 memcpy(efip->efi_format.efi_extents, extp, count * sizeof(*extp));
711 atomic_set(&efip->efi_next_extent, count);
712
713 return &efip->efi_item;
714}
715
716const struct xfs_defer_op_type xfs_extent_free_defer_type = {
717 .name = "extent_free",
718 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
719 .create_intent = xfs_extent_free_create_intent,
720 .abort_intent = xfs_extent_free_abort_intent,
721 .create_done = xfs_extent_free_create_done,
722 .finish_item = xfs_extent_free_finish_item,
723 .cancel_item = xfs_extent_free_cancel_item,
724 .recover_work = xfs_extent_free_recover_work,
725 .relog_intent = xfs_extent_free_relog_intent,
726};
727
728/* sub-type with special handling for AGFL deferred frees */
729const struct xfs_defer_op_type xfs_agfl_free_defer_type = {
730 .name = "agfl_free",
731 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
732 .create_intent = xfs_extent_free_create_intent,
733 .abort_intent = xfs_extent_free_abort_intent,
734 .create_done = xfs_extent_free_create_done,
735 .finish_item = xfs_agfl_free_finish_item,
736 .cancel_item = xfs_extent_free_cancel_item,
737 .recover_work = xfs_extent_free_recover_work,
738 .relog_intent = xfs_extent_free_relog_intent,
739};
740
741#ifdef CONFIG_XFS_RT
742/* Create a realtime extent freeing */
743static struct xfs_log_item *
744xfs_rtextent_free_create_intent(
745 struct xfs_trans *tp,
746 struct list_head *items,
747 unsigned int count,
748 bool sort)
749{
750 return __xfs_extent_free_create_intent(tp, items, count, sort,
751 XFS_LI_EFI_RT);
752}
753
754/* Process a free realtime extent. */
755STATIC int
756xfs_rtextent_free_finish_item(
757 struct xfs_trans *tp,
758 struct xfs_log_item *done,
759 struct list_head *item,
760 struct xfs_btree_cur **state)
761{
762 struct xfs_mount *mp = tp->t_mountp;
763 struct xfs_extent_free_item *xefi = xefi_entry(item);
764 struct xfs_efd_log_item *efdp = EFD_ITEM(done);
765 struct xfs_rtgroup **rtgp = (struct xfs_rtgroup **)state;
766 int error = 0;
767
768 trace_xfs_extent_free_deferred(mp, xefi);
769
770 if (!(xefi->xefi_flags & XFS_EFI_CANCELLED)) {
771 if (*rtgp != to_rtg(xefi->xefi_group)) {
772 *rtgp = to_rtg(xefi->xefi_group);
773 xfs_rtgroup_lock(*rtgp, XFS_RTGLOCK_BITMAP);
774 xfs_rtgroup_trans_join(tp, *rtgp,
775 XFS_RTGLOCK_BITMAP);
776 }
777 error = xfs_rtfree_blocks(tp, *rtgp,
778 xefi->xefi_startblock, xefi->xefi_blockcount);
779 }
780 if (error == -EAGAIN) {
781 xfs_efd_from_efi(efdp);
782 return error;
783 }
784
785 xfs_efd_add_extent(efdp, xefi);
786 xfs_extent_free_cancel_item(item);
787 return error;
788}
789
790const struct xfs_defer_op_type xfs_rtextent_free_defer_type = {
791 .name = "rtextent_free",
792 .max_items = XFS_EFI_MAX_FAST_EXTENTS,
793 .create_intent = xfs_rtextent_free_create_intent,
794 .abort_intent = xfs_extent_free_abort_intent,
795 .create_done = xfs_extent_free_create_done,
796 .finish_item = xfs_rtextent_free_finish_item,
797 .cancel_item = xfs_extent_free_cancel_item,
798 .recover_work = xfs_extent_free_recover_work,
799 .relog_intent = xfs_extent_free_relog_intent,
800};
801#else
802const struct xfs_defer_op_type xfs_rtextent_free_defer_type = {
803 .name = "rtextent_free",
804};
805#endif /* CONFIG_XFS_RT */
806
807STATIC bool
808xfs_efi_item_match(
809 struct xfs_log_item *lip,
810 uint64_t intent_id)
811{
812 return EFI_ITEM(lip)->efi_format.efi_id == intent_id;
813}
814
815static const struct xfs_item_ops xfs_efi_item_ops = {
816 .flags = XFS_ITEM_INTENT,
817 .iop_size = xfs_efi_item_size,
818 .iop_format = xfs_efi_item_format,
819 .iop_unpin = xfs_efi_item_unpin,
820 .iop_release = xfs_efi_item_release,
821 .iop_match = xfs_efi_item_match,
822};
823
824/*
825 * This routine is called to create an in-core extent free intent
826 * item from the efi format structure which was logged on disk.
827 * It allocates an in-core efi, copies the extents from the format
828 * structure into it, and adds the efi to the AIL with the given
829 * LSN.
830 */
831STATIC int
832xlog_recover_efi_commit_pass2(
833 struct xlog *log,
834 struct list_head *buffer_list,
835 struct xlog_recover_item *item,
836 xfs_lsn_t lsn)
837{
838 struct xfs_mount *mp = log->l_mp;
839 struct xfs_efi_log_item *efip;
840 struct xfs_efi_log_format *efi_formatp;
841 int error;
842
843 efi_formatp = item->ri_buf[0].i_addr;
844
845 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
846 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
847 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
848 return -EFSCORRUPTED;
849 }
850
851 efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
852 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
853 if (error) {
854 xfs_efi_item_free(efip);
855 return error;
856 }
857 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
858
859 xlog_recover_intent_item(log, &efip->efi_item, lsn,
860 &xfs_extent_free_defer_type);
861 return 0;
862}
863
864const struct xlog_recover_item_ops xlog_efi_item_ops = {
865 .item_type = XFS_LI_EFI,
866 .commit_pass2 = xlog_recover_efi_commit_pass2,
867};
868
869#ifdef CONFIG_XFS_RT
870STATIC int
871xlog_recover_rtefi_commit_pass2(
872 struct xlog *log,
873 struct list_head *buffer_list,
874 struct xlog_recover_item *item,
875 xfs_lsn_t lsn)
876{
877 struct xfs_mount *mp = log->l_mp;
878 struct xfs_efi_log_item *efip;
879 struct xfs_efi_log_format *efi_formatp;
880 int error;
881
882 efi_formatp = item->ri_buf[0].i_addr;
883
884 if (item->ri_buf[0].i_len < xfs_efi_log_format_sizeof(0)) {
885 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp,
886 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
887 return -EFSCORRUPTED;
888 }
889
890 efip = xfs_efi_init(mp, ITEM_TYPE(item), efi_formatp->efi_nextents);
891 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
892 if (error) {
893 xfs_efi_item_free(efip);
894 return error;
895 }
896 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
897
898 xlog_recover_intent_item(log, &efip->efi_item, lsn,
899 &xfs_rtextent_free_defer_type);
900 return 0;
901}
902#else
903STATIC int
904xlog_recover_rtefi_commit_pass2(
905 struct xlog *log,
906 struct list_head *buffer_list,
907 struct xlog_recover_item *item,
908 xfs_lsn_t lsn)
909{
910 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
911 item->ri_buf[0].i_addr, item->ri_buf[0].i_len);
912 return -EFSCORRUPTED;
913}
914#endif
915
916const struct xlog_recover_item_ops xlog_rtefi_item_ops = {
917 .item_type = XFS_LI_EFI_RT,
918 .commit_pass2 = xlog_recover_rtefi_commit_pass2,
919};
920
921/*
922 * This routine is called when an EFD format structure is found in a committed
923 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
924 * was still in the log. To do this it searches the AIL for the EFI with an id
925 * equal to that in the EFD format structure. If we find it we drop the EFD
926 * reference, which removes the EFI from the AIL and frees it.
927 */
928STATIC int
929xlog_recover_efd_commit_pass2(
930 struct xlog *log,
931 struct list_head *buffer_list,
932 struct xlog_recover_item *item,
933 xfs_lsn_t lsn)
934{
935 struct xfs_efd_log_format *efd_formatp;
936 int buflen = item->ri_buf[0].i_len;
937
938 efd_formatp = item->ri_buf[0].i_addr;
939
940 if (buflen < sizeof(struct xfs_efd_log_format)) {
941 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
942 efd_formatp, buflen);
943 return -EFSCORRUPTED;
944 }
945
946 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
947 efd_formatp->efd_nextents) &&
948 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
949 efd_formatp->efd_nextents)) {
950 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
951 efd_formatp, buflen);
952 return -EFSCORRUPTED;
953 }
954
955 xlog_recover_release_intent(log, XFS_LI_EFI, efd_formatp->efd_efi_id);
956 return 0;
957}
958
959const struct xlog_recover_item_ops xlog_efd_item_ops = {
960 .item_type = XFS_LI_EFD,
961 .commit_pass2 = xlog_recover_efd_commit_pass2,
962};
963
964#ifdef CONFIG_XFS_RT
965STATIC int
966xlog_recover_rtefd_commit_pass2(
967 struct xlog *log,
968 struct list_head *buffer_list,
969 struct xlog_recover_item *item,
970 xfs_lsn_t lsn)
971{
972 struct xfs_efd_log_format *efd_formatp;
973 int buflen = item->ri_buf[0].i_len;
974
975 efd_formatp = item->ri_buf[0].i_addr;
976
977 if (buflen < sizeof(struct xfs_efd_log_format)) {
978 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
979 efd_formatp, buflen);
980 return -EFSCORRUPTED;
981 }
982
983 if (item->ri_buf[0].i_len != xfs_efd_log_format32_sizeof(
984 efd_formatp->efd_nextents) &&
985 item->ri_buf[0].i_len != xfs_efd_log_format64_sizeof(
986 efd_formatp->efd_nextents)) {
987 XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, log->l_mp,
988 efd_formatp, buflen);
989 return -EFSCORRUPTED;
990 }
991
992 xlog_recover_release_intent(log, XFS_LI_EFI_RT,
993 efd_formatp->efd_efi_id);
994 return 0;
995}
996#else
997# define xlog_recover_rtefd_commit_pass2 xlog_recover_rtefi_commit_pass2
998#endif
999
1000const struct xlog_recover_item_ops xlog_rtefd_item_ops = {
1001 .item_type = XFS_LI_EFD_RT,
1002 .commit_pass2 = xlog_recover_rtefd_commit_pass2,
1003};