]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - fs/xfs/xfs_extfree_item.c
xfs: refactor redo intent item processing
[mirror_ubuntu-zesty-kernel.git] / fs / xfs / xfs_extfree_item.c
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_format.h"
21 #include "xfs_log_format.h"
22 #include "xfs_trans_resv.h"
23 #include "xfs_bit.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
32 kmem_zone_t *xfs_efi_zone;
33 kmem_zone_t *xfs_efd_zone;
34
35 static 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
40 void
41 xfs_efi_item_free(
42 struct xfs_efi_log_item *efip)
43 {
44 kmem_free(efip->efi_item.li_lv_shadow);
45 if (efip->efi_format.efi_nextents > XFS_EFI_MAX_FAST_EXTENTS)
46 kmem_free(efip);
47 else
48 kmem_zone_free(xfs_efi_zone, efip);
49 }
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 */
56 static inline int
57 xfs_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
64 STATIC void
65 xfs_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));
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 */
81 STATIC void
82 xfs_efi_item_format(
83 struct xfs_log_item *lip,
84 struct xfs_log_vec *lv)
85 {
86 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
87 struct xfs_log_iovec *vecp = NULL;
88
89 ASSERT(atomic_read(&efip->efi_next_extent) ==
90 efip->efi_format.efi_nextents);
91
92 efip->efi_format.efi_type = XFS_LI_EFI;
93 efip->efi_format.efi_size = 1;
94
95 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFI_FORMAT,
96 &efip->efi_format,
97 xfs_efi_item_sizeof(efip));
98 }
99
100
101 /*
102 * Pinning has no meaning for an efi item, so just return.
103 */
104 STATIC void
105 xfs_efi_item_pin(
106 struct xfs_log_item *lip)
107 {
108 }
109
110 /*
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.
117 */
118 STATIC void
119 xfs_efi_item_unpin(
120 struct xfs_log_item *lip,
121 int remove)
122 {
123 struct xfs_efi_log_item *efip = EFI_ITEM(lip);
124 xfs_efi_release(efip);
125 }
126
127 /*
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.
133 */
134 STATIC uint
135 xfs_efi_item_push(
136 struct xfs_log_item *lip,
137 struct list_head *buffer_list)
138 {
139 return XFS_ITEM_PINNED;
140 }
141
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 */
147 STATIC void
148 xfs_efi_item_unlock(
149 struct xfs_log_item *lip)
150 {
151 if (lip->li_flags & XFS_LI_ABORTED)
152 xfs_efi_item_free(EFI_ITEM(lip));
153 }
154
155 /*
156 * The EFI is logged only once and cannot be moved in the log, so simply return
157 * the lsn at which it's been logged.
158 */
159 STATIC xfs_lsn_t
160 xfs_efi_item_committed(
161 struct xfs_log_item *lip,
162 xfs_lsn_t lsn)
163 {
164 return lsn;
165 }
166
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 */
174 STATIC void
175 xfs_efi_item_committing(
176 struct xfs_log_item *lip,
177 xfs_lsn_t lsn)
178 {
179 }
180
181 /*
182 * This is the ops vector shared by all efi log items.
183 */
184 static const struct xfs_item_ops xfs_efi_item_ops = {
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,
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
193 };
194
195
196 /*
197 * Allocate and initialize an efi item with the given number of extents.
198 */
199 struct xfs_efi_log_item *
200 xfs_efi_init(
201 struct xfs_mount *mp,
202 uint nextents)
203
204 {
205 struct xfs_efi_log_item *efip;
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)));
212 efip = kmem_zalloc(size, KM_SLEEP);
213 } else {
214 efip = kmem_zone_zalloc(xfs_efi_zone, KM_SLEEP);
215 }
216
217 xfs_log_item_init(mp, &efip->efi_item, XFS_LI_EFI, &xfs_efi_item_ops);
218 efip->efi_format.efi_nextents = nextents;
219 efip->efi_format.efi_id = (uintptr_t)(void *)efip;
220 atomic_set(&efip->efi_next_extent, 0);
221 atomic_set(&efip->efi_refcount, 2);
222
223 return efip;
224 }
225
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 */
233 int
234 xfs_efi_copy_format(xfs_log_iovec_t *buf, xfs_efi_log_format_t *dst_efi_fmt)
235 {
236 xfs_efi_log_format_t *src_efi_fmt = buf->i_addr;
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) {
249 xfs_efi_log_format_32_t *src_efi_fmt_32 = buf->i_addr;
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) {
263 xfs_efi_log_format_64_t *src_efi_fmt_64 = buf->i_addr;
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 }
277 return -EFSCORRUPTED;
278 }
279
280 /*
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.
286 */
287 void
288 xfs_efi_release(
289 struct xfs_efi_log_item *efip)
290 {
291 if (atomic_dec_and_test(&efip->efi_refcount)) {
292 xfs_trans_ail_remove(&efip->efi_item, SHUTDOWN_LOG_IO_ERROR);
293 xfs_efi_item_free(efip);
294 }
295 }
296
297 static inline struct xfs_efd_log_item *EFD_ITEM(struct xfs_log_item *lip)
298 {
299 return container_of(lip, struct xfs_efd_log_item, efd_item);
300 }
301
302 STATIC void
303 xfs_efd_item_free(struct xfs_efd_log_item *efdp)
304 {
305 kmem_free(efdp->efd_item.li_lv_shadow);
306 if (efdp->efd_format.efd_nextents > XFS_EFD_MAX_FAST_EXTENTS)
307 kmem_free(efdp);
308 else
309 kmem_zone_free(xfs_efd_zone, efdp);
310 }
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 */
317 static inline int
318 xfs_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
325 STATIC void
326 xfs_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));
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 */
342 STATIC void
343 xfs_efd_item_format(
344 struct xfs_log_item *lip,
345 struct xfs_log_vec *lv)
346 {
347 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
348 struct xfs_log_iovec *vecp = NULL;
349
350 ASSERT(efdp->efd_next_extent == efdp->efd_format.efd_nextents);
351
352 efdp->efd_format.efd_type = XFS_LI_EFD;
353 efdp->efd_format.efd_size = 1;
354
355 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_EFD_FORMAT,
356 &efdp->efd_format,
357 xfs_efd_item_sizeof(efdp));
358 }
359
360 /*
361 * Pinning has no meaning for an efd item, so just return.
362 */
363 STATIC void
364 xfs_efd_item_pin(
365 struct xfs_log_item *lip)
366 {
367 }
368
369 /*
370 * Since pinning has no meaning for an efd item, unpinning does
371 * not either.
372 */
373 STATIC void
374 xfs_efd_item_unpin(
375 struct xfs_log_item *lip,
376 int remove)
377 {
378 }
379
380 /*
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.
383 */
384 STATIC uint
385 xfs_efd_item_push(
386 struct xfs_log_item *lip,
387 struct list_head *buffer_list)
388 {
389 return XFS_ITEM_PINNED;
390 }
391
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 */
396 STATIC void
397 xfs_efd_item_unlock(
398 struct xfs_log_item *lip)
399 {
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 }
406 }
407
408 /*
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.
413 */
414 STATIC xfs_lsn_t
415 xfs_efd_item_committed(
416 struct xfs_log_item *lip,
417 xfs_lsn_t lsn)
418 {
419 struct xfs_efd_log_item *efdp = EFD_ITEM(lip);
420
421 /*
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).
426 */
427 xfs_efi_release(efdp->efd_efip);
428 xfs_efd_item_free(efdp);
429
430 return (xfs_lsn_t)-1;
431 }
432
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 */
440 STATIC void
441 xfs_efd_item_committing(
442 struct xfs_log_item *lip,
443 xfs_lsn_t lsn)
444 {
445 }
446
447 /*
448 * This is the ops vector shared by all efd log items.
449 */
450 static const struct xfs_item_ops xfs_efd_item_ops = {
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,
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
459 };
460
461 /*
462 * Allocate and initialize an efd item with the given number of extents.
463 */
464 struct xfs_efd_log_item *
465 xfs_efd_init(
466 struct xfs_mount *mp,
467 struct xfs_efi_log_item *efip,
468 uint nextents)
469
470 {
471 struct xfs_efd_log_item *efdp;
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)));
478 efdp = kmem_zalloc(size, KM_SLEEP);
479 } else {
480 efdp = kmem_zone_zalloc(xfs_efd_zone, KM_SLEEP);
481 }
482
483 xfs_log_item_init(mp, &efdp->efd_item, XFS_LI_EFD, &xfs_efd_item_ops);
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
488 return efdp;
489 }
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 */
495 int
496 xfs_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
550 abort_error:
551 xfs_trans_cancel(tp);
552 return error;
553 }