xfs: create rmap update intent log items
[linux-2.6-block.git] / fs / xfs / xfs_rmap_item.c
CommitLineData
5880f2d7
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_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"
30#include "xfs_log.h"
31
32
33kmem_zone_t *xfs_rui_zone;
34kmem_zone_t *xfs_rud_zone;
35
36static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
37{
38 return container_of(lip, struct xfs_rui_log_item, rui_item);
39}
40
41void
42xfs_rui_item_free(
43 struct xfs_rui_log_item *ruip)
44{
45 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
46 kmem_free(ruip);
47 else
48 kmem_zone_free(xfs_rui_zone, ruip);
49}
50
51/*
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
54 * structure.
55 */
56static inline int
57xfs_rui_item_sizeof(
58 struct xfs_rui_log_item *ruip)
59{
60 return sizeof(struct xfs_rui_log_format) +
61 (ruip->rui_format.rui_nextents - 1) *
62 sizeof(struct xfs_map_extent);
63}
64
65STATIC void
66xfs_rui_item_size(
67 struct xfs_log_item *lip,
68 int *nvecs,
69 int *nbytes)
70{
71 *nvecs += 1;
72 *nbytes += xfs_rui_item_sizeof(RUI_ITEM(lip));
73}
74
75/*
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.
81 */
82STATIC void
83xfs_rui_item_format(
84 struct xfs_log_item *lip,
85 struct xfs_log_vec *lv)
86{
87 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
88 struct xfs_log_iovec *vecp = NULL;
89
90 ASSERT(atomic_read(&ruip->rui_next_extent) ==
91 ruip->rui_format.rui_nextents);
92
93 ruip->rui_format.rui_type = XFS_LI_RUI;
94 ruip->rui_format.rui_size = 1;
95
96 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
97 xfs_rui_item_sizeof(ruip));
98}
99
100/*
101 * Pinning has no meaning for an rui item, so just return.
102 */
103STATIC void
104xfs_rui_item_pin(
105 struct xfs_log_item *lip)
106{
107}
108
109/*
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.
116 */
117STATIC void
118xfs_rui_item_unpin(
119 struct xfs_log_item *lip,
120 int remove)
121{
122 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
123
124 xfs_rui_release(ruip);
125}
126
127/*
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
132 * the AIL.
133 */
134STATIC uint
135xfs_rui_item_push(
136 struct xfs_log_item *lip,
137 struct list_head *buffer_list)
138{
139 return XFS_ITEM_PINNED;
140}
141
142/*
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.
146 */
147STATIC void
148xfs_rui_item_unlock(
149 struct xfs_log_item *lip)
150{
151 if (lip->li_flags & XFS_LI_ABORTED)
152 xfs_rui_item_free(RUI_ITEM(lip));
153}
154
155/*
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.
158 */
159STATIC xfs_lsn_t
160xfs_rui_item_committed(
161 struct xfs_log_item *lip,
162 xfs_lsn_t lsn)
163{
164 return lsn;
165}
166
167/*
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.
173 */
174STATIC void
175xfs_rui_item_committing(
176 struct xfs_log_item *lip,
177 xfs_lsn_t lsn)
178{
179}
180
181/*
182 * This is the ops vector shared by all rui log items.
183 */
184static 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,
193};
194
195/*
196 * Allocate and initialize an rui item with the given number of extents.
197 */
198struct xfs_rui_log_item *
199xfs_rui_init(
200 struct xfs_mount *mp,
201 uint nextents)
202
203{
204 struct xfs_rui_log_item *ruip;
205 uint size;
206
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);
212 } else {
213 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
214 }
215
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);
221
222 return ruip;
223}
224
225/*
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.
229 */
230int
231xfs_rui_copy_format(
232 struct xfs_log_iovec *buf,
233 struct xfs_rui_log_format *dst_rui_fmt)
234{
235 struct xfs_rui_log_format *src_rui_fmt;
236 uint len;
237
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);
242
243 if (buf->i_len != len)
244 return -EFSCORRUPTED;
245
246 memcpy((char *)dst_rui_fmt, (char *)src_rui_fmt, len);
247 return 0;
248}
249
250/*
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.
256 */
257void
258xfs_rui_release(
259 struct xfs_rui_log_item *ruip)
260{
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);
264 }
265}
266
267static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
268{
269 return container_of(lip, struct xfs_rud_log_item, rud_item);
270}
271
272STATIC void
273xfs_rud_item_free(struct xfs_rud_log_item *rudp)
274{
275 if (rudp->rud_format.rud_nextents > XFS_RUD_MAX_FAST_EXTENTS)
276 kmem_free(rudp);
277 else
278 kmem_zone_free(xfs_rud_zone, rudp);
279}
280
281/*
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
284 * structure.
285 */
286static inline int
287xfs_rud_item_sizeof(
288 struct xfs_rud_log_item *rudp)
289{
290 return sizeof(struct xfs_rud_log_format) +
291 (rudp->rud_format.rud_nextents - 1) *
292 sizeof(struct xfs_map_extent);
293}
294
295STATIC void
296xfs_rud_item_size(
297 struct xfs_log_item *lip,
298 int *nvecs,
299 int *nbytes)
300{
301 *nvecs += 1;
302 *nbytes += xfs_rud_item_sizeof(RUD_ITEM(lip));
303}
304
305/*
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.
311 */
312STATIC void
313xfs_rud_item_format(
314 struct xfs_log_item *lip,
315 struct xfs_log_vec *lv)
316{
317 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
318 struct xfs_log_iovec *vecp = NULL;
319
320 ASSERT(rudp->rud_next_extent == rudp->rud_format.rud_nextents);
321
322 rudp->rud_format.rud_type = XFS_LI_RUD;
323 rudp->rud_format.rud_size = 1;
324
325 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
326 xfs_rud_item_sizeof(rudp));
327}
328
329/*
330 * Pinning has no meaning for an rud item, so just return.
331 */
332STATIC void
333xfs_rud_item_pin(
334 struct xfs_log_item *lip)
335{
336}
337
338/*
339 * Since pinning has no meaning for an rud item, unpinning does
340 * not either.
341 */
342STATIC void
343xfs_rud_item_unpin(
344 struct xfs_log_item *lip,
345 int remove)
346{
347}
348
349/*
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.
352 */
353STATIC uint
354xfs_rud_item_push(
355 struct xfs_log_item *lip,
356 struct list_head *buffer_list)
357{
358 return XFS_ITEM_PINNED;
359}
360
361/*
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
364 * RUD.
365 */
366STATIC void
367xfs_rud_item_unlock(
368 struct xfs_log_item *lip)
369{
370 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
371
372 if (lip->li_flags & XFS_LI_ABORTED) {
373 xfs_rui_release(rudp->rud_ruip);
374 xfs_rud_item_free(rudp);
375 }
376}
377
378/*
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.
383 */
384STATIC xfs_lsn_t
385xfs_rud_item_committed(
386 struct xfs_log_item *lip,
387 xfs_lsn_t lsn)
388{
389 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
390
391 /*
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).
396 */
397 xfs_rui_release(rudp->rud_ruip);
398 xfs_rud_item_free(rudp);
399
400 return (xfs_lsn_t)-1;
401}
402
403/*
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.
409 */
410STATIC void
411xfs_rud_item_committing(
412 struct xfs_log_item *lip,
413 xfs_lsn_t lsn)
414{
415}
416
417/*
418 * This is the ops vector shared by all rud log items.
419 */
420static 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,
429};
430
431/*
432 * Allocate and initialize an rud item with the given number of extents.
433 */
434struct xfs_rud_log_item *
435xfs_rud_init(
436 struct xfs_mount *mp,
437 struct xfs_rui_log_item *ruip,
438 uint nextents)
439
440{
441 struct xfs_rud_log_item *rudp;
442 uint size;
443
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);
449 } else {
450 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
451 }
452
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;
457
458 return rudp;
459}