blob: f7c312fe91fbb6b42737b39a3c3abe328a1a2eef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tim Shimmin87c199c2006-06-09 14:56:16 +10002 * Copyright (c) 2000-2006 Silicon Graphics, Inc.
Nathan Scott7b718762005-11-02 14:58:39 +11003 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Nathan Scotta844f452005-11-02 14:38:42 +110019#include "xfs_fs.h"
Dave Chinner70a98832013-10-23 10:36:05 +110020#include "xfs_shared.h"
Dave Chinner239880e2013-10-23 10:50:10 +110021#include "xfs_format.h"
22#include "xfs_log_format.h"
23#include "xfs_trans_resv.h"
Nathan Scotta844f452005-11-02 14:38:42 +110024#include "xfs_bit.h"
Nathan Scotta844f452005-11-02 14:38:42 +110025#include "xfs_inum.h"
Nathan Scotta844f452005-11-02 14:38:42 +110026#include "xfs_sb.h"
27#include "xfs_ag.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110029#include "xfs_da_format.h"
Dave Chinner9a2cc412014-12-04 09:43:17 +110030#include "xfs_da_btree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_trans.h"
Dave Chinner239880e2013-10-23 10:50:10 +110033#include "xfs_log.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include "xfs_log_priv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include "xfs_log_recover.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110036#include "xfs_inode_item.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include "xfs_extfree_item.h"
38#include "xfs_trans_priv.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110039#include "xfs_alloc.h"
40#include "xfs_ialloc.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include "xfs_quota.h"
Christoph Hellwig0e446be2012-11-12 22:54:24 +110042#include "xfs_cksum.h"
Christoph Hellwig0b1b2132009-12-14 23:14:59 +000043#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110044#include "xfs_icache.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110045#include "xfs_bmap_btree.h"
46#include "xfs_dinode.h"
47#include "xfs_error.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100048#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Dave Chinnerfc06c6d2013-08-12 20:49:22 +100050#define BLK_AVG(blk1, blk2) ((blk1+blk2) >> 1)
51
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050052STATIC int
53xlog_find_zeroed(
54 struct xlog *,
55 xfs_daddr_t *);
56STATIC int
57xlog_clear_stale_blocks(
58 struct xlog *,
59 xfs_lsn_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#if defined(DEBUG)
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050061STATIC void
62xlog_recover_check_summary(
63 struct xlog *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#else
65#define xlog_recover_check_summary(log)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +000069 * This structure is used during recovery to record the buf log items which
70 * have been canceled and should not be replayed.
71 */
72struct xfs_buf_cancel {
73 xfs_daddr_t bc_blkno;
74 uint bc_len;
75 int bc_refcount;
76 struct list_head bc_list;
77};
78
79/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 * Sector aligned buffer routines for buffer create/read/write/access
81 */
82
Alex Elderff30a622010-04-13 15:22:58 +100083/*
84 * Verify the given count of basic blocks is valid number of blocks
85 * to specify for an operation involving the given XFS log buffer.
86 * Returns nonzero if the count is valid, 0 otherwise.
87 */
88
89static inline int
90xlog_buf_bbcount_valid(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -050091 struct xlog *log,
Alex Elderff30a622010-04-13 15:22:58 +100092 int bbcount)
93{
94 return bbcount > 0 && bbcount <= log->l_logBBsize;
95}
96
Alex Elder36adecf2010-04-13 15:21:13 +100097/*
98 * Allocate a buffer to hold log data. The buffer needs to be able
99 * to map to a range of nbblks basic blocks at any valid (basic
100 * block) offset within the log.
101 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000102STATIC xfs_buf_t *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103xlog_get_bp(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500104 struct xlog *log,
Dave Chinner32281492009-01-22 15:37:47 +1100105 int nbblks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +0200107 struct xfs_buf *bp;
108
Alex Elderff30a622010-04-13 15:22:58 +1000109 if (!xlog_buf_bbcount_valid(log, nbblks)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100110 xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
Alex Elderff30a622010-04-13 15:22:58 +1000111 nbblks);
112 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner32281492009-01-22 15:37:47 +1100113 return NULL;
114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Alex Elder36adecf2010-04-13 15:21:13 +1000116 /*
117 * We do log I/O in units of log sectors (a power-of-2
118 * multiple of the basic block size), so we round up the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300119 * requested size to accommodate the basic blocks required
Alex Elder36adecf2010-04-13 15:21:13 +1000120 * for complete log sectors.
121 *
122 * In addition, the buffer may be used for a non-sector-
123 * aligned block offset, in which case an I/O of the
124 * requested size could extend beyond the end of the
125 * buffer. If the requested size is only 1 basic block it
126 * will never straddle a sector boundary, so this won't be
127 * an issue. Nor will this be a problem if the log I/O is
128 * done in basic blocks (sector size 1). But otherwise we
129 * extend the buffer by one extra log sector to ensure
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300130 * there's space to accommodate this possibility.
Alex Elder36adecf2010-04-13 15:21:13 +1000131 */
Alex Elder69ce58f2010-04-20 17:09:59 +1000132 if (nbblks > 1 && log->l_sectBBsize > 1)
133 nbblks += log->l_sectBBsize;
134 nbblks = round_up(nbblks, log->l_sectBBsize);
Alex Elder36adecf2010-04-13 15:21:13 +1000135
Dave Chinnere70b73f2012-04-23 15:58:49 +1000136 bp = xfs_buf_get_uncached(log->l_mp->m_logdev_targp, nbblks, 0);
Christoph Hellwigc8da0fa2011-07-08 14:36:25 +0200137 if (bp)
138 xfs_buf_unlock(bp);
139 return bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140}
141
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000142STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143xlog_put_bp(
144 xfs_buf_t *bp)
145{
146 xfs_buf_free(bp);
147}
148
Alex Elder48389ef2010-04-20 17:10:21 +1000149/*
150 * Return the address of the start of the given block number's data
151 * in a log buffer. The buffer covers a log sector-aligned region.
152 */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100153STATIC xfs_caddr_t
154xlog_align(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500155 struct xlog *log,
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100156 xfs_daddr_t blk_no,
157 int nbblks,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500158 struct xfs_buf *bp)
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100159{
Christoph Hellwigfdc07f42010-05-10 17:28:14 +0000160 xfs_daddr_t offset = blk_no & ((xfs_daddr_t)log->l_sectBBsize - 1);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100161
Dave Chinner4e94b712012-04-23 15:58:51 +1000162 ASSERT(offset + nbblks <= bp->b_length);
Chandra Seetharaman62926042011-07-22 23:40:15 +0000163 return bp->b_addr + BBTOB(offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100164}
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/*
168 * nbblks should be uint, but oh well. Just want to catch that 32-bit length.
169 */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100170STATIC int
171xlog_bread_noalign(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500172 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 xfs_daddr_t blk_no,
174 int nbblks,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500175 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int error;
178
Alex Elderff30a622010-04-13 15:22:58 +1000179 if (!xlog_buf_bbcount_valid(log, nbblks)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100180 xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
Alex Elderff30a622010-04-13 15:22:58 +1000181 nbblks);
182 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +1000183 return -EFSCORRUPTED;
Dave Chinner32281492009-01-22 15:37:47 +1100184 }
185
Alex Elder69ce58f2010-04-20 17:09:59 +1000186 blk_no = round_down(blk_no, log->l_sectBBsize);
187 nbblks = round_up(nbblks, log->l_sectBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189 ASSERT(nbblks > 0);
Dave Chinner4e94b712012-04-23 15:58:51 +1000190 ASSERT(nbblks <= bp->b_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
193 XFS_BUF_READ(bp);
Dave Chinneraa0e8832012-04-23 15:58:52 +1000194 bp->b_io_length = nbblks;
Dave Chinner0e95f192012-04-23 15:58:46 +1000195 bp->b_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
Dave Chinner595bff72014-10-02 09:05:14 +1000197 error = xfs_buf_submit_wait(bp);
198 if (error && !XFS_FORCED_SHUTDOWN(log->l_mp))
Christoph Hellwig901796a2011-10-10 16:52:49 +0000199 xfs_buf_ioerror_alert(bp, __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 return error;
201}
202
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100203STATIC int
204xlog_bread(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500205 struct xlog *log,
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100206 xfs_daddr_t blk_no,
207 int nbblks,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500208 struct xfs_buf *bp,
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100209 xfs_caddr_t *offset)
210{
211 int error;
212
213 error = xlog_bread_noalign(log, blk_no, nbblks, bp);
214 if (error)
215 return error;
216
217 *offset = xlog_align(log, blk_no, nbblks, bp);
218 return 0;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/*
Dave Chinner44396472011-04-21 09:34:27 +0000222 * Read at an offset into the buffer. Returns with the buffer in it's original
223 * state regardless of the result of the read.
224 */
225STATIC int
226xlog_bread_offset(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500227 struct xlog *log,
Dave Chinner44396472011-04-21 09:34:27 +0000228 xfs_daddr_t blk_no, /* block to read from */
229 int nbblks, /* blocks to read */
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500230 struct xfs_buf *bp,
Dave Chinner44396472011-04-21 09:34:27 +0000231 xfs_caddr_t offset)
232{
Chandra Seetharaman62926042011-07-22 23:40:15 +0000233 xfs_caddr_t orig_offset = bp->b_addr;
Dave Chinner4e94b712012-04-23 15:58:51 +1000234 int orig_len = BBTOB(bp->b_length);
Dave Chinner44396472011-04-21 09:34:27 +0000235 int error, error2;
236
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +0000237 error = xfs_buf_associate_memory(bp, offset, BBTOB(nbblks));
Dave Chinner44396472011-04-21 09:34:27 +0000238 if (error)
239 return error;
240
241 error = xlog_bread_noalign(log, blk_no, nbblks, bp);
242
243 /* must reset buffer pointer even on error */
Chandra Seetharaman02fe03d2011-07-22 23:40:22 +0000244 error2 = xfs_buf_associate_memory(bp, orig_offset, orig_len);
Dave Chinner44396472011-04-21 09:34:27 +0000245 if (error)
246 return error;
247 return error2;
248}
249
250/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 * Write out the buffer at the given block for the given number of blocks.
252 * The buffer is kept locked across the write and is returned locked.
253 * This can only be used for synchronous log writes.
254 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000255STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256xlog_bwrite(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500257 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 xfs_daddr_t blk_no,
259 int nbblks,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500260 struct xfs_buf *bp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261{
262 int error;
263
Alex Elderff30a622010-04-13 15:22:58 +1000264 if (!xlog_buf_bbcount_valid(log, nbblks)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100265 xfs_warn(log->l_mp, "Invalid block length (0x%x) for buffer",
Alex Elderff30a622010-04-13 15:22:58 +1000266 nbblks);
267 XFS_ERROR_REPORT(__func__, XFS_ERRLEVEL_HIGH, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +1000268 return -EFSCORRUPTED;
Dave Chinner32281492009-01-22 15:37:47 +1100269 }
270
Alex Elder69ce58f2010-04-20 17:09:59 +1000271 blk_no = round_down(blk_no, log->l_sectBBsize);
272 nbblks = round_up(nbblks, log->l_sectBBsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
274 ASSERT(nbblks > 0);
Dave Chinner4e94b712012-04-23 15:58:51 +1000275 ASSERT(nbblks <= bp->b_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276
277 XFS_BUF_SET_ADDR(bp, log->l_logBBstart + blk_no);
278 XFS_BUF_ZEROFLAGS(bp);
Chandra Seetharaman72790aa2011-07-22 23:40:04 +0000279 xfs_buf_hold(bp);
Christoph Hellwig0c842ad2011-07-08 14:36:19 +0200280 xfs_buf_lock(bp);
Dave Chinneraa0e8832012-04-23 15:58:52 +1000281 bp->b_io_length = nbblks;
Dave Chinner0e95f192012-04-23 15:58:46 +1000282 bp->b_error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000284 error = xfs_bwrite(bp);
Christoph Hellwig901796a2011-10-10 16:52:49 +0000285 if (error)
286 xfs_buf_ioerror_alert(bp, __func__);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +0000287 xfs_buf_relse(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 return error;
289}
290
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291#ifdef DEBUG
292/*
293 * dump debug superblock and log record information
294 */
295STATIC void
296xlog_header_check_dump(
297 xfs_mount_t *mp,
298 xlog_rec_header_t *head)
299{
Eric Sandeen08e96e12013-10-11 20:59:05 -0500300 xfs_debug(mp, "%s: SB : uuid = %pU, fmt = %d",
Joe Perches03daa572009-12-14 18:01:10 -0800301 __func__, &mp->m_sb.sb_uuid, XLOG_FMT);
Eric Sandeen08e96e12013-10-11 20:59:05 -0500302 xfs_debug(mp, " log : uuid = %pU, fmt = %d",
Joe Perches03daa572009-12-14 18:01:10 -0800303 &head->h_fs_uuid, be32_to_cpu(head->h_fmt));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304}
305#else
306#define xlog_header_check_dump(mp, head)
307#endif
308
309/*
310 * check log record header for recovery
311 */
312STATIC int
313xlog_header_check_recover(
314 xfs_mount_t *mp,
315 xlog_rec_header_t *head)
316{
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200317 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /*
320 * IRIX doesn't write the h_fmt field and leaves it zeroed
321 * (XLOG_FMT_UNKNOWN). This stops us from trying to recover
322 * a dirty log created in IRIX.
323 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200324 if (unlikely(head->h_fmt != cpu_to_be32(XLOG_FMT))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100325 xfs_warn(mp,
326 "dirty log written in incompatible format - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 xlog_header_check_dump(mp, head);
328 XFS_ERROR_REPORT("xlog_header_check_recover(1)",
329 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000330 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100332 xfs_warn(mp,
333 "dirty log entry has mismatched uuid - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 xlog_header_check_dump(mp, head);
335 XFS_ERROR_REPORT("xlog_header_check_recover(2)",
336 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000337 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 return 0;
340}
341
342/*
343 * read the head block of the log and check the header
344 */
345STATIC int
346xlog_header_check_mount(
347 xfs_mount_t *mp,
348 xlog_rec_header_t *head)
349{
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200350 ASSERT(head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 if (uuid_is_nil(&head->h_fs_uuid)) {
353 /*
354 * IRIX doesn't write the h_fs_uuid or h_fmt fields. If
355 * h_fs_uuid is nil, we assume this log was last mounted
356 * by IRIX and continue.
357 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100358 xfs_warn(mp, "nil uuid in log - IRIX style log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 } else if (unlikely(!uuid_equal(&mp->m_sb.sb_uuid, &head->h_fs_uuid))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100360 xfs_warn(mp, "log has mismatched uuid - can't recover");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 xlog_header_check_dump(mp, head);
362 XFS_ERROR_REPORT("xlog_header_check_mount",
363 XFS_ERRLEVEL_HIGH, mp);
Dave Chinner24513372014-06-25 14:58:08 +1000364 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 }
366 return 0;
367}
368
369STATIC void
370xlog_recover_iodone(
371 struct xfs_buf *bp)
372{
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +0000373 if (bp->b_error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 /*
375 * We're not going to bother about retrying
376 * this during recovery. One strike!
377 */
Dave Chinner595bff72014-10-02 09:05:14 +1000378 if (!XFS_FORCED_SHUTDOWN(bp->b_target->bt_mount)) {
379 xfs_buf_ioerror_alert(bp, __func__);
380 xfs_force_shutdown(bp->b_target->bt_mount,
381 SHUTDOWN_META_IO_ERROR);
382 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Christoph Hellwigcb669ca2011-07-13 13:43:49 +0200384 bp->b_iodone = NULL;
Dave Chinnere8aaba92014-10-02 09:04:22 +1000385 xfs_buf_ioend(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386}
387
388/*
389 * This routine finds (to an approximation) the first block in the physical
390 * log which contains the given cycle. It uses a binary search algorithm.
391 * Note that the algorithm can not be perfect because the disk will not
392 * necessarily be perfect.
393 */
David Chinnera8272ce2007-11-23 16:28:09 +1100394STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395xlog_find_cycle_start(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500396 struct xlog *log,
397 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 xfs_daddr_t first_blk,
399 xfs_daddr_t *last_blk,
400 uint cycle)
401{
402 xfs_caddr_t offset;
403 xfs_daddr_t mid_blk;
Alex Eldere3bb2e32010-04-15 18:17:30 +0000404 xfs_daddr_t end_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 uint mid_cycle;
406 int error;
407
Alex Eldere3bb2e32010-04-15 18:17:30 +0000408 end_blk = *last_blk;
409 mid_blk = BLK_AVG(first_blk, end_blk);
410 while (mid_blk != first_blk && mid_blk != end_blk) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100411 error = xlog_bread(log, mid_blk, 1, bp, &offset);
412 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 return error;
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000414 mid_cycle = xlog_get_cycle(offset);
Alex Eldere3bb2e32010-04-15 18:17:30 +0000415 if (mid_cycle == cycle)
416 end_blk = mid_blk; /* last_half_cycle == mid_cycle */
417 else
418 first_blk = mid_blk; /* first_half_cycle == mid_cycle */
419 mid_blk = BLK_AVG(first_blk, end_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Alex Eldere3bb2e32010-04-15 18:17:30 +0000421 ASSERT((mid_blk == first_blk && mid_blk+1 == end_blk) ||
422 (mid_blk == end_blk && mid_blk-1 == first_blk));
423
424 *last_blk = end_blk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 return 0;
427}
428
429/*
Alex Elder3f943d82010-04-15 18:17:34 +0000430 * Check that a range of blocks does not contain stop_on_cycle_no.
431 * Fill in *new_blk with the block offset where such a block is
432 * found, or with -1 (an invalid block number) if there is no such
433 * block in the range. The scan needs to occur from front to back
434 * and the pointer into the region must be updated since a later
435 * routine will need to perform another test.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 */
437STATIC int
438xlog_find_verify_cycle(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500439 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 xfs_daddr_t start_blk,
441 int nbblks,
442 uint stop_on_cycle_no,
443 xfs_daddr_t *new_blk)
444{
445 xfs_daddr_t i, j;
446 uint cycle;
447 xfs_buf_t *bp;
448 xfs_daddr_t bufblks;
449 xfs_caddr_t buf = NULL;
450 int error = 0;
451
Alex Elder6881a222010-04-13 15:22:29 +1000452 /*
453 * Greedily allocate a buffer big enough to handle the full
454 * range of basic blocks we'll be examining. If that fails,
455 * try a smaller size. We need to be able to read at least
456 * a log sector, or we're out of luck.
457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 bufblks = 1 << ffs(nbblks);
Dave Chinner81158e02012-04-27 19:45:22 +1000459 while (bufblks > log->l_logBBsize)
460 bufblks >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 while (!(bp = xlog_get_bp(log, bufblks))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 bufblks >>= 1;
Alex Elder69ce58f2010-04-20 17:09:59 +1000463 if (bufblks < log->l_sectBBsize)
Dave Chinner24513372014-06-25 14:58:08 +1000464 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 for (i = start_blk; i < start_blk + nbblks; i += bufblks) {
468 int bcount;
469
470 bcount = min(bufblks, (start_blk + nbblks - i));
471
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100472 error = xlog_bread(log, i, bcount, bp, &buf);
473 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 goto out;
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 for (j = 0; j < bcount; j++) {
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000477 cycle = xlog_get_cycle(buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 if (cycle == stop_on_cycle_no) {
479 *new_blk = i+j;
480 goto out;
481 }
482
483 buf += BBSIZE;
484 }
485 }
486
487 *new_blk = -1;
488
489out:
490 xlog_put_bp(bp);
491 return error;
492}
493
494/*
495 * Potentially backup over partial log record write.
496 *
497 * In the typical case, last_blk is the number of the block directly after
498 * a good log record. Therefore, we subtract one to get the block number
499 * of the last block in the given buffer. extra_bblks contains the number
500 * of blocks we would have read on a previous read. This happens when the
501 * last log record is split over the end of the physical log.
502 *
503 * extra_bblks is the number of blocks potentially verified on a previous
504 * call to this routine.
505 */
506STATIC int
507xlog_find_verify_log_record(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500508 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 xfs_daddr_t start_blk,
510 xfs_daddr_t *last_blk,
511 int extra_bblks)
512{
513 xfs_daddr_t i;
514 xfs_buf_t *bp;
515 xfs_caddr_t offset = NULL;
516 xlog_rec_header_t *head = NULL;
517 int error = 0;
518 int smallmem = 0;
519 int num_blks = *last_blk - start_blk;
520 int xhdrs;
521
522 ASSERT(start_blk != 0 || *last_blk != start_blk);
523
524 if (!(bp = xlog_get_bp(log, num_blks))) {
525 if (!(bp = xlog_get_bp(log, 1)))
Dave Chinner24513372014-06-25 14:58:08 +1000526 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 smallmem = 1;
528 } else {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100529 error = xlog_bread(log, start_blk, num_blks, bp, &offset);
530 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 offset += ((num_blks - 1) << BBSHIFT);
533 }
534
535 for (i = (*last_blk) - 1; i >= 0; i--) {
536 if (i < start_blk) {
537 /* valid log record not found */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100538 xfs_warn(log->l_mp,
539 "Log inconsistent (didn't find previous header)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000541 error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 goto out;
543 }
544
545 if (smallmem) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100546 error = xlog_bread(log, i, 1, bp, &offset);
547 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 }
550
551 head = (xlog_rec_header_t *)offset;
552
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200553 if (head->h_magicno == cpu_to_be32(XLOG_HEADER_MAGIC_NUM))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555
556 if (!smallmem)
557 offset -= BBSIZE;
558 }
559
560 /*
561 * We hit the beginning of the physical log & still no header. Return
562 * to caller. If caller can handle a return of -1, then this routine
563 * will be called again for the end of the physical log.
564 */
565 if (i == -1) {
Dave Chinner24513372014-06-25 14:58:08 +1000566 error = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 goto out;
568 }
569
570 /*
571 * We have the final block of the good log (the first block
572 * of the log record _before_ the head. So we check the uuid.
573 */
574 if ((error = xlog_header_check_mount(log->l_mp, head)))
575 goto out;
576
577 /*
578 * We may have found a log record header before we expected one.
579 * last_blk will be the 1st block # with a given cycle #. We may end
580 * up reading an entire log record. In this case, we don't want to
581 * reset last_blk. Only when last_blk points in the middle of a log
582 * record do we update last_blk.
583 */
Eric Sandeen62118702008-03-06 13:44:28 +1100584 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000585 uint h_size = be32_to_cpu(head->h_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 xhdrs = h_size / XLOG_HEADER_CYCLE_SIZE;
588 if (h_size % XLOG_HEADER_CYCLE_SIZE)
589 xhdrs++;
590 } else {
591 xhdrs = 1;
592 }
593
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000594 if (*last_blk - i + extra_bblks !=
595 BTOBB(be32_to_cpu(head->h_len)) + xhdrs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 *last_blk = i;
597
598out:
599 xlog_put_bp(bp);
600 return error;
601}
602
603/*
604 * Head is defined to be the point of the log where the next log write
Zhi Yong Wu0a94da22013-08-07 10:11:08 +0000605 * could go. This means that incomplete LR writes at the end are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 * eliminated when calculating the head. We aren't guaranteed that previous
607 * LR have complete transactions. We only know that a cycle number of
608 * current cycle number -1 won't be present in the log if we start writing
609 * from our current block number.
610 *
611 * last_blk contains the block number of the first block with a given
612 * cycle number.
613 *
614 * Return: zero if normal, non-zero if error.
615 */
Christoph Hellwigba0f32d2005-06-21 15:36:52 +1000616STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617xlog_find_head(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500618 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 xfs_daddr_t *return_head_blk)
620{
621 xfs_buf_t *bp;
622 xfs_caddr_t offset;
623 xfs_daddr_t new_blk, first_blk, start_blk, last_blk, head_blk;
624 int num_scan_bblks;
625 uint first_half_cycle, last_half_cycle;
626 uint stop_on_cycle;
627 int error, log_bbnum = log->l_logBBsize;
628
629 /* Is the end of the log device zeroed? */
Dave Chinner24513372014-06-25 14:58:08 +1000630 error = xlog_find_zeroed(log, &first_blk);
631 if (error < 0) {
632 xfs_warn(log->l_mp, "empty log check failed");
633 return error;
634 }
635 if (error == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *return_head_blk = first_blk;
637
638 /* Is the whole lot zeroed? */
639 if (!first_blk) {
640 /* Linux XFS shouldn't generate totally zeroed logs -
641 * mkfs etc write a dummy unmount record to a fresh
642 * log so we can store the uuid in there
643 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100644 xfs_warn(log->l_mp, "totally zeroed log");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
650 first_blk = 0; /* get cycle # of 1st block */
651 bp = xlog_get_bp(log, 1);
652 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000653 return -ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100654
655 error = xlog_bread(log, 0, 1, bp, &offset);
656 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100658
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000659 first_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
661 last_blk = head_blk = log_bbnum - 1; /* get cycle # of last block */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100662 error = xlog_bread(log, last_blk, 1, bp, &offset);
663 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100665
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000666 last_half_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 ASSERT(last_half_cycle != 0);
668
669 /*
670 * If the 1st half cycle number is equal to the last half cycle number,
671 * then the entire log is stamped with the same cycle number. In this
672 * case, head_blk can't be set to zero (which makes sense). The below
673 * math doesn't work out properly with head_blk equal to zero. Instead,
674 * we set it to log_bbnum which is an invalid block number, but this
675 * value makes the math correct. If head_blk doesn't changed through
676 * all the tests below, *head_blk is set to zero at the very end rather
677 * than log_bbnum. In a sense, log_bbnum and zero are the same block
678 * in a circular file.
679 */
680 if (first_half_cycle == last_half_cycle) {
681 /*
682 * In this case we believe that the entire log should have
683 * cycle number last_half_cycle. We need to scan backwards
684 * from the end verifying that there are no holes still
685 * containing last_half_cycle - 1. If we find such a hole,
686 * then the start of that hole will be the new head. The
687 * simple case looks like
688 * x | x ... | x - 1 | x
689 * Another case that fits this picture would be
690 * x | x + 1 | x ... | x
Nathan Scottc41564b2006-03-29 08:55:14 +1000691 * In this case the head really is somewhere at the end of the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 * log, as one of the latest writes at the beginning was
693 * incomplete.
694 * One more case is
695 * x | x + 1 | x ... | x - 1 | x
696 * This is really the combination of the above two cases, and
697 * the head has to end up at the start of the x-1 hole at the
698 * end of the log.
699 *
700 * In the 256k log case, we will read from the beginning to the
701 * end of the log and search for cycle numbers equal to x-1.
702 * We don't worry about the x+1 blocks that we encounter,
703 * because we know that they cannot be the head since the log
704 * started with x.
705 */
706 head_blk = log_bbnum;
707 stop_on_cycle = last_half_cycle - 1;
708 } else {
709 /*
710 * In this case we want to find the first block with cycle
711 * number matching last_half_cycle. We expect the log to be
712 * some variation on
Alex Elder3f943d82010-04-15 18:17:34 +0000713 * x + 1 ... | x ... | x
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 * The first block with cycle number x (last_half_cycle) will
715 * be where the new head belongs. First we do a binary search
716 * for the first occurrence of last_half_cycle. The binary
717 * search may not be totally accurate, so then we scan back
718 * from there looking for occurrences of last_half_cycle before
719 * us. If that backwards scan wraps around the beginning of
720 * the log, then we look for occurrences of last_half_cycle - 1
721 * at the end of the log. The cases we're looking for look
722 * like
Alex Elder3f943d82010-04-15 18:17:34 +0000723 * v binary search stopped here
724 * x + 1 ... | x | x + 1 | x ... | x
725 * ^ but we want to locate this spot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * or
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * <---------> less than scan distance
Alex Elder3f943d82010-04-15 18:17:34 +0000728 * x + 1 ... | x ... | x - 1 | x
729 * ^ we want to locate this spot
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 */
731 stop_on_cycle = last_half_cycle;
732 if ((error = xlog_find_cycle_start(log, bp, first_blk,
733 &head_blk, last_half_cycle)))
734 goto bp_err;
735 }
736
737 /*
738 * Now validate the answer. Scan back some number of maximum possible
739 * blocks and make sure each one has the expected cycle number. The
740 * maximum is determined by the total possible amount of buffering
741 * in the in-core log. The following number can be made tighter if
742 * we actually look at the block size of the filesystem.
743 */
744 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
745 if (head_blk >= num_scan_bblks) {
746 /*
747 * We are guaranteed that the entire check can be performed
748 * in one buffer.
749 */
750 start_blk = head_blk - num_scan_bblks;
751 if ((error = xlog_find_verify_cycle(log,
752 start_blk, num_scan_bblks,
753 stop_on_cycle, &new_blk)))
754 goto bp_err;
755 if (new_blk != -1)
756 head_blk = new_blk;
757 } else { /* need to read 2 parts of log */
758 /*
759 * We are going to scan backwards in the log in two parts.
760 * First we scan the physical end of the log. In this part
761 * of the log, we are looking for blocks with cycle number
762 * last_half_cycle - 1.
763 * If we find one, then we know that the log starts there, as
764 * we've found a hole that didn't get written in going around
765 * the end of the physical log. The simple case for this is
766 * x + 1 ... | x ... | x - 1 | x
767 * <---------> less than scan distance
768 * If all of the blocks at the end of the log have cycle number
769 * last_half_cycle, then we check the blocks at the start of
770 * the log looking for occurrences of last_half_cycle. If we
771 * find one, then our current estimate for the location of the
772 * first occurrence of last_half_cycle is wrong and we move
773 * back to the hole we've found. This case looks like
774 * x + 1 ... | x | x + 1 | x ...
775 * ^ binary search stopped here
776 * Another case we need to handle that only occurs in 256k
777 * logs is
778 * x + 1 ... | x ... | x+1 | x ...
779 * ^ binary search stops here
780 * In a 256k log, the scan at the end of the log will see the
781 * x + 1 blocks. We need to skip past those since that is
782 * certainly not the head of the log. By searching for
783 * last_half_cycle-1 we accomplish that.
784 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 ASSERT(head_blk <= INT_MAX &&
Alex Elder3f943d82010-04-15 18:17:34 +0000786 (xfs_daddr_t) num_scan_bblks >= head_blk);
787 start_blk = log_bbnum - (num_scan_bblks - head_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 if ((error = xlog_find_verify_cycle(log, start_blk,
789 num_scan_bblks - (int)head_blk,
790 (stop_on_cycle - 1), &new_blk)))
791 goto bp_err;
792 if (new_blk != -1) {
793 head_blk = new_blk;
Alex Elder9db127e2010-04-15 18:17:26 +0000794 goto validate_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796
797 /*
798 * Scan beginning of log now. The last part of the physical
799 * log is good. This scan needs to verify that it doesn't find
800 * the last_half_cycle.
801 */
802 start_blk = 0;
803 ASSERT(head_blk <= INT_MAX);
804 if ((error = xlog_find_verify_cycle(log,
805 start_blk, (int)head_blk,
806 stop_on_cycle, &new_blk)))
807 goto bp_err;
808 if (new_blk != -1)
809 head_blk = new_blk;
810 }
811
Alex Elder9db127e2010-04-15 18:17:26 +0000812validate_head:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 /*
814 * Now we need to make sure head_blk is not pointing to a block in
815 * the middle of a log record.
816 */
817 num_scan_bblks = XLOG_REC_SHIFT(log);
818 if (head_blk >= num_scan_bblks) {
819 start_blk = head_blk - num_scan_bblks; /* don't read head_blk */
820
821 /* start ptr at last block ptr before head_blk */
Dave Chinner24513372014-06-25 14:58:08 +1000822 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
823 if (error == 1)
824 error = -EIO;
825 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto bp_err;
827 } else {
828 start_blk = 0;
829 ASSERT(head_blk <= INT_MAX);
Dave Chinner24513372014-06-25 14:58:08 +1000830 error = xlog_find_verify_log_record(log, start_blk, &head_blk, 0);
831 if (error < 0)
832 goto bp_err;
833 if (error == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* We hit the beginning of the log during our search */
Alex Elder3f943d82010-04-15 18:17:34 +0000835 start_blk = log_bbnum - (num_scan_bblks - head_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 new_blk = log_bbnum;
837 ASSERT(start_blk <= INT_MAX &&
838 (xfs_daddr_t) log_bbnum-start_blk >= 0);
839 ASSERT(head_blk <= INT_MAX);
Dave Chinner24513372014-06-25 14:58:08 +1000840 error = xlog_find_verify_log_record(log, start_blk,
841 &new_blk, (int)head_blk);
842 if (error == 1)
843 error = -EIO;
844 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto bp_err;
846 if (new_blk != log_bbnum)
847 head_blk = new_blk;
848 } else if (error)
849 goto bp_err;
850 }
851
852 xlog_put_bp(bp);
853 if (head_blk == log_bbnum)
854 *return_head_blk = 0;
855 else
856 *return_head_blk = head_blk;
857 /*
858 * When returning here, we have a good block number. Bad block
859 * means that during a previous crash, we didn't have a clean break
860 * from cycle number N to cycle number N-1. In this case, we need
861 * to find the first block with cycle number N-1.
862 */
863 return 0;
864
865 bp_err:
866 xlog_put_bp(bp);
867
868 if (error)
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100869 xfs_warn(log->l_mp, "failed to find log head");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 return error;
871}
872
873/*
874 * Find the sync block number or the tail of the log.
875 *
876 * This will be the block number of the last record to have its
877 * associated buffers synced to disk. Every log record header has
878 * a sync lsn embedded in it. LSNs hold block numbers, so it is easy
879 * to get a sync block number. The only concern is to figure out which
880 * log record header to believe.
881 *
882 * The following algorithm uses the log record header with the largest
883 * lsn. The entire log record does not need to be valid. We only care
884 * that the header is valid.
885 *
886 * We could speed up search by using current head_blk buffer, but it is not
887 * available.
888 */
Eric Sandeen5d77c0d2009-11-19 15:52:00 +0000889STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890xlog_find_tail(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -0500891 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 xfs_daddr_t *head_blk,
Eric Sandeen65be6052006-01-11 15:34:19 +1100893 xfs_daddr_t *tail_blk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 xlog_rec_header_t *rhead;
896 xlog_op_header_t *op_head;
897 xfs_caddr_t offset = NULL;
898 xfs_buf_t *bp;
899 int error, i, found;
900 xfs_daddr_t umount_data_blk;
901 xfs_daddr_t after_umount_blk;
902 xfs_lsn_t tail_lsn;
903 int hblks;
904
905 found = 0;
906
907 /*
908 * Find previous log record
909 */
910 if ((error = xlog_find_head(log, head_blk)))
911 return error;
912
913 bp = xlog_get_bp(log, 1);
914 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +1000915 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (*head_blk == 0) { /* special case */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100917 error = xlog_bread(log, 0, 1, bp, &offset);
918 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +0000919 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100920
Christoph Hellwig03bea6f2007-10-12 10:58:05 +1000921 if (xlog_get_cycle(offset) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 *tail_blk = 0;
923 /* leave all other log inited values alone */
Alex Elder9db127e2010-04-15 18:17:26 +0000924 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 }
926 }
927
928 /*
929 * Search backwards looking for log record header block
930 */
931 ASSERT(*head_blk < INT_MAX);
932 for (i = (int)(*head_blk) - 1; i >= 0; i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100933 error = xlog_bread(log, i, 1, bp, &offset);
934 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +0000935 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100936
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200937 if (*(__be32 *)offset == cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 found = 1;
939 break;
940 }
941 }
942 /*
943 * If we haven't found the log record header block, start looking
944 * again from the end of the physical log. XXXmiken: There should be
945 * a check here to make sure we didn't search more than N blocks in
946 * the previous code.
947 */
948 if (!found) {
949 for (i = log->l_logBBsize - 1; i >= (int)(*head_blk); i--) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100950 error = xlog_bread(log, i, 1, bp, &offset);
951 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +0000952 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +0100953
Christoph Hellwig69ef9212011-07-08 14:36:05 +0200954 if (*(__be32 *)offset ==
955 cpu_to_be32(XLOG_HEADER_MAGIC_NUM)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 found = 2;
957 break;
958 }
959 }
960 }
961 if (!found) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +1100962 xfs_warn(log->l_mp, "%s: couldn't find sync record", __func__);
Eric Sandeen050a1952013-07-31 20:33:47 -0500963 xlog_put_bp(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +1000965 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 }
967
968 /* find blk_no of tail of log */
969 rhead = (xlog_rec_header_t *)offset;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000970 *tail_blk = BLOCK_LSN(be64_to_cpu(rhead->h_tail_lsn));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 /*
973 * Reset log values according to the state of the log when we
974 * crashed. In the case where head_blk == 0, we bump curr_cycle
975 * one because the next write starts a new cycle rather than
976 * continuing the cycle of the last good log record. At this
977 * point we have guaranteed that all partial log records have been
978 * accounted for. Therefore, we know that the last good log record
979 * written was complete and ended exactly on the end boundary
980 * of the physical log.
981 */
982 log->l_prev_block = i;
983 log->l_curr_block = (int)*head_blk;
Christoph Hellwigb53e6752007-10-12 10:59:34 +1000984 log->l_curr_cycle = be32_to_cpu(rhead->h_cycle);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 if (found == 2)
986 log->l_curr_cycle++;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +1100987 atomic64_set(&log->l_tail_lsn, be64_to_cpu(rhead->h_tail_lsn));
Dave Chinner84f3c682010-12-03 22:11:29 +1100988 atomic64_set(&log->l_last_sync_lsn, be64_to_cpu(rhead->h_lsn));
Christoph Hellwig28496962012-02-20 02:31:25 +0000989 xlog_assign_grant_head(&log->l_reserve_head.grant, log->l_curr_cycle,
Dave Chinnera69ed032010-12-21 12:08:20 +1100990 BBTOB(log->l_curr_block));
Christoph Hellwig28496962012-02-20 02:31:25 +0000991 xlog_assign_grant_head(&log->l_write_head.grant, log->l_curr_cycle,
Dave Chinnera69ed032010-12-21 12:08:20 +1100992 BBTOB(log->l_curr_block));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
994 /*
995 * Look for unmount record. If we find it, then we know there
996 * was a clean unmount. Since 'i' could be the last block in
997 * the physical log, we convert to a log block before comparing
998 * to the head_blk.
999 *
1000 * Save the current tail lsn to use to pass to
1001 * xlog_clear_stale_blocks() below. We won't want to clear the
1002 * unmount record if there is one, so we pass the lsn of the
1003 * unmount record rather than the block after it.
1004 */
Eric Sandeen62118702008-03-06 13:44:28 +11001005 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001006 int h_size = be32_to_cpu(rhead->h_size);
1007 int h_version = be32_to_cpu(rhead->h_version);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
1009 if ((h_version & XLOG_VERSION_2) &&
1010 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
1011 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
1012 if (h_size % XLOG_HEADER_CYCLE_SIZE)
1013 hblks++;
1014 } else {
1015 hblks = 1;
1016 }
1017 } else {
1018 hblks = 1;
1019 }
1020 after_umount_blk = (i + hblks + (int)
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001021 BTOBB(be32_to_cpu(rhead->h_len))) % log->l_logBBsize;
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001022 tail_lsn = atomic64_read(&log->l_tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 if (*head_blk == after_umount_blk &&
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001024 be32_to_cpu(rhead->h_num_logops) == 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 umount_data_blk = (i + hblks) % log->l_logBBsize;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001026 error = xlog_bread(log, umount_data_blk, 1, bp, &offset);
1027 if (error)
Alex Elder9db127e2010-04-15 18:17:26 +00001028 goto done;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 op_head = (xlog_op_header_t *)offset;
1031 if (op_head->oh_flags & XLOG_UNMOUNT_TRANS) {
1032 /*
1033 * Set tail and last sync so that newly written
1034 * log records will point recovery to after the
1035 * current unmount record.
1036 */
Dave Chinner1c3cb9e2010-12-21 12:28:39 +11001037 xlog_assign_atomic_lsn(&log->l_tail_lsn,
1038 log->l_curr_cycle, after_umount_blk);
1039 xlog_assign_atomic_lsn(&log->l_last_sync_lsn,
1040 log->l_curr_cycle, after_umount_blk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 *tail_blk = after_umount_blk;
David Chinner92821e22007-05-24 15:26:31 +10001042
1043 /*
1044 * Note that the unmount was clean. If the unmount
1045 * was not clean, we need to know this to rebuild the
1046 * superblock counters from the perag headers if we
1047 * have a filesystem using non-persistent counters.
1048 */
1049 log->l_mp->m_flags |= XFS_MOUNT_WAS_CLEAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 }
1051 }
1052
1053 /*
1054 * Make sure that there are no blocks in front of the head
1055 * with the same cycle number as the head. This can happen
1056 * because we allow multiple outstanding log writes concurrently,
1057 * and the later writes might make it out before earlier ones.
1058 *
1059 * We use the lsn from before modifying it so that we'll never
1060 * overwrite the unmount record after a clean unmount.
1061 *
1062 * Do this only if we are going to recover the filesystem
1063 *
1064 * NOTE: This used to say "if (!readonly)"
1065 * However on Linux, we can & do recover a read-only filesystem.
1066 * We only skip recovery if NORECOVERY is specified on mount,
1067 * in which case we would not be here.
1068 *
1069 * But... if the -device- itself is readonly, just skip this.
1070 * We can't recover this device anyway, so it won't matter.
1071 */
Alex Elder9db127e2010-04-15 18:17:26 +00001072 if (!xfs_readonly_buftarg(log->l_mp->m_logdev_targp))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 error = xlog_clear_stale_blocks(log, tail_lsn);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074
Alex Elder9db127e2010-04-15 18:17:26 +00001075done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 xlog_put_bp(bp);
1077
1078 if (error)
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001079 xfs_warn(log->l_mp, "failed to locate log tail");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 return error;
1081}
1082
1083/*
1084 * Is the log zeroed at all?
1085 *
1086 * The last binary search should be changed to perform an X block read
1087 * once X becomes small enough. You can then search linearly through
1088 * the X blocks. This will cut down on the number of reads we need to do.
1089 *
1090 * If the log is partially zeroed, this routine will pass back the blkno
1091 * of the first block with cycle number 0. It won't have a complete LR
1092 * preceding it.
1093 *
1094 * Return:
1095 * 0 => the log is completely written to
Dave Chinner24513372014-06-25 14:58:08 +10001096 * 1 => use *blk_no as the first block of the log
1097 * <0 => error has occurred
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 */
David Chinnera8272ce2007-11-23 16:28:09 +11001099STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100xlog_find_zeroed(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001101 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 xfs_daddr_t *blk_no)
1103{
1104 xfs_buf_t *bp;
1105 xfs_caddr_t offset;
1106 uint first_cycle, last_cycle;
1107 xfs_daddr_t new_blk, last_blk, start_blk;
1108 xfs_daddr_t num_scan_bblks;
1109 int error, log_bbnum = log->l_logBBsize;
1110
Nathan Scott6fdf8cc2006-06-28 10:13:52 +10001111 *blk_no = 0;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 /* check totally zeroed log */
1114 bp = xlog_get_bp(log, 1);
1115 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +10001116 return -ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001117 error = xlog_bread(log, 0, 1, bp, &offset);
1118 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001120
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001121 first_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (first_cycle == 0) { /* completely zeroed log */
1123 *blk_no = 0;
1124 xlog_put_bp(bp);
Dave Chinner24513372014-06-25 14:58:08 +10001125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 }
1127
1128 /* check partially zeroed log */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001129 error = xlog_bread(log, log_bbnum-1, 1, bp, &offset);
1130 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 goto bp_err;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001132
Christoph Hellwig03bea6f2007-10-12 10:58:05 +10001133 last_cycle = xlog_get_cycle(offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 if (last_cycle != 0) { /* log completely written to */
1135 xlog_put_bp(bp);
1136 return 0;
1137 } else if (first_cycle != 1) {
1138 /*
1139 * If the cycle of the last block is zero, the cycle of
1140 * the first block must be 1. If it's not, maybe we're
1141 * not looking at a log... Bail out.
1142 */
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001143 xfs_warn(log->l_mp,
1144 "Log inconsistent or not a log (last==0, first!=1)");
Dave Chinner24513372014-06-25 14:58:08 +10001145 error = -EINVAL;
Eric Sandeen5d0a6542013-07-31 20:32:30 -05001146 goto bp_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
1149 /* we have a partially zeroed log */
1150 last_blk = log_bbnum-1;
1151 if ((error = xlog_find_cycle_start(log, bp, 0, &last_blk, 0)))
1152 goto bp_err;
1153
1154 /*
1155 * Validate the answer. Because there is no way to guarantee that
1156 * the entire log is made up of log records which are the same size,
1157 * we scan over the defined maximum blocks. At this point, the maximum
1158 * is not chosen to mean anything special. XXXmiken
1159 */
1160 num_scan_bblks = XLOG_TOTAL_REC_SHIFT(log);
1161 ASSERT(num_scan_bblks <= INT_MAX);
1162
1163 if (last_blk < num_scan_bblks)
1164 num_scan_bblks = last_blk;
1165 start_blk = last_blk - num_scan_bblks;
1166
1167 /*
1168 * We search for any instances of cycle number 0 that occur before
1169 * our current estimate of the head. What we're trying to detect is
1170 * 1 ... | 0 | 1 | 0...
1171 * ^ binary search ends here
1172 */
1173 if ((error = xlog_find_verify_cycle(log, start_blk,
1174 (int)num_scan_bblks, 0, &new_blk)))
1175 goto bp_err;
1176 if (new_blk != -1)
1177 last_blk = new_blk;
1178
1179 /*
1180 * Potentially backup over partial log record write. We don't need
1181 * to search the end of the log because we know it is zero.
1182 */
Dave Chinner24513372014-06-25 14:58:08 +10001183 error = xlog_find_verify_log_record(log, start_blk, &last_blk, 0);
1184 if (error == 1)
1185 error = -EIO;
1186 if (error)
1187 goto bp_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 *blk_no = last_blk;
1190bp_err:
1191 xlog_put_bp(bp);
1192 if (error)
1193 return error;
Dave Chinner24513372014-06-25 14:58:08 +10001194 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195}
1196
1197/*
1198 * These are simple subroutines used by xlog_clear_stale_blocks() below
1199 * to initialize a buffer full of empty log record headers and write
1200 * them into the log.
1201 */
1202STATIC void
1203xlog_add_record(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001204 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 xfs_caddr_t buf,
1206 int cycle,
1207 int block,
1208 int tail_cycle,
1209 int tail_block)
1210{
1211 xlog_rec_header_t *recp = (xlog_rec_header_t *)buf;
1212
1213 memset(buf, 0, BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001214 recp->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM);
1215 recp->h_cycle = cpu_to_be32(cycle);
1216 recp->h_version = cpu_to_be32(
Eric Sandeen62118702008-03-06 13:44:28 +11001217 xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10001218 recp->h_lsn = cpu_to_be64(xlog_assign_lsn(cycle, block));
1219 recp->h_tail_lsn = cpu_to_be64(xlog_assign_lsn(tail_cycle, tail_block));
1220 recp->h_fmt = cpu_to_be32(XLOG_FMT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 memcpy(&recp->h_fs_uuid, &log->l_mp->m_sb.sb_uuid, sizeof(uuid_t));
1222}
1223
1224STATIC int
1225xlog_write_log_records(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001226 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 int cycle,
1228 int start_block,
1229 int blocks,
1230 int tail_cycle,
1231 int tail_block)
1232{
1233 xfs_caddr_t offset;
1234 xfs_buf_t *bp;
1235 int balign, ealign;
Alex Elder69ce58f2010-04-20 17:09:59 +10001236 int sectbb = log->l_sectBBsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 int end_block = start_block + blocks;
1238 int bufblks;
1239 int error = 0;
1240 int i, j = 0;
1241
Alex Elder6881a222010-04-13 15:22:29 +10001242 /*
1243 * Greedily allocate a buffer big enough to handle the full
1244 * range of basic blocks to be written. If that fails, try
1245 * a smaller size. We need to be able to write at least a
1246 * log sector, or we're out of luck.
1247 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 bufblks = 1 << ffs(blocks);
Dave Chinner81158e02012-04-27 19:45:22 +10001249 while (bufblks > log->l_logBBsize)
1250 bufblks >>= 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 while (!(bp = xlog_get_bp(log, bufblks))) {
1252 bufblks >>= 1;
Alex Elder69ce58f2010-04-20 17:09:59 +10001253 if (bufblks < sectbb)
Dave Chinner24513372014-06-25 14:58:08 +10001254 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
1256
1257 /* We may need to do a read at the start to fill in part of
1258 * the buffer in the starting sector not covered by the first
1259 * write below.
1260 */
Alex Elder5c17f532010-04-13 15:22:48 +10001261 balign = round_down(start_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (balign != start_block) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001263 error = xlog_bread_noalign(log, start_block, 1, bp);
1264 if (error)
1265 goto out_put_bp;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 j = start_block - balign;
1268 }
1269
1270 for (i = start_block; i < end_block; i += bufblks) {
1271 int bcount, endcount;
1272
1273 bcount = min(bufblks, end_block - start_block);
1274 endcount = bcount - j;
1275
1276 /* We may need to do a read at the end to fill in part of
1277 * the buffer in the final sector not covered by the write.
1278 * If this is the same sector as the above read, skip it.
1279 */
Alex Elder5c17f532010-04-13 15:22:48 +10001280 ealign = round_down(end_block, sectbb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 if (j == 0 && (start_block + endcount > ealign)) {
Chandra Seetharaman62926042011-07-22 23:40:15 +00001282 offset = bp->b_addr + BBTOB(ealign - start_block);
Dave Chinner44396472011-04-21 09:34:27 +00001283 error = xlog_bread_offset(log, ealign, sectbb,
1284 bp, offset);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001285 if (error)
1286 break;
1287
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
1290 offset = xlog_align(log, start_block, endcount, bp);
1291 for (; j < endcount; j++) {
1292 xlog_add_record(log, offset, cycle, i+j,
1293 tail_cycle, tail_block);
1294 offset += BBSIZE;
1295 }
1296 error = xlog_bwrite(log, start_block, endcount, bp);
1297 if (error)
1298 break;
1299 start_block += endcount;
1300 j = 0;
1301 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01001302
1303 out_put_bp:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 xlog_put_bp(bp);
1305 return error;
1306}
1307
1308/*
1309 * This routine is called to blow away any incomplete log writes out
1310 * in front of the log head. We do this so that we won't become confused
1311 * if we come up, write only a little bit more, and then crash again.
1312 * If we leave the partial log records out there, this situation could
1313 * cause us to think those partial writes are valid blocks since they
1314 * have the current cycle number. We get rid of them by overwriting them
1315 * with empty log records with the old cycle number rather than the
1316 * current one.
1317 *
1318 * The tail lsn is passed in rather than taken from
1319 * the log so that we will not write over the unmount record after a
1320 * clean unmount in a 512 block log. Doing so would leave the log without
1321 * any valid log records in it until a new one was written. If we crashed
1322 * during that time we would not be able to recover.
1323 */
1324STATIC int
1325xlog_clear_stale_blocks(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05001326 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 xfs_lsn_t tail_lsn)
1328{
1329 int tail_cycle, head_cycle;
1330 int tail_block, head_block;
1331 int tail_distance, max_distance;
1332 int distance;
1333 int error;
1334
1335 tail_cycle = CYCLE_LSN(tail_lsn);
1336 tail_block = BLOCK_LSN(tail_lsn);
1337 head_cycle = log->l_curr_cycle;
1338 head_block = log->l_curr_block;
1339
1340 /*
1341 * Figure out the distance between the new head of the log
1342 * and the tail. We want to write over any blocks beyond the
1343 * head that we may have written just before the crash, but
1344 * we don't want to overwrite the tail of the log.
1345 */
1346 if (head_cycle == tail_cycle) {
1347 /*
1348 * The tail is behind the head in the physical log,
1349 * so the distance from the head to the tail is the
1350 * distance from the head to the end of the log plus
1351 * the distance from the beginning of the log to the
1352 * tail.
1353 */
1354 if (unlikely(head_block < tail_block || head_block >= log->l_logBBsize)) {
1355 XFS_ERROR_REPORT("xlog_clear_stale_blocks(1)",
1356 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10001357 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 }
1359 tail_distance = tail_block + (log->l_logBBsize - head_block);
1360 } else {
1361 /*
1362 * The head is behind the tail in the physical log,
1363 * so the distance from the head to the tail is just
1364 * the tail block minus the head block.
1365 */
1366 if (unlikely(head_block >= tail_block || head_cycle != (tail_cycle + 1))){
1367 XFS_ERROR_REPORT("xlog_clear_stale_blocks(2)",
1368 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10001369 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 tail_distance = tail_block - head_block;
1372 }
1373
1374 /*
1375 * If the head is right up against the tail, we can't clear
1376 * anything.
1377 */
1378 if (tail_distance <= 0) {
1379 ASSERT(tail_distance == 0);
1380 return 0;
1381 }
1382
1383 max_distance = XLOG_TOTAL_REC_SHIFT(log);
1384 /*
1385 * Take the smaller of the maximum amount of outstanding I/O
1386 * we could have and the distance to the tail to clear out.
1387 * We take the smaller so that we don't overwrite the tail and
1388 * we don't waste all day writing from the head to the tail
1389 * for no reason.
1390 */
1391 max_distance = MIN(max_distance, tail_distance);
1392
1393 if ((head_block + max_distance) <= log->l_logBBsize) {
1394 /*
1395 * We can stomp all the blocks we need to without
1396 * wrapping around the end of the log. Just do it
1397 * in a single write. Use the cycle number of the
1398 * current cycle minus one so that the log will look like:
1399 * n ... | n - 1 ...
1400 */
1401 error = xlog_write_log_records(log, (head_cycle - 1),
1402 head_block, max_distance, tail_cycle,
1403 tail_block);
1404 if (error)
1405 return error;
1406 } else {
1407 /*
1408 * We need to wrap around the end of the physical log in
1409 * order to clear all the blocks. Do it in two separate
1410 * I/Os. The first write should be from the head to the
1411 * end of the physical log, and it should use the current
1412 * cycle number minus one just like above.
1413 */
1414 distance = log->l_logBBsize - head_block;
1415 error = xlog_write_log_records(log, (head_cycle - 1),
1416 head_block, distance, tail_cycle,
1417 tail_block);
1418
1419 if (error)
1420 return error;
1421
1422 /*
1423 * Now write the blocks at the start of the physical log.
1424 * This writes the remainder of the blocks we want to clear.
1425 * It uses the current cycle number since we're now on the
1426 * same cycle as the head so that we get:
1427 * n ... n ... | n - 1 ...
1428 * ^^^^^ blocks we're writing
1429 */
1430 distance = max_distance - (log->l_logBBsize - head_block);
1431 error = xlog_write_log_records(log, head_cycle, 0, distance,
1432 tail_cycle, tail_block);
1433 if (error)
1434 return error;
1435 }
1436
1437 return 0;
1438}
1439
1440/******************************************************************************
1441 *
1442 * Log recover routines
1443 *
1444 ******************************************************************************
1445 */
1446
Dave Chinnerf0a76952010-01-11 11:49:57 +00001447/*
Dave Chinnera775ad72013-06-05 12:09:07 +10001448 * Sort the log items in the transaction.
1449 *
1450 * The ordering constraints are defined by the inode allocation and unlink
1451 * behaviour. The rules are:
1452 *
1453 * 1. Every item is only logged once in a given transaction. Hence it
1454 * represents the last logged state of the item. Hence ordering is
1455 * dependent on the order in which operations need to be performed so
1456 * required initial conditions are always met.
1457 *
1458 * 2. Cancelled buffers are recorded in pass 1 in a separate table and
1459 * there's nothing to replay from them so we can simply cull them
1460 * from the transaction. However, we can't do that until after we've
1461 * replayed all the other items because they may be dependent on the
1462 * cancelled buffer and replaying the cancelled buffer can remove it
1463 * form the cancelled buffer table. Hence they have tobe done last.
1464 *
1465 * 3. Inode allocation buffers must be replayed before inode items that
Dave Chinner28c8e412013-06-27 16:04:55 +10001466 * read the buffer and replay changes into it. For filesystems using the
1467 * ICREATE transactions, this means XFS_LI_ICREATE objects need to get
1468 * treated the same as inode allocation buffers as they create and
1469 * initialise the buffers directly.
Dave Chinnera775ad72013-06-05 12:09:07 +10001470 *
1471 * 4. Inode unlink buffers must be replayed after inode items are replayed.
1472 * This ensures that inodes are completely flushed to the inode buffer
1473 * in a "free" state before we remove the unlinked inode list pointer.
1474 *
1475 * Hence the ordering needs to be inode allocation buffers first, inode items
1476 * second, inode unlink buffers third and cancelled buffers last.
1477 *
1478 * But there's a problem with that - we can't tell an inode allocation buffer
1479 * apart from a regular buffer, so we can't separate them. We can, however,
1480 * tell an inode unlink buffer from the others, and so we can separate them out
1481 * from all the other buffers and move them to last.
1482 *
1483 * Hence, 4 lists, in order from head to tail:
Dave Chinner28c8e412013-06-27 16:04:55 +10001484 * - buffer_list for all buffers except cancelled/inode unlink buffers
1485 * - item_list for all non-buffer items
1486 * - inode_buffer_list for inode unlink buffers
1487 * - cancel_list for the cancelled buffers
1488 *
1489 * Note that we add objects to the tail of the lists so that first-to-last
1490 * ordering is preserved within the lists. Adding objects to the head of the
1491 * list means when we traverse from the head we walk them in last-to-first
1492 * order. For cancelled buffers and inode unlink buffers this doesn't matter,
1493 * but for all other items there may be specific ordering that we need to
1494 * preserve.
Dave Chinnerf0a76952010-01-11 11:49:57 +00001495 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496STATIC int
1497xlog_recover_reorder_trans(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001498 struct xlog *log,
1499 struct xlog_recover *trans,
Dave Chinner9abbc532010-04-13 15:06:46 +10001500 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501{
Dave Chinnerf0a76952010-01-11 11:49:57 +00001502 xlog_recover_item_t *item, *n;
Mark Tinguely2a841082013-10-02 07:51:12 -05001503 int error = 0;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001504 LIST_HEAD(sort_list);
Dave Chinnera775ad72013-06-05 12:09:07 +10001505 LIST_HEAD(cancel_list);
1506 LIST_HEAD(buffer_list);
1507 LIST_HEAD(inode_buffer_list);
1508 LIST_HEAD(inode_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509
Dave Chinnerf0a76952010-01-11 11:49:57 +00001510 list_splice_init(&trans->r_itemq, &sort_list);
1511 list_for_each_entry_safe(item, n, &sort_list, ri_list) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10001512 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Dave Chinnerf0a76952010-01-11 11:49:57 +00001513
1514 switch (ITEM_TYPE(item)) {
Dave Chinner28c8e412013-06-27 16:04:55 +10001515 case XFS_LI_ICREATE:
1516 list_move_tail(&item->ri_list, &buffer_list);
1517 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 case XFS_LI_BUF:
Dave Chinnera775ad72013-06-05 12:09:07 +10001519 if (buf_f->blf_flags & XFS_BLF_CANCEL) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001520 trace_xfs_log_recover_item_reorder_head(log,
1521 trans, item, pass);
Dave Chinnera775ad72013-06-05 12:09:07 +10001522 list_move(&item->ri_list, &cancel_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 break;
1524 }
Dave Chinnera775ad72013-06-05 12:09:07 +10001525 if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
1526 list_move(&item->ri_list, &inode_buffer_list);
1527 break;
1528 }
1529 list_move_tail(&item->ri_list, &buffer_list);
1530 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 case XFS_LI_INODE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 case XFS_LI_DQUOT:
1533 case XFS_LI_QUOTAOFF:
1534 case XFS_LI_EFD:
1535 case XFS_LI_EFI:
Dave Chinner9abbc532010-04-13 15:06:46 +10001536 trace_xfs_log_recover_item_reorder_tail(log,
1537 trans, item, pass);
Dave Chinnera775ad72013-06-05 12:09:07 +10001538 list_move_tail(&item->ri_list, &inode_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 break;
1540 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001541 xfs_warn(log->l_mp,
1542 "%s: unrecognized type of log operation",
1543 __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ASSERT(0);
Mark Tinguely2a841082013-10-02 07:51:12 -05001545 /*
1546 * return the remaining items back to the transaction
1547 * item list so they can be freed in caller.
1548 */
1549 if (!list_empty(&sort_list))
1550 list_splice_init(&sort_list, &trans->r_itemq);
Dave Chinner24513372014-06-25 14:58:08 +10001551 error = -EIO;
Mark Tinguely2a841082013-10-02 07:51:12 -05001552 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 }
Dave Chinnerf0a76952010-01-11 11:49:57 +00001554 }
Mark Tinguely2a841082013-10-02 07:51:12 -05001555out:
Dave Chinnerf0a76952010-01-11 11:49:57 +00001556 ASSERT(list_empty(&sort_list));
Dave Chinnera775ad72013-06-05 12:09:07 +10001557 if (!list_empty(&buffer_list))
1558 list_splice(&buffer_list, &trans->r_itemq);
1559 if (!list_empty(&inode_list))
1560 list_splice_tail(&inode_list, &trans->r_itemq);
1561 if (!list_empty(&inode_buffer_list))
1562 list_splice_tail(&inode_buffer_list, &trans->r_itemq);
1563 if (!list_empty(&cancel_list))
1564 list_splice_tail(&cancel_list, &trans->r_itemq);
Mark Tinguely2a841082013-10-02 07:51:12 -05001565 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566}
1567
1568/*
1569 * Build up the table of buf cancel records so that we don't replay
1570 * cancelled data in the second pass. For buffer records that are
1571 * not cancel records, there is nothing to do here so we just return.
1572 *
1573 * If we get a cancel record which is already in the table, this indicates
1574 * that the buffer was cancelled multiple times. In order to ensure
1575 * that during pass 2 we keep the record in the table until we reach its
1576 * last occurrence in the log, we keep a reference count in the cancel
1577 * record in the table to tell us how many times we expect to see this
1578 * record during the second pass.
1579 */
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001580STATIC int
1581xlog_recover_buffer_pass1(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001582 struct xlog *log,
1583 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001585 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001586 struct list_head *bucket;
1587 struct xfs_buf_cancel *bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /*
1590 * If this isn't a cancel buffer item, then just return.
1591 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001592 if (!(buf_f->blf_flags & XFS_BLF_CANCEL)) {
Dave Chinner9abbc532010-04-13 15:06:46 +10001593 trace_xfs_log_recover_buf_not_cancel(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001594 return 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10001595 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 /*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001598 * Insert an xfs_buf_cancel record into the hash table of them.
1599 * If there is already an identical record, bump its reference count.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 */
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001601 bucket = XLOG_BUF_CANCEL_BUCKET(log, buf_f->blf_blkno);
1602 list_for_each_entry(bcp, bucket, bc_list) {
1603 if (bcp->bc_blkno == buf_f->blf_blkno &&
1604 bcp->bc_len == buf_f->blf_len) {
1605 bcp->bc_refcount++;
Dave Chinner9abbc532010-04-13 15:06:46 +10001606 trace_xfs_log_recover_buf_cancel_ref_inc(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001607 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001610
1611 bcp = kmem_alloc(sizeof(struct xfs_buf_cancel), KM_SLEEP);
1612 bcp->bc_blkno = buf_f->blf_blkno;
1613 bcp->bc_len = buf_f->blf_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 bcp->bc_refcount = 1;
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001615 list_add_tail(&bcp->bc_list, bucket);
1616
Dave Chinner9abbc532010-04-13 15:06:46 +10001617 trace_xfs_log_recover_buf_cancel_add(log, buf_f);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00001618 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619}
1620
1621/*
1622 * Check to see whether the buffer being recovered has a corresponding
Dave Chinner84a5b732013-08-27 08:10:53 +10001623 * entry in the buffer cancel record table. If it is, return the cancel
1624 * buffer structure to the caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 */
Dave Chinner84a5b732013-08-27 08:10:53 +10001626STATIC struct xfs_buf_cancel *
1627xlog_peek_buffer_cancelled(
Mark Tinguelyad223e62012-06-14 09:22:15 -05001628 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 xfs_daddr_t blkno,
1630 uint len,
1631 ushort flags)
1632{
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001633 struct list_head *bucket;
1634 struct xfs_buf_cancel *bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
Dave Chinner84a5b732013-08-27 08:10:53 +10001636 if (!log->l_buf_cancel_table) {
1637 /* empty table means no cancelled buffers in the log */
Dave Chinnerc1155412010-05-07 11:05:19 +10001638 ASSERT(!(flags & XFS_BLF_CANCEL));
Dave Chinner84a5b732013-08-27 08:10:53 +10001639 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 }
1641
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001642 bucket = XLOG_BUF_CANCEL_BUCKET(log, blkno);
1643 list_for_each_entry(bcp, bucket, bc_list) {
1644 if (bcp->bc_blkno == blkno && bcp->bc_len == len)
Dave Chinner84a5b732013-08-27 08:10:53 +10001645 return bcp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 }
1647
1648 /*
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001649 * We didn't find a corresponding entry in the table, so return 0 so
1650 * that the buffer is NOT cancelled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 */
Dave Chinnerc1155412010-05-07 11:05:19 +10001652 ASSERT(!(flags & XFS_BLF_CANCEL));
Dave Chinner84a5b732013-08-27 08:10:53 +10001653 return NULL;
1654}
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001655
Dave Chinner84a5b732013-08-27 08:10:53 +10001656/*
1657 * If the buffer is being cancelled then return 1 so that it will be cancelled,
1658 * otherwise return 0. If the buffer is actually a buffer cancel item
1659 * (XFS_BLF_CANCEL is set), then decrement the refcount on the entry in the
1660 * table and remove it from the table if this is the last reference.
1661 *
1662 * We remove the cancel record from the table when we encounter its last
1663 * occurrence in the log so that if the same buffer is re-used again after its
1664 * last cancellation we actually replay the changes made at that point.
1665 */
1666STATIC int
1667xlog_check_buffer_cancelled(
1668 struct xlog *log,
1669 xfs_daddr_t blkno,
1670 uint len,
1671 ushort flags)
1672{
1673 struct xfs_buf_cancel *bcp;
1674
1675 bcp = xlog_peek_buffer_cancelled(log, blkno, len, flags);
1676 if (!bcp)
1677 return 0;
1678
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00001679 /*
1680 * We've go a match, so return 1 so that the recovery of this buffer
1681 * is cancelled. If this buffer is actually a buffer cancel log
1682 * item, then decrement the refcount on the one in the table and
1683 * remove it if this is the last reference.
1684 */
1685 if (flags & XFS_BLF_CANCEL) {
1686 if (--bcp->bc_refcount == 0) {
1687 list_del(&bcp->bc_list);
1688 kmem_free(bcp);
1689 }
1690 }
1691 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694/*
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001695 * Perform recovery for a buffer full of inodes. In these buffers, the only
1696 * data which should be recovered is that which corresponds to the
1697 * di_next_unlinked pointers in the on disk inode structures. The rest of the
1698 * data for the inodes is always logged through the inodes themselves rather
1699 * than the inode buffer and is recovered in xlog_recover_inode_pass2().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 *
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001701 * The only time when buffers full of inodes are fully recovered is when the
1702 * buffer is full of newly allocated inodes. In this case the buffer will
1703 * not be marked as an inode buffer and so will be sent to
1704 * xlog_recover_do_reg_buffer() below during recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 */
1706STATIC int
1707xlog_recover_do_inode_buffer(
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001708 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 xlog_recover_item_t *item,
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001710 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 xfs_buf_log_format_t *buf_f)
1712{
1713 int i;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001714 int item_index = 0;
1715 int bit = 0;
1716 int nbits = 0;
1717 int reg_buf_offset = 0;
1718 int reg_buf_bytes = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 int next_unlinked_offset;
1720 int inodes_per_buf;
1721 xfs_agino_t *logged_nextp;
1722 xfs_agino_t *buffer_nextp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Dave Chinner9abbc532010-04-13 15:06:46 +10001724 trace_xfs_log_recover_buf_inode_buf(mp->m_log, buf_f);
Dave Chinner9222a9c2013-06-12 12:19:06 +10001725
1726 /*
1727 * Post recovery validation only works properly on CRC enabled
1728 * filesystems.
1729 */
1730 if (xfs_sb_version_hascrc(&mp->m_sb))
1731 bp->b_ops = &xfs_inode_buf_ops;
Dave Chinner9abbc532010-04-13 15:06:46 +10001732
Dave Chinneraa0e8832012-04-23 15:58:52 +10001733 inodes_per_buf = BBTOB(bp->b_io_length) >> mp->m_sb.sb_inodelog;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 for (i = 0; i < inodes_per_buf; i++) {
1735 next_unlinked_offset = (i * mp->m_sb.sb_inodesize) +
1736 offsetof(xfs_dinode_t, di_next_unlinked);
1737
1738 while (next_unlinked_offset >=
1739 (reg_buf_offset + reg_buf_bytes)) {
1740 /*
1741 * The next di_next_unlinked field is beyond
1742 * the current logged region. Find the next
1743 * logged region that contains or is beyond
1744 * the current di_next_unlinked field.
1745 */
1746 bit += nbits;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001747 bit = xfs_next_bit(buf_f->blf_data_map,
1748 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 /*
1751 * If there are no more logged regions in the
1752 * buffer, then we're done.
1753 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001754 if (bit == -1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001757 nbits = xfs_contig_bits(buf_f->blf_data_map,
1758 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ASSERT(nbits > 0);
Dave Chinnerc1155412010-05-07 11:05:19 +10001760 reg_buf_offset = bit << XFS_BLF_SHIFT;
1761 reg_buf_bytes = nbits << XFS_BLF_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 item_index++;
1763 }
1764
1765 /*
1766 * If the current logged region starts after the current
1767 * di_next_unlinked field, then move on to the next
1768 * di_next_unlinked field.
1769 */
Christoph Hellwige2714bf2010-12-01 22:06:21 +00001770 if (next_unlinked_offset < reg_buf_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772
1773 ASSERT(item->ri_buf[item_index].i_addr != NULL);
Dave Chinnerc1155412010-05-07 11:05:19 +10001774 ASSERT((item->ri_buf[item_index].i_len % XFS_BLF_CHUNK) == 0);
Dave Chinneraa0e8832012-04-23 15:58:52 +10001775 ASSERT((reg_buf_offset + reg_buf_bytes) <=
1776 BBTOB(bp->b_io_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 /*
1779 * The current logged region contains a copy of the
1780 * current di_next_unlinked field. Extract its value
1781 * and copy it to the buffer copy.
1782 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10001783 logged_nextp = item->ri_buf[item_index].i_addr +
1784 next_unlinked_offset - reg_buf_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 if (unlikely(*logged_nextp == 0)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11001786 xfs_alert(mp,
1787 "Bad inode buffer log record (ptr = 0x%p, bp = 0x%p). "
1788 "Trying to replay bad (0) inode di_next_unlinked field.",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 item, bp);
1790 XFS_ERROR_REPORT("xlog_recover_do_inode_buf",
1791 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10001792 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
1795 buffer_nextp = (xfs_agino_t *)xfs_buf_offset(bp,
1796 next_unlinked_offset);
Tim Shimmin87c199c2006-06-09 14:56:16 +10001797 *buffer_nextp = *logged_nextp;
Dave Chinner0a32c262013-06-05 12:09:08 +10001798
1799 /*
1800 * If necessary, recalculate the CRC in the on-disk inode. We
1801 * have to leave the inode in a consistent state for whoever
1802 * reads it next....
1803 */
1804 xfs_dinode_calc_crc(mp, (struct xfs_dinode *)
1805 xfs_buf_offset(bp, i * mp->m_sb.sb_inodesize));
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 }
1808
1809 return 0;
1810}
1811
1812/*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001813 * V5 filesystems know the age of the buffer on disk being recovered. We can
1814 * have newer objects on disk than we are replaying, and so for these cases we
1815 * don't want to replay the current change as that will make the buffer contents
1816 * temporarily invalid on disk.
1817 *
1818 * The magic number might not match the buffer type we are going to recover
1819 * (e.g. reallocated blocks), so we ignore the xfs_buf_log_format flags. Hence
1820 * extract the LSN of the existing object in the buffer based on it's current
1821 * magic number. If we don't recognise the magic number in the buffer, then
1822 * return a LSN of -1 so that the caller knows it was an unrecognised block and
1823 * so can recover the buffer.
Dave Chinner566055d2013-09-24 16:01:16 +10001824 *
1825 * Note: we cannot rely solely on magic number matches to determine that the
1826 * buffer has a valid LSN - we also need to verify that it belongs to this
1827 * filesystem, so we need to extract the object's LSN and compare it to that
1828 * which we read from the superblock. If the UUIDs don't match, then we've got a
1829 * stale metadata block from an old filesystem instance that we need to recover
1830 * over the top of.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001831 */
1832static xfs_lsn_t
1833xlog_recover_get_buf_lsn(
1834 struct xfs_mount *mp,
1835 struct xfs_buf *bp)
1836{
1837 __uint32_t magic32;
1838 __uint16_t magic16;
1839 __uint16_t magicda;
1840 void *blk = bp->b_addr;
Dave Chinner566055d2013-09-24 16:01:16 +10001841 uuid_t *uuid;
1842 xfs_lsn_t lsn = -1;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001843
1844 /* v4 filesystems always recover immediately */
1845 if (!xfs_sb_version_hascrc(&mp->m_sb))
1846 goto recover_immediately;
1847
1848 magic32 = be32_to_cpu(*(__be32 *)blk);
1849 switch (magic32) {
1850 case XFS_ABTB_CRC_MAGIC:
1851 case XFS_ABTC_CRC_MAGIC:
1852 case XFS_ABTB_MAGIC:
1853 case XFS_ABTC_MAGIC:
1854 case XFS_IBT_CRC_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001855 case XFS_IBT_MAGIC: {
1856 struct xfs_btree_block *btb = blk;
1857
1858 lsn = be64_to_cpu(btb->bb_u.s.bb_lsn);
1859 uuid = &btb->bb_u.s.bb_uuid;
1860 break;
1861 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001862 case XFS_BMAP_CRC_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001863 case XFS_BMAP_MAGIC: {
1864 struct xfs_btree_block *btb = blk;
1865
1866 lsn = be64_to_cpu(btb->bb_u.l.bb_lsn);
1867 uuid = &btb->bb_u.l.bb_uuid;
1868 break;
1869 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001870 case XFS_AGF_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001871 lsn = be64_to_cpu(((struct xfs_agf *)blk)->agf_lsn);
1872 uuid = &((struct xfs_agf *)blk)->agf_uuid;
1873 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001874 case XFS_AGFL_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001875 lsn = be64_to_cpu(((struct xfs_agfl *)blk)->agfl_lsn);
1876 uuid = &((struct xfs_agfl *)blk)->agfl_uuid;
1877 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001878 case XFS_AGI_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001879 lsn = be64_to_cpu(((struct xfs_agi *)blk)->agi_lsn);
1880 uuid = &((struct xfs_agi *)blk)->agi_uuid;
1881 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001882 case XFS_SYMLINK_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001883 lsn = be64_to_cpu(((struct xfs_dsymlink_hdr *)blk)->sl_lsn);
1884 uuid = &((struct xfs_dsymlink_hdr *)blk)->sl_uuid;
1885 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001886 case XFS_DIR3_BLOCK_MAGIC:
1887 case XFS_DIR3_DATA_MAGIC:
1888 case XFS_DIR3_FREE_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001889 lsn = be64_to_cpu(((struct xfs_dir3_blk_hdr *)blk)->lsn);
1890 uuid = &((struct xfs_dir3_blk_hdr *)blk)->uuid;
1891 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001892 case XFS_ATTR3_RMT_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001893 lsn = be64_to_cpu(((struct xfs_attr3_rmt_hdr *)blk)->rm_lsn);
1894 uuid = &((struct xfs_attr3_rmt_hdr *)blk)->rm_uuid;
1895 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001896 case XFS_SB_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001897 lsn = be64_to_cpu(((struct xfs_dsb *)blk)->sb_lsn);
1898 uuid = &((struct xfs_dsb *)blk)->sb_uuid;
1899 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001900 default:
1901 break;
1902 }
1903
Dave Chinner566055d2013-09-24 16:01:16 +10001904 if (lsn != (xfs_lsn_t)-1) {
1905 if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
1906 goto recover_immediately;
1907 return lsn;
1908 }
1909
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001910 magicda = be16_to_cpu(((struct xfs_da_blkinfo *)blk)->magic);
1911 switch (magicda) {
1912 case XFS_DIR3_LEAF1_MAGIC:
1913 case XFS_DIR3_LEAFN_MAGIC:
1914 case XFS_DA3_NODE_MAGIC:
Dave Chinner566055d2013-09-24 16:01:16 +10001915 lsn = be64_to_cpu(((struct xfs_da3_blkinfo *)blk)->lsn);
1916 uuid = &((struct xfs_da3_blkinfo *)blk)->uuid;
1917 break;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001918 default:
1919 break;
1920 }
1921
Dave Chinner566055d2013-09-24 16:01:16 +10001922 if (lsn != (xfs_lsn_t)-1) {
1923 if (!uuid_equal(&mp->m_sb.sb_uuid, uuid))
1924 goto recover_immediately;
1925 return lsn;
1926 }
1927
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001928 /*
1929 * We do individual object checks on dquot and inode buffers as they
1930 * have their own individual LSN records. Also, we could have a stale
1931 * buffer here, so we have to at least recognise these buffer types.
1932 *
1933 * A notd complexity here is inode unlinked list processing - it logs
1934 * the inode directly in the buffer, but we don't know which inodes have
1935 * been modified, and there is no global buffer LSN. Hence we need to
1936 * recover all inode buffer types immediately. This problem will be
1937 * fixed by logical logging of the unlinked list modifications.
1938 */
1939 magic16 = be16_to_cpu(*(__be16 *)blk);
1940 switch (magic16) {
1941 case XFS_DQUOT_MAGIC:
1942 case XFS_DINODE_MAGIC:
1943 goto recover_immediately;
1944 default:
1945 break;
1946 }
1947
1948 /* unknown buffer contents, recover immediately */
1949
1950recover_immediately:
1951 return (xfs_lsn_t)-1;
1952
1953}
1954
1955/*
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001956 * Validate the recovered buffer is of the correct type and attach the
1957 * appropriate buffer operations to them for writeback. Magic numbers are in a
1958 * few places:
1959 * the first 16 bits of the buffer (inode buffer, dquot buffer),
1960 * the first 32 bits of the buffer (most blocks),
1961 * inside a struct xfs_da_blkinfo at the start of the buffer.
1962 */
1963static void
Dave Chinner50d5c8d2013-08-28 21:22:47 +10001964xlog_recover_validate_buf_type(
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001965 struct xfs_mount *mp,
1966 struct xfs_buf *bp,
1967 xfs_buf_log_format_t *buf_f)
1968{
1969 struct xfs_da_blkinfo *info = bp->b_addr;
1970 __uint32_t magic32;
1971 __uint16_t magic16;
1972 __uint16_t magicda;
1973
Dave Chinner67dc2882014-08-04 12:43:06 +10001974 /*
1975 * We can only do post recovery validation on items on CRC enabled
1976 * fielsystems as we need to know when the buffer was written to be able
1977 * to determine if we should have replayed the item. If we replay old
1978 * metadata over a newer buffer, then it will enter a temporarily
1979 * inconsistent state resulting in verification failures. Hence for now
1980 * just avoid the verification stage for non-crc filesystems
1981 */
1982 if (!xfs_sb_version_hascrc(&mp->m_sb))
1983 return;
1984
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001985 magic32 = be32_to_cpu(*(__be32 *)bp->b_addr);
1986 magic16 = be16_to_cpu(*(__be16*)bp->b_addr);
1987 magicda = be16_to_cpu(info->magic);
Dave Chinner61fe1352013-04-03 16:11:30 +11001988 switch (xfs_blft_from_flags(buf_f)) {
1989 case XFS_BLFT_BTREE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001990 switch (magic32) {
1991 case XFS_ABTB_CRC_MAGIC:
1992 case XFS_ABTC_CRC_MAGIC:
1993 case XFS_ABTB_MAGIC:
1994 case XFS_ABTC_MAGIC:
1995 bp->b_ops = &xfs_allocbt_buf_ops;
1996 break;
1997 case XFS_IBT_CRC_MAGIC:
Brian Fosteraafc3c22014-04-24 16:00:52 +10001998 case XFS_FIBT_CRC_MAGIC:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11001999 case XFS_IBT_MAGIC:
Brian Fosteraafc3c22014-04-24 16:00:52 +10002000 case XFS_FIBT_MAGIC:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002001 bp->b_ops = &xfs_inobt_buf_ops;
2002 break;
2003 case XFS_BMAP_CRC_MAGIC:
2004 case XFS_BMAP_MAGIC:
2005 bp->b_ops = &xfs_bmbt_buf_ops;
2006 break;
2007 default:
2008 xfs_warn(mp, "Bad btree block magic!");
2009 ASSERT(0);
2010 break;
2011 }
2012 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002013 case XFS_BLFT_AGF_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002014 if (magic32 != XFS_AGF_MAGIC) {
2015 xfs_warn(mp, "Bad AGF block magic!");
2016 ASSERT(0);
2017 break;
2018 }
2019 bp->b_ops = &xfs_agf_buf_ops;
2020 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002021 case XFS_BLFT_AGFL_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002022 if (magic32 != XFS_AGFL_MAGIC) {
2023 xfs_warn(mp, "Bad AGFL block magic!");
2024 ASSERT(0);
2025 break;
2026 }
2027 bp->b_ops = &xfs_agfl_buf_ops;
2028 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002029 case XFS_BLFT_AGI_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002030 if (magic32 != XFS_AGI_MAGIC) {
2031 xfs_warn(mp, "Bad AGI block magic!");
2032 ASSERT(0);
2033 break;
2034 }
2035 bp->b_ops = &xfs_agi_buf_ops;
2036 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002037 case XFS_BLFT_UDQUOT_BUF:
2038 case XFS_BLFT_PDQUOT_BUF:
2039 case XFS_BLFT_GDQUOT_BUF:
Dave Chinner123887e2013-04-30 21:39:33 +10002040#ifdef CONFIG_XFS_QUOTA
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002041 if (magic16 != XFS_DQUOT_MAGIC) {
2042 xfs_warn(mp, "Bad DQUOT block magic!");
2043 ASSERT(0);
2044 break;
2045 }
2046 bp->b_ops = &xfs_dquot_buf_ops;
Dave Chinner123887e2013-04-30 21:39:33 +10002047#else
2048 xfs_alert(mp,
2049 "Trying to recover dquots without QUOTA support built in!");
2050 ASSERT(0);
2051#endif
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002052 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002053 case XFS_BLFT_DINO_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002054 if (magic16 != XFS_DINODE_MAGIC) {
2055 xfs_warn(mp, "Bad INODE block magic!");
2056 ASSERT(0);
2057 break;
2058 }
2059 bp->b_ops = &xfs_inode_buf_ops;
2060 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002061 case XFS_BLFT_SYMLINK_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002062 if (magic32 != XFS_SYMLINK_MAGIC) {
2063 xfs_warn(mp, "Bad symlink block magic!");
2064 ASSERT(0);
2065 break;
2066 }
2067 bp->b_ops = &xfs_symlink_buf_ops;
2068 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002069 case XFS_BLFT_DIR_BLOCK_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002070 if (magic32 != XFS_DIR2_BLOCK_MAGIC &&
2071 magic32 != XFS_DIR3_BLOCK_MAGIC) {
2072 xfs_warn(mp, "Bad dir block magic!");
2073 ASSERT(0);
2074 break;
2075 }
2076 bp->b_ops = &xfs_dir3_block_buf_ops;
2077 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002078 case XFS_BLFT_DIR_DATA_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002079 if (magic32 != XFS_DIR2_DATA_MAGIC &&
2080 magic32 != XFS_DIR3_DATA_MAGIC) {
2081 xfs_warn(mp, "Bad dir data magic!");
2082 ASSERT(0);
2083 break;
2084 }
2085 bp->b_ops = &xfs_dir3_data_buf_ops;
2086 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002087 case XFS_BLFT_DIR_FREE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002088 if (magic32 != XFS_DIR2_FREE_MAGIC &&
2089 magic32 != XFS_DIR3_FREE_MAGIC) {
2090 xfs_warn(mp, "Bad dir3 free magic!");
2091 ASSERT(0);
2092 break;
2093 }
2094 bp->b_ops = &xfs_dir3_free_buf_ops;
2095 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002096 case XFS_BLFT_DIR_LEAF1_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002097 if (magicda != XFS_DIR2_LEAF1_MAGIC &&
2098 magicda != XFS_DIR3_LEAF1_MAGIC) {
2099 xfs_warn(mp, "Bad dir leaf1 magic!");
2100 ASSERT(0);
2101 break;
2102 }
2103 bp->b_ops = &xfs_dir3_leaf1_buf_ops;
2104 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002105 case XFS_BLFT_DIR_LEAFN_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002106 if (magicda != XFS_DIR2_LEAFN_MAGIC &&
2107 magicda != XFS_DIR3_LEAFN_MAGIC) {
2108 xfs_warn(mp, "Bad dir leafn magic!");
2109 ASSERT(0);
2110 break;
2111 }
2112 bp->b_ops = &xfs_dir3_leafn_buf_ops;
2113 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002114 case XFS_BLFT_DA_NODE_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002115 if (magicda != XFS_DA_NODE_MAGIC &&
2116 magicda != XFS_DA3_NODE_MAGIC) {
2117 xfs_warn(mp, "Bad da node magic!");
2118 ASSERT(0);
2119 break;
2120 }
2121 bp->b_ops = &xfs_da3_node_buf_ops;
2122 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002123 case XFS_BLFT_ATTR_LEAF_BUF:
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002124 if (magicda != XFS_ATTR_LEAF_MAGIC &&
2125 magicda != XFS_ATTR3_LEAF_MAGIC) {
2126 xfs_warn(mp, "Bad attr leaf magic!");
2127 ASSERT(0);
2128 break;
2129 }
2130 bp->b_ops = &xfs_attr3_leaf_buf_ops;
2131 break;
Dave Chinner61fe1352013-04-03 16:11:30 +11002132 case XFS_BLFT_ATTR_RMT_BUF:
Dave Chinnercab09a82013-04-30 21:39:36 +10002133 if (magic32 != XFS_ATTR3_RMT_MAGIC) {
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002134 xfs_warn(mp, "Bad attr remote magic!");
2135 ASSERT(0);
2136 break;
2137 }
2138 bp->b_ops = &xfs_attr3_rmt_buf_ops;
2139 break;
Dave Chinner04a1e6c2013-04-03 16:11:31 +11002140 case XFS_BLFT_SB_BUF:
2141 if (magic32 != XFS_SB_MAGIC) {
2142 xfs_warn(mp, "Bad SB block magic!");
2143 ASSERT(0);
2144 break;
2145 }
2146 bp->b_ops = &xfs_sb_buf_ops;
2147 break;
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002148 default:
Dave Chinner61fe1352013-04-03 16:11:30 +11002149 xfs_warn(mp, "Unknown buffer type %d!",
2150 xfs_blft_from_flags(buf_f));
Dave Chinnerd75afeb2013-04-03 16:11:29 +11002151 break;
2152 }
2153}
2154
2155/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * Perform a 'normal' buffer recovery. Each logged region of the
2157 * buffer should be copied over the corresponding region in the
2158 * given buffer. The bitmap in the buf log format structure indicates
2159 * where to place the logged data.
2160 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161STATIC void
2162xlog_recover_do_reg_buffer(
Dave Chinner9abbc532010-04-13 15:06:46 +10002163 struct xfs_mount *mp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 xlog_recover_item_t *item,
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002165 struct xfs_buf *bp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 xfs_buf_log_format_t *buf_f)
2167{
2168 int i;
2169 int bit;
2170 int nbits;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int error;
2172
Dave Chinner9abbc532010-04-13 15:06:46 +10002173 trace_xfs_log_recover_buf_reg_buf(mp->m_log, buf_f);
2174
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 bit = 0;
2176 i = 1; /* 0 is the buf format structure */
2177 while (1) {
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002178 bit = xfs_next_bit(buf_f->blf_data_map,
2179 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 if (bit == -1)
2181 break;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002182 nbits = xfs_contig_bits(buf_f->blf_data_map,
2183 buf_f->blf_map_size, bit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 ASSERT(nbits > 0);
Christoph Hellwig4b809162007-08-16 15:37:36 +10002185 ASSERT(item->ri_buf[i].i_addr != NULL);
Dave Chinnerc1155412010-05-07 11:05:19 +10002186 ASSERT(item->ri_buf[i].i_len % XFS_BLF_CHUNK == 0);
Dave Chinneraa0e8832012-04-23 15:58:52 +10002187 ASSERT(BBTOB(bp->b_io_length) >=
2188 ((uint)bit << XFS_BLF_SHIFT) + (nbits << XFS_BLF_SHIFT));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
2190 /*
Dave Chinner709da6a2013-05-27 16:38:23 +10002191 * The dirty regions logged in the buffer, even though
2192 * contiguous, may span multiple chunks. This is because the
2193 * dirty region may span a physical page boundary in a buffer
2194 * and hence be split into two separate vectors for writing into
2195 * the log. Hence we need to trim nbits back to the length of
2196 * the current region being copied out of the log.
2197 */
2198 if (item->ri_buf[i].i_len < (nbits << XFS_BLF_SHIFT))
2199 nbits = item->ri_buf[i].i_len >> XFS_BLF_SHIFT;
2200
2201 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 * Do a sanity check if this is a dquot buffer. Just checking
2203 * the first dquot in the buffer should do. XXXThis is
2204 * probably a good thing to do for other buf types also.
2205 */
2206 error = 0;
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002207 if (buf_f->blf_flags &
Dave Chinnerc1155412010-05-07 11:05:19 +10002208 (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002209 if (item->ri_buf[i].i_addr == NULL) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002210 xfs_alert(mp,
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002211 "XFS: NULL dquot in %s.", __func__);
2212 goto next;
2213 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00002214 if (item->ri_buf[i].i_len < sizeof(xfs_disk_dquot_t)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002215 xfs_alert(mp,
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002216 "XFS: dquot too small (%d) in %s.",
2217 item->ri_buf[i].i_len, __func__);
2218 goto next;
2219 }
Dave Chinner9aede1d2013-10-15 09:17:52 +11002220 error = xfs_dqcheck(mp, item->ri_buf[i].i_addr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 -1, 0, XFS_QMOPT_DOWARN,
2222 "dquot_buf_recover");
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002223 if (error)
2224 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 }
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002226
2227 memcpy(xfs_buf_offset(bp,
Dave Chinnerc1155412010-05-07 11:05:19 +10002228 (uint)bit << XFS_BLF_SHIFT), /* dest */
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002229 item->ri_buf[i].i_addr, /* source */
Dave Chinnerc1155412010-05-07 11:05:19 +10002230 nbits<<XFS_BLF_SHIFT); /* length */
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002231 next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 i++;
2233 bit += nbits;
2234 }
2235
2236 /* Shouldn't be any more regions */
2237 ASSERT(i == item->ri_total);
Christoph Hellwigee1a47a2013-04-21 14:53:46 -05002238
Dave Chinner67dc2882014-08-04 12:43:06 +10002239 xlog_recover_validate_buf_type(mp, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240}
2241
2242/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * Perform a dquot buffer recovery.
Zhi Yong Wu8ba701e2013-08-12 03:15:01 +00002244 * Simple algorithm: if we have found a QUOTAOFF log item of the same type
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 * (ie. USR or GRP), then just toss this buffer away; don't recover it.
2246 * Else, treat it as a regular buffer and do recovery.
Dave Chinnerad3714b2014-08-04 12:59:31 +10002247 *
2248 * Return false if the buffer was tossed and true if we recovered the buffer to
2249 * indicate to the caller if the buffer needs writing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 */
Dave Chinnerad3714b2014-08-04 12:59:31 +10002251STATIC bool
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252xlog_recover_do_dquot_buffer(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002253 struct xfs_mount *mp,
2254 struct xlog *log,
2255 struct xlog_recover_item *item,
2256 struct xfs_buf *bp,
2257 struct xfs_buf_log_format *buf_f)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
2259 uint type;
2260
Dave Chinner9abbc532010-04-13 15:06:46 +10002261 trace_xfs_log_recover_buf_dquot_buf(log, buf_f);
2262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 /*
2264 * Filesystems are required to send in quota flags at mount time.
2265 */
Dave Chinnerad3714b2014-08-04 12:59:31 +10002266 if (!mp->m_qflags)
2267 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 type = 0;
Dave Chinnerc1155412010-05-07 11:05:19 +10002270 if (buf_f->blf_flags & XFS_BLF_UDQUOT_BUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 type |= XFS_DQ_USER;
Dave Chinnerc1155412010-05-07 11:05:19 +10002272 if (buf_f->blf_flags & XFS_BLF_PDQUOT_BUF)
Nathan Scottc8ad20f2005-06-21 15:38:48 +10002273 type |= XFS_DQ_PROJ;
Dave Chinnerc1155412010-05-07 11:05:19 +10002274 if (buf_f->blf_flags & XFS_BLF_GDQUOT_BUF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 type |= XFS_DQ_GROUP;
2276 /*
2277 * This type of quotas was turned off, so ignore this buffer
2278 */
2279 if (log->l_quotaoffs_flag & type)
Dave Chinnerad3714b2014-08-04 12:59:31 +10002280 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281
Dave Chinner9abbc532010-04-13 15:06:46 +10002282 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Dave Chinnerad3714b2014-08-04 12:59:31 +10002283 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284}
2285
2286/*
2287 * This routine replays a modification made to a buffer at runtime.
2288 * There are actually two types of buffer, regular and inode, which
2289 * are handled differently. Inode buffers are handled differently
2290 * in that we only recover a specific set of data from them, namely
2291 * the inode di_next_unlinked fields. This is because all other inode
2292 * data is actually logged via inode records and any data we replay
2293 * here which overlaps that may be stale.
2294 *
2295 * When meta-data buffers are freed at run time we log a buffer item
Dave Chinnerc1155412010-05-07 11:05:19 +10002296 * with the XFS_BLF_CANCEL bit set to indicate that previous copies
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 * of the buffer in the log should not be replayed at recovery time.
2298 * This is so that if the blocks covered by the buffer are reused for
2299 * file data before we crash we don't end up replaying old, freed
2300 * meta-data into a user's file.
2301 *
2302 * To handle the cancellation of buffer log items, we make two passes
2303 * over the log during recovery. During the first we build a table of
2304 * those buffers which have been cancelled, and during the second we
2305 * only replay those buffers which do not have corresponding cancel
Zhi Yong Wu34be5ff2013-08-07 10:11:07 +00002306 * records in the table. See xlog_recover_buffer_pass[1,2] above
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 * for more details on the implementation of the table of cancel records.
2308 */
2309STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002310xlog_recover_buffer_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002311 struct xlog *log,
2312 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002313 struct xlog_recover_item *item,
2314 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002316 xfs_buf_log_format_t *buf_f = item->ri_buf[0].i_addr;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002317 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 xfs_buf_t *bp;
2319 int error;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002320 uint buf_flags;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002321 xfs_lsn_t lsn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002323 /*
2324 * In this pass we only want to recover all the buffers which have
2325 * not been cancelled and are not cancellation buffers themselves.
2326 */
2327 if (xlog_check_buffer_cancelled(log, buf_f->blf_blkno,
2328 buf_f->blf_len, buf_f->blf_flags)) {
2329 trace_xfs_log_recover_buf_cancel(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002332
Dave Chinner9abbc532010-04-13 15:06:46 +10002333 trace_xfs_log_recover_buf_recover(log, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Dave Chinnera8acad72012-04-23 15:58:54 +10002335 buf_flags = 0;
Dave Chinner611c9942012-04-23 15:59:07 +10002336 if (buf_f->blf_flags & XFS_BLF_INODE_BUF)
2337 buf_flags |= XBF_UNMAPPED;
Christoph Hellwig6ad112b2009-11-24 18:02:23 +00002338
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002339 bp = xfs_buf_read(mp->m_ddev_targp, buf_f->blf_blkno, buf_f->blf_len,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002340 buf_flags, NULL);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002341 if (!bp)
Dave Chinner24513372014-06-25 14:58:08 +10002342 return -ENOMEM;
Chandra Seetharamane5702802011-08-03 02:18:34 +00002343 error = bp->b_error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002344 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002345 xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#1)");
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002346 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 }
2348
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002349 /*
Dave Chinner67dc2882014-08-04 12:43:06 +10002350 * Recover the buffer only if we get an LSN from it and it's less than
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002351 * the lsn of the transaction we are replaying.
Dave Chinner67dc2882014-08-04 12:43:06 +10002352 *
2353 * Note that we have to be extremely careful of readahead here.
2354 * Readahead does not attach verfiers to the buffers so if we don't
2355 * actually do any replay after readahead because of the LSN we found
2356 * in the buffer if more recent than that current transaction then we
2357 * need to attach the verifier directly. Failure to do so can lead to
2358 * future recovery actions (e.g. EFI and unlinked list recovery) can
2359 * operate on the buffers and they won't get the verifier attached. This
2360 * can lead to blocks on disk having the correct content but a stale
2361 * CRC.
2362 *
2363 * It is safe to assume these clean buffers are currently up to date.
2364 * If the buffer is dirtied by a later transaction being replayed, then
2365 * the verifier will be reset to match whatever recover turns that
2366 * buffer into.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002367 */
2368 lsn = xlog_recover_get_buf_lsn(mp, bp);
Dave Chinner67dc2882014-08-04 12:43:06 +10002369 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
2370 xlog_recover_validate_buf_type(mp, bp, buf_f);
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002371 goto out_release;
Dave Chinner67dc2882014-08-04 12:43:06 +10002372 }
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002373
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002374 if (buf_f->blf_flags & XFS_BLF_INODE_BUF) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 error = xlog_recover_do_inode_buffer(mp, item, bp, buf_f);
Dave Chinnerad3714b2014-08-04 12:59:31 +10002376 if (error)
2377 goto out_release;
Christoph Hellwige2714bf2010-12-01 22:06:21 +00002378 } else if (buf_f->blf_flags &
Dave Chinnerc1155412010-05-07 11:05:19 +10002379 (XFS_BLF_UDQUOT_BUF|XFS_BLF_PDQUOT_BUF|XFS_BLF_GDQUOT_BUF)) {
Dave Chinnerad3714b2014-08-04 12:59:31 +10002380 bool dirty;
2381
2382 dirty = xlog_recover_do_dquot_buffer(mp, log, item, bp, buf_f);
2383 if (!dirty)
2384 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 } else {
Dave Chinner9abbc532010-04-13 15:06:46 +10002386 xlog_recover_do_reg_buffer(mp, item, bp, buf_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /*
2390 * Perform delayed write on the buffer. Asynchronous writes will be
2391 * slower when taking into account all the buffers to be flushed.
2392 *
2393 * Also make sure that only inode buffers with good sizes stay in
2394 * the buffer cache. The kernel moves inodes in buffers of 1 block
Jie Liu0f49efd2013-12-13 15:51:48 +11002395 * or mp->m_inode_cluster_size bytes, whichever is bigger. The inode
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 * buffers in the log can be a different size if the log was generated
2397 * by an older kernel using unclustered inode buffers or a newer kernel
2398 * running with a different inode cluster size. Regardless, if the
Jie Liu0f49efd2013-12-13 15:51:48 +11002399 * the inode buffer size isn't MAX(blocksize, mp->m_inode_cluster_size)
2400 * for *our* value of mp->m_inode_cluster_size, then we need to keep
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 * the buffer out of the buffer cache so that the buffer won't
2402 * overlap with future reads of those inodes.
2403 */
2404 if (XFS_DINODE_MAGIC ==
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002405 be16_to_cpu(*((__be16 *)xfs_buf_offset(bp, 0))) &&
Dave Chinneraa0e8832012-04-23 15:58:52 +10002406 (BBTOB(bp->b_io_length) != MAX(log->l_mp->m_sb.sb_blocksize,
Jie Liu0f49efd2013-12-13 15:51:48 +11002407 (__uint32_t)log->l_mp->m_inode_cluster_size))) {
Christoph Hellwigc867cb62011-10-10 16:52:46 +00002408 xfs_buf_stale(bp);
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002409 error = xfs_bwrite(bp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 } else {
Dave Chinnerebad8612010-09-22 10:47:20 +10002411 ASSERT(bp->b_target->bt_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002412 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002413 xfs_buf_delwri_queue(bp, buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002416out_release:
Christoph Hellwigc2b006c2011-08-23 08:28:07 +00002417 xfs_buf_relse(bp);
2418 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Dave Chinner638f44162013-08-30 10:23:45 +10002421/*
2422 * Inode fork owner changes
2423 *
2424 * If we have been told that we have to reparent the inode fork, it's because an
2425 * extent swap operation on a CRC enabled filesystem has been done and we are
2426 * replaying it. We need to walk the BMBT of the appropriate fork and change the
2427 * owners of it.
2428 *
2429 * The complexity here is that we don't have an inode context to work with, so
2430 * after we've replayed the inode we need to instantiate one. This is where the
2431 * fun begins.
2432 *
2433 * We are in the middle of log recovery, so we can't run transactions. That
2434 * means we cannot use cache coherent inode instantiation via xfs_iget(), as
2435 * that will result in the corresponding iput() running the inode through
2436 * xfs_inactive(). If we've just replayed an inode core that changes the link
2437 * count to zero (i.e. it's been unlinked), then xfs_inactive() will run
2438 * transactions (bad!).
2439 *
2440 * So, to avoid this, we instantiate an inode directly from the inode core we've
2441 * just recovered. We have the buffer still locked, and all we really need to
2442 * instantiate is the inode core and the forks being modified. We can do this
2443 * manually, then run the inode btree owner change, and then tear down the
2444 * xfs_inode without having to run any transactions at all.
2445 *
2446 * Also, because we don't have a transaction context available here but need to
2447 * gather all the buffers we modify for writeback so we pass the buffer_list
2448 * instead for the operation to use.
2449 */
2450
2451STATIC int
2452xfs_recover_inode_owner_change(
2453 struct xfs_mount *mp,
2454 struct xfs_dinode *dip,
2455 struct xfs_inode_log_format *in_f,
2456 struct list_head *buffer_list)
2457{
2458 struct xfs_inode *ip;
2459 int error;
2460
2461 ASSERT(in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER));
2462
2463 ip = xfs_inode_alloc(mp, in_f->ilf_ino);
2464 if (!ip)
Dave Chinner24513372014-06-25 14:58:08 +10002465 return -ENOMEM;
Dave Chinner638f44162013-08-30 10:23:45 +10002466
2467 /* instantiate the inode */
2468 xfs_dinode_from_disk(&ip->i_d, dip);
2469 ASSERT(ip->i_d.di_version >= 3);
2470
2471 error = xfs_iformat_fork(ip, dip);
2472 if (error)
2473 goto out_free_ip;
2474
2475
2476 if (in_f->ilf_fields & XFS_ILOG_DOWNER) {
2477 ASSERT(in_f->ilf_fields & XFS_ILOG_DBROOT);
2478 error = xfs_bmbt_change_owner(NULL, ip, XFS_DATA_FORK,
2479 ip->i_ino, buffer_list);
2480 if (error)
2481 goto out_free_ip;
2482 }
2483
2484 if (in_f->ilf_fields & XFS_ILOG_AOWNER) {
2485 ASSERT(in_f->ilf_fields & XFS_ILOG_ABROOT);
2486 error = xfs_bmbt_change_owner(NULL, ip, XFS_ATTR_FORK,
2487 ip->i_ino, buffer_list);
2488 if (error)
2489 goto out_free_ip;
2490 }
2491
2492out_free_ip:
2493 xfs_inode_free(ip);
2494 return error;
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002498xlog_recover_inode_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002499 struct xlog *log,
2500 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002501 struct xlog_recover_item *item,
2502 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503{
2504 xfs_inode_log_format_t *in_f;
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002505 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 xfs_buf_t *bp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 xfs_dinode_t *dip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 int len;
2509 xfs_caddr_t src;
2510 xfs_caddr_t dest;
2511 int error;
2512 int attr_index;
2513 uint fields;
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002514 xfs_icdinode_t *dicp;
Christoph Hellwig93848a92013-04-03 16:11:17 +11002515 uint isize;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002516 int need_free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Tim Shimmin6d192a92006-06-09 14:55:38 +10002518 if (item->ri_buf[0].i_len == sizeof(xfs_inode_log_format_t)) {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002519 in_f = item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002520 } else {
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002521 in_f = kmem_alloc(sizeof(xfs_inode_log_format_t), KM_SLEEP);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002522 need_free = 1;
2523 error = xfs_inode_item_format_convert(&item->ri_buf[0], in_f);
2524 if (error)
2525 goto error;
2526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
2528 /*
2529 * Inode buffers can be freed, look out for it,
2530 * and do not replay the inode.
2531 */
Christoph Hellwiga1941892008-11-28 14:23:40 +11002532 if (xlog_check_buffer_cancelled(log, in_f->ilf_blkno,
2533 in_f->ilf_len, 0)) {
Tim Shimmin6d192a92006-06-09 14:55:38 +10002534 error = 0;
Dave Chinner9abbc532010-04-13 15:06:46 +10002535 trace_xfs_log_recover_inode_cancel(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002536 goto error;
2537 }
Dave Chinner9abbc532010-04-13 15:06:46 +10002538 trace_xfs_log_recover_inode_recover(log, in_f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002540 bp = xfs_buf_read(mp->m_ddev_targp, in_f->ilf_blkno, in_f->ilf_len, 0,
Christoph Hellwig93848a92013-04-03 16:11:17 +11002541 &xfs_inode_buf_ops);
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002542 if (!bp) {
Dave Chinner24513372014-06-25 14:58:08 +10002543 error = -ENOMEM;
Chandra Seetharamanac4d6882011-08-03 02:18:29 +00002544 goto error;
2545 }
Chandra Seetharamane5702802011-08-03 02:18:34 +00002546 error = bp->b_error;
Chandra Seetharaman5a52c2a582011-07-22 23:39:51 +00002547 if (error) {
Christoph Hellwig901796a2011-10-10 16:52:49 +00002548 xfs_buf_ioerror_alert(bp, "xlog_recover_do..(read#2)");
Dave Chinner638f44162013-08-30 10:23:45 +10002549 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 ASSERT(in_f->ilf_fields & XFS_ILOG_CORE);
Christoph Hellwiga1941892008-11-28 14:23:40 +11002552 dip = (xfs_dinode_t *)xfs_buf_offset(bp, in_f->ilf_boffset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553
2554 /*
2555 * Make sure the place we're flushing out to really looks
2556 * like an inode!
2557 */
Christoph Hellwig69ef9212011-07-08 14:36:05 +02002558 if (unlikely(dip->di_magic != cpu_to_be16(XFS_DINODE_MAGIC))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002559 xfs_alert(mp,
2560 "%s: Bad inode magic number, dip = 0x%p, dino bp = 0x%p, ino = %Ld",
2561 __func__, dip, bp, in_f->ilf_ino);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002562 XFS_ERROR_REPORT("xlog_recover_inode_pass2(1)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002564 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002565 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 }
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002567 dicp = item->ri_buf[1].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 if (unlikely(dicp->di_magic != XFS_DINODE_MAGIC)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002569 xfs_alert(mp,
2570 "%s: Bad inode log record, rec ptr 0x%p, ino %Ld",
2571 __func__, item, in_f->ilf_ino);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002572 XFS_ERROR_REPORT("xlog_recover_inode_pass2(2)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 XFS_ERRLEVEL_LOW, mp);
Dave Chinner24513372014-06-25 14:58:08 +10002574 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002575 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 }
2577
Dave Chinnere60896d2013-07-24 15:47:30 +10002578 /*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002579 * If the inode has an LSN in it, recover the inode only if it's less
Dave Chinner638f44162013-08-30 10:23:45 +10002580 * than the lsn of the transaction we are replaying. Note: we still
2581 * need to replay an owner change even though the inode is more recent
2582 * than the transaction as there is no guarantee that all the btree
2583 * blocks are more recent than this transaction, too.
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002584 */
2585 if (dip->di_version >= 3) {
2586 xfs_lsn_t lsn = be64_to_cpu(dip->di_lsn);
2587
2588 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
2589 trace_xfs_log_recover_inode_skip(log, in_f);
2590 error = 0;
Dave Chinner638f44162013-08-30 10:23:45 +10002591 goto out_owner_change;
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002592 }
2593 }
2594
2595 /*
Dave Chinnere60896d2013-07-24 15:47:30 +10002596 * di_flushiter is only valid for v1/2 inodes. All changes for v3 inodes
2597 * are transactional and if ordering is necessary we can determine that
2598 * more accurately by the LSN field in the V3 inode core. Don't trust
2599 * the inode versions we might be changing them here - use the
2600 * superblock flag to determine whether we need to look at di_flushiter
2601 * to skip replay when the on disk inode is newer than the log one
2602 */
2603 if (!xfs_sb_version_hascrc(&mp->m_sb) &&
2604 dicp->di_flushiter < be16_to_cpu(dip->di_flushiter)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 /*
2606 * Deal with the wrap case, DI_MAX_FLUSH is less
2607 * than smaller numbers
2608 */
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002609 if (be16_to_cpu(dip->di_flushiter) == DI_MAX_FLUSH &&
Christoph Hellwig347d1c02007-08-28 13:57:51 +10002610 dicp->di_flushiter < (DI_MAX_FLUSH >> 1)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 /* do nothing */
2612 } else {
Dave Chinner9abbc532010-04-13 15:06:46 +10002613 trace_xfs_log_recover_inode_skip(log, in_f);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002614 error = 0;
Dave Chinner638f44162013-08-30 10:23:45 +10002615 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
2617 }
Dave Chinnere60896d2013-07-24 15:47:30 +10002618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 /* Take the opportunity to reset the flush iteration count */
2620 dicp->di_flushiter = 0;
2621
Al Viroabbede12011-07-26 02:31:30 -04002622 if (unlikely(S_ISREG(dicp->di_mode))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2624 (dicp->di_format != XFS_DINODE_FMT_BTREE)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002625 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(3)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 XFS_ERRLEVEL_LOW, mp, dicp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002627 xfs_alert(mp,
2628 "%s: Bad regular inode log record, rec ptr 0x%p, "
2629 "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2630 __func__, item, dip, bp, in_f->ilf_ino);
Dave Chinner24513372014-06-25 14:58:08 +10002631 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002632 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 }
Al Viroabbede12011-07-26 02:31:30 -04002634 } else if (unlikely(S_ISDIR(dicp->di_mode))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 if ((dicp->di_format != XFS_DINODE_FMT_EXTENTS) &&
2636 (dicp->di_format != XFS_DINODE_FMT_BTREE) &&
2637 (dicp->di_format != XFS_DINODE_FMT_LOCAL)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002638 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(4)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 XFS_ERRLEVEL_LOW, mp, dicp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002640 xfs_alert(mp,
2641 "%s: Bad dir inode log record, rec ptr 0x%p, "
2642 "ino ptr = 0x%p, ino bp = 0x%p, ino %Ld",
2643 __func__, item, dip, bp, in_f->ilf_ino);
Dave Chinner24513372014-06-25 14:58:08 +10002644 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002645 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647 }
2648 if (unlikely(dicp->di_nextents + dicp->di_anextents > dicp->di_nblocks)){
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002649 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(5)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650 XFS_ERRLEVEL_LOW, mp, dicp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002651 xfs_alert(mp,
2652 "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
2653 "dino bp 0x%p, ino %Ld, total extents = %d, nblocks = %Ld",
2654 __func__, item, dip, bp, in_f->ilf_ino,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 dicp->di_nextents + dicp->di_anextents,
2656 dicp->di_nblocks);
Dave Chinner24513372014-06-25 14:58:08 +10002657 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002658 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 }
2660 if (unlikely(dicp->di_forkoff > mp->m_sb.sb_inodesize)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002661 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(6)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 XFS_ERRLEVEL_LOW, mp, dicp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002663 xfs_alert(mp,
2664 "%s: Bad inode log record, rec ptr 0x%p, dino ptr 0x%p, "
2665 "dino bp 0x%p, ino %Ld, forkoff 0x%x", __func__,
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002666 item, dip, bp, in_f->ilf_ino, dicp->di_forkoff);
Dave Chinner24513372014-06-25 14:58:08 +10002667 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002668 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
Christoph Hellwig93848a92013-04-03 16:11:17 +11002670 isize = xfs_icdinode_size(dicp->di_version);
2671 if (unlikely(item->ri_buf[1].i_len > isize)) {
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002672 XFS_CORRUPTION_ERROR("xlog_recover_inode_pass2(7)",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 XFS_ERRLEVEL_LOW, mp, dicp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002674 xfs_alert(mp,
2675 "%s: Bad inode log record length %d, rec ptr 0x%p",
2676 __func__, item->ri_buf[1].i_len, item);
Dave Chinner24513372014-06-25 14:58:08 +10002677 error = -EFSCORRUPTED;
Dave Chinner638f44162013-08-30 10:23:45 +10002678 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680
2681 /* The core is in in-core format */
Christoph Hellwig93848a92013-04-03 16:11:17 +11002682 xfs_dinode_to_disk(dip, dicp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683
2684 /* the rest is in on-disk format */
Christoph Hellwig93848a92013-04-03 16:11:17 +11002685 if (item->ri_buf[1].i_len > isize) {
2686 memcpy((char *)dip + isize,
2687 item->ri_buf[1].i_addr + isize,
2688 item->ri_buf[1].i_len - isize);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
2690
2691 fields = in_f->ilf_fields;
2692 switch (fields & (XFS_ILOG_DEV | XFS_ILOG_UUID)) {
2693 case XFS_ILOG_DEV:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002694 xfs_dinode_put_rdev(dip, in_f->ilf_u.ilfu_rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 break;
2696 case XFS_ILOG_UUID:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002697 memcpy(XFS_DFORK_DPTR(dip),
2698 &in_f->ilf_u.ilfu_uuid,
2699 sizeof(uuid_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 break;
2701 }
2702
2703 if (in_f->ilf_size == 2)
Dave Chinner638f44162013-08-30 10:23:45 +10002704 goto out_owner_change;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 len = item->ri_buf[2].i_len;
2706 src = item->ri_buf[2].i_addr;
2707 ASSERT(in_f->ilf_size <= 4);
2708 ASSERT((in_f->ilf_size == 3) || (fields & XFS_ILOG_AFORK));
2709 ASSERT(!(fields & XFS_ILOG_DFORK) ||
2710 (len == in_f->ilf_dsize));
2711
2712 switch (fields & XFS_ILOG_DFORK) {
2713 case XFS_ILOG_DDATA:
2714 case XFS_ILOG_DEXT:
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002715 memcpy(XFS_DFORK_DPTR(dip), src, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 break;
2717
2718 case XFS_ILOG_DBROOT:
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002719 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src, len,
Christoph Hellwig81591fe2008-11-28 14:23:39 +11002720 (xfs_bmdr_block_t *)XFS_DFORK_DPTR(dip),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 XFS_DFORK_DSIZE(dip, mp));
2722 break;
2723
2724 default:
2725 /*
2726 * There are no data fork flags set.
2727 */
2728 ASSERT((fields & XFS_ILOG_DFORK) == 0);
2729 break;
2730 }
2731
2732 /*
2733 * If we logged any attribute data, recover it. There may or
2734 * may not have been any other non-core data logged in this
2735 * transaction.
2736 */
2737 if (in_f->ilf_fields & XFS_ILOG_AFORK) {
2738 if (in_f->ilf_fields & XFS_ILOG_DFORK) {
2739 attr_index = 3;
2740 } else {
2741 attr_index = 2;
2742 }
2743 len = item->ri_buf[attr_index].i_len;
2744 src = item->ri_buf[attr_index].i_addr;
2745 ASSERT(len == in_f->ilf_asize);
2746
2747 switch (in_f->ilf_fields & XFS_ILOG_AFORK) {
2748 case XFS_ILOG_ADATA:
2749 case XFS_ILOG_AEXT:
2750 dest = XFS_DFORK_APTR(dip);
2751 ASSERT(len <= XFS_DFORK_ASIZE(dip, mp));
2752 memcpy(dest, src, len);
2753 break;
2754
2755 case XFS_ILOG_ABROOT:
2756 dest = XFS_DFORK_APTR(dip);
Christoph Hellwig7cc95a82008-10-30 17:14:34 +11002757 xfs_bmbt_to_bmdr(mp, (struct xfs_btree_block *)src,
2758 len, (xfs_bmdr_block_t*)dest,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 XFS_DFORK_ASIZE(dip, mp));
2760 break;
2761
2762 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002763 xfs_warn(log->l_mp, "%s: Invalid flag", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10002765 error = -EIO;
Dave Chinner638f44162013-08-30 10:23:45 +10002766 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 }
2768 }
2769
Dave Chinner638f44162013-08-30 10:23:45 +10002770out_owner_change:
2771 if (in_f->ilf_fields & (XFS_ILOG_DOWNER|XFS_ILOG_AOWNER))
2772 error = xfs_recover_inode_owner_change(mp, dip, in_f,
2773 buffer_list);
Christoph Hellwig93848a92013-04-03 16:11:17 +11002774 /* re-generate the checksum. */
2775 xfs_dinode_calc_crc(log->l_mp, dip);
2776
Dave Chinnerebad8612010-09-22 10:47:20 +10002777 ASSERT(bp->b_target->bt_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002778 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002779 xfs_buf_delwri_queue(bp, buffer_list);
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002780
2781out_release:
Christoph Hellwig61551f12011-08-23 08:28:06 +00002782 xfs_buf_relse(bp);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002783error:
2784 if (need_free)
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10002785 kmem_free(in_f);
Eric Sandeenb474c7a2014-06-22 15:04:54 +10002786 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787}
2788
2789/*
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002790 * Recover QUOTAOFF records. We simply make a note of it in the xlog
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 * structure, so that we know not to do any dquot item or dquot buffer recovery,
2792 * of that type.
2793 */
2794STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002795xlog_recover_quotaoff_pass1(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002796 struct xlog *log,
2797 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002799 xfs_qoff_logformat_t *qoff_f = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 ASSERT(qoff_f);
2801
2802 /*
2803 * The logitem format's flag tells us if this was user quotaoff,
Nathan Scott77a7cce2006-01-11 15:35:57 +11002804 * group/project quotaoff or both.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 */
2806 if (qoff_f->qf_flags & XFS_UQUOTA_ACCT)
2807 log->l_quotaoffs_flag |= XFS_DQ_USER;
Nathan Scott77a7cce2006-01-11 15:35:57 +11002808 if (qoff_f->qf_flags & XFS_PQUOTA_ACCT)
2809 log->l_quotaoffs_flag |= XFS_DQ_PROJ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 if (qoff_f->qf_flags & XFS_GQUOTA_ACCT)
2811 log->l_quotaoffs_flag |= XFS_DQ_GROUP;
2812
Eric Sandeend99831f2014-06-22 15:03:54 +10002813 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
2816/*
2817 * Recover a dquot record
2818 */
2819STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002820xlog_recover_dquot_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002821 struct xlog *log,
2822 struct list_head *buffer_list,
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002823 struct xlog_recover_item *item,
2824 xfs_lsn_t current_lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002826 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 xfs_buf_t *bp;
2828 struct xfs_disk_dquot *ddq, *recddq;
2829 int error;
2830 xfs_dq_logformat_t *dq_f;
2831 uint type;
2832
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
2834 /*
2835 * Filesystems are required to send in quota flags at mount time.
2836 */
2837 if (mp->m_qflags == 0)
Eric Sandeend99831f2014-06-22 15:03:54 +10002838 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002840 recddq = item->ri_buf[1].i_addr;
2841 if (recddq == NULL) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002842 xfs_alert(log->l_mp, "NULL dquot in %s.", __func__);
Dave Chinner24513372014-06-25 14:58:08 +10002843 return -EIO;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002844 }
Jan Rekorajski8ec6dba2009-11-16 11:57:02 +00002845 if (item->ri_buf[1].i_len < sizeof(xfs_disk_dquot_t)) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002846 xfs_alert(log->l_mp, "dquot too small (%d) in %s.",
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002847 item->ri_buf[1].i_len, __func__);
Dave Chinner24513372014-06-25 14:58:08 +10002848 return -EIO;
Christoph Hellwig0c5e1ce2009-06-08 15:33:21 +02002849 }
2850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 /*
2852 * This type of quotas was turned off, so ignore this record.
2853 */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10002854 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 ASSERT(type);
2856 if (log->l_quotaoffs_flag & type)
Eric Sandeend99831f2014-06-22 15:03:54 +10002857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858
2859 /*
2860 * At this point we know that quota was _not_ turned off.
2861 * Since the mount flags are not indicating to us otherwise, this
2862 * must mean that quota is on, and the dquot needs to be replayed.
2863 * Remember that we may not have fully recovered the superblock yet,
2864 * so we can't do the usual trick of looking at the SB quota bits.
2865 *
2866 * The other possibility, of course, is that the quota subsystem was
2867 * removed since the last mount - ENOSYS.
2868 */
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002869 dq_f = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 ASSERT(dq_f);
Dave Chinner9aede1d2013-10-15 09:17:52 +11002871 error = xfs_dqcheck(mp, recddq, dq_f->qlf_id, 0, XFS_QMOPT_DOWARN,
Dave Chinnera0fa2b62011-03-07 10:01:35 +11002872 "xlog_recover_dquot_pass2 (log copy)");
2873 if (error)
Dave Chinner24513372014-06-25 14:58:08 +10002874 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 ASSERT(dq_f->qlf_len == 1);
2876
Dave Chinnerad3714b2014-08-04 12:59:31 +10002877 /*
2878 * At this point we are assuming that the dquots have been allocated
2879 * and hence the buffer has valid dquots stamped in it. It should,
2880 * therefore, pass verifier validation. If the dquot is bad, then the
2881 * we'll return an error here, so we don't need to specifically check
2882 * the dquot in the buffer after the verifier has run.
2883 */
Dave Chinner7ca790a2012-04-23 15:58:55 +10002884 error = xfs_trans_read_buf(mp, NULL, mp->m_ddev_targp, dq_f->qlf_blkno,
Dave Chinnerc3f8fc72012-11-12 22:54:01 +11002885 XFS_FSB_TO_BB(mp, dq_f->qlf_len), 0, &bp,
Dave Chinnerad3714b2014-08-04 12:59:31 +10002886 &xfs_dquot_buf_ops);
Dave Chinner7ca790a2012-04-23 15:58:55 +10002887 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 return error;
Dave Chinner7ca790a2012-04-23 15:58:55 +10002889
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 ASSERT(bp);
2891 ddq = (xfs_disk_dquot_t *)xfs_buf_offset(bp, dq_f->qlf_boffset);
2892
2893 /*
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002894 * If the dquot has an LSN in it, recover the dquot only if it's less
2895 * than the lsn of the transaction we are replaying.
2896 */
2897 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2898 struct xfs_dqblk *dqb = (struct xfs_dqblk *)ddq;
2899 xfs_lsn_t lsn = be64_to_cpu(dqb->dd_lsn);
2900
2901 if (lsn && lsn != -1 && XFS_LSN_CMP(lsn, current_lsn) >= 0) {
2902 goto out_release;
2903 }
2904 }
2905
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 memcpy(ddq, recddq, item->ri_buf[1].i_len);
Dave Chinner6fcdc592013-06-03 15:28:46 +10002907 if (xfs_sb_version_hascrc(&mp->m_sb)) {
2908 xfs_update_cksum((char *)ddq, sizeof(struct xfs_dqblk),
2909 XFS_DQUOT_CRC_OFF);
2910 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
2912 ASSERT(dq_f->qlf_size == 2);
Dave Chinnerebad8612010-09-22 10:47:20 +10002913 ASSERT(bp->b_target->bt_mount == mp);
Christoph Hellwigcb669ca2011-07-13 13:43:49 +02002914 bp->b_iodone = xlog_recover_iodone;
Christoph Hellwig43ff2122012-04-23 15:58:39 +10002915 xfs_buf_delwri_queue(bp, buffer_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
Dave Chinner50d5c8d2013-08-28 21:22:47 +10002917out_release:
2918 xfs_buf_relse(bp);
2919 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920}
2921
2922/*
2923 * This routine is called to create an in-core extent free intent
2924 * item from the efi format structure which was logged on disk.
2925 * It allocates an in-core efi, copies the extents from the format
2926 * structure into it, and adds the efi to the AIL with the given
2927 * LSN.
2928 */
Tim Shimmin6d192a92006-06-09 14:55:38 +10002929STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002930xlog_recover_efi_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002931 struct xlog *log,
2932 struct xlog_recover_item *item,
2933 xfs_lsn_t lsn)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Tim Shimmin6d192a92006-06-09 14:55:38 +10002935 int error;
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002936 xfs_mount_t *mp = log->l_mp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 xfs_efi_log_item_t *efip;
2938 xfs_efi_log_format_t *efi_formatp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002940 efi_formatp = item->ri_buf[0].i_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 efip = xfs_efi_init(mp, efi_formatp->efi_nextents);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002943 if ((error = xfs_efi_copy_format(&(item->ri_buf[0]),
2944 &(efip->efi_format)))) {
2945 xfs_efi_item_free(efip);
2946 return error;
2947 }
Dave Chinnerb199c8a2010-12-20 11:59:49 +11002948 atomic_set(&efip->efi_next_extent, efi_formatp->efi_nextents);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949
David Chinnera9c21c12008-10-30 17:39:35 +11002950 spin_lock(&log->l_ailp->xa_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 /*
David Chinner783a2f62008-10-30 17:39:58 +11002952 * xfs_trans_ail_update() drops the AIL lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
Dave Chinnere6059942010-12-20 12:34:26 +11002954 xfs_trans_ail_update(log->l_ailp, &efip->efi_item, lsn);
Tim Shimmin6d192a92006-06-09 14:55:38 +10002955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956}
2957
2958
2959/*
2960 * This routine is called when an efd format structure is found in
2961 * a committed transaction in the log. It's purpose is to cancel
2962 * the corresponding efi if it was still in the log. To do this
2963 * it searches the AIL for the efi with an id equal to that in the
2964 * efd format structure. If we find it, we remove the efi from the
2965 * AIL and free it.
2966 */
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00002967STATIC int
2968xlog_recover_efd_pass2(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05002969 struct xlog *log,
2970 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 xfs_efd_log_format_t *efd_formatp;
2973 xfs_efi_log_item_t *efip = NULL;
2974 xfs_log_item_t *lip;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 __uint64_t efi_id;
David Chinner27d8d5f2008-10-30 17:38:39 +11002976 struct xfs_ail_cursor cur;
David Chinner783a2f62008-10-30 17:39:58 +11002977 struct xfs_ail *ailp = log->l_ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
Christoph Hellwig4e0d5f92010-06-23 18:11:15 +10002979 efd_formatp = item->ri_buf[0].i_addr;
Tim Shimmin6d192a92006-06-09 14:55:38 +10002980 ASSERT((item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_32_t) +
2981 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_32_t)))) ||
2982 (item->ri_buf[0].i_len == (sizeof(xfs_efd_log_format_64_t) +
2983 ((efd_formatp->efd_nextents - 1) * sizeof(xfs_extent_64_t)))));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 efi_id = efd_formatp->efd_efi_id;
2985
2986 /*
2987 * Search for the efi with the id in the efd format structure
2988 * in the AIL.
2989 */
David Chinnera9c21c12008-10-30 17:39:35 +11002990 spin_lock(&ailp->xa_lock);
2991 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 while (lip != NULL) {
2993 if (lip->li_type == XFS_LI_EFI) {
2994 efip = (xfs_efi_log_item_t *)lip;
2995 if (efip->efi_format.efi_id == efi_id) {
2996 /*
David Chinner783a2f62008-10-30 17:39:58 +11002997 * xfs_trans_ail_delete() drops the
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 * AIL lock.
2999 */
Dave Chinner04913fd2012-04-23 15:58:41 +10003000 xfs_trans_ail_delete(ailp, lip,
3001 SHUTDOWN_CORRUPT_INCORE);
David Chinner8ae2c0f2007-11-23 16:28:17 +11003002 xfs_efi_item_free(efip);
David Chinnera9c21c12008-10-30 17:39:35 +11003003 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11003004 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 }
3006 }
David Chinnera9c21c12008-10-30 17:39:35 +11003007 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 }
Eric Sandeene4a1e292014-04-14 19:06:05 +10003009 xfs_trans_ail_cursor_done(&cur);
David Chinnera9c21c12008-10-30 17:39:35 +11003010 spin_unlock(&ailp->xa_lock);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003011
3012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013}
3014
3015/*
Dave Chinner28c8e412013-06-27 16:04:55 +10003016 * This routine is called when an inode create format structure is found in a
3017 * committed transaction in the log. It's purpose is to initialise the inodes
3018 * being allocated on disk. This requires us to get inode cluster buffers that
3019 * match the range to be intialised, stamped with inode templates and written
3020 * by delayed write so that subsequent modifications will hit the cached buffer
3021 * and only need writing out at the end of recovery.
3022 */
3023STATIC int
3024xlog_recover_do_icreate_pass2(
3025 struct xlog *log,
3026 struct list_head *buffer_list,
3027 xlog_recover_item_t *item)
3028{
3029 struct xfs_mount *mp = log->l_mp;
3030 struct xfs_icreate_log *icl;
3031 xfs_agnumber_t agno;
3032 xfs_agblock_t agbno;
3033 unsigned int count;
3034 unsigned int isize;
3035 xfs_agblock_t length;
3036
3037 icl = (struct xfs_icreate_log *)item->ri_buf[0].i_addr;
3038 if (icl->icl_type != XFS_LI_ICREATE) {
3039 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad type");
Dave Chinner24513372014-06-25 14:58:08 +10003040 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003041 }
3042
3043 if (icl->icl_size != 1) {
3044 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad icl size");
Dave Chinner24513372014-06-25 14:58:08 +10003045 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003046 }
3047
3048 agno = be32_to_cpu(icl->icl_ag);
3049 if (agno >= mp->m_sb.sb_agcount) {
3050 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agno");
Dave Chinner24513372014-06-25 14:58:08 +10003051 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003052 }
3053 agbno = be32_to_cpu(icl->icl_agbno);
3054 if (!agbno || agbno == NULLAGBLOCK || agbno >= mp->m_sb.sb_agblocks) {
3055 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad agbno");
Dave Chinner24513372014-06-25 14:58:08 +10003056 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003057 }
3058 isize = be32_to_cpu(icl->icl_isize);
3059 if (isize != mp->m_sb.sb_inodesize) {
3060 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad isize");
Dave Chinner24513372014-06-25 14:58:08 +10003061 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003062 }
3063 count = be32_to_cpu(icl->icl_count);
3064 if (!count) {
3065 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count");
Dave Chinner24513372014-06-25 14:58:08 +10003066 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003067 }
3068 length = be32_to_cpu(icl->icl_length);
3069 if (!length || length >= mp->m_sb.sb_agblocks) {
3070 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad length");
Dave Chinner24513372014-06-25 14:58:08 +10003071 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003072 }
3073
3074 /* existing allocation is fixed value */
Jie Liu71783432013-12-13 15:51:46 +11003075 ASSERT(count == mp->m_ialloc_inos);
Jie Liu126cd102013-12-13 15:51:48 +11003076 ASSERT(length == mp->m_ialloc_blks);
Jie Liu71783432013-12-13 15:51:46 +11003077 if (count != mp->m_ialloc_inos ||
Jie Liu126cd102013-12-13 15:51:48 +11003078 length != mp->m_ialloc_blks) {
Dave Chinner28c8e412013-06-27 16:04:55 +10003079 xfs_warn(log->l_mp, "xlog_recover_do_icreate_trans: bad count 2");
Dave Chinner24513372014-06-25 14:58:08 +10003080 return -EINVAL;
Dave Chinner28c8e412013-06-27 16:04:55 +10003081 }
3082
3083 /*
3084 * Inode buffers can be freed. Do not replay the inode initialisation as
3085 * we could be overwriting something written after this inode buffer was
3086 * cancelled.
3087 *
3088 * XXX: we need to iterate all buffers and only init those that are not
3089 * cancelled. I think that a more fine grained factoring of
3090 * xfs_ialloc_inode_init may be appropriate here to enable this to be
3091 * done easily.
3092 */
3093 if (xlog_check_buffer_cancelled(log,
3094 XFS_AGB_TO_DADDR(mp, agno, agbno), length, 0))
3095 return 0;
3096
3097 xfs_ialloc_inode_init(mp, NULL, buffer_list, agno, agbno, length,
3098 be32_to_cpu(icl->icl_gen));
3099 return 0;
3100}
3101
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003102STATIC void
3103xlog_recover_buffer_ra_pass2(
3104 struct xlog *log,
3105 struct xlog_recover_item *item)
3106{
3107 struct xfs_buf_log_format *buf_f = item->ri_buf[0].i_addr;
3108 struct xfs_mount *mp = log->l_mp;
3109
Dave Chinner84a5b732013-08-27 08:10:53 +10003110 if (xlog_peek_buffer_cancelled(log, buf_f->blf_blkno,
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003111 buf_f->blf_len, buf_f->blf_flags)) {
3112 return;
3113 }
3114
3115 xfs_buf_readahead(mp->m_ddev_targp, buf_f->blf_blkno,
3116 buf_f->blf_len, NULL);
3117}
3118
3119STATIC void
3120xlog_recover_inode_ra_pass2(
3121 struct xlog *log,
3122 struct xlog_recover_item *item)
3123{
3124 struct xfs_inode_log_format ilf_buf;
3125 struct xfs_inode_log_format *ilfp;
3126 struct xfs_mount *mp = log->l_mp;
3127 int error;
3128
3129 if (item->ri_buf[0].i_len == sizeof(struct xfs_inode_log_format)) {
3130 ilfp = item->ri_buf[0].i_addr;
3131 } else {
3132 ilfp = &ilf_buf;
3133 memset(ilfp, 0, sizeof(*ilfp));
3134 error = xfs_inode_item_format_convert(&item->ri_buf[0], ilfp);
3135 if (error)
3136 return;
3137 }
3138
Dave Chinner84a5b732013-08-27 08:10:53 +10003139 if (xlog_peek_buffer_cancelled(log, ilfp->ilf_blkno, ilfp->ilf_len, 0))
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003140 return;
3141
3142 xfs_buf_readahead(mp->m_ddev_targp, ilfp->ilf_blkno,
Dave Chinnerd8914002013-08-27 11:39:37 +10003143 ilfp->ilf_len, &xfs_inode_buf_ra_ops);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003144}
3145
3146STATIC void
3147xlog_recover_dquot_ra_pass2(
3148 struct xlog *log,
3149 struct xlog_recover_item *item)
3150{
3151 struct xfs_mount *mp = log->l_mp;
3152 struct xfs_disk_dquot *recddq;
3153 struct xfs_dq_logformat *dq_f;
3154 uint type;
3155
3156
3157 if (mp->m_qflags == 0)
3158 return;
3159
3160 recddq = item->ri_buf[1].i_addr;
3161 if (recddq == NULL)
3162 return;
3163 if (item->ri_buf[1].i_len < sizeof(struct xfs_disk_dquot))
3164 return;
3165
3166 type = recddq->d_flags & (XFS_DQ_USER | XFS_DQ_PROJ | XFS_DQ_GROUP);
3167 ASSERT(type);
3168 if (log->l_quotaoffs_flag & type)
3169 return;
3170
3171 dq_f = item->ri_buf[0].i_addr;
3172 ASSERT(dq_f);
3173 ASSERT(dq_f->qlf_len == 1);
3174
3175 xfs_buf_readahead(mp->m_ddev_targp, dq_f->qlf_blkno,
Dave Chinner0f0d3342013-08-27 13:25:43 +10003176 XFS_FSB_TO_BB(mp, dq_f->qlf_len), NULL);
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003177}
3178
3179STATIC void
3180xlog_recover_ra_pass2(
3181 struct xlog *log,
3182 struct xlog_recover_item *item)
3183{
3184 switch (ITEM_TYPE(item)) {
3185 case XFS_LI_BUF:
3186 xlog_recover_buffer_ra_pass2(log, item);
3187 break;
3188 case XFS_LI_INODE:
3189 xlog_recover_inode_ra_pass2(log, item);
3190 break;
3191 case XFS_LI_DQUOT:
3192 xlog_recover_dquot_ra_pass2(log, item);
3193 break;
3194 case XFS_LI_EFI:
3195 case XFS_LI_EFD:
3196 case XFS_LI_QUOTAOFF:
3197 default:
3198 break;
3199 }
3200}
3201
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202STATIC int
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003203xlog_recover_commit_pass1(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003204 struct xlog *log,
3205 struct xlog_recover *trans,
3206 struct xlog_recover_item *item)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207{
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003208 trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS1);
Christoph Hellwigd0450942010-12-01 22:06:23 +00003209
3210 switch (ITEM_TYPE(item)) {
3211 case XFS_LI_BUF:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003212 return xlog_recover_buffer_pass1(log, item);
Christoph Hellwigd0450942010-12-01 22:06:23 +00003213 case XFS_LI_QUOTAOFF:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003214 return xlog_recover_quotaoff_pass1(log, item);
3215 case XFS_LI_INODE:
3216 case XFS_LI_EFI:
3217 case XFS_LI_EFD:
3218 case XFS_LI_DQUOT:
Dave Chinner28c8e412013-06-27 16:04:55 +10003219 case XFS_LI_ICREATE:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003220 /* nothing to do in pass 1 */
3221 return 0;
Christoph Hellwigd0450942010-12-01 22:06:23 +00003222 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003223 xfs_warn(log->l_mp, "%s: invalid item type (%d)",
3224 __func__, ITEM_TYPE(item));
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003225 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10003226 return -EIO;
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003227 }
3228}
3229
3230STATIC int
3231xlog_recover_commit_pass2(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003232 struct xlog *log,
3233 struct xlog_recover *trans,
3234 struct list_head *buffer_list,
3235 struct xlog_recover_item *item)
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003236{
3237 trace_xfs_log_recover_item_recover(log, trans, item, XLOG_RECOVER_PASS2);
3238
3239 switch (ITEM_TYPE(item)) {
3240 case XFS_LI_BUF:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003241 return xlog_recover_buffer_pass2(log, buffer_list, item,
3242 trans->r_lsn);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003243 case XFS_LI_INODE:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003244 return xlog_recover_inode_pass2(log, buffer_list, item,
3245 trans->r_lsn);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003246 case XFS_LI_EFI:
3247 return xlog_recover_efi_pass2(log, item, trans->r_lsn);
3248 case XFS_LI_EFD:
3249 return xlog_recover_efd_pass2(log, item);
3250 case XFS_LI_DQUOT:
Dave Chinner50d5c8d2013-08-28 21:22:47 +10003251 return xlog_recover_dquot_pass2(log, buffer_list, item,
3252 trans->r_lsn);
Dave Chinner28c8e412013-06-27 16:04:55 +10003253 case XFS_LI_ICREATE:
3254 return xlog_recover_do_icreate_pass2(log, buffer_list, item);
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003255 case XFS_LI_QUOTAOFF:
3256 /* nothing to do in pass2 */
3257 return 0;
3258 default:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003259 xfs_warn(log->l_mp, "%s: invalid item type (%d)",
3260 __func__, ITEM_TYPE(item));
Christoph Hellwigd0450942010-12-01 22:06:23 +00003261 ASSERT(0);
Dave Chinner24513372014-06-25 14:58:08 +10003262 return -EIO;
Christoph Hellwigd0450942010-12-01 22:06:23 +00003263 }
3264}
3265
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003266STATIC int
3267xlog_recover_items_pass2(
3268 struct xlog *log,
3269 struct xlog_recover *trans,
3270 struct list_head *buffer_list,
3271 struct list_head *item_list)
3272{
3273 struct xlog_recover_item *item;
3274 int error = 0;
3275
3276 list_for_each_entry(item, item_list, ri_list) {
3277 error = xlog_recover_commit_pass2(log, trans,
3278 buffer_list, item);
3279 if (error)
3280 return error;
3281 }
3282
3283 return error;
3284}
3285
Christoph Hellwigd0450942010-12-01 22:06:23 +00003286/*
3287 * Perform the transaction.
3288 *
3289 * If the transaction modifies a buffer or inode, do it now. Otherwise,
3290 * EFIs and EFDs get queued up by adding entries into the AIL for them.
3291 */
3292STATIC int
3293xlog_recover_commit_trans(
Mark Tinguelyad223e62012-06-14 09:22:15 -05003294 struct xlog *log,
Christoph Hellwigd0450942010-12-01 22:06:23 +00003295 struct xlog_recover *trans,
3296 int pass)
3297{
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003298 int error = 0;
3299 int error2;
3300 int items_queued = 0;
3301 struct xlog_recover_item *item;
3302 struct xlog_recover_item *next;
3303 LIST_HEAD (buffer_list);
3304 LIST_HEAD (ra_list);
3305 LIST_HEAD (done_list);
3306
3307 #define XLOG_RECOVER_COMMIT_QUEUE_MAX 100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308
Dave Chinnerf0a76952010-01-11 11:49:57 +00003309 hlist_del(&trans->r_list);
Christoph Hellwigd0450942010-12-01 22:06:23 +00003310
3311 error = xlog_recover_reorder_trans(log, trans, pass);
3312 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 return error;
Christoph Hellwigd0450942010-12-01 22:06:23 +00003314
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003315 list_for_each_entry_safe(item, next, &trans->r_itemq, ri_list) {
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003316 switch (pass) {
3317 case XLOG_RECOVER_PASS1:
Christoph Hellwigc9f71f52010-12-01 22:06:24 +00003318 error = xlog_recover_commit_pass1(log, trans, item);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003319 break;
3320 case XLOG_RECOVER_PASS2:
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003321 xlog_recover_ra_pass2(log, item);
3322 list_move_tail(&item->ri_list, &ra_list);
3323 items_queued++;
3324 if (items_queued >= XLOG_RECOVER_COMMIT_QUEUE_MAX) {
3325 error = xlog_recover_items_pass2(log, trans,
3326 &buffer_list, &ra_list);
3327 list_splice_tail_init(&ra_list, &done_list);
3328 items_queued = 0;
3329 }
3330
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003331 break;
3332 default:
3333 ASSERT(0);
3334 }
3335
Christoph Hellwigd0450942010-12-01 22:06:23 +00003336 if (error)
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003337 goto out;
Christoph Hellwigd0450942010-12-01 22:06:23 +00003338 }
3339
Zhi Yong Wu00574da2013-08-14 15:16:03 +08003340out:
3341 if (!list_empty(&ra_list)) {
3342 if (!error)
3343 error = xlog_recover_items_pass2(log, trans,
3344 &buffer_list, &ra_list);
3345 list_splice_tail_init(&ra_list, &done_list);
3346 }
3347
3348 if (!list_empty(&done_list))
3349 list_splice_init(&done_list, &trans->r_itemq);
3350
Christoph Hellwig43ff2122012-04-23 15:58:39 +10003351 error2 = xfs_buf_delwri_submit(&buffer_list);
3352 return error ? error : error2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353}
3354
Dave Chinner76560662014-09-29 09:45:42 +10003355STATIC void
3356xlog_recover_add_item(
3357 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358{
Dave Chinner76560662014-09-29 09:45:42 +10003359 xlog_recover_item_t *item;
3360
3361 item = kmem_zalloc(sizeof(xlog_recover_item_t), KM_SLEEP);
3362 INIT_LIST_HEAD(&item->ri_list);
3363 list_add_tail(&item->ri_list, head);
3364}
3365
3366STATIC int
3367xlog_recover_add_to_cont_trans(
3368 struct xlog *log,
3369 struct xlog_recover *trans,
3370 xfs_caddr_t dp,
3371 int len)
3372{
3373 xlog_recover_item_t *item;
3374 xfs_caddr_t ptr, old_ptr;
3375 int old_len;
3376
3377 if (list_empty(&trans->r_itemq)) {
3378 /* finish copying rest of trans header */
3379 xlog_recover_add_item(&trans->r_itemq);
3380 ptr = (xfs_caddr_t) &trans->r_theader +
3381 sizeof(xfs_trans_header_t) - len;
3382 memcpy(ptr, dp, len);
3383 return 0;
3384 }
3385 /* take the tail entry */
3386 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
3387
3388 old_ptr = item->ri_buf[item->ri_cnt-1].i_addr;
3389 old_len = item->ri_buf[item->ri_cnt-1].i_len;
3390
3391 ptr = kmem_realloc(old_ptr, len+old_len, old_len, KM_SLEEP);
3392 memcpy(&ptr[old_len], dp, len);
3393 item->ri_buf[item->ri_cnt-1].i_len += len;
3394 item->ri_buf[item->ri_cnt-1].i_addr = ptr;
3395 trace_xfs_log_recover_item_add_cont(log, trans, item, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 return 0;
3397}
3398
3399/*
Dave Chinner76560662014-09-29 09:45:42 +10003400 * The next region to add is the start of a new region. It could be
3401 * a whole region or it could be the first part of a new region. Because
3402 * of this, the assumption here is that the type and size fields of all
3403 * format structures fit into the first 32 bits of the structure.
3404 *
3405 * This works because all regions must be 32 bit aligned. Therefore, we
3406 * either have both fields or we have neither field. In the case we have
3407 * neither field, the data part of the region is zero length. We only have
3408 * a log_op_header and can throw away the header since a new one will appear
3409 * later. If we have at least 4 bytes, then we can determine how many regions
3410 * will appear in the current log item.
3411 */
3412STATIC int
3413xlog_recover_add_to_trans(
3414 struct xlog *log,
3415 struct xlog_recover *trans,
3416 xfs_caddr_t dp,
3417 int len)
3418{
3419 xfs_inode_log_format_t *in_f; /* any will do */
3420 xlog_recover_item_t *item;
3421 xfs_caddr_t ptr;
3422
3423 if (!len)
3424 return 0;
3425 if (list_empty(&trans->r_itemq)) {
3426 /* we need to catch log corruptions here */
3427 if (*(uint *)dp != XFS_TRANS_HEADER_MAGIC) {
3428 xfs_warn(log->l_mp, "%s: bad header magic number",
3429 __func__);
3430 ASSERT(0);
3431 return -EIO;
3432 }
3433 if (len == sizeof(xfs_trans_header_t))
3434 xlog_recover_add_item(&trans->r_itemq);
3435 memcpy(&trans->r_theader, dp, len);
3436 return 0;
3437 }
3438
3439 ptr = kmem_alloc(len, KM_SLEEP);
3440 memcpy(ptr, dp, len);
3441 in_f = (xfs_inode_log_format_t *)ptr;
3442
3443 /* take the tail entry */
3444 item = list_entry(trans->r_itemq.prev, xlog_recover_item_t, ri_list);
3445 if (item->ri_total != 0 &&
3446 item->ri_total == item->ri_cnt) {
3447 /* tail item is in use, get a new one */
3448 xlog_recover_add_item(&trans->r_itemq);
3449 item = list_entry(trans->r_itemq.prev,
3450 xlog_recover_item_t, ri_list);
3451 }
3452
3453 if (item->ri_total == 0) { /* first region to be added */
3454 if (in_f->ilf_size == 0 ||
3455 in_f->ilf_size > XLOG_MAX_REGIONS_IN_ITEM) {
3456 xfs_warn(log->l_mp,
3457 "bad number of regions (%d) in inode log format",
3458 in_f->ilf_size);
3459 ASSERT(0);
3460 kmem_free(ptr);
3461 return -EIO;
3462 }
3463
3464 item->ri_total = in_f->ilf_size;
3465 item->ri_buf =
3466 kmem_zalloc(item->ri_total * sizeof(xfs_log_iovec_t),
3467 KM_SLEEP);
3468 }
3469 ASSERT(item->ri_total > item->ri_cnt);
3470 /* Description region is ri_buf[0] */
3471 item->ri_buf[item->ri_cnt].i_addr = ptr;
3472 item->ri_buf[item->ri_cnt].i_len = len;
3473 item->ri_cnt++;
3474 trace_xfs_log_recover_item_add(log, trans, item, 0);
3475 return 0;
3476}
Dave Chinnerb818cca2014-09-29 09:45:54 +10003477
Dave Chinner76560662014-09-29 09:45:42 +10003478/*
3479 * Free up any resources allocated by the transaction
3480 *
3481 * Remember that EFIs, EFDs, and IUNLINKs are handled later.
3482 */
3483STATIC void
3484xlog_recover_free_trans(
3485 struct xlog_recover *trans)
3486{
3487 xlog_recover_item_t *item, *n;
3488 int i;
3489
3490 list_for_each_entry_safe(item, n, &trans->r_itemq, ri_list) {
3491 /* Free the regions in the item. */
3492 list_del(&item->ri_list);
3493 for (i = 0; i < item->ri_cnt; i++)
3494 kmem_free(item->ri_buf[i].i_addr);
3495 /* Free the item itself */
3496 kmem_free(item->ri_buf);
3497 kmem_free(item);
3498 }
3499 /* Free the transaction recover structure */
3500 kmem_free(trans);
3501}
3502
Dave Chinnere9131e502014-09-29 09:45:18 +10003503/*
3504 * On error or completion, trans is freed.
3505 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506STATIC int
Dave Chinnereeb11682014-09-29 09:45:03 +10003507xlog_recovery_process_trans(
3508 struct xlog *log,
3509 struct xlog_recover *trans,
3510 xfs_caddr_t dp,
3511 unsigned int len,
3512 unsigned int flags,
3513 int pass)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Dave Chinnere9131e502014-09-29 09:45:18 +10003515 int error = 0;
3516 bool freeit = false;
Dave Chinnereeb11682014-09-29 09:45:03 +10003517
3518 /* mask off ophdr transaction container flags */
3519 flags &= ~XLOG_END_TRANS;
3520 if (flags & XLOG_WAS_CONT_TRANS)
3521 flags &= ~XLOG_CONTINUE_TRANS;
3522
Dave Chinner88b863d2014-09-29 09:45:32 +10003523 /*
3524 * Callees must not free the trans structure. We'll decide if we need to
3525 * free it or not based on the operation being done and it's result.
3526 */
Dave Chinnereeb11682014-09-29 09:45:03 +10003527 switch (flags) {
3528 /* expected flag values */
3529 case 0:
3530 case XLOG_CONTINUE_TRANS:
3531 error = xlog_recover_add_to_trans(log, trans, dp, len);
3532 break;
3533 case XLOG_WAS_CONT_TRANS:
3534 error = xlog_recover_add_to_cont_trans(log, trans, dp, len);
3535 break;
3536 case XLOG_COMMIT_TRANS:
3537 error = xlog_recover_commit_trans(log, trans, pass);
Dave Chinner88b863d2014-09-29 09:45:32 +10003538 /* success or fail, we are now done with this transaction. */
3539 freeit = true;
Dave Chinnereeb11682014-09-29 09:45:03 +10003540 break;
3541
3542 /* unexpected flag values */
3543 case XLOG_UNMOUNT_TRANS:
Dave Chinnere9131e502014-09-29 09:45:18 +10003544 /* just skip trans */
Dave Chinnereeb11682014-09-29 09:45:03 +10003545 xfs_warn(log->l_mp, "%s: Unmount LR", __func__);
Dave Chinnere9131e502014-09-29 09:45:18 +10003546 freeit = true;
Dave Chinnereeb11682014-09-29 09:45:03 +10003547 break;
3548 case XLOG_START_TRANS:
Dave Chinnereeb11682014-09-29 09:45:03 +10003549 default:
3550 xfs_warn(log->l_mp, "%s: bad flag 0x%x", __func__, flags);
3551 ASSERT(0);
Dave Chinnere9131e502014-09-29 09:45:18 +10003552 error = -EIO;
Dave Chinnereeb11682014-09-29 09:45:03 +10003553 break;
3554 }
Dave Chinnere9131e502014-09-29 09:45:18 +10003555 if (error || freeit)
3556 xlog_recover_free_trans(trans);
Dave Chinnereeb11682014-09-29 09:45:03 +10003557 return error;
3558}
3559
Dave Chinnerb818cca2014-09-29 09:45:54 +10003560/*
3561 * Lookup the transaction recovery structure associated with the ID in the
3562 * current ophdr. If the transaction doesn't exist and the start flag is set in
3563 * the ophdr, then allocate a new transaction for future ID matches to find.
3564 * Either way, return what we found during the lookup - an existing transaction
3565 * or nothing.
3566 */
Dave Chinnereeb11682014-09-29 09:45:03 +10003567STATIC struct xlog_recover *
3568xlog_recover_ophdr_to_trans(
3569 struct hlist_head rhash[],
3570 struct xlog_rec_header *rhead,
3571 struct xlog_op_header *ohead)
3572{
3573 struct xlog_recover *trans;
3574 xlog_tid_t tid;
3575 struct hlist_head *rhp;
3576
3577 tid = be32_to_cpu(ohead->oh_tid);
3578 rhp = &rhash[XLOG_RHASH(tid)];
Dave Chinnerb818cca2014-09-29 09:45:54 +10003579 hlist_for_each_entry(trans, rhp, r_list) {
3580 if (trans->r_log_tid == tid)
3581 return trans;
3582 }
Dave Chinnereeb11682014-09-29 09:45:03 +10003583
3584 /*
Dave Chinnerb818cca2014-09-29 09:45:54 +10003585 * skip over non-start transaction headers - we could be
3586 * processing slack space before the next transaction starts
Dave Chinnereeb11682014-09-29 09:45:03 +10003587 */
Dave Chinnerb818cca2014-09-29 09:45:54 +10003588 if (!(ohead->oh_flags & XLOG_START_TRANS))
3589 return NULL;
3590
3591 ASSERT(be32_to_cpu(ohead->oh_len) == 0);
3592
3593 /*
3594 * This is a new transaction so allocate a new recovery container to
3595 * hold the recovery ops that will follow.
3596 */
3597 trans = kmem_zalloc(sizeof(struct xlog_recover), KM_SLEEP);
3598 trans->r_log_tid = tid;
3599 trans->r_lsn = be64_to_cpu(rhead->h_lsn);
3600 INIT_LIST_HEAD(&trans->r_itemq);
3601 INIT_HLIST_NODE(&trans->r_list);
3602 hlist_add_head(&trans->r_list, rhp);
3603
3604 /*
3605 * Nothing more to do for this ophdr. Items to be added to this new
3606 * transaction will be in subsequent ophdr containers.
3607 */
Dave Chinnereeb11682014-09-29 09:45:03 +10003608 return NULL;
3609}
3610
3611STATIC int
3612xlog_recover_process_ophdr(
3613 struct xlog *log,
3614 struct hlist_head rhash[],
3615 struct xlog_rec_header *rhead,
3616 struct xlog_op_header *ohead,
3617 xfs_caddr_t dp,
3618 xfs_caddr_t end,
3619 int pass)
3620{
3621 struct xlog_recover *trans;
Dave Chinnereeb11682014-09-29 09:45:03 +10003622 unsigned int len;
3623
3624 /* Do we understand who wrote this op? */
3625 if (ohead->oh_clientid != XFS_TRANSACTION &&
3626 ohead->oh_clientid != XFS_LOG) {
3627 xfs_warn(log->l_mp, "%s: bad clientid 0x%x",
3628 __func__, ohead->oh_clientid);
3629 ASSERT(0);
3630 return -EIO;
3631 }
3632
3633 /*
3634 * Check the ophdr contains all the data it is supposed to contain.
3635 */
3636 len = be32_to_cpu(ohead->oh_len);
3637 if (dp + len > end) {
3638 xfs_warn(log->l_mp, "%s: bad length 0x%x", __func__, len);
3639 WARN_ON(1);
3640 return -EIO;
3641 }
3642
3643 trans = xlog_recover_ophdr_to_trans(rhash, rhead, ohead);
3644 if (!trans) {
3645 /* nothing to do, so skip over this ophdr */
3646 return 0;
3647 }
3648
Dave Chinnere9131e502014-09-29 09:45:18 +10003649 return xlog_recovery_process_trans(log, trans, dp, len,
3650 ohead->oh_flags, pass);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651}
3652
3653/*
3654 * There are two valid states of the r_state field. 0 indicates that the
3655 * transaction structure is in a normal state. We have either seen the
3656 * start of the transaction or the last operation we added was not a partial
3657 * operation. If the last operation we added to the transaction was a
3658 * partial operation, we need to mark r_state with XLOG_WAS_CONT_TRANS.
3659 *
3660 * NOTE: skip LRs with 0 data length.
3661 */
3662STATIC int
3663xlog_recover_process_data(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003664 struct xlog *log,
Dave Chinnerf0a76952010-01-11 11:49:57 +00003665 struct hlist_head rhash[],
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003666 struct xlog_rec_header *rhead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 xfs_caddr_t dp,
3668 int pass)
3669{
Dave Chinnereeb11682014-09-29 09:45:03 +10003670 struct xlog_op_header *ohead;
3671 xfs_caddr_t end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 int num_logops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Dave Chinnereeb11682014-09-29 09:45:03 +10003675 end = dp + be32_to_cpu(rhead->h_len);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10003676 num_logops = be32_to_cpu(rhead->h_num_logops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677
3678 /* check the log format matches our own - else we can't recover */
3679 if (xlog_header_check_recover(log->l_mp, rhead))
Dave Chinner24513372014-06-25 14:58:08 +10003680 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
Dave Chinnereeb11682014-09-29 09:45:03 +10003682 while ((dp < end) && num_logops) {
3683
3684 ohead = (struct xlog_op_header *)dp;
3685 dp += sizeof(*ohead);
3686 ASSERT(dp <= end);
3687
3688 /* errors will abort recovery */
3689 error = xlog_recover_process_ophdr(log, rhash, rhead, ohead,
3690 dp, end, pass);
3691 if (error)
3692 return error;
3693
Christoph Hellwig67fcb7b2007-10-12 10:58:59 +10003694 dp += be32_to_cpu(ohead->oh_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 num_logops--;
3696 }
3697 return 0;
3698}
3699
3700/*
3701 * Process an extent free intent item that was recovered from
3702 * the log. We need to free the extents that it describes.
3703 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10003704STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705xlog_recover_process_efi(
3706 xfs_mount_t *mp,
3707 xfs_efi_log_item_t *efip)
3708{
3709 xfs_efd_log_item_t *efdp;
3710 xfs_trans_t *tp;
3711 int i;
David Chinner3c1e2bb2008-04-10 12:21:11 +10003712 int error = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 xfs_extent_t *extp;
3714 xfs_fsblock_t startblock_fsb;
3715
Dave Chinnerb199c8a2010-12-20 11:59:49 +11003716 ASSERT(!test_bit(XFS_EFI_RECOVERED, &efip->efi_flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
3718 /*
3719 * First check the validity of the extents described by the
3720 * EFI. If any are bad, then assume that all are bad and
3721 * just toss the EFI.
3722 */
3723 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3724 extp = &(efip->efi_format.efi_extents[i]);
3725 startblock_fsb = XFS_BB_TO_FSB(mp,
3726 XFS_FSB_TO_DADDR(mp, extp->ext_start));
3727 if ((startblock_fsb == 0) ||
3728 (extp->ext_len == 0) ||
3729 (startblock_fsb >= mp->m_sb.sb_dblocks) ||
3730 (extp->ext_len >= mp->m_sb.sb_agblocks)) {
3731 /*
3732 * This will pull the EFI from the AIL and
3733 * free the memory associated with it.
3734 */
Dave Chinner666d6442013-04-03 14:09:21 +11003735 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 xfs_efi_release(efip, efip->efi_format.efi_nextents);
Dave Chinner24513372014-06-25 14:58:08 +10003737 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 }
3739 }
3740
3741 tp = xfs_trans_alloc(mp, 0);
Jie Liu3d3c8b52013-08-12 20:49:59 +10003742 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
David Chinnerfc6149d2008-04-10 12:21:53 +10003743 if (error)
3744 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003745 efdp = xfs_trans_get_efd(tp, efip, efip->efi_format.efi_nextents);
3746
3747 for (i = 0; i < efip->efi_format.efi_nextents; i++) {
3748 extp = &(efip->efi_format.efi_extents[i]);
David Chinnerfc6149d2008-04-10 12:21:53 +10003749 error = xfs_free_extent(tp, extp->ext_start, extp->ext_len);
3750 if (error)
3751 goto abort_error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752 xfs_trans_log_efd_extent(tp, efdp, extp->ext_start,
3753 extp->ext_len);
3754 }
3755
Dave Chinnerb199c8a2010-12-20 11:59:49 +11003756 set_bit(XFS_EFI_RECOVERED, &efip->efi_flags);
David Chinnere5720ee2008-04-10 12:21:18 +10003757 error = xfs_trans_commit(tp, 0);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003758 return error;
David Chinnerfc6149d2008-04-10 12:21:53 +10003759
3760abort_error:
3761 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3762 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763}
3764
3765/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 * When this is called, all of the EFIs which did not have
3767 * corresponding EFDs should be in the AIL. What we do now
3768 * is free the extents associated with each one.
3769 *
3770 * Since we process the EFIs in normal transactions, they
3771 * will be removed at some point after the commit. This prevents
3772 * us from just walking down the list processing each one.
3773 * We'll use a flag in the EFI to skip those that we've already
3774 * processed and use the AIL iteration mechanism's generation
3775 * count to try to speed this up at least a bit.
3776 *
3777 * When we start, we know that the EFIs are the only things in
3778 * the AIL. As we process them, however, other items are added
3779 * to the AIL. Since everything added to the AIL must come after
3780 * everything already in the AIL, we stop processing as soon as
3781 * we see something other than an EFI in the AIL.
3782 */
David Chinner3c1e2bb2008-04-10 12:21:11 +10003783STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784xlog_recover_process_efis(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003785 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003786{
3787 xfs_log_item_t *lip;
3788 xfs_efi_log_item_t *efip;
David Chinner3c1e2bb2008-04-10 12:21:11 +10003789 int error = 0;
David Chinner27d8d5f2008-10-30 17:38:39 +11003790 struct xfs_ail_cursor cur;
David Chinnera9c21c12008-10-30 17:39:35 +11003791 struct xfs_ail *ailp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
David Chinnera9c21c12008-10-30 17:39:35 +11003793 ailp = log->l_ailp;
3794 spin_lock(&ailp->xa_lock);
3795 lip = xfs_trans_ail_cursor_first(ailp, &cur, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 while (lip != NULL) {
3797 /*
3798 * We're done when we see something other than an EFI.
David Chinner27d8d5f2008-10-30 17:38:39 +11003799 * There should be no EFIs left in the AIL now.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800 */
3801 if (lip->li_type != XFS_LI_EFI) {
David Chinner27d8d5f2008-10-30 17:38:39 +11003802#ifdef DEBUG
David Chinnera9c21c12008-10-30 17:39:35 +11003803 for (; lip; lip = xfs_trans_ail_cursor_next(ailp, &cur))
David Chinner27d8d5f2008-10-30 17:38:39 +11003804 ASSERT(lip->li_type != XFS_LI_EFI);
3805#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806 break;
3807 }
3808
3809 /*
3810 * Skip EFIs that we've already processed.
3811 */
3812 efip = (xfs_efi_log_item_t *)lip;
Dave Chinnerb199c8a2010-12-20 11:59:49 +11003813 if (test_bit(XFS_EFI_RECOVERED, &efip->efi_flags)) {
David Chinnera9c21c12008-10-30 17:39:35 +11003814 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 continue;
3816 }
3817
David Chinnera9c21c12008-10-30 17:39:35 +11003818 spin_unlock(&ailp->xa_lock);
3819 error = xlog_recover_process_efi(log->l_mp, efip);
3820 spin_lock(&ailp->xa_lock);
David Chinner27d8d5f2008-10-30 17:38:39 +11003821 if (error)
3822 goto out;
David Chinnera9c21c12008-10-30 17:39:35 +11003823 lip = xfs_trans_ail_cursor_next(ailp, &cur);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 }
David Chinner27d8d5f2008-10-30 17:38:39 +11003825out:
Eric Sandeene4a1e292014-04-14 19:06:05 +10003826 xfs_trans_ail_cursor_done(&cur);
David Chinnera9c21c12008-10-30 17:39:35 +11003827 spin_unlock(&ailp->xa_lock);
David Chinner3c1e2bb2008-04-10 12:21:11 +10003828 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829}
3830
3831/*
3832 * This routine performs a transaction to null out a bad inode pointer
3833 * in an agi unlinked inode hash bucket.
3834 */
3835STATIC void
3836xlog_recover_clear_agi_bucket(
3837 xfs_mount_t *mp,
3838 xfs_agnumber_t agno,
3839 int bucket)
3840{
3841 xfs_trans_t *tp;
3842 xfs_agi_t *agi;
3843 xfs_buf_t *agibp;
3844 int offset;
3845 int error;
3846
3847 tp = xfs_trans_alloc(mp, XFS_TRANS_CLEAR_AGI_BUCKET);
Jie Liu3d3c8b52013-08-12 20:49:59 +10003848 error = xfs_trans_reserve(tp, &M_RES(mp)->tr_clearagi, 0, 0);
David Chinnere5720ee2008-04-10 12:21:18 +10003849 if (error)
3850 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003852 error = xfs_read_agi(mp, tp, agno, &agibp);
3853 if (error)
David Chinnere5720ee2008-04-10 12:21:18 +10003854 goto out_abort;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003856 agi = XFS_BUF_TO_AGI(agibp);
Christoph Hellwig16259e72005-11-02 15:11:25 +11003857 agi->agi_unlinked[bucket] = cpu_to_be32(NULLAGINO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 offset = offsetof(xfs_agi_t, agi_unlinked) +
3859 (sizeof(xfs_agino_t) * bucket);
3860 xfs_trans_log_buf(tp, agibp, offset,
3861 (offset + sizeof(xfs_agino_t) - 1));
3862
David Chinnere5720ee2008-04-10 12:21:18 +10003863 error = xfs_trans_commit(tp, 0);
3864 if (error)
3865 goto out_error;
3866 return;
3867
3868out_abort:
3869 xfs_trans_cancel(tp, XFS_TRANS_ABORT);
3870out_error:
Dave Chinnera0fa2b62011-03-07 10:01:35 +11003871 xfs_warn(mp, "%s: failed to clear agi %d. Continuing.", __func__, agno);
David Chinnere5720ee2008-04-10 12:21:18 +10003872 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873}
3874
Christoph Hellwig23fac502008-11-28 14:23:40 +11003875STATIC xfs_agino_t
3876xlog_recover_process_one_iunlink(
3877 struct xfs_mount *mp,
3878 xfs_agnumber_t agno,
3879 xfs_agino_t agino,
3880 int bucket)
3881{
3882 struct xfs_buf *ibp;
3883 struct xfs_dinode *dip;
3884 struct xfs_inode *ip;
3885 xfs_ino_t ino;
3886 int error;
3887
3888 ino = XFS_AGINO_TO_INO(mp, agno, agino);
Dave Chinner7b6259e2010-06-24 11:35:17 +10003889 error = xfs_iget(mp, NULL, ino, 0, 0, &ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003890 if (error)
3891 goto fail;
3892
3893 /*
3894 * Get the on disk inode to find the next inode in the bucket.
3895 */
Christoph Hellwig475ee412012-07-03 12:21:22 -04003896 error = xfs_imap_to_bp(mp, NULL, &ip->i_imap, &dip, &ibp, 0, 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003897 if (error)
Christoph Hellwig0e446672008-11-28 14:23:42 +11003898 goto fail_iput;
Christoph Hellwig23fac502008-11-28 14:23:40 +11003899
Christoph Hellwig23fac502008-11-28 14:23:40 +11003900 ASSERT(ip->i_d.di_nlink == 0);
Christoph Hellwig0e446672008-11-28 14:23:42 +11003901 ASSERT(ip->i_d.di_mode != 0);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003902
3903 /* setup for the next pass */
3904 agino = be32_to_cpu(dip->di_next_unlinked);
3905 xfs_buf_relse(ibp);
3906
3907 /*
3908 * Prevent any DMAPI event from being sent when the reference on
3909 * the inode is dropped.
3910 */
3911 ip->i_d.di_dmevmask = 0;
3912
Christoph Hellwig0e446672008-11-28 14:23:42 +11003913 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003914 return agino;
3915
Christoph Hellwig0e446672008-11-28 14:23:42 +11003916 fail_iput:
3917 IRELE(ip);
Christoph Hellwig23fac502008-11-28 14:23:40 +11003918 fail:
3919 /*
3920 * We can't read in the inode this bucket points to, or this inode
3921 * is messed up. Just ditch this bucket of inodes. We will lose
3922 * some inodes and space, but at least we won't hang.
3923 *
3924 * Call xlog_recover_clear_agi_bucket() to perform a transaction to
3925 * clear the inode pointer in the bucket.
3926 */
3927 xlog_recover_clear_agi_bucket(mp, agno, bucket);
3928 return NULLAGINO;
3929}
3930
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931/*
3932 * xlog_iunlink_recover
3933 *
3934 * This is called during recovery to process any inodes which
3935 * we unlinked but not freed when the system crashed. These
3936 * inodes will be on the lists in the AGI blocks. What we do
3937 * here is scan all the AGIs and fully truncate and free any
3938 * inodes found on the lists. Each inode is removed from the
3939 * lists when it has been fully truncated and is freed. The
3940 * freeing of the inode and its removal from the list must be
3941 * atomic.
3942 */
Eric Sandeend96f8f82009-07-02 00:09:33 -05003943STATIC void
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944xlog_recover_process_iunlinks(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05003945 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
3947 xfs_mount_t *mp;
3948 xfs_agnumber_t agno;
3949 xfs_agi_t *agi;
3950 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 xfs_agino_t agino;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 int bucket;
3953 int error;
3954 uint mp_dmevmask;
3955
3956 mp = log->l_mp;
3957
3958 /*
3959 * Prevent any DMAPI event from being sent while in this function.
3960 */
3961 mp_dmevmask = mp->m_dmevmask;
3962 mp->m_dmevmask = 0;
3963
3964 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
3965 /*
3966 * Find the agi for this ag.
3967 */
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11003968 error = xfs_read_agi(mp, NULL, agno, &agibp);
3969 if (error) {
3970 /*
3971 * AGI is b0rked. Don't process it.
3972 *
3973 * We should probably mark the filesystem as corrupt
3974 * after we've recovered all the ag's we can....
3975 */
3976 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 }
Jan Karad97d32e2012-03-15 09:34:02 +00003978 /*
3979 * Unlock the buffer so that it can be acquired in the normal
3980 * course of the transaction to truncate and free each inode.
3981 * Because we are not racing with anyone else here for the AGI
3982 * buffer, we don't even need to hold it locked to read the
3983 * initial unlinked bucket entries out of the buffer. We keep
3984 * buffer reference though, so that it stays pinned in memory
3985 * while we need the buffer.
3986 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 agi = XFS_BUF_TO_AGI(agibp);
Jan Karad97d32e2012-03-15 09:34:02 +00003988 xfs_buf_unlock(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
3990 for (bucket = 0; bucket < XFS_AGI_UNLINKED_BUCKETS; bucket++) {
Christoph Hellwig16259e72005-11-02 15:11:25 +11003991 agino = be32_to_cpu(agi->agi_unlinked[bucket]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 while (agino != NULLAGINO) {
Christoph Hellwig23fac502008-11-28 14:23:40 +11003993 agino = xlog_recover_process_one_iunlink(mp,
3994 agno, agino, bucket);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 }
3996 }
Jan Karad97d32e2012-03-15 09:34:02 +00003997 xfs_buf_rele(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 }
3999
4000 mp->m_dmevmask = mp_dmevmask;
4001}
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003/*
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004004 * Upack the log buffer data and crc check it. If the check fails, issue a
4005 * warning if and only if the CRC in the header is non-zero. This makes the
4006 * check an advisory warning, and the zero CRC check will prevent failure
4007 * warnings from being emitted when upgrading the kernel from one that does not
4008 * add CRCs by default.
4009 *
4010 * When filesystems are CRC enabled, this CRC mismatch becomes a fatal log
4011 * corruption failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004013STATIC int
4014xlog_unpack_data_crc(
4015 struct xlog_rec_header *rhead,
4016 xfs_caddr_t dp,
4017 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018{
Dave Chinnerf9668a02012-11-28 13:01:03 +11004019 __le32 crc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004021 crc = xlog_cksum(log, rhead, dp, be32_to_cpu(rhead->h_len));
4022 if (crc != rhead->h_crc) {
4023 if (rhead->h_crc || xfs_sb_version_hascrc(&log->l_mp->m_sb)) {
4024 xfs_alert(log->l_mp,
Eric Sandeen08e96e12013-10-11 20:59:05 -05004025 "log record CRC mismatch: found 0x%x, expected 0x%x.",
Dave Chinnerf9668a02012-11-28 13:01:03 +11004026 le32_to_cpu(rhead->h_crc),
4027 le32_to_cpu(crc));
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004028 xfs_hex_dump(dp, 32);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
4030
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004031 /*
4032 * If we've detected a log record corruption, then we can't
4033 * recover past this point. Abort recovery if we are enforcing
4034 * CRC protection by punting an error back up the stack.
4035 */
4036 if (xfs_sb_version_hascrc(&log->l_mp->m_sb))
Dave Chinner24513372014-06-25 14:58:08 +10004037 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 }
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004039
4040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041}
4042
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004043STATIC int
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044xlog_unpack_data(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004045 struct xlog_rec_header *rhead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 xfs_caddr_t dp,
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004047 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048{
4049 int i, j, k;
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004050 int error;
4051
4052 error = xlog_unpack_data_crc(rhead, dp, log);
4053 if (error)
4054 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004056 for (i = 0; i < BTOBB(be32_to_cpu(rhead->h_len)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 i < (XLOG_HEADER_CYCLE_SIZE / BBSIZE); i++) {
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004058 *(__be32 *)dp = *(__be32 *)&rhead->h_cycle_data[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 dp += BBSIZE;
4060 }
4061
Eric Sandeen62118702008-03-06 13:44:28 +11004062 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Christoph Hellwigb28708d2008-11-28 14:23:38 +11004063 xlog_in_core_2_t *xhdr = (xlog_in_core_2_t *)rhead;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004064 for ( ; i < BTOBB(be32_to_cpu(rhead->h_len)); i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 j = i / (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
4066 k = i % (XLOG_HEADER_CYCLE_SIZE / BBSIZE);
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004067 *(__be32 *)dp = xhdr[j].hic_xheader.xh_cycle_data[k];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 dp += BBSIZE;
4069 }
4070 }
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004071
4072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
4075STATIC int
4076xlog_valid_rec_header(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004077 struct xlog *log,
4078 struct xlog_rec_header *rhead,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 xfs_daddr_t blkno)
4080{
4081 int hlen;
4082
Christoph Hellwig69ef9212011-07-08 14:36:05 +02004083 if (unlikely(rhead->h_magicno != cpu_to_be32(XLOG_HEADER_MAGIC_NUM))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 XFS_ERROR_REPORT("xlog_valid_rec_header(1)",
4085 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10004086 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 }
4088 if (unlikely(
4089 (!rhead->h_version ||
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004090 (be32_to_cpu(rhead->h_version) & (~XLOG_VERSION_OKBITS))))) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004091 xfs_warn(log->l_mp, "%s: unrecognised log version (%d).",
Harvey Harrison34a622b2008-04-10 12:19:21 +10004092 __func__, be32_to_cpu(rhead->h_version));
Dave Chinner24513372014-06-25 14:58:08 +10004093 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 }
4095
4096 /* LR body must have data or it wouldn't have been written */
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004097 hlen = be32_to_cpu(rhead->h_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (unlikely( hlen <= 0 || hlen > INT_MAX )) {
4099 XFS_ERROR_REPORT("xlog_valid_rec_header(2)",
4100 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10004101 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 }
4103 if (unlikely( blkno > log->l_logBBsize || blkno > INT_MAX )) {
4104 XFS_ERROR_REPORT("xlog_valid_rec_header(3)",
4105 XFS_ERRLEVEL_LOW, log->l_mp);
Dave Chinner24513372014-06-25 14:58:08 +10004106 return -EFSCORRUPTED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 }
4108 return 0;
4109}
4110
4111/*
4112 * Read the log from tail to head and process the log records found.
4113 * Handle the two cases where the tail and head are in the same cycle
4114 * and where the active portion of the log wraps around the end of
4115 * the physical log separately. The pass parameter is passed through
4116 * to the routines called to process the data and is not looked at
4117 * here.
4118 */
4119STATIC int
4120xlog_do_recovery_pass(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004121 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 xfs_daddr_t head_blk,
4123 xfs_daddr_t tail_blk,
4124 int pass)
4125{
4126 xlog_rec_header_t *rhead;
4127 xfs_daddr_t blk_no;
Andy Polingfc5bc4c2009-11-03 17:26:47 +00004128 xfs_caddr_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 xfs_buf_t *hbp, *dbp;
4130 int error = 0, h_size;
4131 int bblks, split_bblks;
4132 int hblks, split_hblks, wrapped_hblks;
Dave Chinnerf0a76952010-01-11 11:49:57 +00004133 struct hlist_head rhash[XLOG_RHASH_SIZE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
4135 ASSERT(head_blk != tail_blk);
4136
4137 /*
4138 * Read the header of the tail block and get the iclog buffer size from
4139 * h_size. Use this to tell how many sectors make up the log header.
4140 */
Eric Sandeen62118702008-03-06 13:44:28 +11004141 if (xfs_sb_version_haslogv2(&log->l_mp->m_sb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 /*
4143 * When using variable length iclogs, read first sector of
4144 * iclog header and extract the header size from it. Get a
4145 * new hbp that is the correct size.
4146 */
4147 hbp = xlog_get_bp(log, 1);
4148 if (!hbp)
Dave Chinner24513372014-06-25 14:58:08 +10004149 return -ENOMEM;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004150
4151 error = xlog_bread(log, tail_blk, 1, hbp, &offset);
4152 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 goto bread_err1;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004154
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 rhead = (xlog_rec_header_t *)offset;
4156 error = xlog_valid_rec_header(log, rhead, tail_blk);
4157 if (error)
4158 goto bread_err1;
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004159 h_size = be32_to_cpu(rhead->h_size);
4160 if ((be32_to_cpu(rhead->h_version) & XLOG_VERSION_2) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 (h_size > XLOG_HEADER_CYCLE_SIZE)) {
4162 hblks = h_size / XLOG_HEADER_CYCLE_SIZE;
4163 if (h_size % XLOG_HEADER_CYCLE_SIZE)
4164 hblks++;
4165 xlog_put_bp(hbp);
4166 hbp = xlog_get_bp(log, hblks);
4167 } else {
4168 hblks = 1;
4169 }
4170 } else {
Alex Elder69ce58f2010-04-20 17:09:59 +10004171 ASSERT(log->l_sectBBsize == 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 hblks = 1;
4173 hbp = xlog_get_bp(log, 1);
4174 h_size = XLOG_BIG_RECORD_BSIZE;
4175 }
4176
4177 if (!hbp)
Dave Chinner24513372014-06-25 14:58:08 +10004178 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 dbp = xlog_get_bp(log, BTOBB(h_size));
4180 if (!dbp) {
4181 xlog_put_bp(hbp);
Dave Chinner24513372014-06-25 14:58:08 +10004182 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 }
4184
4185 memset(rhash, 0, sizeof(rhash));
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004186 blk_no = tail_blk;
4187 if (tail_blk > head_blk) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 /*
4189 * Perform recovery around the end of the physical log.
4190 * When the head is not on the same cycle number as the tail,
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004191 * we can't do a sequential recovery.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 while (blk_no < log->l_logBBsize) {
4194 /*
4195 * Check for header wrapping around physical end-of-log
4196 */
Chandra Seetharaman62926042011-07-22 23:40:15 +00004197 offset = hbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 split_hblks = 0;
4199 wrapped_hblks = 0;
4200 if (blk_no + hblks <= log->l_logBBsize) {
4201 /* Read header in one read */
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004202 error = xlog_bread(log, blk_no, hblks, hbp,
4203 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 if (error)
4205 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 } else {
4207 /* This LR is split across physical log end */
4208 if (blk_no != log->l_logBBsize) {
4209 /* some data before physical log end */
4210 ASSERT(blk_no <= INT_MAX);
4211 split_hblks = log->l_logBBsize - (int)blk_no;
4212 ASSERT(split_hblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004213 error = xlog_bread(log, blk_no,
4214 split_hblks, hbp,
4215 &offset);
4216 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004219
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 /*
4221 * Note: this black magic still works with
4222 * large sector sizes (non-512) only because:
4223 * - we increased the buffer size originally
4224 * by 1 sector giving us enough extra space
4225 * for the second read;
4226 * - the log start is guaranteed to be sector
4227 * aligned;
4228 * - we read the log end (LR header start)
4229 * _first_, then the log start (LR header end)
4230 * - order is important.
4231 */
David Chinner234f56a2008-04-10 12:24:24 +10004232 wrapped_hblks = hblks - split_hblks;
Dave Chinner44396472011-04-21 09:34:27 +00004233 error = xlog_bread_offset(log, 0,
4234 wrapped_hblks, hbp,
4235 offset + BBTOB(split_hblks));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 if (error)
4237 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 }
4239 rhead = (xlog_rec_header_t *)offset;
4240 error = xlog_valid_rec_header(log, rhead,
4241 split_hblks ? blk_no : 0);
4242 if (error)
4243 goto bread_err2;
4244
Christoph Hellwigb53e6752007-10-12 10:59:34 +10004245 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 blk_no += hblks;
4247
4248 /* Read in data for log record */
4249 if (blk_no + bblks <= log->l_logBBsize) {
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004250 error = xlog_bread(log, blk_no, bblks, dbp,
4251 &offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 if (error)
4253 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 } else {
4255 /* This log record is split across the
4256 * physical end of log */
Chandra Seetharaman62926042011-07-22 23:40:15 +00004257 offset = dbp->b_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 split_bblks = 0;
4259 if (blk_no != log->l_logBBsize) {
4260 /* some data is before the physical
4261 * end of log */
4262 ASSERT(!wrapped_hblks);
4263 ASSERT(blk_no <= INT_MAX);
4264 split_bblks =
4265 log->l_logBBsize - (int)blk_no;
4266 ASSERT(split_bblks > 0);
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004267 error = xlog_bread(log, blk_no,
4268 split_bblks, dbp,
4269 &offset);
4270 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 }
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004273
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 /*
4275 * Note: this black magic still works with
4276 * large sector sizes (non-512) only because:
4277 * - we increased the buffer size originally
4278 * by 1 sector giving us enough extra space
4279 * for the second read;
4280 * - the log start is guaranteed to be sector
4281 * aligned;
4282 * - we read the log end (LR header start)
4283 * _first_, then the log start (LR header end)
4284 * - order is important.
4285 */
Dave Chinner44396472011-04-21 09:34:27 +00004286 error = xlog_bread_offset(log, 0,
Dave Chinner009507b2012-11-02 11:38:44 +11004287 bblks - split_bblks, dbp,
Dave Chinner44396472011-04-21 09:34:27 +00004288 offset + BBTOB(split_bblks));
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004289 if (error)
4290 goto bread_err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 }
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004292
4293 error = xlog_unpack_data(rhead, offset, log);
4294 if (error)
4295 goto bread_err2;
4296
4297 error = xlog_recover_process_data(log, rhash,
4298 rhead, offset, pass);
4299 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 goto bread_err2;
4301 blk_no += bblks;
4302 }
4303
4304 ASSERT(blk_no >= log->l_logBBsize);
4305 blk_no -= log->l_logBBsize;
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004306 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004308 /* read first part of physical log */
4309 while (blk_no < head_blk) {
4310 error = xlog_bread(log, blk_no, hblks, hbp, &offset);
4311 if (error)
4312 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004313
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004314 rhead = (xlog_rec_header_t *)offset;
4315 error = xlog_valid_rec_header(log, rhead, blk_no);
4316 if (error)
4317 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004318
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004319 /* blocks in data section */
4320 bblks = (int)BTOBB(be32_to_cpu(rhead->h_len));
4321 error = xlog_bread(log, blk_no+hblks, bblks, dbp,
4322 &offset);
4323 if (error)
4324 goto bread_err2;
Christoph Hellwig076e6ac2009-03-16 08:24:13 +01004325
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004326 error = xlog_unpack_data(rhead, offset, log);
4327 if (error)
4328 goto bread_err2;
Christoph Hellwig0e446be2012-11-12 22:54:24 +11004329
Eric Sandeen970fd3f2014-09-09 11:57:29 +10004330 error = xlog_recover_process_data(log, rhash,
4331 rhead, offset, pass);
4332 if (error)
4333 goto bread_err2;
4334 blk_no += bblks + hblks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 }
4336
4337 bread_err2:
4338 xlog_put_bp(dbp);
4339 bread_err1:
4340 xlog_put_bp(hbp);
4341 return error;
4342}
4343
4344/*
4345 * Do the recovery of the log. We actually do this in two phases.
4346 * The two passes are necessary in order to implement the function
4347 * of cancelling a record written into the log. The first pass
4348 * determines those things which have been cancelled, and the
4349 * second pass replays log items normally except for those which
4350 * have been cancelled. The handling of the replay and cancellations
4351 * takes place in the log item type specific routines.
4352 *
4353 * The table of items which have cancel records in the log is allocated
4354 * and freed at this level, since only here do we know when all of
4355 * the log recovery has been completed.
4356 */
4357STATIC int
4358xlog_do_log_recovery(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004359 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 xfs_daddr_t head_blk,
4361 xfs_daddr_t tail_blk)
4362{
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00004363 int error, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
4365 ASSERT(head_blk != tail_blk);
4366
4367 /*
4368 * First do a pass to find all of the cancelled buf log items.
4369 * Store them in the buf_cancel_table for use in the second pass.
4370 */
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00004371 log->l_buf_cancel_table = kmem_zalloc(XLOG_BC_TABLE_SIZE *
4372 sizeof(struct list_head),
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 KM_SLEEP);
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00004374 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
4375 INIT_LIST_HEAD(&log->l_buf_cancel_table[i]);
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
4378 XLOG_RECOVER_PASS1);
4379 if (error != 0) {
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10004380 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 log->l_buf_cancel_table = NULL;
4382 return error;
4383 }
4384 /*
4385 * Then do a second pass to actually recover the items in the log.
4386 * When it is complete free the table of buf cancel items.
4387 */
4388 error = xlog_do_recovery_pass(log, head_blk, tail_blk,
4389 XLOG_RECOVER_PASS2);
4390#ifdef DEBUG
Tim Shimmin6d192a92006-06-09 14:55:38 +10004391 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 int i;
4393
4394 for (i = 0; i < XLOG_BC_TABLE_SIZE; i++)
Christoph Hellwigd5689ea2010-12-01 22:06:22 +00004395 ASSERT(list_empty(&log->l_buf_cancel_table[i]));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 }
4397#endif /* DEBUG */
4398
Denys Vlasenkof0e2d932008-05-19 16:31:57 +10004399 kmem_free(log->l_buf_cancel_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400 log->l_buf_cancel_table = NULL;
4401
4402 return error;
4403}
4404
4405/*
4406 * Do the actual recovery
4407 */
4408STATIC int
4409xlog_do_recover(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004410 struct xlog *log,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 xfs_daddr_t head_blk,
4412 xfs_daddr_t tail_blk)
4413{
4414 int error;
4415 xfs_buf_t *bp;
4416 xfs_sb_t *sbp;
4417
4418 /*
4419 * First replay the images in the log.
4420 */
4421 error = xlog_do_log_recovery(log, head_blk, tail_blk);
Christoph Hellwig43ff2122012-04-23 15:58:39 +10004422 if (error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424
4425 /*
4426 * If IO errors happened during recovery, bail out.
4427 */
4428 if (XFS_FORCED_SHUTDOWN(log->l_mp)) {
Dave Chinner24513372014-06-25 14:58:08 +10004429 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 }
4431
4432 /*
4433 * We now update the tail_lsn since much of the recovery has completed
4434 * and there may be space available to use. If there were no extent
4435 * or iunlinks, we can free up the entire log and set the tail_lsn to
4436 * be the last_sync_lsn. This was set in xlog_find_tail to be the
4437 * lsn of the last known good LR on disk. If there are extent frees
4438 * or iunlinks they will have some entries in the AIL; so we look at
4439 * the AIL to determine how to set the tail_lsn.
4440 */
4441 xlog_assign_tail_lsn(log->l_mp);
4442
4443 /*
4444 * Now that we've finished replaying all buffer and inode
Dave Chinner98021822012-11-12 22:54:03 +11004445 * updates, re-read in the superblock and reverify it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 */
4447 bp = xfs_getsb(log->l_mp, 0);
4448 XFS_BUF_UNDONE(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10004449 ASSERT(!(XFS_BUF_ISWRITE(bp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450 XFS_BUF_READ(bp);
Lachlan McIlroybebf9632007-10-15 13:18:02 +10004451 XFS_BUF_UNASYNC(bp);
Dave Chinner1813dd62012-11-14 17:54:40 +11004452 bp->b_ops = &xfs_sb_buf_ops;
Christoph Hellwig83a0adc2013-12-17 00:03:52 -08004453
Dave Chinner595bff72014-10-02 09:05:14 +10004454 error = xfs_buf_submit_wait(bp);
David Chinnerd64e31a2008-04-10 12:22:17 +10004455 if (error) {
Dave Chinner595bff72014-10-02 09:05:14 +10004456 if (!XFS_FORCED_SHUTDOWN(log->l_mp)) {
4457 xfs_buf_ioerror_alert(bp, __func__);
4458 ASSERT(0);
4459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 xfs_buf_relse(bp);
4461 return error;
4462 }
4463
4464 /* Convert superblock from on-disk format */
4465 sbp = &log->l_mp->m_sb;
Dave Chinner98021822012-11-12 22:54:03 +11004466 xfs_sb_from_disk(sbp, XFS_BUF_TO_SBP(bp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 ASSERT(sbp->sb_magicnum == XFS_SB_MAGIC);
Eric Sandeen62118702008-03-06 13:44:28 +11004468 ASSERT(xfs_sb_good_version(sbp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 xfs_buf_relse(bp);
4470
Lachlan McIlroy5478eea2007-02-10 18:36:29 +11004471 /* We've re-read the superblock so re-initialize per-cpu counters */
4472 xfs_icsb_reinit_counters(log->l_mp);
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 xlog_recover_check_summary(log);
4475
4476 /* Normal transactions can now occur */
4477 log->l_flags &= ~XLOG_ACTIVE_RECOVERY;
4478 return 0;
4479}
4480
4481/*
4482 * Perform recovery and re-initialize some log variables in xlog_find_tail.
4483 *
4484 * Return error or zero.
4485 */
4486int
4487xlog_recover(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004488 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489{
4490 xfs_daddr_t head_blk, tail_blk;
4491 int error;
4492
4493 /* find the tail of the log */
Eric Sandeen65be6052006-01-11 15:34:19 +11004494 if ((error = xlog_find_tail(log, &head_blk, &tail_blk)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return error;
4496
4497 if (tail_blk != head_blk) {
4498 /* There used to be a comment here:
4499 *
4500 * disallow recovery on read-only mounts. note -- mount
4501 * checks for ENOSPC and turns it into an intelligent
4502 * error message.
4503 * ...but this is no longer true. Now, unless you specify
4504 * NORECOVERY (in which case this function would never be
4505 * called), we just go ahead and recover. We do this all
4506 * under the vfs layer, so we can get away with it unless
4507 * the device itself is read-only, in which case we fail.
4508 */
Utako Kusaka3a02ee12007-05-08 13:50:06 +10004509 if ((error = xfs_dev_is_read_only(log->l_mp, "recovery"))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 return error;
4511 }
4512
Dave Chinnere721f502013-04-03 16:11:32 +11004513 /*
4514 * Version 5 superblock log feature mask validation. We know the
4515 * log is dirty so check if there are any unknown log features
4516 * in what we need to recover. If there are unknown features
4517 * (e.g. unsupported transactions, then simply reject the
4518 * attempt at recovery before touching anything.
4519 */
4520 if (XFS_SB_VERSION_NUM(&log->l_mp->m_sb) == XFS_SB_VERSION_5 &&
4521 xfs_sb_has_incompat_log_feature(&log->l_mp->m_sb,
4522 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN)) {
4523 xfs_warn(log->l_mp,
4524"Superblock has unknown incompatible log features (0x%x) enabled.\n"
4525"The log can not be fully and/or safely recovered by this kernel.\n"
4526"Please recover the log on a kernel that supports the unknown features.",
4527 (log->l_mp->m_sb.sb_features_log_incompat &
4528 XFS_SB_FEAT_INCOMPAT_LOG_UNKNOWN));
Dave Chinner24513372014-06-25 14:58:08 +10004529 return -EINVAL;
Dave Chinnere721f502013-04-03 16:11:32 +11004530 }
4531
Brian Foster2e227172014-09-09 11:56:13 +10004532 /*
4533 * Delay log recovery if the debug hook is set. This is debug
4534 * instrumention to coordinate simulation of I/O failures with
4535 * log recovery.
4536 */
4537 if (xfs_globals.log_recovery_delay) {
4538 xfs_notice(log->l_mp,
4539 "Delaying log recovery for %d seconds.",
4540 xfs_globals.log_recovery_delay);
4541 msleep(xfs_globals.log_recovery_delay * 1000);
4542 }
4543
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004544 xfs_notice(log->l_mp, "Starting recovery (logdev: %s)",
4545 log->l_mp->m_logname ? log->l_mp->m_logname
4546 : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547
4548 error = xlog_do_recover(log, head_blk, tail_blk);
4549 log->l_flags |= XLOG_RECOVERY_NEEDED;
4550 }
4551 return error;
4552}
4553
4554/*
4555 * In the first part of recovery we replay inodes and buffers and build
4556 * up the list of extent free items which need to be processed. Here
4557 * we process the extent free items and clean up the on disk unlinked
4558 * inode lists. This is separated from the first part of recovery so
4559 * that the root and real-time bitmap inodes can be read in from disk in
4560 * between the two stages. This is necessary so that we can free space
4561 * in the real-time portion of the file system.
4562 */
4563int
4564xlog_recover_finish(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004565 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566{
4567 /*
4568 * Now we're ready to do the transactions needed for the
4569 * rest of recovery. Start with completing all the extent
4570 * free intent records and then process the unlinked inode
4571 * lists. At this point, we essentially run in normal mode
4572 * except that we're still performing recovery actions
4573 * rather than accepting new requests.
4574 */
4575 if (log->l_flags & XLOG_RECOVERY_NEEDED) {
David Chinner3c1e2bb2008-04-10 12:21:11 +10004576 int error;
4577 error = xlog_recover_process_efis(log);
4578 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004579 xfs_alert(log->l_mp, "Failed to recover EFIs");
David Chinner3c1e2bb2008-04-10 12:21:11 +10004580 return error;
4581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 /*
4583 * Sync the log to get all the EFIs out of the AIL.
4584 * This isn't absolutely necessary, but it helps in
4585 * case the unlink transactions would have problems
4586 * pushing the EFIs out of the way.
4587 */
Christoph Hellwiga14a3482010-01-19 09:56:46 +00004588 xfs_log_force(log->l_mp, XFS_LOG_SYNC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589
Christoph Hellwig42490232008-08-13 16:49:32 +10004590 xlog_recover_process_iunlinks(log);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 xlog_recover_check_summary(log);
4593
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004594 xfs_notice(log->l_mp, "Ending recovery (logdev: %s)",
4595 log->l_mp->m_logname ? log->l_mp->m_logname
4596 : "internal");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 log->l_flags &= ~XLOG_RECOVERY_NEEDED;
4598 } else {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004599 xfs_info(log->l_mp, "Ending clean mount");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600 }
4601 return 0;
4602}
4603
4604
4605#if defined(DEBUG)
4606/*
4607 * Read all of the agf and agi counters and check that they
4608 * are consistent with the superblock counters.
4609 */
4610void
4611xlog_recover_check_summary(
Mark Tinguely9a8d2fd2012-06-14 09:22:16 -05004612 struct xlog *log)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
4614 xfs_mount_t *mp;
4615 xfs_agf_t *agfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616 xfs_buf_t *agfbp;
4617 xfs_buf_t *agibp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 xfs_agnumber_t agno;
4619 __uint64_t freeblks;
4620 __uint64_t itotal;
4621 __uint64_t ifree;
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004622 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623
4624 mp = log->l_mp;
4625
4626 freeblks = 0LL;
4627 itotal = 0LL;
4628 ifree = 0LL;
4629 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) {
From: Christoph Hellwig48056212008-11-28 14:23:38 +11004630 error = xfs_read_agf(mp, NULL, agno, 0, &agfbp);
4631 if (error) {
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004632 xfs_alert(mp, "%s agf read failed agno %d error %d",
4633 __func__, agno, error);
From: Christoph Hellwig48056212008-11-28 14:23:38 +11004634 } else {
4635 agfp = XFS_BUF_TO_AGF(agfbp);
4636 freeblks += be32_to_cpu(agfp->agf_freeblks) +
4637 be32_to_cpu(agfp->agf_flcount);
4638 xfs_buf_relse(agfbp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004641 error = xfs_read_agi(mp, NULL, agno, &agibp);
Dave Chinnera0fa2b62011-03-07 10:01:35 +11004642 if (error) {
4643 xfs_alert(mp, "%s agi read failed agno %d error %d",
4644 __func__, agno, error);
4645 } else {
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004646 struct xfs_agi *agi = XFS_BUF_TO_AGI(agibp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Christoph Hellwig5e1be0f2008-11-28 14:23:37 +11004648 itotal += be32_to_cpu(agi->agi_count);
4649 ifree += be32_to_cpu(agi->agi_freecount);
4650 xfs_buf_relse(agibp);
4651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654#endif /* DEBUG */