blob: fe86a668a57e70419e7fc6e5f126c0aa97cecb96 [file] [log] [blame]
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -07001/*
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"
Darrick J. Wongf997ee22016-10-03 09:11:21 -070025#include "xfs_bit.h"
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -070026#include "xfs_mount.h"
Darrick J. Wongf997ee22016-10-03 09:11:21 -070027#include "xfs_defer.h"
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -070028#include "xfs_trans.h"
29#include "xfs_trans_priv.h"
30#include "xfs_buf_item.h"
31#include "xfs_refcount_item.h"
32#include "xfs_log.h"
Darrick J. Wongf997ee22016-10-03 09:11:21 -070033#include "xfs_refcount.h"
Darrick J. Wongbaf4bcac2016-10-03 09:11:20 -070034
35
36kmem_zone_t *xfs_cui_zone;
37kmem_zone_t *xfs_cud_zone;
38
39static inline struct xfs_cui_log_item *CUI_ITEM(struct xfs_log_item *lip)
40{
41 return container_of(lip, struct xfs_cui_log_item, cui_item);
42}
43
44void
45xfs_cui_item_free(
46 struct xfs_cui_log_item *cuip)
47{
48 if (cuip->cui_format.cui_nextents > XFS_CUI_MAX_FAST_EXTENTS)
49 kmem_free(cuip);
50 else
51 kmem_zone_free(xfs_cui_zone, cuip);
52}
53
54STATIC void
55xfs_cui_item_size(
56 struct xfs_log_item *lip,
57 int *nvecs,
58 int *nbytes)
59{
60 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
61
62 *nvecs += 1;
63 *nbytes += xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents);
64}
65
66/*
67 * This is called to fill in the vector of log iovecs for the
68 * given cui log item. We use only 1 iovec, and we point that
69 * at the cui_log_format structure embedded in the cui item.
70 * It is at this point that we assert that all of the extent
71 * slots in the cui item have been filled.
72 */
73STATIC void
74xfs_cui_item_format(
75 struct xfs_log_item *lip,
76 struct xfs_log_vec *lv)
77{
78 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
79 struct xfs_log_iovec *vecp = NULL;
80
81 ASSERT(atomic_read(&cuip->cui_next_extent) ==
82 cuip->cui_format.cui_nextents);
83
84 cuip->cui_format.cui_type = XFS_LI_CUI;
85 cuip->cui_format.cui_size = 1;
86
87 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUI_FORMAT, &cuip->cui_format,
88 xfs_cui_log_format_sizeof(cuip->cui_format.cui_nextents));
89}
90
91/*
92 * Pinning has no meaning for an cui item, so just return.
93 */
94STATIC void
95xfs_cui_item_pin(
96 struct xfs_log_item *lip)
97{
98}
99
100/*
101 * The unpin operation is the last place an CUI is manipulated in the log. It is
102 * either inserted in the AIL or aborted in the event of a log I/O error. In
103 * either case, the CUI transaction has been successfully committed to make it
104 * this far. Therefore, we expect whoever committed the CUI to either construct
105 * and commit the CUD or drop the CUD's reference in the event of error. Simply
106 * drop the log's CUI reference now that the log is done with it.
107 */
108STATIC void
109xfs_cui_item_unpin(
110 struct xfs_log_item *lip,
111 int remove)
112{
113 struct xfs_cui_log_item *cuip = CUI_ITEM(lip);
114
115 xfs_cui_release(cuip);
116}
117
118/*
119 * CUI items have no locking or pushing. However, since CUIs are pulled from
120 * the AIL when their corresponding CUDs are committed to disk, their situation
121 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller
122 * will eventually flush the log. This should help in getting the CUI out of
123 * the AIL.
124 */
125STATIC uint
126xfs_cui_item_push(
127 struct xfs_log_item *lip,
128 struct list_head *buffer_list)
129{
130 return XFS_ITEM_PINNED;
131}
132
133/*
134 * The CUI has been either committed or aborted if the transaction has been
135 * cancelled. If the transaction was cancelled, an CUD isn't going to be
136 * constructed and thus we free the CUI here directly.
137 */
138STATIC void
139xfs_cui_item_unlock(
140 struct xfs_log_item *lip)
141{
142 if (lip->li_flags & XFS_LI_ABORTED)
143 xfs_cui_item_free(CUI_ITEM(lip));
144}
145
146/*
147 * The CUI is logged only once and cannot be moved in the log, so simply return
148 * the lsn at which it's been logged.
149 */
150STATIC xfs_lsn_t
151xfs_cui_item_committed(
152 struct xfs_log_item *lip,
153 xfs_lsn_t lsn)
154{
155 return lsn;
156}
157
158/*
159 * The CUI dependency tracking op doesn't do squat. It can't because
160 * it doesn't know where the free extent is coming from. The dependency
161 * tracking has to be handled by the "enclosing" metadata object. For
162 * example, for inodes, the inode is locked throughout the extent freeing
163 * so the dependency should be recorded there.
164 */
165STATIC void
166xfs_cui_item_committing(
167 struct xfs_log_item *lip,
168 xfs_lsn_t lsn)
169{
170}
171
172/*
173 * This is the ops vector shared by all cui log items.
174 */
175static const struct xfs_item_ops xfs_cui_item_ops = {
176 .iop_size = xfs_cui_item_size,
177 .iop_format = xfs_cui_item_format,
178 .iop_pin = xfs_cui_item_pin,
179 .iop_unpin = xfs_cui_item_unpin,
180 .iop_unlock = xfs_cui_item_unlock,
181 .iop_committed = xfs_cui_item_committed,
182 .iop_push = xfs_cui_item_push,
183 .iop_committing = xfs_cui_item_committing,
184};
185
186/*
187 * Allocate and initialize an cui item with the given number of extents.
188 */
189struct xfs_cui_log_item *
190xfs_cui_init(
191 struct xfs_mount *mp,
192 uint nextents)
193
194{
195 struct xfs_cui_log_item *cuip;
196
197 ASSERT(nextents > 0);
198 if (nextents > XFS_CUI_MAX_FAST_EXTENTS)
199 cuip = kmem_zalloc(xfs_cui_log_item_sizeof(nextents),
200 KM_SLEEP);
201 else
202 cuip = kmem_zone_zalloc(xfs_cui_zone, KM_SLEEP);
203
204 xfs_log_item_init(mp, &cuip->cui_item, XFS_LI_CUI, &xfs_cui_item_ops);
205 cuip->cui_format.cui_nextents = nextents;
206 cuip->cui_format.cui_id = (uintptr_t)(void *)cuip;
207 atomic_set(&cuip->cui_next_extent, 0);
208 atomic_set(&cuip->cui_refcount, 2);
209
210 return cuip;
211}
212
213/*
214 * Freeing the CUI requires that we remove it from the AIL if it has already
215 * been placed there. However, the CUI may not yet have been placed in the AIL
216 * when called by xfs_cui_release() from CUD processing due to the ordering of
217 * committed vs unpin operations in bulk insert operations. Hence the reference
218 * count to ensure only the last caller frees the CUI.
219 */
220void
221xfs_cui_release(
222 struct xfs_cui_log_item *cuip)
223{
224 if (atomic_dec_and_test(&cuip->cui_refcount)) {
225 xfs_trans_ail_remove(&cuip->cui_item, SHUTDOWN_LOG_IO_ERROR);
226 xfs_cui_item_free(cuip);
227 }
228}
229
230static inline struct xfs_cud_log_item *CUD_ITEM(struct xfs_log_item *lip)
231{
232 return container_of(lip, struct xfs_cud_log_item, cud_item);
233}
234
235STATIC void
236xfs_cud_item_size(
237 struct xfs_log_item *lip,
238 int *nvecs,
239 int *nbytes)
240{
241 *nvecs += 1;
242 *nbytes += sizeof(struct xfs_cud_log_format);
243}
244
245/*
246 * This is called to fill in the vector of log iovecs for the
247 * given cud log item. We use only 1 iovec, and we point that
248 * at the cud_log_format structure embedded in the cud item.
249 * It is at this point that we assert that all of the extent
250 * slots in the cud item have been filled.
251 */
252STATIC void
253xfs_cud_item_format(
254 struct xfs_log_item *lip,
255 struct xfs_log_vec *lv)
256{
257 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
258 struct xfs_log_iovec *vecp = NULL;
259
260 cudp->cud_format.cud_type = XFS_LI_CUD;
261 cudp->cud_format.cud_size = 1;
262
263 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_CUD_FORMAT, &cudp->cud_format,
264 sizeof(struct xfs_cud_log_format));
265}
266
267/*
268 * Pinning has no meaning for an cud item, so just return.
269 */
270STATIC void
271xfs_cud_item_pin(
272 struct xfs_log_item *lip)
273{
274}
275
276/*
277 * Since pinning has no meaning for an cud item, unpinning does
278 * not either.
279 */
280STATIC void
281xfs_cud_item_unpin(
282 struct xfs_log_item *lip,
283 int remove)
284{
285}
286
287/*
288 * There isn't much you can do to push on an cud item. It is simply stuck
289 * waiting for the log to be flushed to disk.
290 */
291STATIC uint
292xfs_cud_item_push(
293 struct xfs_log_item *lip,
294 struct list_head *buffer_list)
295{
296 return XFS_ITEM_PINNED;
297}
298
299/*
300 * The CUD is either committed or aborted if the transaction is cancelled. If
301 * the transaction is cancelled, drop our reference to the CUI and free the
302 * CUD.
303 */
304STATIC void
305xfs_cud_item_unlock(
306 struct xfs_log_item *lip)
307{
308 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
309
310 if (lip->li_flags & XFS_LI_ABORTED) {
311 xfs_cui_release(cudp->cud_cuip);
312 kmem_zone_free(xfs_cud_zone, cudp);
313 }
314}
315
316/*
317 * When the cud item is committed to disk, all we need to do is delete our
318 * reference to our partner cui item and then free ourselves. Since we're
319 * freeing ourselves we must return -1 to keep the transaction code from
320 * further referencing this item.
321 */
322STATIC xfs_lsn_t
323xfs_cud_item_committed(
324 struct xfs_log_item *lip,
325 xfs_lsn_t lsn)
326{
327 struct xfs_cud_log_item *cudp = CUD_ITEM(lip);
328
329 /*
330 * Drop the CUI reference regardless of whether the CUD has been
331 * aborted. Once the CUD transaction is constructed, it is the sole
332 * responsibility of the CUD to release the CUI (even if the CUI is
333 * aborted due to log I/O error).
334 */
335 xfs_cui_release(cudp->cud_cuip);
336 kmem_zone_free(xfs_cud_zone, cudp);
337
338 return (xfs_lsn_t)-1;
339}
340
341/*
342 * The CUD dependency tracking op doesn't do squat. It can't because
343 * it doesn't know where the free extent is coming from. The dependency
344 * tracking has to be handled by the "enclosing" metadata object. For
345 * example, for inodes, the inode is locked throughout the extent freeing
346 * so the dependency should be recorded there.
347 */
348STATIC void
349xfs_cud_item_committing(
350 struct xfs_log_item *lip,
351 xfs_lsn_t lsn)
352{
353}
354
355/*
356 * This is the ops vector shared by all cud log items.
357 */
358static const struct xfs_item_ops xfs_cud_item_ops = {
359 .iop_size = xfs_cud_item_size,
360 .iop_format = xfs_cud_item_format,
361 .iop_pin = xfs_cud_item_pin,
362 .iop_unpin = xfs_cud_item_unpin,
363 .iop_unlock = xfs_cud_item_unlock,
364 .iop_committed = xfs_cud_item_committed,
365 .iop_push = xfs_cud_item_push,
366 .iop_committing = xfs_cud_item_committing,
367};
368
369/*
370 * Allocate and initialize an cud item with the given number of extents.
371 */
372struct xfs_cud_log_item *
373xfs_cud_init(
374 struct xfs_mount *mp,
375 struct xfs_cui_log_item *cuip)
376
377{
378 struct xfs_cud_log_item *cudp;
379
380 cudp = kmem_zone_zalloc(xfs_cud_zone, KM_SLEEP);
381 xfs_log_item_init(mp, &cudp->cud_item, XFS_LI_CUD, &xfs_cud_item_ops);
382 cudp->cud_cuip = cuip;
383 cudp->cud_format.cud_cui_id = cuip->cui_format.cui_id;
384
385 return cudp;
386}
Darrick J. Wongf997ee22016-10-03 09:11:21 -0700387
388/*
389 * Process a refcount update intent item that was recovered from the log.
390 * We need to update the refcountbt.
391 */
392int
393xfs_cui_recover(
394 struct xfs_mount *mp,
395 struct xfs_cui_log_item *cuip)
396{
397 int i;
398 int error = 0;
Darrick J. Wong33ba6122016-10-03 09:11:22 -0700399 unsigned int refc_type;
Darrick J. Wongf997ee22016-10-03 09:11:21 -0700400 struct xfs_phys_extent *refc;
401 xfs_fsblock_t startblock_fsb;
402 bool op_ok;
Darrick J. Wong33ba6122016-10-03 09:11:22 -0700403 struct xfs_cud_log_item *cudp;
404 struct xfs_trans *tp;
405 struct xfs_btree_cur *rcur = NULL;
406 enum xfs_refcount_intent_type type;
407 xfs_fsblock_t firstfsb;
408 xfs_fsblock_t new_fsb;
409 xfs_extlen_t new_len;
410 struct xfs_bmbt_irec irec;
411 struct xfs_defer_ops dfops;
412 bool requeue_only = false;
Darrick J. Wongf997ee22016-10-03 09:11:21 -0700413
414 ASSERT(!test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags));
415
416 /*
417 * First check the validity of the extents described by the
418 * CUI. If any are bad, then assume that all are bad and
419 * just toss the CUI.
420 */
421 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
422 refc = &cuip->cui_format.cui_extents[i];
423 startblock_fsb = XFS_BB_TO_FSB(mp,
424 XFS_FSB_TO_DADDR(mp, refc->pe_startblock));
425 switch (refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK) {
426 case XFS_REFCOUNT_INCREASE:
427 case XFS_REFCOUNT_DECREASE:
428 case XFS_REFCOUNT_ALLOC_COW:
429 case XFS_REFCOUNT_FREE_COW:
430 op_ok = true;
431 break;
432 default:
433 op_ok = false;
434 break;
435 }
436 if (!op_ok || startblock_fsb == 0 ||
437 refc->pe_len == 0 ||
438 startblock_fsb >= mp->m_sb.sb_dblocks ||
439 refc->pe_len >= mp->m_sb.sb_agblocks ||
440 (refc->pe_flags & ~XFS_REFCOUNT_EXTENT_FLAGS)) {
441 /*
442 * This will pull the CUI from the AIL and
443 * free the memory associated with it.
444 */
445 set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
446 xfs_cui_release(cuip);
447 return -EIO;
448 }
449 }
450
Darrick J. Wong33ba6122016-10-03 09:11:22 -0700451 /*
452 * Under normal operation, refcount updates are deferred, so we
453 * wouldn't be adding them directly to a transaction. All
454 * refcount updates manage reservation usage internally and
455 * dynamically by deferring work that won't fit in the
456 * transaction. Normally, any work that needs to be deferred
457 * gets attached to the same defer_ops that scheduled the
458 * refcount update. However, we're in log recovery here, so we
459 * we create our own defer_ops and use that to finish up any
460 * work that doesn't fit.
461 */
462 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp);
463 if (error)
464 return error;
465 cudp = xfs_trans_get_cud(tp, cuip);
466
467 xfs_defer_init(&dfops, &firstfsb);
468 for (i = 0; i < cuip->cui_format.cui_nextents; i++) {
469 refc = &cuip->cui_format.cui_extents[i];
470 refc_type = refc->pe_flags & XFS_REFCOUNT_EXTENT_TYPE_MASK;
471 switch (refc_type) {
472 case XFS_REFCOUNT_INCREASE:
473 case XFS_REFCOUNT_DECREASE:
474 case XFS_REFCOUNT_ALLOC_COW:
475 case XFS_REFCOUNT_FREE_COW:
476 type = refc_type;
477 break;
478 default:
479 error = -EFSCORRUPTED;
480 goto abort_error;
481 }
482 if (requeue_only) {
483 new_fsb = refc->pe_startblock;
484 new_len = refc->pe_len;
485 } else
486 error = xfs_trans_log_finish_refcount_update(tp, cudp,
487 &dfops, type, refc->pe_startblock, refc->pe_len,
488 &new_fsb, &new_len, &rcur);
489 if (error)
490 goto abort_error;
491
492 /* Requeue what we didn't finish. */
493 if (new_len > 0) {
494 irec.br_startblock = new_fsb;
495 irec.br_blockcount = new_len;
496 switch (type) {
497 case XFS_REFCOUNT_INCREASE:
498 error = xfs_refcount_increase_extent(
499 tp->t_mountp, &dfops, &irec);
500 break;
501 case XFS_REFCOUNT_DECREASE:
502 error = xfs_refcount_decrease_extent(
503 tp->t_mountp, &dfops, &irec);
504 break;
Darrick J. Wong174edb02016-10-03 09:11:39 -0700505 case XFS_REFCOUNT_ALLOC_COW:
506 error = xfs_refcount_alloc_cow_extent(
507 tp->t_mountp, &dfops,
508 irec.br_startblock,
509 irec.br_blockcount);
510 break;
511 case XFS_REFCOUNT_FREE_COW:
512 error = xfs_refcount_free_cow_extent(
513 tp->t_mountp, &dfops,
514 irec.br_startblock,
515 irec.br_blockcount);
516 break;
Darrick J. Wong33ba6122016-10-03 09:11:22 -0700517 default:
518 ASSERT(0);
519 }
520 if (error)
521 goto abort_error;
522 requeue_only = true;
523 }
524 }
525
526 xfs_refcount_finish_one_cleanup(tp, rcur, error);
527 error = xfs_defer_finish(&tp, &dfops, NULL);
528 if (error)
529 goto abort_error;
Darrick J. Wongf997ee22016-10-03 09:11:21 -0700530 set_bit(XFS_CUI_RECOVERED, &cuip->cui_flags);
Darrick J. Wong33ba6122016-10-03 09:11:22 -0700531 error = xfs_trans_commit(tp);
532 return error;
533
534abort_error:
535 xfs_refcount_finish_one_cleanup(tp, rcur, error);
536 xfs_defer_cancel(&dfops);
537 xfs_trans_cancel(tp);
Darrick J. Wongf997ee22016-10-03 09:11:21 -0700538 return error;
539}