]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/xfs/xfs_extfree_item.c
xfs: refactor redo intent item processing
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_extfree_item.c
CommitLineData
1da177e4 1/*
7b718769
NS
2 * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
1da177e4 4 *
7b718769
NS
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
1da177e4
LT
7 * published by the Free Software Foundation.
8 *
7b718769
NS
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.
1da177e4 13 *
7b718769
NS
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
1da177e4 17 */
1da177e4 18#include "xfs.h"
a844f451 19#include "xfs_fs.h"
4fb6e8ad 20#include "xfs_format.h"
239880ef
DC
21#include "xfs_log_format.h"
22#include "xfs_trans_resv.h"
dc42375d 23#include "xfs_bit.h"
1da177e4 24#include "xfs_mount.h"
239880ef 25#include "xfs_trans.h"
1da177e4 26#include "xfs_trans_priv.h"
239880ef 27#include "xfs_buf_item.h"
1da177e4 28#include "xfs_extfree_item.h"
1234351c 29#include "xfs_log.h"
1da177e4
LT
30
31
32kmem_zone_t *xfs_efi_zone;
33kmem_zone_t *xfs_efd_zone;
34
7bfa31d8
CH
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}
1da177e4 39
7d795ca3 40void
7bfa31d8
CH
41xfs_efi_item_free(
42 struct xfs_efi_log_item *efip)
7d795ca3 43{
b1c5ebb2 44 kmem_free(efip->efi_item.li_lv_shadow);
7bfa31d8 45 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
f0e2d93c 46 kmem_free(efip);
7bfa31d8 47 else
7d795ca3 48 kmem_zone_free(xfs_efi_zone, efip);
7d795ca3 49}
1da177e4
LT
50
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 */
166d1368
DC
56static inline int
57xfs_efi_item_sizeof(
58 struct xfs_efi_log_item *efip)
59{
60 return sizeof(struct xfs_efi_log_format) +
61 (efip->efi_format.efi_nextents - 1) * sizeof(xfs_extent_t);
62}
63
64STATIC void
7bfa31d8 65xfs_efi_item_size(
166d1368
DC
66 struct xfs_log_item *lip,
67 int *nvecs,
68 int *nbytes)
1da177e4 69{
166d1368
DC
70 *nvecs += 1;
71 *nbytes += xfs_efi_item_sizeof(EFI_ITEM(lip));
1da177e4
LT
72}
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
7bfa31d8
CH
82xfs_efi_item_format(
83 struct xfs_log_item *lip,
bde7cff6 84 struct xfs_log_vec *lv)
1da177e4 85{
7bfa31d8 86 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
bde7cff6 87 struct xfs_log_iovec *vecp = NULL;
1da177e4 88
b199c8a4
DC
89 ASSERT(atomic_read(&efip->efi_next_extent) ==
90 efip->efi_format.efi_nextents);
1da177e4
LT
91
92 efip->efi_format.efi_type = XFS_LI_EFI;
1da177e4
LT
93 efip->efi_format.efi_size = 1;
94
bde7cff6 95 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
1234351c
CH
96 &efip->efi_format,
97 xfs_efi_item_sizeof(efip));
1da177e4
LT
98}
99
100
101/*
102 * Pinning has no meaning for an efi item, so just return.
103 */
1da177e4 104STATIC void
7bfa31d8
CH
105xfs_efi_item_pin(
106 struct xfs_log_item *lip)
1da177e4 107{
1da177e4
LT
108}
109
1da177e4 110/*
8d99fe92
BF
111 * The unpin operation is the last place an EFI is manipulated in the log. It is
112 * either inserted in the AIL or aborted in the event of a log I/O error. In
113 * either case, the EFI transaction has been successfully committed to make it
114 * this far. Therefore, we expect whoever committed the EFI to either construct
115 * and commit the EFD or drop the EFD's reference in the event of error. Simply
116 * drop the log's EFI reference now that the log is done with it.
1da177e4 117 */
1da177e4 118STATIC void
7bfa31d8
CH
119xfs_efi_item_unpin(
120 struct xfs_log_item *lip,
121 int remove)
1da177e4 122{
7bfa31d8 123 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
5e4b5386 124 xfs_efi_release(efip);
1da177e4
LT
125}
126
127/*
43ff2122
CH
128 * 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.
1da177e4 133 */
1da177e4 134STATIC uint
43ff2122
CH
135xfs_efi_item_push(
136 struct xfs_log_item *lip,
137 struct list_head *buffer_list)
1da177e4
LT
138{
139 return XFS_ITEM_PINNED;
140}
141
8d99fe92
BF
142/*
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 */
1da177e4 147STATIC void
7bfa31d8
CH
148xfs_efi_item_unlock(
149 struct xfs_log_item *lip)
1da177e4 150{
7bfa31d8
CH
151 if (lip->li_flags & XFS_LI_ABORTED)
152 xfs_efi_item_free(EFI_ITEM(lip));
1da177e4
LT
153}
154
155/*
b199c8a4 156 * The EFI is logged only once and cannot be moved in the log, so simply return
666d644c 157 * the lsn at which it's been logged.
1da177e4 158 */
1da177e4 159STATIC xfs_lsn_t
7bfa31d8
CH
160xfs_efi_item_committed(
161 struct xfs_log_item *lip,
162 xfs_lsn_t lsn)
1da177e4
LT
163{
164 return lsn;
165}
166
1da177e4
LT
167/*
168 * 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 */
1da177e4 174STATIC void
7bfa31d8
CH
175xfs_efi_item_committing(
176 struct xfs_log_item *lip,
177 xfs_lsn_t lsn)
1da177e4 178{
1da177e4
LT
179}
180
181/*
182 * This is the ops vector shared by all efi log items.
183 */
272e42b2 184static const struct xfs_item_ops xfs_efi_item_ops = {
7bfa31d8
CH
185 .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,
7bfa31d8
CH
189 .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
1da177e4
LT
193};
194
195
196/*
197 * Allocate and initialize an efi item with the given number of extents.
198 */
7bfa31d8
CH
199struct xfs_efi_log_item *
200xfs_efi_init(
201 struct xfs_mount *mp,
202 uint nextents)
1da177e4
LT
203
204{
7bfa31d8 205 struct xfs_efi_log_item *efip;
1da177e4
LT
206 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)));
7bfa31d8 212 efip = kmem_zalloc(size, KM_SLEEP);
1da177e4 213 } else {
7bfa31d8 214 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
1da177e4
LT
215 }
216
43f5efc5 217 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
1da177e4 218 efip->efi_format.efi_nextents = nextents;
db9d67d6 219 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
b199c8a4 220 atomic_set(&efip->efi_next_extent, 0);
666d644c 221 atomic_set(&efip->efi_refcount, 2);
1da177e4 222
7bfa31d8 223 return efip;
1da177e4
LT
224}
225
6d192a9b
TS
226/*
227 * 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{
4e0d5f92 236 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
6d192a9b
TS
237 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) {
4e0d5f92 249 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
6d192a9b
TS
250
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) {
4e0d5f92 263 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
6d192a9b
TS
264
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 }
2451337d 277 return -EFSCORRUPTED;
6d192a9b
TS
278}
279
1da177e4 280/*
e32a1d1f
BF
281 * 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.
1da177e4
LT
286 */
287void
5e4b5386
BF
288xfs_efi_release(
289 struct xfs_efi_log_item *efip)
1da177e4 290{
e32a1d1f 291 if (atomic_dec_and_test(&efip->efi_refcount)) {
146e54b7 292 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
e32a1d1f
BF
293 xfs_efi_item_free(efip);
294 }
1da177e4
LT
295}
296
7bfa31d8 297static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
7d795ca3 298{
7bfa31d8
CH
299 return container_of(lip, struct xfs_efd_log_item, efd_item);
300}
1da177e4 301
7bfa31d8
CH
302STATIC void
303xfs_efd_item_free(struct xfs_efd_log_item *efdp)
304{
b1c5ebb2 305 kmem_free(efdp->efd_item.li_lv_shadow);
7bfa31d8 306 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
f0e2d93c 307 kmem_free(efdp);
7bfa31d8 308 else
7d795ca3 309 kmem_zone_free(xfs_efd_zone, efdp);
7d795ca3 310}
1da177e4
LT
311
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 */
166d1368
DC
317static inline int
318xfs_efd_item_sizeof(
319 struct xfs_efd_log_item *efdp)
320{
321 return sizeof(xfs_efd_log_format_t) +
322 (efdp->efd_format.efd_nextents - 1) * sizeof(xfs_extent_t);
323}
324
325STATIC void
7bfa31d8 326xfs_efd_item_size(
166d1368
DC
327 struct xfs_log_item *lip,
328 int *nvecs,
329 int *nbytes)
1da177e4 330{
166d1368
DC
331 *nvecs += 1;
332 *nbytes += xfs_efd_item_sizeof(EFD_ITEM(lip));
1da177e4
LT
333}
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
7bfa31d8
CH
343xfs_efd_item_format(
344 struct xfs_log_item *lip,
bde7cff6 345 struct xfs_log_vec *lv)
1da177e4 346{
7bfa31d8 347 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
bde7cff6 348 struct xfs_log_iovec *vecp = NULL;
1da177e4
LT
349
350 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
351
352 efdp->efd_format.efd_type = XFS_LI_EFD;
1da177e4
LT
353 efdp->efd_format.efd_size = 1;
354
bde7cff6 355 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
1234351c
CH
356 &efdp->efd_format,
357 xfs_efd_item_sizeof(efdp));
1da177e4
LT
358}
359
1da177e4
LT
360/*
361 * Pinning has no meaning for an efd item, so just return.
362 */
1da177e4 363STATIC void
7bfa31d8
CH
364xfs_efd_item_pin(
365 struct xfs_log_item *lip)
1da177e4 366{
1da177e4
LT
367}
368
1da177e4
LT
369/*
370 * Since pinning has no meaning for an efd item, unpinning does
371 * not either.
372 */
1da177e4 373STATIC void
7bfa31d8
CH
374xfs_efd_item_unpin(
375 struct xfs_log_item *lip,
376 int remove)
1da177e4 377{
1da177e4
LT
378}
379
380/*
43ff2122
CH
381 * 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.
1da177e4 383 */
1da177e4 384STATIC uint
43ff2122
CH
385xfs_efd_item_push(
386 struct xfs_log_item *lip,
387 struct list_head *buffer_list)
1da177e4 388{
43ff2122 389 return XFS_ITEM_PINNED;
1da177e4
LT
390}
391
8d99fe92
BF
392/*
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 */
1da177e4 396STATIC void
7bfa31d8
CH
397xfs_efd_item_unlock(
398 struct xfs_log_item *lip)
1da177e4 399{
8d99fe92
BF
400 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 }
1da177e4
LT
406}
407
408/*
8d99fe92
BF
409 * 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.
1da177e4 413 */
1da177e4 414STATIC xfs_lsn_t
7bfa31d8
CH
415xfs_efd_item_committed(
416 struct xfs_log_item *lip,
417 xfs_lsn_t lsn)
1da177e4 418{
7bfa31d8
CH
419 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
420
1da177e4 421 /*
8d99fe92
BF
422 * 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).
1da177e4 426 */
8d99fe92 427 xfs_efi_release(efdp->efd_efip);
7d795ca3 428 xfs_efd_item_free(efdp);
8d99fe92 429
1da177e4
LT
430 return (xfs_lsn_t)-1;
431}
432
1da177e4
LT
433/*
434 * 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 */
1da177e4 440STATIC void
7bfa31d8
CH
441xfs_efd_item_committing(
442 struct xfs_log_item *lip,
443 xfs_lsn_t lsn)
1da177e4 444{
1da177e4
LT
445}
446
447/*
448 * This is the ops vector shared by all efd log items.
449 */
272e42b2 450static const struct xfs_item_ops xfs_efd_item_ops = {
7bfa31d8
CH
451 .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,
7bfa31d8
CH
455 .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
1da177e4
LT
459};
460
1da177e4
LT
461/*
462 * Allocate and initialize an efd item with the given number of extents.
463 */
7bfa31d8
CH
464struct xfs_efd_log_item *
465xfs_efd_init(
466 struct xfs_mount *mp,
467 struct xfs_efi_log_item *efip,
468 uint nextents)
1da177e4
LT
469
470{
7bfa31d8 471 struct xfs_efd_log_item *efdp;
1da177e4
LT
472 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)));
7bfa31d8 478 efdp = kmem_zalloc(size, KM_SLEEP);
1da177e4 479 } else {
7bfa31d8 480 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
1da177e4
LT
481 }
482
43f5efc5 483 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
1da177e4
LT
484 efdp->efd_efip = efip;
485 efdp->efd_format.efd_nextents = nextents;
486 efdp->efd_format.efd_efi_id = efip->efi_format.efi_id;
487
7bfa31d8 488 return efdp;
1da177e4 489}
dc42375d
DW
490
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}