]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/xfs/xfs_rmap_item.c
xfs: enable the xfs_defer mechanism to process rmaps to update
[mirror_ubuntu-bionic-kernel.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"
9e88b5d8 25#include "xfs_bit.h"
5880f2d7
DW
26#include "xfs_mount.h"
27#include "xfs_trans.h"
28#include "xfs_trans_priv.h"
29#include "xfs_buf_item.h"
30#include "xfs_rmap_item.h"
31#include "xfs_log.h"
32
33
34kmem_zone_t *xfs_rui_zone;
35kmem_zone_t *xfs_rud_zone;
36
37static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip)
38{
39 return container_of(lip, struct xfs_rui_log_item, rui_item);
40}
41
42void
43xfs_rui_item_free(
44 struct xfs_rui_log_item *ruip)
45{
46 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS)
47 kmem_free(ruip);
48 else
49 kmem_zone_free(xfs_rui_zone, ruip);
50}
51
52/*
53 * This returns the number of iovecs needed to log the given rui item.
54 * We only need 1 iovec for an rui item. It just logs the rui_log_format
55 * structure.
56 */
57static inline int
58xfs_rui_item_sizeof(
59 struct xfs_rui_log_item *ruip)
60{
61 return sizeof(struct xfs_rui_log_format) +
62 (ruip->rui_format.rui_nextents - 1) *
63 sizeof(struct xfs_map_extent);
64}
65
66STATIC void
67xfs_rui_item_size(
68 struct xfs_log_item *lip,
69 int *nvecs,
70 int *nbytes)
71{
72 *nvecs += 1;
73 *nbytes += xfs_rui_item_sizeof(RUI_ITEM(lip));
74}
75
76/*
77 * This is called to fill in the vector of log iovecs for the
78 * given rui log item. We use only 1 iovec, and we point that
79 * at the rui_log_format structure embedded in the rui item.
80 * It is at this point that we assert that all of the extent
81 * slots in the rui item have been filled.
82 */
83STATIC void
84xfs_rui_item_format(
85 struct xfs_log_item *lip,
86 struct xfs_log_vec *lv)
87{
88 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
89 struct xfs_log_iovec *vecp = NULL;
90
91 ASSERT(atomic_read(&ruip->rui_next_extent) ==
92 ruip->rui_format.rui_nextents);
93
94 ruip->rui_format.rui_type = XFS_LI_RUI;
95 ruip->rui_format.rui_size = 1;
96
97 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format,
98 xfs_rui_item_sizeof(ruip));
99}
100
101/*
102 * Pinning has no meaning for an rui item, so just return.
103 */
104STATIC void
105xfs_rui_item_pin(
106 struct xfs_log_item *lip)
107{
108}
109
110/*
111 * The unpin operation is the last place an RUI is manipulated in the log. It is
112 * either inserted in the AIL or aborted in the event of a log I/O error. In
113 * either case, the RUI transaction has been successfully committed to make it
114 * this far. Therefore, we expect whoever committed the RUI to either construct
115 * and commit the RUD or drop the RUD's reference in the event of error. Simply
116 * drop the log's RUI reference now that the log is done with it.
117 */
118STATIC void
119xfs_rui_item_unpin(
120 struct xfs_log_item *lip,
121 int remove)
122{
123 struct xfs_rui_log_item *ruip = RUI_ITEM(lip);
124
125 xfs_rui_release(ruip);
126}
127
128/*
129 * RUI items have no locking or pushing. However, since RUIs are pulled from
130 * the AIL when their corresponding RUDs are committed to disk, their situation
131 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
132 * will eventually flush the log. This should help in getting the RUI out of
133 * the AIL.
134 */
135STATIC uint
136xfs_rui_item_push(
137 struct xfs_log_item *lip,
138 struct list_head *buffer_list)
139{
140 return XFS_ITEM_PINNED;
141}
142
143/*
144 * The RUI has been either committed or aborted if the transaction has been
145 * cancelled. If the transaction was cancelled, an RUD isn't going to be
146 * constructed and thus we free the RUI here directly.
147 */
148STATIC void
149xfs_rui_item_unlock(
150 struct xfs_log_item *lip)
151{
152 if (lip->li_flags & XFS_LI_ABORTED)
153 xfs_rui_item_free(RUI_ITEM(lip));
154}
155
156/*
157 * The RUI is logged only once and cannot be moved in the log, so simply return
158 * the lsn at which it's been logged.
159 */
160STATIC xfs_lsn_t
161xfs_rui_item_committed(
162 struct xfs_log_item *lip,
163 xfs_lsn_t lsn)
164{
165 return lsn;
166}
167
168/*
169 * The RUI dependency tracking op doesn't do squat. It can't because
170 * it doesn't know where the free extent is coming from. The dependency
171 * tracking has to be handled by the "enclosing" metadata object. For
172 * example, for inodes, the inode is locked throughout the extent freeing
173 * so the dependency should be recorded there.
174 */
175STATIC void
176xfs_rui_item_committing(
177 struct xfs_log_item *lip,
178 xfs_lsn_t lsn)
179{
180}
181
182/*
183 * This is the ops vector shared by all rui log items.
184 */
185static const struct xfs_item_ops xfs_rui_item_ops = {
186 .iop_size = xfs_rui_item_size,
187 .iop_format = xfs_rui_item_format,
188 .iop_pin = xfs_rui_item_pin,
189 .iop_unpin = xfs_rui_item_unpin,
190 .iop_unlock = xfs_rui_item_unlock,
191 .iop_committed = xfs_rui_item_committed,
192 .iop_push = xfs_rui_item_push,
193 .iop_committing = xfs_rui_item_committing,
194};
195
196/*
197 * Allocate and initialize an rui item with the given number of extents.
198 */
199struct xfs_rui_log_item *
200xfs_rui_init(
201 struct xfs_mount *mp,
202 uint nextents)
203
204{
205 struct xfs_rui_log_item *ruip;
206 uint size;
207
208 ASSERT(nextents > 0);
209 if (nextents > XFS_RUI_MAX_FAST_EXTENTS) {
210 size = (uint)(sizeof(struct xfs_rui_log_item) +
211 ((nextents - 1) * sizeof(struct xfs_map_extent)));
212 ruip = kmem_zalloc(size, KM_SLEEP);
213 } else {
214 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP);
215 }
216
217 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops);
218 ruip->rui_format.rui_nextents = nextents;
219 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip;
220 atomic_set(&ruip->rui_next_extent, 0);
221 atomic_set(&ruip->rui_refcount, 2);
222
223 return ruip;
224}
225
226/*
227 * Copy an RUI format buffer from the given buf, and into the destination
228 * RUI format structure. The RUI/RUD items were designed not to need any
229 * special alignment handling.
230 */
231int
232xfs_rui_copy_format(
233 struct xfs_log_iovec *buf,
234 struct xfs_rui_log_format *dst_rui_fmt)
235{
236 struct xfs_rui_log_format *src_rui_fmt;
237 uint len;
238
239 src_rui_fmt = buf->i_addr;
240 len = sizeof(struct xfs_rui_log_format) +
241 (src_rui_fmt->rui_nextents - 1) *
242 sizeof(struct xfs_map_extent);
243
244 if (buf->i_len != len)
245 return -EFSCORRUPTED;
246
247 memcpy((char *)dst_rui_fmt, (char *)src_rui_fmt, len);
248 return 0;
249}
250
251/*
252 * Freeing the RUI requires that we remove it from the AIL if it has already
253 * been placed there. However, the RUI may not yet have been placed in the AIL
254 * when called by xfs_rui_release() from RUD processing due to the ordering of
255 * committed vs unpin operations in bulk insert operations. Hence the reference
256 * count to ensure only the last caller frees the RUI.
257 */
258void
259xfs_rui_release(
260 struct xfs_rui_log_item *ruip)
261{
262 if (atomic_dec_and_test(&ruip->rui_refcount)) {
263 xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR);
264 xfs_rui_item_free(ruip);
265 }
266}
267
268static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip)
269{
270 return container_of(lip, struct xfs_rud_log_item, rud_item);
271}
272
273STATIC void
274xfs_rud_item_free(struct xfs_rud_log_item *rudp)
275{
276 if (rudp->rud_format.rud_nextents > XFS_RUD_MAX_FAST_EXTENTS)
277 kmem_free(rudp);
278 else
279 kmem_zone_free(xfs_rud_zone, rudp);
280}
281
282/*
283 * This returns the number of iovecs needed to log the given rud item.
284 * We only need 1 iovec for an rud item. It just logs the rud_log_format
285 * structure.
286 */
287static inline int
288xfs_rud_item_sizeof(
289 struct xfs_rud_log_item *rudp)
290{
291 return sizeof(struct xfs_rud_log_format) +
292 (rudp->rud_format.rud_nextents - 1) *
293 sizeof(struct xfs_map_extent);
294}
295
296STATIC void
297xfs_rud_item_size(
298 struct xfs_log_item *lip,
299 int *nvecs,
300 int *nbytes)
301{
302 *nvecs += 1;
303 *nbytes += xfs_rud_item_sizeof(RUD_ITEM(lip));
304}
305
306/*
307 * This is called to fill in the vector of log iovecs for the
308 * given rud log item. We use only 1 iovec, and we point that
309 * at the rud_log_format structure embedded in the rud item.
310 * It is at this point that we assert that all of the extent
311 * slots in the rud item have been filled.
312 */
313STATIC void
314xfs_rud_item_format(
315 struct xfs_log_item *lip,
316 struct xfs_log_vec *lv)
317{
318 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
319 struct xfs_log_iovec *vecp = NULL;
320
321 ASSERT(rudp->rud_next_extent == rudp->rud_format.rud_nextents);
322
323 rudp->rud_format.rud_type = XFS_LI_RUD;
324 rudp->rud_format.rud_size = 1;
325
326 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format,
327 xfs_rud_item_sizeof(rudp));
328}
329
330/*
331 * Pinning has no meaning for an rud item, so just return.
332 */
333STATIC void
334xfs_rud_item_pin(
335 struct xfs_log_item *lip)
336{
337}
338
339/*
340 * Since pinning has no meaning for an rud item, unpinning does
341 * not either.
342 */
343STATIC void
344xfs_rud_item_unpin(
345 struct xfs_log_item *lip,
346 int remove)
347{
348}
349
350/*
351 * There isn't much you can do to push on an rud item. It is simply stuck
352 * waiting for the log to be flushed to disk.
353 */
354STATIC uint
355xfs_rud_item_push(
356 struct xfs_log_item *lip,
357 struct list_head *buffer_list)
358{
359 return XFS_ITEM_PINNED;
360}
361
362/*
363 * The RUD is either committed or aborted if the transaction is cancelled. If
364 * the transaction is cancelled, drop our reference to the RUI and free the
365 * RUD.
366 */
367STATIC void
368xfs_rud_item_unlock(
369 struct xfs_log_item *lip)
370{
371 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
372
373 if (lip->li_flags & XFS_LI_ABORTED) {
374 xfs_rui_release(rudp->rud_ruip);
375 xfs_rud_item_free(rudp);
376 }
377}
378
379/*
380 * When the rud item is committed to disk, all we need to do is delete our
381 * reference to our partner rui item and then free ourselves. Since we're
382 * freeing ourselves we must return -1 to keep the transaction code from
383 * further referencing this item.
384 */
385STATIC xfs_lsn_t
386xfs_rud_item_committed(
387 struct xfs_log_item *lip,
388 xfs_lsn_t lsn)
389{
390 struct xfs_rud_log_item *rudp = RUD_ITEM(lip);
391
392 /*
393 * Drop the RUI reference regardless of whether the RUD has been
394 * aborted. Once the RUD transaction is constructed, it is the sole
395 * responsibility of the RUD to release the RUI (even if the RUI is
396 * aborted due to log I/O error).
397 */
398 xfs_rui_release(rudp->rud_ruip);
399 xfs_rud_item_free(rudp);
400
401 return (xfs_lsn_t)-1;
402}
403
404/*
405 * The RUD dependency tracking op doesn't do squat. It can't because
406 * it doesn't know where the free extent is coming from. The dependency
407 * tracking has to be handled by the "enclosing" metadata object. For
408 * example, for inodes, the inode is locked throughout the extent freeing
409 * so the dependency should be recorded there.
410 */
411STATIC void
412xfs_rud_item_committing(
413 struct xfs_log_item *lip,
414 xfs_lsn_t lsn)
415{
416}
417
418/*
419 * This is the ops vector shared by all rud log items.
420 */
421static const struct xfs_item_ops xfs_rud_item_ops = {
422 .iop_size = xfs_rud_item_size,
423 .iop_format = xfs_rud_item_format,
424 .iop_pin = xfs_rud_item_pin,
425 .iop_unpin = xfs_rud_item_unpin,
426 .iop_unlock = xfs_rud_item_unlock,
427 .iop_committed = xfs_rud_item_committed,
428 .iop_push = xfs_rud_item_push,
429 .iop_committing = xfs_rud_item_committing,
430};
431
432/*
433 * Allocate and initialize an rud item with the given number of extents.
434 */
435struct xfs_rud_log_item *
436xfs_rud_init(
437 struct xfs_mount *mp,
438 struct xfs_rui_log_item *ruip,
439 uint nextents)
440
441{
442 struct xfs_rud_log_item *rudp;
443 uint size;
444
445 ASSERT(nextents > 0);
446 if (nextents > XFS_RUD_MAX_FAST_EXTENTS) {
447 size = (uint)(sizeof(struct xfs_rud_log_item) +
448 ((nextents - 1) * sizeof(struct xfs_map_extent)));
449 rudp = kmem_zalloc(size, KM_SLEEP);
450 } else {
451 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP);
452 }
453
454 xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops);
455 rudp->rud_ruip = ruip;
456 rudp->rud_format.rud_nextents = nextents;
457 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id;
458
459 return rudp;
460}
9e88b5d8
DW
461
462/*
463 * Process an rmap update intent item that was recovered from the log.
464 * We need to update the rmapbt.
465 */
466int
467xfs_rui_recover(
468 struct xfs_mount *mp,
469 struct xfs_rui_log_item *ruip)
470{
471 int i;
472 int error = 0;
473 struct xfs_map_extent *rmap;
474 xfs_fsblock_t startblock_fsb;
475 bool op_ok;
476
477 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags));
478
479 /*
480 * First check the validity of the extents described by the
481 * RUI. If any are bad, then assume that all are bad and
482 * just toss the RUI.
483 */
484 for (i = 0; i < ruip->rui_format.rui_nextents; i++) {
485 rmap = &(ruip->rui_format.rui_extents[i]);
486 startblock_fsb = XFS_BB_TO_FSB(mp,
487 XFS_FSB_TO_DADDR(mp, rmap->me_startblock));
488 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) {
489 case XFS_RMAP_EXTENT_MAP:
490 case XFS_RMAP_EXTENT_UNMAP:
491 case XFS_RMAP_EXTENT_CONVERT:
492 case XFS_RMAP_EXTENT_ALLOC:
493 case XFS_RMAP_EXTENT_FREE:
494 op_ok = true;
495 break;
496 default:
497 op_ok = false;
498 break;
499 }
500 if (!op_ok || (startblock_fsb == 0) ||
501 (rmap->me_len == 0) ||
502 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
503 (rmap->me_len >= mp->m_sb.sb_agblocks) ||
504 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) {
505 /*
506 * This will pull the RUI from the AIL and
507 * free the memory associated with it.
508 */
509 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
510 xfs_rui_release(ruip);
511 return -EIO;
512 }
513 }
514
515 /* XXX: do nothing for now */
516 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags);
517 xfs_rui_release(ruip);
518 return error;
519}