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_shared.h"
23 #include "xfs_format.h"
24 #include "xfs_log_format.h"
25 #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_rmap_item.h"
31 #include "xfs_alloc.h"
34 /* Set the map extent flags for this reverse mapping. */
36 xfs_trans_set_rmap_flags(
37 struct xfs_map_extent *rmap,
38 enum xfs_rmap_intent_type type,
43 if (state == XFS_EXT_UNWRITTEN)
44 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
45 if (whichfork == XFS_ATTR_FORK)
46 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
49 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
51 case XFS_RMAP_MAP_SHARED:
52 rmap->me_flags |= XFS_RMAP_EXTENT_MAP_SHARED;
55 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
57 case XFS_RMAP_UNMAP_SHARED:
58 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP_SHARED;
60 case XFS_RMAP_CONVERT:
61 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
63 case XFS_RMAP_CONVERT_SHARED:
64 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT_SHARED;
67 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
70 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
77 struct xfs_rud_log_item *
80 struct xfs_rui_log_item *ruip)
82 struct xfs_rud_log_item *rudp;
84 rudp = xfs_rud_init(tp->t_mountp, ruip);
85 xfs_trans_add_item(tp, &rudp->rud_item);
90 * Finish an rmap update and log it to the RUD. Note that the transaction is
91 * marked dirty regardless of whether the rmap update succeeds or fails to
92 * support the RUI/RUD lifecycle rules.
95 xfs_trans_log_finish_rmap_update(
97 struct xfs_rud_log_item *rudp,
98 enum xfs_rmap_intent_type type,
101 xfs_fileoff_t startoff,
102 xfs_fsblock_t startblock,
103 xfs_filblks_t blockcount,
105 struct xfs_btree_cur **pcur)
109 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
110 startblock, blockcount, state, pcur);
113 * Mark the transaction dirty, even on error. This ensures the
114 * transaction is aborted, which:
116 * 1.) releases the RUI and frees the RUD
117 * 2.) shuts down the filesystem
119 tp->t_flags |= XFS_TRANS_DIRTY;
120 rudp->rud_item.li_desc->lid_flags |= XFS_LID_DIRTY;
125 /* Sort rmap intents by AG. */
127 xfs_rmap_update_diff_items(
132 struct xfs_mount *mp = priv;
133 struct xfs_rmap_intent *ra;
134 struct xfs_rmap_intent *rb;
136 ra = container_of(a, struct xfs_rmap_intent, ri_list);
137 rb = container_of(b, struct xfs_rmap_intent, ri_list);
138 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
139 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
144 xfs_rmap_update_create_intent(
145 struct xfs_trans *tp,
148 struct xfs_rui_log_item *ruip;
153 ruip = xfs_rui_init(tp->t_mountp, count);
154 ASSERT(ruip != NULL);
157 * Get a log_item_desc to point at the new item.
159 xfs_trans_add_item(tp, &ruip->rui_item);
163 /* Log rmap updates in the intent item. */
165 xfs_rmap_update_log_item(
166 struct xfs_trans *tp,
168 struct list_head *item)
170 struct xfs_rui_log_item *ruip = intent;
171 struct xfs_rmap_intent *rmap;
173 struct xfs_map_extent *map;
175 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
177 tp->t_flags |= XFS_TRANS_DIRTY;
178 ruip->rui_item.li_desc->lid_flags |= XFS_LID_DIRTY;
181 * atomic_inc_return gives us the value after the increment;
182 * we want to use it as an array index so we need to subtract 1 from
185 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
186 ASSERT(next_extent < ruip->rui_format.rui_nextents);
187 map = &ruip->rui_format.rui_extents[next_extent];
188 map->me_owner = rmap->ri_owner;
189 map->me_startblock = rmap->ri_bmap.br_startblock;
190 map->me_startoff = rmap->ri_bmap.br_startoff;
191 map->me_len = rmap->ri_bmap.br_blockcount;
192 xfs_trans_set_rmap_flags(map, rmap->ri_type, rmap->ri_whichfork,
193 rmap->ri_bmap.br_state);
196 /* Get an RUD so we can process all the deferred rmap updates. */
198 xfs_rmap_update_create_done(
199 struct xfs_trans *tp,
203 return xfs_trans_get_rud(tp, intent);
206 /* Process a deferred rmap update. */
208 xfs_rmap_update_finish_item(
209 struct xfs_trans *tp,
210 struct xfs_defer_ops *dop,
211 struct list_head *item,
215 struct xfs_rmap_intent *rmap;
218 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
219 error = xfs_trans_log_finish_rmap_update(tp, done_item,
221 rmap->ri_owner, rmap->ri_whichfork,
222 rmap->ri_bmap.br_startoff,
223 rmap->ri_bmap.br_startblock,
224 rmap->ri_bmap.br_blockcount,
225 rmap->ri_bmap.br_state,
226 (struct xfs_btree_cur **)state);
231 /* Clean up after processing deferred rmaps. */
233 xfs_rmap_update_finish_cleanup(
234 struct xfs_trans *tp,
238 struct xfs_btree_cur *rcur = state;
240 xfs_rmap_finish_one_cleanup(tp, rcur, error);
243 /* Abort all pending RUIs. */
245 xfs_rmap_update_abort_intent(
248 xfs_rui_release(intent);
251 /* Cancel a deferred rmap update. */
253 xfs_rmap_update_cancel_item(
254 struct list_head *item)
256 struct xfs_rmap_intent *rmap;
258 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
262 static const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
263 .type = XFS_DEFER_OPS_TYPE_RMAP,
264 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
265 .diff_items = xfs_rmap_update_diff_items,
266 .create_intent = xfs_rmap_update_create_intent,
267 .abort_intent = xfs_rmap_update_abort_intent,
268 .log_item = xfs_rmap_update_log_item,
269 .create_done = xfs_rmap_update_create_done,
270 .finish_item = xfs_rmap_update_finish_item,
271 .finish_cleanup = xfs_rmap_update_finish_cleanup,
272 .cancel_item = xfs_rmap_update_cancel_item,
275 /* Register the deferred op type. */
277 xfs_rmap_update_init_defer_op(void)
279 xfs_defer_init_op_type(&xfs_rmap_update_defer_type);