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
;
52 rmap
->me_flags
|= XFS_RMAP_EXTENT_UNMAP
;
54 case XFS_RMAP_CONVERT
:
55 rmap
->me_flags
|= XFS_RMAP_EXTENT_CONVERT
;
58 rmap
->me_flags
|= XFS_RMAP_EXTENT_ALLOC
;
61 rmap
->me_flags
|= XFS_RMAP_EXTENT_FREE
;
68 struct xfs_rud_log_item
*
71 struct xfs_rui_log_item
*ruip
)
73 struct xfs_rud_log_item
*rudp
;
75 rudp
= xfs_rud_init(tp
->t_mountp
, ruip
);
76 xfs_trans_add_item(tp
, &rudp
->rud_item
);
81 * Finish an rmap update and log it to the RUD. Note that the transaction is
82 * marked dirty regardless of whether the rmap update succeeds or fails to
83 * support the RUI/RUD lifecycle rules.
86 xfs_trans_log_finish_rmap_update(
88 struct xfs_rud_log_item
*rudp
,
89 enum xfs_rmap_intent_type type
,
92 xfs_fileoff_t startoff
,
93 xfs_fsblock_t startblock
,
94 xfs_filblks_t blockcount
,
96 struct xfs_btree_cur
**pcur
)
100 error
= xfs_rmap_finish_one(tp
, type
, owner
, whichfork
, startoff
,
101 startblock
, blockcount
, state
, pcur
);
104 * Mark the transaction dirty, even on error. This ensures the
105 * transaction is aborted, which:
107 * 1.) releases the RUI and frees the RUD
108 * 2.) shuts down the filesystem
110 tp
->t_flags
|= XFS_TRANS_DIRTY
;
111 rudp
->rud_item
.li_desc
->lid_flags
|= XFS_LID_DIRTY
;
116 /* Sort rmap intents by AG. */
118 xfs_rmap_update_diff_items(
123 struct xfs_mount
*mp
= priv
;
124 struct xfs_rmap_intent
*ra
;
125 struct xfs_rmap_intent
*rb
;
127 ra
= container_of(a
, struct xfs_rmap_intent
, ri_list
);
128 rb
= container_of(b
, struct xfs_rmap_intent
, ri_list
);
129 return XFS_FSB_TO_AGNO(mp
, ra
->ri_bmap
.br_startblock
) -
130 XFS_FSB_TO_AGNO(mp
, rb
->ri_bmap
.br_startblock
);
135 xfs_rmap_update_create_intent(
136 struct xfs_trans
*tp
,
139 struct xfs_rui_log_item
*ruip
;
144 ruip
= xfs_rui_init(tp
->t_mountp
, count
);
145 ASSERT(ruip
!= NULL
);
148 * Get a log_item_desc to point at the new item.
150 xfs_trans_add_item(tp
, &ruip
->rui_item
);
154 /* Log rmap updates in the intent item. */
156 xfs_rmap_update_log_item(
157 struct xfs_trans
*tp
,
159 struct list_head
*item
)
161 struct xfs_rui_log_item
*ruip
= intent
;
162 struct xfs_rmap_intent
*rmap
;
164 struct xfs_map_extent
*map
;
166 rmap
= container_of(item
, struct xfs_rmap_intent
, ri_list
);
168 tp
->t_flags
|= XFS_TRANS_DIRTY
;
169 ruip
->rui_item
.li_desc
->lid_flags
|= XFS_LID_DIRTY
;
172 * atomic_inc_return gives us the value after the increment;
173 * we want to use it as an array index so we need to subtract 1 from
176 next_extent
= atomic_inc_return(&ruip
->rui_next_extent
) - 1;
177 ASSERT(next_extent
< ruip
->rui_format
.rui_nextents
);
178 map
= &ruip
->rui_format
.rui_extents
[next_extent
];
179 map
->me_owner
= rmap
->ri_owner
;
180 map
->me_startblock
= rmap
->ri_bmap
.br_startblock
;
181 map
->me_startoff
= rmap
->ri_bmap
.br_startoff
;
182 map
->me_len
= rmap
->ri_bmap
.br_blockcount
;
183 xfs_trans_set_rmap_flags(map
, rmap
->ri_type
, rmap
->ri_whichfork
,
184 rmap
->ri_bmap
.br_state
);
187 /* Get an RUD so we can process all the deferred rmap updates. */
189 xfs_rmap_update_create_done(
190 struct xfs_trans
*tp
,
194 return xfs_trans_get_rud(tp
, intent
);
197 /* Process a deferred rmap update. */
199 xfs_rmap_update_finish_item(
200 struct xfs_trans
*tp
,
201 struct xfs_defer_ops
*dop
,
202 struct list_head
*item
,
206 struct xfs_rmap_intent
*rmap
;
209 rmap
= container_of(item
, struct xfs_rmap_intent
, ri_list
);
210 error
= xfs_trans_log_finish_rmap_update(tp
, done_item
,
212 rmap
->ri_owner
, rmap
->ri_whichfork
,
213 rmap
->ri_bmap
.br_startoff
,
214 rmap
->ri_bmap
.br_startblock
,
215 rmap
->ri_bmap
.br_blockcount
,
216 rmap
->ri_bmap
.br_state
,
217 (struct xfs_btree_cur
**)state
);
222 /* Clean up after processing deferred rmaps. */
224 xfs_rmap_update_finish_cleanup(
225 struct xfs_trans
*tp
,
229 struct xfs_btree_cur
*rcur
= state
;
231 xfs_rmap_finish_one_cleanup(tp
, rcur
, error
);
234 /* Abort all pending RUIs. */
236 xfs_rmap_update_abort_intent(
239 xfs_rui_release(intent
);
242 /* Cancel a deferred rmap update. */
244 xfs_rmap_update_cancel_item(
245 struct list_head
*item
)
247 struct xfs_rmap_intent
*rmap
;
249 rmap
= container_of(item
, struct xfs_rmap_intent
, ri_list
);
253 static const struct xfs_defer_op_type xfs_rmap_update_defer_type
= {
254 .type
= XFS_DEFER_OPS_TYPE_RMAP
,
255 .max_items
= XFS_RUI_MAX_FAST_EXTENTS
,
256 .diff_items
= xfs_rmap_update_diff_items
,
257 .create_intent
= xfs_rmap_update_create_intent
,
258 .abort_intent
= xfs_rmap_update_abort_intent
,
259 .log_item
= xfs_rmap_update_log_item
,
260 .create_done
= xfs_rmap_update_create_done
,
261 .finish_item
= xfs_rmap_update_finish_item
,
262 .finish_cleanup
= xfs_rmap_update_finish_cleanup
,
263 .cancel_item
= xfs_rmap_update_cancel_item
,
266 /* Register the deferred op type. */
268 xfs_rmap_update_init_defer_op(void)
270 xfs_defer_init_op_type(&xfs_rmap_update_defer_type
);