blob: 44f8c54512102577dbe768e5f137298ba1ee79c7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Christoph Hellwig4fb6e8a2014-11-28 14:25:04 +110020#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
Darrick J. Wongdc423752016-08-03 11:23:49 +100023#include "xfs_bit.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
Dave Chinner239880e2013-10-23 10:50:10 +110025#include "xfs_trans.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_trans_priv.h"
Dave Chinner239880e2013-10-23 10:50:10 +110027#include "xfs_buf_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_extfree_item.h"
Christoph Hellwig12343512013-12-13 11:00:43 +110029#include "xfs_log.h"
Darrick J. Wong340785c2016-08-03 11:33:42 +100030#include "xfs_btree.h"
31#include "xfs_rmap.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33
34kmem_zone_t *xfs_efi_zone;
35kmem_zone_t *xfs_efd_zone;
36
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100037static inline struct xfs_efi_log_item *EFI_ITEM(struct xfs_log_item *lip)
38{
39 return container_of(lip, struct xfs_efi_log_item, efi_item);
40}
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100042void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100043xfs_efi_item_free(
44 struct xfs_efi_log_item *efip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100045{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100046 kmem_free(efip->efi_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100047 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +100048 kmem_free(efip);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100049 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100050 kmem_zone_free(xfs_efi_zone, efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100051}
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53/*
54 * This returns the number of iovecs needed to log the given efi item.
55 * We only need 1 iovec for an efi item. It just logs the efi_log_format
56 * structure.
57 */
Dave Chinner166d1362013-08-12 20:50:04 +100058static inline int
59xfs_efi_item_sizeof(
60 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Dave Chinner166d1362013-08-12 20:50:04 +100062 return sizeof(struct xfs_efi_log_format) +
63 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
64}
65
66STATIC void
67xfs_efi_item_size(
68 struct xfs_log_item *lip,
69 int *nvecs,
70 int *nbytes)
71{
72 *nvecs += 1;
73 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
75
76/*
77 * This is called to fill in the vector of log iovecs for the
78 * given efi log item. We use only 1 iovec, and we point that
79 * at the efi_log_format structure embedded in the efi item.
80 * It is at this point that we assert that all of the extent
81 * slots in the efi item have been filled.
82 */
83STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100084xfs_efi_item_format(
85 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110086 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070087{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100088 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110089 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Dave Chinnerb199c8a2010-12-20 11:59:49 +110091 ASSERT(atomic_read(&efip->efi_next_extent) ==
92 efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 efip->efi_format.efi_type = XFS_LI_EFI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 efip->efi_format.efi_size = 1;
96
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110097 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +110098 &efip->efi_format,
99 xfs_efi_item_sizeof(efip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102
103/*
104 * Pinning has no meaning for an efi item, so just return.
105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000107xfs_efi_item_pin(
108 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000113 * The unpin operation is the last place an EFI is manipulated in the log. It is
114 * either inserted in the AIL or aborted in the event of a log I/O error. In
115 * either case, the EFI transaction has been successfully committed to make it
116 * this far. Therefore, we expect whoever committed the EFI to either construct
117 * and commit the EFD or drop the EFD's reference in the event of error. Simply
118 * drop the log's EFI reference now that the log is done with it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000121xfs_efi_item_unpin(
122 struct xfs_log_item *lip,
123 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000125 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Brian Foster5e4b5382015-08-19 09:50:12 +1000126 xfs_efi_release(efip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127}
128
129/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000130 * Efi items have no locking or pushing. However, since EFIs are pulled from
131 * the AIL when their corresponding EFDs are committed to disk, their situation
132 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
133 * will eventually flush the log. This should help in getting the EFI out of
134 * the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000137xfs_efi_item_push(
138 struct xfs_log_item *lip,
139 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 return XFS_ITEM_PINNED;
142}
143
Brian Foster8d99fe92015-08-19 09:51:16 +1000144/*
145 * The EFI has been either committed or aborted if the transaction has been
146 * cancelled. If the transaction was cancelled, an EFD isn't going to be
147 * constructed and thus we free the EFI here directly.
148 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000150xfs_efi_item_unlock(
151 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000153 if (lip->li_flags & XFS_LI_ABORTED)
154 xfs_efi_item_free(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100158 * The EFI is logged only once and cannot be moved in the log, so simply return
Dave Chinner666d6442013-04-03 14:09:21 +1100159 * the lsn at which it's been logged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161STATIC xfs_lsn_t
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000162xfs_efi_item_committed(
163 struct xfs_log_item *lip,
164 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 return lsn;
167}
168
169/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 * The EFI dependency tracking op doesn't do squat. It can't because
171 * it doesn't know where the free extent is coming from. The dependency
172 * tracking has to be handled by the "enclosing" metadata object. For
173 * example, for inodes, the inode is locked throughout the extent freeing
174 * so the dependency should be recorded there.
175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000177xfs_efi_item_committing(
178 struct xfs_log_item *lip,
179 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
183/*
184 * This is the ops vector shared by all efi log items.
185 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000186static const struct xfs_item_ops xfs_efi_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000187 .iop_size = xfs_efi_item_size,
188 .iop_format = xfs_efi_item_format,
189 .iop_pin = xfs_efi_item_pin,
190 .iop_unpin = xfs_efi_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000191 .iop_unlock = xfs_efi_item_unlock,
192 .iop_committed = xfs_efi_item_committed,
193 .iop_push = xfs_efi_item_push,
194 .iop_committing = xfs_efi_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195};
196
197
198/*
199 * Allocate and initialize an efi item with the given number of extents.
200 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000201struct xfs_efi_log_item *
202xfs_efi_init(
203 struct xfs_mount *mp,
204 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000207 struct xfs_efi_log_item *efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 uint size;
209
210 ASSERT(nextents > 0);
211 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
212 size = (uint)(sizeof(xfs_efi_log_item_t) +
213 ((nextents - 1) * sizeof(xfs_extent_t)));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000214 efip = kmem_zalloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 } else {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000216 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
Dave Chinner43f5efc2010-03-23 10:10:00 +1100219 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 efip->efi_format.efi_nextents = nextents;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000221 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100222 atomic_set(&efip->efi_next_extent, 0);
Dave Chinner666d6442013-04-03 14:09:21 +1100223 atomic_set(&efip->efi_refcount, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000225 return efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228/*
Tim Shimmin6d192a92006-06-09 14:55:38 +1000229 * Copy an EFI format buffer from the given buf, and into the destination
230 * EFI format structure.
231 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
232 * one of which will be the native format for this kernel.
233 * It will handle the conversion of formats if necessary.
234 */
235int
236xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
237{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000238 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000239 uint i;
240 uint len = sizeof(xfs_efi_log_format_t) +
241 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
242 uint len32 = sizeof(xfs_efi_log_format_32_t) +
243 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
244 uint len64 = sizeof(xfs_efi_log_format_64_t) +
245 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
246
247 if (buf->i_len == len) {
248 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
249 return 0;
250 } else if (buf->i_len == len32) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000251 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000252
253 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
254 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
255 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
256 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
257 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
258 dst_efi_fmt->efi_extents[i].ext_start =
259 src_efi_fmt_32->efi_extents[i].ext_start;
260 dst_efi_fmt->efi_extents[i].ext_len =
261 src_efi_fmt_32->efi_extents[i].ext_len;
262 }
263 return 0;
264 } else if (buf->i_len == len64) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000265 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000266
267 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
268 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
269 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
270 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
271 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
272 dst_efi_fmt->efi_extents[i].ext_start =
273 src_efi_fmt_64->efi_extents[i].ext_start;
274 dst_efi_fmt->efi_extents[i].ext_len =
275 src_efi_fmt_64->efi_extents[i].ext_len;
276 }
277 return 0;
278 }
Dave Chinner24513372014-06-25 14:58:08 +1000279 return -EFSCORRUPTED;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000280}
281
282/*
Brian Fostere32a1d12015-08-19 09:52:21 +1000283 * Freeing the efi requires that we remove it from the AIL if it has already
284 * been placed there. However, the EFI may not yet have been placed in the AIL
285 * when called by xfs_efi_release() from EFD processing due to the ordering of
286 * committed vs unpin operations in bulk insert operations. Hence the reference
287 * count to ensure only the last caller frees the EFI.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289void
Brian Foster5e4b5382015-08-19 09:50:12 +1000290xfs_efi_release(
291 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
Darrick J. Wongc4cf1ac2017-04-20 15:09:05 -0700293 ASSERT(atomic_read(&efip->efi_refcount) > 0);
Brian Fostere32a1d12015-08-19 09:52:21 +1000294 if (atomic_dec_and_test(&efip->efi_refcount)) {
Brian Foster146e54b2015-08-19 10:01:08 +1000295 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
Brian Fostere32a1d12015-08-19 09:52:21 +1000296 xfs_efi_item_free(efip);
297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298}
299
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000300static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000301{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000302 return container_of(lip, struct xfs_efd_log_item, efd_item);
303}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000305STATIC void
306xfs_efd_item_free(struct xfs_efd_log_item *efdp)
307{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +1000308 kmem_free(efdp->efd_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000309 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000310 kmem_free(efdp);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000311 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000312 kmem_zone_free(xfs_efd_zone, efdp);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000313}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315/*
316 * This returns the number of iovecs needed to log the given efd item.
317 * We only need 1 iovec for an efd item. It just logs the efd_log_format
318 * structure.
319 */
Dave Chinner166d1362013-08-12 20:50:04 +1000320static inline int
321xfs_efd_item_sizeof(
322 struct xfs_efd_log_item *efdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
Dave Chinner166d1362013-08-12 20:50:04 +1000324 return sizeof(xfs_efd_log_format_t) +
325 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
326}
327
328STATIC void
329xfs_efd_item_size(
330 struct xfs_log_item *lip,
331 int *nvecs,
332 int *nbytes)
333{
334 *nvecs += 1;
335 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
338/*
339 * This is called to fill in the vector of log iovecs for the
340 * given efd log item. We use only 1 iovec, and we point that
341 * at the efd_log_format structure embedded in the efd item.
342 * It is at this point that we assert that all of the extent
343 * slots in the efd item have been filled.
344 */
345STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000346xfs_efd_item_format(
347 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100348 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000350 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100351 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
353 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
354
355 efdp->efd_format.efd_type = XFS_LI_EFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 efdp->efd_format.efd_size = 1;
357
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100358 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100359 &efdp->efd_format,
360 xfs_efd_item_sizeof(efdp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363/*
364 * Pinning has no meaning for an efd item, so just return.
365 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000367xfs_efd_item_pin(
368 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372/*
373 * Since pinning has no meaning for an efd item, unpinning does
374 * not either.
375 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000377xfs_efd_item_unpin(
378 struct xfs_log_item *lip,
379 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
383/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000384 * There isn't much you can do to push on an efd item. It is simply stuck
385 * waiting for the log to be flushed to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000388xfs_efd_item_push(
389 struct xfs_log_item *lip,
390 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000392 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Brian Foster8d99fe92015-08-19 09:51:16 +1000395/*
396 * The EFD is either committed or aborted if the transaction is cancelled. If
397 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
398 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000400xfs_efd_item_unlock(
401 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
Brian Foster8d99fe92015-08-19 09:51:16 +1000403 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
404
405 if (lip->li_flags & XFS_LI_ABORTED) {
406 xfs_efi_release(efdp->efd_efip);
407 xfs_efd_item_free(efdp);
408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409}
410
411/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000412 * When the efd item is committed to disk, all we need to do is delete our
413 * reference to our partner efi item and then free ourselves. Since we're
414 * freeing ourselves we must return -1 to keep the transaction code from further
415 * referencing this item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417STATIC xfs_lsn_t
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000418xfs_efd_item_committed(
419 struct xfs_log_item *lip,
420 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000422 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
Brian Foster8d99fe92015-08-19 09:51:16 +1000425 * Drop the EFI reference regardless of whether the EFD has been
426 * aborted. Once the EFD transaction is constructed, it is the sole
427 * responsibility of the EFD to release the EFI (even if the EFI is
428 * aborted due to log I/O error).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
Brian Foster8d99fe92015-08-19 09:51:16 +1000430 xfs_efi_release(efdp->efd_efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000431 xfs_efd_item_free(efdp);
Brian Foster8d99fe92015-08-19 09:51:16 +1000432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return (xfs_lsn_t)-1;
434}
435
436/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * The EFD dependency tracking op doesn't do squat. It can't because
438 * it doesn't know where the free extent is coming from. The dependency
439 * tracking has to be handled by the "enclosing" metadata object. For
440 * example, for inodes, the inode is locked throughout the extent freeing
441 * so the dependency should be recorded there.
442 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000444xfs_efd_item_committing(
445 struct xfs_log_item *lip,
446 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450/*
451 * This is the ops vector shared by all efd log items.
452 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000453static const struct xfs_item_ops xfs_efd_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000454 .iop_size = xfs_efd_item_size,
455 .iop_format = xfs_efd_item_format,
456 .iop_pin = xfs_efd_item_pin,
457 .iop_unpin = xfs_efd_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000458 .iop_unlock = xfs_efd_item_unlock,
459 .iop_committed = xfs_efd_item_committed,
460 .iop_push = xfs_efd_item_push,
461 .iop_committing = xfs_efd_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462};
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464/*
465 * Allocate and initialize an efd item with the given number of extents.
466 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000467struct xfs_efd_log_item *
468xfs_efd_init(
469 struct xfs_mount *mp,
470 struct xfs_efi_log_item *efip,
471 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000474 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 uint size;
476
477 ASSERT(nextents > 0);
478 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
479 size = (uint)(sizeof(xfs_efd_log_item_t) +
480 ((nextents - 1) * sizeof(xfs_extent_t)));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000481 efdp = kmem_zalloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 } else {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000483 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Dave Chinner43f5efc2010-03-23 10:10:00 +1100486 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 efdp->efd_efip = efip;
488 efdp->efd_format.efd_nextents = nextents;
489 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
490
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000491 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
Darrick J. Wongdc423752016-08-03 11:23:49 +1000493
494/*
495 * Process an extent free intent item that was recovered from
496 * the log. We need to free the extents that it describes.
497 */
498int
499xfs_efi_recover(
500 struct xfs_mount *mp,
501 struct xfs_efi_log_item *efip)
502{
503 struct xfs_efd_log_item *efdp;
504 struct xfs_trans *tp;
505 int i;
506 int error = 0;
507 xfs_extent_t *extp;
508 xfs_fsblock_t startblock_fsb;
Darrick J. Wong340785c2016-08-03 11:33:42 +1000509 struct xfs_owner_info oinfo;
Darrick J. Wongdc423752016-08-03 11:23:49 +1000510
511 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
512
513 /*
514 * First check the validity of the extents described by the
515 * EFI. If any are bad, then assume that all are bad and
516 * just toss the EFI.
517 */
518 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000519 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000520 startblock_fsb = XFS_BB_TO_FSB(mp,
521 XFS_FSB_TO_DADDR(mp, extp->ext_start));
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000522 if (startblock_fsb == 0 ||
523 extp->ext_len == 0 ||
524 startblock_fsb >= mp->m_sb.sb_dblocks ||
525 extp->ext_len >= mp->m_sb.sb_agblocks) {
Darrick J. Wongdc423752016-08-03 11:23:49 +1000526 /*
527 * This will pull the EFI from the AIL and
528 * free the memory associated with it.
529 */
530 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
531 xfs_efi_release(efip);
532 return -EIO;
533 }
534 }
535
536 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
537 if (error)
538 return error;
539 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
540
Darrick J. Wong340785c2016-08-03 11:33:42 +1000541 xfs_rmap_skip_owner_update(&oinfo);
Darrick J. Wongdc423752016-08-03 11:23:49 +1000542 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
Darrick J. Wonge127faf2016-08-03 12:29:32 +1000543 extp = &efip->efi_format.efi_extents[i];
Darrick J. Wongdc423752016-08-03 11:23:49 +1000544 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
Darrick J. Wong340785c2016-08-03 11:33:42 +1000545 extp->ext_len, &oinfo);
Darrick J. Wongdc423752016-08-03 11:23:49 +1000546 if (error)
547 goto abort_error;
548
549 }
550
551 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
552 error = xfs_trans_commit(tp);
553 return error;
554
555abort_error:
556 xfs_trans_cancel(tp);
557 return error;
558}