blob: 13d1d3e95b888fb2630c784869bf932092a51666 [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Tim Shimmin87c199c2006-06-09 14:56:16 +10003 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11004 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +11007#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +11008#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_format.h"
10#include "xfs_log_format.h"
11#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110012#include "xfs_bit.h"
Nathan Scotta844f452005-11-02 14:38:42 +110013#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_mount.h"
Darrick J. Wong50995582017-11-21 20:53:02 -080015#include "xfs_defer.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110017#include "xfs_trans.h"
Dave Chinner239880e2013-10-23 10:50:10 +110018#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "xfs_log_recover.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110021#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_extfree_item.h"
23#include "xfs_trans_priv.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110024#include "xfs_alloc.h"
25#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_quota.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000027#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110028#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110029#include "xfs_bmap_btree.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110030#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100031#include "xfs_dir2.h"
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +100032#include "xfs_rmap_item.h"
Brian Foster60a4a222016-09-26 08:34:27 +100033#include "xfs_buf_item.h"
Darrick J. Wongf997ee22016-10-03 09:11:21 -070034#include "xfs_refcount_item.h"
Darrick J. Wong77d61fe2016-10-03 09:11:26 -070035#include "xfs_bmap_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Dave Chinnerfc06c6d2013-08-12 20:49:22 +100037#define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
38
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050039STATIC int
40xlog_find_zeroed(
41 struct xlog *,
42 xfs_daddr_t *);
43STATIC int
44xlog_clear_stale_blocks(
45 struct xlog *,
46 xfs_lsn_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#if defined(DEBUG)
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050048STATIC void
49xlog_recover_check_summary(
50 struct xlog *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#else
52#define xlog_recover_check_summary(log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#endif
Brian Foster7088c412016-01-05 07:40:16 +110054STATIC int
55xlog_do_recovery_pass(
56 struct xlog *, xfs_daddr_t, xfs_daddr_t, int, xfs_daddr_t *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +000059 * This structure is used during recovery to record the buf log items which
60 * have been canceled and should not be replayed.
61 */
62struct xfs_buf_cancel {
63 xfs_daddr_t bc_blkno;
64 uint bc_len;
65 int bc_refcount;
66 struct list_head bc_list;
67};
68
69/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 * Sector aligned buffer routines for buffer create/read/write/access
71 */
72
Alex Elderff30a622010-04-13 15:22:58 +100073/*
Brian Foster99c26592017-10-26 09:31:15 -070074 * Verify the log-relative block number and length in basic blocks are valid for
75 * an operation involving the given XFS log buffer. Returns true if the fields
76 * are valid, false otherwise.
Alex Elderff30a622010-04-13 15:22:58 +100077 */
Brian Foster99c26592017-10-26 09:31:15 -070078static inline bool
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -070079xlog_verify_bno(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050080 struct xlog *log,
Brian Foster99c26592017-10-26 09:31:15 -070081 xfs_daddr_t blk_no,
Alex Elderff30a622010-04-13 15:22:58 +100082 int bbcount)
83{
Brian Foster99c26592017-10-26 09:31:15 -070084 if (blk_no < 0 || blk_no >= log->l_logBBsize)
85 return false;
86 if (bbcount <= 0 || (blk_no + bbcount) > log->l_logBBsize)
87 return false;
88 return true;
Alex Elderff30a622010-04-13 15:22:58 +100089}
90
Alex Elder36adecf2010-04-13 15:21:13 +100091/*
Christoph Hellwig6ad5b322019-06-28 19:27:26 -070092 * Allocate a buffer to hold log data. The buffer needs to be able to map to
93 * a range of nbblks basic blocks at any valid offset within the log.
Alex Elder36adecf2010-04-13 15:21:13 +100094 */
Christoph Hellwig6ad5b322019-06-28 19:27:26 -070095static char *
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -070096xlog_alloc_buffer(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050097 struct xlog *log,
Dave Chinner32281492009-01-22 15:37:47 +110098 int nbblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Brian Foster99c26592017-10-26 09:31:15 -0700100 /*
101 * Pass log block 0 since we don't have an addr yet, buffer will be
102 * verified on read.
103 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700104 if (!xlog_verify_bno(log, 0, nbblks)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100105 xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
Alex Elderff30a622010-04-13 15:22:58 +1000106 nbblks);
107 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner32281492009-01-22 15:37:47 +1100108 return NULL;
109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Alex Elder36adecf2010-04-13 15:21:13 +1000111 /*
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700112 * We do log I/O in units of log sectors (a power-of-2 multiple of the
113 * basic block size), so we round up the requested size to accommodate
114 * the basic blocks required for complete log sectors.
Alex Elder36adecf2010-04-13 15:21:13 +1000115 *
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700116 * In addition, the buffer may be used for a non-sector-aligned block
117 * offset, in which case an I/O of the requested size could extend
118 * beyond the end of the buffer. If the requested size is only 1 basic
119 * block it will never straddle a sector boundary, so this won't be an
120 * issue. Nor will this be a problem if the log I/O is done in basic
121 * blocks (sector size 1). But otherwise we extend the buffer by one
122 * extra log sector to ensure there's space to accommodate this
123 * possibility.
Alex Elder36adecf2010-04-13 15:21:13 +1000124 */
Alex Elder69ce58f2010-04-20 17:09:59 +1000125 if (nbblks > 1 && log->l_sectBBsize > 1)
126 nbblks += log->l_sectBBsize;
127 nbblks = round_up(nbblks, log->l_sectBBsize);
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700128 return kmem_alloc_large(BBTOB(nbblks), KM_MAYFAIL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Alex Elder48389ef2010-04-20 17:10:21 +1000131/*
132 * Return the address of the start of the given block number's data
133 * in a log buffer. The buffer covers a log sector-aligned region.
134 */
Christoph Hellwig18ffb8c2019-06-28 19:27:26 -0700135static inline unsigned int
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100136xlog_align(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500137 struct xlog *log,
Christoph Hellwig18ffb8c2019-06-28 19:27:26 -0700138 xfs_daddr_t blk_no)
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100139{
Christoph Hellwig18ffb8c2019-06-28 19:27:26 -0700140 return BBTOB(blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100141}
142
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700143static int
144xlog_do_io(
145 struct xlog *log,
146 xfs_daddr_t blk_no,
147 unsigned int nbblks,
148 char *data,
149 unsigned int op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700151 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700153 if (!xlog_verify_bno(log, blk_no, nbblks)) {
Brian Foster99c26592017-10-26 09:31:15 -0700154 xfs_warn(log->l_mp,
155 "Invalid log block/length (0x%llx, 0x%x) for buffer",
156 blk_no, nbblks);
Alex Elderff30a622010-04-13 15:22:58 +1000157 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +1000158 return -EFSCORRUPTED;
Dave Chinner32281492009-01-22 15:37:47 +1100159 }
160
Alex Elder69ce58f2010-04-20 17:09:59 +1000161 blk_no = round_down(blk_no, log->l_sectBBsize);
162 nbblks = round_up(nbblks, log->l_sectBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 ASSERT(nbblks > 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700165 error = xfs_rw_bdev(log->l_targ->bt_bdev, log->l_logBBstart + blk_no,
166 BBTOB(nbblks), data, op);
167 if (error && !XFS_FORCED_SHUTDOWN(log->l_mp)) {
168 xfs_alert(log->l_mp,
169 "log recovery %s I/O error at daddr 0x%llx len %d error %d",
170 op == REQ_OP_WRITE ? "write" : "read",
171 blk_no, nbblks, error);
172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return error;
174}
175
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100176STATIC int
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700177xlog_bread_noalign(
178 struct xlog *log,
179 xfs_daddr_t blk_no,
180 int nbblks,
181 char *data)
182{
183 return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
184}
185
186STATIC int
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100187xlog_bread(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500188 struct xlog *log,
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100189 xfs_daddr_t blk_no,
190 int nbblks,
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700191 char *data,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +1000192 char **offset)
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100193{
194 int error;
195
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700196 error = xlog_do_io(log, blk_no, nbblks, data, REQ_OP_READ);
197 if (!error)
198 *offset = data + xlog_align(log, blk_no);
199 return error;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100200}
201
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000202STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203xlog_bwrite(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500204 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 xfs_daddr_t blk_no,
206 int nbblks,
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700207 char *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Christoph Hellwig6ad5b322019-06-28 19:27:26 -0700209 return xlog_do_io(log, blk_no, nbblks, data, REQ_OP_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212#ifdef DEBUG
213/*
214 * dump debug superblock and log record information
215 */
216STATIC void
217xlog_header_check_dump(
218 xfs_mount_t *mp,
219 xlog_rec_header_t *head)
220{
Eric Sandeen08e96e12013-10-11 20:59:05 -0500221 xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
Joe Perches03daa572009-12-14 18:01:10 -0800222 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
Eric Sandeen08e96e12013-10-11 20:59:05 -0500223 xfs_debug(mp, " log : uuid = %pU, fmt = %d",
Joe Perches03daa572009-12-14 18:01:10 -0800224 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226#else
227#define xlog_header_check_dump(mp, head)
228#endif
229
230/*
231 * check log record header for recovery
232 */
233STATIC int
234xlog_header_check_recover(
235 xfs_mount_t *mp,
236 xlog_rec_header_t *head)
237{
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200238 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 /*
241 * IRIX doesn't write the h_fmt field and leaves it zeroed
242 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
243 * a dirty log created in IRIX.
244 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200245 if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100246 xfs_warn(mp,
247 "dirty log written in incompatible format - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 xlog_header_check_dump(mp, head);
249 XFS_ERROR_REPORT("xlog_header_check_recover(1)",
250 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000251 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100253 xfs_warn(mp,
254 "dirty log entry has mismatched uuid - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 xlog_header_check_dump(mp, head);
256 XFS_ERROR_REPORT("xlog_header_check_recover(2)",
257 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000258 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260 return 0;
261}
262
263/*
264 * read the head block of the log and check the header
265 */
266STATIC int
267xlog_header_check_mount(
268 xfs_mount_t *mp,
269 xlog_rec_header_t *head)
270{
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200271 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Amir Goldsteind905fda2017-05-04 16:26:23 +0300273 if (uuid_is_null(&head->h_fs_uuid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
Amir Goldsteind905fda2017-05-04 16:26:23 +0300276 * h_fs_uuid is null, we assume this log was last mounted
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * by IRIX and continue.
278 */
Amir Goldsteind905fda2017-05-04 16:26:23 +0300279 xfs_warn(mp, "null uuid in log - IRIX style log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100281 xfs_warn(mp, "log has mismatched uuid - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 xlog_header_check_dump(mp, head);
283 XFS_ERROR_REPORT("xlog_header_check_mount",
284 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000285 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
287 return 0;
288}
289
290STATIC void
291xlog_recover_iodone(
292 struct xfs_buf *bp)
293{
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000294 if (bp->b_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 /*
296 * We're not going to bother about retrying
297 * this during recovery. One strike!
298 */
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700299 if (!XFS_FORCED_SHUTDOWN(bp->b_mount)) {
Dave Chinner595bff72014-10-02 09:05:14 +1000300 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -0700301 xfs_force_shutdown(bp->b_mount, SHUTDOWN_META_IO_ERROR);
Dave Chinner595bff72014-10-02 09:05:14 +1000302 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 }
Brian Foster60a4a222016-09-26 08:34:27 +1000304
305 /*
306 * On v5 supers, a bli could be attached to update the metadata LSN.
307 * Clean it up.
308 */
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800309 if (bp->b_log_item)
Brian Foster60a4a222016-09-26 08:34:27 +1000310 xfs_buf_item_relse(bp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -0800311 ASSERT(bp->b_log_item == NULL);
Brian Foster60a4a222016-09-26 08:34:27 +1000312
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200313 bp->b_iodone = NULL;
Dave Chinnere8aaba92014-10-02 09:04:22 +1000314 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315}
316
317/*
318 * This routine finds (to an approximation) the first block in the physical
319 * log which contains the given cycle. It uses a binary search algorithm.
320 * Note that the algorithm can not be perfect because the disk will not
321 * necessarily be perfect.
322 */
David Chinnera8272ce2007-11-23 16:28:09 +1100323STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324xlog_find_cycle_start(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500325 struct xlog *log,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700326 char *buffer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xfs_daddr_t first_blk,
328 xfs_daddr_t *last_blk,
329 uint cycle)
330{
Christoph Hellwigb2a922c2015-06-22 09:45:10 +1000331 char *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 xfs_daddr_t mid_blk;
Alex Eldere3bb2e32010-04-15 18:17:30 +0000333 xfs_daddr_t end_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 uint mid_cycle;
335 int error;
336
Alex Eldere3bb2e32010-04-15 18:17:30 +0000337 end_blk = *last_blk;
338 mid_blk = BLK_AVG(first_blk, end_blk);
339 while (mid_blk != first_blk && mid_blk != end_blk) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700340 error = xlog_bread(log, mid_blk, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100341 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return error;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000343 mid_cycle = xlog_get_cycle(offset);
Alex Eldere3bb2e32010-04-15 18:17:30 +0000344 if (mid_cycle == cycle)
345 end_blk = mid_blk; /* last_half_cycle == mid_cycle */
346 else
347 first_blk = mid_blk; /* first_half_cycle == mid_cycle */
348 mid_blk = BLK_AVG(first_blk, end_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Alex Eldere3bb2e32010-04-15 18:17:30 +0000350 ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
351 (mid_blk == end_blk && mid_blk-1 == first_blk));
352
353 *last_blk = end_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 return 0;
356}
357
358/*
Alex Elder3f943d82010-04-15 18:17:34 +0000359 * Check that a range of blocks does not contain stop_on_cycle_no.
360 * Fill in *new_blk with the block offset where such a block is
361 * found, or with -1 (an invalid block number) if there is no such
362 * block in the range. The scan needs to occur from front to back
363 * and the pointer into the region must be updated since a later
364 * routine will need to perform another test.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
366STATIC int
367xlog_find_verify_cycle(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500368 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 xfs_daddr_t start_blk,
370 int nbblks,
371 uint stop_on_cycle_no,
372 xfs_daddr_t *new_blk)
373{
374 xfs_daddr_t i, j;
375 uint cycle;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700376 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 xfs_daddr_t bufblks;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +1000378 char *buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 int error = 0;
380
Alex Elder6881a222010-04-13 15:22:29 +1000381 /*
382 * Greedily allocate a buffer big enough to handle the full
383 * range of basic blocks we'll be examining. If that fails,
384 * try a smaller size. We need to be able to read at least
385 * a log sector, or we're out of luck.
386 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 bufblks = 1 << ffs(nbblks);
Dave Chinner81158e02012-04-27 19:45:22 +1000388 while (bufblks > log->l_logBBsize)
389 bufblks >>= 1;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700390 while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 bufblks >>= 1;
Alex Elder69ce58f2010-04-20 17:09:59 +1000392 if (bufblks < log->l_sectBBsize)
Dave Chinner24513372014-06-25 14:58:08 +1000393 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 }
395
396 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
397 int bcount;
398
399 bcount = min(bufblks, (start_blk + nbblks - i));
400
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700401 error = xlog_bread(log, i, bcount, buffer, &buf);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100402 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 goto out;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 for (j = 0; j < bcount; j++) {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000406 cycle = xlog_get_cycle(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (cycle == stop_on_cycle_no) {
408 *new_blk = i+j;
409 goto out;
410 }
411
412 buf += BBSIZE;
413 }
414 }
415
416 *new_blk = -1;
417
418out:
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700419 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 return error;
421}
422
423/*
424 * Potentially backup over partial log record write.
425 *
426 * In the typical case, last_blk is the number of the block directly after
427 * a good log record. Therefore, we subtract one to get the block number
428 * of the last block in the given buffer. extra_bblks contains the number
429 * of blocks we would have read on a previous read. This happens when the
430 * last log record is split over the end of the physical log.
431 *
432 * extra_bblks is the number of blocks potentially verified on a previous
433 * call to this routine.
434 */
435STATIC int
436xlog_find_verify_log_record(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500437 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 xfs_daddr_t start_blk,
439 xfs_daddr_t *last_blk,
440 int extra_bblks)
441{
442 xfs_daddr_t i;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700443 char *buffer;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +1000444 char *offset = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 xlog_rec_header_t *head = NULL;
446 int error = 0;
447 int smallmem = 0;
448 int num_blks = *last_blk - start_blk;
449 int xhdrs;
450
451 ASSERT(start_blk != 0 || *last_blk != start_blk);
452
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700453 buffer = xlog_alloc_buffer(log, num_blks);
454 if (!buffer) {
455 buffer = xlog_alloc_buffer(log, 1);
456 if (!buffer)
Dave Chinner24513372014-06-25 14:58:08 +1000457 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 smallmem = 1;
459 } else {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700460 error = xlog_bread(log, start_blk, num_blks, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100461 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 offset += ((num_blks - 1) << BBSHIFT);
464 }
465
466 for (i = (*last_blk) - 1; i >= 0; i--) {
467 if (i < start_blk) {
468 /* valid log record not found */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100469 xfs_warn(log->l_mp,
470 "Log inconsistent (didn't find previous header)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000472 error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto out;
474 }
475
476 if (smallmem) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700477 error = xlog_bread(log, i, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100478 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
481
482 head = (xlog_rec_header_t *)offset;
483
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200484 if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 break;
486
487 if (!smallmem)
488 offset -= BBSIZE;
489 }
490
491 /*
492 * We hit the beginning of the physical log & still no header. Return
493 * to caller. If caller can handle a return of -1, then this routine
494 * will be called again for the end of the physical log.
495 */
496 if (i == -1) {
Dave Chinner24513372014-06-25 14:58:08 +1000497 error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 goto out;
499 }
500
501 /*
502 * We have the final block of the good log (the first block
503 * of the log record _before_ the head. So we check the uuid.
504 */
505 if ((error = xlog_header_check_mount(log->l_mp, head)))
506 goto out;
507
508 /*
509 * We may have found a log record header before we expected one.
510 * last_blk will be the 1st block # with a given cycle #. We may end
511 * up reading an entire log record. In this case, we don't want to
512 * reset last_blk. Only when last_blk points in the middle of a log
513 * record do we update last_blk.
514 */
Eric Sandeen62118702008-03-06 13:44:28 +1100515 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000516 uint h_size = be32_to_cpu(head->h_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
518 xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
519 if (h_size % XLOG_HEADER_CYCLE_SIZE)
520 xhdrs++;
521 } else {
522 xhdrs = 1;
523 }
524
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000525 if (*last_blk - i + extra_bblks !=
526 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 *last_blk = i;
528
529out:
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700530 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return error;
532}
533
534/*
535 * Head is defined to be the point of the log where the next log write
Zhi Yong Wu0a94da22013-08-07 10:11:08 +0000536 * could go. This means that incomplete LR writes at the end are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * eliminated when calculating the head. We aren't guaranteed that previous
538 * LR have complete transactions. We only know that a cycle number of
539 * current cycle number -1 won't be present in the log if we start writing
540 * from our current block number.
541 *
542 * last_blk contains the block number of the first block with a given
543 * cycle number.
544 *
545 * Return: zero if normal, non-zero if error.
546 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000547STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548xlog_find_head(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500549 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 xfs_daddr_t *return_head_blk)
551{
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700552 char *buffer;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +1000553 char *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
555 int num_scan_bblks;
556 uint first_half_cycle, last_half_cycle;
557 uint stop_on_cycle;
558 int error, log_bbnum = log->l_logBBsize;
559
560 /* Is the end of the log device zeroed? */
Dave Chinner24513372014-06-25 14:58:08 +1000561 error = xlog_find_zeroed(log, &first_blk);
562 if (error < 0) {
563 xfs_warn(log->l_mp, "empty log check failed");
564 return error;
565 }
566 if (error == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 *return_head_blk = first_blk;
568
569 /* Is the whole lot zeroed? */
570 if (!first_blk) {
571 /* Linux XFS shouldn't generate totally zeroed logs -
572 * mkfs etc write a dummy unmount record to a fresh
573 * log so we can store the uuid in there
574 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100575 xfs_warn(log->l_mp, "totally zeroed log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
578 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 }
580
581 first_blk = 0; /* get cycle # of 1st block */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700582 buffer = xlog_alloc_buffer(log, 1);
583 if (!buffer)
Dave Chinner24513372014-06-25 14:58:08 +1000584 return -ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100585
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700586 error = xlog_bread(log, 0, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100587 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700588 goto out_free_buffer;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100589
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000590 first_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
592 last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700593 error = xlog_bread(log, last_blk, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100594 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700595 goto out_free_buffer;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100596
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000597 last_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 ASSERT(last_half_cycle != 0);
599
600 /*
601 * If the 1st half cycle number is equal to the last half cycle number,
602 * then the entire log is stamped with the same cycle number. In this
603 * case, head_blk can't be set to zero (which makes sense). The below
604 * math doesn't work out properly with head_blk equal to zero. Instead,
605 * we set it to log_bbnum which is an invalid block number, but this
606 * value makes the math correct. If head_blk doesn't changed through
607 * all the tests below, *head_blk is set to zero at the very end rather
608 * than log_bbnum. In a sense, log_bbnum and zero are the same block
609 * in a circular file.
610 */
611 if (first_half_cycle == last_half_cycle) {
612 /*
613 * In this case we believe that the entire log should have
614 * cycle number last_half_cycle. We need to scan backwards
615 * from the end verifying that there are no holes still
616 * containing last_half_cycle - 1. If we find such a hole,
617 * then the start of that hole will be the new head. The
618 * simple case looks like
619 * x | x ... | x - 1 | x
620 * Another case that fits this picture would be
621 * x | x + 1 | x ... | x
Nathan Scottc41564b2006-03-29 08:55:14 +1000622 * In this case the head really is somewhere at the end of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 * log, as one of the latest writes at the beginning was
624 * incomplete.
625 * One more case is
626 * x | x + 1 | x ... | x - 1 | x
627 * This is really the combination of the above two cases, and
628 * the head has to end up at the start of the x-1 hole at the
629 * end of the log.
630 *
631 * In the 256k log case, we will read from the beginning to the
632 * end of the log and search for cycle numbers equal to x-1.
633 * We don't worry about the x+1 blocks that we encounter,
634 * because we know that they cannot be the head since the log
635 * started with x.
636 */
637 head_blk = log_bbnum;
638 stop_on_cycle = last_half_cycle - 1;
639 } else {
640 /*
641 * In this case we want to find the first block with cycle
642 * number matching last_half_cycle. We expect the log to be
643 * some variation on
Alex Elder3f943d82010-04-15 18:17:34 +0000644 * x + 1 ... | x ... | x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 * The first block with cycle number x (last_half_cycle) will
646 * be where the new head belongs. First we do a binary search
647 * for the first occurrence of last_half_cycle. The binary
648 * search may not be totally accurate, so then we scan back
649 * from there looking for occurrences of last_half_cycle before
650 * us. If that backwards scan wraps around the beginning of
651 * the log, then we look for occurrences of last_half_cycle - 1
652 * at the end of the log. The cases we're looking for look
653 * like
Alex Elder3f943d82010-04-15 18:17:34 +0000654 * v binary search stopped here
655 * x + 1 ... | x | x + 1 | x ... | x
656 * ^ but we want to locate this spot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 * <---------> less than scan distance
Alex Elder3f943d82010-04-15 18:17:34 +0000659 * x + 1 ... | x ... | x - 1 | x
660 * ^ we want to locate this spot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
662 stop_on_cycle = last_half_cycle;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700663 error = xlog_find_cycle_start(log, buffer, first_blk, &head_blk,
664 last_half_cycle);
665 if (error)
666 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 }
668
669 /*
670 * Now validate the answer. Scan back some number of maximum possible
671 * blocks and make sure each one has the expected cycle number. The
672 * maximum is determined by the total possible amount of buffering
673 * in the in-core log. The following number can be made tighter if
674 * we actually look at the block size of the filesystem.
675 */
Brian Foster9f2a4502017-10-26 09:31:16 -0700676 num_scan_bblks = min_t(int, log_bbnum, XLOG_TOTAL_REC_SHIFT(log));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (head_blk >= num_scan_bblks) {
678 /*
679 * We are guaranteed that the entire check can be performed
680 * in one buffer.
681 */
682 start_blk = head_blk - num_scan_bblks;
683 if ((error = xlog_find_verify_cycle(log,
684 start_blk, num_scan_bblks,
685 stop_on_cycle, &new_blk)))
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700686 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (new_blk != -1)
688 head_blk = new_blk;
689 } else { /* need to read 2 parts of log */
690 /*
691 * We are going to scan backwards in the log in two parts.
692 * First we scan the physical end of the log. In this part
693 * of the log, we are looking for blocks with cycle number
694 * last_half_cycle - 1.
695 * If we find one, then we know that the log starts there, as
696 * we've found a hole that didn't get written in going around
697 * the end of the physical log. The simple case for this is
698 * x + 1 ... | x ... | x - 1 | x
699 * <---------> less than scan distance
700 * If all of the blocks at the end of the log have cycle number
701 * last_half_cycle, then we check the blocks at the start of
702 * the log looking for occurrences of last_half_cycle. If we
703 * find one, then our current estimate for the location of the
704 * first occurrence of last_half_cycle is wrong and we move
705 * back to the hole we've found. This case looks like
706 * x + 1 ... | x | x + 1 | x ...
707 * ^ binary search stopped here
708 * Another case we need to handle that only occurs in 256k
709 * logs is
710 * x + 1 ... | x ... | x+1 | x ...
711 * ^ binary search stops here
712 * In a 256k log, the scan at the end of the log will see the
713 * x + 1 blocks. We need to skip past those since that is
714 * certainly not the head of the log. By searching for
715 * last_half_cycle-1 we accomplish that.
716 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 ASSERT(head_blk <= INT_MAX &&
Alex Elder3f943d82010-04-15 18:17:34 +0000718 (xfs_daddr_t) num_scan_bblks >= head_blk);
719 start_blk = log_bbnum - (num_scan_bblks - head_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if ((error = xlog_find_verify_cycle(log, start_blk,
721 num_scan_bblks - (int)head_blk,
722 (stop_on_cycle - 1), &new_blk)))
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700723 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (new_blk != -1) {
725 head_blk = new_blk;
Alex Elder9db127e2010-04-15 18:17:26 +0000726 goto validate_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
729 /*
730 * Scan beginning of log now. The last part of the physical
731 * log is good. This scan needs to verify that it doesn't find
732 * the last_half_cycle.
733 */
734 start_blk = 0;
735 ASSERT(head_blk <= INT_MAX);
736 if ((error = xlog_find_verify_cycle(log,
737 start_blk, (int)head_blk,
738 stop_on_cycle, &new_blk)))
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700739 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (new_blk != -1)
741 head_blk = new_blk;
742 }
743
Alex Elder9db127e2010-04-15 18:17:26 +0000744validate_head:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /*
746 * Now we need to make sure head_blk is not pointing to a block in
747 * the middle of a log record.
748 */
749 num_scan_bblks = XLOG_REC_SHIFT(log);
750 if (head_blk >= num_scan_bblks) {
751 start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
752
753 /* start ptr at last block ptr before head_blk */
Dave Chinner24513372014-06-25 14:58:08 +1000754 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
755 if (error == 1)
756 error = -EIO;
757 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700758 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 } else {
760 start_blk = 0;
761 ASSERT(head_blk <= INT_MAX);
Dave Chinner24513372014-06-25 14:58:08 +1000762 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
763 if (error < 0)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700764 goto out_free_buffer;
Dave Chinner24513372014-06-25 14:58:08 +1000765 if (error == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* We hit the beginning of the log during our search */
Alex Elder3f943d82010-04-15 18:17:34 +0000767 start_blk = log_bbnum - (num_scan_bblks - head_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 new_blk = log_bbnum;
769 ASSERT(start_blk <= INT_MAX &&
770 (xfs_daddr_t) log_bbnum-start_blk >= 0);
771 ASSERT(head_blk <= INT_MAX);
Dave Chinner24513372014-06-25 14:58:08 +1000772 error = xlog_find_verify_log_record(log, start_blk,
773 &new_blk, (int)head_blk);
774 if (error == 1)
775 error = -EIO;
776 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700777 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (new_blk != log_bbnum)
779 head_blk = new_blk;
780 } else if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700781 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
783
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700784 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (head_blk == log_bbnum)
786 *return_head_blk = 0;
787 else
788 *return_head_blk = head_blk;
789 /*
790 * When returning here, we have a good block number. Bad block
791 * means that during a previous crash, we didn't have a clean break
792 * from cycle number N to cycle number N-1. In this case, we need
793 * to find the first block with cycle number N-1.
794 */
795 return 0;
796
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700797out_free_buffer:
798 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 if (error)
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100800 xfs_warn(log->l_mp, "failed to find log head");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return error;
802}
803
804/*
Brian Fostereed6b462016-01-04 15:55:10 +1100805 * Seek backwards in the log for log record headers.
806 *
807 * Given a starting log block, walk backwards until we find the provided number
808 * of records or hit the provided tail block. The return value is the number of
809 * records encountered or a negative error code. The log block and buffer
810 * pointer of the last record seen are returned in rblk and rhead respectively.
811 */
812STATIC int
813xlog_rseek_logrec_hdr(
814 struct xlog *log,
815 xfs_daddr_t head_blk,
816 xfs_daddr_t tail_blk,
817 int count,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700818 char *buffer,
Brian Fostereed6b462016-01-04 15:55:10 +1100819 xfs_daddr_t *rblk,
820 struct xlog_rec_header **rhead,
821 bool *wrapped)
822{
823 int i;
824 int error;
825 int found = 0;
826 char *offset = NULL;
827 xfs_daddr_t end_blk;
828
829 *wrapped = false;
830
831 /*
832 * Walk backwards from the head block until we hit the tail or the first
833 * block in the log.
834 */
835 end_blk = head_blk > tail_blk ? tail_blk : 0;
836 for (i = (int) head_blk - 1; i >= end_blk; i--) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700837 error = xlog_bread(log, i, 1, buffer, &offset);
Brian Fostereed6b462016-01-04 15:55:10 +1100838 if (error)
839 goto out_error;
840
841 if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
842 *rblk = i;
843 *rhead = (struct xlog_rec_header *) offset;
844 if (++found == count)
845 break;
846 }
847 }
848
849 /*
850 * If we haven't hit the tail block or the log record header count,
851 * start looking again from the end of the physical log. Note that
852 * callers can pass head == tail if the tail is not yet known.
853 */
854 if (tail_blk >= head_blk && found != count) {
855 for (i = log->l_logBBsize - 1; i >= (int) tail_blk; i--) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700856 error = xlog_bread(log, i, 1, buffer, &offset);
Brian Fostereed6b462016-01-04 15:55:10 +1100857 if (error)
858 goto out_error;
859
860 if (*(__be32 *)offset ==
861 cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
862 *wrapped = true;
863 *rblk = i;
864 *rhead = (struct xlog_rec_header *) offset;
865 if (++found == count)
866 break;
867 }
868 }
869 }
870
871 return found;
872
873out_error:
874 return error;
875}
876
877/*
Brian Foster7088c412016-01-05 07:40:16 +1100878 * Seek forward in the log for log record headers.
879 *
880 * Given head and tail blocks, walk forward from the tail block until we find
881 * the provided number of records or hit the head block. The return value is the
882 * number of records encountered or a negative error code. The log block and
883 * buffer pointer of the last record seen are returned in rblk and rhead
884 * respectively.
885 */
886STATIC int
887xlog_seek_logrec_hdr(
888 struct xlog *log,
889 xfs_daddr_t head_blk,
890 xfs_daddr_t tail_blk,
891 int count,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700892 char *buffer,
Brian Foster7088c412016-01-05 07:40:16 +1100893 xfs_daddr_t *rblk,
894 struct xlog_rec_header **rhead,
895 bool *wrapped)
896{
897 int i;
898 int error;
899 int found = 0;
900 char *offset = NULL;
901 xfs_daddr_t end_blk;
902
903 *wrapped = false;
904
905 /*
906 * Walk forward from the tail block until we hit the head or the last
907 * block in the log.
908 */
909 end_blk = head_blk > tail_blk ? head_blk : log->l_logBBsize - 1;
910 for (i = (int) tail_blk; i <= end_blk; i++) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700911 error = xlog_bread(log, i, 1, buffer, &offset);
Brian Foster7088c412016-01-05 07:40:16 +1100912 if (error)
913 goto out_error;
914
915 if (*(__be32 *) offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
916 *rblk = i;
917 *rhead = (struct xlog_rec_header *) offset;
918 if (++found == count)
919 break;
920 }
921 }
922
923 /*
924 * If we haven't hit the head block or the log record header count,
925 * start looking again from the start of the physical log.
926 */
927 if (tail_blk > head_blk && found != count) {
928 for (i = 0; i < (int) head_blk; i++) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700929 error = xlog_bread(log, i, 1, buffer, &offset);
Brian Foster7088c412016-01-05 07:40:16 +1100930 if (error)
931 goto out_error;
932
933 if (*(__be32 *)offset ==
934 cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
935 *wrapped = true;
936 *rblk = i;
937 *rhead = (struct xlog_rec_header *) offset;
938 if (++found == count)
939 break;
940 }
941 }
942 }
943
944 return found;
945
946out_error:
947 return error;
948}
949
950/*
Brian Foster4a4f66e2017-08-08 18:21:52 -0700951 * Calculate distance from head to tail (i.e., unused space in the log).
952 */
953static inline int
954xlog_tail_distance(
955 struct xlog *log,
956 xfs_daddr_t head_blk,
957 xfs_daddr_t tail_blk)
958{
959 if (head_blk < tail_blk)
960 return tail_blk - head_blk;
961
962 return tail_blk + (log->l_logBBsize - head_blk);
963}
964
965/*
966 * Verify the log tail. This is particularly important when torn or incomplete
967 * writes have been detected near the front of the log and the head has been
968 * walked back accordingly.
Brian Foster7088c412016-01-05 07:40:16 +1100969 *
Brian Foster4a4f66e2017-08-08 18:21:52 -0700970 * We also have to handle the case where the tail was pinned and the head
971 * blocked behind the tail right before a crash. If the tail had been pushed
972 * immediately prior to the crash and the subsequent checkpoint was only
973 * partially written, it's possible it overwrote the last referenced tail in the
974 * log with garbage. This is not a coherency problem because the tail must have
975 * been pushed before it can be overwritten, but appears as log corruption to
976 * recovery because we have no way to know the tail was updated if the
977 * subsequent checkpoint didn't write successfully.
978 *
979 * Therefore, CRC check the log from tail to head. If a failure occurs and the
980 * offending record is within max iclog bufs from the head, walk the tail
981 * forward and retry until a valid tail is found or corruption is detected out
982 * of the range of a possible overwrite.
Brian Foster7088c412016-01-05 07:40:16 +1100983 */
984STATIC int
985xlog_verify_tail(
986 struct xlog *log,
987 xfs_daddr_t head_blk,
Brian Foster4a4f66e2017-08-08 18:21:52 -0700988 xfs_daddr_t *tail_blk,
989 int hsize)
Brian Foster7088c412016-01-05 07:40:16 +1100990{
991 struct xlog_rec_header *thead;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700992 char *buffer;
Brian Foster7088c412016-01-05 07:40:16 +1100993 xfs_daddr_t first_bad;
Brian Foster7088c412016-01-05 07:40:16 +1100994 int error = 0;
995 bool wrapped;
Brian Foster4a4f66e2017-08-08 18:21:52 -0700996 xfs_daddr_t tmp_tail;
997 xfs_daddr_t orig_tail = *tail_blk;
Brian Foster7088c412016-01-05 07:40:16 +1100998
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -0700999 buffer = xlog_alloc_buffer(log, 1);
1000 if (!buffer)
Brian Foster7088c412016-01-05 07:40:16 +11001001 return -ENOMEM;
1002
1003 /*
Brian Foster4a4f66e2017-08-08 18:21:52 -07001004 * Make sure the tail points to a record (returns positive count on
1005 * success).
Brian Foster7088c412016-01-05 07:40:16 +11001006 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001007 error = xlog_seek_logrec_hdr(log, head_blk, *tail_blk, 1, buffer,
Brian Foster4a4f66e2017-08-08 18:21:52 -07001008 &tmp_tail, &thead, &wrapped);
1009 if (error < 0)
Brian Foster7088c412016-01-05 07:40:16 +11001010 goto out;
Brian Foster4a4f66e2017-08-08 18:21:52 -07001011 if (*tail_blk != tmp_tail)
1012 *tail_blk = tmp_tail;
1013
1014 /*
1015 * Run a CRC check from the tail to the head. We can't just check
1016 * MAX_ICLOGS records past the tail because the tail may point to stale
1017 * blocks cleared during the search for the head/tail. These blocks are
1018 * overwritten with zero-length records and thus record count is not a
1019 * reliable indicator of the iclog state before a crash.
1020 */
1021 first_bad = 0;
1022 error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
1023 XLOG_RECOVER_CRCPASS, &first_bad);
Brian Fostera4c9b342017-08-08 18:21:53 -07001024 while ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
Brian Foster4a4f66e2017-08-08 18:21:52 -07001025 int tail_distance;
1026
1027 /*
1028 * Is corruption within range of the head? If so, retry from
1029 * the next record. Otherwise return an error.
1030 */
1031 tail_distance = xlog_tail_distance(log, head_blk, first_bad);
1032 if (tail_distance > BTOBB(XLOG_MAX_ICLOGS * hsize))
1033 break;
1034
1035 /* skip to the next record; returns positive count on success */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001036 error = xlog_seek_logrec_hdr(log, head_blk, first_bad, 2,
1037 buffer, &tmp_tail, &thead, &wrapped);
Brian Foster4a4f66e2017-08-08 18:21:52 -07001038 if (error < 0)
1039 goto out;
1040
1041 *tail_blk = tmp_tail;
1042 first_bad = 0;
1043 error = xlog_do_recovery_pass(log, head_blk, *tail_blk,
1044 XLOG_RECOVER_CRCPASS, &first_bad);
Brian Foster7088c412016-01-05 07:40:16 +11001045 }
1046
Brian Foster4a4f66e2017-08-08 18:21:52 -07001047 if (!error && *tail_blk != orig_tail)
1048 xfs_warn(log->l_mp,
1049 "Tail block (0x%llx) overwrite detected. Updated to 0x%llx",
1050 orig_tail, *tail_blk);
Brian Foster7088c412016-01-05 07:40:16 +11001051out:
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001052 kmem_free(buffer);
Brian Foster7088c412016-01-05 07:40:16 +11001053 return error;
1054}
1055
1056/*
1057 * Detect and trim torn writes from the head of the log.
1058 *
1059 * Storage without sector atomicity guarantees can result in torn writes in the
1060 * log in the event of a crash. Our only means to detect this scenario is via
1061 * CRC verification. While we can't always be certain that CRC verification
1062 * failure is due to a torn write vs. an unrelated corruption, we do know that
1063 * only a certain number (XLOG_MAX_ICLOGS) of log records can be written out at
1064 * one time. Therefore, CRC verify up to XLOG_MAX_ICLOGS records at the head of
1065 * the log and treat failures in this range as torn writes as a matter of
1066 * policy. In the event of CRC failure, the head is walked back to the last good
1067 * record in the log and the tail is updated from that record and verified.
1068 */
1069STATIC int
1070xlog_verify_head(
1071 struct xlog *log,
1072 xfs_daddr_t *head_blk, /* in/out: unverified head */
1073 xfs_daddr_t *tail_blk, /* out: tail block */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001074 char *buffer,
Brian Foster7088c412016-01-05 07:40:16 +11001075 xfs_daddr_t *rhead_blk, /* start blk of last record */
1076 struct xlog_rec_header **rhead, /* ptr to last record */
1077 bool *wrapped) /* last rec. wraps phys. log */
1078{
1079 struct xlog_rec_header *tmp_rhead;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001080 char *tmp_buffer;
Brian Foster7088c412016-01-05 07:40:16 +11001081 xfs_daddr_t first_bad;
1082 xfs_daddr_t tmp_rhead_blk;
1083 int found;
1084 int error;
1085 bool tmp_wrapped;
1086
1087 /*
Brian Foster82ff6cc2016-03-07 08:22:22 +11001088 * Check the head of the log for torn writes. Search backwards from the
1089 * head until we hit the tail or the maximum number of log record I/Os
1090 * that could have been in flight at one time. Use a temporary buffer so
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001091 * we don't trash the rhead/buffer pointers from the caller.
Brian Foster7088c412016-01-05 07:40:16 +11001092 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001093 tmp_buffer = xlog_alloc_buffer(log, 1);
1094 if (!tmp_buffer)
Brian Foster7088c412016-01-05 07:40:16 +11001095 return -ENOMEM;
1096 error = xlog_rseek_logrec_hdr(log, *head_blk, *tail_blk,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001097 XLOG_MAX_ICLOGS, tmp_buffer,
1098 &tmp_rhead_blk, &tmp_rhead, &tmp_wrapped);
1099 kmem_free(tmp_buffer);
Brian Foster7088c412016-01-05 07:40:16 +11001100 if (error < 0)
1101 return error;
1102
1103 /*
1104 * Now run a CRC verification pass over the records starting at the
1105 * block found above to the current head. If a CRC failure occurs, the
1106 * log block of the first bad record is saved in first_bad.
1107 */
1108 error = xlog_do_recovery_pass(log, *head_blk, tmp_rhead_blk,
1109 XLOG_RECOVER_CRCPASS, &first_bad);
Brian Fostera4c9b342017-08-08 18:21:53 -07001110 if ((error == -EFSBADCRC || error == -EFSCORRUPTED) && first_bad) {
Brian Foster7088c412016-01-05 07:40:16 +11001111 /*
1112 * We've hit a potential torn write. Reset the error and warn
1113 * about it.
1114 */
1115 error = 0;
1116 xfs_warn(log->l_mp,
1117"Torn write (CRC failure) detected at log block 0x%llx. Truncating head block from 0x%llx.",
1118 first_bad, *head_blk);
1119
1120 /*
1121 * Get the header block and buffer pointer for the last good
1122 * record before the bad record.
1123 *
1124 * Note that xlog_find_tail() clears the blocks at the new head
1125 * (i.e., the records with invalid CRC) if the cycle number
1126 * matches the the current cycle.
1127 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001128 found = xlog_rseek_logrec_hdr(log, first_bad, *tail_blk, 1,
1129 buffer, rhead_blk, rhead, wrapped);
Brian Foster7088c412016-01-05 07:40:16 +11001130 if (found < 0)
1131 return found;
1132 if (found == 0) /* XXX: right thing to do here? */
1133 return -EIO;
1134
1135 /*
1136 * Reset the head block to the starting block of the first bad
1137 * log record and set the tail block based on the last good
1138 * record.
1139 *
1140 * Bail out if the updated head/tail match as this indicates
1141 * possible corruption outside of the acceptable
1142 * (XLOG_MAX_ICLOGS) range. This is a job for xfs_repair...
1143 */
1144 *head_blk = first_bad;
1145 *tail_blk = BLOCK_LSN(be64_to_cpu((*rhead)->h_tail_lsn));
1146 if (*head_blk == *tail_blk) {
1147 ASSERT(0);
1148 return 0;
1149 }
Brian Foster7088c412016-01-05 07:40:16 +11001150 }
Brian Foster5297ac12017-08-08 18:21:51 -07001151 if (error)
1152 return error;
Brian Foster7088c412016-01-05 07:40:16 +11001153
Brian Foster4a4f66e2017-08-08 18:21:52 -07001154 return xlog_verify_tail(log, *head_blk, tail_blk,
1155 be32_to_cpu((*rhead)->h_size));
Brian Foster7088c412016-01-05 07:40:16 +11001156}
1157
1158/*
Dave Chinner0703a8e2018-06-08 09:54:22 -07001159 * We need to make sure we handle log wrapping properly, so we can't use the
1160 * calculated logbno directly. Make sure it wraps to the correct bno inside the
1161 * log.
1162 *
1163 * The log is limited to 32 bit sizes, so we use the appropriate modulus
1164 * operation here and cast it back to a 64 bit daddr on return.
1165 */
1166static inline xfs_daddr_t
1167xlog_wrap_logbno(
1168 struct xlog *log,
1169 xfs_daddr_t bno)
1170{
1171 int mod;
1172
1173 div_s64_rem(bno, log->l_logBBsize, &mod);
1174 return mod;
1175}
1176
1177/*
Brian Foster65b99a02016-03-07 08:22:22 +11001178 * Check whether the head of the log points to an unmount record. In other
1179 * words, determine whether the log is clean. If so, update the in-core state
1180 * appropriately.
1181 */
1182static int
1183xlog_check_unmount_rec(
1184 struct xlog *log,
1185 xfs_daddr_t *head_blk,
1186 xfs_daddr_t *tail_blk,
1187 struct xlog_rec_header *rhead,
1188 xfs_daddr_t rhead_blk,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001189 char *buffer,
Brian Foster65b99a02016-03-07 08:22:22 +11001190 bool *clean)
1191{
1192 struct xlog_op_header *op_head;
1193 xfs_daddr_t umount_data_blk;
1194 xfs_daddr_t after_umount_blk;
1195 int hblks;
1196 int error;
1197 char *offset;
1198
1199 *clean = false;
1200
1201 /*
1202 * Look for unmount record. If we find it, then we know there was a
1203 * clean unmount. Since 'i' could be the last block in the physical
1204 * log, we convert to a log block before comparing to the head_blk.
1205 *
1206 * Save the current tail lsn to use to pass to xlog_clear_stale_blocks()
1207 * below. We won't want to clear the unmount record if there is one, so
1208 * we pass the lsn of the unmount record rather than the block after it.
1209 */
1210 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
1211 int h_size = be32_to_cpu(rhead->h_size);
1212 int h_version = be32_to_cpu(rhead->h_version);
1213
1214 if ((h_version & XLOG_VERSION_2) &&
1215 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
1216 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
1217 if (h_size % XLOG_HEADER_CYCLE_SIZE)
1218 hblks++;
1219 } else {
1220 hblks = 1;
1221 }
1222 } else {
1223 hblks = 1;
1224 }
Dave Chinner0703a8e2018-06-08 09:54:22 -07001225
1226 after_umount_blk = xlog_wrap_logbno(log,
1227 rhead_blk + hblks + BTOBB(be32_to_cpu(rhead->h_len)));
1228
Brian Foster65b99a02016-03-07 08:22:22 +11001229 if (*head_blk == after_umount_blk &&
1230 be32_to_cpu(rhead->h_num_logops) == 1) {
Dave Chinner0703a8e2018-06-08 09:54:22 -07001231 umount_data_blk = xlog_wrap_logbno(log, rhead_blk + hblks);
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001232 error = xlog_bread(log, umount_data_blk, 1, buffer, &offset);
Brian Foster65b99a02016-03-07 08:22:22 +11001233 if (error)
1234 return error;
1235
1236 op_head = (struct xlog_op_header *)offset;
1237 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
1238 /*
1239 * Set tail and last sync so that newly written log
1240 * records will point recovery to after the current
1241 * unmount record.
1242 */
1243 xlog_assign_atomic_lsn(&log->l_tail_lsn,
1244 log->l_curr_cycle, after_umount_blk);
1245 xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1246 log->l_curr_cycle, after_umount_blk);
1247 *tail_blk = after_umount_blk;
1248
1249 *clean = true;
1250 }
1251 }
1252
1253 return 0;
1254}
1255
Brian Foster717bc0e2016-03-07 08:22:22 +11001256static void
1257xlog_set_state(
1258 struct xlog *log,
1259 xfs_daddr_t head_blk,
1260 struct xlog_rec_header *rhead,
1261 xfs_daddr_t rhead_blk,
1262 bool bump_cycle)
1263{
1264 /*
1265 * Reset log values according to the state of the log when we
1266 * crashed. In the case where head_blk == 0, we bump curr_cycle
1267 * one because the next write starts a new cycle rather than
1268 * continuing the cycle of the last good log record. At this
1269 * point we have guaranteed that all partial log records have been
1270 * accounted for. Therefore, we know that the last good log record
1271 * written was complete and ended exactly on the end boundary
1272 * of the physical log.
1273 */
1274 log->l_prev_block = rhead_blk;
1275 log->l_curr_block = (int)head_blk;
1276 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
1277 if (bump_cycle)
1278 log->l_curr_cycle++;
1279 atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
1280 atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
1281 xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
1282 BBTOB(log->l_curr_block));
1283 xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
1284 BBTOB(log->l_curr_block));
1285}
1286
Brian Foster65b99a02016-03-07 08:22:22 +11001287/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 * Find the sync block number or the tail of the log.
1289 *
1290 * This will be the block number of the last record to have its
1291 * associated buffers synced to disk. Every log record header has
1292 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
1293 * to get a sync block number. The only concern is to figure out which
1294 * log record header to believe.
1295 *
1296 * The following algorithm uses the log record header with the largest
1297 * lsn. The entire log record does not need to be valid. We only care
1298 * that the header is valid.
1299 *
1300 * We could speed up search by using current head_blk buffer, but it is not
1301 * available.
1302 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +00001303STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304xlog_find_tail(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001305 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 xfs_daddr_t *head_blk,
Eric Sandeen65be6052006-01-11 15:34:19 +11001307 xfs_daddr_t *tail_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308{
1309 xlog_rec_header_t *rhead;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10001310 char *offset = NULL;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001311 char *buffer;
Brian Foster7088c412016-01-05 07:40:16 +11001312 int error;
Brian Foster7088c412016-01-05 07:40:16 +11001313 xfs_daddr_t rhead_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 xfs_lsn_t tail_lsn;
Brian Fostereed6b462016-01-04 15:55:10 +11001315 bool wrapped = false;
Brian Foster65b99a02016-03-07 08:22:22 +11001316 bool clean = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
1318 /*
1319 * Find previous log record
1320 */
1321 if ((error = xlog_find_head(log, head_blk)))
1322 return error;
Brian Foster82ff6cc2016-03-07 08:22:22 +11001323 ASSERT(*head_blk < INT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001325 buffer = xlog_alloc_buffer(log, 1);
1326 if (!buffer)
Dave Chinner24513372014-06-25 14:58:08 +10001327 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 if (*head_blk == 0) { /* special case */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001329 error = xlog_bread(log, 0, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001330 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +00001331 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001332
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001333 if (xlog_get_cycle(offset) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *tail_blk = 0;
1335 /* leave all other log inited values alone */
Alex Elder9db127e2010-04-15 18:17:26 +00001336 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 }
1338 }
1339
1340 /*
Brian Foster82ff6cc2016-03-07 08:22:22 +11001341 * Search backwards through the log looking for the log record header
1342 * block. This wraps all the way back around to the head so something is
1343 * seriously wrong if we can't find it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001345 error = xlog_rseek_logrec_hdr(log, *head_blk, *head_blk, 1, buffer,
Brian Foster82ff6cc2016-03-07 08:22:22 +11001346 &rhead_blk, &rhead, &wrapped);
1347 if (error < 0)
1348 return error;
1349 if (!error) {
1350 xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
1351 return -EIO;
1352 }
1353 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
1354
1355 /*
Brian Foster717bc0e2016-03-07 08:22:22 +11001356 * Set the log state based on the current head record.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 */
Brian Foster717bc0e2016-03-07 08:22:22 +11001358 xlog_set_state(log, *head_blk, rhead, rhead_blk, wrapped);
Brian Foster65b99a02016-03-07 08:22:22 +11001359 tail_lsn = atomic64_read(&log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360
1361 /*
Brian Foster65b99a02016-03-07 08:22:22 +11001362 * Look for an unmount record at the head of the log. This sets the log
1363 * state to determine whether recovery is necessary.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
Brian Foster65b99a02016-03-07 08:22:22 +11001365 error = xlog_check_unmount_rec(log, head_blk, tail_blk, rhead,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001366 rhead_blk, buffer, &clean);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 if (error)
1368 goto done;
1369
1370 /*
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001371 * Verify the log head if the log is not clean (e.g., we have anything
1372 * but an unmount record at the head). This uses CRC verification to
1373 * detect and trim torn writes. If discovered, CRC failures are
1374 * considered torn writes and the log head is trimmed accordingly.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 *
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001376 * Note that we can only run CRC verification when the log is dirty
1377 * because there's no guarantee that the log data behind an unmount
1378 * record is compatible with the current architecture.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 */
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001380 if (!clean) {
1381 xfs_daddr_t orig_head = *head_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001383 error = xlog_verify_head(log, head_blk, tail_blk, buffer,
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001384 &rhead_blk, &rhead, &wrapped);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001385 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +00001386 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001387
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001388 /* update in-core state again if the head changed */
1389 if (*head_blk != orig_head) {
1390 xlog_set_state(log, *head_blk, rhead, rhead_blk,
1391 wrapped);
1392 tail_lsn = atomic64_read(&log->l_tail_lsn);
1393 error = xlog_check_unmount_rec(log, head_blk, tail_blk,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001394 rhead, rhead_blk, buffer,
Brian Foster7f6aff3a2016-03-07 08:22:22 +11001395 &clean);
1396 if (error)
1397 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 }
1399 }
1400
1401 /*
Brian Foster65b99a02016-03-07 08:22:22 +11001402 * Note that the unmount was clean. If the unmount was not clean, we
1403 * need to know this to rebuild the superblock counters from the perag
1404 * headers if we have a filesystem using non-persistent counters.
1405 */
1406 if (clean)
1407 log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408
1409 /*
1410 * Make sure that there are no blocks in front of the head
1411 * with the same cycle number as the head. This can happen
1412 * because we allow multiple outstanding log writes concurrently,
1413 * and the later writes might make it out before earlier ones.
1414 *
1415 * We use the lsn from before modifying it so that we'll never
1416 * overwrite the unmount record after a clean unmount.
1417 *
1418 * Do this only if we are going to recover the filesystem
1419 *
1420 * NOTE: This used to say "if (!readonly)"
1421 * However on Linux, we can & do recover a read-only filesystem.
1422 * We only skip recovery if NORECOVERY is specified on mount,
1423 * in which case we would not be here.
1424 *
1425 * But... if the -device- itself is readonly, just skip this.
1426 * We can't recover this device anyway, so it won't matter.
1427 */
Christoph Hellwig2d15d2c2019-06-28 19:27:24 -07001428 if (!xfs_readonly_buftarg(log->l_targ))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 error = xlog_clear_stale_blocks(log, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Alex Elder9db127e2010-04-15 18:17:26 +00001431done:
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001432 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 if (error)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001435 xfs_warn(log->l_mp, "failed to locate log tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 return error;
1437}
1438
1439/*
1440 * Is the log zeroed at all?
1441 *
1442 * The last binary search should be changed to perform an X block read
1443 * once X becomes small enough. You can then search linearly through
1444 * the X blocks. This will cut down on the number of reads we need to do.
1445 *
1446 * If the log is partially zeroed, this routine will pass back the blkno
1447 * of the first block with cycle number 0. It won't have a complete LR
1448 * preceding it.
1449 *
1450 * Return:
1451 * 0 => the log is completely written to
Dave Chinner24513372014-06-25 14:58:08 +10001452 * 1 => use *blk_no as the first block of the log
1453 * <0 => error has occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
David Chinnera8272ce2007-11-23 16:28:09 +11001455STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456xlog_find_zeroed(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001457 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 xfs_daddr_t *blk_no)
1459{
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001460 char *buffer;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10001461 char *offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 uint first_cycle, last_cycle;
1463 xfs_daddr_t new_blk, last_blk, start_blk;
1464 xfs_daddr_t num_scan_bblks;
1465 int error, log_bbnum = log->l_logBBsize;
1466
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001467 *blk_no = 0;
1468
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 /* check totally zeroed log */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001470 buffer = xlog_alloc_buffer(log, 1);
1471 if (!buffer)
Dave Chinner24513372014-06-25 14:58:08 +10001472 return -ENOMEM;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001473 error = xlog_bread(log, 0, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001474 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001475 goto out_free_buffer;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001476
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001477 first_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 if (first_cycle == 0) { /* completely zeroed log */
1479 *blk_no = 0;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001480 kmem_free(buffer);
Dave Chinner24513372014-06-25 14:58:08 +10001481 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 }
1483
1484 /* check partially zeroed log */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001485 error = xlog_bread(log, log_bbnum-1, 1, buffer, &offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001486 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001487 goto out_free_buffer;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001488
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001489 last_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 if (last_cycle != 0) { /* log completely written to */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001491 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 }
1494
1495 /* we have a partially zeroed log */
1496 last_blk = log_bbnum-1;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001497 error = xlog_find_cycle_start(log, buffer, 0, &last_blk, 0);
1498 if (error)
1499 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500
1501 /*
1502 * Validate the answer. Because there is no way to guarantee that
1503 * the entire log is made up of log records which are the same size,
1504 * we scan over the defined maximum blocks. At this point, the maximum
1505 * is not chosen to mean anything special. XXXmiken
1506 */
1507 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1508 ASSERT(num_scan_bblks <= INT_MAX);
1509
1510 if (last_blk < num_scan_bblks)
1511 num_scan_bblks = last_blk;
1512 start_blk = last_blk - num_scan_bblks;
1513
1514 /*
1515 * We search for any instances of cycle number 0 that occur before
1516 * our current estimate of the head. What we're trying to detect is
1517 * 1 ... | 0 | 1 | 0...
1518 * ^ binary search ends here
1519 */
1520 if ((error = xlog_find_verify_cycle(log, start_blk,
1521 (int)num_scan_bblks, 0, &new_blk)))
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001522 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (new_blk != -1)
1524 last_blk = new_blk;
1525
1526 /*
1527 * Potentially backup over partial log record write. We don't need
1528 * to search the end of the log because we know it is zero.
1529 */
Dave Chinner24513372014-06-25 14:58:08 +10001530 error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1531 if (error == 1)
1532 error = -EIO;
1533 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001534 goto out_free_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 *blk_no = last_blk;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001537out_free_buffer:
1538 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 if (error)
1540 return error;
Dave Chinner24513372014-06-25 14:58:08 +10001541 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542}
1543
1544/*
1545 * These are simple subroutines used by xlog_clear_stale_blocks() below
1546 * to initialize a buffer full of empty log record headers and write
1547 * them into the log.
1548 */
1549STATIC void
1550xlog_add_record(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001551 struct xlog *log,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10001552 char *buf,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 int cycle,
1554 int block,
1555 int tail_cycle,
1556 int tail_block)
1557{
1558 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1559
1560 memset(buf, 0, BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001561 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1562 recp->h_cycle = cpu_to_be32(cycle);
1563 recp->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001564 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001565 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1566 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1567 recp->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1569}
1570
1571STATIC int
1572xlog_write_log_records(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001573 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 int cycle,
1575 int start_block,
1576 int blocks,
1577 int tail_cycle,
1578 int tail_block)
1579{
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10001580 char *offset;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001581 char *buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 int balign, ealign;
Alex Elder69ce58f2010-04-20 17:09:59 +10001583 int sectbb = log->l_sectBBsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 int end_block = start_block + blocks;
1585 int bufblks;
1586 int error = 0;
1587 int i, j = 0;
1588
Alex Elder6881a222010-04-13 15:22:29 +10001589 /*
1590 * Greedily allocate a buffer big enough to handle the full
1591 * range of basic blocks to be written. If that fails, try
1592 * a smaller size. We need to be able to write at least a
1593 * log sector, or we're out of luck.
1594 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 bufblks = 1 << ffs(blocks);
Dave Chinner81158e02012-04-27 19:45:22 +10001596 while (bufblks > log->l_logBBsize)
1597 bufblks >>= 1;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001598 while (!(buffer = xlog_alloc_buffer(log, bufblks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 bufblks >>= 1;
Alex Elder69ce58f2010-04-20 17:09:59 +10001600 if (bufblks < sectbb)
Dave Chinner24513372014-06-25 14:58:08 +10001601 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 }
1603
1604 /* We may need to do a read at the start to fill in part of
1605 * the buffer in the starting sector not covered by the first
1606 * write below.
1607 */
Alex Elder5c17f532010-04-13 15:22:48 +10001608 balign = round_down(start_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 if (balign != start_block) {
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001610 error = xlog_bread_noalign(log, start_block, 1, buffer);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001611 if (error)
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001612 goto out_free_buffer;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 j = start_block - balign;
1615 }
1616
1617 for (i = start_block; i < end_block; i += bufblks) {
1618 int bcount, endcount;
1619
1620 bcount = min(bufblks, end_block - start_block);
1621 endcount = bcount - j;
1622
1623 /* We may need to do a read at the end to fill in part of
1624 * the buffer in the final sector not covered by the write.
1625 * If this is the same sector as the above read, skip it.
1626 */
Alex Elder5c17f532010-04-13 15:22:48 +10001627 ealign = round_down(end_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 if (j == 0 && (start_block + endcount > ealign)) {
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07001629 error = xlog_bread_noalign(log, ealign, sectbb,
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001630 buffer + BBTOB(ealign - start_block));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001631 if (error)
1632 break;
1633
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 }
1635
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001636 offset = buffer + xlog_align(log, start_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 for (; j < endcount; j++) {
1638 xlog_add_record(log, offset, cycle, i+j,
1639 tail_cycle, tail_block);
1640 offset += BBSIZE;
1641 }
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001642 error = xlog_bwrite(log, start_block, endcount, buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 if (error)
1644 break;
1645 start_block += endcount;
1646 j = 0;
1647 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001648
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07001649out_free_buffer:
1650 kmem_free(buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return error;
1652}
1653
1654/*
1655 * This routine is called to blow away any incomplete log writes out
1656 * in front of the log head. We do this so that we won't become confused
1657 * if we come up, write only a little bit more, and then crash again.
1658 * If we leave the partial log records out there, this situation could
1659 * cause us to think those partial writes are valid blocks since they
1660 * have the current cycle number. We get rid of them by overwriting them
1661 * with empty log records with the old cycle number rather than the
1662 * current one.
1663 *
1664 * The tail lsn is passed in rather than taken from
1665 * the log so that we will not write over the unmount record after a
1666 * clean unmount in a 512 block log. Doing so would leave the log without
1667 * any valid log records in it until a new one was written. If we crashed
1668 * during that time we would not be able to recover.
1669 */
1670STATIC int
1671xlog_clear_stale_blocks(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001672 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 xfs_lsn_t tail_lsn)
1674{
1675 int tail_cycle, head_cycle;
1676 int tail_block, head_block;
1677 int tail_distance, max_distance;
1678 int distance;
1679 int error;
1680
1681 tail_cycle = CYCLE_LSN(tail_lsn);
1682 tail_block = BLOCK_LSN(tail_lsn);
1683 head_cycle = log->l_curr_cycle;
1684 head_block = log->l_curr_block;
1685
1686 /*
1687 * Figure out the distance between the new head of the log
1688 * and the tail. We want to write over any blocks beyond the
1689 * head that we may have written just before the crash, but
1690 * we don't want to overwrite the tail of the log.
1691 */
1692 if (head_cycle == tail_cycle) {
1693 /*
1694 * The tail is behind the head in the physical log,
1695 * so the distance from the head to the tail is the
1696 * distance from the head to the end of the log plus
1697 * the distance from the beginning of the log to the
1698 * tail.
1699 */
1700 if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
1701 XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
1702 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10001703 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 }
1705 tail_distance = tail_block + (log->l_logBBsize - head_block);
1706 } else {
1707 /*
1708 * The head is behind the tail in the physical log,
1709 * so the distance from the head to the tail is just
1710 * the tail block minus the head block.
1711 */
1712 if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
1713 XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
1714 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10001715 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 }
1717 tail_distance = tail_block - head_block;
1718 }
1719
1720 /*
1721 * If the head is right up against the tail, we can't clear
1722 * anything.
1723 */
1724 if (tail_distance <= 0) {
1725 ASSERT(tail_distance == 0);
1726 return 0;
1727 }
1728
1729 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1730 /*
1731 * Take the smaller of the maximum amount of outstanding I/O
1732 * we could have and the distance to the tail to clear out.
1733 * We take the smaller so that we don't overwrite the tail and
1734 * we don't waste all day writing from the head to the tail
1735 * for no reason.
1736 */
Dave Chinner9bb54cb2018-06-07 07:54:02 -07001737 max_distance = min(max_distance, tail_distance);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
1739 if ((head_block + max_distance) <= log->l_logBBsize) {
1740 /*
1741 * We can stomp all the blocks we need to without
1742 * wrapping around the end of the log. Just do it
1743 * in a single write. Use the cycle number of the
1744 * current cycle minus one so that the log will look like:
1745 * n ... | n - 1 ...
1746 */
1747 error = xlog_write_log_records(log, (head_cycle - 1),
1748 head_block, max_distance, tail_cycle,
1749 tail_block);
1750 if (error)
1751 return error;
1752 } else {
1753 /*
1754 * We need to wrap around the end of the physical log in
1755 * order to clear all the blocks. Do it in two separate
1756 * I/Os. The first write should be from the head to the
1757 * end of the physical log, and it should use the current
1758 * cycle number minus one just like above.
1759 */
1760 distance = log->l_logBBsize - head_block;
1761 error = xlog_write_log_records(log, (head_cycle - 1),
1762 head_block, distance, tail_cycle,
1763 tail_block);
1764
1765 if (error)
1766 return error;
1767
1768 /*
1769 * Now write the blocks at the start of the physical log.
1770 * This writes the remainder of the blocks we want to clear.
1771 * It uses the current cycle number since we're now on the
1772 * same cycle as the head so that we get:
1773 * n ... n ... | n - 1 ...
1774 * ^^^^^ blocks we're writing
1775 */
1776 distance = max_distance - (log->l_logBBsize - head_block);
1777 error = xlog_write_log_records(log, head_cycle, 0, distance,
1778 tail_cycle, tail_block);
1779 if (error)
1780 return error;
1781 }
1782
1783 return 0;
1784}
1785
1786/******************************************************************************
1787 *
1788 * Log recover routines
1789 *
1790 ******************************************************************************
1791 */
1792
Dave Chinnerf0a76952010-01-11 11:49:57 +00001793/*
Dave Chinnera775ad72013-06-05 12:09:07 +10001794 * Sort the log items in the transaction.
1795 *
1796 * The ordering constraints are defined by the inode allocation and unlink
1797 * behaviour. The rules are:
1798 *
1799 * 1. Every item is only logged once in a given transaction. Hence it
1800 * represents the last logged state of the item. Hence ordering is
1801 * dependent on the order in which operations need to be performed so
1802 * required initial conditions are always met.
1803 *
1804 * 2. Cancelled buffers are recorded in pass 1 in a separate table and
1805 * there's nothing to replay from them so we can simply cull them
1806 * from the transaction. However, we can't do that until after we've
1807 * replayed all the other items because they may be dependent on the
1808 * cancelled buffer and replaying the cancelled buffer can remove it
1809 * form the cancelled buffer table. Hence they have tobe done last.
1810 *
1811 * 3. Inode allocation buffers must be replayed before inode items that
Dave Chinner28c8e412013-06-27 16:04:55 +10001812 * read the buffer and replay changes into it. For filesystems using the
1813 * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
1814 * treated the same as inode allocation buffers as they create and
1815 * initialise the buffers directly.
Dave Chinnera775ad72013-06-05 12:09:07 +10001816 *
1817 * 4. Inode unlink buffers must be replayed after inode items are replayed.
1818 * This ensures that inodes are completely flushed to the inode buffer
1819 * in a "free" state before we remove the unlinked inode list pointer.
1820 *
1821 * Hence the ordering needs to be inode allocation buffers first, inode items
1822 * second, inode unlink buffers third and cancelled buffers last.
1823 *
1824 * But there's a problem with that - we can't tell an inode allocation buffer
1825 * apart from a regular buffer, so we can't separate them. We can, however,
1826 * tell an inode unlink buffer from the others, and so we can separate them out
1827 * from all the other buffers and move them to last.
1828 *
1829 * Hence, 4 lists, in order from head to tail:
Dave Chinner28c8e412013-06-27 16:04:55 +10001830 * - buffer_list for all buffers except cancelled/inode unlink buffers
1831 * - item_list for all non-buffer items
1832 * - inode_buffer_list for inode unlink buffers
1833 * - cancel_list for the cancelled buffers
1834 *
1835 * Note that we add objects to the tail of the lists so that first-to-last
1836 * ordering is preserved within the lists. Adding objects to the head of the
1837 * list means when we traverse from the head we walk them in last-to-first
1838 * order. For cancelled buffers and inode unlink buffers this doesn't matter,
1839 * but for all other items there may be specific ordering that we need to
1840 * preserve.
Dave Chinnerf0a76952010-01-11 11:49:57 +00001841 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842STATIC int
1843xlog_recover_reorder_trans(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001844 struct xlog *log,
1845 struct xlog_recover *trans,
Dave Chinner9abbc532010-04-13 15:06:46 +10001846 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001848 xlog_recover_item_t *item, *n;
Mark Tinguely2a841082013-10-02 07:51:12 -05001849 int error = 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001850 LIST_HEAD(sort_list);
Dave Chinnera775ad72013-06-05 12:09:07 +10001851 LIST_HEAD(cancel_list);
1852 LIST_HEAD(buffer_list);
1853 LIST_HEAD(inode_buffer_list);
1854 LIST_HEAD(inode_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Dave Chinnerf0a76952010-01-11 11:49:57 +00001856 list_splice_init(&trans->r_itemq, &sort_list);
1857 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10001858 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001859
1860 switch (ITEM_TYPE(item)) {
Dave Chinner28c8e412013-06-27 16:04:55 +10001861 case XFS_LI_ICREATE:
1862 list_move_tail(&item->ri_list, &buffer_list);
1863 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 case XFS_LI_BUF:
Dave Chinnera775ad72013-06-05 12:09:07 +10001865 if (buf_f->blf_flags & XFS_BLF_CANCEL) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001866 trace_xfs_log_recover_item_reorder_head(log,
1867 trans, item, pass);
Dave Chinnera775ad72013-06-05 12:09:07 +10001868 list_move(&item->ri_list, &cancel_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 break;
1870 }
Dave Chinnera775ad72013-06-05 12:09:07 +10001871 if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
1872 list_move(&item->ri_list, &inode_buffer_list);
1873 break;
1874 }
1875 list_move_tail(&item->ri_list, &buffer_list);
1876 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 case XFS_LI_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 case XFS_LI_DQUOT:
1879 case XFS_LI_QUOTAOFF:
1880 case XFS_LI_EFD:
1881 case XFS_LI_EFI:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10001882 case XFS_LI_RUI:
1883 case XFS_LI_RUD:
Darrick J. Wongf997ee22016-10-03 09:11:21 -07001884 case XFS_LI_CUI:
1885 case XFS_LI_CUD:
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07001886 case XFS_LI_BUI:
1887 case XFS_LI_BUD:
Dave Chinner9abbc532010-04-13 15:06:46 +10001888 trace_xfs_log_recover_item_reorder_tail(log,
1889 trans, item, pass);
Dave Chinnera775ad72013-06-05 12:09:07 +10001890 list_move_tail(&item->ri_list, &inode_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 break;
1892 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001893 xfs_warn(log->l_mp,
1894 "%s: unrecognized type of log operation",
1895 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 ASSERT(0);
Mark Tinguely2a841082013-10-02 07:51:12 -05001897 /*
1898 * return the remaining items back to the transaction
1899 * item list so they can be freed in caller.
1900 */
1901 if (!list_empty(&sort_list))
1902 list_splice_init(&sort_list, &trans->r_itemq);
Dave Chinner24513372014-06-25 14:58:08 +10001903 error = -EIO;
Mark Tinguely2a841082013-10-02 07:51:12 -05001904 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001906 }
Mark Tinguely2a841082013-10-02 07:51:12 -05001907out:
Dave Chinnerf0a76952010-01-11 11:49:57 +00001908 ASSERT(list_empty(&sort_list));
Dave Chinnera775ad72013-06-05 12:09:07 +10001909 if (!list_empty(&buffer_list))
1910 list_splice(&buffer_list, &trans->r_itemq);
1911 if (!list_empty(&inode_list))
1912 list_splice_tail(&inode_list, &trans->r_itemq);
1913 if (!list_empty(&inode_buffer_list))
1914 list_splice_tail(&inode_buffer_list, &trans->r_itemq);
1915 if (!list_empty(&cancel_list))
1916 list_splice_tail(&cancel_list, &trans->r_itemq);
Mark Tinguely2a841082013-10-02 07:51:12 -05001917 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
1920/*
1921 * Build up the table of buf cancel records so that we don't replay
1922 * cancelled data in the second pass. For buffer records that are
1923 * not cancel records, there is nothing to do here so we just return.
1924 *
1925 * If we get a cancel record which is already in the table, this indicates
1926 * that the buffer was cancelled multiple times. In order to ensure
1927 * that during pass 2 we keep the record in the table until we reach its
1928 * last occurrence in the log, we keep a reference count in the cancel
1929 * record in the table to tell us how many times we expect to see this
1930 * record during the second pass.
1931 */
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001932STATIC int
1933xlog_recover_buffer_pass1(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001934 struct xlog *log,
1935 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001937 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001938 struct list_head *bucket;
1939 struct xfs_buf_cancel *bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
1941 /*
1942 * If this isn't a cancel buffer item, then just return.
1943 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001944 if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001945 trace_xfs_log_recover_buf_not_cancel(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001946 return 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10001947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 /*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001950 * Insert an xfs_buf_cancel record into the hash table of them.
1951 * If there is already an identical record, bump its reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001953 bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
1954 list_for_each_entry(bcp, bucket, bc_list) {
1955 if (bcp->bc_blkno == buf_f->blf_blkno &&
1956 bcp->bc_len == buf_f->blf_len) {
1957 bcp->bc_refcount++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001958 trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001959 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001962
1963 bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
1964 bcp->bc_blkno = buf_f->blf_blkno;
1965 bcp->bc_len = buf_f->blf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 bcp->bc_refcount = 1;
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001967 list_add_tail(&bcp->bc_list, bucket);
1968
Dave Chinner9abbc532010-04-13 15:06:46 +10001969 trace_xfs_log_recover_buf_cancel_add(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001970 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
1973/*
1974 * Check to see whether the buffer being recovered has a corresponding
Dave Chinner84a5b732013-08-27 08:10:53 +10001975 * entry in the buffer cancel record table. If it is, return the cancel
1976 * buffer structure to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Dave Chinner84a5b732013-08-27 08:10:53 +10001978STATIC struct xfs_buf_cancel *
1979xlog_peek_buffer_cancelled(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001980 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 xfs_daddr_t blkno,
1982 uint len,
Darrick J. Wong755c7bf2016-11-08 11:55:48 +11001983 unsigned short flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001985 struct list_head *bucket;
1986 struct xfs_buf_cancel *bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987
Dave Chinner84a5b732013-08-27 08:10:53 +10001988 if (!log->l_buf_cancel_table) {
1989 /* empty table means no cancelled buffers in the log */
Dave Chinnerc1155412010-05-07 11:05:19 +10001990 ASSERT(!(flags & XFS_BLF_CANCEL));
Dave Chinner84a5b732013-08-27 08:10:53 +10001991 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
1993
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001994 bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
1995 list_for_each_entry(bcp, bucket, bc_list) {
1996 if (bcp->bc_blkno == blkno && bcp->bc_len == len)
Dave Chinner84a5b732013-08-27 08:10:53 +10001997 return bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
2000 /*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00002001 * We didn't find a corresponding entry in the table, so return 0 so
2002 * that the buffer is NOT cancelled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 */
Dave Chinnerc1155412010-05-07 11:05:19 +10002004 ASSERT(!(flags & XFS_BLF_CANCEL));
Dave Chinner84a5b732013-08-27 08:10:53 +10002005 return NULL;
2006}
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00002007
Dave Chinner84a5b732013-08-27 08:10:53 +10002008/*
2009 * If the buffer is being cancelled then return 1 so that it will be cancelled,
2010 * otherwise return 0. If the buffer is actually a buffer cancel item
2011 * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
2012 * table and remove it from the table if this is the last reference.
2013 *
2014 * We remove the cancel record from the table when we encounter its last
2015 * occurrence in the log so that if the same buffer is re-used again after its
2016 * last cancellation we actually replay the changes made at that point.
2017 */
2018STATIC int
2019xlog_check_buffer_cancelled(
2020 struct xlog *log,
2021 xfs_daddr_t blkno,
2022 uint len,
Darrick J. Wong755c7bf2016-11-08 11:55:48 +11002023 unsigned short flags)
Dave Chinner84a5b732013-08-27 08:10:53 +10002024{
2025 struct xfs_buf_cancel *bcp;
2026
2027 bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
2028 if (!bcp)
2029 return 0;
2030
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00002031 /*
2032 * We've go a match, so return 1 so that the recovery of this buffer
2033 * is cancelled. If this buffer is actually a buffer cancel log
2034 * item, then decrement the refcount on the one in the table and
2035 * remove it if this is the last reference.
2036 */
2037 if (flags & XFS_BLF_CANCEL) {
2038 if (--bcp->bc_refcount == 0) {
2039 list_del(&bcp->bc_list);
2040 kmem_free(bcp);
2041 }
2042 }
2043 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046/*
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002047 * Perform recovery for a buffer full of inodes. In these buffers, the only
2048 * data which should be recovered is that which corresponds to the
2049 * di_next_unlinked pointers in the on disk inode structures. The rest of the
2050 * data for the inodes is always logged through the inodes themselves rather
2051 * than the inode buffer and is recovered in xlog_recover_inode_pass2().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 *
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002053 * The only time when buffers full of inodes are fully recovered is when the
2054 * buffer is full of newly allocated inodes. In this case the buffer will
2055 * not be marked as an inode buffer and so will be sent to
2056 * xlog_recover_do_reg_buffer() below during recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 */
2058STATIC int
2059xlog_recover_do_inode_buffer(
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002060 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 xlog_recover_item_t *item,
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002062 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 xfs_buf_log_format_t *buf_f)
2064{
2065 int i;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002066 int item_index = 0;
2067 int bit = 0;
2068 int nbits = 0;
2069 int reg_buf_offset = 0;
2070 int reg_buf_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 int next_unlinked_offset;
2072 int inodes_per_buf;
2073 xfs_agino_t *logged_nextp;
2074 xfs_agino_t *buffer_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
Dave Chinner9abbc532010-04-13 15:06:46 +10002076 trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
Dave Chinner9222a9c2013-06-12 12:19:06 +10002077
2078 /*
2079 * Post recovery validation only works properly on CRC enabled
2080 * filesystems.
2081 */
2082 if (xfs_sb_version_hascrc(&mp->m_sb))
2083 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinner9abbc532010-04-13 15:06:46 +10002084
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07002085 inodes_per_buf = BBTOB(bp->b_length) >> mp->m_sb.sb_inodelog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 for (i = 0; i < inodes_per_buf; i++) {
2087 next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
2088 offsetof(xfs_dinode_t, di_next_unlinked);
2089
2090 while (next_unlinked_offset >=
2091 (reg_buf_offset + reg_buf_bytes)) {
2092 /*
2093 * The next di_next_unlinked field is beyond
2094 * the current logged region. Find the next
2095 * logged region that contains or is beyond
2096 * the current di_next_unlinked field.
2097 */
2098 bit += nbits;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002099 bit = xfs_next_bit(buf_f->blf_data_map,
2100 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /*
2103 * If there are no more logged regions in the
2104 * buffer, then we're done.
2105 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002106 if (bit == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002109 nbits = xfs_contig_bits(buf_f->blf_data_map,
2110 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 ASSERT(nbits > 0);
Dave Chinnerc1155412010-05-07 11:05:19 +10002112 reg_buf_offset = bit << XFS_BLF_SHIFT;
2113 reg_buf_bytes = nbits << XFS_BLF_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 item_index++;
2115 }
2116
2117 /*
2118 * If the current logged region starts after the current
2119 * di_next_unlinked field, then move on to the next
2120 * di_next_unlinked field.
2121 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002122 if (next_unlinked_offset < reg_buf_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
2125 ASSERT(item->ri_buf[item_index].i_addr != NULL);
Dave Chinnerc1155412010-05-07 11:05:19 +10002126 ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07002127 ASSERT((reg_buf_offset + reg_buf_bytes) <= BBTOB(bp->b_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /*
2130 * The current logged region contains a copy of the
2131 * current di_next_unlinked field. Extract its value
2132 * and copy it to the buffer copy.
2133 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002134 logged_nextp = item->ri_buf[item_index].i_addr +
2135 next_unlinked_offset - reg_buf_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (unlikely(*logged_nextp == 0)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002137 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08002138 "Bad inode buffer log record (ptr = "PTR_FMT", bp = "PTR_FMT"). "
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002139 "Trying to replay bad (0) inode di_next_unlinked field.",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 item, bp);
2141 XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
2142 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002143 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 }
2145
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10002146 buffer_nextp = xfs_buf_offset(bp, next_unlinked_offset);
Tim Shimmin87c199c2006-06-09 14:56:16 +10002147 *buffer_nextp = *logged_nextp;
Dave Chinner0a32c262013-06-05 12:09:08 +10002148
2149 /*
2150 * If necessary, recalculate the CRC in the on-disk inode. We
2151 * have to leave the inode in a consistent state for whoever
2152 * reads it next....
2153 */
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10002154 xfs_dinode_calc_crc(mp,
Dave Chinner0a32c262013-06-05 12:09:08 +10002155 xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
2156
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 }
2158
2159 return 0;
2160}
2161
2162/*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002163 * V5 filesystems know the age of the buffer on disk being recovered. We can
2164 * have newer objects on disk than we are replaying, and so for these cases we
2165 * don't want to replay the current change as that will make the buffer contents
2166 * temporarily invalid on disk.
2167 *
2168 * The magic number might not match the buffer type we are going to recover
2169 * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
2170 * extract the LSN of the existing object in the buffer based on it's current
2171 * magic number. If we don't recognise the magic number in the buffer, then
2172 * return a LSN of -1 so that the caller knows it was an unrecognised block and
2173 * so can recover the buffer.
Dave Chinner566055d2013-09-24 16:01:16 +10002174 *
2175 * Note: we cannot rely solely on magic number matches to determine that the
2176 * buffer has a valid LSN - we also need to verify that it belongs to this
2177 * filesystem, so we need to extract the object's LSN and compare it to that
2178 * which we read from the superblock. If the UUIDs don't match, then we've got a
2179 * stale metadata block from an old filesystem instance that we need to recover
2180 * over the top of.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002181 */
2182static xfs_lsn_t
2183xlog_recover_get_buf_lsn(
2184 struct xfs_mount *mp,
2185 struct xfs_buf *bp)
2186{
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002187 uint32_t magic32;
2188 uint16_t magic16;
2189 uint16_t magicda;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002190 void *blk = bp->b_addr;
Dave Chinner566055d2013-09-24 16:01:16 +10002191 uuid_t *uuid;
2192 xfs_lsn_t lsn = -1;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002193
2194 /* v4 filesystems always recover immediately */
2195 if (!xfs_sb_version_hascrc(&mp->m_sb))
2196 goto recover_immediately;
2197
2198 magic32 = be32_to_cpu(*(__be32 *)blk);
2199 switch (magic32) {
2200 case XFS_ABTB_CRC_MAGIC:
2201 case XFS_ABTC_CRC_MAGIC:
2202 case XFS_ABTB_MAGIC:
2203 case XFS_ABTC_MAGIC:
Darrick J. Wonga650e8f2016-08-03 12:17:11 +10002204 case XFS_RMAP_CRC_MAGIC:
Darrick J. Wonga90c00f2016-10-03 09:11:23 -07002205 case XFS_REFC_CRC_MAGIC:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002206 case XFS_IBT_CRC_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002207 case XFS_IBT_MAGIC: {
2208 struct xfs_btree_block *btb = blk;
2209
2210 lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
2211 uuid = &btb->bb_u.s.bb_uuid;
2212 break;
2213 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002214 case XFS_BMAP_CRC_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002215 case XFS_BMAP_MAGIC: {
2216 struct xfs_btree_block *btb = blk;
2217
2218 lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
2219 uuid = &btb->bb_u.l.bb_uuid;
2220 break;
2221 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002222 case XFS_AGF_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002223 lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
2224 uuid = &((struct xfs_agf *)blk)->agf_uuid;
2225 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002226 case XFS_AGFL_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002227 lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
2228 uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
2229 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002230 case XFS_AGI_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002231 lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
2232 uuid = &((struct xfs_agi *)blk)->agi_uuid;
2233 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002234 case XFS_SYMLINK_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002235 lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
2236 uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
2237 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002238 case XFS_DIR3_BLOCK_MAGIC:
2239 case XFS_DIR3_DATA_MAGIC:
2240 case XFS_DIR3_FREE_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002241 lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
2242 uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
2243 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002244 case XFS_ATTR3_RMT_MAGIC:
Dave Chinnere3c32ee2015-07-29 11:48:01 +10002245 /*
2246 * Remote attr blocks are written synchronously, rather than
2247 * being logged. That means they do not contain a valid LSN
2248 * (i.e. transactionally ordered) in them, and hence any time we
2249 * see a buffer to replay over the top of a remote attribute
2250 * block we should simply do so.
2251 */
2252 goto recover_immediately;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002253 case XFS_SB_MAGIC:
Dave Chinnerfcfbe2c2015-08-19 10:31:54 +10002254 /*
2255 * superblock uuids are magic. We may or may not have a
2256 * sb_meta_uuid on disk, but it will be set in the in-core
2257 * superblock. We set the uuid pointer for verification
2258 * according to the superblock feature mask to ensure we check
2259 * the relevant UUID in the superblock.
2260 */
Dave Chinner566055d2013-09-24 16:01:16 +10002261 lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
Dave Chinnerfcfbe2c2015-08-19 10:31:54 +10002262 if (xfs_sb_version_hasmetauuid(&mp->m_sb))
2263 uuid = &((struct xfs_dsb *)blk)->sb_meta_uuid;
2264 else
2265 uuid = &((struct xfs_dsb *)blk)->sb_uuid;
Dave Chinner566055d2013-09-24 16:01:16 +10002266 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002267 default:
2268 break;
2269 }
2270
Dave Chinner566055d2013-09-24 16:01:16 +10002271 if (lsn != (xfs_lsn_t)-1) {
Dave Chinnerfcfbe2c2015-08-19 10:31:54 +10002272 if (!uuid_equal(&mp->m_sb.sb_meta_uuid, uuid))
Dave Chinner566055d2013-09-24 16:01:16 +10002273 goto recover_immediately;
2274 return lsn;
2275 }
2276
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002277 magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
2278 switch (magicda) {
2279 case XFS_DIR3_LEAF1_MAGIC:
2280 case XFS_DIR3_LEAFN_MAGIC:
2281 case XFS_DA3_NODE_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10002282 lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
2283 uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
2284 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002285 default:
2286 break;
2287 }
2288
Dave Chinner566055d2013-09-24 16:01:16 +10002289 if (lsn != (xfs_lsn_t)-1) {
2290 if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
2291 goto recover_immediately;
2292 return lsn;
2293 }
2294
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002295 /*
2296 * We do individual object checks on dquot and inode buffers as they
2297 * have their own individual LSN records. Also, we could have a stale
2298 * buffer here, so we have to at least recognise these buffer types.
2299 *
2300 * A notd complexity here is inode unlinked list processing - it logs
2301 * the inode directly in the buffer, but we don't know which inodes have
2302 * been modified, and there is no global buffer LSN. Hence we need to
2303 * recover all inode buffer types immediately. This problem will be
2304 * fixed by logical logging of the unlinked list modifications.
2305 */
2306 magic16 = be16_to_cpu(*(__be16 *)blk);
2307 switch (magic16) {
2308 case XFS_DQUOT_MAGIC:
2309 case XFS_DINODE_MAGIC:
2310 goto recover_immediately;
2311 default:
2312 break;
2313 }
2314
2315 /* unknown buffer contents, recover immediately */
2316
2317recover_immediately:
2318 return (xfs_lsn_t)-1;
2319
2320}
2321
2322/*
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002323 * Validate the recovered buffer is of the correct type and attach the
2324 * appropriate buffer operations to them for writeback. Magic numbers are in a
2325 * few places:
2326 * the first 16 bits of the buffer (inode buffer, dquot buffer),
2327 * the first 32 bits of the buffer (most blocks),
2328 * inside a struct xfs_da_blkinfo at the start of the buffer.
2329 */
2330static void
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002331xlog_recover_validate_buf_type(
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002332 struct xfs_mount *mp,
2333 struct xfs_buf *bp,
Brian Foster22db9af2016-09-26 08:32:07 +10002334 xfs_buf_log_format_t *buf_f,
2335 xfs_lsn_t current_lsn)
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002336{
2337 struct xfs_da_blkinfo *info = bp->b_addr;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07002338 uint32_t magic32;
2339 uint16_t magic16;
2340 uint16_t magicda;
Brian Foster040c52c2016-09-26 08:32:50 +10002341 char *warnmsg = NULL;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002342
Dave Chinner67dc2882014-08-04 12:43:06 +10002343 /*
2344 * We can only do post recovery validation on items on CRC enabled
2345 * fielsystems as we need to know when the buffer was written to be able
2346 * to determine if we should have replayed the item. If we replay old
2347 * metadata over a newer buffer, then it will enter a temporarily
2348 * inconsistent state resulting in verification failures. Hence for now
2349 * just avoid the verification stage for non-crc filesystems
2350 */
2351 if (!xfs_sb_version_hascrc(&mp->m_sb))
2352 return;
2353
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002354 magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
2355 magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
2356 magicda = be16_to_cpu(info->magic);
Dave Chinner61fe1352013-04-03 16:11:30 +11002357 switch (xfs_blft_from_flags(buf_f)) {
2358 case XFS_BLFT_BTREE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002359 switch (magic32) {
2360 case XFS_ABTB_CRC_MAGIC:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002361 case XFS_ABTB_MAGIC:
Brian Foster27df4f52019-02-07 10:45:47 -08002362 bp->b_ops = &xfs_bnobt_buf_ops;
2363 break;
2364 case XFS_ABTC_CRC_MAGIC:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002365 case XFS_ABTC_MAGIC:
Brian Foster27df4f52019-02-07 10:45:47 -08002366 bp->b_ops = &xfs_cntbt_buf_ops;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002367 break;
2368 case XFS_IBT_CRC_MAGIC:
2369 case XFS_IBT_MAGIC:
2370 bp->b_ops = &xfs_inobt_buf_ops;
2371 break;
Brian Foster01e68f42019-02-07 10:45:46 -08002372 case XFS_FIBT_CRC_MAGIC:
2373 case XFS_FIBT_MAGIC:
2374 bp->b_ops = &xfs_finobt_buf_ops;
2375 break;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002376 case XFS_BMAP_CRC_MAGIC:
2377 case XFS_BMAP_MAGIC:
2378 bp->b_ops = &xfs_bmbt_buf_ops;
2379 break;
Darrick J. Wonga650e8f2016-08-03 12:17:11 +10002380 case XFS_RMAP_CRC_MAGIC:
2381 bp->b_ops = &xfs_rmapbt_buf_ops;
2382 break;
Darrick J. Wonga90c00f2016-10-03 09:11:23 -07002383 case XFS_REFC_CRC_MAGIC:
2384 bp->b_ops = &xfs_refcountbt_buf_ops;
2385 break;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002386 default:
Brian Foster040c52c2016-09-26 08:32:50 +10002387 warnmsg = "Bad btree block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002388 break;
2389 }
2390 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002391 case XFS_BLFT_AGF_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002392 if (magic32 != XFS_AGF_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002393 warnmsg = "Bad AGF block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002394 break;
2395 }
2396 bp->b_ops = &xfs_agf_buf_ops;
2397 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002398 case XFS_BLFT_AGFL_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002399 if (magic32 != XFS_AGFL_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002400 warnmsg = "Bad AGFL block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002401 break;
2402 }
2403 bp->b_ops = &xfs_agfl_buf_ops;
2404 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002405 case XFS_BLFT_AGI_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002406 if (magic32 != XFS_AGI_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002407 warnmsg = "Bad AGI block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002408 break;
2409 }
2410 bp->b_ops = &xfs_agi_buf_ops;
2411 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002412 case XFS_BLFT_UDQUOT_BUF:
2413 case XFS_BLFT_PDQUOT_BUF:
2414 case XFS_BLFT_GDQUOT_BUF:
Dave Chinner123887e2013-04-30 21:39:33 +10002415#ifdef CONFIG_XFS_QUOTA
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002416 if (magic16 != XFS_DQUOT_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002417 warnmsg = "Bad DQUOT block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002418 break;
2419 }
2420 bp->b_ops = &xfs_dquot_buf_ops;
Dave Chinner123887e2013-04-30 21:39:33 +10002421#else
2422 xfs_alert(mp,
2423 "Trying to recover dquots without QUOTA support built in!");
2424 ASSERT(0);
2425#endif
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002426 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002427 case XFS_BLFT_DINO_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002428 if (magic16 != XFS_DINODE_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002429 warnmsg = "Bad INODE block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002430 break;
2431 }
2432 bp->b_ops = &xfs_inode_buf_ops;
2433 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002434 case XFS_BLFT_SYMLINK_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002435 if (magic32 != XFS_SYMLINK_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002436 warnmsg = "Bad symlink block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002437 break;
2438 }
2439 bp->b_ops = &xfs_symlink_buf_ops;
2440 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002441 case XFS_BLFT_DIR_BLOCK_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002442 if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
2443 magic32 != XFS_DIR3_BLOCK_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002444 warnmsg = "Bad dir block magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002445 break;
2446 }
2447 bp->b_ops = &xfs_dir3_block_buf_ops;
2448 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002449 case XFS_BLFT_DIR_DATA_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002450 if (magic32 != XFS_DIR2_DATA_MAGIC &&
2451 magic32 != XFS_DIR3_DATA_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002452 warnmsg = "Bad dir data magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002453 break;
2454 }
2455 bp->b_ops = &xfs_dir3_data_buf_ops;
2456 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002457 case XFS_BLFT_DIR_FREE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002458 if (magic32 != XFS_DIR2_FREE_MAGIC &&
2459 magic32 != XFS_DIR3_FREE_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002460 warnmsg = "Bad dir3 free magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002461 break;
2462 }
2463 bp->b_ops = &xfs_dir3_free_buf_ops;
2464 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002465 case XFS_BLFT_DIR_LEAF1_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002466 if (magicda != XFS_DIR2_LEAF1_MAGIC &&
2467 magicda != XFS_DIR3_LEAF1_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002468 warnmsg = "Bad dir leaf1 magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002469 break;
2470 }
2471 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
2472 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002473 case XFS_BLFT_DIR_LEAFN_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002474 if (magicda != XFS_DIR2_LEAFN_MAGIC &&
2475 magicda != XFS_DIR3_LEAFN_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002476 warnmsg = "Bad dir leafn magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002477 break;
2478 }
2479 bp->b_ops = &xfs_dir3_leafn_buf_ops;
2480 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002481 case XFS_BLFT_DA_NODE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002482 if (magicda != XFS_DA_NODE_MAGIC &&
2483 magicda != XFS_DA3_NODE_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002484 warnmsg = "Bad da node magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002485 break;
2486 }
2487 bp->b_ops = &xfs_da3_node_buf_ops;
2488 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002489 case XFS_BLFT_ATTR_LEAF_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002490 if (magicda != XFS_ATTR_LEAF_MAGIC &&
2491 magicda != XFS_ATTR3_LEAF_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002492 warnmsg = "Bad attr leaf magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002493 break;
2494 }
2495 bp->b_ops = &xfs_attr3_leaf_buf_ops;
2496 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002497 case XFS_BLFT_ATTR_RMT_BUF:
Dave Chinnercab09a82013-04-30 21:39:36 +10002498 if (magic32 != XFS_ATTR3_RMT_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002499 warnmsg = "Bad attr remote magic!";
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002500 break;
2501 }
2502 bp->b_ops = &xfs_attr3_rmt_buf_ops;
2503 break;
Dave Chinner04a1e6c2013-04-03 16:11:31 +11002504 case XFS_BLFT_SB_BUF:
2505 if (magic32 != XFS_SB_MAGIC) {
Brian Foster040c52c2016-09-26 08:32:50 +10002506 warnmsg = "Bad SB block magic!";
Dave Chinner04a1e6c2013-04-03 16:11:31 +11002507 break;
2508 }
2509 bp->b_ops = &xfs_sb_buf_ops;
2510 break;
Dave Chinnerf67ca6e2016-02-09 16:41:31 +11002511#ifdef CONFIG_XFS_RT
2512 case XFS_BLFT_RTBITMAP_BUF:
2513 case XFS_BLFT_RTSUMMARY_BUF:
Dave Chinnerbf85e092016-02-09 16:41:45 +11002514 /* no magic numbers for verification of RT buffers */
2515 bp->b_ops = &xfs_rtbuf_ops;
Dave Chinnerf67ca6e2016-02-09 16:41:31 +11002516 break;
2517#endif /* CONFIG_XFS_RT */
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002518 default:
Dave Chinner61fe1352013-04-03 16:11:30 +11002519 xfs_warn(mp, "Unknown buffer type %d!",
2520 xfs_blft_from_flags(buf_f));
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002521 break;
2522 }
Brian Foster040c52c2016-09-26 08:32:50 +10002523
2524 /*
Brian Foster60a4a222016-09-26 08:34:27 +10002525 * Nothing else to do in the case of a NULL current LSN as this means
2526 * the buffer is more recent than the change in the log and will be
2527 * skipped.
Brian Foster040c52c2016-09-26 08:32:50 +10002528 */
Brian Foster60a4a222016-09-26 08:34:27 +10002529 if (current_lsn == NULLCOMMITLSN)
2530 return;
2531
2532 if (warnmsg) {
Brian Foster040c52c2016-09-26 08:32:50 +10002533 xfs_warn(mp, warnmsg);
2534 ASSERT(0);
2535 }
Brian Foster60a4a222016-09-26 08:34:27 +10002536
2537 /*
2538 * We must update the metadata LSN of the buffer as it is written out to
2539 * ensure that older transactions never replay over this one and corrupt
2540 * the buffer. This can occur if log recovery is interrupted at some
2541 * point after the current transaction completes, at which point a
2542 * subsequent mount starts recovery from the beginning.
2543 *
2544 * Write verifiers update the metadata LSN from log items attached to
2545 * the buffer. Therefore, initialize a bli purely to carry the LSN to
2546 * the verifier. We'll clean it up in our ->iodone() callback.
2547 */
2548 if (bp->b_ops) {
2549 struct xfs_buf_log_item *bip;
2550
2551 ASSERT(!bp->b_iodone || bp->b_iodone == xlog_recover_iodone);
2552 bp->b_iodone = xlog_recover_iodone;
2553 xfs_buf_item_init(bp, mp);
Carlos Maiolinofb1755a2018-01-24 13:38:48 -08002554 bip = bp->b_log_item;
Brian Foster60a4a222016-09-26 08:34:27 +10002555 bip->bli_item.li_lsn = current_lsn;
2556 }
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002557}
2558
2559/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 * Perform a 'normal' buffer recovery. Each logged region of the
2561 * buffer should be copied over the corresponding region in the
2562 * given buffer. The bitmap in the buf log format structure indicates
2563 * where to place the logged data.
2564 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565STATIC void
2566xlog_recover_do_reg_buffer(
Dave Chinner9abbc532010-04-13 15:06:46 +10002567 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 xlog_recover_item_t *item,
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002569 struct xfs_buf *bp,
Brian Foster22db9af2016-09-26 08:32:07 +10002570 xfs_buf_log_format_t *buf_f,
2571 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572{
2573 int i;
2574 int bit;
2575 int nbits;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08002576 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Dave Chinner9abbc532010-04-13 15:06:46 +10002578 trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
2579
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 bit = 0;
2581 i = 1; /* 0 is the buf format structure */
2582 while (1) {
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002583 bit = xfs_next_bit(buf_f->blf_data_map,
2584 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (bit == -1)
2586 break;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002587 nbits = xfs_contig_bits(buf_f->blf_data_map,
2588 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589 ASSERT(nbits > 0);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002590 ASSERT(item->ri_buf[i].i_addr != NULL);
Dave Chinnerc1155412010-05-07 11:05:19 +10002591 ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07002592 ASSERT(BBTOB(bp->b_length) >=
Dave Chinneraa0e8832012-04-23 15:58:52 +10002593 ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
2595 /*
Dave Chinner709da6a2013-05-27 16:38:23 +10002596 * The dirty regions logged in the buffer, even though
2597 * contiguous, may span multiple chunks. This is because the
2598 * dirty region may span a physical page boundary in a buffer
2599 * and hence be split into two separate vectors for writing into
2600 * the log. Hence we need to trim nbits back to the length of
2601 * the current region being copied out of the log.
2602 */
2603 if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
2604 nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
2605
2606 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607 * Do a sanity check if this is a dquot buffer. Just checking
2608 * the first dquot in the buffer should do. XXXThis is
2609 * probably a good thing to do for other buf types also.
2610 */
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08002611 fa = NULL;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002612 if (buf_f->blf_flags &
Dave Chinnerc1155412010-05-07 11:05:19 +10002613 (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002614 if (item->ri_buf[i].i_addr == NULL) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002615 xfs_alert(mp,
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002616 "XFS: NULL dquot in %s.", __func__);
2617 goto next;
2618 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00002619 if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002620 xfs_alert(mp,
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002621 "XFS: dquot too small (%d) in %s.",
2622 item->ri_buf[i].i_len, __func__);
2623 goto next;
2624 }
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08002625 fa = xfs_dquot_verify(mp, item->ri_buf[i].i_addr,
Eric Sandeene381a0f2018-05-04 15:15:48 -07002626 -1, 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08002627 if (fa) {
2628 xfs_alert(mp,
2629 "dquot corrupt at %pS trying to replay into block 0x%llx",
2630 fa, bp->b_bn);
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002631 goto next;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08002632 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002634
2635 memcpy(xfs_buf_offset(bp,
Dave Chinnerc1155412010-05-07 11:05:19 +10002636 (uint)bit << XFS_BLF_SHIFT), /* dest */
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002637 item->ri_buf[i].i_addr, /* source */
Dave Chinnerc1155412010-05-07 11:05:19 +10002638 nbits<<XFS_BLF_SHIFT); /* length */
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002639 next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 i++;
2641 bit += nbits;
2642 }
2643
2644 /* Shouldn't be any more regions */
2645 ASSERT(i == item->ri_total);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002646
Brian Foster22db9af2016-09-26 08:32:07 +10002647 xlog_recover_validate_buf_type(mp, bp, buf_f, current_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648}
2649
2650/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 * Perform a dquot buffer recovery.
Zhi Yong Wu8ba701e2013-08-12 03:15:01 +00002652 * Simple algorithm: if we have found a QUOTAOFF log item of the same type
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 * (ie. USR or GRP), then just toss this buffer away; don't recover it.
2654 * Else, treat it as a regular buffer and do recovery.
Dave Chinnerad3714b2014-08-04 12:59:31 +10002655 *
2656 * Return false if the buffer was tossed and true if we recovered the buffer to
2657 * indicate to the caller if the buffer needs writing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 */
Dave Chinnerad3714b2014-08-04 12:59:31 +10002659STATIC bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660xlog_recover_do_dquot_buffer(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002661 struct xfs_mount *mp,
2662 struct xlog *log,
2663 struct xlog_recover_item *item,
2664 struct xfs_buf *bp,
2665 struct xfs_buf_log_format *buf_f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666{
2667 uint type;
2668
Dave Chinner9abbc532010-04-13 15:06:46 +10002669 trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
2670
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 /*
2672 * Filesystems are required to send in quota flags at mount time.
2673 */
Dave Chinnerad3714b2014-08-04 12:59:31 +10002674 if (!mp->m_qflags)
2675 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 type = 0;
Dave Chinnerc1155412010-05-07 11:05:19 +10002678 if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 type |= XFS_DQ_USER;
Dave Chinnerc1155412010-05-07 11:05:19 +10002680 if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002681 type |= XFS_DQ_PROJ;
Dave Chinnerc1155412010-05-07 11:05:19 +10002682 if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 type |= XFS_DQ_GROUP;
2684 /*
2685 * This type of quotas was turned off, so ignore this buffer
2686 */
2687 if (log->l_quotaoffs_flag & type)
Dave Chinnerad3714b2014-08-04 12:59:31 +10002688 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Brian Foster22db9af2016-09-26 08:32:07 +10002690 xlog_recover_do_reg_buffer(mp, item, bp, buf_f, NULLCOMMITLSN);
Dave Chinnerad3714b2014-08-04 12:59:31 +10002691 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692}
2693
2694/*
2695 * This routine replays a modification made to a buffer at runtime.
2696 * There are actually two types of buffer, regular and inode, which
2697 * are handled differently. Inode buffers are handled differently
2698 * in that we only recover a specific set of data from them, namely
2699 * the inode di_next_unlinked fields. This is because all other inode
2700 * data is actually logged via inode records and any data we replay
2701 * here which overlaps that may be stale.
2702 *
2703 * When meta-data buffers are freed at run time we log a buffer item
Dave Chinnerc1155412010-05-07 11:05:19 +10002704 * with the XFS_BLF_CANCEL bit set to indicate that previous copies
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 * of the buffer in the log should not be replayed at recovery time.
2706 * This is so that if the blocks covered by the buffer are reused for
2707 * file data before we crash we don't end up replaying old, freed
2708 * meta-data into a user's file.
2709 *
2710 * To handle the cancellation of buffer log items, we make two passes
2711 * over the log during recovery. During the first we build a table of
2712 * those buffers which have been cancelled, and during the second we
2713 * only replay those buffers which do not have corresponding cancel
Zhi Yong Wu34be5ff2013-08-07 10:11:07 +00002714 * records in the table. See xlog_recover_buffer_pass[1,2] above
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715 * for more details on the implementation of the table of cancel records.
2716 */
2717STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002718xlog_recover_buffer_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002719 struct xlog *log,
2720 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002721 struct xlog_recover_item *item,
2722 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002724 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002725 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 xfs_buf_t *bp;
2727 int error;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002728 uint buf_flags;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002729 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002731 /*
2732 * In this pass we only want to recover all the buffers which have
2733 * not been cancelled and are not cancellation buffers themselves.
2734 */
2735 if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
2736 buf_f->blf_len, buf_f->blf_flags)) {
2737 trace_xfs_log_recover_buf_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002740
Dave Chinner9abbc532010-04-13 15:06:46 +10002741 trace_xfs_log_recover_buf_recover(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Dave Chinnera8acad72012-04-23 15:58:54 +10002743 buf_flags = 0;
Dave Chinner611c9942012-04-23 15:59:07 +10002744 if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
2745 buf_flags |= XBF_UNMAPPED;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002746
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002747 bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002748 buf_flags, NULL);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002749 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +10002750 return -ENOMEM;
Chandra Seetharamane5702802011-08-03 02:18:34 +00002751 error = bp->b_error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002752 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002753 xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002754 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
2756
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002757 /*
Dave Chinner67dc2882014-08-04 12:43:06 +10002758 * Recover the buffer only if we get an LSN from it and it's less than
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002759 * the lsn of the transaction we are replaying.
Dave Chinner67dc2882014-08-04 12:43:06 +10002760 *
2761 * Note that we have to be extremely careful of readahead here.
2762 * Readahead does not attach verfiers to the buffers so if we don't
2763 * actually do any replay after readahead because of the LSN we found
2764 * in the buffer if more recent than that current transaction then we
2765 * need to attach the verifier directly. Failure to do so can lead to
2766 * future recovery actions (e.g. EFI and unlinked list recovery) can
2767 * operate on the buffers and they won't get the verifier attached. This
2768 * can lead to blocks on disk having the correct content but a stale
2769 * CRC.
2770 *
2771 * It is safe to assume these clean buffers are currently up to date.
2772 * If the buffer is dirtied by a later transaction being replayed, then
2773 * the verifier will be reset to match whatever recover turns that
2774 * buffer into.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002775 */
2776 lsn = xlog_recover_get_buf_lsn(mp, bp);
Dave Chinner67dc2882014-08-04 12:43:06 +10002777 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
Brian Foster5cd9cee2016-09-26 08:34:52 +10002778 trace_xfs_log_recover_buf_skip(log, buf_f);
Brian Foster22db9af2016-09-26 08:32:07 +10002779 xlog_recover_validate_buf_type(mp, bp, buf_f, NULLCOMMITLSN);
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002780 goto out_release;
Dave Chinner67dc2882014-08-04 12:43:06 +10002781 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002782
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002783 if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
Dave Chinnerad3714b2014-08-04 12:59:31 +10002785 if (error)
2786 goto out_release;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002787 } else if (buf_f->blf_flags &
Dave Chinnerc1155412010-05-07 11:05:19 +10002788 (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
Dave Chinnerad3714b2014-08-04 12:59:31 +10002789 bool dirty;
2790
2791 dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
2792 if (!dirty)
2793 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 } else {
Brian Foster22db9af2016-09-26 08:32:07 +10002795 xlog_recover_do_reg_buffer(mp, item, bp, buf_f, current_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 /*
2799 * Perform delayed write on the buffer. Asynchronous writes will be
2800 * slower when taking into account all the buffers to be flushed.
2801 *
2802 * Also make sure that only inode buffers with good sizes stay in
2803 * the buffer cache. The kernel moves inodes in buffers of 1 block
Darrick J. Wongef325952019-06-05 11:19:34 -07002804 * or inode_cluster_size bytes, whichever is bigger. The inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 * buffers in the log can be a different size if the log was generated
2806 * by an older kernel using unclustered inode buffers or a newer kernel
2807 * running with a different inode cluster size. Regardless, if the
Darrick J. Wongef325952019-06-05 11:19:34 -07002808 * the inode buffer size isn't max(blocksize, inode_cluster_size)
2809 * for *our* value of inode_cluster_size, then we need to keep
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 * the buffer out of the buffer cache so that the buffer won't
2811 * overlap with future reads of those inodes.
2812 */
2813 if (XFS_DINODE_MAGIC ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002814 be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
Christoph Hellwig8124b9b2019-06-28 19:27:28 -07002815 (BBTOB(bp->b_length) != M_IGEO(log->l_mp)->inode_cluster_size)) {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002816 xfs_buf_stale(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002817 error = xfs_bwrite(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 } else {
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07002819 ASSERT(bp->b_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002820 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002821 xfs_buf_delwri_queue(bp, buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
2823
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002824out_release:
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002825 xfs_buf_relse(bp);
2826 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827}
2828
Dave Chinner638f44162013-08-30 10:23:45 +10002829/*
2830 * Inode fork owner changes
2831 *
2832 * If we have been told that we have to reparent the inode fork, it's because an
2833 * extent swap operation on a CRC enabled filesystem has been done and we are
2834 * replaying it. We need to walk the BMBT of the appropriate fork and change the
2835 * owners of it.
2836 *
2837 * The complexity here is that we don't have an inode context to work with, so
2838 * after we've replayed the inode we need to instantiate one. This is where the
2839 * fun begins.
2840 *
2841 * We are in the middle of log recovery, so we can't run transactions. That
2842 * means we cannot use cache coherent inode instantiation via xfs_iget(), as
2843 * that will result in the corresponding iput() running the inode through
2844 * xfs_inactive(). If we've just replayed an inode core that changes the link
2845 * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
2846 * transactions (bad!).
2847 *
2848 * So, to avoid this, we instantiate an inode directly from the inode core we've
2849 * just recovered. We have the buffer still locked, and all we really need to
2850 * instantiate is the inode core and the forks being modified. We can do this
2851 * manually, then run the inode btree owner change, and then tear down the
2852 * xfs_inode without having to run any transactions at all.
2853 *
2854 * Also, because we don't have a transaction context available here but need to
2855 * gather all the buffers we modify for writeback so we pass the buffer_list
2856 * instead for the operation to use.
2857 */
2858
2859STATIC int
2860xfs_recover_inode_owner_change(
2861 struct xfs_mount *mp,
2862 struct xfs_dinode *dip,
2863 struct xfs_inode_log_format *in_f,
2864 struct list_head *buffer_list)
2865{
2866 struct xfs_inode *ip;
2867 int error;
2868
2869 ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
2870
2871 ip = xfs_inode_alloc(mp, in_f->ilf_ino);
2872 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +10002873 return -ENOMEM;
Dave Chinner638f44162013-08-30 10:23:45 +10002874
2875 /* instantiate the inode */
Dave Chinner39878482016-02-09 16:54:58 +11002876 xfs_inode_from_disk(ip, dip);
Dave Chinner638f44162013-08-30 10:23:45 +10002877 ASSERT(ip->i_d.di_version >= 3);
2878
2879 error = xfs_iformat_fork(ip, dip);
2880 if (error)
2881 goto out_free_ip;
2882
Darrick J. Wong9cfb9b42018-01-08 10:51:06 -08002883 if (!xfs_inode_verify_forks(ip)) {
2884 error = -EFSCORRUPTED;
2885 goto out_free_ip;
2886 }
Dave Chinner638f44162013-08-30 10:23:45 +10002887
2888 if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
2889 ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
2890 error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
2891 ip->i_ino, buffer_list);
2892 if (error)
2893 goto out_free_ip;
2894 }
2895
2896 if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
2897 ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
2898 error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
2899 ip->i_ino, buffer_list);
2900 if (error)
2901 goto out_free_ip;
2902 }
2903
2904out_free_ip:
2905 xfs_inode_free(ip);
2906 return error;
2907}
2908
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002910xlog_recover_inode_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002911 struct xlog *log,
2912 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002913 struct xlog_recover_item *item,
2914 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
Darrick J. Wong06b11322017-10-31 12:04:24 -07002916 struct xfs_inode_log_format *in_f;
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002917 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 int len;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10002921 char *src;
2922 char *dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 int error;
2924 int attr_index;
2925 uint fields;
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11002926 struct xfs_log_dinode *ldip;
Christoph Hellwig93848a92013-04-03 16:11:17 +11002927 uint isize;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002928 int need_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
Darrick J. Wong06b11322017-10-31 12:04:24 -07002930 if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002931 in_f = item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002932 } else {
Darrick J. Wong06b11322017-10-31 12:04:24 -07002933 in_f = kmem_alloc(sizeof(struct xfs_inode_log_format), KM_SLEEP);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002934 need_free = 1;
2935 error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
2936 if (error)
2937 goto error;
2938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
2940 /*
2941 * Inode buffers can be freed, look out for it,
2942 * and do not replay the inode.
2943 */
Christoph Hellwiga1941892008-11-28 14:23:40 +11002944 if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
2945 in_f->ilf_len, 0)) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002946 error = 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10002947 trace_xfs_log_recover_inode_cancel(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002948 goto error;
2949 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002950 trace_xfs_log_recover_inode_recover(log, in_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002952 bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
Christoph Hellwig93848a92013-04-03 16:11:17 +11002953 &xfs_inode_buf_ops);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002954 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +10002955 error = -ENOMEM;
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002956 goto error;
2957 }
Chandra Seetharamane5702802011-08-03 02:18:34 +00002958 error = bp->b_error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002959 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002960 xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
Dave Chinner638f44162013-08-30 10:23:45 +10002961 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10002964 dip = xfs_buf_offset(bp, in_f->ilf_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965
2966 /*
2967 * Make sure the place we're flushing out to really looks
2968 * like an inode!
2969 */
Darrick J. Wong15baadf2019-02-16 11:47:28 -08002970 if (unlikely(!xfs_verify_magic16(bp, dip->di_magic))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002971 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08002972 "%s: Bad inode magic number, dip = "PTR_FMT", dino bp = "PTR_FMT", ino = %Ld",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002973 __func__, dip, bp, in_f->ilf_ino);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002974 XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002976 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002977 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11002979 ldip = item->ri_buf[1].i_addr;
2980 if (unlikely(ldip->di_magic != XFS_DINODE_MAGIC)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002981 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08002982 "%s: Bad inode log record, rec ptr "PTR_FMT", ino %Ld",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002983 __func__, item, in_f->ilf_ino);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002984 XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002986 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002987 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
2989
Dave Chinnere60896d2013-07-24 15:47:30 +10002990 /*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002991 * If the inode has an LSN in it, recover the inode only if it's less
Dave Chinner638f44162013-08-30 10:23:45 +10002992 * than the lsn of the transaction we are replaying. Note: we still
2993 * need to replay an owner change even though the inode is more recent
2994 * than the transaction as there is no guarantee that all the btree
2995 * blocks are more recent than this transaction, too.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002996 */
2997 if (dip->di_version >= 3) {
2998 xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
2999
3000 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
3001 trace_xfs_log_recover_inode_skip(log, in_f);
3002 error = 0;
Dave Chinner638f44162013-08-30 10:23:45 +10003003 goto out_owner_change;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003004 }
3005 }
3006
3007 /*
Dave Chinnere60896d2013-07-24 15:47:30 +10003008 * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
3009 * are transactional and if ordering is necessary we can determine that
3010 * more accurately by the LSN field in the V3 inode core. Don't trust
3011 * the inode versions we might be changing them here - use the
3012 * superblock flag to determine whether we need to look at di_flushiter
3013 * to skip replay when the on disk inode is newer than the log one
3014 */
3015 if (!xfs_sb_version_hascrc(&mp->m_sb) &&
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003016 ldip->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 /*
3018 * Deal with the wrap case, DI_MAX_FLUSH is less
3019 * than smaller numbers
3020 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003021 if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003022 ldip->di_flushiter < (DI_MAX_FLUSH >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* do nothing */
3024 } else {
Dave Chinner9abbc532010-04-13 15:06:46 +10003025 trace_xfs_log_recover_inode_skip(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10003026 error = 0;
Dave Chinner638f44162013-08-30 10:23:45 +10003027 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
3029 }
Dave Chinnere60896d2013-07-24 15:47:30 +10003030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 /* Take the opportunity to reset the flush iteration count */
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003032 ldip->di_flushiter = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003034 if (unlikely(S_ISREG(ldip->di_mode))) {
3035 if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
3036 (ldip->di_format != XFS_DINODE_FMT_BTREE)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003037 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
Darrick J. Wong2551a532018-06-04 10:23:54 -07003038 XFS_ERRLEVEL_LOW, mp, ldip,
3039 sizeof(*ldip));
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003040 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003041 "%s: Bad regular inode log record, rec ptr "PTR_FMT", "
3042 "ino ptr = "PTR_FMT", ino bp = "PTR_FMT", ino %Ld",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003043 __func__, item, dip, bp, in_f->ilf_ino);
Dave Chinner24513372014-06-25 14:58:08 +10003044 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10003045 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003047 } else if (unlikely(S_ISDIR(ldip->di_mode))) {
3048 if ((ldip->di_format != XFS_DINODE_FMT_EXTENTS) &&
3049 (ldip->di_format != XFS_DINODE_FMT_BTREE) &&
3050 (ldip->di_format != XFS_DINODE_FMT_LOCAL)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003051 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
Darrick J. Wong2551a532018-06-04 10:23:54 -07003052 XFS_ERRLEVEL_LOW, mp, ldip,
3053 sizeof(*ldip));
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003054 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003055 "%s: Bad dir inode log record, rec ptr "PTR_FMT", "
3056 "ino ptr = "PTR_FMT", ino bp = "PTR_FMT", ino %Ld",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003057 __func__, item, dip, bp, in_f->ilf_ino);
Dave Chinner24513372014-06-25 14:58:08 +10003058 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10003059 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061 }
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003062 if (unlikely(ldip->di_nextents + ldip->di_anextents > ldip->di_nblocks)){
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003063 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
Darrick J. Wong2551a532018-06-04 10:23:54 -07003064 XFS_ERRLEVEL_LOW, mp, ldip,
3065 sizeof(*ldip));
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003066 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003067 "%s: Bad inode log record, rec ptr "PTR_FMT", dino ptr "PTR_FMT", "
3068 "dino bp "PTR_FMT", ino %Ld, total extents = %d, nblocks = %Ld",
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003069 __func__, item, dip, bp, in_f->ilf_ino,
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003070 ldip->di_nextents + ldip->di_anextents,
3071 ldip->di_nblocks);
Dave Chinner24513372014-06-25 14:58:08 +10003072 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10003073 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003075 if (unlikely(ldip->di_forkoff > mp->m_sb.sb_inodesize)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003076 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
Darrick J. Wong2551a532018-06-04 10:23:54 -07003077 XFS_ERRLEVEL_LOW, mp, ldip,
3078 sizeof(*ldip));
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003079 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003080 "%s: Bad inode log record, rec ptr "PTR_FMT", dino ptr "PTR_FMT", "
3081 "dino bp "PTR_FMT", ino %Ld, forkoff 0x%x", __func__,
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003082 item, dip, bp, in_f->ilf_ino, ldip->di_forkoff);
Dave Chinner24513372014-06-25 14:58:08 +10003083 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10003084 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
Dave Chinnerf8d55aa0522016-02-09 16:54:58 +11003086 isize = xfs_log_dinode_size(ldip->di_version);
Christoph Hellwig93848a92013-04-03 16:11:17 +11003087 if (unlikely(item->ri_buf[1].i_len > isize)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003088 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
Darrick J. Wong2551a532018-06-04 10:23:54 -07003089 XFS_ERRLEVEL_LOW, mp, ldip,
3090 sizeof(*ldip));
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003091 xfs_alert(mp,
Darrick J. Wongc9690042018-01-09 12:02:55 -08003092 "%s: Bad inode log record length %d, rec ptr "PTR_FMT,
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003093 __func__, item->ri_buf[1].i_len, item);
Dave Chinner24513372014-06-25 14:58:08 +10003094 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10003095 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 }
3097
Dave Chinner39878482016-02-09 16:54:58 +11003098 /* recover the log dinode inode into the on disk inode */
3099 xfs_log_dinode_to_disk(ldip, dip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 fields = in_f->ilf_fields;
Christoph Hellwig42b67dc2017-10-19 11:07:09 -07003102 if (fields & XFS_ILOG_DEV)
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003103 xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104
3105 if (in_f->ilf_size == 2)
Dave Chinner638f44162013-08-30 10:23:45 +10003106 goto out_owner_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 len = item->ri_buf[2].i_len;
3108 src = item->ri_buf[2].i_addr;
3109 ASSERT(in_f->ilf_size <= 4);
3110 ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
3111 ASSERT(!(fields & XFS_ILOG_DFORK) ||
3112 (len == in_f->ilf_dsize));
3113
3114 switch (fields & XFS_ILOG_DFORK) {
3115 case XFS_ILOG_DDATA:
3116 case XFS_ILOG_DEXT:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003117 memcpy(XFS_DFORK_DPTR(dip), src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 break;
3119
3120 case XFS_ILOG_DBROOT:
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003121 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
Christoph Hellwig81591fe2008-11-28 14:23:39 +11003122 (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 XFS_DFORK_DSIZE(dip, mp));
3124 break;
3125
3126 default:
3127 /*
3128 * There are no data fork flags set.
3129 */
3130 ASSERT((fields & XFS_ILOG_DFORK) == 0);
3131 break;
3132 }
3133
3134 /*
3135 * If we logged any attribute data, recover it. There may or
3136 * may not have been any other non-core data logged in this
3137 * transaction.
3138 */
3139 if (in_f->ilf_fields & XFS_ILOG_AFORK) {
3140 if (in_f->ilf_fields & XFS_ILOG_DFORK) {
3141 attr_index = 3;
3142 } else {
3143 attr_index = 2;
3144 }
3145 len = item->ri_buf[attr_index].i_len;
3146 src = item->ri_buf[attr_index].i_addr;
3147 ASSERT(len == in_f->ilf_asize);
3148
3149 switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
3150 case XFS_ILOG_ADATA:
3151 case XFS_ILOG_AEXT:
3152 dest = XFS_DFORK_APTR(dip);
3153 ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
3154 memcpy(dest, src, len);
3155 break;
3156
3157 case XFS_ILOG_ABROOT:
3158 dest = XFS_DFORK_APTR(dip);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11003159 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
3160 len, (xfs_bmdr_block_t*)dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 XFS_DFORK_ASIZE(dip, mp));
3162 break;
3163
3164 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003165 xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10003167 error = -EIO;
Dave Chinner638f44162013-08-30 10:23:45 +10003168 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 }
3170 }
3171
Dave Chinner638f44162013-08-30 10:23:45 +10003172out_owner_change:
Eric Sandeendc1baa72018-03-28 17:48:08 -07003173 /* Recover the swapext owner change unless inode has been deleted */
3174 if ((in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER)) &&
3175 (dip->di_mode != 0))
Dave Chinner638f44162013-08-30 10:23:45 +10003176 error = xfs_recover_inode_owner_change(mp, dip, in_f,
3177 buffer_list);
Christoph Hellwig93848a92013-04-03 16:11:17 +11003178 /* re-generate the checksum. */
3179 xfs_dinode_calc_crc(log->l_mp, dip);
3180
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07003181 ASSERT(bp->b_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02003182 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003183 xfs_buf_delwri_queue(bp, buffer_list);
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003184
3185out_release:
Christoph Hellwig61551f12011-08-23 08:28:06 +00003186 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10003187error:
3188 if (need_free)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10003189 kmem_free(in_f);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10003190 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
3193/*
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003194 * Recover QUOTAOFF records. We simply make a note of it in the xlog
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 * structure, so that we know not to do any dquot item or dquot buffer recovery,
3196 * of that type.
3197 */
3198STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003199xlog_recover_quotaoff_pass1(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003200 struct xlog *log,
3201 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003203 xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 ASSERT(qoff_f);
3205
3206 /*
3207 * The logitem format's flag tells us if this was user quotaoff,
Nathan Scott77a7cce2006-01-11 15:35:57 +11003208 * group/project quotaoff or both.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 */
3210 if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
3211 log->l_quotaoffs_flag |= XFS_DQ_USER;
Nathan Scott77a7cce2006-01-11 15:35:57 +11003212 if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
3213 log->l_quotaoffs_flag |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
3215 log->l_quotaoffs_flag |= XFS_DQ_GROUP;
3216
Eric Sandeend99831f2014-06-22 15:03:54 +10003217 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218}
3219
3220/*
3221 * Recover a dquot record
3222 */
3223STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003224xlog_recover_dquot_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003225 struct xlog *log,
3226 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003227 struct xlog_recover_item *item,
3228 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003230 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 xfs_buf_t *bp;
3232 struct xfs_disk_dquot *ddq, *recddq;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08003233 xfs_failaddr_t fa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 int error;
3235 xfs_dq_logformat_t *dq_f;
3236 uint type;
3237
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238
3239 /*
3240 * Filesystems are required to send in quota flags at mount time.
3241 */
3242 if (mp->m_qflags == 0)
Eric Sandeend99831f2014-06-22 15:03:54 +10003243 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10003245 recddq = item->ri_buf[1].i_addr;
3246 if (recddq == NULL) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003247 xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
Dave Chinner24513372014-06-25 14:58:08 +10003248 return -EIO;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02003249 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00003250 if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003251 xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02003252 item->ri_buf[1].i_len, __func__);
Dave Chinner24513372014-06-25 14:58:08 +10003253 return -EIO;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02003254 }
3255
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 /*
3257 * This type of quotas was turned off, so ignore this record.
3258 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003259 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003260 ASSERT(type);
3261 if (log->l_quotaoffs_flag & type)
Eric Sandeend99831f2014-06-22 15:03:54 +10003262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 /*
3265 * At this point we know that quota was _not_ turned off.
3266 * Since the mount flags are not indicating to us otherwise, this
3267 * must mean that quota is on, and the dquot needs to be replayed.
3268 * Remember that we may not have fully recovered the superblock yet,
3269 * so we can't do the usual trick of looking at the SB quota bits.
3270 *
3271 * The other possibility, of course, is that the quota subsystem was
3272 * removed since the last mount - ENOSYS.
3273 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10003274 dq_f = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 ASSERT(dq_f);
Eric Sandeene381a0f2018-05-04 15:15:48 -07003276 fa = xfs_dquot_verify(mp, recddq, dq_f->qlf_id, 0);
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08003277 if (fa) {
3278 xfs_alert(mp, "corrupt dquot ID 0x%x in log at %pS",
3279 dq_f->qlf_id, fa);
Dave Chinner24513372014-06-25 14:58:08 +10003280 return -EIO;
Darrick J. Wongeebf3ca2018-01-08 10:51:25 -08003281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282 ASSERT(dq_f->qlf_len == 1);
3283
Dave Chinnerad3714b2014-08-04 12:59:31 +10003284 /*
3285 * At this point we are assuming that the dquots have been allocated
3286 * and hence the buffer has valid dquots stamped in it. It should,
3287 * therefore, pass verifier validation. If the dquot is bad, then the
3288 * we'll return an error here, so we don't need to specifically check
3289 * the dquot in the buffer after the verifier has run.
3290 */
Dave Chinner7ca790a2012-04-23 15:58:55 +10003291 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11003292 XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
Dave Chinnerad3714b2014-08-04 12:59:31 +10003293 &xfs_dquot_buf_ops);
Dave Chinner7ca790a2012-04-23 15:58:55 +10003294 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295 return error;
Dave Chinner7ca790a2012-04-23 15:58:55 +10003296
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 ASSERT(bp);
Christoph Hellwig88ee2df2015-06-22 09:44:29 +10003298 ddq = xfs_buf_offset(bp, dq_f->qlf_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
3300 /*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003301 * If the dquot has an LSN in it, recover the dquot only if it's less
3302 * than the lsn of the transaction we are replaying.
3303 */
3304 if (xfs_sb_version_hascrc(&mp->m_sb)) {
3305 struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
3306 xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
3307
3308 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
3309 goto out_release;
3310 }
3311 }
3312
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 memcpy(ddq, recddq, item->ri_buf[1].i_len);
Dave Chinner6fcdc592013-06-03 15:28:46 +10003314 if (xfs_sb_version_hascrc(&mp->m_sb)) {
3315 xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
3316 XFS_DQUOT_CRC_OFF);
3317 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
3319 ASSERT(dq_f->qlf_size == 2);
Christoph Hellwigdbd329f12019-06-28 19:27:29 -07003320 ASSERT(bp->b_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02003321 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003322 xfs_buf_delwri_queue(bp, buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003324out_release:
3325 xfs_buf_relse(bp);
3326 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}
3328
3329/*
3330 * This routine is called to create an in-core extent free intent
3331 * item from the efi format structure which was logged on disk.
3332 * It allocates an in-core efi, copies the extents from the format
3333 * structure into it, and adds the efi to the AIL with the given
3334 * LSN.
3335 */
Tim Shimmin6d192a92006-06-09 14:55:38 +10003336STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003337xlog_recover_efi_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003338 struct xlog *log,
3339 struct xlog_recover_item *item,
3340 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Brian Fostere32a1d12015-08-19 09:52:21 +10003342 int error;
3343 struct xfs_mount *mp = log->l_mp;
3344 struct xfs_efi_log_item *efip;
3345 struct xfs_efi_log_format *efi_formatp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10003347 efi_formatp = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
Brian Fostere32a1d12015-08-19 09:52:21 +10003350 error = xfs_efi_copy_format(&item->ri_buf[0], &efip->efi_format);
3351 if (error) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10003352 xfs_efi_item_free(efip);
3353 return error;
3354 }
Dave Chinnerb199c8a2010-12-20 11:59:49 +11003355 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
Matthew Wilcox57e80952018-03-07 14:59:39 -08003357 spin_lock(&log->l_ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 /*
Brian Fostere32a1d12015-08-19 09:52:21 +10003359 * The EFI has two references. One for the EFD and one for EFI to ensure
3360 * it makes it into the AIL. Insert the EFI into the AIL directly and
3361 * drop the EFI reference. Note that xfs_trans_ail_update() drops the
3362 * AIL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 */
Dave Chinnere6059942010-12-20 12:34:26 +11003364 xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
Brian Fostere32a1d12015-08-19 09:52:21 +10003365 xfs_efi_release(efip);
Tim Shimmin6d192a92006-06-09 14:55:38 +10003366 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
3369
3370/*
Brian Fostere32a1d12015-08-19 09:52:21 +10003371 * This routine is called when an EFD format structure is found in a committed
3372 * transaction in the log. Its purpose is to cancel the corresponding EFI if it
3373 * was still in the log. To do this it searches the AIL for the EFI with an id
3374 * equal to that in the EFD format structure. If we find it we drop the EFD
3375 * reference, which removes the EFI from the AIL and frees it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 */
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003377STATIC int
3378xlog_recover_efd_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003379 struct xlog *log,
3380 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 xfs_efd_log_format_t *efd_formatp;
3383 xfs_efi_log_item_t *efip = NULL;
Christoph Hellwigefe23302019-06-28 19:27:33 -07003384 struct xfs_log_item *lip;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07003385 uint64_t efi_id;
David Chinner27d8d5f2008-10-30 17:38:39 +11003386 struct xfs_ail_cursor cur;
David Chinner783a2f62008-10-30 17:39:58 +11003387 struct xfs_ail *ailp = log->l_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10003389 efd_formatp = item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10003390 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
3391 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
3392 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
3393 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 efi_id = efd_formatp->efd_efi_id;
3395
3396 /*
Brian Fostere32a1d12015-08-19 09:52:21 +10003397 * Search for the EFI with the id in the EFD format structure in the
3398 * AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003400 spin_lock(&ailp->ail_lock);
David Chinnera9c21c12008-10-30 17:39:35 +11003401 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 while (lip != NULL) {
3403 if (lip->li_type == XFS_LI_EFI) {
3404 efip = (xfs_efi_log_item_t *)lip;
3405 if (efip->efi_format.efi_id == efi_id) {
3406 /*
Brian Fostere32a1d12015-08-19 09:52:21 +10003407 * Drop the EFD reference to the EFI. This
3408 * removes the EFI from the AIL and frees it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003410 spin_unlock(&ailp->ail_lock);
Brian Fostere32a1d12015-08-19 09:52:21 +10003411 xfs_efi_release(efip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003412 spin_lock(&ailp->ail_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11003413 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 }
3415 }
David Chinnera9c21c12008-10-30 17:39:35 +11003416 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 }
Brian Fostere32a1d12015-08-19 09:52:21 +10003418
Eric Sandeene4a1e292014-04-14 19:06:05 +10003419 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003420 spin_unlock(&ailp->ail_lock);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003421
3422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423}
3424
3425/*
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003426 * This routine is called to create an in-core extent rmap update
3427 * item from the rui format structure which was logged on disk.
3428 * It allocates an in-core rui, copies the extents from the format
3429 * structure into it, and adds the rui to the AIL with the given
3430 * LSN.
3431 */
3432STATIC int
3433xlog_recover_rui_pass2(
3434 struct xlog *log,
3435 struct xlog_recover_item *item,
3436 xfs_lsn_t lsn)
3437{
3438 int error;
3439 struct xfs_mount *mp = log->l_mp;
3440 struct xfs_rui_log_item *ruip;
3441 struct xfs_rui_log_format *rui_formatp;
3442
3443 rui_formatp = item->ri_buf[0].i_addr;
3444
3445 ruip = xfs_rui_init(mp, rui_formatp->rui_nextents);
3446 error = xfs_rui_copy_format(&item->ri_buf[0], &ruip->rui_format);
3447 if (error) {
3448 xfs_rui_item_free(ruip);
3449 return error;
3450 }
3451 atomic_set(&ruip->rui_next_extent, rui_formatp->rui_nextents);
3452
Matthew Wilcox57e80952018-03-07 14:59:39 -08003453 spin_lock(&log->l_ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003454 /*
3455 * The RUI has two references. One for the RUD and one for RUI to ensure
3456 * it makes it into the AIL. Insert the RUI into the AIL directly and
3457 * drop the RUI reference. Note that xfs_trans_ail_update() drops the
3458 * AIL lock.
3459 */
3460 xfs_trans_ail_update(log->l_ailp, &ruip->rui_item, lsn);
3461 xfs_rui_release(ruip);
3462 return 0;
3463}
3464
3465
3466/*
3467 * This routine is called when an RUD format structure is found in a committed
3468 * transaction in the log. Its purpose is to cancel the corresponding RUI if it
3469 * was still in the log. To do this it searches the AIL for the RUI with an id
3470 * equal to that in the RUD format structure. If we find it we drop the RUD
3471 * reference, which removes the RUI from the AIL and frees it.
3472 */
3473STATIC int
3474xlog_recover_rud_pass2(
3475 struct xlog *log,
3476 struct xlog_recover_item *item)
3477{
3478 struct xfs_rud_log_format *rud_formatp;
3479 struct xfs_rui_log_item *ruip = NULL;
3480 struct xfs_log_item *lip;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07003481 uint64_t rui_id;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003482 struct xfs_ail_cursor cur;
3483 struct xfs_ail *ailp = log->l_ailp;
3484
3485 rud_formatp = item->ri_buf[0].i_addr;
Darrick J. Wong722e2512016-08-03 12:28:43 +10003486 ASSERT(item->ri_buf[0].i_len == sizeof(struct xfs_rud_log_format));
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003487 rui_id = rud_formatp->rud_rui_id;
3488
3489 /*
3490 * Search for the RUI with the id in the RUD format structure in the
3491 * AIL.
3492 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003493 spin_lock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003494 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
3495 while (lip != NULL) {
3496 if (lip->li_type == XFS_LI_RUI) {
3497 ruip = (struct xfs_rui_log_item *)lip;
3498 if (ruip->rui_format.rui_id == rui_id) {
3499 /*
3500 * Drop the RUD reference to the RUI. This
3501 * removes the RUI from the AIL and frees it.
3502 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003503 spin_unlock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003504 xfs_rui_release(ruip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003505 spin_lock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003506 break;
3507 }
3508 }
3509 lip = xfs_trans_ail_cursor_next(ailp, &cur);
3510 }
3511
3512 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003513 spin_unlock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003514
3515 return 0;
3516}
3517
3518/*
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003519 * Copy an CUI format buffer from the given buf, and into the destination
3520 * CUI format structure. The CUI/CUD items were designed not to need any
3521 * special alignment handling.
3522 */
3523static int
3524xfs_cui_copy_format(
3525 struct xfs_log_iovec *buf,
3526 struct xfs_cui_log_format *dst_cui_fmt)
3527{
3528 struct xfs_cui_log_format *src_cui_fmt;
3529 uint len;
3530
3531 src_cui_fmt = buf->i_addr;
3532 len = xfs_cui_log_format_sizeof(src_cui_fmt->cui_nextents);
3533
3534 if (buf->i_len == len) {
3535 memcpy(dst_cui_fmt, src_cui_fmt, len);
3536 return 0;
3537 }
3538 return -EFSCORRUPTED;
3539}
3540
3541/*
3542 * This routine is called to create an in-core extent refcount update
3543 * item from the cui format structure which was logged on disk.
3544 * It allocates an in-core cui, copies the extents from the format
3545 * structure into it, and adds the cui to the AIL with the given
3546 * LSN.
3547 */
3548STATIC int
3549xlog_recover_cui_pass2(
3550 struct xlog *log,
3551 struct xlog_recover_item *item,
3552 xfs_lsn_t lsn)
3553{
3554 int error;
3555 struct xfs_mount *mp = log->l_mp;
3556 struct xfs_cui_log_item *cuip;
3557 struct xfs_cui_log_format *cui_formatp;
3558
3559 cui_formatp = item->ri_buf[0].i_addr;
3560
3561 cuip = xfs_cui_init(mp, cui_formatp->cui_nextents);
3562 error = xfs_cui_copy_format(&item->ri_buf[0], &cuip->cui_format);
3563 if (error) {
3564 xfs_cui_item_free(cuip);
3565 return error;
3566 }
3567 atomic_set(&cuip->cui_next_extent, cui_formatp->cui_nextents);
3568
Matthew Wilcox57e80952018-03-07 14:59:39 -08003569 spin_lock(&log->l_ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003570 /*
3571 * The CUI has two references. One for the CUD and one for CUI to ensure
3572 * it makes it into the AIL. Insert the CUI into the AIL directly and
3573 * drop the CUI reference. Note that xfs_trans_ail_update() drops the
3574 * AIL lock.
3575 */
3576 xfs_trans_ail_update(log->l_ailp, &cuip->cui_item, lsn);
3577 xfs_cui_release(cuip);
3578 return 0;
3579}
3580
3581
3582/*
3583 * This routine is called when an CUD format structure is found in a committed
3584 * transaction in the log. Its purpose is to cancel the corresponding CUI if it
3585 * was still in the log. To do this it searches the AIL for the CUI with an id
3586 * equal to that in the CUD format structure. If we find it we drop the CUD
3587 * reference, which removes the CUI from the AIL and frees it.
3588 */
3589STATIC int
3590xlog_recover_cud_pass2(
3591 struct xlog *log,
3592 struct xlog_recover_item *item)
3593{
3594 struct xfs_cud_log_format *cud_formatp;
3595 struct xfs_cui_log_item *cuip = NULL;
3596 struct xfs_log_item *lip;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07003597 uint64_t cui_id;
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003598 struct xfs_ail_cursor cur;
3599 struct xfs_ail *ailp = log->l_ailp;
3600
3601 cud_formatp = item->ri_buf[0].i_addr;
3602 if (item->ri_buf[0].i_len != sizeof(struct xfs_cud_log_format))
3603 return -EFSCORRUPTED;
3604 cui_id = cud_formatp->cud_cui_id;
3605
3606 /*
3607 * Search for the CUI with the id in the CUD format structure in the
3608 * AIL.
3609 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003610 spin_lock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003611 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
3612 while (lip != NULL) {
3613 if (lip->li_type == XFS_LI_CUI) {
3614 cuip = (struct xfs_cui_log_item *)lip;
3615 if (cuip->cui_format.cui_id == cui_id) {
3616 /*
3617 * Drop the CUD reference to the CUI. This
3618 * removes the CUI from the AIL and frees it.
3619 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003620 spin_unlock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003621 xfs_cui_release(cuip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003622 spin_lock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003623 break;
3624 }
3625 }
3626 lip = xfs_trans_ail_cursor_next(ailp, &cur);
3627 }
3628
3629 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003630 spin_unlock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003631
3632 return 0;
3633}
3634
3635/*
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003636 * Copy an BUI format buffer from the given buf, and into the destination
3637 * BUI format structure. The BUI/BUD items were designed not to need any
3638 * special alignment handling.
3639 */
3640static int
3641xfs_bui_copy_format(
3642 struct xfs_log_iovec *buf,
3643 struct xfs_bui_log_format *dst_bui_fmt)
3644{
3645 struct xfs_bui_log_format *src_bui_fmt;
3646 uint len;
3647
3648 src_bui_fmt = buf->i_addr;
3649 len = xfs_bui_log_format_sizeof(src_bui_fmt->bui_nextents);
3650
3651 if (buf->i_len == len) {
3652 memcpy(dst_bui_fmt, src_bui_fmt, len);
3653 return 0;
3654 }
3655 return -EFSCORRUPTED;
3656}
3657
3658/*
3659 * This routine is called to create an in-core extent bmap update
3660 * item from the bui format structure which was logged on disk.
3661 * It allocates an in-core bui, copies the extents from the format
3662 * structure into it, and adds the bui to the AIL with the given
3663 * LSN.
3664 */
3665STATIC int
3666xlog_recover_bui_pass2(
3667 struct xlog *log,
3668 struct xlog_recover_item *item,
3669 xfs_lsn_t lsn)
3670{
3671 int error;
3672 struct xfs_mount *mp = log->l_mp;
3673 struct xfs_bui_log_item *buip;
3674 struct xfs_bui_log_format *bui_formatp;
3675
3676 bui_formatp = item->ri_buf[0].i_addr;
3677
3678 if (bui_formatp->bui_nextents != XFS_BUI_MAX_FAST_EXTENTS)
3679 return -EFSCORRUPTED;
3680 buip = xfs_bui_init(mp);
3681 error = xfs_bui_copy_format(&item->ri_buf[0], &buip->bui_format);
3682 if (error) {
3683 xfs_bui_item_free(buip);
3684 return error;
3685 }
3686 atomic_set(&buip->bui_next_extent, bui_formatp->bui_nextents);
3687
Matthew Wilcox57e80952018-03-07 14:59:39 -08003688 spin_lock(&log->l_ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003689 /*
3690 * The RUI has two references. One for the RUD and one for RUI to ensure
3691 * it makes it into the AIL. Insert the RUI into the AIL directly and
3692 * drop the RUI reference. Note that xfs_trans_ail_update() drops the
3693 * AIL lock.
3694 */
3695 xfs_trans_ail_update(log->l_ailp, &buip->bui_item, lsn);
3696 xfs_bui_release(buip);
3697 return 0;
3698}
3699
3700
3701/*
3702 * This routine is called when an BUD format structure is found in a committed
3703 * transaction in the log. Its purpose is to cancel the corresponding BUI if it
3704 * was still in the log. To do this it searches the AIL for the BUI with an id
3705 * equal to that in the BUD format structure. If we find it we drop the BUD
3706 * reference, which removes the BUI from the AIL and frees it.
3707 */
3708STATIC int
3709xlog_recover_bud_pass2(
3710 struct xlog *log,
3711 struct xlog_recover_item *item)
3712{
3713 struct xfs_bud_log_format *bud_formatp;
3714 struct xfs_bui_log_item *buip = NULL;
3715 struct xfs_log_item *lip;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07003716 uint64_t bui_id;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003717 struct xfs_ail_cursor cur;
3718 struct xfs_ail *ailp = log->l_ailp;
3719
3720 bud_formatp = item->ri_buf[0].i_addr;
3721 if (item->ri_buf[0].i_len != sizeof(struct xfs_bud_log_format))
3722 return -EFSCORRUPTED;
3723 bui_id = bud_formatp->bud_bui_id;
3724
3725 /*
3726 * Search for the BUI with the id in the BUD format structure in the
3727 * AIL.
3728 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003729 spin_lock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003730 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
3731 while (lip != NULL) {
3732 if (lip->li_type == XFS_LI_BUI) {
3733 buip = (struct xfs_bui_log_item *)lip;
3734 if (buip->bui_format.bui_id == bui_id) {
3735 /*
3736 * Drop the BUD reference to the BUI. This
3737 * removes the BUI from the AIL and frees it.
3738 */
Matthew Wilcox57e80952018-03-07 14:59:39 -08003739 spin_unlock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003740 xfs_bui_release(buip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003741 spin_lock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003742 break;
3743 }
3744 }
3745 lip = xfs_trans_ail_cursor_next(ailp, &cur);
3746 }
3747
3748 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08003749 spin_unlock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003750
3751 return 0;
3752}
3753
3754/*
Dave Chinner28c8e412013-06-27 16:04:55 +10003755 * This routine is called when an inode create format structure is found in a
3756 * committed transaction in the log. It's purpose is to initialise the inodes
3757 * being allocated on disk. This requires us to get inode cluster buffers that
Masahiro Yamada6e7c2b42017-05-08 15:57:53 -07003758 * match the range to be initialised, stamped with inode templates and written
Dave Chinner28c8e412013-06-27 16:04:55 +10003759 * by delayed write so that subsequent modifications will hit the cached buffer
3760 * and only need writing out at the end of recovery.
3761 */
3762STATIC int
3763xlog_recover_do_icreate_pass2(
3764 struct xlog *log,
3765 struct list_head *buffer_list,
3766 xlog_recover_item_t *item)
3767{
3768 struct xfs_mount *mp = log->l_mp;
3769 struct xfs_icreate_log *icl;
Darrick J. Wongef325952019-06-05 11:19:34 -07003770 struct xfs_ino_geometry *igeo = M_IGEO(mp);
Dave Chinner28c8e412013-06-27 16:04:55 +10003771 xfs_agnumber_t agno;
3772 xfs_agblock_t agbno;
3773 unsigned int count;
3774 unsigned int isize;
3775 xfs_agblock_t length;
Brian Fosterfc0d1652015-08-19 09:59:38 +10003776 int bb_per_cluster;
3777 int cancel_count;
3778 int nbufs;
3779 int i;
Dave Chinner28c8e412013-06-27 16:04:55 +10003780
3781 icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
3782 if (icl->icl_type != XFS_LI_ICREATE) {
3783 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
Dave Chinner24513372014-06-25 14:58:08 +10003784 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003785 }
3786
3787 if (icl->icl_size != 1) {
3788 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
Dave Chinner24513372014-06-25 14:58:08 +10003789 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003790 }
3791
3792 agno = be32_to_cpu(icl->icl_ag);
3793 if (agno >= mp->m_sb.sb_agcount) {
3794 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
Dave Chinner24513372014-06-25 14:58:08 +10003795 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003796 }
3797 agbno = be32_to_cpu(icl->icl_agbno);
3798 if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
3799 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
Dave Chinner24513372014-06-25 14:58:08 +10003800 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003801 }
3802 isize = be32_to_cpu(icl->icl_isize);
3803 if (isize != mp->m_sb.sb_inodesize) {
3804 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
Dave Chinner24513372014-06-25 14:58:08 +10003805 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003806 }
3807 count = be32_to_cpu(icl->icl_count);
3808 if (!count) {
3809 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
Dave Chinner24513372014-06-25 14:58:08 +10003810 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003811 }
3812 length = be32_to_cpu(icl->icl_length);
3813 if (!length || length >= mp->m_sb.sb_agblocks) {
3814 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
Dave Chinner24513372014-06-25 14:58:08 +10003815 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003816 }
3817
Brian Foster7f43c902015-05-29 09:06:30 +10003818 /*
3819 * The inode chunk is either full or sparse and we only support
Darrick J. Wongef325952019-06-05 11:19:34 -07003820 * m_ino_geo.ialloc_min_blks sized sparse allocations at this time.
Brian Foster7f43c902015-05-29 09:06:30 +10003821 */
Darrick J. Wongef325952019-06-05 11:19:34 -07003822 if (length != igeo->ialloc_blks &&
3823 length != igeo->ialloc_min_blks) {
Brian Foster7f43c902015-05-29 09:06:30 +10003824 xfs_warn(log->l_mp,
3825 "%s: unsupported chunk length", __FUNCTION__);
3826 return -EINVAL;
3827 }
3828
3829 /* verify inode count is consistent with extent length */
3830 if ((count >> mp->m_sb.sb_inopblog) != length) {
3831 xfs_warn(log->l_mp,
3832 "%s: inconsistent inode count and chunk length",
3833 __FUNCTION__);
Dave Chinner24513372014-06-25 14:58:08 +10003834 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003835 }
3836
3837 /*
Brian Fosterfc0d1652015-08-19 09:59:38 +10003838 * The icreate transaction can cover multiple cluster buffers and these
3839 * buffers could have been freed and reused. Check the individual
3840 * buffers for cancellation so we don't overwrite anything written after
3841 * a cancellation.
Dave Chinner28c8e412013-06-27 16:04:55 +10003842 */
Darrick J. Wongef325952019-06-05 11:19:34 -07003843 bb_per_cluster = XFS_FSB_TO_BB(mp, igeo->blocks_per_cluster);
3844 nbufs = length / igeo->blocks_per_cluster;
Brian Fosterfc0d1652015-08-19 09:59:38 +10003845 for (i = 0, cancel_count = 0; i < nbufs; i++) {
3846 xfs_daddr_t daddr;
Dave Chinner28c8e412013-06-27 16:04:55 +10003847
Brian Fosterfc0d1652015-08-19 09:59:38 +10003848 daddr = XFS_AGB_TO_DADDR(mp, agno,
Darrick J. Wongef325952019-06-05 11:19:34 -07003849 agbno + i * igeo->blocks_per_cluster);
Brian Fosterfc0d1652015-08-19 09:59:38 +10003850 if (xlog_check_buffer_cancelled(log, daddr, bb_per_cluster, 0))
3851 cancel_count++;
3852 }
3853
3854 /*
3855 * We currently only use icreate for a single allocation at a time. This
3856 * means we should expect either all or none of the buffers to be
3857 * cancelled. Be conservative and skip replay if at least one buffer is
3858 * cancelled, but warn the user that something is awry if the buffers
3859 * are not consistent.
3860 *
3861 * XXX: This must be refined to only skip cancelled clusters once we use
3862 * icreate for multiple chunk allocations.
3863 */
3864 ASSERT(!cancel_count || cancel_count == nbufs);
3865 if (cancel_count) {
3866 if (cancel_count != nbufs)
3867 xfs_warn(mp,
3868 "WARNING: partial inode chunk cancellation, skipped icreate.");
Brian Foster78d57e42015-08-19 09:58:48 +10003869 trace_xfs_log_recover_icreate_cancel(log, icl);
Dave Chinner28c8e412013-06-27 16:04:55 +10003870 return 0;
Brian Foster78d57e42015-08-19 09:58:48 +10003871 }
Dave Chinner28c8e412013-06-27 16:04:55 +10003872
Brian Foster78d57e42015-08-19 09:58:48 +10003873 trace_xfs_log_recover_icreate_recover(log, icl);
Brian Fosterfc0d1652015-08-19 09:59:38 +10003874 return xfs_ialloc_inode_init(mp, NULL, buffer_list, count, agno, agbno,
3875 length, be32_to_cpu(icl->icl_gen));
Dave Chinner28c8e412013-06-27 16:04:55 +10003876}
3877
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003878STATIC void
3879xlog_recover_buffer_ra_pass2(
3880 struct xlog *log,
3881 struct xlog_recover_item *item)
3882{
3883 struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
3884 struct xfs_mount *mp = log->l_mp;
3885
Dave Chinner84a5b732013-08-27 08:10:53 +10003886 if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003887 buf_f->blf_len, buf_f->blf_flags)) {
3888 return;
3889 }
3890
3891 xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
3892 buf_f->blf_len, NULL);
3893}
3894
3895STATIC void
3896xlog_recover_inode_ra_pass2(
3897 struct xlog *log,
3898 struct xlog_recover_item *item)
3899{
3900 struct xfs_inode_log_format ilf_buf;
3901 struct xfs_inode_log_format *ilfp;
3902 struct xfs_mount *mp = log->l_mp;
3903 int error;
3904
3905 if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
3906 ilfp = item->ri_buf[0].i_addr;
3907 } else {
3908 ilfp = &ilf_buf;
3909 memset(ilfp, 0, sizeof(*ilfp));
3910 error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
3911 if (error)
3912 return;
3913 }
3914
Dave Chinner84a5b732013-08-27 08:10:53 +10003915 if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003916 return;
3917
3918 xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
Dave Chinnerd8914002013-08-27 11:39:37 +10003919 ilfp->ilf_len, &xfs_inode_buf_ra_ops);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003920}
3921
3922STATIC void
3923xlog_recover_dquot_ra_pass2(
3924 struct xlog *log,
3925 struct xlog_recover_item *item)
3926{
3927 struct xfs_mount *mp = log->l_mp;
3928 struct xfs_disk_dquot *recddq;
3929 struct xfs_dq_logformat *dq_f;
3930 uint type;
Dave Chinner7d6a13f2016-01-12 07:04:01 +11003931 int len;
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003932
3933
3934 if (mp->m_qflags == 0)
3935 return;
3936
3937 recddq = item->ri_buf[1].i_addr;
3938 if (recddq == NULL)
3939 return;
3940 if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
3941 return;
3942
3943 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
3944 ASSERT(type);
3945 if (log->l_quotaoffs_flag & type)
3946 return;
3947
3948 dq_f = item->ri_buf[0].i_addr;
3949 ASSERT(dq_f);
3950 ASSERT(dq_f->qlf_len == 1);
3951
Dave Chinner7d6a13f2016-01-12 07:04:01 +11003952 len = XFS_FSB_TO_BB(mp, dq_f->qlf_len);
3953 if (xlog_peek_buffer_cancelled(log, dq_f->qlf_blkno, len, 0))
3954 return;
3955
3956 xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno, len,
3957 &xfs_dquot_buf_ra_ops);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003958}
3959
3960STATIC void
3961xlog_recover_ra_pass2(
3962 struct xlog *log,
3963 struct xlog_recover_item *item)
3964{
3965 switch (ITEM_TYPE(item)) {
3966 case XFS_LI_BUF:
3967 xlog_recover_buffer_ra_pass2(log, item);
3968 break;
3969 case XFS_LI_INODE:
3970 xlog_recover_inode_ra_pass2(log, item);
3971 break;
3972 case XFS_LI_DQUOT:
3973 xlog_recover_dquot_ra_pass2(log, item);
3974 break;
3975 case XFS_LI_EFI:
3976 case XFS_LI_EFD:
3977 case XFS_LI_QUOTAOFF:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10003978 case XFS_LI_RUI:
3979 case XFS_LI_RUD:
Darrick J. Wongf997ee22016-10-03 09:11:21 -07003980 case XFS_LI_CUI:
3981 case XFS_LI_CUD:
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07003982 case XFS_LI_BUI:
3983 case XFS_LI_BUD:
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003984 default:
3985 break;
3986 }
3987}
3988
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003990xlog_recover_commit_pass1(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003991 struct xlog *log,
3992 struct xlog_recover *trans,
3993 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003995 trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
Christoph Hellwigd0450942010-12-01 22:06:23 +00003996
3997 switch (ITEM_TYPE(item)) {
3998 case XFS_LI_BUF:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003999 return xlog_recover_buffer_pass1(log, item);
Christoph Hellwigd0450942010-12-01 22:06:23 +00004000 case XFS_LI_QUOTAOFF:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004001 return xlog_recover_quotaoff_pass1(log, item);
4002 case XFS_LI_INODE:
4003 case XFS_LI_EFI:
4004 case XFS_LI_EFD:
4005 case XFS_LI_DQUOT:
Dave Chinner28c8e412013-06-27 16:04:55 +10004006 case XFS_LI_ICREATE:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004007 case XFS_LI_RUI:
4008 case XFS_LI_RUD:
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004009 case XFS_LI_CUI:
4010 case XFS_LI_CUD:
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004011 case XFS_LI_BUI:
4012 case XFS_LI_BUD:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004013 /* nothing to do in pass 1 */
4014 return 0;
Christoph Hellwigd0450942010-12-01 22:06:23 +00004015 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004016 xfs_warn(log->l_mp, "%s: invalid item type (%d)",
4017 __func__, ITEM_TYPE(item));
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004018 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10004019 return -EIO;
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004020 }
4021}
4022
4023STATIC int
4024xlog_recover_commit_pass2(
Mark Tinguelyad223e62012-06-14 09:22:15 -05004025 struct xlog *log,
4026 struct xlog_recover *trans,
4027 struct list_head *buffer_list,
4028 struct xlog_recover_item *item)
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004029{
4030 trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
4031
4032 switch (ITEM_TYPE(item)) {
4033 case XFS_LI_BUF:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10004034 return xlog_recover_buffer_pass2(log, buffer_list, item,
4035 trans->r_lsn);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004036 case XFS_LI_INODE:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10004037 return xlog_recover_inode_pass2(log, buffer_list, item,
4038 trans->r_lsn);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004039 case XFS_LI_EFI:
4040 return xlog_recover_efi_pass2(log, item, trans->r_lsn);
4041 case XFS_LI_EFD:
4042 return xlog_recover_efd_pass2(log, item);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004043 case XFS_LI_RUI:
4044 return xlog_recover_rui_pass2(log, item, trans->r_lsn);
4045 case XFS_LI_RUD:
4046 return xlog_recover_rud_pass2(log, item);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004047 case XFS_LI_CUI:
4048 return xlog_recover_cui_pass2(log, item, trans->r_lsn);
4049 case XFS_LI_CUD:
4050 return xlog_recover_cud_pass2(log, item);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004051 case XFS_LI_BUI:
4052 return xlog_recover_bui_pass2(log, item, trans->r_lsn);
4053 case XFS_LI_BUD:
4054 return xlog_recover_bud_pass2(log, item);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004055 case XFS_LI_DQUOT:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10004056 return xlog_recover_dquot_pass2(log, buffer_list, item,
4057 trans->r_lsn);
Dave Chinner28c8e412013-06-27 16:04:55 +10004058 case XFS_LI_ICREATE:
4059 return xlog_recover_do_icreate_pass2(log, buffer_list, item);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004060 case XFS_LI_QUOTAOFF:
4061 /* nothing to do in pass2 */
4062 return 0;
4063 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004064 xfs_warn(log->l_mp, "%s: invalid item type (%d)",
4065 __func__, ITEM_TYPE(item));
Christoph Hellwigd0450942010-12-01 22:06:23 +00004066 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10004067 return -EIO;
Christoph Hellwigd0450942010-12-01 22:06:23 +00004068 }
4069}
4070
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004071STATIC int
4072xlog_recover_items_pass2(
4073 struct xlog *log,
4074 struct xlog_recover *trans,
4075 struct list_head *buffer_list,
4076 struct list_head *item_list)
4077{
4078 struct xlog_recover_item *item;
4079 int error = 0;
4080
4081 list_for_each_entry(item, item_list, ri_list) {
4082 error = xlog_recover_commit_pass2(log, trans,
4083 buffer_list, item);
4084 if (error)
4085 return error;
4086 }
4087
4088 return error;
4089}
4090
Christoph Hellwigd0450942010-12-01 22:06:23 +00004091/*
4092 * Perform the transaction.
4093 *
4094 * If the transaction modifies a buffer or inode, do it now. Otherwise,
4095 * EFIs and EFDs get queued up by adding entries into the AIL for them.
4096 */
4097STATIC int
4098xlog_recover_commit_trans(
Mark Tinguelyad223e62012-06-14 09:22:15 -05004099 struct xlog *log,
Christoph Hellwigd0450942010-12-01 22:06:23 +00004100 struct xlog_recover *trans,
Brian Foster12818d22016-09-26 08:22:16 +10004101 int pass,
4102 struct list_head *buffer_list)
Christoph Hellwigd0450942010-12-01 22:06:23 +00004103{
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004104 int error = 0;
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004105 int items_queued = 0;
4106 struct xlog_recover_item *item;
4107 struct xlog_recover_item *next;
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004108 LIST_HEAD (ra_list);
4109 LIST_HEAD (done_list);
4110
4111 #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Brian Foster39775432017-06-24 10:11:41 -07004113 hlist_del_init(&trans->r_list);
Christoph Hellwigd0450942010-12-01 22:06:23 +00004114
4115 error = xlog_recover_reorder_trans(log, trans, pass);
4116 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 return error;
Christoph Hellwigd0450942010-12-01 22:06:23 +00004118
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004119 list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10004120 switch (pass) {
4121 case XLOG_RECOVER_PASS1:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00004122 error = xlog_recover_commit_pass1(log, trans, item);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10004123 break;
4124 case XLOG_RECOVER_PASS2:
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004125 xlog_recover_ra_pass2(log, item);
4126 list_move_tail(&item->ri_list, &ra_list);
4127 items_queued++;
4128 if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
4129 error = xlog_recover_items_pass2(log, trans,
Brian Foster12818d22016-09-26 08:22:16 +10004130 buffer_list, &ra_list);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004131 list_splice_tail_init(&ra_list, &done_list);
4132 items_queued = 0;
4133 }
4134
Christoph Hellwig43ff2122012-04-23 15:58:39 +10004135 break;
4136 default:
4137 ASSERT(0);
4138 }
4139
Christoph Hellwigd0450942010-12-01 22:06:23 +00004140 if (error)
Christoph Hellwig43ff2122012-04-23 15:58:39 +10004141 goto out;
Christoph Hellwigd0450942010-12-01 22:06:23 +00004142 }
4143
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004144out:
4145 if (!list_empty(&ra_list)) {
4146 if (!error)
4147 error = xlog_recover_items_pass2(log, trans,
Brian Foster12818d22016-09-26 08:22:16 +10004148 buffer_list, &ra_list);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08004149 list_splice_tail_init(&ra_list, &done_list);
4150 }
4151
4152 if (!list_empty(&done_list))
4153 list_splice_init(&done_list, &trans->r_itemq);
4154
Brian Foster12818d22016-09-26 08:22:16 +10004155 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156}
4157
Dave Chinner76560662014-09-29 09:45:42 +10004158STATIC void
4159xlog_recover_add_item(
4160 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161{
Dave Chinner76560662014-09-29 09:45:42 +10004162 xlog_recover_item_t *item;
4163
4164 item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
4165 INIT_LIST_HEAD(&item->ri_list);
4166 list_add_tail(&item->ri_list, head);
4167}
4168
4169STATIC int
4170xlog_recover_add_to_cont_trans(
4171 struct xlog *log,
4172 struct xlog_recover *trans,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004173 char *dp,
Dave Chinner76560662014-09-29 09:45:42 +10004174 int len)
4175{
4176 xlog_recover_item_t *item;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004177 char *ptr, *old_ptr;
Dave Chinner76560662014-09-29 09:45:42 +10004178 int old_len;
4179
Brian Foster89cebc842015-07-29 11:51:10 +10004180 /*
4181 * If the transaction is empty, the header was split across this and the
4182 * previous record. Copy the rest of the header.
4183 */
Dave Chinner76560662014-09-29 09:45:42 +10004184 if (list_empty(&trans->r_itemq)) {
Brian Foster848ccfc2015-11-10 10:10:33 +11004185 ASSERT(len <= sizeof(struct xfs_trans_header));
Brian Foster89cebc842015-07-29 11:51:10 +10004186 if (len > sizeof(struct xfs_trans_header)) {
4187 xfs_warn(log->l_mp, "%s: bad header length", __func__);
4188 return -EIO;
4189 }
4190
Dave Chinner76560662014-09-29 09:45:42 +10004191 xlog_recover_add_item(&trans->r_itemq);
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004192 ptr = (char *)&trans->r_theader +
Brian Foster89cebc842015-07-29 11:51:10 +10004193 sizeof(struct xfs_trans_header) - len;
Dave Chinner76560662014-09-29 09:45:42 +10004194 memcpy(ptr, dp, len);
4195 return 0;
4196 }
Brian Foster89cebc842015-07-29 11:51:10 +10004197
Dave Chinner76560662014-09-29 09:45:42 +10004198 /* take the tail entry */
4199 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
4200
4201 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
4202 old_len = item->ri_buf[item->ri_cnt-1].i_len;
4203
Christoph Hellwig664b60f2016-04-06 09:47:01 +10004204 ptr = kmem_realloc(old_ptr, len + old_len, KM_SLEEP);
Dave Chinner76560662014-09-29 09:45:42 +10004205 memcpy(&ptr[old_len], dp, len);
4206 item->ri_buf[item->ri_cnt-1].i_len += len;
4207 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
4208 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 return 0;
4210}
4211
4212/*
Dave Chinner76560662014-09-29 09:45:42 +10004213 * The next region to add is the start of a new region. It could be
4214 * a whole region or it could be the first part of a new region. Because
4215 * of this, the assumption here is that the type and size fields of all
4216 * format structures fit into the first 32 bits of the structure.
4217 *
4218 * This works because all regions must be 32 bit aligned. Therefore, we
4219 * either have both fields or we have neither field. In the case we have
4220 * neither field, the data part of the region is zero length. We only have
4221 * a log_op_header and can throw away the header since a new one will appear
4222 * later. If we have at least 4 bytes, then we can determine how many regions
4223 * will appear in the current log item.
4224 */
4225STATIC int
4226xlog_recover_add_to_trans(
4227 struct xlog *log,
4228 struct xlog_recover *trans,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004229 char *dp,
Dave Chinner76560662014-09-29 09:45:42 +10004230 int len)
4231{
Darrick J. Wong06b11322017-10-31 12:04:24 -07004232 struct xfs_inode_log_format *in_f; /* any will do */
Dave Chinner76560662014-09-29 09:45:42 +10004233 xlog_recover_item_t *item;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004234 char *ptr;
Dave Chinner76560662014-09-29 09:45:42 +10004235
4236 if (!len)
4237 return 0;
4238 if (list_empty(&trans->r_itemq)) {
4239 /* we need to catch log corruptions here */
4240 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
4241 xfs_warn(log->l_mp, "%s: bad header magic number",
4242 __func__);
4243 ASSERT(0);
4244 return -EIO;
4245 }
Brian Foster89cebc842015-07-29 11:51:10 +10004246
4247 if (len > sizeof(struct xfs_trans_header)) {
4248 xfs_warn(log->l_mp, "%s: bad header length", __func__);
4249 ASSERT(0);
4250 return -EIO;
4251 }
4252
4253 /*
4254 * The transaction header can be arbitrarily split across op
4255 * records. If we don't have the whole thing here, copy what we
4256 * do have and handle the rest in the next record.
4257 */
4258 if (len == sizeof(struct xfs_trans_header))
Dave Chinner76560662014-09-29 09:45:42 +10004259 xlog_recover_add_item(&trans->r_itemq);
4260 memcpy(&trans->r_theader, dp, len);
4261 return 0;
4262 }
4263
4264 ptr = kmem_alloc(len, KM_SLEEP);
4265 memcpy(ptr, dp, len);
Darrick J. Wong06b11322017-10-31 12:04:24 -07004266 in_f = (struct xfs_inode_log_format *)ptr;
Dave Chinner76560662014-09-29 09:45:42 +10004267
4268 /* take the tail entry */
4269 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
4270 if (item->ri_total != 0 &&
4271 item->ri_total == item->ri_cnt) {
4272 /* tail item is in use, get a new one */
4273 xlog_recover_add_item(&trans->r_itemq);
4274 item = list_entry(trans->r_itemq.prev,
4275 xlog_recover_item_t, ri_list);
4276 }
4277
4278 if (item->ri_total == 0) { /* first region to be added */
4279 if (in_f->ilf_size == 0 ||
4280 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
4281 xfs_warn(log->l_mp,
4282 "bad number of regions (%d) in inode log format",
4283 in_f->ilf_size);
4284 ASSERT(0);
4285 kmem_free(ptr);
4286 return -EIO;
4287 }
4288
4289 item->ri_total = in_f->ilf_size;
4290 item->ri_buf =
4291 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
4292 KM_SLEEP);
4293 }
4294 ASSERT(item->ri_total > item->ri_cnt);
4295 /* Description region is ri_buf[0] */
4296 item->ri_buf[item->ri_cnt].i_addr = ptr;
4297 item->ri_buf[item->ri_cnt].i_len = len;
4298 item->ri_cnt++;
4299 trace_xfs_log_recover_item_add(log, trans, item, 0);
4300 return 0;
4301}
Dave Chinnerb818cca2014-09-29 09:45:54 +10004302
Dave Chinner76560662014-09-29 09:45:42 +10004303/*
4304 * Free up any resources allocated by the transaction
4305 *
4306 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
4307 */
4308STATIC void
4309xlog_recover_free_trans(
4310 struct xlog_recover *trans)
4311{
4312 xlog_recover_item_t *item, *n;
4313 int i;
4314
Brian Foster39775432017-06-24 10:11:41 -07004315 hlist_del_init(&trans->r_list);
4316
Dave Chinner76560662014-09-29 09:45:42 +10004317 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
4318 /* Free the regions in the item. */
4319 list_del(&item->ri_list);
4320 for (i = 0; i < item->ri_cnt; i++)
4321 kmem_free(item->ri_buf[i].i_addr);
4322 /* Free the item itself */
4323 kmem_free(item->ri_buf);
4324 kmem_free(item);
4325 }
4326 /* Free the transaction recover structure */
4327 kmem_free(trans);
4328}
4329
Dave Chinnere9131e502014-09-29 09:45:18 +10004330/*
4331 * On error or completion, trans is freed.
4332 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333STATIC int
Dave Chinnereeb11682014-09-29 09:45:03 +10004334xlog_recovery_process_trans(
4335 struct xlog *log,
4336 struct xlog_recover *trans,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004337 char *dp,
Dave Chinnereeb11682014-09-29 09:45:03 +10004338 unsigned int len,
4339 unsigned int flags,
Brian Foster12818d22016-09-26 08:22:16 +10004340 int pass,
4341 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342{
Dave Chinnere9131e502014-09-29 09:45:18 +10004343 int error = 0;
4344 bool freeit = false;
Dave Chinnereeb11682014-09-29 09:45:03 +10004345
4346 /* mask off ophdr transaction container flags */
4347 flags &= ~XLOG_END_TRANS;
4348 if (flags & XLOG_WAS_CONT_TRANS)
4349 flags &= ~XLOG_CONTINUE_TRANS;
4350
Dave Chinner88b863d2014-09-29 09:45:32 +10004351 /*
4352 * Callees must not free the trans structure. We'll decide if we need to
4353 * free it or not based on the operation being done and it's result.
4354 */
Dave Chinnereeb11682014-09-29 09:45:03 +10004355 switch (flags) {
4356 /* expected flag values */
4357 case 0:
4358 case XLOG_CONTINUE_TRANS:
4359 error = xlog_recover_add_to_trans(log, trans, dp, len);
4360 break;
4361 case XLOG_WAS_CONT_TRANS:
4362 error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
4363 break;
4364 case XLOG_COMMIT_TRANS:
Brian Foster12818d22016-09-26 08:22:16 +10004365 error = xlog_recover_commit_trans(log, trans, pass,
4366 buffer_list);
Dave Chinner88b863d2014-09-29 09:45:32 +10004367 /* success or fail, we are now done with this transaction. */
4368 freeit = true;
Dave Chinnereeb11682014-09-29 09:45:03 +10004369 break;
4370
4371 /* unexpected flag values */
4372 case XLOG_UNMOUNT_TRANS:
Dave Chinnere9131e502014-09-29 09:45:18 +10004373 /* just skip trans */
Dave Chinnereeb11682014-09-29 09:45:03 +10004374 xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
Dave Chinnere9131e502014-09-29 09:45:18 +10004375 freeit = true;
Dave Chinnereeb11682014-09-29 09:45:03 +10004376 break;
4377 case XLOG_START_TRANS:
Dave Chinnereeb11682014-09-29 09:45:03 +10004378 default:
4379 xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
4380 ASSERT(0);
Dave Chinnere9131e502014-09-29 09:45:18 +10004381 error = -EIO;
Dave Chinnereeb11682014-09-29 09:45:03 +10004382 break;
4383 }
Dave Chinnere9131e502014-09-29 09:45:18 +10004384 if (error || freeit)
4385 xlog_recover_free_trans(trans);
Dave Chinnereeb11682014-09-29 09:45:03 +10004386 return error;
4387}
4388
Dave Chinnerb818cca2014-09-29 09:45:54 +10004389/*
4390 * Lookup the transaction recovery structure associated with the ID in the
4391 * current ophdr. If the transaction doesn't exist and the start flag is set in
4392 * the ophdr, then allocate a new transaction for future ID matches to find.
4393 * Either way, return what we found during the lookup - an existing transaction
4394 * or nothing.
4395 */
Dave Chinnereeb11682014-09-29 09:45:03 +10004396STATIC struct xlog_recover *
4397xlog_recover_ophdr_to_trans(
4398 struct hlist_head rhash[],
4399 struct xlog_rec_header *rhead,
4400 struct xlog_op_header *ohead)
4401{
4402 struct xlog_recover *trans;
4403 xlog_tid_t tid;
4404 struct hlist_head *rhp;
4405
4406 tid = be32_to_cpu(ohead->oh_tid);
4407 rhp = &rhash[XLOG_RHASH(tid)];
Dave Chinnerb818cca2014-09-29 09:45:54 +10004408 hlist_for_each_entry(trans, rhp, r_list) {
4409 if (trans->r_log_tid == tid)
4410 return trans;
4411 }
Dave Chinnereeb11682014-09-29 09:45:03 +10004412
4413 /*
Dave Chinnerb818cca2014-09-29 09:45:54 +10004414 * skip over non-start transaction headers - we could be
4415 * processing slack space before the next transaction starts
Dave Chinnereeb11682014-09-29 09:45:03 +10004416 */
Dave Chinnerb818cca2014-09-29 09:45:54 +10004417 if (!(ohead->oh_flags & XLOG_START_TRANS))
4418 return NULL;
4419
4420 ASSERT(be32_to_cpu(ohead->oh_len) == 0);
4421
4422 /*
4423 * This is a new transaction so allocate a new recovery container to
4424 * hold the recovery ops that will follow.
4425 */
4426 trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
4427 trans->r_log_tid = tid;
4428 trans->r_lsn = be64_to_cpu(rhead->h_lsn);
4429 INIT_LIST_HEAD(&trans->r_itemq);
4430 INIT_HLIST_NODE(&trans->r_list);
4431 hlist_add_head(&trans->r_list, rhp);
4432
4433 /*
4434 * Nothing more to do for this ophdr. Items to be added to this new
4435 * transaction will be in subsequent ophdr containers.
4436 */
Dave Chinnereeb11682014-09-29 09:45:03 +10004437 return NULL;
4438}
4439
4440STATIC int
4441xlog_recover_process_ophdr(
4442 struct xlog *log,
4443 struct hlist_head rhash[],
4444 struct xlog_rec_header *rhead,
4445 struct xlog_op_header *ohead,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004446 char *dp,
4447 char *end,
Brian Foster12818d22016-09-26 08:22:16 +10004448 int pass,
4449 struct list_head *buffer_list)
Dave Chinnereeb11682014-09-29 09:45:03 +10004450{
4451 struct xlog_recover *trans;
Dave Chinnereeb11682014-09-29 09:45:03 +10004452 unsigned int len;
Brian Foster12818d22016-09-26 08:22:16 +10004453 int error;
Dave Chinnereeb11682014-09-29 09:45:03 +10004454
4455 /* Do we understand who wrote this op? */
4456 if (ohead->oh_clientid != XFS_TRANSACTION &&
4457 ohead->oh_clientid != XFS_LOG) {
4458 xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
4459 __func__, ohead->oh_clientid);
4460 ASSERT(0);
4461 return -EIO;
4462 }
4463
4464 /*
4465 * Check the ophdr contains all the data it is supposed to contain.
4466 */
4467 len = be32_to_cpu(ohead->oh_len);
4468 if (dp + len > end) {
4469 xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
4470 WARN_ON(1);
4471 return -EIO;
4472 }
4473
4474 trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
4475 if (!trans) {
4476 /* nothing to do, so skip over this ophdr */
4477 return 0;
4478 }
4479
Brian Foster12818d22016-09-26 08:22:16 +10004480 /*
4481 * The recovered buffer queue is drained only once we know that all
4482 * recovery items for the current LSN have been processed. This is
4483 * required because:
4484 *
4485 * - Buffer write submission updates the metadata LSN of the buffer.
4486 * - Log recovery skips items with a metadata LSN >= the current LSN of
4487 * the recovery item.
4488 * - Separate recovery items against the same metadata buffer can share
4489 * a current LSN. I.e., consider that the LSN of a recovery item is
4490 * defined as the starting LSN of the first record in which its
4491 * transaction appears, that a record can hold multiple transactions,
4492 * and/or that a transaction can span multiple records.
4493 *
4494 * In other words, we are allowed to submit a buffer from log recovery
4495 * once per current LSN. Otherwise, we may incorrectly skip recovery
4496 * items and cause corruption.
4497 *
4498 * We don't know up front whether buffers are updated multiple times per
4499 * LSN. Therefore, track the current LSN of each commit log record as it
4500 * is processed and drain the queue when it changes. Use commit records
4501 * because they are ordered correctly by the logging code.
4502 */
4503 if (log->l_recovery_lsn != trans->r_lsn &&
4504 ohead->oh_flags & XLOG_COMMIT_TRANS) {
4505 error = xfs_buf_delwri_submit(buffer_list);
4506 if (error)
4507 return error;
4508 log->l_recovery_lsn = trans->r_lsn;
4509 }
4510
Dave Chinnere9131e502014-09-29 09:45:18 +10004511 return xlog_recovery_process_trans(log, trans, dp, len,
Brian Foster12818d22016-09-26 08:22:16 +10004512 ohead->oh_flags, pass, buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513}
4514
4515/*
4516 * There are two valid states of the r_state field. 0 indicates that the
4517 * transaction structure is in a normal state. We have either seen the
4518 * start of the transaction or the last operation we added was not a partial
4519 * operation. If the last operation we added to the transaction was a
4520 * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
4521 *
4522 * NOTE: skip LRs with 0 data length.
4523 */
4524STATIC int
4525xlog_recover_process_data(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004526 struct xlog *log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00004527 struct hlist_head rhash[],
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004528 struct xlog_rec_header *rhead,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004529 char *dp,
Brian Foster12818d22016-09-26 08:22:16 +10004530 int pass,
4531 struct list_head *buffer_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
Dave Chinnereeb11682014-09-29 09:45:03 +10004533 struct xlog_op_header *ohead;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10004534 char *end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 int num_logops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537
Dave Chinnereeb11682014-09-29 09:45:03 +10004538 end = dp + be32_to_cpu(rhead->h_len);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004539 num_logops = be32_to_cpu(rhead->h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 /* check the log format matches our own - else we can't recover */
4542 if (xlog_header_check_recover(log->l_mp, rhead))
Dave Chinner24513372014-06-25 14:58:08 +10004543 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544
Brian Foster5cd9cee2016-09-26 08:34:52 +10004545 trace_xfs_log_recover_record(log, rhead, pass);
Dave Chinnereeb11682014-09-29 09:45:03 +10004546 while ((dp < end) && num_logops) {
4547
4548 ohead = (struct xlog_op_header *)dp;
4549 dp += sizeof(*ohead);
4550 ASSERT(dp <= end);
4551
4552 /* errors will abort recovery */
4553 error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
Brian Foster12818d22016-09-26 08:22:16 +10004554 dp, end, pass, buffer_list);
Dave Chinnereeb11682014-09-29 09:45:03 +10004555 if (error)
4556 return error;
4557
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10004558 dp += be32_to_cpu(ohead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 num_logops--;
4560 }
4561 return 0;
4562}
4563
Darrick J. Wongdc423752016-08-03 11:23:49 +10004564/* Recover the EFI if necessary. */
David Chinner3c1e2bb2008-04-10 12:21:11 +10004565STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566xlog_recover_process_efi(
Darrick J. Wongdc423752016-08-03 11:23:49 +10004567 struct xfs_mount *mp,
4568 struct xfs_ail *ailp,
4569 struct xfs_log_item *lip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570{
Darrick J. Wongdc423752016-08-03 11:23:49 +10004571 struct xfs_efi_log_item *efip;
4572 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573
4574 /*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004575 * Skip EFIs that we've already processed.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 */
Darrick J. Wongdc423752016-08-03 11:23:49 +10004577 efip = container_of(lip, struct xfs_efi_log_item, efi_item);
4578 if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags))
4579 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580
Matthew Wilcox57e80952018-03-07 14:59:39 -08004581 spin_unlock(&ailp->ail_lock);
Darrick J. Wongdc423752016-08-03 11:23:49 +10004582 error = xfs_efi_recover(mp, efip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004583 spin_lock(&ailp->ail_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584
David Chinnerfc6149d2008-04-10 12:21:53 +10004585 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586}
4587
Darrick J. Wongdc423752016-08-03 11:23:49 +10004588/* Release the EFI since we're cancelling everything. */
4589STATIC void
4590xlog_recover_cancel_efi(
4591 struct xfs_mount *mp,
4592 struct xfs_ail *ailp,
4593 struct xfs_log_item *lip)
4594{
4595 struct xfs_efi_log_item *efip;
4596
4597 efip = container_of(lip, struct xfs_efi_log_item, efi_item);
4598
Matthew Wilcox57e80952018-03-07 14:59:39 -08004599 spin_unlock(&ailp->ail_lock);
Darrick J. Wongdc423752016-08-03 11:23:49 +10004600 xfs_efi_release(efip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004601 spin_lock(&ailp->ail_lock);
Darrick J. Wongdc423752016-08-03 11:23:49 +10004602}
4603
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004604/* Recover the RUI if necessary. */
4605STATIC int
4606xlog_recover_process_rui(
4607 struct xfs_mount *mp,
4608 struct xfs_ail *ailp,
4609 struct xfs_log_item *lip)
4610{
4611 struct xfs_rui_log_item *ruip;
4612 int error;
4613
4614 /*
4615 * Skip RUIs that we've already processed.
4616 */
4617 ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
4618 if (test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags))
4619 return 0;
4620
Matthew Wilcox57e80952018-03-07 14:59:39 -08004621 spin_unlock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004622 error = xfs_rui_recover(mp, ruip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004623 spin_lock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004624
4625 return error;
4626}
4627
4628/* Release the RUI since we're cancelling everything. */
4629STATIC void
4630xlog_recover_cancel_rui(
4631 struct xfs_mount *mp,
4632 struct xfs_ail *ailp,
4633 struct xfs_log_item *lip)
4634{
4635 struct xfs_rui_log_item *ruip;
4636
4637 ruip = container_of(lip, struct xfs_rui_log_item, rui_item);
4638
Matthew Wilcox57e80952018-03-07 14:59:39 -08004639 spin_unlock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004640 xfs_rui_release(ruip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004641 spin_lock(&ailp->ail_lock);
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004642}
4643
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004644/* Recover the CUI if necessary. */
4645STATIC int
4646xlog_recover_process_cui(
Brian Fosterfbfa9772018-08-01 07:20:29 -07004647 struct xfs_trans *parent_tp,
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004648 struct xfs_ail *ailp,
Brian Fosterfbfa9772018-08-01 07:20:29 -07004649 struct xfs_log_item *lip)
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004650{
4651 struct xfs_cui_log_item *cuip;
4652 int error;
4653
4654 /*
4655 * Skip CUIs that we've already processed.
4656 */
4657 cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
4658 if (test_bit(XFS_CUI_RECOVERED, &cuip->cui_flags))
4659 return 0;
4660
Matthew Wilcox57e80952018-03-07 14:59:39 -08004661 spin_unlock(&ailp->ail_lock);
Brian Fosterfbfa9772018-08-01 07:20:29 -07004662 error = xfs_cui_recover(parent_tp, cuip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004663 spin_lock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004664
4665 return error;
4666}
4667
4668/* Release the CUI since we're cancelling everything. */
4669STATIC void
4670xlog_recover_cancel_cui(
4671 struct xfs_mount *mp,
4672 struct xfs_ail *ailp,
4673 struct xfs_log_item *lip)
4674{
4675 struct xfs_cui_log_item *cuip;
4676
4677 cuip = container_of(lip, struct xfs_cui_log_item, cui_item);
4678
Matthew Wilcox57e80952018-03-07 14:59:39 -08004679 spin_unlock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004680 xfs_cui_release(cuip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004681 spin_lock(&ailp->ail_lock);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004682}
4683
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004684/* Recover the BUI if necessary. */
4685STATIC int
4686xlog_recover_process_bui(
Brian Fosterfbfa9772018-08-01 07:20:29 -07004687 struct xfs_trans *parent_tp,
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004688 struct xfs_ail *ailp,
Brian Fosterfbfa9772018-08-01 07:20:29 -07004689 struct xfs_log_item *lip)
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004690{
4691 struct xfs_bui_log_item *buip;
4692 int error;
4693
4694 /*
4695 * Skip BUIs that we've already processed.
4696 */
4697 buip = container_of(lip, struct xfs_bui_log_item, bui_item);
4698 if (test_bit(XFS_BUI_RECOVERED, &buip->bui_flags))
4699 return 0;
4700
Matthew Wilcox57e80952018-03-07 14:59:39 -08004701 spin_unlock(&ailp->ail_lock);
Brian Fosterfbfa9772018-08-01 07:20:29 -07004702 error = xfs_bui_recover(parent_tp, buip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004703 spin_lock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004704
4705 return error;
4706}
4707
4708/* Release the BUI since we're cancelling everything. */
4709STATIC void
4710xlog_recover_cancel_bui(
4711 struct xfs_mount *mp,
4712 struct xfs_ail *ailp,
4713 struct xfs_log_item *lip)
4714{
4715 struct xfs_bui_log_item *buip;
4716
4717 buip = container_of(lip, struct xfs_bui_log_item, bui_item);
4718
Matthew Wilcox57e80952018-03-07 14:59:39 -08004719 spin_unlock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004720 xfs_bui_release(buip);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004721 spin_lock(&ailp->ail_lock);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004722}
4723
Darrick J. Wongdc423752016-08-03 11:23:49 +10004724/* Is this log item a deferred action intent? */
4725static inline bool xlog_item_is_intent(struct xfs_log_item *lip)
4726{
4727 switch (lip->li_type) {
4728 case XFS_LI_EFI:
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004729 case XFS_LI_RUI:
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004730 case XFS_LI_CUI:
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004731 case XFS_LI_BUI:
Darrick J. Wongdc423752016-08-03 11:23:49 +10004732 return true;
4733 default:
4734 return false;
4735 }
4736}
4737
Darrick J. Wong50995582017-11-21 20:53:02 -08004738/* Take all the collected deferred ops and finish them in order. */
4739static int
4740xlog_finish_defer_ops(
Brian Fosterfbfa9772018-08-01 07:20:29 -07004741 struct xfs_trans *parent_tp)
Darrick J. Wong50995582017-11-21 20:53:02 -08004742{
Brian Fosterfbfa9772018-08-01 07:20:29 -07004743 struct xfs_mount *mp = parent_tp->t_mountp;
Darrick J. Wong50995582017-11-21 20:53:02 -08004744 struct xfs_trans *tp;
4745 int64_t freeblks;
4746 uint resblks;
4747 int error;
4748
4749 /*
4750 * We're finishing the defer_ops that accumulated as a result of
4751 * recovering unfinished intent items during log recovery. We
4752 * reserve an itruncate transaction because it is the largest
4753 * permanent transaction type. Since we're the only user of the fs
4754 * right now, take 93% (15/16) of the available free blocks. Use
4755 * weird math to avoid a 64-bit division.
4756 */
4757 freeblks = percpu_counter_sum(&mp->m_fdblocks);
4758 if (freeblks <= 0)
4759 return -ENOSPC;
4760 resblks = min_t(int64_t, UINT_MAX, freeblks);
4761 resblks = (resblks * 15) >> 4;
4762 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, resblks,
4763 0, XFS_TRANS_RESERVE, &tp);
4764 if (error)
4765 return error;
Brian Foster91ef75b2018-07-24 13:43:13 -07004766 /* transfer all collected dfops to this transaction */
Brian Fosterce356d62018-08-01 07:20:30 -07004767 xfs_defer_move(tp, parent_tp);
Darrick J. Wong50995582017-11-21 20:53:02 -08004768
Darrick J. Wong50995582017-11-21 20:53:02 -08004769 return xfs_trans_commit(tp);
Darrick J. Wong50995582017-11-21 20:53:02 -08004770}
4771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772/*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004773 * When this is called, all of the log intent items which did not have
4774 * corresponding log done items should be in the AIL. What we do now
4775 * is update the data structures associated with each one.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776 *
Darrick J. Wongdc423752016-08-03 11:23:49 +10004777 * Since we process the log intent items in normal transactions, they
4778 * will be removed at some point after the commit. This prevents us
4779 * from just walking down the list processing each one. We'll use a
4780 * flag in the intent item to skip those that we've already processed
4781 * and use the AIL iteration mechanism's generation count to try to
4782 * speed this up at least a bit.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 *
Darrick J. Wongdc423752016-08-03 11:23:49 +10004784 * When we start, we know that the intents are the only things in the
4785 * AIL. As we process them, however, other items are added to the
4786 * AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10004788STATIC int
Darrick J. Wongdc423752016-08-03 11:23:49 +10004789xlog_recover_process_intents(
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004790 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791{
Brian Fosterfbfa9772018-08-01 07:20:29 -07004792 struct xfs_trans *parent_tp;
David Chinner27d8d5f2008-10-30 17:38:39 +11004793 struct xfs_ail_cursor cur;
Darrick J. Wong50995582017-11-21 20:53:02 -08004794 struct xfs_log_item *lip;
David Chinnera9c21c12008-10-30 17:39:35 +11004795 struct xfs_ail *ailp;
Brian Fosterfbfa9772018-08-01 07:20:29 -07004796 int error;
Darrick J. Wong7bf7a192017-08-31 15:11:06 -07004797#if defined(DEBUG) || defined(XFS_WARN)
Darrick J. Wongdc423752016-08-03 11:23:49 +10004798 xfs_lsn_t last_lsn;
Darrick J. Wong7bf7a192017-08-31 15:11:06 -07004799#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Brian Fosterfbfa9772018-08-01 07:20:29 -07004801 /*
4802 * The intent recovery handlers commit transactions to complete recovery
4803 * for individual intents, but any new deferred operations that are
4804 * queued during that process are held off until the very end. The
4805 * purpose of this transaction is to serve as a container for deferred
4806 * operations. Each intent recovery handler must transfer dfops here
4807 * before its local transaction commits, and we'll finish the entire
4808 * list below.
4809 */
4810 error = xfs_trans_alloc_empty(log->l_mp, &parent_tp);
4811 if (error)
4812 return error;
4813
David Chinnera9c21c12008-10-30 17:39:35 +11004814 ailp = log->l_ailp;
Matthew Wilcox57e80952018-03-07 14:59:39 -08004815 spin_lock(&ailp->ail_lock);
David Chinnera9c21c12008-10-30 17:39:35 +11004816 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Darrick J. Wong7bf7a192017-08-31 15:11:06 -07004817#if defined(DEBUG) || defined(XFS_WARN)
Darrick J. Wongdc423752016-08-03 11:23:49 +10004818 last_lsn = xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block);
Darrick J. Wong7bf7a192017-08-31 15:11:06 -07004819#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820 while (lip != NULL) {
4821 /*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004822 * We're done when we see something other than an intent.
4823 * There should be no intents left in the AIL now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004824 */
Darrick J. Wongdc423752016-08-03 11:23:49 +10004825 if (!xlog_item_is_intent(lip)) {
David Chinner27d8d5f2008-10-30 17:38:39 +11004826#ifdef DEBUG
David Chinnera9c21c12008-10-30 17:39:35 +11004827 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
Darrick J. Wongdc423752016-08-03 11:23:49 +10004828 ASSERT(!xlog_item_is_intent(lip));
David Chinner27d8d5f2008-10-30 17:38:39 +11004829#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830 break;
4831 }
4832
4833 /*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004834 * We should never see a redo item with a LSN higher than
4835 * the last transaction we found in the log at the start
4836 * of recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 */
Darrick J. Wongdc423752016-08-03 11:23:49 +10004838 ASSERT(XFS_LSN_CMP(last_lsn, lip->li_lsn) >= 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839
Darrick J. Wong50995582017-11-21 20:53:02 -08004840 /*
4841 * NOTE: If your intent processing routine can create more
4842 * deferred ops, you /must/ attach them to the dfops in this
4843 * routine or else those subsequent intents will get
4844 * replayed in the wrong order!
4845 */
Darrick J. Wongdc423752016-08-03 11:23:49 +10004846 switch (lip->li_type) {
4847 case XFS_LI_EFI:
4848 error = xlog_recover_process_efi(log->l_mp, ailp, lip);
4849 break;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004850 case XFS_LI_RUI:
4851 error = xlog_recover_process_rui(log->l_mp, ailp, lip);
4852 break;
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004853 case XFS_LI_CUI:
Brian Fosterfbfa9772018-08-01 07:20:29 -07004854 error = xlog_recover_process_cui(parent_tp, ailp, lip);
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004855 break;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004856 case XFS_LI_BUI:
Brian Fosterfbfa9772018-08-01 07:20:29 -07004857 error = xlog_recover_process_bui(parent_tp, ailp, lip);
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004858 break;
Darrick J. Wongdc423752016-08-03 11:23:49 +10004859 }
David Chinner27d8d5f2008-10-30 17:38:39 +11004860 if (error)
4861 goto out;
David Chinnera9c21c12008-10-30 17:39:35 +11004862 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863 }
David Chinner27d8d5f2008-10-30 17:38:39 +11004864out:
Eric Sandeene4a1e292014-04-14 19:06:05 +10004865 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004866 spin_unlock(&ailp->ail_lock);
Brian Fosterfbfa9772018-08-01 07:20:29 -07004867 if (!error)
4868 error = xlog_finish_defer_ops(parent_tp);
4869 xfs_trans_cancel(parent_tp);
Darrick J. Wong50995582017-11-21 20:53:02 -08004870
David Chinner3c1e2bb2008-04-10 12:21:11 +10004871 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872}
4873
4874/*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004875 * A cancel occurs when the mount has failed and we're bailing out.
4876 * Release all pending log intent items so they don't pin the AIL.
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004877 */
Hariprasad Kelama7a92502019-07-03 07:34:18 -07004878STATIC void
Darrick J. Wongdc423752016-08-03 11:23:49 +10004879xlog_recover_cancel_intents(
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004880 struct xlog *log)
4881{
4882 struct xfs_log_item *lip;
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004883 struct xfs_ail_cursor cur;
4884 struct xfs_ail *ailp;
4885
4886 ailp = log->l_ailp;
Matthew Wilcox57e80952018-03-07 14:59:39 -08004887 spin_lock(&ailp->ail_lock);
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004888 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
4889 while (lip != NULL) {
4890 /*
Darrick J. Wongdc423752016-08-03 11:23:49 +10004891 * We're done when we see something other than an intent.
4892 * There should be no intents left in the AIL now.
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004893 */
Darrick J. Wongdc423752016-08-03 11:23:49 +10004894 if (!xlog_item_is_intent(lip)) {
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004895#ifdef DEBUG
4896 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
Darrick J. Wongdc423752016-08-03 11:23:49 +10004897 ASSERT(!xlog_item_is_intent(lip));
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004898#endif
4899 break;
4900 }
4901
Darrick J. Wongdc423752016-08-03 11:23:49 +10004902 switch (lip->li_type) {
4903 case XFS_LI_EFI:
4904 xlog_recover_cancel_efi(log->l_mp, ailp, lip);
4905 break;
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10004906 case XFS_LI_RUI:
4907 xlog_recover_cancel_rui(log->l_mp, ailp, lip);
4908 break;
Darrick J. Wongf997ee22016-10-03 09:11:21 -07004909 case XFS_LI_CUI:
4910 xlog_recover_cancel_cui(log->l_mp, ailp, lip);
4911 break;
Darrick J. Wong77d61fe2016-10-03 09:11:26 -07004912 case XFS_LI_BUI:
4913 xlog_recover_cancel_bui(log->l_mp, ailp, lip);
4914 break;
Darrick J. Wongdc423752016-08-03 11:23:49 +10004915 }
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004916
4917 lip = xfs_trans_ail_cursor_next(ailp, &cur);
4918 }
4919
4920 xfs_trans_ail_cursor_done(&cur);
Matthew Wilcox57e80952018-03-07 14:59:39 -08004921 spin_unlock(&ailp->ail_lock);
Brian Fosterf0b2efa2015-08-19 09:58:36 +10004922}
4923
4924/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 * This routine performs a transaction to null out a bad inode pointer
4926 * in an agi unlinked inode hash bucket.
4927 */
4928STATIC void
4929xlog_recover_clear_agi_bucket(
4930 xfs_mount_t *mp,
4931 xfs_agnumber_t agno,
4932 int bucket)
4933{
4934 xfs_trans_t *tp;
4935 xfs_agi_t *agi;
4936 xfs_buf_t *agibp;
4937 int offset;
4938 int error;
4939
Christoph Hellwig253f4912016-04-06 09:19:55 +10004940 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_clearagi, 0, 0, 0, &tp);
David Chinnere5720ee2008-04-10 12:21:18 +10004941 if (error)
Christoph Hellwig253f4912016-04-06 09:19:55 +10004942 goto out_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004944 error = xfs_read_agi(mp, tp, agno, &agibp);
4945 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10004946 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004948 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11004949 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 offset = offsetof(xfs_agi_t, agi_unlinked) +
4951 (sizeof(xfs_agino_t) * bucket);
4952 xfs_trans_log_buf(tp, agibp, offset,
4953 (offset + sizeof(xfs_agino_t) - 1));
4954
Christoph Hellwig70393312015-06-04 13:48:08 +10004955 error = xfs_trans_commit(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10004956 if (error)
4957 goto out_error;
4958 return;
4959
4960out_abort:
Christoph Hellwig4906e212015-06-04 13:47:56 +10004961 xfs_trans_cancel(tp);
David Chinnere5720ee2008-04-10 12:21:18 +10004962out_error:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004963 xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
David Chinnere5720ee2008-04-10 12:21:18 +10004964 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
Christoph Hellwig23fac502008-11-28 14:23:40 +11004967STATIC xfs_agino_t
4968xlog_recover_process_one_iunlink(
4969 struct xfs_mount *mp,
4970 xfs_agnumber_t agno,
4971 xfs_agino_t agino,
4972 int bucket)
4973{
4974 struct xfs_buf *ibp;
4975 struct xfs_dinode *dip;
4976 struct xfs_inode *ip;
4977 xfs_ino_t ino;
4978 int error;
4979
4980 ino = XFS_AGINO_TO_INO(mp, agno, agino);
Dave Chinner7b6259e2010-06-24 11:35:17 +10004981 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11004982 if (error)
4983 goto fail;
4984
4985 /*
4986 * Get the on disk inode to find the next inode in the bucket.
4987 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04004988 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11004989 if (error)
Christoph Hellwig0e446672008-11-28 14:23:42 +11004990 goto fail_iput;
Christoph Hellwig23fac502008-11-28 14:23:40 +11004991
Darrick J. Wong17c12bc2016-10-03 09:11:29 -07004992 xfs_iflags_clear(ip, XFS_IRECOVERY);
Dave Chinner54d7b5c2016-02-09 16:54:58 +11004993 ASSERT(VFS_I(ip)->i_nlink == 0);
Dave Chinnerc19b3b052016-02-09 16:54:58 +11004994 ASSERT(VFS_I(ip)->i_mode != 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11004995
4996 /* setup for the next pass */
4997 agino = be32_to_cpu(dip->di_next_unlinked);
4998 xfs_buf_relse(ibp);
4999
5000 /*
5001 * Prevent any DMAPI event from being sent when the reference on
5002 * the inode is dropped.
5003 */
5004 ip->i_d.di_dmevmask = 0;
5005
Darrick J. Wong44a87362018-07-25 12:52:32 -07005006 xfs_irele(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11005007 return agino;
5008
Christoph Hellwig0e446672008-11-28 14:23:42 +11005009 fail_iput:
Darrick J. Wong44a87362018-07-25 12:52:32 -07005010 xfs_irele(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11005011 fail:
5012 /*
5013 * We can't read in the inode this bucket points to, or this inode
5014 * is messed up. Just ditch this bucket of inodes. We will lose
5015 * some inodes and space, but at least we won't hang.
5016 *
5017 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
5018 * clear the inode pointer in the bucket.
5019 */
5020 xlog_recover_clear_agi_bucket(mp, agno, bucket);
5021 return NULLAGINO;
5022}
5023
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024/*
5025 * xlog_iunlink_recover
5026 *
5027 * This is called during recovery to process any inodes which
5028 * we unlinked but not freed when the system crashed. These
5029 * inodes will be on the lists in the AGI blocks. What we do
5030 * here is scan all the AGIs and fully truncate and free any
5031 * inodes found on the lists. Each inode is removed from the
5032 * lists when it has been fully truncated and is freed. The
5033 * freeing of the inode and its removal from the list must be
5034 * atomic.
5035 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05005036STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037xlog_recover_process_iunlinks(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005038 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039{
5040 xfs_mount_t *mp;
5041 xfs_agnumber_t agno;
5042 xfs_agi_t *agi;
5043 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045 int bucket;
5046 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047
5048 mp = log->l_mp;
5049
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
5051 /*
5052 * Find the agi for this ag.
5053 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11005054 error = xfs_read_agi(mp, NULL, agno, &agibp);
5055 if (error) {
5056 /*
5057 * AGI is b0rked. Don't process it.
5058 *
5059 * We should probably mark the filesystem as corrupt
5060 * after we've recovered all the ag's we can....
5061 */
5062 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 }
Jan Karad97d32e2012-03-15 09:34:02 +00005064 /*
5065 * Unlock the buffer so that it can be acquired in the normal
5066 * course of the transaction to truncate and free each inode.
5067 * Because we are not racing with anyone else here for the AGI
5068 * buffer, we don't even need to hold it locked to read the
5069 * initial unlinked bucket entries out of the buffer. We keep
5070 * buffer reference though, so that it stays pinned in memory
5071 * while we need the buffer.
5072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 agi = XFS_BUF_TO_AGI(agibp);
Jan Karad97d32e2012-03-15 09:34:02 +00005074 xfs_buf_unlock(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
5076 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11005077 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 while (agino != NULLAGINO) {
Christoph Hellwig23fac502008-11-28 14:23:40 +11005079 agino = xlog_recover_process_one_iunlink(mp,
5080 agno, agino, bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
5082 }
Jan Karad97d32e2012-03-15 09:34:02 +00005083 xfs_buf_rele(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085}
5086
Eric Sandeen91083262019-05-01 20:26:30 -07005087STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088xlog_unpack_data(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005089 struct xlog_rec_header *rhead,
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10005090 char *dp,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005091 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093 int i, j, k;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005095 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005097 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 dp += BBSIZE;
5099 }
5100
Eric Sandeen62118702008-03-06 13:44:28 +11005101 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11005102 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005103 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
5105 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005106 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005107 dp += BBSIZE;
5108 }
5109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Brian Foster9d949012016-01-04 15:55:10 +11005112/*
Brian Fosterb94fb2d2016-01-04 15:55:10 +11005113 * CRC check, unpack and process a log record.
Brian Foster9d949012016-01-04 15:55:10 +11005114 */
5115STATIC int
5116xlog_recover_process(
5117 struct xlog *log,
5118 struct hlist_head rhash[],
5119 struct xlog_rec_header *rhead,
5120 char *dp,
Brian Foster12818d22016-09-26 08:22:16 +10005121 int pass,
5122 struct list_head *buffer_list)
Brian Foster9d949012016-01-04 15:55:10 +11005123{
Dave Chinnercae028d2016-12-05 14:40:32 +11005124 __le32 old_crc = rhead->h_crc;
Brian Fosterb94fb2d2016-01-04 15:55:10 +11005125 __le32 crc;
5126
Brian Fosterb94fb2d2016-01-04 15:55:10 +11005127 crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
Brian Foster65282502016-01-04 15:55:10 +11005128
5129 /*
5130 * Nothing else to do if this is a CRC verification pass. Just return
5131 * if this a record with a non-zero crc. Unfortunately, mkfs always
Dave Chinnercae028d2016-12-05 14:40:32 +11005132 * sets old_crc to 0 so we must consider this valid even on v5 supers.
Brian Foster65282502016-01-04 15:55:10 +11005133 * Otherwise, return EFSBADCRC on failure so the callers up the stack
5134 * know precisely what failed.
5135 */
5136 if (pass == XLOG_RECOVER_CRCPASS) {
Dave Chinnercae028d2016-12-05 14:40:32 +11005137 if (old_crc && crc != old_crc)
Brian Foster65282502016-01-04 15:55:10 +11005138 return -EFSBADCRC;
5139 return 0;
5140 }
5141
5142 /*
5143 * We're in the normal recovery path. Issue a warning if and only if the
5144 * CRC in the header is non-zero. This is an advisory warning and the
5145 * zero CRC check prevents warnings from being emitted when upgrading
5146 * the kernel from one that does not add CRCs by default.
5147 */
Dave Chinnercae028d2016-12-05 14:40:32 +11005148 if (crc != old_crc) {
5149 if (old_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
Brian Fosterb94fb2d2016-01-04 15:55:10 +11005150 xfs_alert(log->l_mp,
5151 "log record CRC mismatch: found 0x%x, expected 0x%x.",
Dave Chinnercae028d2016-12-05 14:40:32 +11005152 le32_to_cpu(old_crc),
Brian Fosterb94fb2d2016-01-04 15:55:10 +11005153 le32_to_cpu(crc));
5154 xfs_hex_dump(dp, 32);
5155 }
5156
5157 /*
5158 * If the filesystem is CRC enabled, this mismatch becomes a
5159 * fatal log corruption failure.
5160 */
5161 if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
5162 return -EFSCORRUPTED;
5163 }
Brian Foster9d949012016-01-04 15:55:10 +11005164
Eric Sandeen91083262019-05-01 20:26:30 -07005165 xlog_unpack_data(rhead, dp, log);
Brian Foster9d949012016-01-04 15:55:10 +11005166
Brian Foster12818d22016-09-26 08:22:16 +10005167 return xlog_recover_process_data(log, rhash, rhead, dp, pass,
5168 buffer_list);
Brian Foster9d949012016-01-04 15:55:10 +11005169}
5170
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171STATIC int
5172xlog_valid_rec_header(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005173 struct xlog *log,
5174 struct xlog_rec_header *rhead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005175 xfs_daddr_t blkno)
5176{
5177 int hlen;
5178
Christoph Hellwig69ef9212011-07-08 14:36:05 +02005179 if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180 XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
5181 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10005182 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005183 }
5184 if (unlikely(
5185 (!rhead->h_version ||
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005186 (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005187 xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
Harvey Harrison34a622b2008-04-10 12:19:21 +10005188 __func__, be32_to_cpu(rhead->h_version));
Dave Chinner24513372014-06-25 14:58:08 +10005189 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005190 }
5191
5192 /* LR body must have data or it wouldn't have been written */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005193 hlen = be32_to_cpu(rhead->h_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005194 if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
5195 XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
5196 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10005197 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 }
5199 if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
5200 XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
5201 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10005202 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005203 }
5204 return 0;
5205}
5206
5207/*
5208 * Read the log from tail to head and process the log records found.
5209 * Handle the two cases where the tail and head are in the same cycle
5210 * and where the active portion of the log wraps around the end of
5211 * the physical log separately. The pass parameter is passed through
5212 * to the routines called to process the data and is not looked at
5213 * here.
5214 */
5215STATIC int
5216xlog_do_recovery_pass(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005217 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 xfs_daddr_t head_blk,
5219 xfs_daddr_t tail_blk,
Brian Fosterd7f37692016-01-04 15:55:10 +11005220 int pass,
5221 xfs_daddr_t *first_bad) /* out: first bad log rec */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005222{
5223 xlog_rec_header_t *rhead;
Brian Foster284f1c22017-08-08 18:21:51 -07005224 xfs_daddr_t blk_no, rblk_no;
Brian Fosterd7f37692016-01-04 15:55:10 +11005225 xfs_daddr_t rhead_blk;
Christoph Hellwigb2a922c2015-06-22 09:45:10 +10005226 char *offset;
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005227 char *hbp, *dbp;
Brian Fostera70f9fe2016-01-04 15:55:10 +11005228 int error = 0, h_size, h_len;
Brian Foster12818d22016-09-26 08:22:16 +10005229 int error2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005230 int bblks, split_bblks;
5231 int hblks, split_hblks, wrapped_hblks;
Brian Foster39775432017-06-24 10:11:41 -07005232 int i;
Dave Chinnerf0a76952010-01-11 11:49:57 +00005233 struct hlist_head rhash[XLOG_RHASH_SIZE];
Brian Foster12818d22016-09-26 08:22:16 +10005234 LIST_HEAD (buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
5236 ASSERT(head_blk != tail_blk);
Brian Fostera4c9b342017-08-08 18:21:53 -07005237 blk_no = rhead_blk = tail_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Brian Foster39775432017-06-24 10:11:41 -07005239 for (i = 0; i < XLOG_RHASH_SIZE; i++)
5240 INIT_HLIST_HEAD(&rhash[i]);
5241
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 /*
5243 * Read the header of the tail block and get the iclog buffer size from
5244 * h_size. Use this to tell how many sectors make up the log header.
5245 */
Eric Sandeen62118702008-03-06 13:44:28 +11005246 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005247 /*
5248 * When using variable length iclogs, read first sector of
5249 * iclog header and extract the header size from it. Get a
5250 * new hbp that is the correct size.
5251 */
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07005252 hbp = xlog_alloc_buffer(log, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253 if (!hbp)
Dave Chinner24513372014-06-25 14:58:08 +10005254 return -ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005255
5256 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
5257 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005258 goto bread_err1;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005259
Linus Torvalds1da177e2005-04-16 15:20:36 -07005260 rhead = (xlog_rec_header_t *)offset;
5261 error = xlog_valid_rec_header(log, rhead, tail_blk);
5262 if (error)
5263 goto bread_err1;
Brian Fostera70f9fe2016-01-04 15:55:10 +11005264
5265 /*
5266 * xfsprogs has a bug where record length is based on lsunit but
5267 * h_size (iclog size) is hardcoded to 32k. Now that we
5268 * unconditionally CRC verify the unmount record, this means the
5269 * log buffer can be too small for the record and cause an
5270 * overrun.
5271 *
5272 * Detect this condition here. Use lsunit for the buffer size as
5273 * long as this looks like the mkfs case. Otherwise, return an
5274 * error to avoid a buffer overrun.
5275 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005276 h_size = be32_to_cpu(rhead->h_size);
Brian Fostera70f9fe2016-01-04 15:55:10 +11005277 h_len = be32_to_cpu(rhead->h_len);
5278 if (h_len > h_size) {
5279 if (h_len <= log->l_mp->m_logbsize &&
5280 be32_to_cpu(rhead->h_num_logops) == 1) {
5281 xfs_warn(log->l_mp,
5282 "invalid iclog size (%d bytes), using lsunit (%d bytes)",
5283 h_size, log->l_mp->m_logbsize);
5284 h_size = log->l_mp->m_logbsize;
5285 } else
5286 return -EFSCORRUPTED;
5287 }
5288
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005289 if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
5291 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
5292 if (h_size % XLOG_HEADER_CYCLE_SIZE)
5293 hblks++;
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005294 kmem_free(hbp);
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07005295 hbp = xlog_alloc_buffer(log, hblks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296 } else {
5297 hblks = 1;
5298 }
5299 } else {
Alex Elder69ce58f2010-04-20 17:09:59 +10005300 ASSERT(log->l_sectBBsize == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005301 hblks = 1;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07005302 hbp = xlog_alloc_buffer(log, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303 h_size = XLOG_BIG_RECORD_BSIZE;
5304 }
5305
5306 if (!hbp)
Dave Chinner24513372014-06-25 14:58:08 +10005307 return -ENOMEM;
Christoph Hellwig6e9b3dd2019-06-28 19:27:27 -07005308 dbp = xlog_alloc_buffer(log, BTOBB(h_size));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309 if (!dbp) {
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005310 kmem_free(hbp);
Dave Chinner24513372014-06-25 14:58:08 +10005311 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 }
5313
5314 memset(rhash, 0, sizeof(rhash));
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005315 if (tail_blk > head_blk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316 /*
5317 * Perform recovery around the end of the physical log.
5318 * When the head is not on the same cycle number as the tail,
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005319 * we can't do a sequential recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005321 while (blk_no < log->l_logBBsize) {
5322 /*
5323 * Check for header wrapping around physical end-of-log
5324 */
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005325 offset = hbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326 split_hblks = 0;
5327 wrapped_hblks = 0;
5328 if (blk_no + hblks <= log->l_logBBsize) {
5329 /* Read header in one read */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005330 error = xlog_bread(log, blk_no, hblks, hbp,
5331 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005332 if (error)
5333 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334 } else {
5335 /* This LR is split across physical log end */
5336 if (blk_no != log->l_logBBsize) {
5337 /* some data before physical log end */
5338 ASSERT(blk_no <= INT_MAX);
5339 split_hblks = log->l_logBBsize - (int)blk_no;
5340 ASSERT(split_hblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005341 error = xlog_bread(log, blk_no,
5342 split_hblks, hbp,
5343 &offset);
5344 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005346 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005347
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 /*
5349 * Note: this black magic still works with
5350 * large sector sizes (non-512) only because:
5351 * - we increased the buffer size originally
5352 * by 1 sector giving us enough extra space
5353 * for the second read;
5354 * - the log start is guaranteed to be sector
5355 * aligned;
5356 * - we read the log end (LR header start)
5357 * _first_, then the log start (LR header end)
5358 * - order is important.
5359 */
David Chinner234f56a2008-04-10 12:24:24 +10005360 wrapped_hblks = hblks - split_hblks;
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005361 error = xlog_bread_noalign(log, 0,
5362 wrapped_hblks,
Dave Chinner44396472011-04-21 09:34:27 +00005363 offset + BBTOB(split_hblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 if (error)
5365 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366 }
5367 rhead = (xlog_rec_header_t *)offset;
5368 error = xlog_valid_rec_header(log, rhead,
5369 split_hblks ? blk_no : 0);
5370 if (error)
5371 goto bread_err2;
5372
Christoph Hellwigb53e6752007-10-12 10:59:34 +10005373 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 blk_no += hblks;
5375
Brian Foster284f1c22017-08-08 18:21:51 -07005376 /*
5377 * Read the log record data in multiple reads if it
5378 * wraps around the end of the log. Note that if the
5379 * header already wrapped, blk_no could point past the
5380 * end of the log. The record data is contiguous in
5381 * that case.
5382 */
5383 if (blk_no + bblks <= log->l_logBBsize ||
5384 blk_no >= log->l_logBBsize) {
Dave Chinner0703a8e2018-06-08 09:54:22 -07005385 rblk_no = xlog_wrap_logbno(log, blk_no);
Brian Foster284f1c22017-08-08 18:21:51 -07005386 error = xlog_bread(log, rblk_no, bblks, dbp,
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005387 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 if (error)
5389 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390 } else {
5391 /* This log record is split across the
5392 * physical end of log */
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005393 offset = dbp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005394 split_bblks = 0;
5395 if (blk_no != log->l_logBBsize) {
5396 /* some data is before the physical
5397 * end of log */
5398 ASSERT(!wrapped_hblks);
5399 ASSERT(blk_no <= INT_MAX);
5400 split_bblks =
5401 log->l_logBBsize - (int)blk_no;
5402 ASSERT(split_bblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005403 error = xlog_bread(log, blk_no,
5404 split_bblks, dbp,
5405 &offset);
5406 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005407 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005409
Linus Torvalds1da177e2005-04-16 15:20:36 -07005410 /*
5411 * Note: this black magic still works with
5412 * large sector sizes (non-512) only because:
5413 * - we increased the buffer size originally
5414 * by 1 sector giving us enough extra space
5415 * for the second read;
5416 * - the log start is guaranteed to be sector
5417 * aligned;
5418 * - we read the log end (LR header start)
5419 * _first_, then the log start (LR header end)
5420 * - order is important.
5421 */
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005422 error = xlog_bread_noalign(log, 0,
5423 bblks - split_bblks,
Dave Chinner44396472011-04-21 09:34:27 +00005424 offset + BBTOB(split_bblks));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005425 if (error)
5426 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005427 }
Christoph Hellwig0e446be2012-11-12 22:54:24 +11005428
Brian Foster9d949012016-01-04 15:55:10 +11005429 error = xlog_recover_process(log, rhash, rhead, offset,
Brian Foster12818d22016-09-26 08:22:16 +10005430 pass, &buffer_list);
Christoph Hellwig0e446be2012-11-12 22:54:24 +11005431 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005432 goto bread_err2;
Brian Fosterd7f37692016-01-04 15:55:10 +11005433
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434 blk_no += bblks;
Brian Fosterd7f37692016-01-04 15:55:10 +11005435 rhead_blk = blk_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 }
5437
5438 ASSERT(blk_no >= log->l_logBBsize);
5439 blk_no -= log->l_logBBsize;
Brian Fosterd7f37692016-01-04 15:55:10 +11005440 rhead_blk = blk_no;
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005443 /* read first part of physical log */
5444 while (blk_no < head_blk) {
5445 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
5446 if (error)
5447 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005448
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005449 rhead = (xlog_rec_header_t *)offset;
5450 error = xlog_valid_rec_header(log, rhead, blk_no);
5451 if (error)
5452 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005453
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005454 /* blocks in data section */
5455 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
5456 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
5457 &offset);
5458 if (error)
5459 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01005460
Brian Foster12818d22016-09-26 08:22:16 +10005461 error = xlog_recover_process(log, rhash, rhead, offset, pass,
5462 &buffer_list);
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005463 if (error)
5464 goto bread_err2;
Brian Fosterd7f37692016-01-04 15:55:10 +11005465
Eric Sandeen970fd3f2014-09-09 11:57:29 +10005466 blk_no += bblks + hblks;
Brian Fosterd7f37692016-01-04 15:55:10 +11005467 rhead_blk = blk_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005468 }
5469
5470 bread_err2:
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005471 kmem_free(dbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005472 bread_err1:
Christoph Hellwig6ad5b322019-06-28 19:27:26 -07005473 kmem_free(hbp);
Brian Fosterd7f37692016-01-04 15:55:10 +11005474
Brian Foster12818d22016-09-26 08:22:16 +10005475 /*
5476 * Submit buffers that have been added from the last record processed,
5477 * regardless of error status.
5478 */
5479 if (!list_empty(&buffer_list))
5480 error2 = xfs_buf_delwri_submit(&buffer_list);
5481
Brian Fosterd7f37692016-01-04 15:55:10 +11005482 if (error && first_bad)
5483 *first_bad = rhead_blk;
5484
Brian Foster39775432017-06-24 10:11:41 -07005485 /*
5486 * Transactions are freed at commit time but transactions without commit
5487 * records on disk are never committed. Free any that may be left in the
5488 * hash table.
5489 */
5490 for (i = 0; i < XLOG_RHASH_SIZE; i++) {
5491 struct hlist_node *tmp;
5492 struct xlog_recover *trans;
5493
5494 hlist_for_each_entry_safe(trans, tmp, &rhash[i], r_list)
5495 xlog_recover_free_trans(trans);
5496 }
5497
Brian Foster12818d22016-09-26 08:22:16 +10005498 return error ? error : error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499}
5500
5501/*
5502 * Do the recovery of the log. We actually do this in two phases.
5503 * The two passes are necessary in order to implement the function
5504 * of cancelling a record written into the log. The first pass
5505 * determines those things which have been cancelled, and the
5506 * second pass replays log items normally except for those which
5507 * have been cancelled. The handling of the replay and cancellations
5508 * takes place in the log item type specific routines.
5509 *
5510 * The table of items which have cancel records in the log is allocated
5511 * and freed at this level, since only here do we know when all of
5512 * the log recovery has been completed.
5513 */
5514STATIC int
5515xlog_do_log_recovery(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005516 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517 xfs_daddr_t head_blk,
5518 xfs_daddr_t tail_blk)
5519{
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00005520 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521
5522 ASSERT(head_blk != tail_blk);
5523
5524 /*
5525 * First do a pass to find all of the cancelled buf log items.
5526 * Store them in the buf_cancel_table for use in the second pass.
5527 */
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00005528 log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
5529 sizeof(struct list_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 KM_SLEEP);
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00005531 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
5532 INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
5533
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
Brian Fosterd7f37692016-01-04 15:55:10 +11005535 XLOG_RECOVER_PASS1, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 if (error != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005537 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 log->l_buf_cancel_table = NULL;
5539 return error;
5540 }
5541 /*
5542 * Then do a second pass to actually recover the items in the log.
5543 * When it is complete free the table of buf cancel items.
5544 */
5545 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
Brian Fosterd7f37692016-01-04 15:55:10 +11005546 XLOG_RECOVER_PASS2, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547#ifdef DEBUG
Tim Shimmin6d192a92006-06-09 14:55:38 +10005548 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 int i;
5550
5551 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00005552 ASSERT(list_empty(&log->l_buf_cancel_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005553 }
5554#endif /* DEBUG */
5555
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10005556 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557 log->l_buf_cancel_table = NULL;
5558
5559 return error;
5560}
5561
5562/*
5563 * Do the actual recovery
5564 */
5565STATIC int
5566xlog_do_recover(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005567 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005568 xfs_daddr_t head_blk,
5569 xfs_daddr_t tail_blk)
5570{
Dave Chinnera7980112016-03-07 08:39:36 +11005571 struct xfs_mount *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572 int error;
5573 xfs_buf_t *bp;
5574 xfs_sb_t *sbp;
5575
Brian Fostere67d3d42017-08-08 18:21:53 -07005576 trace_xfs_log_recover(log, head_blk, tail_blk);
5577
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 /*
5579 * First replay the images in the log.
5580 */
5581 error = xlog_do_log_recovery(log, head_blk, tail_blk);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10005582 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005583 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
5585 /*
5586 * If IO errors happened during recovery, bail out.
5587 */
Dave Chinnera7980112016-03-07 08:39:36 +11005588 if (XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner24513372014-06-25 14:58:08 +10005589 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 }
5591
5592 /*
5593 * We now update the tail_lsn since much of the recovery has completed
5594 * and there may be space available to use. If there were no extent
5595 * or iunlinks, we can free up the entire log and set the tail_lsn to
5596 * be the last_sync_lsn. This was set in xlog_find_tail to be the
5597 * lsn of the last known good LR on disk. If there are extent frees
5598 * or iunlinks they will have some entries in the AIL; so we look at
5599 * the AIL to determine how to set the tail_lsn.
5600 */
Dave Chinnera7980112016-03-07 08:39:36 +11005601 xlog_assign_tail_lsn(mp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602
5603 /*
5604 * Now that we've finished replaying all buffer and inode
Dave Chinner98021822012-11-12 22:54:03 +11005605 * updates, re-read in the superblock and reverify it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 */
Eric Sandeen8c9ce2f2019-06-12 08:59:58 -07005607 bp = xfs_getsb(mp);
Dave Chinner1157b32c2016-02-10 15:01:11 +11005608 bp->b_flags &= ~(XBF_DONE | XBF_ASYNC);
Dave Chinnerb68c0822016-02-10 15:01:11 +11005609 ASSERT(!(bp->b_flags & XBF_WRITE));
Dave Chinner0cac6822016-02-10 15:01:11 +11005610 bp->b_flags |= XBF_READ;
Dave Chinner1813dd62012-11-14 17:54:40 +11005611 bp->b_ops = &xfs_sb_buf_ops;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -08005612
Brian Foster6af88cd2018-07-11 22:26:35 -07005613 error = xfs_buf_submit(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10005614 if (error) {
Dave Chinnera7980112016-03-07 08:39:36 +11005615 if (!XFS_FORCED_SHUTDOWN(mp)) {
Dave Chinner595bff72014-10-02 09:05:14 +10005616 xfs_buf_ioerror_alert(bp, __func__);
5617 ASSERT(0);
5618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005619 xfs_buf_relse(bp);
5620 return error;
5621 }
5622
5623 /* Convert superblock from on-disk format */
Dave Chinnera7980112016-03-07 08:39:36 +11005624 sbp = &mp->m_sb;
Dave Chinner98021822012-11-12 22:54:03 +11005625 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 xfs_buf_relse(bp);
5627
Dave Chinnera7980112016-03-07 08:39:36 +11005628 /* re-initialise in-core superblock and geometry structures */
5629 xfs_reinit_percpu_counters(mp);
5630 error = xfs_initialize_perag(mp, sbp->sb_agcount, &mp->m_maxagi);
5631 if (error) {
5632 xfs_warn(mp, "Failed post-recovery per-ag init: %d", error);
5633 return error;
5634 }
Darrick J. Wong52548852016-08-03 11:38:24 +10005635 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp);
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11005636
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 xlog_recover_check_summary(log);
5638
5639 /* Normal transactions can now occur */
5640 log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
5641 return 0;
5642}
5643
5644/*
5645 * Perform recovery and re-initialize some log variables in xlog_find_tail.
5646 *
5647 * Return error or zero.
5648 */
5649int
5650xlog_recover(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005651 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005652{
5653 xfs_daddr_t head_blk, tail_blk;
5654 int error;
5655
5656 /* find the tail of the log */
Brian Fostera45086e2015-10-12 15:59:25 +11005657 error = xlog_find_tail(log, &head_blk, &tail_blk);
5658 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659 return error;
5660
Brian Fostera45086e2015-10-12 15:59:25 +11005661 /*
5662 * The superblock was read before the log was available and thus the LSN
5663 * could not be verified. Check the superblock LSN against the current
5664 * LSN now that it's known.
5665 */
5666 if (xfs_sb_version_hascrc(&log->l_mp->m_sb) &&
5667 !xfs_log_check_lsn(log->l_mp, log->l_mp->m_sb.sb_lsn))
5668 return -EINVAL;
5669
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 if (tail_blk != head_blk) {
5671 /* There used to be a comment here:
5672 *
5673 * disallow recovery on read-only mounts. note -- mount
5674 * checks for ENOSPC and turns it into an intelligent
5675 * error message.
5676 * ...but this is no longer true. Now, unless you specify
5677 * NORECOVERY (in which case this function would never be
5678 * called), we just go ahead and recover. We do this all
5679 * under the vfs layer, so we can get away with it unless
5680 * the device itself is read-only, in which case we fail.
5681 */
Utako Kusaka3a02ee12007-05-08 13:50:06 +10005682 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683 return error;
5684 }
5685
Dave Chinnere721f502013-04-03 16:11:32 +11005686 /*
5687 * Version 5 superblock log feature mask validation. We know the
5688 * log is dirty so check if there are any unknown log features
5689 * in what we need to recover. If there are unknown features
5690 * (e.g. unsupported transactions, then simply reject the
5691 * attempt at recovery before touching anything.
5692 */
5693 if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
5694 xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
5695 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
5696 xfs_warn(log->l_mp,
Joe Perchesf41febd2015-07-29 11:52:04 +10005697"Superblock has unknown incompatible log features (0x%x) enabled.",
Dave Chinnere721f502013-04-03 16:11:32 +11005698 (log->l_mp->m_sb.sb_features_log_incompat &
5699 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
Joe Perchesf41febd2015-07-29 11:52:04 +10005700 xfs_warn(log->l_mp,
5701"The log can not be fully and/or safely recovered by this kernel.");
5702 xfs_warn(log->l_mp,
5703"Please recover the log on a kernel that supports the unknown features.");
Dave Chinner24513372014-06-25 14:58:08 +10005704 return -EINVAL;
Dave Chinnere721f502013-04-03 16:11:32 +11005705 }
5706
Brian Foster2e227172014-09-09 11:56:13 +10005707 /*
5708 * Delay log recovery if the debug hook is set. This is debug
5709 * instrumention to coordinate simulation of I/O failures with
5710 * log recovery.
5711 */
5712 if (xfs_globals.log_recovery_delay) {
5713 xfs_notice(log->l_mp,
5714 "Delaying log recovery for %d seconds.",
5715 xfs_globals.log_recovery_delay);
5716 msleep(xfs_globals.log_recovery_delay * 1000);
5717 }
5718
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005719 xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
5720 log->l_mp->m_logname ? log->l_mp->m_logname
5721 : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722
5723 error = xlog_do_recover(log, head_blk, tail_blk);
5724 log->l_flags |= XLOG_RECOVERY_NEEDED;
5725 }
5726 return error;
5727}
5728
5729/*
5730 * In the first part of recovery we replay inodes and buffers and build
5731 * up the list of extent free items which need to be processed. Here
5732 * we process the extent free items and clean up the on disk unlinked
5733 * inode lists. This is separated from the first part of recovery so
5734 * that the root and real-time bitmap inodes can be read in from disk in
5735 * between the two stages. This is necessary so that we can free space
5736 * in the real-time portion of the file system.
5737 */
5738int
5739xlog_recover_finish(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005740 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741{
5742 /*
5743 * Now we're ready to do the transactions needed for the
5744 * rest of recovery. Start with completing all the extent
5745 * free intent records and then process the unlinked inode
5746 * lists. At this point, we essentially run in normal mode
5747 * except that we're still performing recovery actions
5748 * rather than accepting new requests.
5749 */
5750 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
David Chinner3c1e2bb2008-04-10 12:21:11 +10005751 int error;
Darrick J. Wongdc423752016-08-03 11:23:49 +10005752 error = xlog_recover_process_intents(log);
David Chinner3c1e2bb2008-04-10 12:21:11 +10005753 if (error) {
Darrick J. Wongdc423752016-08-03 11:23:49 +10005754 xfs_alert(log->l_mp, "Failed to recover intents");
David Chinner3c1e2bb2008-04-10 12:21:11 +10005755 return error;
5756 }
Darrick J. Wong9e88b5d2016-08-03 12:09:48 +10005757
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 /*
Darrick J. Wongdc423752016-08-03 11:23:49 +10005759 * Sync the log to get all the intents out of the AIL.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 * This isn't absolutely necessary, but it helps in
5761 * case the unlink transactions would have problems
Darrick J. Wongdc423752016-08-03 11:23:49 +10005762 * pushing the intents out of the way.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00005764 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765
Christoph Hellwig42490232008-08-13 16:49:32 +10005766 xlog_recover_process_iunlinks(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767
5768 xlog_recover_check_summary(log);
5769
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005770 xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
5771 log->l_mp->m_logname ? log->l_mp->m_logname
5772 : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005773 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
5774 } else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005775 xfs_info(log->l_mp, "Ending clean mount");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 }
5777 return 0;
5778}
5779
Hariprasad Kelama7a92502019-07-03 07:34:18 -07005780void
Brian Fosterf0b2efa2015-08-19 09:58:36 +10005781xlog_recover_cancel(
5782 struct xlog *log)
5783{
Brian Fosterf0b2efa2015-08-19 09:58:36 +10005784 if (log->l_flags & XLOG_RECOVERY_NEEDED)
Hariprasad Kelama7a92502019-07-03 07:34:18 -07005785 xlog_recover_cancel_intents(log);
Brian Fosterf0b2efa2015-08-19 09:58:36 +10005786}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787
5788#if defined(DEBUG)
5789/*
5790 * Read all of the agf and agi counters and check that they
5791 * are consistent with the superblock counters.
5792 */
Christoph Hellwige89fbb52017-11-06 11:54:01 -08005793STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794xlog_recover_check_summary(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05005795 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
5797 xfs_mount_t *mp;
5798 xfs_agf_t *agfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 xfs_buf_t *agfbp;
5800 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 xfs_agnumber_t agno;
Darrick J. Wongc8ce5402017-06-16 11:00:05 -07005802 uint64_t freeblks;
5803 uint64_t itotal;
5804 uint64_t ifree;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11005805 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
5807 mp = log->l_mp;
5808
5809 freeblks = 0LL;
5810 itotal = 0LL;
5811 ifree = 0LL;
5812 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
From: Christoph Hellwig48056212008-11-28 14:23:38 +11005813 error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
5814 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005815 xfs_alert(mp, "%s agf read failed agno %d error %d",
5816 __func__, agno, error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11005817 } else {
5818 agfp = XFS_BUF_TO_AGF(agfbp);
5819 freeblks += be32_to_cpu(agfp->agf_freeblks) +
5820 be32_to_cpu(agfp->agf_flcount);
5821 xfs_buf_relse(agfbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005822 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11005824 error = xfs_read_agi(mp, NULL, agno, &agibp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11005825 if (error) {
5826 xfs_alert(mp, "%s agi read failed agno %d error %d",
5827 __func__, agno, error);
5828 } else {
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11005829 struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11005831 itotal += be32_to_cpu(agi->agi_count);
5832 ifree += be32_to_cpu(agi->agi_freecount);
5833 xfs_buf_relse(agibp);
5834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836}
5837#endif /* DEBUG */