xfs: in btree_lshift, only allocate temporary cursor when needed
[linux-2.6-block.git] / fs / xfs / xfs_trans_rmap.c
CommitLineData
9e88b5d8
DW
1/*
2 * Copyright (C) 2016 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
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.
10 *
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.
15 *
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.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
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"
32#include "xfs_rmap.h"
33
34/*
35 * This routine is called to allocate an "rmap update intent"
36 * log item that will hold nextents worth of extents. The
37 * caller must use all nextents extents, because we are not
38 * flexible about this at all.
39 */
f8dbebef 40STATIC struct xfs_rui_log_item *
9e88b5d8
DW
41xfs_trans_get_rui(
42 struct xfs_trans *tp,
43 uint nextents)
44{
45 struct xfs_rui_log_item *ruip;
46
47 ASSERT(tp != NULL);
48 ASSERT(nextents > 0);
49
50 ruip = xfs_rui_init(tp->t_mountp, nextents);
51 ASSERT(ruip != NULL);
52
53 /*
54 * Get a log_item_desc to point at the new item.
55 */
56 xfs_trans_add_item(tp, &ruip->rui_item);
57 return ruip;
58}
59
60/* Set the map extent flags for this reverse mapping. */
61static void
62xfs_trans_set_rmap_flags(
63 struct xfs_map_extent *rmap,
64 enum xfs_rmap_intent_type type,
65 int whichfork,
66 xfs_exntst_t state)
67{
68 rmap->me_flags = 0;
69 if (state == XFS_EXT_UNWRITTEN)
70 rmap->me_flags |= XFS_RMAP_EXTENT_UNWRITTEN;
71 if (whichfork == XFS_ATTR_FORK)
72 rmap->me_flags |= XFS_RMAP_EXTENT_ATTR_FORK;
73 switch (type) {
74 case XFS_RMAP_MAP:
75 rmap->me_flags |= XFS_RMAP_EXTENT_MAP;
76 break;
77 case XFS_RMAP_UNMAP:
78 rmap->me_flags |= XFS_RMAP_EXTENT_UNMAP;
79 break;
80 case XFS_RMAP_CONVERT:
81 rmap->me_flags |= XFS_RMAP_EXTENT_CONVERT;
82 break;
83 case XFS_RMAP_ALLOC:
84 rmap->me_flags |= XFS_RMAP_EXTENT_ALLOC;
85 break;
86 case XFS_RMAP_FREE:
87 rmap->me_flags |= XFS_RMAP_EXTENT_FREE;
88 break;
89 default:
90 ASSERT(0);
91 }
92}
93
94/*
95 * This routine is called to indicate that the described reverse
96 * mapping is to be logged as needing to be updated. It should be
97 * called once for each mapping.
98 */
f8dbebef 99STATIC void
9e88b5d8
DW
100xfs_trans_log_start_rmap_update(
101 struct xfs_trans *tp,
102 struct xfs_rui_log_item *ruip,
103 enum xfs_rmap_intent_type type,
104 __uint64_t owner,
105 int whichfork,
106 xfs_fileoff_t startoff,
107 xfs_fsblock_t startblock,
108 xfs_filblks_t blockcount,
109 xfs_exntst_t state)
110{
111 uint next_extent;
112 struct xfs_map_extent *rmap;
113
114 tp->t_flags |= XFS_TRANS_DIRTY;
115 ruip->rui_item.li_desc->lid_flags |= XFS_LID_DIRTY;
116
117 /*
118 * atomic_inc_return gives us the value after the increment;
119 * we want to use it as an array index so we need to subtract 1 from
120 * it.
121 */
122 next_extent = atomic_inc_return(&ruip->rui_next_extent) - 1;
123 ASSERT(next_extent < ruip->rui_format.rui_nextents);
124 rmap = &(ruip->rui_format.rui_extents[next_extent]);
125 rmap->me_owner = owner;
126 rmap->me_startblock = startblock;
127 rmap->me_startoff = startoff;
128 rmap->me_len = blockcount;
129 xfs_trans_set_rmap_flags(rmap, type, whichfork, state);
130}
131
9e88b5d8
DW
132/*
133 * This routine is called to allocate an "rmap update done"
134 * log item that will hold nextents worth of extents. The
135 * caller must use all nextents extents, because we are not
136 * flexible about this at all.
137 */
138struct xfs_rud_log_item *
139xfs_trans_get_rud(
140 struct xfs_trans *tp,
141 struct xfs_rui_log_item *ruip,
142 uint nextents)
143{
144 struct xfs_rud_log_item *rudp;
145
146 ASSERT(tp != NULL);
147 ASSERT(nextents > 0);
148
149 rudp = xfs_rud_init(tp->t_mountp, ruip, nextents);
150 ASSERT(rudp != NULL);
151
152 /*
153 * Get a log_item_desc to point at the new item.
154 */
155 xfs_trans_add_item(tp, &rudp->rud_item);
156 return rudp;
157}
158
159/*
160 * Finish an rmap update and log it to the RUD. Note that the transaction is
161 * marked dirty regardless of whether the rmap update succeeds or fails to
162 * support the RUI/RUD lifecycle rules.
163 */
164int
165xfs_trans_log_finish_rmap_update(
166 struct xfs_trans *tp,
167 struct xfs_rud_log_item *rudp,
168 enum xfs_rmap_intent_type type,
169 __uint64_t owner,
170 int whichfork,
171 xfs_fileoff_t startoff,
172 xfs_fsblock_t startblock,
173 xfs_filblks_t blockcount,
9c194644
DW
174 xfs_exntst_t state,
175 struct xfs_btree_cur **pcur)
9e88b5d8
DW
176{
177 uint next_extent;
178 struct xfs_map_extent *rmap;
179 int error;
180
9c194644
DW
181 error = xfs_rmap_finish_one(tp, type, owner, whichfork, startoff,
182 startblock, blockcount, state, pcur);
9e88b5d8
DW
183
184 /*
185 * Mark the transaction dirty, even on error. This ensures the
186 * transaction is aborted, which:
187 *
188 * 1.) releases the RUI and frees the RUD
189 * 2.) shuts down the filesystem
190 */
191 tp->t_flags |= XFS_TRANS_DIRTY;
192 rudp->rud_item.li_desc->lid_flags |= XFS_LID_DIRTY;
193
194 next_extent = rudp->rud_next_extent;
195 ASSERT(next_extent < rudp->rud_format.rud_nextents);
196 rmap = &(rudp->rud_format.rud_extents[next_extent]);
197 rmap->me_owner = owner;
198 rmap->me_startblock = startblock;
199 rmap->me_startoff = startoff;
200 rmap->me_len = blockcount;
201 xfs_trans_set_rmap_flags(rmap, type, whichfork, state);
202 rudp->rud_next_extent++;
203
204 return error;
205}
f8dbebef
DW
206
207/* Sort rmap intents by AG. */
208static int
209xfs_rmap_update_diff_items(
210 void *priv,
211 struct list_head *a,
212 struct list_head *b)
213{
214 struct xfs_mount *mp = priv;
215 struct xfs_rmap_intent *ra;
216 struct xfs_rmap_intent *rb;
217
218 ra = container_of(a, struct xfs_rmap_intent, ri_list);
219 rb = container_of(b, struct xfs_rmap_intent, ri_list);
220 return XFS_FSB_TO_AGNO(mp, ra->ri_bmap.br_startblock) -
221 XFS_FSB_TO_AGNO(mp, rb->ri_bmap.br_startblock);
222}
223
224/* Get an RUI. */
225STATIC void *
226xfs_rmap_update_create_intent(
227 struct xfs_trans *tp,
228 unsigned int count)
229{
230 return xfs_trans_get_rui(tp, count);
231}
232
233/* Log rmap updates in the intent item. */
234STATIC void
235xfs_rmap_update_log_item(
236 struct xfs_trans *tp,
237 void *intent,
238 struct list_head *item)
239{
240 struct xfs_rmap_intent *rmap;
241
242 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
243 xfs_trans_log_start_rmap_update(tp, intent, rmap->ri_type,
244 rmap->ri_owner, rmap->ri_whichfork,
245 rmap->ri_bmap.br_startoff,
246 rmap->ri_bmap.br_startblock,
247 rmap->ri_bmap.br_blockcount,
248 rmap->ri_bmap.br_state);
249}
250
251/* Get an RUD so we can process all the deferred rmap updates. */
252STATIC void *
253xfs_rmap_update_create_done(
254 struct xfs_trans *tp,
255 void *intent,
256 unsigned int count)
257{
258 return xfs_trans_get_rud(tp, intent, count);
259}
260
261/* Process a deferred rmap update. */
262STATIC int
263xfs_rmap_update_finish_item(
264 struct xfs_trans *tp,
265 struct xfs_defer_ops *dop,
266 struct list_head *item,
267 void *done_item,
268 void **state)
269{
270 struct xfs_rmap_intent *rmap;
271 int error;
272
273 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
274 error = xfs_trans_log_finish_rmap_update(tp, done_item,
275 rmap->ri_type,
276 rmap->ri_owner, rmap->ri_whichfork,
277 rmap->ri_bmap.br_startoff,
278 rmap->ri_bmap.br_startblock,
279 rmap->ri_bmap.br_blockcount,
9c194644
DW
280 rmap->ri_bmap.br_state,
281 (struct xfs_btree_cur **)state);
f8dbebef
DW
282 kmem_free(rmap);
283 return error;
284}
285
286/* Clean up after processing deferred rmaps. */
287STATIC void
288xfs_rmap_update_finish_cleanup(
289 struct xfs_trans *tp,
290 void *state,
291 int error)
292{
9c194644
DW
293 struct xfs_btree_cur *rcur = state;
294
295 xfs_rmap_finish_one_cleanup(tp, rcur, error);
f8dbebef
DW
296}
297
298/* Abort all pending RUIs. */
299STATIC void
300xfs_rmap_update_abort_intent(
301 void *intent)
302{
303 xfs_rui_release(intent);
304}
305
306/* Cancel a deferred rmap update. */
307STATIC void
308xfs_rmap_update_cancel_item(
309 struct list_head *item)
310{
311 struct xfs_rmap_intent *rmap;
312
313 rmap = container_of(item, struct xfs_rmap_intent, ri_list);
314 kmem_free(rmap);
315}
316
317static const struct xfs_defer_op_type xfs_rmap_update_defer_type = {
318 .type = XFS_DEFER_OPS_TYPE_RMAP,
319 .max_items = XFS_RUI_MAX_FAST_EXTENTS,
320 .diff_items = xfs_rmap_update_diff_items,
321 .create_intent = xfs_rmap_update_create_intent,
322 .abort_intent = xfs_rmap_update_abort_intent,
323 .log_item = xfs_rmap_update_log_item,
324 .create_done = xfs_rmap_update_create_done,
325 .finish_item = xfs_rmap_update_finish_item,
326 .finish_cleanup = xfs_rmap_update_finish_cleanup,
327 .cancel_item = xfs_rmap_update_cancel_item,
328};
329
330/* Register the deferred op type. */
331void
332xfs_rmap_update_init_defer_op(void)
333{
334 xfs_defer_init_op_type(&xfs_rmap_update_defer_type);
335}