blob: 8c49f883934a07dc75eba2eb89dd70c710464b30 [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"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31
32kmem_zone_t *xfs_efi_zone;
33kmem_zone_t *xfs_efd_zone;
34
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100035static 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}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100040void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100041xfs_efi_item_free(
42 struct xfs_efi_log_item *efip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100043{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +100044 kmem_free(efip->efi_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100045 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +100046 kmem_free(efip);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100047 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100048 kmem_zone_free(xfs_efi_zone, efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +100049}
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/*
52 * This returns the number of iovecs needed to log the given efi item.
53 * We only need 1 iovec for an efi item. It just logs the efi_log_format
54 * structure.
55 */
Dave Chinner166d1362013-08-12 20:50:04 +100056static inline int
57xfs_efi_item_sizeof(
58 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Dave Chinner166d1362013-08-12 20:50:04 +100060 return sizeof(struct xfs_efi_log_format) +
61 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
62}
63
64STATIC void
65xfs_efi_item_size(
66 struct xfs_log_item *lip,
67 int *nvecs,
68 int *nbytes)
69{
70 *nvecs += 1;
71 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070072}
73
74/*
75 * This is called to fill in the vector of log iovecs for the
76 * given efi log item. We use only 1 iovec, and we point that
77 * at the efi_log_format structure embedded in the efi item.
78 * It is at this point that we assert that all of the extent
79 * slots in the efi item have been filled.
80 */
81STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100082xfs_efi_item_format(
83 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110084 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +100086 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110087 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Dave Chinnerb199c8a2010-12-20 11:59:49 +110089 ASSERT(atomic_read(&efip->efi_next_extent) ==
90 efip->efi_format.efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 efip->efi_format.efi_type = XFS_LI_EFI;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 efip->efi_format.efi_size = 1;
94
Christoph Hellwigbde7cff2013-12-13 11:34:02 +110095 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +110096 &efip->efi_format,
97 xfs_efi_item_sizeof(efip));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100
101/*
102 * Pinning has no meaning for an efi item, so just return.
103 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000105xfs_efi_item_pin(
106 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000111 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000119xfs_efi_item_unpin(
120 struct xfs_log_item *lip,
121 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000123 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
Brian Foster5e4b5382015-08-19 09:50:12 +1000124 xfs_efi_release(efip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000128 * Efi items have no locking or pushing. However, since EFIs are pulled from
129 * the AIL when their corresponding EFDs are committed to disk, their situation
130 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
131 * will eventually flush the log. This should help in getting the EFI out of
132 * the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000135xfs_efi_item_push(
136 struct xfs_log_item *lip,
137 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 return XFS_ITEM_PINNED;
140}
141
Brian Foster8d99fe92015-08-19 09:51:16 +1000142/*
143 * The EFI has been either committed or aborted if the transaction has been
144 * cancelled. If the transaction was cancelled, an EFD isn't going to be
145 * constructed and thus we free the EFI here directly.
146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000148xfs_efi_item_unlock(
149 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000151 if (lip->li_flags & XFS_LI_ABORTED)
152 xfs_efi_item_free(EFI_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100156 * The EFI is logged only once and cannot be moved in the log, so simply return
Dave Chinner666d6442013-04-03 14:09:21 +1100157 * the lsn at which it's been logged.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159STATIC xfs_lsn_t
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000160xfs_efi_item_committed(
161 struct xfs_log_item *lip,
162 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 return lsn;
165}
166
167/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 * The EFI dependency tracking op doesn't do squat. It can't because
169 * it doesn't know where the free extent is coming from. The dependency
170 * tracking has to be handled by the "enclosing" metadata object. For
171 * example, for inodes, the inode is locked throughout the extent freeing
172 * so the dependency should be recorded there.
173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000175xfs_efi_item_committing(
176 struct xfs_log_item *lip,
177 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
181/*
182 * This is the ops vector shared by all efi log items.
183 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000184static const struct xfs_item_ops xfs_efi_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000185 .iop_size = xfs_efi_item_size,
186 .iop_format = xfs_efi_item_format,
187 .iop_pin = xfs_efi_item_pin,
188 .iop_unpin = xfs_efi_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000189 .iop_unlock = xfs_efi_item_unlock,
190 .iop_committed = xfs_efi_item_committed,
191 .iop_push = xfs_efi_item_push,
192 .iop_committing = xfs_efi_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
195
196/*
197 * Allocate and initialize an efi item with the given number of extents.
198 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000199struct xfs_efi_log_item *
200xfs_efi_init(
201 struct xfs_mount *mp,
202 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000205 struct xfs_efi_log_item *efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 uint size;
207
208 ASSERT(nextents > 0);
209 if (nextents > XFS_EFI_MAX_FAST_EXTENTS) {
210 size = (uint)(sizeof(xfs_efi_log_item_t) +
211 ((nextents - 1) * sizeof(xfs_extent_t)));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000212 efip = kmem_zalloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 } else {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000214 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Dave Chinner43f5efc2010-03-23 10:10:00 +1100217 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 efip->efi_format.efi_nextents = nextents;
Christoph Hellwigdb9d67d2015-06-22 09:43:32 +1000219 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +1100220 atomic_set(&efip->efi_next_extent, 0);
Dave Chinner666d6442013-04-03 14:09:21 +1100221 atomic_set(&efip->efi_refcount, 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000223 return efip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/*
Tim Shimmin6d192a92006-06-09 14:55:38 +1000227 * Copy an EFI format buffer from the given buf, and into the destination
228 * EFI format structure.
229 * The given buffer can be in 32 bit or 64 bit form (which has different padding),
230 * one of which will be the native format for this kernel.
231 * It will handle the conversion of formats if necessary.
232 */
233int
234xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
235{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000236 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000237 uint i;
238 uint len = sizeof(xfs_efi_log_format_t) +
239 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_t);
240 uint len32 = sizeof(xfs_efi_log_format_32_t) +
241 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_32_t);
242 uint len64 = sizeof(xfs_efi_log_format_64_t) +
243 (src_efi_fmt->efi_nextents - 1) * sizeof(xfs_extent_64_t);
244
245 if (buf->i_len == len) {
246 memcpy((char *)dst_efi_fmt, (char*)src_efi_fmt, len);
247 return 0;
248 } else if (buf->i_len == len32) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000249 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000250
251 dst_efi_fmt->efi_type = src_efi_fmt_32->efi_type;
252 dst_efi_fmt->efi_size = src_efi_fmt_32->efi_size;
253 dst_efi_fmt->efi_nextents = src_efi_fmt_32->efi_nextents;
254 dst_efi_fmt->efi_id = src_efi_fmt_32->efi_id;
255 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
256 dst_efi_fmt->efi_extents[i].ext_start =
257 src_efi_fmt_32->efi_extents[i].ext_start;
258 dst_efi_fmt->efi_extents[i].ext_len =
259 src_efi_fmt_32->efi_extents[i].ext_len;
260 }
261 return 0;
262 } else if (buf->i_len == len64) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +1000263 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000264
265 dst_efi_fmt->efi_type = src_efi_fmt_64->efi_type;
266 dst_efi_fmt->efi_size = src_efi_fmt_64->efi_size;
267 dst_efi_fmt->efi_nextents = src_efi_fmt_64->efi_nextents;
268 dst_efi_fmt->efi_id = src_efi_fmt_64->efi_id;
269 for (i = 0; i < dst_efi_fmt->efi_nextents; i++) {
270 dst_efi_fmt->efi_extents[i].ext_start =
271 src_efi_fmt_64->efi_extents[i].ext_start;
272 dst_efi_fmt->efi_extents[i].ext_len =
273 src_efi_fmt_64->efi_extents[i].ext_len;
274 }
275 return 0;
276 }
Dave Chinner24513372014-06-25 14:58:08 +1000277 return -EFSCORRUPTED;
Tim Shimmin6d192a92006-06-09 14:55:38 +1000278}
279
280/*
Brian Fostere32a1d12015-08-19 09:52:21 +1000281 * Freeing the efi requires that we remove it from the AIL if it has already
282 * been placed there. However, the EFI may not yet have been placed in the AIL
283 * when called by xfs_efi_release() from EFD processing due to the ordering of
284 * committed vs unpin operations in bulk insert operations. Hence the reference
285 * count to ensure only the last caller frees the EFI.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 */
287void
Brian Foster5e4b5382015-08-19 09:50:12 +1000288xfs_efi_release(
289 struct xfs_efi_log_item *efip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
Brian Fostere32a1d12015-08-19 09:52:21 +1000291 if (atomic_dec_and_test(&efip->efi_refcount)) {
Brian Foster146e54b2015-08-19 10:01:08 +1000292 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
Brian Fostere32a1d12015-08-19 09:52:21 +1000293 xfs_efi_item_free(efip);
294 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000297static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000298{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000299 return container_of(lip, struct xfs_efd_log_item, efd_item);
300}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000302STATIC void
303xfs_efd_item_free(struct xfs_efd_log_item *efdp)
304{
Dave Chinnerb1c5ebb2016-07-22 09:52:35 +1000305 kmem_free(efdp->efd_item.li_lv_shadow);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000306 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +1000307 kmem_free(efdp);
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000308 else
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000309 kmem_zone_free(xfs_efd_zone, efdp);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000310}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312/*
313 * This returns the number of iovecs needed to log the given efd item.
314 * We only need 1 iovec for an efd item. It just logs the efd_log_format
315 * structure.
316 */
Dave Chinner166d1362013-08-12 20:50:04 +1000317static inline int
318xfs_efd_item_sizeof(
319 struct xfs_efd_log_item *efdp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320{
Dave Chinner166d1362013-08-12 20:50:04 +1000321 return sizeof(xfs_efd_log_format_t) +
322 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
323}
324
325STATIC void
326xfs_efd_item_size(
327 struct xfs_log_item *lip,
328 int *nvecs,
329 int *nbytes)
330{
331 *nvecs += 1;
332 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335/*
336 * This is called to fill in the vector of log iovecs for the
337 * given efd log item. We use only 1 iovec, and we point that
338 * at the efd_log_format structure embedded in the efd item.
339 * It is at this point that we assert that all of the extent
340 * slots in the efd item have been filled.
341 */
342STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000343xfs_efd_item_format(
344 struct xfs_log_item *lip,
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100345 struct xfs_log_vec *lv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000347 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100348 struct xfs_log_iovec *vecp = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
351
352 efdp->efd_format.efd_type = XFS_LI_EFD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 efdp->efd_format.efd_size = 1;
354
Christoph Hellwigbde7cff2013-12-13 11:34:02 +1100355 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
Christoph Hellwig12343512013-12-13 11:00:43 +1100356 &efdp->efd_format,
357 xfs_efd_item_sizeof(efdp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * Pinning has no meaning for an efd item, so just return.
362 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000364xfs_efd_item_pin(
365 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*
370 * Since pinning has no meaning for an efd item, unpinning does
371 * not either.
372 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000374xfs_efd_item_unpin(
375 struct xfs_log_item *lip,
376 int remove)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378}
379
380/*
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000381 * There isn't much you can do to push on an efd item. It is simply stuck
382 * waiting for the log to be flushed to disk.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384STATIC uint
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000385xfs_efd_item_push(
386 struct xfs_log_item *lip,
387 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388{
Christoph Hellwig43ff2122012-04-23 15:58:39 +1000389 return XFS_ITEM_PINNED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
Brian Foster8d99fe92015-08-19 09:51:16 +1000392/*
393 * The EFD is either committed or aborted if the transaction is cancelled. If
394 * the transaction is cancelled, drop our reference to the EFI and free the EFD.
395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000397xfs_efd_item_unlock(
398 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Brian Foster8d99fe92015-08-19 09:51:16 +1000400 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
401
402 if (lip->li_flags & XFS_LI_ABORTED) {
403 xfs_efi_release(efdp->efd_efip);
404 xfs_efd_item_free(efdp);
405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
408/*
Brian Foster8d99fe92015-08-19 09:51:16 +1000409 * When the efd item is committed to disk, all we need to do is delete our
410 * reference to our partner efi item and then free ourselves. Since we're
411 * freeing ourselves we must return -1 to keep the transaction code from further
412 * referencing this item.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414STATIC xfs_lsn_t
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000415xfs_efd_item_committed(
416 struct xfs_log_item *lip,
417 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000419 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 /*
Brian Foster8d99fe92015-08-19 09:51:16 +1000422 * Drop the EFI reference regardless of whether the EFD has been
423 * aborted. Once the EFD transaction is constructed, it is the sole
424 * responsibility of the EFD to release the EFI (even if the EFI is
425 * aborted due to log I/O error).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Brian Foster8d99fe92015-08-19 09:51:16 +1000427 xfs_efi_release(efdp->efd_efip);
Christoph Hellwig7d795ca2005-06-21 15:41:19 +1000428 xfs_efd_item_free(efdp);
Brian Foster8d99fe92015-08-19 09:51:16 +1000429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return (xfs_lsn_t)-1;
431}
432
433/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 * The EFD dependency tracking op doesn't do squat. It can't because
435 * it doesn't know where the free extent is coming from. The dependency
436 * tracking has to be handled by the "enclosing" metadata object. For
437 * example, for inodes, the inode is locked throughout the extent freeing
438 * so the dependency should be recorded there.
439 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440STATIC void
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000441xfs_efd_item_committing(
442 struct xfs_log_item *lip,
443 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * This is the ops vector shared by all efd log items.
449 */
Christoph Hellwig272e42b2011-10-28 09:54:24 +0000450static const struct xfs_item_ops xfs_efd_item_ops = {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000451 .iop_size = xfs_efd_item_size,
452 .iop_format = xfs_efd_item_format,
453 .iop_pin = xfs_efd_item_pin,
454 .iop_unpin = xfs_efd_item_unpin,
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000455 .iop_unlock = xfs_efd_item_unlock,
456 .iop_committed = xfs_efd_item_committed,
457 .iop_push = xfs_efd_item_push,
458 .iop_committing = xfs_efd_item_committing
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459};
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461/*
462 * Allocate and initialize an efd item with the given number of extents.
463 */
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000464struct xfs_efd_log_item *
465xfs_efd_init(
466 struct xfs_mount *mp,
467 struct xfs_efi_log_item *efip,
468 uint nextents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470{
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000471 struct xfs_efd_log_item *efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 uint size;
473
474 ASSERT(nextents > 0);
475 if (nextents > XFS_EFD_MAX_FAST_EXTENTS) {
476 size = (uint)(sizeof(xfs_efd_log_item_t) +
477 ((nextents - 1) * sizeof(xfs_extent_t)));
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000478 efdp = kmem_zalloc(size, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 } else {
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000480 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
482
Dave Chinner43f5efc2010-03-23 10:10:00 +1100483 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 efdp->efd_efip = efip;
485 efdp->efd_format.efd_nextents = nextents;
486 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
487
Christoph Hellwig7bfa31d2010-06-23 18:11:15 +1000488 return efdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Darrick J. Wongdc423752016-08-03 11:23:49 +1000490
491/*
492 * Process an extent free intent item that was recovered from
493 * the log. We need to free the extents that it describes.
494 */
495int
496xfs_efi_recover(
497 struct xfs_mount *mp,
498 struct xfs_efi_log_item *efip)
499{
500 struct xfs_efd_log_item *efdp;
501 struct xfs_trans *tp;
502 int i;
503 int error = 0;
504 xfs_extent_t *extp;
505 xfs_fsblock_t startblock_fsb;
506
507 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
508
509 /*
510 * First check the validity of the extents described by the
511 * EFI. If any are bad, then assume that all are bad and
512 * just toss the EFI.
513 */
514 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
515 extp = &(efip->efi_format.efi_extents[i]);
516 startblock_fsb = XFS_BB_TO_FSB(mp,
517 XFS_FSB_TO_DADDR(mp, extp->ext_start));
518 if ((startblock_fsb == 0) ||
519 (extp->ext_len == 0) ||
520 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
521 (extp->ext_len >= mp->m_sb.sb_agblocks)) {
522 /*
523 * This will pull the EFI from the AIL and
524 * free the memory associated with it.
525 */
526 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
527 xfs_efi_release(efip);
528 return -EIO;
529 }
530 }
531
532 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
533 if (error)
534 return error;
535 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
536
537 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
538 extp = &(efip->efi_format.efi_extents[i]);
539 error = xfs_trans_free_extent(tp, efdp, extp->ext_start,
540 extp->ext_len);
541 if (error)
542 goto abort_error;
543
544 }
545
546 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
547 error = xfs_trans_commit(tp);
548 return error;
549
550abort_error:
551 xfs_trans_cancel(tp);
552 return error;
553}