2 * Copyright (C) 2016 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "xfs_format.h"
23 #include "xfs_log_format.h"
24 #include "xfs_trans_resv.h"
26 #include "xfs_mount.h"
27 #include "xfs_defer.h"
28 #include "xfs_trans.h"
29 #include "xfs_trans_priv.h"
30 #include "xfs_buf_item.h"
31 #include "xfs_rmap_item.h"
36 kmem_zone_t
*xfs_rui_zone
;
37 kmem_zone_t
*xfs_rud_zone
;
39 static inline struct xfs_rui_log_item
*RUI_ITEM(struct xfs_log_item
*lip
)
41 return container_of(lip
, struct xfs_rui_log_item
, rui_item
);
46 struct xfs_rui_log_item
*ruip
)
48 if (ruip
->rui_format
.rui_nextents
> XFS_RUI_MAX_FAST_EXTENTS
)
51 kmem_zone_free(xfs_rui_zone
, ruip
);
56 struct xfs_log_item
*lip
,
60 struct xfs_rui_log_item
*ruip
= RUI_ITEM(lip
);
63 *nbytes
+= xfs_rui_log_format_sizeof(ruip
->rui_format
.rui_nextents
);
67 * This is called to fill in the vector of log iovecs for the
68 * given rui log item. We use only 1 iovec, and we point that
69 * at the rui_log_format structure embedded in the rui item.
70 * It is at this point that we assert that all of the extent
71 * slots in the rui item have been filled.
75 struct xfs_log_item
*lip
,
76 struct xfs_log_vec
*lv
)
78 struct xfs_rui_log_item
*ruip
= RUI_ITEM(lip
);
79 struct xfs_log_iovec
*vecp
= NULL
;
81 ASSERT(atomic_read(&ruip
->rui_next_extent
) ==
82 ruip
->rui_format
.rui_nextents
);
84 ruip
->rui_format
.rui_type
= XFS_LI_RUI
;
85 ruip
->rui_format
.rui_size
= 1;
87 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_RUI_FORMAT
, &ruip
->rui_format
,
88 xfs_rui_log_format_sizeof(ruip
->rui_format
.rui_nextents
));
92 * Pinning has no meaning for an rui item, so just return.
96 struct xfs_log_item
*lip
)
101 * The unpin operation is the last place an RUI is manipulated in the log. It is
102 * either inserted in the AIL or aborted in the event of a log I/O error. In
103 * either case, the RUI transaction has been successfully committed to make it
104 * this far. Therefore, we expect whoever committed the RUI to either construct
105 * and commit the RUD or drop the RUD's reference in the event of error. Simply
106 * drop the log's RUI reference now that the log is done with it.
110 struct xfs_log_item
*lip
,
113 struct xfs_rui_log_item
*ruip
= RUI_ITEM(lip
);
115 xfs_rui_release(ruip
);
119 * RUI items have no locking or pushing. However, since RUIs are pulled from
120 * the AIL when their corresponding RUDs are committed to disk, their situation
121 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
122 * will eventually flush the log. This should help in getting the RUI out of
127 struct xfs_log_item
*lip
,
128 struct list_head
*buffer_list
)
130 return XFS_ITEM_PINNED
;
134 * The RUI has been either committed or aborted if the transaction has been
135 * cancelled. If the transaction was cancelled, an RUD isn't going to be
136 * constructed and thus we free the RUI here directly.
140 struct xfs_log_item
*lip
)
142 if (lip
->li_flags
& XFS_LI_ABORTED
)
143 xfs_rui_item_free(RUI_ITEM(lip
));
147 * The RUI is logged only once and cannot be moved in the log, so simply return
148 * the lsn at which it's been logged.
151 xfs_rui_item_committed(
152 struct xfs_log_item
*lip
,
159 * The RUI dependency tracking op doesn't do squat. It can't because
160 * it doesn't know where the free extent is coming from. The dependency
161 * tracking has to be handled by the "enclosing" metadata object. For
162 * example, for inodes, the inode is locked throughout the extent freeing
163 * so the dependency should be recorded there.
166 xfs_rui_item_committing(
167 struct xfs_log_item
*lip
,
173 * This is the ops vector shared by all rui log items.
175 static const struct xfs_item_ops xfs_rui_item_ops
= {
176 .iop_size
= xfs_rui_item_size
,
177 .iop_format
= xfs_rui_item_format
,
178 .iop_pin
= xfs_rui_item_pin
,
179 .iop_unpin
= xfs_rui_item_unpin
,
180 .iop_unlock
= xfs_rui_item_unlock
,
181 .iop_committed
= xfs_rui_item_committed
,
182 .iop_push
= xfs_rui_item_push
,
183 .iop_committing
= xfs_rui_item_committing
,
187 * Allocate and initialize an rui item with the given number of extents.
189 struct xfs_rui_log_item
*
191 struct xfs_mount
*mp
,
195 struct xfs_rui_log_item
*ruip
;
197 ASSERT(nextents
> 0);
198 if (nextents
> XFS_RUI_MAX_FAST_EXTENTS
)
199 ruip
= kmem_zalloc(xfs_rui_log_item_sizeof(nextents
), KM_SLEEP
);
201 ruip
= kmem_zone_zalloc(xfs_rui_zone
, KM_SLEEP
);
203 xfs_log_item_init(mp
, &ruip
->rui_item
, XFS_LI_RUI
, &xfs_rui_item_ops
);
204 ruip
->rui_format
.rui_nextents
= nextents
;
205 ruip
->rui_format
.rui_id
= (uintptr_t)(void *)ruip
;
206 atomic_set(&ruip
->rui_next_extent
, 0);
207 atomic_set(&ruip
->rui_refcount
, 2);
213 * Copy an RUI format buffer from the given buf, and into the destination
214 * RUI format structure. The RUI/RUD items were designed not to need any
215 * special alignment handling.
219 struct xfs_log_iovec
*buf
,
220 struct xfs_rui_log_format
*dst_rui_fmt
)
222 struct xfs_rui_log_format
*src_rui_fmt
;
225 src_rui_fmt
= buf
->i_addr
;
226 len
= xfs_rui_log_format_sizeof(src_rui_fmt
->rui_nextents
);
228 if (buf
->i_len
!= len
)
229 return -EFSCORRUPTED
;
231 memcpy(dst_rui_fmt
, src_rui_fmt
, len
);
236 * Freeing the RUI requires that we remove it from the AIL if it has already
237 * been placed there. However, the RUI may not yet have been placed in the AIL
238 * when called by xfs_rui_release() from RUD processing due to the ordering of
239 * committed vs unpin operations in bulk insert operations. Hence the reference
240 * count to ensure only the last caller frees the RUI.
244 struct xfs_rui_log_item
*ruip
)
246 ASSERT(atomic_read(&ruip
->rui_refcount
) > 0);
247 if (atomic_dec_and_test(&ruip
->rui_refcount
)) {
248 xfs_trans_ail_remove(&ruip
->rui_item
, SHUTDOWN_LOG_IO_ERROR
);
249 xfs_rui_item_free(ruip
);
253 static inline struct xfs_rud_log_item
*RUD_ITEM(struct xfs_log_item
*lip
)
255 return container_of(lip
, struct xfs_rud_log_item
, rud_item
);
260 struct xfs_log_item
*lip
,
265 *nbytes
+= sizeof(struct xfs_rud_log_format
);
269 * This is called to fill in the vector of log iovecs for the
270 * given rud log item. We use only 1 iovec, and we point that
271 * at the rud_log_format structure embedded in the rud item.
272 * It is at this point that we assert that all of the extent
273 * slots in the rud item have been filled.
277 struct xfs_log_item
*lip
,
278 struct xfs_log_vec
*lv
)
280 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
281 struct xfs_log_iovec
*vecp
= NULL
;
283 rudp
->rud_format
.rud_type
= XFS_LI_RUD
;
284 rudp
->rud_format
.rud_size
= 1;
286 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_RUD_FORMAT
, &rudp
->rud_format
,
287 sizeof(struct xfs_rud_log_format
));
291 * Pinning has no meaning for an rud item, so just return.
295 struct xfs_log_item
*lip
)
300 * Since pinning has no meaning for an rud item, unpinning does
305 struct xfs_log_item
*lip
,
311 * There isn't much you can do to push on an rud item. It is simply stuck
312 * waiting for the log to be flushed to disk.
316 struct xfs_log_item
*lip
,
317 struct list_head
*buffer_list
)
319 return XFS_ITEM_PINNED
;
323 * The RUD is either committed or aborted if the transaction is cancelled. If
324 * the transaction is cancelled, drop our reference to the RUI and free the
329 struct xfs_log_item
*lip
)
331 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
333 if (lip
->li_flags
& XFS_LI_ABORTED
) {
334 xfs_rui_release(rudp
->rud_ruip
);
335 kmem_zone_free(xfs_rud_zone
, rudp
);
340 * When the rud item is committed to disk, all we need to do is delete our
341 * reference to our partner rui item and then free ourselves. Since we're
342 * freeing ourselves we must return -1 to keep the transaction code from
343 * further referencing this item.
346 xfs_rud_item_committed(
347 struct xfs_log_item
*lip
,
350 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
353 * Drop the RUI reference regardless of whether the RUD has been
354 * aborted. Once the RUD transaction is constructed, it is the sole
355 * responsibility of the RUD to release the RUI (even if the RUI is
356 * aborted due to log I/O error).
358 xfs_rui_release(rudp
->rud_ruip
);
359 kmem_zone_free(xfs_rud_zone
, rudp
);
361 return (xfs_lsn_t
)-1;
365 * The RUD dependency tracking op doesn't do squat. It can't because
366 * it doesn't know where the free extent is coming from. The dependency
367 * tracking has to be handled by the "enclosing" metadata object. For
368 * example, for inodes, the inode is locked throughout the extent freeing
369 * so the dependency should be recorded there.
372 xfs_rud_item_committing(
373 struct xfs_log_item
*lip
,
379 * This is the ops vector shared by all rud log items.
381 static const struct xfs_item_ops xfs_rud_item_ops
= {
382 .iop_size
= xfs_rud_item_size
,
383 .iop_format
= xfs_rud_item_format
,
384 .iop_pin
= xfs_rud_item_pin
,
385 .iop_unpin
= xfs_rud_item_unpin
,
386 .iop_unlock
= xfs_rud_item_unlock
,
387 .iop_committed
= xfs_rud_item_committed
,
388 .iop_push
= xfs_rud_item_push
,
389 .iop_committing
= xfs_rud_item_committing
,
393 * Allocate and initialize an rud item with the given number of extents.
395 struct xfs_rud_log_item
*
397 struct xfs_mount
*mp
,
398 struct xfs_rui_log_item
*ruip
)
401 struct xfs_rud_log_item
*rudp
;
403 rudp
= kmem_zone_zalloc(xfs_rud_zone
, KM_SLEEP
);
404 xfs_log_item_init(mp
, &rudp
->rud_item
, XFS_LI_RUD
, &xfs_rud_item_ops
);
405 rudp
->rud_ruip
= ruip
;
406 rudp
->rud_format
.rud_rui_id
= ruip
->rui_format
.rui_id
;
412 * Process an rmap update intent item that was recovered from the log.
413 * We need to update the rmapbt.
417 struct xfs_mount
*mp
,
418 struct xfs_rui_log_item
*ruip
)
422 struct xfs_map_extent
*rmap
;
423 xfs_fsblock_t startblock_fsb
;
425 struct xfs_rud_log_item
*rudp
;
426 enum xfs_rmap_intent_type type
;
429 struct xfs_trans
*tp
;
430 struct xfs_btree_cur
*rcur
= NULL
;
432 ASSERT(!test_bit(XFS_RUI_RECOVERED
, &ruip
->rui_flags
));
435 * First check the validity of the extents described by the
436 * RUI. If any are bad, then assume that all are bad and
439 for (i
= 0; i
< ruip
->rui_format
.rui_nextents
; i
++) {
440 rmap
= &ruip
->rui_format
.rui_extents
[i
];
441 startblock_fsb
= XFS_BB_TO_FSB(mp
,
442 XFS_FSB_TO_DADDR(mp
, rmap
->me_startblock
));
443 switch (rmap
->me_flags
& XFS_RMAP_EXTENT_TYPE_MASK
) {
444 case XFS_RMAP_EXTENT_MAP
:
445 case XFS_RMAP_EXTENT_MAP_SHARED
:
446 case XFS_RMAP_EXTENT_UNMAP
:
447 case XFS_RMAP_EXTENT_UNMAP_SHARED
:
448 case XFS_RMAP_EXTENT_CONVERT
:
449 case XFS_RMAP_EXTENT_CONVERT_SHARED
:
450 case XFS_RMAP_EXTENT_ALLOC
:
451 case XFS_RMAP_EXTENT_FREE
:
458 if (!op_ok
|| startblock_fsb
== 0 ||
460 startblock_fsb
>= mp
->m_sb
.sb_dblocks
||
461 rmap
->me_len
>= mp
->m_sb
.sb_agblocks
||
462 (rmap
->me_flags
& ~XFS_RMAP_EXTENT_FLAGS
)) {
464 * This will pull the RUI from the AIL and
465 * free the memory associated with it.
467 set_bit(XFS_RUI_RECOVERED
, &ruip
->rui_flags
);
468 xfs_rui_release(ruip
);
473 error
= xfs_trans_alloc(mp
, &M_RES(mp
)->tr_itruncate
, 0, 0, 0, &tp
);
476 rudp
= xfs_trans_get_rud(tp
, ruip
);
478 for (i
= 0; i
< ruip
->rui_format
.rui_nextents
; i
++) {
479 rmap
= &ruip
->rui_format
.rui_extents
[i
];
480 state
= (rmap
->me_flags
& XFS_RMAP_EXTENT_UNWRITTEN
) ?
481 XFS_EXT_UNWRITTEN
: XFS_EXT_NORM
;
482 whichfork
= (rmap
->me_flags
& XFS_RMAP_EXTENT_ATTR_FORK
) ?
483 XFS_ATTR_FORK
: XFS_DATA_FORK
;
484 switch (rmap
->me_flags
& XFS_RMAP_EXTENT_TYPE_MASK
) {
485 case XFS_RMAP_EXTENT_MAP
:
488 case XFS_RMAP_EXTENT_MAP_SHARED
:
489 type
= XFS_RMAP_MAP_SHARED
;
491 case XFS_RMAP_EXTENT_UNMAP
:
492 type
= XFS_RMAP_UNMAP
;
494 case XFS_RMAP_EXTENT_UNMAP_SHARED
:
495 type
= XFS_RMAP_UNMAP_SHARED
;
497 case XFS_RMAP_EXTENT_CONVERT
:
498 type
= XFS_RMAP_CONVERT
;
500 case XFS_RMAP_EXTENT_CONVERT_SHARED
:
501 type
= XFS_RMAP_CONVERT_SHARED
;
503 case XFS_RMAP_EXTENT_ALLOC
:
504 type
= XFS_RMAP_ALLOC
;
506 case XFS_RMAP_EXTENT_FREE
:
507 type
= XFS_RMAP_FREE
;
510 error
= -EFSCORRUPTED
;
513 error
= xfs_trans_log_finish_rmap_update(tp
, rudp
, type
,
514 rmap
->me_owner
, whichfork
,
515 rmap
->me_startoff
, rmap
->me_startblock
,
516 rmap
->me_len
, state
, &rcur
);
522 xfs_rmap_finish_one_cleanup(tp
, rcur
, error
);
523 set_bit(XFS_RUI_RECOVERED
, &ruip
->rui_flags
);
524 error
= xfs_trans_commit(tp
);
528 xfs_rmap_finish_one_cleanup(tp
, rcur
, error
);
529 xfs_trans_cancel(tp
);