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"
25 #include "xfs_mount.h"
26 #include "xfs_trans.h"
27 #include "xfs_trans_priv.h"
28 #include "xfs_buf_item.h"
29 #include "xfs_rmap_item.h"
33 kmem_zone_t
*xfs_rui_zone
;
34 kmem_zone_t
*xfs_rud_zone
;
36 static inline struct xfs_rui_log_item
*RUI_ITEM(struct xfs_log_item
*lip
)
38 return container_of(lip
, struct xfs_rui_log_item
, rui_item
);
43 struct xfs_rui_log_item
*ruip
)
45 if (ruip
->rui_format
.rui_nextents
> XFS_RUI_MAX_FAST_EXTENTS
)
48 kmem_zone_free(xfs_rui_zone
, ruip
);
52 * This returns the number of iovecs needed to log the given rui item.
53 * We only need 1 iovec for an rui item. It just logs the rui_log_format
58 struct xfs_rui_log_item
*ruip
)
60 return sizeof(struct xfs_rui_log_format
) +
61 (ruip
->rui_format
.rui_nextents
- 1) *
62 sizeof(struct xfs_map_extent
);
67 struct xfs_log_item
*lip
,
72 *nbytes
+= xfs_rui_item_sizeof(RUI_ITEM(lip
));
76 * This is called to fill in the vector of log iovecs for the
77 * given rui log item. We use only 1 iovec, and we point that
78 * at the rui_log_format structure embedded in the rui item.
79 * It is at this point that we assert that all of the extent
80 * slots in the rui item have been filled.
84 struct xfs_log_item
*lip
,
85 struct xfs_log_vec
*lv
)
87 struct xfs_rui_log_item
*ruip
= RUI_ITEM(lip
);
88 struct xfs_log_iovec
*vecp
= NULL
;
90 ASSERT(atomic_read(&ruip
->rui_next_extent
) ==
91 ruip
->rui_format
.rui_nextents
);
93 ruip
->rui_format
.rui_type
= XFS_LI_RUI
;
94 ruip
->rui_format
.rui_size
= 1;
96 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_RUI_FORMAT
, &ruip
->rui_format
,
97 xfs_rui_item_sizeof(ruip
));
101 * Pinning has no meaning for an rui item, so just return.
105 struct xfs_log_item
*lip
)
110 * The unpin operation is the last place an RUI is manipulated in the log. It is
111 * either inserted in the AIL or aborted in the event of a log I/O error. In
112 * either case, the RUI transaction has been successfully committed to make it
113 * this far. Therefore, we expect whoever committed the RUI to either construct
114 * and commit the RUD or drop the RUD's reference in the event of error. Simply
115 * drop the log's RUI reference now that the log is done with it.
119 struct xfs_log_item
*lip
,
122 struct xfs_rui_log_item
*ruip
= RUI_ITEM(lip
);
124 xfs_rui_release(ruip
);
128 * RUI items have no locking or pushing. However, since RUIs are pulled from
129 * the AIL when their corresponding RUDs 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 RUI out of
136 struct xfs_log_item
*lip
,
137 struct list_head
*buffer_list
)
139 return XFS_ITEM_PINNED
;
143 * The RUI has been either committed or aborted if the transaction has been
144 * cancelled. If the transaction was cancelled, an RUD isn't going to be
145 * constructed and thus we free the RUI here directly.
149 struct xfs_log_item
*lip
)
151 if (lip
->li_flags
& XFS_LI_ABORTED
)
152 xfs_rui_item_free(RUI_ITEM(lip
));
156 * The RUI is logged only once and cannot be moved in the log, so simply return
157 * the lsn at which it's been logged.
160 xfs_rui_item_committed(
161 struct xfs_log_item
*lip
,
168 * The RUI 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.
175 xfs_rui_item_committing(
176 struct xfs_log_item
*lip
,
182 * This is the ops vector shared by all rui log items.
184 static const struct xfs_item_ops xfs_rui_item_ops
= {
185 .iop_size
= xfs_rui_item_size
,
186 .iop_format
= xfs_rui_item_format
,
187 .iop_pin
= xfs_rui_item_pin
,
188 .iop_unpin
= xfs_rui_item_unpin
,
189 .iop_unlock
= xfs_rui_item_unlock
,
190 .iop_committed
= xfs_rui_item_committed
,
191 .iop_push
= xfs_rui_item_push
,
192 .iop_committing
= xfs_rui_item_committing
,
196 * Allocate and initialize an rui item with the given number of extents.
198 struct xfs_rui_log_item
*
200 struct xfs_mount
*mp
,
204 struct xfs_rui_log_item
*ruip
;
207 ASSERT(nextents
> 0);
208 if (nextents
> XFS_RUI_MAX_FAST_EXTENTS
) {
209 size
= (uint
)(sizeof(struct xfs_rui_log_item
) +
210 ((nextents
- 1) * sizeof(struct xfs_map_extent
)));
211 ruip
= kmem_zalloc(size
, KM_SLEEP
);
213 ruip
= kmem_zone_zalloc(xfs_rui_zone
, KM_SLEEP
);
216 xfs_log_item_init(mp
, &ruip
->rui_item
, XFS_LI_RUI
, &xfs_rui_item_ops
);
217 ruip
->rui_format
.rui_nextents
= nextents
;
218 ruip
->rui_format
.rui_id
= (uintptr_t)(void *)ruip
;
219 atomic_set(&ruip
->rui_next_extent
, 0);
220 atomic_set(&ruip
->rui_refcount
, 2);
226 * Copy an RUI format buffer from the given buf, and into the destination
227 * RUI format structure. The RUI/RUD items were designed not to need any
228 * special alignment handling.
232 struct xfs_log_iovec
*buf
,
233 struct xfs_rui_log_format
*dst_rui_fmt
)
235 struct xfs_rui_log_format
*src_rui_fmt
;
238 src_rui_fmt
= buf
->i_addr
;
239 len
= sizeof(struct xfs_rui_log_format
) +
240 (src_rui_fmt
->rui_nextents
- 1) *
241 sizeof(struct xfs_map_extent
);
243 if (buf
->i_len
!= len
)
244 return -EFSCORRUPTED
;
246 memcpy((char *)dst_rui_fmt
, (char *)src_rui_fmt
, len
);
251 * Freeing the RUI requires that we remove it from the AIL if it has already
252 * been placed there. However, the RUI may not yet have been placed in the AIL
253 * when called by xfs_rui_release() from RUD processing due to the ordering of
254 * committed vs unpin operations in bulk insert operations. Hence the reference
255 * count to ensure only the last caller frees the RUI.
259 struct xfs_rui_log_item
*ruip
)
261 if (atomic_dec_and_test(&ruip
->rui_refcount
)) {
262 xfs_trans_ail_remove(&ruip
->rui_item
, SHUTDOWN_LOG_IO_ERROR
);
263 xfs_rui_item_free(ruip
);
267 static inline struct xfs_rud_log_item
*RUD_ITEM(struct xfs_log_item
*lip
)
269 return container_of(lip
, struct xfs_rud_log_item
, rud_item
);
273 xfs_rud_item_free(struct xfs_rud_log_item
*rudp
)
275 if (rudp
->rud_format
.rud_nextents
> XFS_RUD_MAX_FAST_EXTENTS
)
278 kmem_zone_free(xfs_rud_zone
, rudp
);
282 * This returns the number of iovecs needed to log the given rud item.
283 * We only need 1 iovec for an rud item. It just logs the rud_log_format
288 struct xfs_rud_log_item
*rudp
)
290 return sizeof(struct xfs_rud_log_format
) +
291 (rudp
->rud_format
.rud_nextents
- 1) *
292 sizeof(struct xfs_map_extent
);
297 struct xfs_log_item
*lip
,
302 *nbytes
+= xfs_rud_item_sizeof(RUD_ITEM(lip
));
306 * This is called to fill in the vector of log iovecs for the
307 * given rud log item. We use only 1 iovec, and we point that
308 * at the rud_log_format structure embedded in the rud item.
309 * It is at this point that we assert that all of the extent
310 * slots in the rud item have been filled.
314 struct xfs_log_item
*lip
,
315 struct xfs_log_vec
*lv
)
317 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
318 struct xfs_log_iovec
*vecp
= NULL
;
320 ASSERT(rudp
->rud_next_extent
== rudp
->rud_format
.rud_nextents
);
322 rudp
->rud_format
.rud_type
= XFS_LI_RUD
;
323 rudp
->rud_format
.rud_size
= 1;
325 xlog_copy_iovec(lv
, &vecp
, XLOG_REG_TYPE_RUD_FORMAT
, &rudp
->rud_format
,
326 xfs_rud_item_sizeof(rudp
));
330 * Pinning has no meaning for an rud item, so just return.
334 struct xfs_log_item
*lip
)
339 * Since pinning has no meaning for an rud item, unpinning does
344 struct xfs_log_item
*lip
,
350 * There isn't much you can do to push on an rud item. It is simply stuck
351 * waiting for the log to be flushed to disk.
355 struct xfs_log_item
*lip
,
356 struct list_head
*buffer_list
)
358 return XFS_ITEM_PINNED
;
362 * The RUD is either committed or aborted if the transaction is cancelled. If
363 * the transaction is cancelled, drop our reference to the RUI and free the
368 struct xfs_log_item
*lip
)
370 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
372 if (lip
->li_flags
& XFS_LI_ABORTED
) {
373 xfs_rui_release(rudp
->rud_ruip
);
374 xfs_rud_item_free(rudp
);
379 * When the rud item is committed to disk, all we need to do is delete our
380 * reference to our partner rui item and then free ourselves. Since we're
381 * freeing ourselves we must return -1 to keep the transaction code from
382 * further referencing this item.
385 xfs_rud_item_committed(
386 struct xfs_log_item
*lip
,
389 struct xfs_rud_log_item
*rudp
= RUD_ITEM(lip
);
392 * Drop the RUI reference regardless of whether the RUD has been
393 * aborted. Once the RUD transaction is constructed, it is the sole
394 * responsibility of the RUD to release the RUI (even if the RUI is
395 * aborted due to log I/O error).
397 xfs_rui_release(rudp
->rud_ruip
);
398 xfs_rud_item_free(rudp
);
400 return (xfs_lsn_t
)-1;
404 * The RUD dependency tracking op doesn't do squat. It can't because
405 * it doesn't know where the free extent is coming from. The dependency
406 * tracking has to be handled by the "enclosing" metadata object. For
407 * example, for inodes, the inode is locked throughout the extent freeing
408 * so the dependency should be recorded there.
411 xfs_rud_item_committing(
412 struct xfs_log_item
*lip
,
418 * This is the ops vector shared by all rud log items.
420 static const struct xfs_item_ops xfs_rud_item_ops
= {
421 .iop_size
= xfs_rud_item_size
,
422 .iop_format
= xfs_rud_item_format
,
423 .iop_pin
= xfs_rud_item_pin
,
424 .iop_unpin
= xfs_rud_item_unpin
,
425 .iop_unlock
= xfs_rud_item_unlock
,
426 .iop_committed
= xfs_rud_item_committed
,
427 .iop_push
= xfs_rud_item_push
,
428 .iop_committing
= xfs_rud_item_committing
,
432 * Allocate and initialize an rud item with the given number of extents.
434 struct xfs_rud_log_item
*
436 struct xfs_mount
*mp
,
437 struct xfs_rui_log_item
*ruip
,
441 struct xfs_rud_log_item
*rudp
;
444 ASSERT(nextents
> 0);
445 if (nextents
> XFS_RUD_MAX_FAST_EXTENTS
) {
446 size
= (uint
)(sizeof(struct xfs_rud_log_item
) +
447 ((nextents
- 1) * sizeof(struct xfs_map_extent
)));
448 rudp
= kmem_zalloc(size
, KM_SLEEP
);
450 rudp
= kmem_zone_zalloc(xfs_rud_zone
, KM_SLEEP
);
453 xfs_log_item_init(mp
, &rudp
->rud_item
, XFS_LI_RUD
, &xfs_rud_item_ops
);
454 rudp
->rud_ruip
= ruip
;
455 rudp
->rud_format
.rud_nextents
= nextents
;
456 rudp
->rud_format
.rud_rui_id
= ruip
->rui_format
.rui_id
;